cachedtext.cpp

main 82 lines · 2.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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
28CachedText::CachedText()
29{
30 m_font = g_fonts.getDefaultFont();
31 m_align = Fw::AlignCenter;
32}
33
34void CachedText::draw(const Rect& rect, const Color& color)
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
44 if (m_textColors.empty()) {
45 m_font->drawText(m_text, m_textCachedScreenCoords, Fw::AlignCenter, color);
46 } else {
47 m_font->drawColoredText(m_text, m_textCachedScreenCoords, Fw::AlignCenter, m_textColors);
48 }
49}
50
51void CachedText::setColoredText(const std::vector<std::string>& texts)
52{
53 m_text = "";
54 m_textColors.clear();
55 for (size_t i = 0, p = 0; i < texts.size() - 1; i += 2) {
56 Color c(Color::white);
57 stdext::cast<Color>(texts[i + 1], c);
58 m_text += texts[i];
59 for (auto& c : texts[i]) {
60 if ((uint8)c >= 32)
61 p += 1;
62 }
63 m_textColors.push_back(std::make_pair(p, c));
64 }
65 update();
66}
67
68void CachedText::update()
69{
70 if(m_font)
71 m_textSize = m_font->calculateTextRectSize(m_text);
72 m_textMustRecache = true;
73}
74
75void CachedText::wrapText(int maxWidth)
76{
77 if(m_font) {
78 m_text = m_font->wrapText(m_text, maxWidth, m_textColors.empty() ? nullptr : &m_textColors);
79 update();
80 }
81}
82
Top