Otclient  14/8/2020
uiwidgettext.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "uiwidget.h"
24 #include "uitranslator.h"
29 
30 void UIWidget::initText()
31 {
34  m_textCoordsBuffer.enableHardwareCaching();
35 }
36 
38 {
39  if(m_textWrap && m_rect.isValid())
41  else
43 
44  // update rect size
46  Size textBoxSize = getTextSize();
48  Size size = getSize();
49  if(size.width() <= 0 || (m_textHorizontalAutoResize && !m_textWrap))
50  size.setWidth(textBoxSize.width());
51  if(size.height() <= 0 || m_textVerticalAutoResize)
52  size.setHeight(textBoxSize.height());
53  setSize(size);
54  }
55 
56  m_textMustRecache = true;
57 }
58 
59 void UIWidget::parseTextStyle(const OTMLNodePtr& styleNode)
60 {
61  for(const OTMLNodePtr& node : styleNode->children()) {
62  if(node->tag() == "text")
63  setText(node->value());
64  else if(node->tag() == "text-align")
65  setTextAlign(Fw::translateAlignment(node->value()));
66  else if(node->tag() == "text-offset")
67  setTextOffset(node->value<Point>());
68  else if(node->tag() == "text-wrap")
69  setTextWrap(node->value<bool>());
70  else if(node->tag() == "text-auto-resize")
71  setTextAutoResize(node->value<bool>());
72  else if(node->tag() == "text-horizontal-auto-resize")
73  setTextHorizontalAutoResize(node->value<bool>());
74  else if(node->tag() == "text-vertical-auto-resize")
75  setTextVerticalAutoResize(node->value<bool>());
76  else if(node->tag() == "text-only-upper-case")
77  setTextOnlyUpperCase(node->value<bool>());
78  else if(node->tag() == "font")
79  setFont(node->value());
80  }
81 }
82 
83 void UIWidget::drawText(const Rect& screenCoords)
84 {
85  if(m_drawText.length() == 0 || m_color.aF() == 0.0f)
86  return;
87 
88  if(screenCoords != m_textCachedScreenCoords || m_textMustRecache) {
89  Rect coords = Rect(screenCoords.topLeft(), screenCoords.bottomRight());
90  coords.translate(m_textOffset);
91 
92  m_textMustRecache = false;
93  m_textCachedScreenCoords = coords;
94 
95  m_textCoordsBuffer.clear();
96 
97  m_font->calculateDrawTextCoords(m_textCoordsBuffer, m_drawText, coords, m_textAlign);
98  }
99 
101 
102  if(m_font->getTexture())
103  g_painter->drawTextureCoords(m_textCoordsBuffer, m_font->getTexture());
104 }
105 
106 void UIWidget::onTextChange(const std::string& text, const std::string& oldText)
107 {
108  g_app.repaint();
109  callLuaField("onTextChange", text, oldText);
110 }
111 
112 void UIWidget::onFontChange(const std::string& font)
113 {
114  callLuaField("onFontChange", font);
115 }
116 
117 void UIWidget::setText(std::string text, bool dontFireLuaCall)
118 {
120  stdext::toupper(text);
121 
122  if(m_text == text)
123  return;
124 
125  std::string oldText = m_text;
126  m_text = text;
127  updateText();
128 
129  text = m_text;
130 
131  if(!dontFireLuaCall) {
132  onTextChange(text, oldText);
133  }
134 }
135 
136 void UIWidget::setFont(const std::string& fontName)
137 {
138  m_font = g_fonts.getFont(fontName);
139  updateText();
140  onFontChange(fontName);
141 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
UIWidget::setTextVerticalAutoResize
void setTextVerticalAutoResize(bool textAutoResize)
Definition: uiwidget.h:501
EdgeGroup::right
T right
Definition: uiwidget.h:41
UIWidget::setTextOnlyUpperCase
void setTextOnlyUpperCase(bool textOnlyUpperCase)
Definition: uiwidget.h:502
TSize::setWidth
void setWidth(T w)
Definition: size.h:47
UIWidget::m_textOffset
Point m_textOffset
Definition: uiwidget.h:484
TRect< int >
UIWidget::m_textWrap
stdext::boolean< false > m_textWrap
Definition: uiwidget.h:485
Fw::AlignCenter
@ AlignCenter
Definition: const.h:208
UIWidget::m_textAlign
Fw::AlignmentFlag m_textAlign
Definition: uiwidget.h:483
UIWidget::setTextAlign
void setTextAlign(Fw::AlignmentFlag align)
Definition: uiwidget.h:496
uitranslator.h
EdgeGroup::top
T top
Definition: uiwidget.h:40
g_fonts
FontManager g_fonts
Definition: fontmanager.cpp:29
TPoint::toSize
TSize< T > toSize() const
Definition: point.h:42
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
uiwidget.h
UIWidget::m_textVerticalAutoResize
stdext::boolean< false > m_textVerticalAutoResize
Definition: uiwidget.h:486
UIWidget::m_text
std::string m_text
Definition: uiwidget.h:481
TSize::width
int width() const
Definition: size.h:43
UIWidget::m_rect
Rect m_rect
Definition: uiwidget.h:62
UIWidget::setTextHorizontalAutoResize
void setTextHorizontalAutoResize(bool textAutoResize)
Definition: uiwidget.h:500
CoordsBuffer::enableHardwareCaching
void enableHardwareCaching(HardwareBuffer::UsagePattern usagePattern=HardwareBuffer::DynamicDraw)
Definition: coordsbuffer.cpp:86
UIWidget::m_font
BitmapFontPtr m_font
Definition: uiwidget.h:489
UIWidget::m_textOnlyUpperCase
stdext::boolean< false > m_textOnlyUpperCase
Definition: uiwidget.h:488
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
painter.h
BitmapFont::wrapText
std::string wrapText(const std::string &text, int maxWidth)
Definition: bitmapfont.cpp:293
TSize::setHeight
void setHeight(T h)
Definition: size.h:48
UIWidget::m_padding
EdgeGroup< int > m_padding
Definition: uiwidget.h:292
LuaObject::callLuaField
R callLuaField(const std::string &field, const T &... args)
Definition: luaobject.h:172
UIWidget::onTextChange
virtual void onTextChange(const std::string &text, const std::string &oldText)
Definition: uiwidgettext.cpp:106
UIWidget::updateText
virtual void updateText()
Definition: uiwidgettext.cpp:37
UIWidget::onFontChange
virtual void onFontChange(const std::string &font)
Definition: uiwidgettext.cpp:112
UIWidget::setTextAutoResize
void setTextAutoResize(bool textAutoResize)
Definition: uiwidget.h:499
CoordsBuffer::clear
void clear()
Definition: coordsbuffer.h:38
BitmapFont::calculateDrawTextCoords
void calculateDrawTextCoords(CoordsBuffer &coordsBuffer, const std::string &text, const Rect &screenCoords, Fw::AlignmentFlag align=Fw::AlignTopLeft)
Definition: bitmapfont.cpp:96
UIWidget::setText
void setText(std::string text, bool dontFireLuaCall=false)
Definition: uiwidgettext.cpp:117
BitmapFont::getTexture
const TexturePtr & getTexture()
Definition: bitmapfont.h:62
UIWidget::setTextWrap
void setTextWrap(bool textWrap)
Definition: uiwidget.h:498
Size
TSize< int > Size
Definition: size.h:107
TPoint::x
T x
Definition: point.h:83
Color::aF
float aF() const
Definition: color.h:49
UIWidget::getWidth
int getWidth()
Definition: uiwidget.h:355
g_app
ConsoleApplication g_app
Definition: consoleapplication.cpp:32
UIWidget::getSize
Size getSize()
Definition: uiwidget.h:357
stdext::toupper
void toupper(std::string &str)
Definition: string.cpp:221
Rect
TRect< int > Rect
Definition: rect.h:319
fontmanager.h
UIWidget::m_textHorizontalAutoResize
stdext::boolean< false > m_textHorizontalAutoResize
Definition: uiwidget.h:487
TSize::height
int height() const
Definition: size.h:44
TRect::isValid
bool isValid() const
Definition: rect.h:50
stdext::shared_object_ptr< OTMLNode >
FontManager::getFont
BitmapFontPtr getFont(const std::string &fontName)
Definition: fontmanager.cpp:90
Fw::translateAlignment
AlignmentFlag translateAlignment(std::string aligment)
Definition: uitranslator.cpp:27
g_painter
Painter * g_painter
Definition: painter.cpp:28
UIWidget::setSize
void setSize(const Size &size)
Definition: uiwidget.h:303
FontManager::getDefaultFont
BitmapFontPtr getDefaultFont()
Definition: fontmanager.h:41
framebuffer.h
UIWidget::getTextSize
Size getTextSize()
Definition: uiwidget.h:511
TRect::bottomRight
TPoint< T > bottomRight() const
Definition: rect.h:61
TPoint< int >
EdgeGroup::bottom
T bottom
Definition: uiwidget.h:42
UIWidget::setFont
void setFont(const std::string &fontName)
Definition: uiwidgettext.cpp:136
EdgeGroup::left
T left
Definition: uiwidget.h:43
UIWidget::m_drawText
std::string m_drawText
Definition: uiwidget.h:482
UIWidget::drawText
void drawText(const Rect &screenCoords)
Definition: uiwidgettext.cpp:83
TSize< int >
Painter::drawTextureCoords
virtual void drawTextureCoords(CoordsBuffer &coordsBuffer, const TexturePtr &texture)=0
UIWidget::setTextOffset
void setTextOffset(const Point &offset)
Definition: uiwidget.h:497
TRect::translate
void translate(T x, T y)
Definition: rect.h:98
application.h
UIWidget::m_color
Color m_color
Definition: uiwidget.h:281