tibia-client/src/otclient/core/statictext.cpp

121 lines
4.0 KiB
C++
Raw Normal View History

2011-12-29 18:45:59 +01:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 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>
StaticText::StaticText()
{
m_font = g_fonts.getFont("verdana-11px-rounded");
}
2012-01-30 01:00:12 +01:00
void StaticText::draw(const Point& dest, const Rect& parentRect)
2011-12-29 18:45:59 +01:00
{
2012-01-30 01:00:12 +01:00
Rect rect = Rect(dest - Point(m_textSize.width() / 2, m_textSize.height()) + Point(20, 5), m_textSize);
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-02-03 01:25:18 +01:00
if((boundRect.center() - rect.center()).length() < parentRect.width() / 15 || isYell()) {
2012-01-30 01:00:12 +01:00
//TODO: cache into a framebuffer
m_font->renderText(m_text, boundRect, Fw::AlignCenter, m_color);
2012-01-30 01:00:12 +01:00
}
2011-12-29 18:45:59 +01:00
}
2012-02-03 01:25:18 +01:00
bool StaticText::addMessage(const std::string& name, Otc::SpeakType type, const std::string& message)
2011-12-29 18:45:59 +01:00
{
2012-01-30 01:00:12 +01:00
//TODO: this could be moved to lua
2011-12-29 18:45:59 +01:00
// First message
if(m_messages.size() == 0) {
m_name = name;
m_messageType = type;
2011-12-29 18:45:59 +01:00
}
else {
// we can only add another message if it follows these conditions
if(m_name != name || m_messageType != type)
2011-12-29 18:45:59 +01:00
return false;
}
m_messages.push_back(message);
compose();
auto self = asStaticText();
2012-03-14 19:45:15 +01:00
g_eventDispatcher.scheduleEvent([self]() {
2011-12-29 18:45:59 +01:00
self->removeMessage();
2012-01-30 01:00:12 +01:00
}, std::max<int>(Otc::STATIC_DURATION_PER_CHARACTER * message.length(), Otc::MIN_STATIC_TEXT_DURATION));
2011-12-29 18:45:59 +01:00
return true;
}
void StaticText::removeMessage()
{
m_messages.erase(m_messages.begin());
if(m_messages.empty()) {
// schedule removal
auto self = asStaticText();
2012-03-14 19:45:15 +01:00
g_eventDispatcher.addEvent([self]() { g_map.removeThing(self); });
2012-01-30 01:00:12 +01:00
} else
2011-12-29 18:45:59 +01:00
compose();
}
void StaticText::compose()
{
2012-01-30 01:00:12 +01:00
//TODO: this could be moved to lua
std::string text;
2011-12-29 18:45:59 +01:00
2012-02-03 01:25:18 +01:00
if(m_messageType == Otc::SpeakSay) {
text += m_name;
text += " says:\n";
m_color = Color(239, 239, 0);
2012-02-03 01:25:18 +01:00
} else if(m_messageType == Otc::SpeakWhisper) {
text += m_name;
text += " whispers:\n";
m_color = Color(239, 239, 0);
2012-02-03 01:25:18 +01:00
} else if(m_messageType == Otc::SpeakYell) {
text += m_name;
text += " yells:\n";
m_color = Color(239, 239, 0);
2012-02-03 01:25:18 +01:00
} else if(m_messageType == Otc::SpeakMonsterSay || m_messageType == Otc::SpeakMonsterYell) {
m_color = Color(254, 101, 0);
2012-02-03 01:25:18 +01:00
} else if(m_messageType == Otc::SpeakPrivateNpcToPlayer) {
text += m_name;
text += " says:\n";
m_color = Color(95, 247, 247);
2012-01-30 01:00:12 +01:00
} else {
logWarning("unknown speak type: ", m_messageType);
2011-12-29 18:45:59 +01:00
}
for(uint i = 0; i < m_messages.size(); ++i) {
text += m_messages[i];
2011-12-29 18:45:59 +01:00
if(i < m_messages.size() - 1)
text += "\n";
2011-12-29 18:45:59 +01:00
}
2012-01-30 01:00:12 +01:00
m_text = m_font->wrapText(text, Otc::MAX_STATIC_TEXT_WIDTH);
m_textSize = m_font->calculateTextRectSize(m_text);
2011-12-29 18:45:59 +01:00
}