Otclient  14/8/2020
statictext.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 "statictext.h"
24 #include "map.h"
25 #include <framework/core/clock.h>
29 
31 {
32  m_mode = Otc::MessageNone;
33  m_color = Color::white;
34  m_cachedText.setFont(g_fonts.getFont("verdana-11px-rounded"));
35  m_cachedText.setAlign(Fw::AlignCenter);
36 }
37 
38 void StaticText::drawText(const Point& dest, const Rect& parentRect)
39 {
40  Size textSize = m_cachedText.getTextSize();
41  Rect rect = Rect(dest - Point(textSize.width() / 2, textSize.height()) + Point(20, 5), textSize);
42  Rect boundRect = rect;
43  boundRect.bind(parentRect);
44 
45  // draw only if the real center is not too far from the parent center, or its a yell
46  //if(g_map.isAwareOfPosition(m_position) || isYell()) {
47  g_painter->setColor(m_color);
48  m_cachedText.draw(boundRect);
49  //}
50 }
51 
52 void StaticText::setFont(const std::string& fontName)
53 {
54  m_cachedText.setFont(g_fonts.getFont(fontName));
55 }
56 
57 void StaticText::setText(const std::string& text)
58 {
59  m_cachedText.setText(text);
60 }
61 
62 bool StaticText::addMessage(const std::string& name, Otc::MessageMode mode, const std::string& text)
63 {
64  //TODO: this could be moved to lua
65  // first message
66  if(m_messages.empty()) {
67  m_name = name;
68  m_mode = mode;
69  }
70  // check if we can really own the message
71  else if(m_name != name || m_mode != mode) {
72  return false;
73  }
74  // too many messages
75  else if(m_messages.size() > 10) {
76  m_messages.pop_front();
77  m_updateEvent->cancel();
78  m_updateEvent = nullptr;
79  }
80 
81  int delay = std::max<int>(Otc::STATIC_DURATION_PER_CHARACTER * text.length(), Otc::MIN_STATIC_TEXT_DURATION);
82  if(isYell())
83  delay *= 2;
84 
85  m_messages.emplace_back(text, g_clock.millis() + delay);
86  compose();
87 
88  if(!m_updateEvent)
89  scheduleUpdate();
90 
91  return true;
92 }
93 
94 void StaticText::update()
95 {
96  m_messages.pop_front();
97  if(m_messages.empty()) {
98  // schedule removal
99  auto self = asStaticText();
100  g_dispatcher.addEvent([self]() { g_map.removeThing(self); });
101  } else {
102  compose();
103  scheduleUpdate();
104  }
105 }
106 
107 void StaticText::scheduleUpdate()
108 {
109  int delay = std::max<int>(m_messages.front().second - g_clock.millis(), 0);
110 
111  auto self = asStaticText();
112  m_updateEvent = g_dispatcher.scheduleEvent([self]() {
113  self->m_updateEvent = nullptr;
114  self->update();
115  }, delay);
116 }
117 
118 void StaticText::compose()
119 {
120  //TODO: this could be moved to lua
121  std::string text;
122 
123  if(m_mode == Otc::MessageSay) {
124  text += m_name;
125  text += " says:\n";
126  m_color = Color(239, 239, 0);
127  } else if(m_mode == Otc::MessageWhisper) {
128  text += m_name;
129  text += " whispers:\n";
130  m_color = Color(239, 239, 0);
131  } else if(m_mode == Otc::MessageYell) {
132  text += m_name;
133  text += " yells:\n";
134  m_color = Color(239, 239, 0);
135  } else if(m_mode == Otc::MessageMonsterSay || m_mode == Otc::MessageMonsterYell || m_mode == Otc::MessageSpell
136  || m_mode == Otc::MessageBarkLow || m_mode == Otc::MessageBarkLoud) {
137  m_color = Color(254, 101, 0);
138  } else if(m_mode == Otc::MessageNpcFrom || m_mode == Otc::MessageNpcFromStartBlock) {
139  text += m_name;
140  text += " says:\n";
141  m_color = Color(95, 247, 247);
142  } else {
143  g_logger.warning(stdext::format("Unknown speak type: %d", m_mode));
144  }
145 
146  for(uint i = 0; i < m_messages.size(); ++i) {
147  text += m_messages[i].first;
148 
149  if(i < m_messages.size() - 1)
150  text += "\n";
151  }
152 
153  m_cachedText.setText(text);
154  m_cachedText.wrapText(275);
155 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
CachedText::getTextSize
Size getTextSize()
Definition: cachedtext.h:41
eventdispatcher.h
graphics.h
g_map
Map g_map
Definition: map.cpp:36
Otc::MessageBarkLow
@ MessageBarkLow
Definition: const.h:321
Color
Definition: color.h:32
StaticText::setText
void setText(const std::string &text)
Definition: statictext.cpp:57
TRect< int >
Fw::AlignCenter
@ AlignCenter
Definition: const.h:208
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
g_fonts
FontManager g_fonts
Definition: fontmanager.cpp:29
TRect::bind
void bind(const TRect< T > &r)
Definition: rect.h:273
StaticText::addMessage
bool addMessage(const std::string &name, Otc::MessageMode mode, const std::string &text)
Definition: statictext.cpp:62
StaticText::asStaticText
StaticTextPtr asStaticText()
Definition: statictext.h:48
CachedText::wrapText
void wrapText(int maxWidth)
Definition: cachedtext.cpp:58
Otc::MessageSay
@ MessageSay
Definition: const.h:288
Otc::MIN_STATIC_TEXT_DURATION
@ MIN_STATIC_TEXT_DURATION
Definition: const.h:41
Otc::MessageNpcFrom
@ MessageNpcFrom
Definition: const.h:297
Point
TPoint< int > Point
Definition: point.h:86
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
Otc::STATIC_DURATION_PER_CHARACTER
@ STATIC_DURATION_PER_CHARACTER
Definition: const.h:40
clock.h
Otc::MessageYell
@ MessageYell
Definition: const.h:290
StaticText::isYell
bool isYell()
Definition: statictext.h:42
uint
unsigned int uint
Definition: types.h:31
Otc::MessageMode
MessageMode
Definition: const.h:286
g_logger
Logger g_logger
Definition: logger.cpp:35
Otc::MessageBarkLoud
@ MessageBarkLoud
Definition: const.h:322
Otc::MessageNone
@ MessageNone
Definition: const.h:287
Color::white
static const Color white
Definition: color.h:101
StaticText::StaticText
StaticText()
Definition: statictext.cpp:30
EventDispatcher::addEvent
EventPtr addEvent(const std::function< void()> &callback, bool pushFront=false)
Definition: eventdispatcher.cpp:104
CachedText::setAlign
void setAlign(Fw::AlignmentFlag align)
Definition: cachedtext.h:39
map.h
Rect
TRect< int > Rect
Definition: rect.h:319
CachedText::draw
void draw(const Rect &rect)
Definition: cachedtext.cpp:34
StaticText::drawText
void drawText(const Point &dest, const Rect &parentRect)
Definition: statictext.cpp:38
fontmanager.h
TSize::height
int height() const
Definition: size.h:44
Otc::MessageMonsterSay
@ MessageMonsterSay
Definition: const.h:333
FontManager::getFont
BitmapFontPtr getFont(const std::string &fontName)
Definition: fontmanager.cpp:90
Otc::MessageWhisper
@ MessageWhisper
Definition: const.h:289
g_painter
Painter * g_painter
Definition: painter.cpp:28
Clock::millis
ticks_t millis()
Definition: clock.h:37
statictext.h
TPoint< int >
StaticText::setFont
void setFont(const std::string &fontName)
Definition: statictext.cpp:52
Otc::MessageNpcFromStartBlock
@ MessageNpcFromStartBlock
Definition: const.h:340
EventDispatcher::scheduleEvent
ScheduledEventPtr scheduleEvent(const std::function< void()> &callback, int delay)
Definition: eventdispatcher.cpp:82
Otc::MessageMonsterYell
@ MessageMonsterYell
Definition: const.h:332
Event::cancel
void cancel()
Definition: event.cpp:49
TSize< int >
g_clock
Clock g_clock
Definition: clock.cpp:25
Map::removeThing
bool removeThing(const ThingPtr &thing)
Definition: map.cpp:177
Logger::warning
void warning(const std::string &what)
Definition: logger.h:53
Otc::MessageSpell
@ MessageSpell
Definition: const.h:296