Otclient  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 "map.h"
25 #include "game.h"
26 #include <framework/core/clock.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  static float tf = Otc::ANIMATED_TEXT_DURATION;
40 
41  Point p = dest;
42  Size textSize = m_cachedText.getTextSize();
43  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  Rect rect(p, textSize);
53 
54  if(visibleRect.contains(rect)) {
55  //TODO: cache into a framebuffer
56  float t0 = tf / 1.2;
57  if(t > t0) {
58  Color color = m_color;
59  color.setAlpha((float)(1 - (t - t0) / (tf - t0)));
60  g_painter->setColor(color);
61  }
62  else
63  g_painter->setColor(m_color);
64  m_cachedText.draw(rect);
65  }
66 }
67 
69 {
70  m_animationTimer.restart();
71 
72  // schedule removal
73  auto self = asAnimatedText();
75 }
76 
77 void AnimatedText::setColor(int color)
78 {
79  m_color = Color::from8bit(color);
80 }
81 
82 void AnimatedText::setText(const std::string& text)
83 {
84  m_cachedText.setText(text);
85 }
86 
88 {
89  if(other->getColor() != m_color)
90  return false;
91 
92  if(other->getCachedText().getFont() != m_cachedText.getFont())
93  return false;
94 
95  if(m_animationTimer.ticksElapsed() > Otc::ANIMATED_TEXT_DURATION / 2.5)
96  return false;
97 
98  try {
99  int number = stdext::safe_cast<int>(m_cachedText.getText());
100  int otherNumber = stdext::safe_cast<int>(other->getCachedText().getText());
101 
102  std::string text = stdext::format("%d", number + otherNumber);
103  m_cachedText.setText(text);
104  return true;
105  }
106  catch(...) {
107  return false;
108  }
109 }
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:87
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:82
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:82
CachedText::setText
void setText(const std::string &text)
Definition: cachedtext.h:38
TSize::width
int width() const
Definition: size.h:43
clock.h
AnimatedText::onAppear
virtual void onAppear()
Definition: animatedtext.cpp:68
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:310
AnimatedText::setColor
void setColor(int color)
Definition: animatedtext.cpp:77
Otc::ANIMATED_TEXT_DURATION
@ ANIMATED_TEXT_DURATION
Definition: const.h:39
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:377
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:177