1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2009 Aethyra Development Team |
4 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
5 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
6 |
|
|
* |
7 |
|
|
* This file is part of The ManaPlus Client. |
8 |
|
|
* |
9 |
|
|
* This program is free software; you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU General Public License as published by |
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
12 |
|
|
* any later version. |
13 |
|
|
* |
14 |
|
|
* This program is distributed in the hope that it will be useful, |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
* GNU General Public License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU General Public License |
20 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
/* _______ __ __ __ ______ __ __ _______ __ __ |
24 |
|
|
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ |
25 |
|
|
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / |
26 |
|
|
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / |
27 |
|
|
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / |
28 |
|
|
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / |
29 |
|
|
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ |
30 |
|
|
* |
31 |
|
|
* Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson |
32 |
|
|
* |
33 |
|
|
* |
34 |
|
|
* Per Larsson a.k.a finalman |
35 |
|
|
* Olof Naessén a.k.a jansem/yakslem |
36 |
|
|
* |
37 |
|
|
* Visit: http://guichan.sourceforge.net |
38 |
|
|
* |
39 |
|
|
* License: (BSD) |
40 |
|
|
* Redistribution and use in source and binary forms, with or without |
41 |
|
|
* modification, are permitted provided that the following conditions |
42 |
|
|
* are met: |
43 |
|
|
* 1. Redistributions of source code must retain the above copyright |
44 |
|
|
* notice, this list of conditions and the following disclaimer. |
45 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
46 |
|
|
* notice, this list of conditions and the following disclaimer in |
47 |
|
|
* the documentation and/or other materials provided with the |
48 |
|
|
* distribution. |
49 |
|
|
* 3. Neither the name of Guichan nor the names of its contributors may |
50 |
|
|
* be used to endorse or promote products derived from this software |
51 |
|
|
* without specific prior written permission. |
52 |
|
|
* |
53 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
54 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
55 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
56 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
57 |
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
58 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
59 |
|
|
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
60 |
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
61 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
62 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
63 |
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
64 |
|
|
*/ |
65 |
|
|
|
66 |
|
|
#include "gui/widgets/label.h" |
67 |
|
|
|
68 |
|
|
#include "gui/gui.h" |
69 |
|
|
#include "gui/skin.h" |
70 |
|
|
|
71 |
|
|
#include "gui/fonts/font.h" |
72 |
|
|
|
73 |
|
|
#include "debug.h" |
74 |
|
|
|
75 |
|
|
Skin *Label::mSkin = nullptr; |
76 |
|
|
int Label::mInstances = 0; |
77 |
|
|
|
78 |
|
126 |
Label::Label(const Widget2 *const widget) : |
79 |
|
|
Widget(widget), |
80 |
|
|
WidgetListener(), |
81 |
|
|
ToolTipListener(), |
82 |
|
|
mCaption(), |
83 |
|
|
mTextChunk(), |
84 |
|
|
mAlignment(Graphics::LEFT), |
85 |
|
|
mPadding(0), |
86 |
✓✗✓✗
|
378 |
mTextChanged(true) |
87 |
|
|
{ |
88 |
✓✗ |
126 |
init(); |
89 |
|
126 |
} |
90 |
|
|
|
91 |
|
489 |
Label::Label(const Widget2 *const widget, |
92 |
|
489 |
const std::string &caption) : |
93 |
|
|
Widget(widget), |
94 |
|
|
WidgetListener(), |
95 |
|
|
ToolTipListener(), |
96 |
|
|
mCaption(caption), |
97 |
|
|
mTextChunk(), |
98 |
|
|
mAlignment(Graphics::LEFT), |
99 |
|
|
mPadding(0), |
100 |
✓✗✓✗
|
1467 |
mTextChanged(true) |
101 |
|
|
{ |
102 |
✓✗ |
489 |
const Font *const font = getFont(); |
103 |
✓✗✓✗
|
489 |
setWidth(font->getWidth(caption)); |
104 |
✓✗✓✗
|
489 |
setHeight(font->getHeight()); |
105 |
✓✗ |
489 |
init(); |
106 |
|
489 |
} |
107 |
|
|
|
108 |
|
3690 |
Label::~Label() |
109 |
|
|
{ |
110 |
✓✓ |
615 |
if (mWindow != nullptr) |
111 |
|
611 |
mWindow->removeWidgetListener(this); |
112 |
|
|
|
113 |
✓✗ |
615 |
if (gui != nullptr) |
114 |
|
615 |
gui->removeDragged(this); |
115 |
|
|
|
116 |
|
615 |
mInstances --; |
117 |
✓✓ |
615 |
if (mInstances == 0) |
118 |
|
|
{ |
119 |
✓✗ |
39 |
if (theme != nullptr) |
120 |
|
39 |
theme->unload(mSkin); |
121 |
|
|
} |
122 |
|
615 |
removeMouseListener(this); |
123 |
|
615 |
mTextChunk.deleteImage(); |
124 |
|
1230 |
} |
125 |
|
|
|
126 |
|
615 |
void Label::init() |
127 |
|
|
{ |
128 |
|
615 |
addMouseListener(this); |
129 |
|
615 |
mAllowLogic = false; |
130 |
|
1230 |
mForegroundColor = getThemeColor(ThemeColorId::LABEL, 255U); |
131 |
|
1230 |
mForegroundColor2 = getThemeColor(ThemeColorId::LABEL_OUTLINE, 255U); |
132 |
✓✓ |
615 |
if (mInstances == 0) |
133 |
|
|
{ |
134 |
✓✗ |
39 |
if (theme != nullptr) |
135 |
|
|
{ |
136 |
✓✗✓✗ ✓✗ |
273 |
mSkin = theme->load("label.xml", |
137 |
|
|
"", |
138 |
|
|
true, |
139 |
|
39 |
Theme::getThemePath()); |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
615 |
mInstances ++; |
143 |
|
|
|
144 |
✓✗ |
615 |
if (mSkin != nullptr) |
145 |
|
1230 |
mPadding = mSkin->getPadding(); |
146 |
|
|
else |
147 |
|
|
mPadding = 0; |
148 |
|
1230 |
setSelectable(false); |
149 |
|
615 |
} |
150 |
|
|
|
151 |
|
60 |
void Label::draw(Graphics *const graphics) |
152 |
|
|
{ |
153 |
|
|
BLOCK_START("Label::draw") |
154 |
|
|
int textX; |
155 |
|
60 |
const Rect &rect = mDimension; |
156 |
|
60 |
Font *const font = getFont(); |
157 |
|
|
|
158 |
✓✓✗ |
60 |
switch (mAlignment) |
159 |
|
|
{ |
160 |
|
|
case Graphics::LEFT: |
161 |
|
|
default: |
162 |
|
45 |
textX = mPadding; |
163 |
|
45 |
break; |
164 |
|
|
case Graphics::CENTER: |
165 |
|
15 |
textX = (rect.width - font->getWidth(mCaption)) / 2; |
166 |
|
15 |
break; |
167 |
|
|
case Graphics::RIGHT: |
168 |
|
|
if (rect.width > mPadding) |
169 |
|
|
textX = rect.width - mPadding - font->getWidth(mCaption); |
170 |
|
|
else |
171 |
|
|
textX = 0; |
172 |
|
|
break; |
173 |
|
|
} |
174 |
|
|
|
175 |
✓✗ |
60 |
if (mTextChanged) |
176 |
|
|
{ |
177 |
|
60 |
mTextChunk.textFont = font; |
178 |
|
60 |
mTextChunk.deleteImage(); |
179 |
|
120 |
mTextChunk.text = mCaption; |
180 |
|
60 |
mTextChunk.color = mForegroundColor; |
181 |
|
60 |
mTextChunk.color2 = mForegroundColor2; |
182 |
|
60 |
font->generate(mTextChunk); |
183 |
|
60 |
mTextChanged = false; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
60 |
const Image *const image = mTextChunk.img; |
187 |
✓✓ |
60 |
if (image != nullptr) |
188 |
|
|
{ |
189 |
|
47 |
const int textY = rect.height / 2 - font->getHeight() / 2; |
190 |
|
47 |
graphics->drawImage(image, textX, textY); |
191 |
|
|
} |
192 |
|
|
BLOCK_END("Label::draw") |
193 |
|
60 |
} |
194 |
|
|
|
195 |
|
|
void Label::safeDraw(Graphics *const graphics) |
196 |
|
|
{ |
197 |
|
|
Label::draw(graphics); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
172 |
void Label::adjustSize() |
201 |
|
|
{ |
202 |
|
172 |
const Font *const font = getFont(); |
203 |
|
172 |
const int pad2 = 2 * mPadding; |
204 |
|
172 |
setWidth(font->getWidth(mCaption) + pad2); |
205 |
|
172 |
setHeight(font->getHeight() + pad2); |
206 |
|
172 |
} |
207 |
|
|
|
208 |
|
|
void Label::setForegroundColor(const Color &color) |
209 |
|
|
{ |
210 |
|
|
if (mForegroundColor != color || mForegroundColor2 != color) |
211 |
|
|
mTextChanged = true; |
212 |
|
|
// logger->log("Label::setForegroundColor: " + mCaption); |
213 |
|
|
mForegroundColor = color; |
214 |
|
|
mForegroundColor2 = color; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
68 |
void Label::setForegroundColorAll(const Color &color1, |
218 |
|
|
const Color &color2) |
219 |
|
|
{ |
220 |
✓✓✗✓ ✓✓ |
138 |
if (mForegroundColor != color1 || mForegroundColor2 != color2) |
221 |
|
66 |
mTextChanged = true; |
222 |
|
|
// logger->log("Label::setForegroundColorAll: " + mCaption); |
223 |
|
68 |
mForegroundColor = color1; |
224 |
|
68 |
mForegroundColor2 = color2; |
225 |
|
68 |
} |
226 |
|
|
|
227 |
|
9 |
void Label::resizeTo(const int maxSize, const int minSize) |
228 |
|
|
{ |
229 |
|
9 |
const Font *const font = getFont(); |
230 |
|
9 |
const int pad2 = 2 * mPadding; |
231 |
|
9 |
setHeight(font->getHeight() + pad2); |
232 |
|
|
|
233 |
✗✓ |
9 |
if (font->getWidth(mCaption) + pad2 > maxSize) |
234 |
|
|
{ |
235 |
|
|
const int dots = font->getWidth("..."); |
236 |
|
|
if (dots > maxSize) |
237 |
|
|
{ |
238 |
|
|
setWidth(maxSize); |
239 |
|
|
return; |
240 |
|
|
} |
241 |
|
|
const size_t szChars = mCaption.size(); |
242 |
|
|
for (size_t f = 1; f < szChars - 1; f ++) |
243 |
|
|
{ |
244 |
|
|
const std::string text = mCaption.substr(0, szChars - f); |
245 |
|
|
const int width = font->getWidth(text) + dots + pad2; |
246 |
|
|
if (width <= maxSize) |
247 |
|
|
{ |
248 |
|
|
setCaption(text + "..."); |
249 |
|
|
setWidth(width); |
250 |
|
|
return; |
251 |
|
|
} |
252 |
|
|
} |
253 |
|
|
setWidth(maxSize); |
254 |
|
|
} |
255 |
|
|
else |
256 |
|
|
{ |
257 |
|
9 |
int sz = font->getWidth(mCaption) + pad2; |
258 |
✗✓ |
9 |
if (sz < minSize) |
259 |
|
|
sz = minSize; |
260 |
|
9 |
setWidth(sz); |
261 |
|
|
} |
262 |
|
|
} |
263 |
|
|
|
264 |
|
158 |
void Label::setCaption(const std::string& caption) |
265 |
|
|
{ |
266 |
✓✓ |
316 |
if (caption != mCaption) |
267 |
|
129 |
mTextChanged = true; |
268 |
|
316 |
mCaption = caption; |
269 |
|
158 |
} |
270 |
|
|
|
271 |
|
623 |
void Label::setParent(Widget *const widget) |
272 |
|
|
{ |
273 |
✓✓ |
623 |
if (mWindow != nullptr) |
274 |
|
615 |
mWindow->addWidgetListener(this); |
275 |
|
1246 |
Widget::setParent(widget); |
276 |
|
623 |
} |
277 |
|
|
|
278 |
|
12 |
void Label::setWindow(Widget *const widget) |
279 |
|
|
{ |
280 |
✓✓✓✗
|
12 |
if ((widget == nullptr) && (mWindow != nullptr)) |
281 |
|
|
{ |
282 |
|
6 |
mWindow->removeWidgetListener(this); |
283 |
|
6 |
mWindow = nullptr; |
284 |
|
|
} |
285 |
|
|
else |
286 |
|
|
{ |
287 |
|
6 |
Widget2::setWindow(widget); |
288 |
|
|
} |
289 |
|
12 |
} |
290 |
|
|
|
291 |
|
1 |
void Label::widgetHidden(const Event &event A_UNUSED) |
292 |
|
|
{ |
293 |
|
1 |
mTextChanged = true; |
294 |
|
1 |
mTextChunk.deleteImage(); |
295 |
|
1 |
} |
296 |
|
|
|
297 |
|
127 |
void Label::finalCleanup() |
298 |
|
|
{ |
299 |
|
127 |
mSkin = nullptr; |
300 |
|
127 |
} |