2011-12-29 18:45:59 +01:00
|
|
|
/*
|
2014-04-01 07:36:42 +02:00
|
|
|
* Copyright (c) 2010-2014 OTClient <https://github.com/edubart/otclient>
|
2011-12-29 18:45:59 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "statictext.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include <framework/core/clock.h>
|
|
|
|
#include <framework/core/eventdispatcher.h>
|
|
|
|
#include <framework/graphics/graphics.h>
|
2012-06-22 05:14:13 +02:00
|
|
|
#include <framework/graphics/fontmanager.h>
|
2011-12-29 18:45:59 +01:00
|
|
|
|
|
|
|
StaticText::StaticText()
|
|
|
|
{
|
2013-01-09 20:29:58 +01:00
|
|
|
m_mode = Otc::MessageNone;
|
|
|
|
m_color = Color::white;
|
2012-06-09 02:40:22 +02:00
|
|
|
m_cachedText.setFont(g_fonts.getFont("verdana-11px-rounded"));
|
|
|
|
m_cachedText.setAlign(Fw::AlignCenter);
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2012-06-17 23:47:05 +02:00
|
|
|
void StaticText::drawText(const Point& dest, const Rect& parentRect)
|
2011-12-29 18:45:59 +01:00
|
|
|
{
|
2012-06-09 02:40:22 +02:00
|
|
|
Size textSize = m_cachedText.getTextSize();
|
|
|
|
Rect rect = Rect(dest - Point(textSize.width() / 2, textSize.height()) + Point(20, 5), textSize);
|
2012-01-25 15:56:17 +01:00
|
|
|
Rect boundRect = rect;
|
2012-01-30 01:00:12 +01:00
|
|
|
boundRect.bind(parentRect);
|
|
|
|
|
2012-02-02 22:28:53 +01:00
|
|
|
// draw only if the real center is not too far from the parent center, or its a yell
|
2012-06-16 00:18:30 +02:00
|
|
|
//if(g_map.isAwareOfPosition(m_position) || isYell()) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(m_color);
|
2012-06-09 02:40:22 +02:00
|
|
|
m_cachedText.draw(boundRect);
|
2012-06-16 00:18:30 +02:00
|
|
|
//}
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2013-01-09 20:29:58 +01:00
|
|
|
void StaticText::setFont(const std::string& fontName)
|
|
|
|
{
|
|
|
|
m_cachedText.setFont(g_fonts.getFont(fontName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticText::setText(const std::string& text)
|
|
|
|
{
|
|
|
|
m_cachedText.setText(text);
|
|
|
|
}
|
|
|
|
|
2012-07-26 11:12:20 +02:00
|
|
|
bool StaticText::addMessage(const std::string& name, Otc::MessageMode mode, const std::string& text)
|
2011-12-29 18:45:59 +01:00
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
//TODO: this could be moved to lua
|
2012-06-16 00:18:30 +02:00
|
|
|
// first message
|
2011-12-29 18:45:59 +01:00
|
|
|
if(m_messages.size() == 0) {
|
|
|
|
m_name = name;
|
2012-07-26 11:12:20 +02:00
|
|
|
m_mode = mode;
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
2012-06-16 00:18:30 +02:00
|
|
|
// check if we can really own the message
|
2012-07-26 11:12:20 +02:00
|
|
|
else if(m_name != name || m_mode != mode) {
|
2012-06-15 00:27:57 +02:00
|
|
|
return false;
|
2012-06-16 00:18:30 +02:00
|
|
|
}
|
|
|
|
// too many messages
|
|
|
|
else if(m_messages.size() > 10) {
|
|
|
|
m_messages.pop_front();
|
|
|
|
m_updateEvent->cancel();
|
|
|
|
m_updateEvent = nullptr;
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:53:54 +01:00
|
|
|
int delay = std::max<int>(Otc::STATIC_DURATION_PER_CHARACTER * text.length(), Otc::MIN_STATIC_TEXT_DURATION);
|
2012-07-27 00:56:46 +02:00
|
|
|
if(isYell())
|
|
|
|
delay *= 2;
|
|
|
|
|
|
|
|
m_messages.push_back(std::make_pair(text, g_clock.millis() + delay));
|
2011-12-29 18:45:59 +01:00
|
|
|
compose();
|
|
|
|
|
2012-06-16 00:18:30 +02:00
|
|
|
if(!m_updateEvent)
|
|
|
|
scheduleUpdate();
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2011-12-29 18:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-16 00:18:30 +02:00
|
|
|
void StaticText::update()
|
2011-12-29 18:45:59 +01:00
|
|
|
{
|
2012-06-15 00:27:57 +02:00
|
|
|
m_messages.pop_front();
|
2011-12-29 18:45:59 +01:00
|
|
|
if(m_messages.empty()) {
|
|
|
|
// schedule removal
|
|
|
|
auto self = asStaticText();
|
2012-06-26 00:13:30 +02:00
|
|
|
g_dispatcher.addEvent([self]() { g_map.removeThing(self); });
|
2012-06-16 00:18:30 +02:00
|
|
|
} else {
|
2011-12-29 18:45:59 +01:00
|
|
|
compose();
|
2012-06-16 00:18:30 +02:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StaticText::scheduleUpdate()
|
|
|
|
{
|
2013-11-13 22:53:54 +01:00
|
|
|
int delay = std::max<int>(m_messages.front().second - g_clock.millis(), 0);
|
2012-06-16 00:18:30 +02:00
|
|
|
|
|
|
|
auto self = asStaticText();
|
2012-06-26 00:13:30 +02:00
|
|
|
m_updateEvent = g_dispatcher.scheduleEvent([self]() {
|
2012-06-16 00:18:30 +02:00
|
|
|
self->m_updateEvent = nullptr;
|
|
|
|
self->update();
|
|
|
|
}, delay);
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StaticText::compose()
|
|
|
|
{
|
2012-01-30 01:00:12 +01:00
|
|
|
//TODO: this could be moved to lua
|
2012-01-25 15:56:17 +01:00
|
|
|
std::string text;
|
2011-12-29 18:45:59 +01:00
|
|
|
|
2012-07-26 11:12:20 +02:00
|
|
|
if(m_mode == Otc::MessageSay) {
|
2012-01-25 15:56:17 +01:00
|
|
|
text += m_name;
|
|
|
|
text += " says:\n";
|
2011-12-30 09:19:43 +01:00
|
|
|
m_color = Color(239, 239, 0);
|
2012-07-26 11:12:20 +02:00
|
|
|
} else if(m_mode == Otc::MessageWhisper) {
|
2012-01-25 15:56:17 +01:00
|
|
|
text += m_name;
|
|
|
|
text += " whispers:\n";
|
2011-12-30 09:19:43 +01:00
|
|
|
m_color = Color(239, 239, 0);
|
2012-07-26 11:12:20 +02:00
|
|
|
} else if(m_mode == Otc::MessageYell) {
|
2012-01-25 15:56:17 +01:00
|
|
|
text += m_name;
|
|
|
|
text += " yells:\n";
|
2011-12-30 09:19:43 +01:00
|
|
|
m_color = Color(239, 239, 0);
|
2012-07-30 14:29:13 +02:00
|
|
|
} else if(m_mode == Otc::MessageMonsterSay || m_mode == Otc::MessageMonsterYell || m_mode == Otc::MessageSpell || m_mode == Otc::MessageBarkLow || m_mode == Otc::MessageBarkLoud) {
|
2011-12-30 09:19:43 +01:00
|
|
|
m_color = Color(254, 101, 0);
|
2012-07-26 11:12:20 +02:00
|
|
|
} else if(m_mode == Otc::MessageNpcFrom) {
|
2012-01-25 15:56:17 +01:00
|
|
|
text += m_name;
|
|
|
|
text += " says:\n";
|
2011-12-30 09:19:43 +01:00
|
|
|
m_color = Color(95, 247, 247);
|
2012-01-30 01:00:12 +01:00
|
|
|
} else {
|
2012-07-26 11:12:20 +02:00
|
|
|
g_logger.warning(stdext::format("Unknown speak type: %d", m_mode));
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(uint i = 0; i < m_messages.size(); ++i) {
|
2012-07-27 00:56:46 +02:00
|
|
|
text += m_messages[i].first;
|
2011-12-29 18:45:59 +01:00
|
|
|
|
|
|
|
if(i < m_messages.size() - 1)
|
2012-01-25 15:56:17 +01:00
|
|
|
text += "\n";
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
m_cachedText.setText(text);
|
2012-06-16 00:18:30 +02:00
|
|
|
m_cachedText.wrapText(275);
|
2011-12-29 18:45:59 +01:00
|
|
|
}
|