2012-01-10 23:13:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/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 "uiwidget.h"
|
|
|
|
#include "uitranslator.h"
|
|
|
|
#include <framework/graphics/fontmanager.h>
|
|
|
|
#include <framework/graphics/painter.h>
|
2012-01-17 06:36:25 +01:00
|
|
|
#include <framework/graphics/framebuffer.h>
|
2012-01-10 23:13:40 +01:00
|
|
|
|
|
|
|
void UIWidget::initText()
|
|
|
|
{
|
|
|
|
m_font = g_fonts.getDefaultFont();
|
|
|
|
m_textAlign = Fw::AlignCenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
|
|
|
|
{
|
|
|
|
for(const OTMLNodePtr& node : styleNode->children()) {
|
2012-01-11 00:13:38 +01:00
|
|
|
if(node->tag() == "text")
|
2012-01-10 23:13:40 +01:00
|
|
|
setText(node->value());
|
|
|
|
else if(node->tag() == "text-align")
|
|
|
|
setTextAlign(Fw::translateAlignment(node->value()));
|
|
|
|
else if(node->tag() == "text-offset")
|
|
|
|
setTextOffset(node->value<Point>());
|
|
|
|
else if(node->tag() == "font")
|
|
|
|
setFont(node->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::drawText(const Rect& screenCoords)
|
|
|
|
{
|
2012-01-17 06:36:25 +01:00
|
|
|
if(m_text.length() == 0 || m_color.a() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Size boxSize = screenCoords.size();
|
|
|
|
if(boxSize != m_textCachedBoxSize || m_textMustRecache) {
|
|
|
|
if(!m_textFramebuffer)
|
|
|
|
m_textFramebuffer = FrameBufferPtr(new FrameBuffer(boxSize));
|
|
|
|
else
|
|
|
|
m_textFramebuffer->resize(boxSize);
|
|
|
|
|
|
|
|
m_textFramebuffer->bind();
|
|
|
|
Rect virtualTextRect(0, 0, boxSize);
|
|
|
|
virtualTextRect.translate(m_textOffset);
|
2012-01-19 17:23:24 +01:00
|
|
|
g_painter.setCompositionMode(Painter::CompositionMode_DestBlending);
|
2012-01-17 06:36:25 +01:00
|
|
|
m_font->renderText(m_text, virtualTextRect, m_textAlign, Fw::white);
|
|
|
|
g_painter.resetCompositionMode();
|
|
|
|
m_textFramebuffer->release();
|
|
|
|
|
|
|
|
m_textMustRecache = false;
|
|
|
|
m_textCachedBoxSize = boxSize;
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
2012-01-17 06:36:25 +01:00
|
|
|
|
|
|
|
g_painter.setColor(m_color);
|
|
|
|
m_textFramebuffer->draw(screenCoords);
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::onTextChange(const std::string& text)
|
|
|
|
{
|
|
|
|
callLuaField("onTextChange", text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::onFontChange(const std::string& font)
|
|
|
|
{
|
|
|
|
callLuaField("onFontChange", font);
|
|
|
|
}
|
|
|
|
|
2012-01-25 01:50:30 +01:00
|
|
|
void UIWidget::wrapText()
|
|
|
|
{
|
|
|
|
setText(m_font->wrapText(m_text, getWidth()));
|
|
|
|
}
|
|
|
|
|
2012-01-10 23:13:40 +01:00
|
|
|
void UIWidget::setText(const std::string& text)
|
|
|
|
{
|
2012-01-17 06:36:25 +01:00
|
|
|
if(m_text == text)
|
|
|
|
return;
|
2012-01-10 23:13:40 +01:00
|
|
|
|
2012-01-17 06:36:25 +01:00
|
|
|
m_text = text;
|
2012-01-10 23:13:40 +01:00
|
|
|
|
2012-01-17 06:36:25 +01:00
|
|
|
// update rect size
|
|
|
|
if(!m_rect.isValid()) {
|
|
|
|
Size textSize = m_font->calculateTextRectSize(m_text);
|
|
|
|
Size newSize = getSize();
|
|
|
|
if(newSize.width() <= 0)
|
|
|
|
newSize.setWidth(textSize.width());
|
|
|
|
if(newSize.height() <= 0)
|
|
|
|
newSize.setHeight(textSize.height());
|
|
|
|
setSize(newSize);
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
2012-01-17 06:36:25 +01:00
|
|
|
|
|
|
|
onTextChange(text);
|
|
|
|
m_textMustRecache = true;
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::setFont(const std::string& fontName)
|
|
|
|
{
|
|
|
|
m_font = g_fonts.getFont(fontName);
|
|
|
|
onFontChange(fontName);
|
2012-01-17 06:36:25 +01:00
|
|
|
m_textMustRecache = true;
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|