Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_skin.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_SKIN_H
7 #define TB_SKIN_H
8 
9 #include "tb_core.h"
10 #include "tb_system.h"
11 #include "tb_renderer.h"
12 #include "tb_bitmap_fragment.h"
13 #include "tb_hashtable.h"
14 #include "tb_linklist.h"
15 #include "tb_dimension.h"
16 #include "tb_value.h"
17 
18 namespace tb {
19 
20 class TBNode;
21 class TBSkinConditionContext;
22 
24 #define SKIN_VALUE_NOT_SPECIFIED TB_INVALID_DIMENSION
25 
28 enum SKIN_STATE {
29  SKIN_STATE_NONE = 0,
30  SKIN_STATE_DISABLED = 1,
31  SKIN_STATE_FOCUSED = 2,
32  SKIN_STATE_PRESSED = 4,
33  SKIN_STATE_SELECTED = 8,
34  SKIN_STATE_HOVERED = 16,
35 
36  SKIN_STATE_ALL = SKIN_STATE_DISABLED |
37  SKIN_STATE_FOCUSED |
38  SKIN_STATE_PRESSED |
39  SKIN_STATE_SELECTED |
40  SKIN_STATE_HOVERED
41 };
42 MAKE_ENUM_FLAG_COMBO(SKIN_STATE);
43 
51  SKIN_ELEMENT_TYPE_IMAGE
52 };
53 
58 class TBSkinCondition : public TBLinkOf<TBSkinCondition>
59 {
60 public:
62  enum TARGET {
68  };
70  enum PROPERTY {
82  };
83 
85  enum TEST {
88  };
89 
91  struct CONDITION_INFO {
95  };
96 
97  TBSkinCondition(TARGET target, PROPERTY prop, const TBID &custom_prop, const TBID &value, TEST test);
98 
100  bool GetCondition(TBSkinConditionContext &context) const;
101 private:
102  TARGET m_target;
103  CONDITION_INFO m_info;
104  TEST m_test;
105 };
106 
112 {
113 public:
115  virtual bool GetCondition(TBSkinCondition::TARGET target, const TBSkinCondition::CONDITION_INFO &info) = 0;
116 };
117 
122 class TBSkinElementState : public TBLinkOf<TBSkinElementState>
123 {
124 public:
126  enum MATCH_RULE {
131  };
132 
133  bool IsMatch(SKIN_STATE state, TBSkinConditionContext &context,
134  MATCH_RULE rule = MATCH_RULE_DEFAULT) const;
135 
136  bool IsExactMatch(SKIN_STATE state, TBSkinConditionContext &context,
137  MATCH_RULE rule = MATCH_RULE_DEFAULT) const;
138 
139  TBID element_id;
140  SKIN_STATE state;
142 };
143 
147 {
148 public:
150 
151  TBSkinElementState *GetStateElement(SKIN_STATE state, TBSkinConditionContext &context,
153 
154  TBSkinElementState *GetStateElementExactMatch(SKIN_STATE state, TBSkinConditionContext &context,
156 
157  bool HasStateElements() const { return m_state_elements.HasLinks(); }
158  const TBSkinElementState *GetFirstElement() const { return m_state_elements.GetFirst(); }
159 
160  void Load(TBNode *n);
161 private:
162  TBLinkListOf<TBSkinElementState> m_state_elements;
163 };
164 
169 {
170 public:
171  TBSkinElement();
172  ~TBSkinElement();
173 
174  // Skin properties
179  uint8_t cut;
180  int16_t expand;
182  bool is_painting;
183  bool is_getting;
184  int16_t padding_left;
185  int16_t padding_top;
186  int16_t padding_right;
187  int16_t padding_bottom;
188  int16_t width;
189  int16_t height;
190  int16_t pref_width;
191  int16_t pref_height;
192  int16_t min_width;
193  int16_t min_height;
194  int16_t max_width;
195  int16_t max_height;
196  int16_t spacing;
197  int16_t content_ofs_x;
198  int16_t content_ofs_y;
199  int16_t img_ofs_x;
200  int16_t img_ofs_y;
201  int8_t img_position_x;
202  int8_t img_position_y;
204  int8_t flip_x;
206  int8_t flip_y;
207  float opacity;
211  int16_t bitmap_dpi;
213 
215  int GetMinWidth() const { return min_width; }
216 
218  int GetMinHeight() const { return min_height; }
219 
221  int GetIntrinsicMinWidth() const;
222 
224  int GetIntrinsicMinHeight() const;
225 
227  int GetMaxWidth() const { return max_width; }
228 
230  int GetMaxHeight() const { return max_height; }
231 
233  int GetPrefWidth() const { return pref_width; }
234 
236  int GetPrefHeight() const { return pref_height; }
237 
241  int GetIntrinsicWidth() const;
242 
246  int GetIntrinsicHeight() const;
247 
250  void SetBitmapDPI(const TBDimensionConverter &dim_conv, int bitmap_dpi);
251 
254 
257 
260 
263 
267  bool HasState(SKIN_STATE state, TBSkinConditionContext &context);
268 
270  bool HasOverlayElements() const { return m_overlay_elements.HasStateElements(); }
271 
272  void Load(TBNode *n, TBSkin *skin, const TBStr & skin_path);
273  void Write(TBFile * file, TBSkin * skin);
274 };
275 
277 {
278 public:
283  virtual void OnSkinElementLoaded(TBSkin *skin, TBSkinElement *element, TBNode *node) = 0;
284 };
285 
287 class TBSkin : private TBRendererListener
288 {
289 public:
290  TBSkin();
291  virtual ~TBSkin();
292 
294  void SetListener(TBSkinListener *listener) { m_listener = listener; }
295  TBSkinListener *GetListener() const { return m_listener; }
296 
305  bool Load(const TBStr & skin_file, const TBStr & override_skin_file = TBStr());
306 
308  bool Write(const TBStr & skin_file);
309 
311  void UnloadBitmaps();
312 
315  bool ReloadBitmaps();
316 
319  const TBDimensionConverter *GetDimensionConverter() const { return &m_dim_conv; }
320 
323  TBSkinElement *GetSkinElement(const TBID &skin_id) const;
324 
330  TBSkinConditionContext &context) const;
331 
333  TBColor GetDefaultTextColor() const { return m_default_text_color; }
334 
336  void SetDefaultTextColor(TBColor color) { m_default_text_color = color; }
337 
339  float GetDefaultDisabledOpacity() const { return m_default_disabled_opacity; }
340 
342  float GetDefaultPlaceholderOpacity() const { return m_default_placeholder_opacity; }
343 
345  int GetDefaultSpacing() const { return m_default_spacing; }
346 
374  TBSkinElement *PaintSkin(const TBRect &dst_rect, const TBID &skin_id, SKIN_STATE state,
375  TBSkinConditionContext &context);
376 
379  TBSkinElement *PaintSkin(const TBRect &dst_rect, TBSkinElement *element, SKIN_STATE state,
380  TBSkinConditionContext &context);
381 
383  void PaintSkinOverlay(const TBRect &dst_rect, TBSkinElement *element, SKIN_STATE state,
384  TBSkinConditionContext &context);
385 
387  void PaintRect(const TBRect &dst_rect, const TBColor &color, int thickness);
388 
390  void PaintRectFill(const TBRect &dst_rect, const TBColor &color);
391 
392 #ifdef TB_RUNTIME_DEBUG_INFO
393 
394  void Debug();
395 #endif
396 
398  TBBitmapFragmentManager *GetFragmentManager() { return &m_frag_manager; }
399 
400  // Implementing TBRendererListener
401  virtual void OnContextLost();
402  virtual void OnContextRestored();
403 private:
404  friend class TBSkinElement;
405  TBSkinListener *m_listener;
407  TBBitmapFragmentManager m_frag_manager;
408  TBDimensionConverter m_dim_conv;
409  TBColor m_default_text_color;
410  TBBitmapFragment *m_color_frag;
411  float m_default_disabled_opacity;
412  float m_default_placeholder_opacity;
413  int16_t m_default_spacing;
414  bool LoadInternal(const TBStr & skin_file);
415  bool ReloadBitmapsInternal();
416  void PaintElement(const TBRect &dst_rect, TBSkinElement *element);
417  void PaintElementBGColor(const TBRect &dst_rect, TBSkinElement *element);
418  void PaintElementImage(const TBRect &dst_rect, TBSkinElement *element);
419  void PaintElementTile(const TBRect &dst_rect, TBSkinElement *element);
420  void PaintElementStretchImage(const TBRect &dst_rect, TBSkinElement *element);
421  void PaintElementStretchBox(const TBRect &dst_rect, TBSkinElement *element, bool fill_center);
422  TBRect GetFlippedRect(const TBRect &src_rect, TBSkinElement *element) const;
423  int GetPxFromNode(TBNode *node, int def_value) const;
424 };
425 
426 } // namespace tb
427 
428 #endif // TB_SKIN_H
int16_t height
Intrinsic height or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:189
int16_t width
Intrinsic width or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:188
TBBitmapFragment * bitmap
Bitmap fragment containing the graphics, or nullptr.
Definition: tb_skin.h:178
All ancestors of the object painting the skin.
Definition: tb_skin.h:65
int16_t img_ofs_y
Y offset for type image. Relative to image position (img_position_y).
Definition: tb_skin.h:200
virtual void OnContextLost()
Called when the context has been lost and all TBBitmaps need to be deleted.
Definition: tb_skin.cpp:639
int GetDefaultSpacing() const
Get the default layout spacing in pixels.
Definition: tb_skin.h:345
int16_t pref_height
Preferred height or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:191
SKIN_ELEMENT_TYPE type
Skin element type.
Definition: tb_skin.h:181
TBBitmapFragmentManager manages loading bitmaps of arbitrary size, pack as many of them into as few T...
Definition: tb_bitmap_fragment.h:177
Focus is on the target or any child (no value required).
Definition: tb_skin.h:80
int8_t flip_x
The skin is flipped horizontally.
Definition: tb_skin.h:205
TBID custom_prop
Which property (only if prop is PROPERTY_CUSTOM).
Definition: tb_skin.h:93
void SetListener(TBSkinListener *listener)
Set the listener for this skin.
Definition: tb_skin.h:294
The object painting the skin.
Definition: tb_skin.h:63
TBSkinElement * GetSkinElement(const TBID &skin_id) const
Get the skin element with the given id.
Definition: tb_skin.cpp:357
TBSkinElement * GetSkinElementStrongOverride(const TBID &skin_id, SKIN_STATE state, TBSkinConditionContext &context) const
Get the skin element with the given id and state.
Definition: tb_skin.cpp:364
const TBDimensionConverter * GetDimensionConverter() const
Get the dimension converter used for the current skin.
Definition: tb_skin.h:319
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:112
Same as above, but dont fill the center.
Definition: tb_skin.h:48
bool HasOverlayElements() const
Return true if this element has overlay elements.
Definition: tb_skin.h:270
bool Load(const TBStr &skin_file, const TBStr &override_skin_file=TBStr())
Load the skin file and the bitmaps it refers to.
Definition: tb_skin.cpp:155
TBSkinElementStateList m_child_elements
List of child elements (See TBSkin::PaintSkin)
Definition: tb_skin.h:259
TBColor bitmap_color
Color to paint the bitmap with. (for now assume text_color if true).
Definition: tb_skin.h:210
int16_t padding_bottom
Bottom padding for any content in the element.
Definition: tb_skin.h:187
Value should be equal for condition to be true.
Definition: tb_skin.h:86
TEST
Defines if the condition tested should be equal or not for the condition to be true.
Definition: tb_skin.h:85
int16_t pref_width
Preferred width or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:190
It&#39;s a property unknown to skin, that the TBSkinConditionContext might know about.
Definition: tb_skin.h:81
States with &quot;all&quot; (SKIN_STATE_ALL) will also be considered a match.
Definition: tb_skin.h:128
The id.
Definition: tb_skin.h:75
int16_t content_ofs_y
Y offset of the content in the widget.
Definition: tb_skin.h:198
The state is set.
Definition: tb_skin.h:76
bool GetCondition(TBSkinConditionContext &context) const
Return true if the condition is true for the given context.
Definition: tb_skin.cpp:134
TBColor GetDefaultTextColor() const
Get the default text color for all skin elements.
Definition: tb_skin.h:333
Default element type, cut bitmap into 9 pieces &quot;cut&quot; wide.
Definition: tb_skin.h:46
float GetDefaultDisabledOpacity() const
Get the default disabled opacity for all skin elements.
Definition: tb_skin.h:339
TBStr name
Name of the skin element, f.ex &quot;TBSelectDropdown.arrow&quot;.
Definition: tb_skin.h:176
Scale the bitmap to the dest rect.
Definition: tb_skin.h:49
int16_t min_width
Minimum width or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:192
int16_t max_width
Maximum width or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:194
int16_t padding_left
Left padding for any content in the element.
Definition: tb_skin.h:184
TBSkin contains a list of TBSkinElement.
Definition: tb_skin.h:287
int8_t img_position_y
Vertical position for type image.
Definition: tb_skin.h:203
SKIN_ELEMENT_TYPE
Type of painting that should be done for a TBSkinElement.
Definition: tb_skin.h:45
int16_t content_ofs_x
X offset of the content in the widget.
Definition: tb_skin.h:197
TBBitmapFragmentManager * GetFragmentManager()
Get the fragment manager.
Definition: tb_skin.h:398
int16_t min_height
Minimum height or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:193
TBColor bg_color
Color of the background in the widget.
Definition: tb_skin.h:209
Skin element.
Definition: tb_skin.h:168
int GetMaxHeight() const
Get the maximum height, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:230
bool Write(const TBStr &skin_file)
Write the current skin to a file.
Definition: tb_skin.cpp:257
TARGET
Defines which target(s) relative to the context that should be tested for the condition.
Definition: tb_skin.h:62
TBStr bitmap_file
File name of the bitmap (might be empty)
Definition: tb_skin.h:177
int GetMinWidth() const
Get the minimum width, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:215
int8_t img_position_x
Horizontal position for type image.
Definition: tb_skin.h:201
TBRendererListener is a listener for TBRenderer.
Definition: tb_renderer.h:19
TBID is a wrapper for a uint32_t to be used as ID.
Definition: tb_id.h:18
The previous sibling of the object painting the skin.
Definition: tb_skin.h:66
int GetIntrinsicMinWidth() const
Get the intrinsic minimum width.
Definition: tb_skin.cpp:685
int16_t padding_right
Right padding for any content in the element.
Definition: tb_skin.h:186
TBBitmapFragment represents a sub part of a TBBitmap.
Definition: tb_bitmap_fragment.h:140
void SetBitmapDPI(const TBDimensionConverter &dim_conv, int bitmap_dpi)
Set the DPI that the bitmap was loaded in.
Definition: tb_skin.cpp:723
void SetDefaultTextColor(TBColor color)
Set the default text color for all skin elements.
Definition: tb_skin.h:336
virtual void OnSkinElementLoaded(TBSkin *skin, TBSkinElement *element, TBNode *node)=0
Called when a skin element has been loaded from the given TBNode.
Focus is on the target or any child (no value required).
Definition: tb_skin.h:78
int16_t max_height
Maximum height or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:195
int16_t expand
How much the skin should expand outside the widgets rect.
Definition: tb_skin.h:180
TBSkinElementStateList m_override_elements
List of override elements (See TBSkin::PaintSkin)
Definition: tb_skin.h:253
int GetIntrinsicWidth() const
Get the intrinsic width.
Definition: tb_skin.cpp:703
TBDimensionConverter converts device independant points to pixels, based on two DPI values...
Definition: tb_dimension.h:26
void PaintSkinOverlay(const TBRect &dst_rect, TBSkinElement *element, SKIN_STATE state, TBSkinConditionContext &context)
Paint the overlay elements for the given skin element and state.
Definition: tb_skin.cpp:450
int16_t bitmap_dpi
The DPI of the bitmap that was loaded.
Definition: tb_skin.h:211
Tile the bitmap to the dest rect.
Definition: tb_skin.h:50
Value should not be equal for condition to be true.
Definition: tb_skin.h:87
The alignment.
Definition: tb_skin.h:74
TBValue tag
This value is free to use for anything. It&#39;s not used internally.
Definition: tb_skin.h:212
Definition: tb_skin.h:276
PROPERTY prop
Which property.
Definition: tb_skin.h:92
The background skin id.
Definition: tb_skin.h:71
TBStr is a simple string class.
Definition: tb_str.h:62
TBNode is a tree node with a string name and a value (TBValue).
Definition: tb_node_tree.h:30
The current value (integer).
Definition: tb_skin.h:77
Simple rectangle class.
Definition: tb_geometry.h:25
TBValue holds value of a specific type.
Definition: tb_value.h:59
int GetMinHeight() const
Get the minimum height, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:218
The window is active (no value required).
Definition: tb_skin.h:72
bool is_painting
If the skin is being painted (avoiding eternal recursing)
Definition: tb_skin.h:182
TBSkinElementStateList m_overlay_elements
List of overlay elements (See TBSkin::PaintSkin)
Definition: tb_skin.h:262
TBFile is a portable interface for file access.
Definition: tb_system.h:69
float GetDefaultPlaceholderOpacity() const
Get the default placeholder opacity for all skin elements.
Definition: tb_skin.h:342
float opacity
Opacity that should be used for the whole widget (0.f - 1.f).
Definition: tb_skin.h:207
int GetMaxWidth() const
Get the maximum width, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:227
TBSkinElementState has a skin element id that should be used if its state and condition matches that ...
Definition: tb_skin.h:122
TBColor contains a 32bit color.
Definition: tb_color.h:21
The next sibling of the object painting the skin.
Definition: tb_skin.h:67
Stores the information needed for checking a condition.
Definition: tb_skin.h:91
int8_t flip_y
The skin is flipped vertically.
Definition: tb_skin.h:206
bool HasState(SKIN_STATE state, TBSkinConditionContext &context)
Check if there&#39;s a exact or partial match for the given state in either override, child or overlay el...
Definition: tb_skin.cpp:753
int16_t padding_top
Top padding for any content in the element.
Definition: tb_skin.h:185
int GetIntrinsicHeight() const
Get the intrinsic height.
Definition: tb_skin.cpp:713
Capture is on the target or any child (no value required).
Definition: tb_skin.h:79
void UnloadBitmaps()
Unload all bitmaps used in this skin.
Definition: tb_skin.cpp:272
void PaintRectFill(const TBRect &dst_rect, const TBColor &color)
Paint a filled rectangle with the given color.
Definition: tb_skin.cpp:525
States with &quot;all&quot; will not be considered a match.
Definition: tb_skin.h:130
SKIN_STATE
Skin state types (may be combined).
Definition: tb_skin.h:28
TBSkinCondition checks if a condition is true for a given TBSkinConditionContext. ...
Definition: tb_skin.h:58
TBID id
ID of the skin element.
Definition: tb_skin.h:175
The axis of the content (x or y)
Definition: tb_skin.h:73
int GetIntrinsicMinHeight() const
Get the intrinsic minimum height.
Definition: tb_skin.cpp:694
TBColor text_color
Color of the text in the widget.
Definition: tb_skin.h:208
List of state elements in a TBSkinElement.
Definition: tb_skin.h:146
TBID value
The value to compare.
Definition: tb_skin.h:94
int GetPrefWidth() const
Get the preferred width, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:233
int GetPrefHeight() const
Get the preferred height, or SKIN_VALUE_NOT_SPECIFIED if not specified.
Definition: tb_skin.h:236
bool is_getting
If the skin is being got (avoiding eternal recursion)
Definition: tb_skin.h:183
void PaintRect(const TBRect &dst_rect, const TBColor &color, int thickness)
Paint a rectangle outline inside dst_rect with the given thickness and color.
Definition: tb_skin.cpp:505
bool ReloadBitmaps()
Reload all bitmaps used in this skin.
Definition: tb_skin.cpp:284
virtual void OnContextRestored()
Called when the context has been restored again, and new TBBitmaps can be created again...
Definition: tb_skin.cpp:648
TBSkinElementStateList m_strong_override_elements
List of strong-override elements (See TBSkin::PaintSkin)
Definition: tb_skin.h:256
TBSkinElement * PaintSkin(const TBRect &dst_rect, const TBID &skin_id, SKIN_STATE state, TBSkinConditionContext &context)
Paint the skin at dst_rect.
Definition: tb_skin.cpp:391
int16_t img_ofs_x
X offset for type image. Relative to image position (img_position_x).
Definition: tb_skin.h:199
int16_t spacing
Spacing used on layout or SKIN_VALUE_NOT_SPECIFIED.
Definition: tb_skin.h:196
TBSkinConditionContext checks if a condition is true.
Definition: tb_skin.h:111
The parent of the object painting the skin.
Definition: tb_skin.h:64
virtual bool GetCondition(TBSkinCondition::TARGET target, const TBSkinCondition::CONDITION_INFO &info)=0
Return true if the given target and property equals the given value.
PROPERTY
Defines which property in the context that should be checked.
Definition: tb_skin.h:70
uint8_t cut
How the bitmap should be sliced using StretchBox.
Definition: tb_skin.h:179
MATCH_RULE
Defines how to match states.
Definition: tb_skin.h:126