Otclient 1.0  14/8/2020
animatedtext.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "animatedtext.h"
24 #include <framework/core/clock.h>
27 #include "game.h"
28 #include "map.h"
29 
31 {
32  m_cachedText.setFont(g_fonts.getFont("verdana-11px-rounded"));
33  m_cachedText.setAlign(Fw::AlignLeft);
34 }
35 
36 void AnimatedText::drawText(const Point& dest, const Rect& visibleRect)
37 {
38  const static float tf = Otc::ANIMATED_TEXT_DURATION;
39  const static float tftf = Otc::ANIMATED_TEXT_DURATION * Otc::ANIMATED_TEXT_DURATION;
40 
41  Point p = dest;
42  const Size textSize = m_cachedText.getTextSize();
43  const float t = m_animationTimer.ticksElapsed();
44  p.x += (24 - textSize.width() / 2);
45 
47  p.x -= (4 * t / tf) + (8 * t * t / tftf);
48  }
49 
50  p.y += 8 + (-48 * t) / tf;
51  p += m_offset;
52  const Rect rect(p, textSize);
53 
54  if(visibleRect.contains(rect)) {
55  //TODO: cache into a framebuffer
56  const float t0 = tf / 1.2;
57  if(t > t0) {
58  Color color = m_color;
59  color.setAlpha(static_cast<float>(1 - (t - t0) / (tf - t0)));
60  g_painter->setColor(color);
61  } else
62  g_painter->setColor(m_color);
63  m_cachedText.draw(rect);
64  }
65 }
66 
68 {
69  m_animationTimer.restart();
70 
71  // schedule removal
72  auto self = asAnimatedText();
74 }
75 
76 void AnimatedText::setColor(int color)
77 {
78  m_color = Color::from8bit(color);
79 }
80 
81 void AnimatedText::setText(const std::string& text)
82 {
83  m_cachedText.setText(text);
84 }
85 
87 {
88  if(other->getColor() != m_color)
89  return false;
90 
91  if(other->getCachedText().getFont() != m_cachedText.getFont())
92  return false;
93 
94  if(m_animationTimer.ticksElapsed() > Otc::ANIMATED_TEXT_DURATION / 2.5)
95  return false;
96 
97  try {
98  const int number = stdext::safe_cast<int>(m_cachedText.getText());
99  const int otherNumber = stdext::safe_cast<int>(other->getCachedText().getText());
100 
101  const std::string text = stdext::format("%d", number + otherNumber);
102  m_cachedText.setText(text);
103  return true;
104  } catch(...) {
105  return false;
106  }
107 }
TRect::contains
bool contains(const TPoint< T > &p, bool insideOnly=false) const
Definition: rect.h:141
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
CachedText::getTextSize
Size getTextSize()
Definition: cachedtext.h:41
Color::setAlpha
void setAlpha(int a)
Definition: color.h:59
AnimatedText::AnimatedText
AnimatedText()
Definition: animatedtext.cpp:30
eventdispatcher.h
graphics.h
g_map
Map g_map
Definition: map.cpp:36
Color
Definition: color.h:32
AnimatedText::merge
bool merge(const AnimatedTextPtr &other)
Definition: animatedtext.cpp:86
TPoint::y
T y
Definition: point.h:83
Timer::ticksElapsed
ticks_t ticksElapsed()
Definition: timer.cpp:33
AnimatedText::setText
void setText(const std::string &text)
Definition: animatedtext.cpp:81
TRect< int >
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
g_fonts
FontManager g_fonts
Definition: fontmanager.cpp:29
CachedText::getFont
BitmapFontPtr getFont() const
Definition: cachedtext.h:43
g_game
Game g_game
Definition: game.cpp:37
CachedText::setFont
void setFont(const BitmapFontPtr &font)
Definition: cachedtext.h:37
stdext::format
std::string format()
Definition: format.h:84
CachedText::setText
void setText(const std::string &text)
Definition: cachedtext.h:38
TSize::width
int width() const
Definition: size.h:43
clock.h
CachedText::getText
std::string getText() const
Definition: cachedtext.h:42
CachedText::setAlign
void setAlign(Fw::AlignmentFlag align)
Definition: cachedtext.h:39
map.h
Timer::restart
void restart()
Definition: timer.cpp:27
TPoint::x
T x
Definition: point.h:83
Game::getFeature
bool getFeature(Otc::GameFeature feature)
Definition: game.h:312
AnimatedText::setColor
void setColor(int color)
Definition: animatedtext.cpp:76
AnimatedText::onAppear
void onAppear() override
Definition: animatedtext.cpp:67
Otc::ANIMATED_TEXT_DURATION
@ ANIMATED_TEXT_DURATION
Definition: const.h:47
CachedText::draw
void draw(const Rect &rect)
Definition: cachedtext.cpp:34
stdext::shared_object_ptr
Definition: shared_object.h:39
FontManager::getFont
BitmapFontPtr getFont(const std::string &fontName)
Definition: fontmanager.cpp:90
g_painter
Painter * g_painter
Definition: painter.cpp:28
game.h
Otc::GameDiagonalAnimatedText
@ GameDiagonalAnimatedText
Definition: const.h:399
Fw::AlignLeft
@ AlignLeft
Definition: const.h:194
TPoint< int >
AnimatedText::asAnimatedText
AnimatedTextPtr asAnimatedText()
Definition: animatedtext.h:50
Color::from8bit
static Color from8bit(int color)
Definition: color.h:90
EventDispatcher::scheduleEvent
ScheduledEventPtr scheduleEvent(const std::function< void()> &callback, int delay)
Definition: eventdispatcher.cpp:82
animatedtext.h
AnimatedText::drawText
void drawText(const Point &dest, const Rect &visibleRect)
Definition: animatedtext.cpp:36
TSize< int >
Map::removeThing
bool removeThing(const ThingPtr &thing)
Definition: map.cpp:198