2012-01-10 23:13:40 +01:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2012-01-10 23:13:40 +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 "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-07-14 03:10:24 +02:00
|
|
|
#include <framework/core/application.h>
|
2012-01-10 23:13:40 +01:00
|
|
|
|
|
|
|
void UIWidget::initText()
|
|
|
|
{
|
|
|
|
m_font = g_fonts.getDefaultFont();
|
|
|
|
m_textAlign = Fw::AlignCenter;
|
2012-03-20 20:10:04 +01:00
|
|
|
m_textCoordsBuffer.enableHardwareCaching();
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 00:14:00 +02:00
|
|
|
void UIWidget::updateText()
|
|
|
|
{
|
|
|
|
if(m_textWrap && m_rect.isValid())
|
|
|
|
m_drawText = m_font->wrapText(m_text, getWidth() - m_textOffset.x);
|
|
|
|
else
|
|
|
|
m_drawText = m_text;
|
|
|
|
|
|
|
|
// update rect size
|
2013-02-09 22:57:37 +01:00
|
|
|
if(!m_rect.isValid() || m_textHorizontalAutoResize || m_textVerticalAutoResize) {
|
2012-04-11 04:59:05 +02:00
|
|
|
Size textBoxSize = getTextSize();
|
2013-01-08 21:01:47 +01:00
|
|
|
textBoxSize += Size(m_padding.left + m_padding.right, m_padding.top + m_padding.bottom) + m_textOffset.toSize();
|
2012-03-26 20:33:00 +02:00
|
|
|
Size size = getSize();
|
2013-02-09 22:57:37 +01:00
|
|
|
if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap))
|
2012-04-11 04:59:05 +02:00
|
|
|
size.setWidth(textBoxSize.width());
|
2013-02-09 22:57:37 +01:00
|
|
|
if(size.height() <= 0 || m_textVerticalAutoResize)
|
2012-04-11 04:59:05 +02:00
|
|
|
size.setHeight(textBoxSize.height());
|
2012-03-26 20:33:00 +02:00
|
|
|
setSize(size);
|
2012-03-26 00:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_textMustRecache = true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 23:13:40 +01:00
|
|
|
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>());
|
2012-03-26 00:14:00 +02:00
|
|
|
else if(node->tag() == "text-wrap")
|
|
|
|
setTextWrap(node->value<bool>());
|
|
|
|
else if(node->tag() == "text-auto-resize")
|
|
|
|
setTextAutoResize(node->value<bool>());
|
2013-02-09 22:57:37 +01:00
|
|
|
else if(node->tag() == "text-horizontal-auto-resize")
|
|
|
|
setTextHorizontalAutoResize(node->value<bool>());
|
|
|
|
else if(node->tag() == "text-vertical-auto-resize")
|
|
|
|
setTextVerticalAutoResize(node->value<bool>());
|
2012-08-05 20:26:08 +02:00
|
|
|
else if(node->tag() == "text-only-upper-case")
|
|
|
|
setTextOnlyUpperCase(node->value<bool>());
|
2012-01-10 23:13:40 +01:00
|
|
|
else if(node->tag() == "font")
|
|
|
|
setFont(node->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::drawText(const Rect& screenCoords)
|
|
|
|
{
|
2012-03-26 00:14:00 +02:00
|
|
|
if(m_drawText.length() == 0 || m_color.aF() == 0.0f)
|
2012-01-17 06:36:25 +01:00
|
|
|
return;
|
|
|
|
|
2012-03-20 20:10:04 +01:00
|
|
|
if(screenCoords != m_textCachedScreenCoords || m_textMustRecache) {
|
2013-01-08 21:01:47 +01:00
|
|
|
Rect coords = Rect(screenCoords.topLeft() + m_textOffset, screenCoords.bottomRight());
|
2012-01-17 06:36:25 +01:00
|
|
|
m_textMustRecache = false;
|
2013-01-08 21:01:47 +01:00
|
|
|
m_textCachedScreenCoords = coords;
|
2012-03-20 20:10:04 +01:00
|
|
|
|
|
|
|
m_textCoordsBuffer.clear();
|
2012-03-21 13:41:43 +01:00
|
|
|
|
2013-01-08 21:01:47 +01:00
|
|
|
m_font->calculateDrawTextCoords(m_textCoordsBuffer, m_drawText, coords, m_textAlign);
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
2012-01-17 06:36:25 +01:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(m_color);
|
2013-01-18 23:39:11 +01:00
|
|
|
|
|
|
|
if(m_font->getTexture())
|
|
|
|
g_painter->drawTextureCoords(m_textCoordsBuffer, m_font->getTexture());
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 12:59:55 +01:00
|
|
|
void UIWidget::onTextChange(const std::string& text, const std::string& oldText)
|
2012-01-10 23:13:40 +01:00
|
|
|
{
|
2012-06-20 02:15:56 +02:00
|
|
|
g_app.repaint();
|
2012-02-03 12:59:55 +01:00
|
|
|
callLuaField("onTextChange", text, oldText);
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::onFontChange(const std::string& font)
|
|
|
|
{
|
|
|
|
callLuaField("onFontChange", font);
|
|
|
|
}
|
|
|
|
|
2012-08-05 20:26:08 +02:00
|
|
|
void UIWidget::setText(std::string text)
|
2012-01-10 23:13:40 +01:00
|
|
|
{
|
2012-08-05 20:26:08 +02:00
|
|
|
if(m_textOnlyUpperCase)
|
2012-08-08 02:12:36 +02:00
|
|
|
stdext::toupper(text);
|
2012-08-05 20:26:08 +02:00
|
|
|
|
2012-01-17 06:36:25 +01:00
|
|
|
if(m_text == text)
|
|
|
|
return;
|
2012-01-10 23:13:40 +01:00
|
|
|
|
2012-02-03 12:59:55 +01:00
|
|
|
std::string oldText = m_text;
|
2012-01-17 06:36:25 +01:00
|
|
|
m_text = text;
|
2012-03-26 00:14:00 +02:00
|
|
|
updateText();
|
2012-01-17 06:36:25 +01:00
|
|
|
|
2013-01-20 16:40:40 +01:00
|
|
|
text = m_text;
|
2012-02-03 12:59:55 +01:00
|
|
|
onTextChange(text, oldText);
|
2012-01-10 23:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIWidget::setFont(const std::string& fontName)
|
|
|
|
{
|
|
|
|
m_font = g_fonts.getFont(fontName);
|
2012-03-26 00:14:00 +02:00
|
|
|
updateText();
|
2012-01-10 23:13:40 +01:00
|
|
|
onFontChange(fontName);
|
|
|
|
}
|