MyGUI 3.5.3
MyGUI_Widget.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_WIDGET_H_
8#define MYGUI_WIDGET_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_Any.h"
12#include "MyGUI_EventPair.h"
15#include "MyGUI_WidgetInput.h"
16#include "MyGUI_ResourceSkin.h"
18#include "MyGUI_IObject.h"
19#include "MyGUI_SkinItem.h"
22
23namespace MyGUI
24{
25
26 struct Vertex;
27 struct RenderTargetInfo;
28
32
36 class MYGUI_EXPORT Widget :
37 public IObject,
38 public ICroppedRectangle,
39 public UserData,
40 public WidgetInput,
41 public delegates::IDelegateUnlink,
42 public SkinItem,
43 public WidgetContainer,
44 public MemberObsolete<Widget>
45 {
46 // To call private destructors
47 friend class WidgetManager;
48
49 MYGUI_RTTI_DERIVED(Widget)
50
51 public:
52 Widget();
53
61 Widget* createWidgetT(
62 std::string_view _type,
63 std::string_view _skin,
64 const IntCoord& _coord,
65 Align _align,
66 std::string_view _name = {});
67
69 Widget* createWidgetT(
70 std::string_view _type,
71 std::string_view _skin,
72 int _left,
73 int _top,
74 int _width,
75 int _height,
76 Align _align,
77 std::string_view _name = {});
78
80 Widget* createWidgetRealT(
81 std::string_view _type,
82 std::string_view _skin,
83 const FloatCoord& _coord,
84 Align _align,
85 std::string_view _name = {});
86
88 Widget* createWidgetRealT(
89 std::string_view _type,
90 std::string_view _skin,
91 float _left,
92 float _top,
93 float _width,
94 float _height,
95 Align _align,
96 std::string_view _name = {});
97
98 // templates for creating widgets by type
100 template<typename T>
101 T* createWidget(std::string_view _skin, const IntCoord& _coord, Align _align, std::string_view _name = {})
102 {
103 return static_cast<T*>(createWidgetT(T::getClassTypeName(), _skin, _coord, _align, _name));
104 }
105
107 template<typename T>
108 T* createWidget(
109 std::string_view _skin,
110 int _left,
111 int _top,
112 int _width,
113 int _height,
114 Align _align,
115 std::string_view _name = {})
116 {
117 return static_cast<T*>(
118 createWidgetT(T::getClassTypeName(), _skin, IntCoord(_left, _top, _width, _height), _align, _name));
119 }
120
122 template<typename T>
123 T* createWidgetReal(std::string_view _skin, const FloatCoord& _coord, Align _align, std::string_view _name = {})
124 {
125 return static_cast<T*>(createWidgetRealT(T::getClassTypeName(), _skin, _coord, _align, _name));
126 }
127
129 template<typename T>
130 T* createWidgetReal(
131 std::string_view _skin,
132 float _left,
133 float _top,
134 float _width,
135 float _height,
136 Align _align,
137 std::string_view _name = {})
138 {
139 return static_cast<T*>(
140 createWidgetRealT(T::getClassTypeName(), _skin, _left, _top, _width, _height, _align, _name));
141 }
142
152 Widget* createWidgetT(
153 WidgetStyle _style,
154 std::string_view _type,
155 std::string_view _skin,
156 const IntCoord& _coord,
157 Align _align,
158 std::string_view _layer = {},
159 std::string_view _name = {});
160
162 template<typename T>
163 T* createWidget(
164 WidgetStyle _style,
165 std::string_view _skin,
166 const IntCoord& _coord,
167 Align _align,
168 std::string_view _layer = {},
169 std::string_view _name = {})
170 {
171 return static_cast<T*>(createWidgetT(_style, T::getClassTypeName(), _skin, _coord, _align, _layer, _name));
172 }
173
175 void setPosition(const IntPoint& _point) override;
177 void setSize(const IntSize& _size) override;
179 void setCoord(const IntCoord& _coord) override;
180
182 void setPosition(int _left, int _top);
184 void setSize(int _width, int _height);
186 void setCoord(int _left, int _top, int _width, int _height);
187
189 void setRealPosition(const FloatPoint& _point);
191 void setRealSize(const FloatSize& _size);
193 void setRealCoord(const FloatCoord& _coord);
194
196 void setRealPosition(float _left, float _top);
198 void setRealSize(float _width, float _height);
200 void setRealCoord(float _left, float _top, float _width, float _height);
201
203 const std::string& getName() const;
204
206 virtual void setVisible(bool _value);
208 bool getVisible() const;
209
214 void setDepth(int _value);
216 int getDepth() const;
217
219 bool getInheritedVisible() const;
220
222 virtual void setAlign(Align _value);
224 Align getAlign() const;
225
227 void setRotation(float _radians);
228 float getRotation() const;
230 void setRotationCenter(const FloatPoint& _center);
231 FloatPoint getRotationCenter() const;
233 FloatPoint localToLayer(const FloatPoint& _point) const;
235 void localToLayer(FloatPoint* _points, size_t _count) const;
236 FloatPoint layerToLocal(const FloatPoint& _point) const;
238 FloatPoint rotatePoint(const FloatPoint& _point) const;
239 FloatPoint unrotatePoint(const FloatPoint& _point) const;
241 IntPoint unrotateVector(const IntPoint& _vector) const;
242
244 void setAlpha(float _alpha);
246 float getAlpha() const;
247
258 void setInheritsAlpha(bool _inherits);
260 bool getInheritsAlpha() const;
261
262 void setColour(const Colour& _value);
263
265 bool isRootWidget() const;
266
268 Widget* getParent() const;
269
271 IntSize getParentSize() const;
272
274 const VectorWidgetPtr& getChildWidgets() const;
275
277 size_t getChildCount() const;
278
280 Widget* getChildAt(size_t _index) const;
281
286 Widget* findWidget(std::string_view _name);
287
291 void findWidgets(std::string_view _name, VectorWidgetPtr& _result);
292
294 virtual void setEnabled(bool _value);
296 void setEnabledSilent(bool _value);
298 bool getEnabled() const;
299
301 bool getInheritedEnabled() const;
302
304 IntCoord getClientCoord() const;
305
307 Widget* getClientWidget();
308 const Widget* getClientWidget() const;
309
313 void detachFromWidget(std::string_view _layer = {});
314
320 void attachToWidget(Widget* _parent, WidgetStyle _style = WidgetStyle::Child, std::string_view _layer = {});
321
323 void changeWidgetSkin(std::string_view _skinName);
324
330 void setWidgetStyle(WidgetStyle _style, std::string_view _layer = {});
332 WidgetStyle getWidgetStyle() const;
333
338 void setProperty(std::string_view _key, std::string_view _value);
339
346 EventHandle_WidgetStringString eventChangeProperty;
347
352 EventHandle_WidgetVoid eventChangeCoord;
353
359 EventHandle_WidgetVoid eventChangeAbsoluteCoord;
360
365 EventHandle_WidgetVoid eventWidgetDestroyed;
366
367 /*internal:*/
368 // Method for requesting item index from container
369 virtual size_t _getItemIndex(Widget* _item) const;
370
371 // Gives priority to widget during picking
372 void _forcePick(Widget* _widget);
373
374 void _initialise(
375 WidgetStyle _style,
376 const IntCoord& _coord,
377 std::string_view _skinName,
378 Widget* _parent,
379 ICroppedRectangle* _croppedParent,
380 std::string_view _name);
381 void _shutdown();
382
383 void _destroyChildWidget(Widget* _widget) override;
384
385 void _setContainer(Widget* _value);
386 Widget* _getContainer() const;
387
388 void _setAlign(const IntSize& _oldsize, const IntSize& _newSize);
389 bool _checkPoint(int _left, int _top) const;
390 void _transformVertices(Vertex* _vertices, size_t _count, const RenderTargetInfo& _info) const;
391 bool _hasRotation() const override;
392
393 Widget* _createSkinWidget(
394 WidgetStyle _style,
395 std::string_view _type,
396 std::string_view _skin,
397 const IntCoord& _coord,
398 Align _align,
399 std::string_view _layer = {},
400 std::string_view _name = {});
401
402 // Reset all container data including tooltips
403 virtual void _resetContainer(bool _update);
404
405 bool _setWidgetState(std::string_view _state);
406
407 void _updateChilds(bool _fullSort);
408
409 protected:
410 // All creation is only through factory
411 ~Widget() override;
412
413 virtual void shutdownOverride();
414 virtual void initialiseOverride();
415
416 void _updateView();
417
418 using WidgetContainer::baseCreateWidget;
419
420 Widget* baseCreateWidget(
421 WidgetStyle _style,
422 std::string_view _type,
423 std::string_view _skin,
424 const IntCoord& _coord,
425 Align _align,
426 std::string_view _layer,
427 std::string_view _name,
428 bool _template);
429
430
431 // Request item index from container by mouse position
432 virtual size_t _getContainerIndex(const IntPoint& _point) const;
433
434 virtual void baseUpdateEnable();
435
436 ILayerItem* getLayerItemByPoint(int _left, int _top) const override;
437 const IntCoord& getLayerItemCoord() const override;
438
439 template<typename T>
440 void assignWidget(T*& _widget, std::string_view _name)
441 {
442 _widget = nullptr;
443 for (const auto& iter : mWidgetChildSkin)
444 {
445 Widget* find = iter->findWidget(_name);
446 if (nullptr != find)
447 {
448 _widget = find->castType<T>(false);
449 break;
450 }
451 }
452 }
453
454 VectorWidgetPtr getSkinWidgetsByName(std::string_view _name) const;
455
456 virtual void onWidgetCreated(Widget* _widget);
457 virtual void onWidgetDestroy(Widget* _widget);
458
459 void setWidgetClient(Widget* _widget);
461 Widget* _getClientWidget();
462 const Widget* _getClientWidget() const;
463
464 virtual void setPropertyOverride(std::string_view _key, std::string_view _value);
465
466 private:
467 struct RotationTransform
468 {
469 float cosine{1.0f};
470 float sine{0.0f};
471 float offsetX{0.0f};
472 float offsetY{0.0f};
473
474 FloatPoint transformPoint(const FloatPoint& _point) const;
475 FloatPoint inverseTransformPoint(const FloatPoint& _point) const;
476 };
477
478 const WidgetInfo* initialiseWidgetSkinBase(ResourceSkin* _skinInfo, ResourceLayout* _templateInfo);
479 void shutdownWidgetSkinBase();
480
481 void _updateAlpha();
482 void _updateCoord(const IntSize& _oldSize);
483 void _updateGeometry(const IntSize* _oldSize = nullptr);
484 // Detach hierarchy and layers without updating geometry
485 void _detachFromWidget(std::string_view _layer);
486
487 void _updateVisible();
488
489 void _updateEnabled();
490
491 float _getRealAlpha() const;
492
493 void _createChildSkinWidget(ResourceSkin* _info);
494 void _destroyChildSkinWidget();
495
496 void _parseSkinProperties(ResourceSkin* _info);
497 void _checkInheristProperties();
498 template<typename T>
499 ILayerItem* getLayerItemByLocalPoint(types::TPoint<T> _point) const;
500 ILayerItem* getLayerItemByRotatedPoint(int _left, int _top) const;
501 ILayerItem* pickChildrenOrSelf(IntPoint _point) const;
502 void updateRotationTransform();
503 void updateWorldHasRotation();
504 RotationTransform getWorldRotationTransform() const;
505 void invalidateRotation();
506
507 void _linkChildWidget(Widget* _widget) override;
508
509 void setSkinProperty(ResourceSkin* _info);
510
511 void resizeLayerItemView(const IntSize& _oldView, const IntSize& _newView) override;
512
513 private:
514 // Client area of the widget
515 // If widget has child windows not within itself, must initialize Client
516 Widget* mWidgetClient{nullptr};
517
518 // Child widgets created from skin
519 VectorWidgetPtr mWidgetChildSkin;
520
521 // availability for mouse/keyboard
522 bool mEnabled{true};
523 bool mInheritedEnabled{true};
524 // visibility on screen and for mouse
525 bool mVisible{true};
526 bool mInheritedVisible{true};
527 // transparency and transparency inheritance
528 float mAlpha{ALPHA_MAX};
529 float mRealAlpha{ALPHA_MAX};
530 bool mInheritsAlpha{true};
531 std::string mName;
532
533 // parent in widgets hierarchy
534 Widget* mParent{nullptr};
535
536 WidgetStyle mWidgetStyle{WidgetStyle::Child};
537
538 Widget* mContainer{nullptr};
539
540 Align mAlign{Align::Default};
541 float mRotation{0.0f};
542 FloatPoint mRotationCenter;
543 bool mCustomRotationCenter{false};
544 // Local to parent coordinates (absolute layout coordinates for popups).
545 RotationTransform mRotationTransform;
546 // True when this widget or any logical ancestor has a nonzero rotation.
547 bool mWorldHasRotation{false};
548 int mDepth{0};
549 };
550
551} // namespace MyGUI
552
553#endif // MYGUI_WIDGET_H_
#define MYGUI_EXPORT
#define MYGUI_RTTI_DERIVED(DerivedType)
Definition MyGUI_RTTI.h:67
constexpr float ALPHA_MAX
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TCoord< float > FloatCoord
Definition MyGUI_Types.h:36
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
delegates::MultiDelegate< Widget * > EventHandle_WidgetVoid
EventPairConvertStringView< delegates::MultiDelegate< Widget *, const std::string &, const std::string & >, delegates::MultiDelegate< Widget *, std::string_view, std::string_view > > EventHandle_WidgetStringString
types::TSize< float > FloatSize
Definition MyGUI_Types.h:30
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
std::vector< Widget * > VectorWidgetPtr