uiwidget.h

main 554 lines · 25 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#ifndef UIWIDGET_H
24#define UIWIDGET_H
25
26#include "declarations.h"
27#include "uilayout.h"
28
29#include <framework/luaengine/luaobject.h>
30#include <framework/graphics/declarations.h>
31#include <framework/otml/otmlnode.h>
32#include <framework/graphics/bitmapfont.h>
33#include <framework/graphics/coordsbuffer.h>
34#include <framework/core/timer.h>
35
36template<typename T = int>
37struct EdgeGroup {
38 EdgeGroup() { top = right = bottom = left = T(0); }
39 void set(T value) { top = right = bottom = left = value; }
40 T top;
41 T right;
42 T bottom;
43 T left;
44};
45
46// @bindclass
47class UIWidget : public LuaObject
48{
49// widget core
50public:
51 UIWidget();
52 virtual ~UIWidget();
53
54 virtual void draw(const Rect& visibleRect, Fw::DrawPane drawPane);
55
56protected:
57 virtual void drawSelf(Fw::DrawPane drawPane);
58 virtual void drawChildren(const Rect& visibleRect, Fw::DrawPane drawPane);
59
60 friend class UIManager;
61
62 std::string m_id;
63 std::string m_source;
64 Rect m_rect;
65 Point m_virtualOffset;
66 stdext::boolean<true> m_autoDraw;
67 stdext::boolean<true> m_enabled;
68 stdext::boolean<true> m_visible;
69 stdext::boolean<true> m_focusable;
70 stdext::boolean<false> m_fixedSize;
71 stdext::boolean<false> m_phantom;
72 stdext::boolean<false> m_draggable;
73 stdext::boolean<false> m_destroyed;
74 stdext::boolean<false> m_clipping;
75 UILayoutPtr m_layout;
76 UIWidgetPtr m_parent;
77 std::string m_parentId;
78 UIWidgetList m_children;
79 UIWidgetList m_lockedChildren;
80 std::map<UIWidgetPtr, std::string> m_childrenShortcuts;
81 UIWidgetPtr m_focusedChild;
82 OTMLNodePtr m_style;
83 Timer m_clickTimer;
84 Fw::FocusReason m_lastFocusReason;
85 Fw::AutoFocusPolicy m_autoFocusPolicy;
86
87public:
88 void addChild(const UIWidgetPtr& child);
89 void onChildIdChange(const UIWidgetPtr& child);
90 void insertChild(int index, const UIWidgetPtr& child);
91 void removeChild(UIWidgetPtr child);
92 void focusChild(const UIWidgetPtr& child, Fw::FocusReason reason);
93 void focusNextChild(Fw::FocusReason reason, bool rotate = false);
94 void focusPreviousChild(Fw::FocusReason reason, bool rotate = false);
95 void lowerChild(UIWidgetPtr child);
96 void raiseChild(UIWidgetPtr child);
97 void moveChildToIndex(const UIWidgetPtr& child, int index);
98 void reorderChildren(const std::vector<UIWidgetPtr>& childrens);
99 void lockChild(const UIWidgetPtr& child);
100 void unlockChild(const UIWidgetPtr& child);
101 void mergeStyle(const OTMLNodePtr& styleNode);
102 void applyStyle(const OTMLNodePtr& styleNode);
103 void addAnchor(Fw::AnchorEdge anchoredEdge, const std::string& hookedWidgetId, Fw::AnchorEdge hookedEdge);
104 void removeAnchor(Fw::AnchorEdge anchoredEdge);
105 void fill(const std::string& hookedWidgetId);
106 void centerIn(const std::string& hookedWidgetId);
107 void breakAnchors();
108 void updateParentLayout();
109 void updateLayout();
110 void lock();
111 void unlock();
112 void focus();
113 void recursiveFocus(Fw::FocusReason reason);
114 void lower();
115 void raise();
116 void grabMouse();
117 void ungrabMouse();
118 void grabKeyboard();
119 void ungrabKeyboard();
120 void bindRectToParent();
121 void destroy();
122 void destroyChildren();
123
124 void setId(const std::string& id);
125 void setParent(const UIWidgetPtr& parent);
126 void setLayout(const UILayoutPtr& layout);
127 bool setRect(const Rect& rect);
128 void setStyle(const std::string& styleName);
129 void setStyleFromNode(const OTMLNodePtr& styleNode);
130 void setEnabled(bool enabled);
131 void setVisible(bool visible);
132 void setAutoDraw(bool value);
133 void setOn(bool on);
134 void setChecked(bool checked);
135 void setFocusable(bool focusable);
136 void setPhantom(bool phantom);
137 void setDraggable(bool draggable);
138 void setFixedSize(bool fixed);
139 void setClipping(bool clipping) { m_clipping = clipping; }
140 void setLastFocusReason(Fw::FocusReason reason);
141 void setAutoFocusPolicy(Fw::AutoFocusPolicy policy);
142 void setAutoRepeatDelay(int delay) { m_autoRepeatDelay = delay; }
143 void setVirtualOffset(const Point& offset);
144
145 bool isAnchored();
146 bool isChildLocked(const UIWidgetPtr& child);
147 bool hasChild(const UIWidgetPtr& child);
148 int getChildIndex(const UIWidgetPtr& child);
149 Rect getPaddingRect();
150 Rect getMarginRect();
151 Rect getChildrenRect();
152 UIAnchorLayoutPtr getAnchoredLayout();
153 UIWidgetPtr getRootParent();
154 UIWidgetPtr getChildAfter(const UIWidgetPtr& relativeChild);
155 UIWidgetPtr getChildBefore(const UIWidgetPtr& relativeChild);
156 UIWidgetPtr getChildById(const std::string& childId);
157 UIWidgetPtr getChildByPos(const Point& childPos);
158 UIWidgetPtr getChildByIndex(int index);
159 UIWidgetPtr recursiveGetChildById(const std::string& id);
160 UIWidgetPtr recursiveGetChildByPos(const Point& childPos, bool wantsPhantom);
161 UIWidgetList recursiveGetChildren();
162 UIWidgetList recursiveGetChildrenByPos(const Point& childPos);
163 UIWidgetList recursiveGetChildrenByMarginPos(const Point& childPos);
164 UIWidgetPtr backwardsGetWidgetById(const std::string& id);
165
166private:
167 stdext::boolean<false> m_updateEventScheduled;
168 stdext::boolean<false> m_loadingStyle;
169
170
171// state managment
172protected:
173 bool setState(Fw::WidgetState state, bool on);
174 bool hasState(Fw::WidgetState state);
175
176private:
177 void internalDestroy();
178 void updateState(Fw::WidgetState state);
179 void updateStates();
180 void updateChildrenIndexStates();
181 void updateStyle();
182
183 stdext::boolean<false> m_updateStyleScheduled;
184 stdext::boolean<true> m_firstOnStyle;
185 OTMLNodePtr m_stateStyle;
186 int m_states;
187
188
189// event processing
190protected:
191 virtual void onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode);
192 virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
193 virtual void onLayoutUpdate();
194 virtual void onFocusChange(bool focused, Fw::FocusReason reason);
195 virtual void onChildFocusChange(const UIWidgetPtr& focusedChild, const UIWidgetPtr& unfocusedChild, Fw::FocusReason reason);
196 virtual void onHoverChange(bool hovered);
197 virtual void onVisibilityChange(bool visible);
198 virtual bool onDragEnter(const Point& mousePos);
199 virtual bool onDragLeave(UIWidgetPtr droppedWidget, const Point& mousePos);
200 virtual bool onDragMove(const Point& mousePos, const Point& mouseMoved);
201 virtual bool onDrop(UIWidgetPtr draggedWidget, const Point& mousePos);
202 virtual bool onKeyText(const std::string& keyText);
203 virtual bool onKeyDown(uchar keyCode, int keyboardModifiers);
204 virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
205 virtual bool onKeyUp(uchar keyCode, int keyboardModifiers);
206 virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
207 virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
208 virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
209 virtual bool onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
210 virtual bool onClick(const Point& mousePos);
211 virtual bool onDoubleClick(const Point& mousePos);
212
213 friend class UILayout;
214
215 bool propagateOnKeyText(const std::string& keyText);
216 bool propagateOnKeyDown(uchar keyCode, int keyboardModifiers);
217 bool propagateOnKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
218 bool propagateOnKeyUp(uchar keyCode, int keyboardModifiers);
219 bool propagateOnMouseEvent(const Point& mousePos, UIWidgetList& widgetList);
220 bool propagateOnMouseMove(const Point& mousePos, const Point& mouseMoved, UIWidgetList& widgetList);
221
222
223// function shortcuts
224public:
225 void resize(int width, int height) { setRect(Rect(getPosition(), Size(width, height))); }
226 void move(int x, int y) { setRect(Rect(x, y, getSize())); }
227 void rotate(float degrees) { setRotation(degrees); }
228 void hide() { setVisible(false); }
229 void show() { setVisible(true); }
230 void disable() { setEnabled(false); }
231 void enable() { setEnabled(true); }
232
233 bool isActive() { return hasState(Fw::ActiveState); }
234 bool isEnabled() { return !hasState(Fw::DisabledState); }
235 bool isDisabled() { return hasState(Fw::DisabledState); }
236 bool isFocused() { return hasState(Fw::FocusState); }
237 bool isHovered() { return hasState(Fw::HoverState); }
238 bool isPressed() { return hasState(Fw::PressedState); }
239 bool isFirst() { return hasState(Fw::FirstState); }
240 bool isMiddle() { return hasState(Fw::MiddleState); }
241 bool isLast() { return hasState(Fw::LastState); }
242 bool isAlternate() { return hasState(Fw::AlternateState); }
243 bool isChecked() { return hasState(Fw::CheckedState); }
244 bool isOn() { return hasState(Fw::OnState); }
245 bool isDragging() { return hasState(Fw::DraggingState); }
246 bool isVisible() { return !hasState(Fw::HiddenState); }
247 bool isHidden() { return hasState(Fw::HiddenState); }
248 bool isExplicitlyEnabled() { return m_enabled; }
249 bool isExplicitlyVisible() { return m_visible; }
250 bool isAutoDraw() { return m_autoDraw; }
251 bool isFocusable() { return m_focusable; }
252 bool isPhantom() { return m_phantom; }
253 bool isDraggable() { return m_draggable; }
254 bool isFixedSize() { return m_fixedSize; }
255 bool isClipping() { return m_clipping; }
256 bool isDestroyed() { return m_destroyed; }
257
258 bool hasChildren() { return m_children.size() > 0; }
259 bool containsMarginPoint(const Point& point) { return getMarginRect().contains(point); }
260 bool containsPaddingPoint(const Point& point) { return getPaddingRect().contains(point); }
261 bool containsPoint(const Point& point) { return m_rect.contains(point); }
262
263 std::string getId() { return m_id; }
264 std::string getSource() { return m_source; }
265 UIWidgetPtr getParent() { return m_parent; }
266 std::string getParentId() { return m_parentId; }
267 UIWidgetPtr getFocusedChild() { return m_focusedChild; }
268 UIWidgetList getChildren() { return m_children; }
269 UIWidgetPtr getFirstChild() { return getChildByIndex(1); }
270 UIWidgetPtr getLastChild() { return getChildByIndex(-1); }
271 UILayoutPtr getLayout() { return m_layout; }
272 OTMLNodePtr getStyle() { return m_style; }
273 int getChildCount() { return m_children.size(); }
274 Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
275 Fw::AutoFocusPolicy getAutoFocusPolicy() { return m_autoFocusPolicy; }
276 int getAutoRepeatDelay() { return m_autoRepeatDelay; }
277 Point getVirtualOffset() { return m_virtualOffset; }
278 std::string getStyleName() { return m_style->tag(); }
279 Point getLastClickPosition() { return m_lastClickPosition; }
280
281 // for stats only
282 bool isRootChild()
283 {
284 return m_isRootChild;
285 }
286
287 void setRootChild(bool v)
288 {
289 m_isRootChild = v;
290 }
291
292
293// base style
294private:
295 void initBaseStyle();
296 void parseBaseStyle(const OTMLNodePtr& styleNode);
297
298protected:
299 void drawBackground(const Rect& screenCoords);
300 void drawBorder(const Rect& screenCoords);
301 void drawIcon(const Rect& screenCoords);
302
303 Color m_color;
304 Color m_backgroundColor;
305 Rect m_backgroundRect;
306 TexturePtr m_icon;
307 Color m_iconColor;
308 Rect m_iconRect;
309 Rect m_iconClipRect;
310 std::string m_iconPath;
311 Fw::AlignmentFlag m_iconAlign;
312 EdgeGroup<Color> m_borderColor;
313 EdgeGroup<int> m_borderWidth;
314 EdgeGroup<int> m_margin;
315 EdgeGroup<int> m_padding;
316 float m_opacity;
317 float m_rotation;
318 int m_autoRepeatDelay;
319 Point m_lastClickPosition;
320 bool m_isRootChild = false; // for stats
321
322public:
323 void setX(int x) { move(x, getY()); }
324 void setY(int y) { move(getX(), y); }
325 void setWidth(int width) { resize(width, getHeight()); }
326 void setHeight(int height) { resize(getWidth(), height); }
327 void setSize(const Size& size) { resize(size.width(), size.height()); }
328 void setPosition(const Point& pos) { move(pos.x, pos.y); }
329 void setColor(const Color& color) { m_color = color; }
330 void setBackgroundColor(const Color& color) { m_backgroundColor = color; }
331 void setBackgroundOffsetX(int x) { m_backgroundRect.setX(x); }
332 void setBackgroundOffsetY(int y) { m_backgroundRect.setX(y); }
333 void setBackgroundOffset(const Point& pos) { m_backgroundRect.move(pos); }
334 void setBackgroundWidth(int width) { m_backgroundRect.setWidth(width); }
335 void setBackgroundHeight(int height) { m_backgroundRect.setHeight(height); }
336 void setBackgroundSize(const Size& size) { m_backgroundRect.resize(size); }
337 void setBackgroundRect(const Rect& rect) { m_backgroundRect = rect; }
338 void setIcon(const std::string& iconFile);
339 void setIconColor(const Color& color) { m_iconColor = color; }
340 void setIconOffsetX(int x) { m_iconOffset.x = x; }
341 void setIconOffsetY(int y) { m_iconOffset.y = y; }
342 void setIconOffset(const Point& pos) { m_iconOffset = pos; }
343 void setIconWidth(int width) { m_iconRect.setWidth(width); }
344 void setIconHeight(int height) { m_iconRect.setHeight(height); }
345 void setIconSize(const Size& size) { m_iconRect.resize(size); }
346 void setIconRect(const Rect& rect) { m_iconRect = rect; }
347 void setIconClip(const Rect& rect) { m_iconClipRect = rect; }
348 void setIconAlign(Fw::AlignmentFlag align) { m_iconAlign = align; }
349 void setBorderWidth(int width) { m_borderWidth.set(width); updateLayout(); }
350 void setBorderWidthTop(int width) { m_borderWidth.top = width; }
351 void setBorderWidthRight(int width) { m_borderWidth.right = width; }
352 void setBorderWidthBottom(int width) { m_borderWidth.bottom = width; }
353 void setBorderWidthLeft(int width) { m_borderWidth.left = width; }
354 void setBorderColor(const Color& color) { m_borderColor.set(color); updateLayout(); }
355 void setBorderColorTop(const Color& color) { m_borderColor.top = color; }
356 void setBorderColorRight(const Color& color) { m_borderColor.right = color; }
357 void setBorderColorBottom(const Color& color) { m_borderColor.bottom = color; }
358 void setBorderColorLeft(const Color& color) { m_borderColor.left = color; }
359 void setMargin(int margin) { m_margin.set(margin); updateParentLayout(); }
360 void setMarginHorizontal(int margin) { m_margin.right = m_margin.left = margin; updateParentLayout(); }
361 void setMarginVertical(int margin) { m_margin.bottom = m_margin.top = margin; updateParentLayout(); }
362 void setMarginTop(int margin) { m_margin.top = margin; updateParentLayout(); }
363 void setMarginRight(int margin) { m_margin.right = margin; updateParentLayout(); }
364 void setMarginBottom(int margin) { m_margin.bottom = margin; updateParentLayout(); }
365 void setMarginLeft(int margin) { m_margin.left = margin; updateParentLayout(); }
366 void setPadding(int padding) { m_padding.top = m_padding.right = m_padding.bottom = m_padding.left = padding; updateLayout(); }
367 void setPaddingHorizontal(int padding) { m_padding.right = m_padding.left = padding; updateLayout(); }
368 void setPaddingVertical(int padding) { m_padding.bottom = m_padding.top = padding; updateLayout(); }
369 void setPaddingTop(int padding) { m_padding.top = padding; updateLayout(); }
370 void setPaddingRight(int padding) { m_padding.right = padding; updateLayout(); }
371 void setPaddingBottom(int padding) { m_padding.bottom = padding; updateLayout(); }
372 void setPaddingLeft(int padding) { m_padding.left = padding; updateLayout(); }
373 void setOpacity(float opacity) { m_opacity = stdext::clamp<float>(opacity, 0.0f, 1.0f); }
374 void setRotation(float degrees) { m_rotation = degrees; }
375 void setChangeCursorImage(bool enable) { m_changeCursorImage = enable; }
376 void setCursor(const std::string& cursor);
377
378 int getX() { return m_rect.x(); }
379 int getY() { return m_rect.y(); }
380 Point getPosition() { return m_rect.topLeft(); }
381 int getWidth() { return m_rect.width(); }
382 int getHeight() { return m_rect.height(); }
383 Size getSize() { return m_rect.size(); }
384 Rect getRect() { return m_rect; }
385 Color getColor() { return m_color; }
386 Color getBackgroundColor() { return m_backgroundColor; }
387 int getBackgroundOffsetX() { return m_backgroundRect.x(); }
388 int getBackgroundOffsetY() { return m_backgroundRect.y(); }
389 Point getBackgroundOffset() { return m_backgroundRect.topLeft(); }
390 int getBackgroundWidth() { return m_backgroundRect.width(); }
391 int getBackgroundHeight() { return m_backgroundRect.height(); }
392 Size getBackgroundSize() { return m_backgroundRect.size(); }
393 Rect getBackgroundRect() { return m_backgroundRect; }
394 Color getIconColor() { return m_iconColor; }
395 int getIconOffsetX() { return m_iconOffset.x; }
396 int getIconOffsetY() { return m_iconOffset.y; }
397 Point getIconOffset() { return m_iconOffset; }
398 int getIconWidth() { return m_iconRect.width(); }
399 int getIconHeight() { return m_iconRect.height(); }
400 Size getIconSize() { return m_iconRect.size(); }
401 Rect getIconRect() { return m_iconRect; }
402 Rect getIconClip() { return m_iconClipRect; }
403 std::string getIconPath() { return m_iconPath; }
404 Fw::AlignmentFlag getIconAlign() { return m_iconAlign; }
405 Color getBorderTopColor() { return m_borderColor.top; }
406 Color getBorderRightColor() { return m_borderColor.right; }
407 Color getBorderBottomColor() { return m_borderColor.bottom; }
408 Color getBorderLeftColor() { return m_borderColor.left; }
409 int getBorderTopWidth() { return m_borderWidth.top; }
410 int getBorderRightWidth() { return m_borderWidth.right; }
411 int getBorderBottomWidth() { return m_borderWidth.bottom; }
412 int getBorderLeftWidth() { return m_borderWidth.left; }
413 int getMarginTop() { return m_margin.top; }
414 int getMarginRight() { return m_margin.right; }
415 int getMarginBottom() { return m_margin.bottom; }
416 int getMarginLeft() { return m_margin.left; }
417 int getPaddingTop() { return m_padding.top; }
418 int getPaddingRight() { return m_padding.right; }
419 int getPaddingBottom() { return m_padding.bottom; }
420 int getPaddingLeft() { return m_padding.left; }
421 float getOpacity() { return m_opacity; }
422 float getRotation() { return m_rotation; }
423 bool isChangingCursorImage() { return m_changeCursorImage; }
424
425// image
426private:
427 void initImage();
428 void parseImageStyle(const OTMLNodePtr& styleNode);
429
430 void updateImageCache() { m_imageMustRecache = true; }
431 void configureBorderImage() { m_imageBordered = true; updateImageCache(); }
432
433 CoordsBuffer m_imageCoordsBuffer;
434 Rect m_imageCachedScreenCoords;
435 stdext::boolean<true> m_imageMustRecache;
436 stdext::boolean<false> m_imageBordered;
437
438 std::string m_cursor;
439 stdext::boolean<false> m_changeCursorImage;
440
441protected:
442 void drawImage(const Rect& screenCoords);
443
444 TexturePtr m_imageTexture;
445 Rect m_imageClipRect;
446 Rect m_imageRect;
447 Color m_imageColor;
448 Point m_iconOffset;
449 stdext::boolean<false> m_imageFixedRatio;
450 stdext::boolean<false> m_imageRepeated;
451 stdext::boolean<true> m_imageSmooth;
452 stdext::boolean<false> m_imageAutoResize;
453 EdgeGroup<int> m_imageBorder;
454 std::string m_shader;
455
456public:
457 void setQRCode(const std::string& code, int border);
458 void setImageSource(const std::string& source);
459 void setImageSourceBase64(const std::string & data);
460 void setImageClip(const Rect& clipRect) { m_imageClipRect = clipRect; updateImageCache(); }
461 void setImageOffsetX(int x) { m_imageRect.setX(x); updateImageCache(); }
462 void setImageOffsetY(int y) { m_imageRect.setY(y); updateImageCache(); }
463 void setImageOffset(const Point& pos) { m_imageRect.move(pos); updateImageCache(); }
464 void setImageWidth(int width) { m_imageRect.setWidth(width); updateImageCache(); }
465 void setImageHeight(int height) { m_imageRect.setHeight(height); updateImageCache(); }
466 void setImageSize(const Size& size) { m_imageRect.resize(size); updateImageCache(); }
467 void setImageRect(const Rect& rect) { m_imageRect = rect; updateImageCache(); }
468 void setImageColor(const Color& color) { m_imageColor = color; updateImageCache(); }
469 void setImageFixedRatio(bool fixedRatio) { m_imageFixedRatio = fixedRatio; updateImageCache(); }
470 void setImageRepeated(bool repeated) { m_imageRepeated = repeated; updateImageCache(); }
471 void setImageSmooth(bool smooth) { m_imageSmooth = smooth; }
472 void setImageAutoResize(bool autoResize) { m_imageAutoResize = autoResize; }
473 void setImageBorderTop(int border) { m_imageBorder.top = border; configureBorderImage(); }
474 void setImageBorderRight(int border) { m_imageBorder.right = border; configureBorderImage(); }
475 void setImageBorderBottom(int border) { m_imageBorder.bottom = border; configureBorderImage(); }
476 void setImageBorderLeft(int border) { m_imageBorder.left = border; configureBorderImage(); }
477 void setImageBorder(int border) { m_imageBorder.set(border); configureBorderImage(); }
478 void setImageShader(const std::string& str) { m_shader = str; }
479
480 Rect getImageClip() { return m_imageClipRect; }
481 int getImageOffsetX() { return m_imageRect.x(); }
482 int getImageOffsetY() { return m_imageRect.y(); }
483 Point getImageOffset() { return m_imageRect.topLeft(); }
484 int getImageWidth() { return m_imageRect.width(); }
485 int getImageHeight() { return m_imageRect.height(); }
486 Size getImageSize() { return m_imageRect.size(); }
487 Rect getImageRect() { return m_imageRect; }
488 Color getImageColor() { return m_imageColor; }
489 bool isImageFixedRatio() { return m_imageFixedRatio; }
490 bool isImageSmooth() { return m_imageSmooth; }
491 bool isImageAutoResize() { return m_imageAutoResize; }
492 int getImageBorderTop() { return m_imageBorder.top; }
493 int getImageBorderRight() { return m_imageBorder.right; }
494 int getImageBorderBottom() { return m_imageBorder.bottom; }
495 int getImageBorderLeft() { return m_imageBorder.left; }
496 int getImageTextureWidth() { return m_imageTexture ? m_imageTexture->getWidth() : 0; }
497 int getImageTextureHeight() { return m_imageTexture ? m_imageTexture->getHeight() : 0; }
498 std::string getImageShader() { return m_shader; }
499
500// text related
501private:
502 void initText();
503 void parseTextStyle(const OTMLNodePtr& styleNode);
504
505 stdext::boolean<true> m_textMustRecache;
506 Rect m_textCachedScreenCoords;
507
508protected:
509 virtual void updateText();
510 void drawText(const Rect& screenCoords);
511
512 virtual void onTextChange(const std::string& text, const std::string& oldText);
513 virtual void onFontChange(const std::string& font);
514
515 std::string m_text;
516 std::string m_drawText;
517 Fw::AlignmentFlag m_textAlign;
518 Point m_textOffset;
519 stdext::boolean<false> m_textWrap;
520 stdext::boolean<false> m_textVerticalAutoResize;
521 stdext::boolean<false> m_textHorizontalAutoResize;
522 stdext::boolean<false> m_textOnlyUpperCase;
523 BitmapFontPtr m_font;
524 std::vector<std::pair<int, Color>> m_textColors;
525 std::vector<std::pair<int, Color>> m_drawTextColors;
526 stdext::boolean<false> m_shadow;
527
528public:
529 void resizeToText() { setSize(getTextSize()); }
530 void clearText() { setText(""); }
531
532 void setText(std::string text, bool dontFireLuaCall = false);
533 void setColoredText(const std::vector<std::string>& texts, bool dontFireLuaCall = false);
534 void setTextAlign(Fw::AlignmentFlag align) { m_textAlign = align; updateText(); }
535 void setTextOffset(const Point& offset) { m_textOffset = offset; updateText(); }
536 void setTextWrap(bool textWrap) { m_textWrap = textWrap; updateText(); }
537 void setTextAutoResize(bool textAutoResize) { m_textHorizontalAutoResize = textAutoResize; m_textVerticalAutoResize = textAutoResize; updateText(); }
538 void setTextHorizontalAutoResize(bool textAutoResize) { m_textHorizontalAutoResize = textAutoResize; updateText(); }
539 void setTextVerticalAutoResize(bool textAutoResize) { m_textVerticalAutoResize = textAutoResize; updateText(); }
540 void setTextOnlyUpperCase(bool textOnlyUpperCase) { m_textOnlyUpperCase = textOnlyUpperCase; setText(m_text); }
541 void setFont(const std::string& fontName);
542 void setShadow(bool shadow) { m_shadow = shadow; }
543
544 std::string getText() { return m_text; }
545 std::string getDrawText() { return m_drawText; }
546 Fw::AlignmentFlag getTextAlign() { return m_textAlign; }
547 Point getTextOffset() { return m_textOffset; }
548 bool getTextWrap() { return m_textWrap; }
549 std::string getFont() { return m_font->getName(); }
550 Size getTextSize() { return m_font->calculateTextRectSize(m_drawText); }
551};
552
553#endif
554
Top