/* * Copyright (c) 2010-2012 OTClient * * 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 #include #include StaticText::StaticText() { m_cachedText.setFont(g_fonts.getFont("verdana-11px-rounded")); m_cachedText.setAlign(Fw::AlignCenter); } void StaticText::draw(const Point& dest, const Rect& parentRect) { Size textSize = m_cachedText.getTextSize(); Rect rect = Rect(dest - Point(textSize.width() / 2, textSize.height()) + Point(20, 5), textSize); Rect boundRect = rect; boundRect.bind(parentRect); // draw only if the real center is not too far from the parent center, or its a yell if((boundRect.center() - rect.center()).length() < parentRect.width() / 15 || isYell()) { g_painter->setColor(m_color); m_cachedText.draw(boundRect); } } bool StaticText::addMessage(const std::string& name, Otc::SpeakType type, const std::string& message) { //TODO: this could be moved to lua // First message if(m_messages.size() == 0) { m_name = name; m_messageType = type; } else if(m_messages.size() > 20) { // to many messages, ignore to avoid lag return false; } else { // we can only add another message if it follows these conditions if(m_name != name || m_messageType != type) return false; } m_messages.push_back(message); compose(); auto self = asStaticText(); g_eventDispatcher.scheduleEvent([self]() { self->removeMessage(); }, std::max(Otc::STATIC_DURATION_PER_CHARACTER * message.length(), Otc::MIN_STATIC_TEXT_DURATION)); return true; } void StaticText::removeMessage() { m_messages.pop_front(); if(m_messages.empty()) { // schedule removal auto self = asStaticText(); g_eventDispatcher.addEvent([self]() { g_map.removeThing(self); }); } else compose(); } void StaticText::compose() { //TODO: this could be moved to lua std::string text; if(m_messageType == Otc::SpeakSay) { text += m_name; text += " says:\n"; m_color = Color(239, 239, 0); } else if(m_messageType == Otc::SpeakWhisper) { text += m_name; text += " whispers:\n"; m_color = Color(239, 239, 0); } else if(m_messageType == Otc::SpeakYell) { text += m_name; text += " yells:\n"; m_color = Color(239, 239, 0); } else if(m_messageType == Otc::SpeakMonsterSay || m_messageType == Otc::SpeakMonsterYell) { m_color = Color(254, 101, 0); } else if(m_messageType == Otc::SpeakPrivateNpcToPlayer) { text += m_name; text += " says:\n"; m_color = Color(95, 247, 247); } else { g_logger.warning(stdext::format("Unknown speak type: %d", m_messageType)); } for(uint i = 0; i < m_messages.size(); ++i) { text += m_messages[i]; if(i < m_messages.size() - 1) text += "\n"; } m_cachedText.setText(text); m_cachedText.wrapText(Otc::MAX_STATIC_TEXT_WIDTH); }