statictext.cpp

main 170 lines · 5.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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 "spritemanager.h"
26#include <framework/core/clock.h>
27#include <framework/core/eventdispatcher.h>
28#include <framework/graphics/graphics.h>
29#include <framework/graphics/fontmanager.h>
30
31StaticText::StaticText()
32{
33 m_mode = Otc::MessageNone;
34 m_color = Color::white;
35 m_cachedText.setFont(g_fonts.getFont("verdana-11px-rounded"));
36 m_cachedText.setAlign(Fw::AlignCenter);
37}
38
39void StaticText::drawText(const Point& dest, const Rect& parentRect)
40{
41 Size textSize = m_cachedText.getTextSize();
42 Rect rect = Rect(dest - Point(textSize.width() / 2, textSize.height()) + Point(20, 5), textSize);
43 Rect boundRect = rect;
44 boundRect.bind(parentRect);
45
46 // draw only if the real center is not too far from the parent center, or its a yell
47 //if(g_map.isAwareOfPosition(m_position) || isYell()) {
48 m_cachedText.draw(boundRect, m_color);
49 //}
50}
51
52void StaticText::setFont(const std::string& fontName)
53{
54 m_cachedText.setFont(g_fonts.getFont(fontName));
55}
56
57void StaticText::setText(const std::string& text)
58{
59 m_cachedText.setText(text);
60}
61
62bool StaticText::addMessage(const std::string& name, Otc::MessageMode mode, const std::string& text)
63{
64 return addColoredMessage(name, mode, { text, "" });
65}
66
67bool StaticText::addColoredMessage(const std::string& name, Otc::MessageMode mode, const std::vector<std::string>& texts)
68{
69 if (texts.empty() || texts.size() % 2 != 0)
70 return false;
71 //TODO: this could be moved to lua
72 // first message
73 if (m_messages.size() == 0) {
74 m_name = name;
75 m_mode = mode;
76 }
77 // check if we can really own the message
78 else if (m_name != name || m_mode != mode) {
79 return false;
80 }
81 // too many messages
82 else if (m_messages.size() > 10) {
83 m_messages.pop_front();
84 m_updateEvent->cancel();
85 m_updateEvent = nullptr;
86 }
87
88 size_t len = 0;
89 for (size_t i = 0; i < texts.size(); i += 2) {
90 len += texts[i].length();
91 }
92
93 int delay = std::max<int>(Otc::STATIC_DURATION_PER_CHARACTER * len, Otc::MIN_STATIC_TEXT_DURATION);
94 if (isYell())
95 delay *= 2;
96
97 m_messages.push_back(StaticTextMessage{ texts, g_clock.millis() + delay });
98 compose();
99
100 if (!m_updateEvent)
101 scheduleUpdate();
102 return true;
103}
104
105void StaticText::update()
106{
107 m_messages.pop_front();
108 if(m_messages.empty()) {
109 // schedule removal
110 auto self = asStaticText();
111 g_dispatcher.addEvent([self]() { g_map.removeThing(self); });
112 } else {
113 compose();
114 scheduleUpdate();
115 }
116}
117
118void StaticText::scheduleUpdate()
119{
120 int delay = std::max<int>(m_messages.front().time - g_clock.millis(), 0);
121
122 auto self = asStaticText();
123 m_updateEvent = g_dispatcher.scheduleEvent([self]() {
124 self->m_updateEvent = nullptr;
125 self->update();
126 }, delay);
127}
128
129void StaticText::compose()
130{
131 //TODO: this could be moved to lua
132 std::vector<std::string> texts;
133
134 if(m_mode == Otc::MessageSay) {
135 texts.push_back(m_name + " says:\n");
136 texts.push_back("#EFEF00");
137 m_color = Color(239, 239, 0);
138 } else if(m_mode == Otc::MessageWhisper) {
139 texts.push_back(m_name + " whispers:\n");
140 texts.push_back("#EFEF00");
141 m_color = Color(239, 239, 0);
142 } else if(m_mode == Otc::MessageYell) {
143 texts.push_back(m_name + " yells:\n");
144 texts.push_back("#EFEF00");
145 m_color = Color(239, 239, 0);
146 } else if(m_mode == Otc::MessageMonsterSay || m_mode == Otc::MessageMonsterYell || m_mode == Otc::MessageSpell
147 || m_mode == Otc::MessageBarkLow || m_mode == Otc::MessageBarkLoud) {
148 m_color = Color(254, 101, 0);
149 } else if(m_mode == Otc::MessageNpcFrom || m_mode == Otc::MessageNpcFromStartBlock) {
150 texts.push_back(m_name + " says:\n");
151 texts.push_back("#5FF7F7");
152 m_color = Color(95, 247, 247);
153 } else {
154 g_logger.warning(stdext::format("Unknown speak type: %d", m_mode));
155 }
156
157 for(uint i = 0; i < m_messages.size(); ++i) {
158 for (size_t j = 0; j < m_messages[i].texts.size() - 1; j += 2) {
159 texts.push_back(m_messages[i].texts[j]);
160 texts.push_back(m_messages[i].texts[j + 1].empty() ? m_color.toHex() : m_messages[i].texts[j + 1]);
161 }
162 if (texts.size() >= 2 && i < m_messages.size() - 1) {
163 texts[texts.size() - 2] += "\n";
164 }
165 }
166
167 m_cachedText.setColoredText(texts);
168 m_cachedText.wrapText(275);
169}
170
Top