Otclient  14/8/2020
cachedtext.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 "cachedtext.h"
24 #include "painter.h"
25 #include "fontmanager.h"
26 #include "bitmapfont.h"
27 
29 {
30  m_font = g_fonts.getDefaultFont();
31  m_align = Fw::AlignCenter;
32 }
33 
34 void CachedText::draw(const Rect& rect)
35 {
36  if(!m_font)
37  return;
38 
39  if(m_textMustRecache || m_textCachedScreenCoords != rect) {
40  m_textMustRecache = false;
41  m_textCachedScreenCoords = rect;
42 
43  m_textCoordsBuffer.clear();
44  m_font->calculateDrawTextCoords(m_textCoordsBuffer, m_text, rect, Fw::AlignCenter);
45  }
46 
47  if(m_font->getTexture())
48  g_painter->drawTextureCoords(m_textCoordsBuffer, m_font->getTexture());
49 }
50 
51 void CachedText::update()
52 {
53  if(m_font)
54  m_textSize = m_font->calculateTextRectSize(m_text);
55  m_textMustRecache = true;
56 }
57 
58 void CachedText::wrapText(int maxWidth)
59 {
60  if(m_font) {
61  m_text = m_font->wrapText(m_text, maxWidth);
62  update();
63  }
64 }
BitmapFont::calculateTextRectSize
Size calculateTextRectSize(const std::string &text)
Simulate render and calculate text size.
Definition: bitmapfont.cpp:256
TRect< int >
Fw::AlignCenter
@ AlignCenter
Definition: const.h:208
g_fonts
FontManager g_fonts
Definition: fontmanager.cpp:29
CachedText::wrapText
void wrapText(int maxWidth)
Definition: cachedtext.cpp:58
cachedtext.h
painter.h
BitmapFont::wrapText
std::string wrapText(const std::string &text, int maxWidth)
Definition: bitmapfont.cpp:293
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
BitmapFont::getTexture
const TexturePtr & getTexture()
Definition: bitmapfont.h:62
CachedText::draw
void draw(const Rect &rect)
Definition: cachedtext.cpp:34
fontmanager.h
CachedText::CachedText
CachedText()
Definition: cachedtext.cpp:28
bitmapfont.h
g_painter
Painter * g_painter
Definition: painter.cpp:28
FontManager::getDefaultFont
BitmapFontPtr getDefaultFont()
Definition: fontmanager.h:41
Painter::drawTextureCoords
virtual void drawTextureCoords(CoordsBuffer &coordsBuffer, const TexturePtr &texture)=0