MyGUI 3.5.3
MyGUI_Widget.cpp
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#include "MyGUI_Precompiled.h"
8#include "MyGUI_Widget.h"
9#include "MyGUI_Gui.h"
10#include "MyGUI_InputManager.h"
11#include "MyGUI_SkinManager.h"
12#include "MyGUI_WidgetManager.h"
13#include "MyGUI_ResourceSkin.h"
14#include "MyGUI_WidgetDefines.h"
15#include "MyGUI_LayerItem.h"
16#include "MyGUI_LayerManager.h"
17#include "MyGUI_RenderItem.h"
20#include "MyGUI_RenderManager.h"
22#include "MyGUI_LayoutManager.h"
24#include <cmath>
25#include <type_traits>
26
27namespace MyGUI
28{
29
30 Widget::Widget() = default;
31
32 Widget::~Widget() = default;
33
34 void Widget::_initialise(
35 WidgetStyle _style,
36 const IntCoord& _coord,
37 std::string_view _skinName,
38 Widget* _parent,
39 ICroppedRectangle* _croppedParent,
40 std::string_view _name)
41 {
42 ResourceSkin* skinInfo = nullptr;
43 ResourceLayout* templateInfo = nullptr;
44
45 if (LayoutManager::getInstance().isExist(_skinName))
46 templateInfo = LayoutManager::getInstance().getByName(_skinName);
47 else
48 skinInfo = SkinManager::getInstance().getByName(_skinName);
49
50 mCoord = _coord;
51
52 mAlign = Align::Default;
53 mWidgetStyle = _style;
54 mName = _name;
55
56 mCroppedParent = _croppedParent;
57 mParent = _parent;
58 updateWorldHasRotation();
59
60
61#if MYGUI_DEBUG_MODE == 1
62 // check input data consistency
63 if (mWidgetStyle == WidgetStyle::Child)
64 {
65 MYGUI_ASSERT(mCroppedParent, "must be cropped");
66 MYGUI_ASSERT(mParent, "must be parent");
67 }
68 else if (mWidgetStyle == WidgetStyle::Overlapped)
69 {
70 MYGUI_ASSERT((mParent == nullptr) == (mCroppedParent == nullptr), "error cropped");
71 }
72 else if (mWidgetStyle == WidgetStyle::Popup)
73 {
74 MYGUI_ASSERT(!mCroppedParent, "cropped must be nullptr");
75 MYGUI_ASSERT(mParent, "must be parent");
76 }
77#endif
78
79 // adjust absolute coordinates
80 mAbsolutePosition = _coord.point();
81
82 if (nullptr != mCroppedParent)
83 mAbsolutePosition += mCroppedParent->getAbsolutePosition();
84 updateRotationTransform();
85
86 const WidgetInfo* root = initialiseWidgetSkinBase(skinInfo, templateInfo);
87
88 // regular child window
89 if (mWidgetStyle == WidgetStyle::Child)
90 {
91 if (mParent)
92 mParent->addChildItem(this);
93 }
94 else if (mWidgetStyle == WidgetStyle::Overlapped)
95 {
96 if (mParent)
97 mParent->addChildNode(this);
98 }
99
100 // virtual method for subclasses
101 initialiseOverride();
102
103 if (skinInfo != nullptr)
104 setSkinProperty(skinInfo);
105
106 if (root != nullptr)
107 {
108 for (const auto& property : root->properties)
109 {
110 setProperty(property.first, property.second);
111 }
112 }
113 }
114
115 void Widget::_shutdown()
116 {
117 eventWidgetDestroyed(this);
118
119 setUserData(Any::Null);
120
121 // virtual method for subclasses
122 shutdownOverride();
123
124 shutdownWidgetSkinBase();
125
126 _destroyAllChildWidget();
127
128 if (mWidgetStyle == WidgetStyle::Child)
129 {
130 if (mParent)
131 mParent->removeChildItem(this);
132 }
133 else if (mWidgetStyle == WidgetStyle::Overlapped)
134 {
135 if (mParent)
136 mParent->removeChildNode(this);
137 }
138
139 mParent = nullptr;
140 mCroppedParent = nullptr;
141 }
142
143 void Widget::changeWidgetSkin(std::string_view _skinName)
144 {
145 ResourceSkin* skinInfo = nullptr;
146 ResourceLayout* templateInfo = nullptr;
147
148 if (LayoutManager::getInstance().isExist(_skinName))
149 templateInfo = LayoutManager::getInstance().getByName(_skinName);
150 else
151 skinInfo = SkinManager::getInstance().getByName(_skinName);
152
153 shutdownOverride();
154
155 saveLayerItem();
156
157 shutdownWidgetSkinBase();
158 const WidgetInfo* root = initialiseWidgetSkinBase(skinInfo, templateInfo);
159
160 restoreLayerItem();
161
162 initialiseOverride();
163
164 if (skinInfo != nullptr)
165 setSkinProperty(skinInfo);
166
167 if (root != nullptr)
168 {
169 for (const auto& property : root->properties)
170 {
171 setProperty(property.first, property.second);
172 }
173 }
174 }
175
176 const WidgetInfo* Widget::initialiseWidgetSkinBase(ResourceSkin* _skinInfo, ResourceLayout* _templateInfo)
177 {
178 const WidgetInfo* root = nullptr;
179 bool skinOnly = false;
180
181 if (_skinInfo == nullptr)
182 {
183 skinOnly = true;
184 std::string_view skinName;
185
186 const VectorWidgetInfo& data = _templateInfo->getLayoutData();
187 for (const auto& item : data)
188 {
189 if (item.name == "Root")
190 {
191 skinName = item.skin;
192 root = &item;
193 break;
194 }
195 }
196
197 _skinInfo = SkinManager::getInstance().getByName(skinName);
198 }
199
200 //SAVE
201 const IntSize& _size = mCoord.size();
202
203 if (_skinInfo != nullptr)
204 {
205 // Bypass derived resize handlers while skin children are being initialized.
206 Widget::setSize(_skinInfo->getSize());
207
208 _createSkinItem(_skinInfo);
209 }
210
211 // set alpha, auto-adjusted from parent
212 _updateAlpha();
213 _updateEnabled();
214 _updateVisible();
215
216 if (!skinOnly)
217 {
218#ifndef MYGUI_DONT_USE_OBSOLETE
219 const MapString& properties = _skinInfo->getProperties();
220 for (const auto& property : properties)
221 {
222 if (BackwardCompatibility::isIgnoreProperty(property.first))
223 {
224 MYGUI_LOG(
225 Warning,
226 property.first
227 << " skin property is deprecated, move it to UserString in a widget in ResourceLayout"
228 << LayoutManager::getInstance().getCurrentLayout() << "]");
229 setUserString(property.first, property.second);
230 }
231 }
232#endif
233
234 // create skin childs
235 const VectorChildSkinInfo& child = _skinInfo->getChild();
236 for (const auto& iter : child)
237 {
238 Widget* widget = baseCreateWidget(
239 iter.style,
240 iter.type,
241 iter.skin,
242 iter.coord,
243 iter.align,
244 iter.layer,
245 iter.name,
246 true);
247 // fill UserString with skin properties
248 for (const auto& prop : iter.params)
249 widget->setUserString(prop.first, prop.second);
250 }
251 }
252
253 if (root != nullptr)
254 {
255 // Bypass derived resize handlers while skin children are being initialized.
256 Widget::setSize(root->intCoord.size());
257
258 for (const auto& userString : root->userStrings)
259 {
260 setUserString(userString.first, userString.second);
261 }
262
263 for (const auto& iter : root->childWidgetsInfo)
264 {
265 _templateInfo->createWidget(iter, {}, this, true);
266 }
267 }
268
269 // Newly created skin subwidgets need geometry even when the size is unchanged.
270 if (mCoord.size() == _size)
271 _updateCoord(_size);
272 else
273 Widget::setSize(_size);
274
275 return root;
276 }
277
278 void Widget::shutdownWidgetSkinBase()
279 {
280 setMaskPick(MaskPickInfo{});
281
282 _deleteSkinItem();
283
284 // remove widgets that were in skin
285 for (auto& iter : mWidgetChildSkin)
286 {
287 // add self so it gets deleted
288 mWidgetChild.push_back(iter);
289 _destroyChildWidget(iter);
290 }
291 mWidgetChildSkin.clear();
292
293 mWidgetClient = nullptr;
294 }
295
296 Widget* Widget::baseCreateWidget(
297 WidgetStyle _style,
298 std::string_view _type,
299 std::string_view _skin,
300 const IntCoord& _coord,
301 Align _align,
302 std::string_view _layer,
303 std::string_view _name,
304 bool _template)
305 {
306 Widget* widget = nullptr;
307
308 if (_template)
309 {
310 widget = WidgetManager::getInstance().createWidget(
311 _style,
312 _type,
313 _skin,
314 _coord,
315 this,
316 _style == WidgetStyle::Popup ? nullptr : this,
317 _name);
318 mWidgetChildSkin.push_back(widget);
319 }
320 else
321 {
322 if (mWidgetClient != nullptr)
323 {
324 widget =
325 mWidgetClient->baseCreateWidget(_style, _type, _skin, _coord, _align, _layer, _name, _template);
326 onWidgetCreated(widget);
327 return widget;
328 }
329
330 widget = WidgetManager::getInstance().createWidget(
331 _style,
332 _type,
333 _skin,
334 _coord,
335 this,
336 _style == WidgetStyle::Popup ? nullptr : this,
337 _name);
338 _linkChildWidget(widget);
339 }
340
341 widget->setAlign(_align);
342
343 // attach widget to layer
344 if (!_layer.empty() && widget->isRootWidget())
345 LayerManager::getInstance().attachToLayerNode(_layer, widget);
346
347 onWidgetCreated(widget);
348
349 return widget;
350 }
351
352 Widget* Widget::createWidgetRealT(
353 std::string_view _type,
354 std::string_view _skin,
355 const FloatCoord& _coord,
356 Align _align,
357 std::string_view _name)
358 {
359 return createWidgetT(_type, _skin, CoordConverter::convertFromRelative(_coord, getSize()), _align, _name);
360 }
361
362 void Widget::_updateView()
363 {
364 bool margin = mCroppedParent ? _checkMargin() : false;
365
366 // viewport became invalid
367 if (margin)
368 {
369 if (_checkOutsideUnrotated())
370 {
371 // remember current state
372 mIsMargin = margin;
373
374 _setSubSkinVisible(false);
375
376 // entire hierarchy must be checked
377 for (auto& widget : mWidgetChild)
378 widget->_updateView();
379 for (auto& widget : mWidgetChildSkin)
380 widget->_updateView();
381
382 return;
383 }
384 }
385 // we are not clipped and were normal
386 else if (!mIsMargin)
387 {
388 _updateSkinItemView();
389 return;
390 }
391
392 // remember current state
393 mIsMargin = margin;
394
395 // if skin was hidden, show it
396 _setSubSkinVisible(true);
397
398 // update our children, they decide whether to update theirs
399 for (auto& widget : mWidgetChild)
400 widget->_updateView();
401 for (auto& widget : mWidgetChildSkin)
402 widget->_updateView();
403
404 _updateSkinItemView();
405 }
406
407 bool Widget::_setWidgetState(std::string_view _state)
408 {
409 return _setSkinItemState(_state);
410 }
411
412 void Widget::_destroyChildWidget(Widget* _widget)
413 {
414 MYGUI_ASSERT(nullptr != _widget, "invalid widget pointer");
415
416 if (mParent != nullptr && mParent->getClientWidget() == this)
417 mParent->onWidgetDestroy(_widget);
418
419 onWidgetDestroy(_widget);
420
421 WidgetContainer::_destroyChildWidget(_widget);
422 }
423
424 IntCoord Widget::getClientCoord() const
425 {
426 if (mWidgetClient != nullptr)
427 return mWidgetClient->getCoord();
428 return {0, 0, mCoord.width, mCoord.height};
429 }
430
431 void Widget::setAlpha(float _alpha)
432 {
433 if (mAlpha == _alpha)
434 return;
435 mAlpha = _alpha;
436
437 _updateAlpha();
438 }
439
440 void Widget::_updateAlpha()
441 {
442 if (nullptr != mParent)
443 mRealAlpha = mAlpha * (mInheritsAlpha ? mParent->_getRealAlpha() : ALPHA_MAX);
444 else
445 mRealAlpha = mAlpha;
446
447 for (auto& widget : mWidgetChild)
448 widget->_updateAlpha();
449 for (auto& widget : mWidgetChildSkin)
450 widget->_updateAlpha();
451
452 _setSkinItemAlpha(mRealAlpha);
453 }
454
455 void Widget::setInheritsAlpha(bool _inherits)
456 {
457 if (mInheritsAlpha == _inherits)
458 return;
459 mInheritsAlpha = _inherits;
460 _updateAlpha();
461 }
462
463 ILayerItem* Widget::getLayerItemByPoint(int _left, int _top) const
464 {
465 if (!mInheritedEnabled || !mInheritedVisible || (!getNeedMouseFocus() && !getInheritsPick()))
466 return nullptr;
467
468 // Recursive picking has already removed ancestor rotations; only the local rotation matters.
469 // Popups enter from a layer and still need the full world transform.
470 if (mRotation == 0.0f && (mCroppedParent || !mParent))
471 return getLayerItemByLocalPoint(IntPoint(_left - mCoord.left, _top - mCoord.top));
472
473 return getLayerItemByRotatedPoint(_left, _top);
474 }
475
476 ILayerItem* Widget::getLayerItemByRotatedPoint(int _left, int _top) const
477 {
478 const FloatPoint point((float)_left, (float)_top);
479 // Popups enter from a layer, bypassing their logical parent's picking traversal.
480 const FloatPoint localPoint =
481 (mParent && !mCroppedParent) ? layerToLocal(point) : mRotationTransform.inverseTransformPoint(point);
482 return getLayerItemByLocalPoint(localPoint);
483 }
484
485 template<typename T>
486 ILayerItem* Widget::getLayerItemByLocalPoint(types::TPoint<T> _point) const
487 {
488 // check point hit
489 if (_point.left < mMargin.left || _point.top < mMargin.top || _point.left >= mCoord.width - mMargin.right ||
490 _point.top >= mCoord.height - mMargin.bottom
491 // if there is a mask, also check by mask
492 || !isMaskPickInside(IntPoint((int)_point.left, (int)_point.top), mCoord))
493 return nullptr;
494
495 // ask children
496 int childLeft = (int)_point.left;
497 int childTop = (int)_point.top;
498 if constexpr (std::is_floating_point_v<T>)
499 {
500 childLeft = (int)std::floor(_point.left);
501 childTop = (int)std::floor(_point.top);
502 }
503 return pickChildrenOrSelf({childLeft, childTop});
504 }
505
506 ILayerItem* Widget::pickChildrenOrSelf(IntPoint _point) const
507 {
508 for (VectorWidgetPtr::const_reverse_iterator widget = mWidgetChild.rbegin(); widget != mWidgetChild.rend();
509 ++widget)
510 {
511 // only talk to obedient children
512 if ((*widget)->mWidgetStyle == WidgetStyle::Popup)
513 continue;
514
515 ILayerItem* item = (*widget)->getLayerItemByPoint(_point.left, _point.top);
516 if (item != nullptr)
517 return item;
518 }
519 // ask skin children
520 for (VectorWidgetPtr::const_reverse_iterator widget = mWidgetChildSkin.rbegin();
521 widget != mWidgetChildSkin.rend();
522 ++widget)
523 {
524 ILayerItem* item = (*widget)->getLayerItemByPoint(_point.left, _point.top);
525 if (item != nullptr)
526 return item;
527 }
528
529 // disobedient children
530 return getInheritsPick() ? nullptr : const_cast<Widget*>(this);
531 }
532
533 void Widget::_updateGeometry(const IntSize* _oldSize)
534 {
535 const IntPoint oldPosition = mAbsolutePosition;
536 mAbsolutePosition = mCroppedParent ? mCroppedParent->getAbsolutePosition() + mCoord.point() : mCoord.point();
537 updateRotationTransform();
538
539 for (auto& widget : mWidgetChild)
540 if (_oldSize != nullptr)
541 widget->_setAlign(*_oldSize, getSize());
542 else if (widget->mCroppedParent != nullptr)
543 widget->_updateGeometry();
544 else if (widget->mWorldHasRotation)
545 widget->invalidateRotation();
546 for (auto& widget : mWidgetChildSkin)
547 if (_oldSize != nullptr)
548 widget->_setAlign(*_oldSize, getSize());
549 else if (widget->mCroppedParent != nullptr)
550 widget->_updateGeometry();
551 else if (widget->mWorldHasRotation)
552 widget->invalidateRotation();
553
554 _correctSkinItemView();
555
556 if (mAbsolutePosition != oldPosition || (_oldSize != nullptr && getSize() != *_oldSize))
557 eventChangeAbsoluteCoord(this);
558 }
559
560 void Widget::_forcePick(Widget* _widget)
561 {
562 if (mWidgetClient != nullptr)
563 {
564 mWidgetClient->_forcePick(_widget);
565 return;
566 }
567
568 auto iter = std::find(mWidgetChild.begin(), mWidgetChild.end(), _widget);
569 if (iter == mWidgetChild.end())
570 return;
571
572 for (auto& widget : mWidgetChild)
573 {
574 if (widget == _widget)
575 widget->mDepth = -1;
576 else if (widget->getDepth() == -1)
577 widget->mDepth = 0;
578 }
579 _updateChilds(true);
580 }
581
582 Widget* Widget::findWidget(std::string_view _name)
583 {
584 if (_name == mName)
585 return this;
586 if (mWidgetClient != nullptr)
587 return mWidgetClient->findWidget(_name);
588
589 for (const auto& widget : mWidgetChild)
590 {
591 Widget* find = widget->findWidget(_name);
592 if (nullptr != find)
593 return find;
594 }
595 return nullptr;
596 }
597
598 void Widget::setRealPosition(const FloatPoint& _point)
599 {
600 auto parentSize =
601 mCroppedParent == nullptr ? RenderManager::getInstance().getViewSize() : mCroppedParent->getSize();
602 setPosition(CoordConverter::convertFromRelative(_point, parentSize));
603 }
604
605 void Widget::setRealSize(const FloatSize& _size)
606 {
607 auto parentSize =
608 mCroppedParent == nullptr ? RenderManager::getInstance().getViewSize() : mCroppedParent->getSize();
609 setSize(CoordConverter::convertFromRelative(_size, parentSize));
610 }
611
612 void Widget::setRealCoord(const FloatCoord& _coord)
613 {
614 auto parentSize =
615 mCroppedParent == nullptr ? RenderManager::getInstance().getViewSize() : mCroppedParent->getSize();
616 setCoord(CoordConverter::convertFromRelative(_coord, parentSize));
617 }
618
619 void Widget::_setAlign(const IntSize& _oldsize, const IntSize& _newSize)
620 {
621 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, _newSize, _oldsize);
622
623 if (move && resize)
624 setCoord(coord);
625 else if (move)
626 setPosition(coord.point());
627 else if (resize)
628 setSize(coord.size());
629
630 // Unchanged local coordinates (including a resize rejected by an override) can still inherit movement.
631 if (mCroppedParent != nullptr && mAbsolutePosition != mCroppedParent->getAbsolutePosition() + mCoord.point())
632 _updateGeometry();
633
634 // Parent clipping can change even when alignments leave coordinates unchanged
635 // The coordinate setters may return early, so refresh the view explicitly
636 // Descendants also inherit rotation around the parent's changed pivot.
637 if (mWorldHasRotation)
638 invalidateRotation();
639 else
640 _updateView();
641 }
642
643 void Widget::setPosition(const IntPoint& _point)
644 {
645 if (mCoord.point() == _point)
646 return;
647 mCoord = _point;
648 _updateGeometry();
649
650 _updateView();
651
652 eventChangeCoord(this);
653 }
654
655 void Widget::setSize(const IntSize& _size)
656 {
657 if (mCoord.size() == _size)
658 return;
659 Widget::setCoord(IntCoord(mCoord.point(), _size));
660 }
661
662 void Widget::setCoord(const IntCoord& _coord)
663 {
664 if (mCoord == _coord)
665 return;
666 // set new coordinate, use old one in calculations
667 IntSize old = mCoord.size();
668 mCoord = _coord;
669
670 _updateCoord(old);
671
672 eventChangeCoord(this);
673 }
674
675 void Widget::_updateCoord(const IntSize& _oldSize)
676 {
677 bool visible = true;
678
679 bool margin = mCroppedParent ? _checkMargin() : false;
680
681 if (margin)
682 {
683 if (_checkOutsideUnrotated())
684 {
685 visible = false;
686 }
687 }
688
689 _setSubSkinVisible(visible);
690
691 _updateGeometry(&_oldSize);
692
693 _setSkinItemAlign(_oldSize);
694
695 // remember current state
696 mIsMargin = margin;
697 }
698
699 void Widget::setAlign(Align _value)
700 {
701 mAlign = _value;
702 }
703
704 void Widget::setRotation(float _radians)
705 {
706 if (mRotation == _radians)
707 return;
708 const bool rotationPresenceChanged = (mRotation == 0.0f) != (_radians == 0.0f);
709 mRotation = _radians;
710 if (rotationPresenceChanged)
711 updateWorldHasRotation();
712 mRotationTransform.cosine = std::cos(_radians);
713 mRotationTransform.sine = std::sin(_radians);
714 invalidateRotation();
715 }
716
717 float Widget::getRotation() const
718 {
719 return mRotation;
720 }
721
722 void Widget::setRotationCenter(const FloatPoint& _center)
723 {
724 if (mCustomRotationCenter && mRotationCenter == _center)
725 return;
726 mRotationCenter = _center;
727 mCustomRotationCenter = true;
728 invalidateRotation();
729 }
730
731 FloatPoint Widget::getRotationCenter() const
732 {
733 return mCustomRotationCenter ? mRotationCenter : FloatPoint(mCoord.width * 0.5f, mCoord.height * 0.5f);
734 }
735
736 FloatPoint Widget::RotationTransform::transformPoint(const FloatPoint& _point) const
737 {
738 return FloatPoint(
739 cosine * _point.left - sine * _point.top + offsetX,
740 sine * _point.left + cosine * _point.top + offsetY);
741 }
742
743 FloatPoint Widget::RotationTransform::inverseTransformPoint(const FloatPoint& _point) const
744 {
745 const float x = _point.left - offsetX;
746 const float y = _point.top - offsetY;
747 return FloatPoint(cosine * x + sine * y, cosine * y - sine * x);
748 }
749
750 void Widget::updateRotationTransform()
751 {
752 // Map widget-local points into the coordinate space used by mCoord.
753 mRotationTransform.offsetX = (float)mCoord.left;
754 mRotationTransform.offsetY = (float)mCoord.top;
755 if (mRotation == 0.0f)
756 return;
757 const FloatPoint center = getRotationCenter();
758 mRotationTransform.offsetX +=
759 center.left - mRotationTransform.cosine * center.left + mRotationTransform.sine * center.top;
760 mRotationTransform.offsetY +=
761 center.top - mRotationTransform.sine * center.left - mRotationTransform.cosine * center.top;
762 }
763
764 void Widget::updateWorldHasRotation()
765 {
766 const bool hasRotation = mRotation != 0.0f || (mParent && mParent->mWorldHasRotation);
767 if (mWorldHasRotation == hasRotation)
768 return;
769 mWorldHasRotation = hasRotation;
770 for (auto* widget : mWidgetChild)
771 widget->updateWorldHasRotation();
772 for (auto* widget : mWidgetChildSkin)
773 widget->updateWorldHasRotation();
774 }
775
776 Widget::RotationTransform Widget::getWorldRotationTransform() const
777 {
778 if (mParent == nullptr)
779 return mRotationTransform;
780 const RotationTransform parent = mParent->getWorldRotationTransform();
781 FloatPoint offset(mRotationTransform.offsetX, mRotationTransform.offsetY);
782 // Popup positions are absolute, but the parent transform takes local coordinates.
783 if (!mCroppedParent)
784 offset -= FloatPoint((float)mParent->getAbsoluteLeft(), (float)mParent->getAbsoluteTop());
785 offset = parent.transformPoint(offset);
786 RotationTransform result;
787 result.cosine = parent.cosine * mRotationTransform.cosine - parent.sine * mRotationTransform.sine;
788 result.sine = parent.sine * mRotationTransform.cosine + parent.cosine * mRotationTransform.sine;
789 result.offsetX = offset.left;
790 result.offsetY = offset.top;
791 return result;
792 }
793
794 FloatPoint Widget::localToLayer(const FloatPoint& _point) const
795 {
796 return getWorldRotationTransform().transformPoint(_point);
797 }
798
799 void Widget::localToLayer(FloatPoint* _points, size_t _count) const
800 {
801 const RotationTransform transform = getWorldRotationTransform();
802 for (size_t i = 0; i < _count; ++i)
803 _points[i] = transform.transformPoint(_points[i]);
804 }
805
806 FloatPoint Widget::layerToLocal(const FloatPoint& _point) const
807 {
808 return getWorldRotationTransform().inverseTransformPoint(_point);
809 }
810
811 FloatPoint Widget::rotatePoint(const FloatPoint& _point) const
812 {
813 if (!mWorldHasRotation)
814 return _point;
815 const RotationTransform transform = getWorldRotationTransform();
816 return transform.transformPoint(
817 _point - FloatPoint((float)mAbsolutePosition.left, (float)mAbsolutePosition.top));
818 }
819
820 FloatPoint Widget::unrotatePoint(const FloatPoint& _point) const
821 {
822 if (!mWorldHasRotation)
823 return _point;
824 const RotationTransform transform = getWorldRotationTransform();
825 return transform.inverseTransformPoint(_point) +
826 FloatPoint((float)mAbsolutePosition.left, (float)mAbsolutePosition.top);
827 }
828
829 IntPoint Widget::unrotateVector(const IntPoint& _vector) const
830 {
831 const RotationTransform transform = getWorldRotationTransform();
832 return IntPoint(
833 (int)std::lround(transform.cosine * _vector.left + transform.sine * _vector.top),
834 (int)std::lround(transform.cosine * _vector.top - transform.sine * _vector.left));
835 }
836
837 void Widget::_transformVertices(Vertex* _vertices, size_t _count, const RenderTargetInfo& _info) const
838 {
839 if (!mWorldHasRotation)
840 return;
841 const RotationTransform transform = getWorldRotationTransform();
842
843 for (size_t i = 0; i < _count; ++i)
844 {
845 Vertex& vertex = _vertices[i];
846 const FloatPoint pixel = geometry_utility::fromRenderTarget({vertex.x, vertex.y}, _info, mAbsolutePosition);
847 const FloatPoint transformed = geometry_utility::toRenderTarget(transform.transformPoint(pixel), _info);
848 vertex.x = transformed.left;
849 vertex.y = transformed.top;
850 }
851 }
852
853 bool Widget::_hasRotation() const
854 {
855 return mWorldHasRotation;
856 }
857
858 void Widget::invalidateRotation()
859 {
860 updateRotationTransform();
861 _updateView();
862 for (auto* widget : mWidgetChild)
863 widget->invalidateRotation();
864 for (auto* widget : mWidgetChildSkin)
865 widget->invalidateRotation();
866 }
867
868 void Widget::detachFromWidget(std::string_view _layer)
869 {
870 const bool hadRotation = mWorldHasRotation;
871 _detachFromWidget(_layer);
872 _updateAlpha();
873 _updateEnabled();
874 _updateVisible();
875 _updateGeometry();
876 // A rotation change affects descendant clipping even when this widget stays unclipped.
877 if (hadRotation != mWorldHasRotation)
878 invalidateRotation();
879 else
880 _updateView();
881 }
882
883 void Widget::_detachFromWidget(std::string_view _layer)
884 {
885 std::string_view oldlayer;
886 if (getLayer())
887 oldlayer = getLayer()->getName();
888
889 if (mParent != nullptr)
890 {
891 // detach from layers
892 if (!isRootWidget())
893 {
894 detachFromLayerItemNode(true);
895
896 if (mWidgetStyle == WidgetStyle::Child)
897 {
898 mParent->removeChildItem(this);
899 }
900 else if (mWidgetStyle == WidgetStyle::Overlapped)
901 {
902 mParent->removeChildNode(this);
903 }
904
905 mWidgetStyle = WidgetStyle::Overlapped;
906
907 mCroppedParent = nullptr;
908
909 // reset clipping
910 mMargin.clear();
911 }
912
913 Gui::getInstance()._linkChildWidget(this);
914 mParent->_unlinkChildWidget(this);
915 mParent = nullptr;
916 updateWorldHasRotation();
917 }
918
919 if (!_layer.empty())
920 {
921 LayerManager::getInstance().attachToLayerNode(_layer, this);
922 }
923 else if (!oldlayer.empty())
924 {
925 LayerManager::getInstance().attachToLayerNode(oldlayer, this);
926 }
927 }
928
929 void Widget::attachToWidget(Widget* _parent, WidgetStyle _style, std::string_view _layer)
930 {
931 MYGUI_ASSERT(_parent, "parent must be valid");
932 MYGUI_ASSERT(_parent != this, "cyclic attach (attaching to self)");
933
934 // attach to client if widget have it
935 if (_parent->getClientWidget())
936 _parent = _parent->getClientWidget();
937
938 Widget* parent = _parent;
939 while (parent->getParent())
940 {
941 MYGUI_ASSERT(parent != this, "cyclic attach");
942 parent = parent->getParent();
943 }
944
945 const bool hadRotation = mWorldHasRotation;
946 _detachFromWidget({});
947
948 mWidgetStyle = _style;
949
950 if (_style == WidgetStyle::Popup)
951 {
952 if (mParent == nullptr)
953 Gui::getInstance()._unlinkChildWidget(this);
954 else
955 mParent->_unlinkChildWidget(this);
956
957 mParent = _parent;
958 updateWorldHasRotation();
959 mParent->_linkChildWidget(this);
960
961 mCroppedParent = nullptr;
962
963 if (!_layer.empty())
964 {
965 LayerManager::getInstance().attachToLayerNode(_layer, this);
966 }
967 }
968 else if (_style == WidgetStyle::Child)
969 {
970 LayerManager::getInstance().detachFromLayer(this);
971
972 if (mParent == nullptr)
973 Gui::getInstance()._unlinkChildWidget(this);
974 else
975 mParent->_unlinkChildWidget(this);
976
977 mParent = _parent;
978 updateWorldHasRotation();
979 mParent->addChildItem(this);
980 mParent->_linkChildWidget(this);
981
982 mCroppedParent = _parent;
983 }
984 else if (_style == WidgetStyle::Overlapped)
985 {
986 LayerManager::getInstance().detachFromLayer(this);
987
988 if (mParent == nullptr)
989 Gui::getInstance()._unlinkChildWidget(this);
990 else
991 mParent->_unlinkChildWidget(this);
992
993 mParent = _parent;
994 updateWorldHasRotation();
995 mParent->_linkChildWidget(this);
996
997 mCroppedParent = _parent;
998
999 mParent->addChildNode(this);
1000 }
1001
1002 _updateAlpha();
1003 _updateEnabled();
1004 _updateVisible();
1005 _updateGeometry();
1006 // Refresh descendants after the final parent, cropping parent, and geometry are established.
1007 if (hadRotation != mWorldHasRotation)
1008 invalidateRotation();
1009 else
1010 _updateView();
1011 }
1012
1013 void Widget::setWidgetStyle(WidgetStyle _style, std::string_view _layer)
1014 {
1015 if (_style == mWidgetStyle)
1016 return;
1017 if (nullptr == getParent())
1018 return;
1019
1020 attachToWidget(mParent, _style, _layer);
1021 // find layer to which we are attached
1022 }
1023
1024 Widget* Widget::createWidgetT(
1025 std::string_view _type,
1026 std::string_view _skin,
1027 const IntCoord& _coord,
1028 Align _align,
1029 std::string_view _name)
1030 {
1031 return baseCreateWidget(WidgetStyle::Child, _type, _skin, _coord, _align, {}, _name, false);
1032 }
1033
1034 Widget* Widget::createWidgetT(
1035 std::string_view _type,
1036 std::string_view _skin,
1037 int _left,
1038 int _top,
1039 int _width,
1040 int _height,
1041 Align _align,
1042 std::string_view _name)
1043 {
1044 return createWidgetT(_type, _skin, IntCoord(_left, _top, _width, _height), _align, _name);
1045 }
1046
1047 Widget* Widget::createWidgetRealT(
1048 std::string_view _type,
1049 std::string_view _skin,
1050 float _left,
1051 float _top,
1052 float _width,
1053 float _height,
1054 Align _align,
1055 std::string_view _name)
1056 {
1057 return createWidgetRealT(_type, _skin, FloatCoord(_left, _top, _width, _height), _align, _name);
1058 }
1059
1060 Widget* Widget::createWidgetT(
1061 WidgetStyle _style,
1062 std::string_view _type,
1063 std::string_view _skin,
1064 const IntCoord& _coord,
1065 Align _align,
1066 std::string_view _layer,
1067 std::string_view _name)
1068 {
1069 return baseCreateWidget(_style, _type, _skin, _coord, _align, _layer, _name, false);
1070 }
1071
1072 const VectorWidgetPtr& Widget::getChildWidgets() const
1073 {
1074 if (mWidgetClient != nullptr)
1075 return mWidgetClient->getChildWidgets();
1076 return mWidgetChild;
1077 }
1078
1079 size_t Widget::getChildCount() const
1080 {
1081 return getChildWidgets().size();
1082 }
1083
1084 Widget* Widget::getChildAt(size_t _index) const
1085 {
1086 const auto& widgets = getChildWidgets();
1087 MYGUI_ASSERT_RANGE(_index, widgets.size(), "Widget::getChildAt");
1088 return widgets[_index];
1089 }
1090
1091 void Widget::baseUpdateEnable()
1092 {
1093 if (getInheritedEnabled())
1094 _setWidgetState("normal");
1095 else
1096 _setWidgetState("disabled");
1097 }
1098
1099 void Widget::setVisible(bool _value)
1100 {
1101 if (mVisible == _value)
1102 return;
1103 mVisible = _value;
1104
1105 _updateVisible();
1106 }
1107
1108 void Widget::_updateVisible()
1109 {
1110 mInheritedVisible = mParent == nullptr || mParent->getInheritedVisible();
1111 mInheritedVisible = mVisible && mInheritedVisible;
1112
1113 _setSkinItemVisible(mInheritedVisible);
1114
1115 for (auto& widget : mWidgetChild)
1116 widget->_updateVisible();
1117 for (auto& widget : mWidgetChildSkin)
1118 widget->_updateVisible();
1119
1120 if (!mInheritedVisible && InputManager::getInstance().getMouseFocusWidget() == this)
1121 InputManager::getInstance()._resetMouseFocusWidget();
1122 if (!mInheritedVisible && InputManager::getInstance().getKeyFocusWidget() == this)
1123 InputManager::getInstance().resetKeyFocusWidget();
1124 }
1125
1126 void Widget::setEnabled(bool _value)
1127 {
1128 if (mEnabled == _value)
1129 return;
1130 mEnabled = _value;
1131
1132 _updateEnabled();
1133 }
1134
1135 void Widget::_updateEnabled()
1136 {
1137 mInheritedEnabled = mParent == nullptr || (mParent->getInheritedEnabled());
1138 mInheritedEnabled = mInheritedEnabled && mEnabled;
1139
1140 for (auto& iter : mWidgetChild)
1141 iter->_updateEnabled();
1142 for (auto& iter : mWidgetChildSkin)
1143 iter->_updateEnabled();
1144
1145 baseUpdateEnable();
1146
1147 if (!mInheritedEnabled)
1148 InputManager::getInstance().unlinkWidget(this);
1149 }
1150
1151 void Widget::setColour(const Colour& _value)
1152 {
1153 _setSkinItemColour(_value);
1154
1155 for (auto& widget : mWidgetChildSkin)
1156 widget->setColour(_value);
1157 }
1158
1159 IntSize Widget::getParentSize() const
1160 {
1161 if (mCroppedParent)
1162 return static_cast<Widget*>(mCroppedParent)->getSize();
1163 if (getLayer())
1164 return getLayer()->getSize();
1165
1166 return RenderManager::getInstance().getViewSize();
1167 }
1168
1169 void Widget::_resetContainer(bool _updateOnly)
1170 {
1171 if (getNeedToolTip())
1172 ToolTipManager::getInstance()._unlinkWidget(this);
1173 }
1174
1175 bool Widget::_checkPoint(int _left, int _top) const
1176 {
1177 // Exclude the right and bottom edges to match the rendered pixel bounds.
1178 return _getViewLeft() <= _left && _getViewTop() <= _top && _left < _getViewRight() && _top < _getViewBottom();
1179 }
1180
1181 void Widget::_linkChildWidget(Widget* _widget)
1182 {
1183 WidgetContainer::_linkChildWidget(_widget);
1184 _updateChilds(false);
1185 }
1186
1187 void Widget::shutdownOverride()
1188 {
1189 }
1190
1191 void Widget::initialiseOverride()
1192 {
1194 assignWidget(mWidgetClient, "Client");
1195 }
1196
1197 void Widget::setSkinProperty(ResourceSkin* _info)
1198 {
1199 const MapString& properties = _info->getProperties();
1200 for (const auto& property : properties)
1201 setProperty(property.first, property.second);
1202 }
1203
1204 void Widget::setProperty(std::string_view _key, std::string_view _value)
1205 {
1206 std::string key{_key};
1207 std::string value{_value};
1208
1209 if (BackwardCompatibility::processDeprecatedProperty(this, key, value))
1210 setPropertyOverride(key, value);
1211 }
1212
1213 VectorWidgetPtr Widget::getSkinWidgetsByName(std::string_view _name) const
1214 {
1215 VectorWidgetPtr result;
1216
1217 for (const auto& childSkin : mWidgetChildSkin)
1218 childSkin->findWidgets(_name, result);
1219
1220 return result;
1221 }
1222
1223 void Widget::findWidgets(std::string_view _name, VectorWidgetPtr& _result)
1224 {
1225 if (_name == mName)
1226 _result.push_back(this);
1227
1228 if (mWidgetClient != nullptr)
1229 {
1230 mWidgetClient->findWidgets(_name, _result);
1231 }
1232 else
1233 {
1234 for (const auto& widget : mWidgetChild)
1235 widget->findWidgets(_name, _result);
1236 }
1237 }
1238
1239 void Widget::onWidgetCreated(Widget* _widget)
1240 {
1241 }
1242
1243 void Widget::onWidgetDestroy(Widget* _widget)
1244 {
1245 }
1246
1247 void Widget::setWidgetClient(Widget* _widget)
1248 {
1249 MYGUI_ASSERT(mWidgetClient != this, "mWidgetClient can not be this widget");
1250 mWidgetClient = _widget;
1251 }
1252
1253 Widget* Widget::_getClientWidget()
1254 {
1255 return getClientWidget() == nullptr ? this : getClientWidget();
1256 }
1257
1258 const Widget* Widget::_getClientWidget() const
1259 {
1260 return getClientWidget() == nullptr ? this : getClientWidget();
1261 }
1262
1263 Widget* Widget::_createSkinWidget(
1264 WidgetStyle _style,
1265 std::string_view _type,
1266 std::string_view _skin,
1267 const IntCoord& _coord,
1268 Align _align,
1269 std::string_view _layer,
1270 std::string_view _name)
1271 {
1272 return baseCreateWidget(_style, _type, _skin, _coord, _align, _layer, _name, true);
1273 }
1274
1275 void Widget::setPropertyOverride(std::string_view _key, std::string_view _value)
1276 {
1278 if (_key == "Rotation")
1279 setRotation(utility::parseValue<float>(_value));
1281 else if (_key == "RotationCenter")
1282 setRotationCenter(utility::parseValue<FloatPoint>(_value));
1284 else if (_key == "Position")
1285 setPosition(utility::parseValue<IntPoint>(_value));
1286
1288 else if (_key == "Size")
1289 setSize(utility::parseValue<IntSize>(_value));
1290
1292 else if (_key == "Coord")
1293 setCoord(utility::parseValue<IntCoord>(_value));
1294
1296 else if (_key == "Visible")
1297 setVisible(utility::parseValue<bool>(_value));
1298
1300 else if (_key == "Depth")
1301 setDepth(utility::parseValue<int>(_value));
1302
1304 else if (_key == "Alpha")
1305 setAlpha(utility::parseValue<float>(_value));
1306
1308 else if (_key == "Colour")
1309 setColour(utility::parseValue<Colour>(_value));
1310
1312 else if (_key == "InheritsAlpha")
1313 setInheritsAlpha(utility::parseValue<bool>(_value));
1314
1316 else if (_key == "InheritsPick")
1317 setInheritsPick(utility::parseValue<bool>(_value));
1318
1320 else if (_key == "MaskPick")
1321 setMaskPick(std::string{_value});
1322
1324 else if (_key == "NeedKey")
1325 setNeedKeyFocus(utility::parseValue<bool>(_value));
1326
1328 else if (_key == "NeedMouse")
1329 setNeedMouseFocus(utility::parseValue<bool>(_value));
1330
1332 else if (_key == "Enabled")
1333 setEnabled(utility::parseValue<bool>(_value));
1334
1336 else if (_key == "NeedToolTip")
1337 setNeedToolTip(utility::parseValue<bool>(_value));
1338
1340 else if (_key == "Pointer")
1341 setPointer(_value);
1342
1343 else
1344 {
1345 MYGUI_LOG(
1346 Warning,
1347 "Widget '" << getName() << "|" << getTypeName() << "' have unknown property '" << _key << "' "
1348 << " [" << LayoutManager::getInstance().getCurrentLayout() << "]");
1349 return;
1350 }
1351
1352 eventChangeProperty(this, _key, _value);
1353 }
1354
1355 void Widget::setPosition(int _left, int _top)
1356 {
1357 setPosition(IntPoint(_left, _top));
1358 }
1359
1360 void Widget::setSize(int _width, int _height)
1361 {
1362 setSize(IntSize(_width, _height));
1363 }
1364
1365 void Widget::setCoord(int _left, int _top, int _width, int _height)
1366 {
1367 setCoord(IntCoord(_left, _top, _width, _height));
1368 }
1369
1370 void Widget::setRealPosition(float _left, float _top)
1371 {
1372 setRealPosition(FloatPoint(_left, _top));
1373 }
1374
1375 void Widget::setRealSize(float _width, float _height)
1376 {
1377 setRealSize(FloatSize(_width, _height));
1378 }
1379
1380 void Widget::setRealCoord(float _left, float _top, float _width, float _height)
1381 {
1382 setRealCoord(FloatCoord(_left, _top, _width, _height));
1383 }
1384
1385 const std::string& Widget::getName() const
1386 {
1387 return mName;
1388 }
1389
1390 bool Widget::getVisible() const
1391 {
1392 return mVisible;
1393 }
1394
1395 Align Widget::getAlign() const
1396 {
1397 return mAlign;
1398 }
1399
1400 float Widget::getAlpha() const
1401 {
1402 return mAlpha;
1403 }
1404
1405 bool Widget::getInheritsAlpha() const
1406 {
1407 return mInheritsAlpha;
1408 }
1409
1410 bool Widget::isRootWidget() const
1411 {
1412 return nullptr == mCroppedParent;
1413 }
1414
1415 Widget* Widget::getParent() const
1416 {
1417 return mParent;
1418 }
1419
1420 void Widget::setEnabledSilent(bool _value)
1421 {
1422 mEnabled = _value;
1423 }
1424
1425 bool Widget::getEnabled() const
1426 {
1427 return mEnabled;
1428 }
1429
1430 Widget* Widget::getClientWidget()
1431 {
1432 return mWidgetClient;
1433 }
1434
1435 const Widget* Widget::getClientWidget() const
1436 {
1437 return mWidgetClient;
1438 }
1439
1440 WidgetStyle Widget::getWidgetStyle() const
1441 {
1442 return mWidgetStyle;
1443 }
1444
1445 size_t Widget::_getItemIndex(Widget* _item) const
1446 {
1447 return ITEM_NONE;
1448 }
1449
1450 void Widget::_setContainer(Widget* _value)
1451 {
1452 mContainer = _value;
1453 }
1454
1455 Widget* Widget::_getContainer() const
1456 {
1457 return mContainer;
1458 }
1459
1460 size_t Widget::_getContainerIndex(const IntPoint& _point) const
1461 {
1462 return ITEM_NONE;
1463 }
1464
1465 const IntCoord& Widget::getLayerItemCoord() const
1466 {
1467 return mCoord;
1468 }
1469
1470 float Widget::_getRealAlpha() const
1471 {
1472 return mRealAlpha;
1473 }
1474
1475 bool Widget::getInheritedEnabled() const
1476 {
1477 return mInheritedEnabled;
1478 }
1479
1480 bool Widget::getInheritedVisible() const
1481 {
1482 return mInheritedVisible;
1483 }
1484
1485 void Widget::resizeLayerItemView(const IntSize& _oldView, const IntSize& _newView)
1486 {
1487 _setAlign(_oldView, _newView);
1488 }
1489
1490 void Widget::setDepth(int _value)
1491 {
1492 if (mDepth == _value)
1493 return;
1494
1495 mDepth = _value;
1496
1497 if (mParent != nullptr)
1498 {
1499 // move element to last position, then bubble sort it
1500 mParent->_unlinkChildWidget(this);
1501 mParent->_linkChildWidget(this);
1502 mParent->_updateChilds(false);
1503 }
1504 }
1505
1506 int Widget::getDepth() const
1507 {
1508 return mDepth;
1509 }
1510
1511 void Widget::_updateChilds(bool _fullSort)
1512 {
1513 if (_fullSort)
1514 {
1515 std::stable_sort(
1516 mWidgetChild.begin(),
1517 mWidgetChild.end(),
1518 [](Widget* lhs, Widget* rhs) { return lhs->getDepth() > rhs->getDepth(); });
1519 }
1520 else if (!mWidgetChild.empty())
1521 {
1522 // raise newly added item if necessary
1523 size_t index = mWidgetChild.size() - 1;
1524 while (index > 0 && mWidgetChild[index]->getDepth() > mWidgetChild[index - 1]->getDepth())
1525 {
1526 std::swap(mWidgetChild[index], mWidgetChild[index - 1]);
1527 --index;
1528 }
1529 }
1530
1531 for (auto& widget : mWidgetChild)
1532 {
1533 if (widget->getWidgetStyle() == WidgetStyle::Child)
1534 {
1535 widget->detachFromLayerItemNode(false);
1536 removeChildItem(widget);
1537 }
1538 }
1539
1540 for (auto& widget : mWidgetChild)
1541 {
1542 if (widget->getWidgetStyle() == WidgetStyle::Child)
1543 {
1544 addChildItem(widget);
1545 widget->_updateView();
1546 }
1547 }
1548 }
1549
1550} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_ASSERT_RANGE(index, size, owner)
#define MYGUI_LOG(level, text)
void const IntCoord IntCoord & _result
FloatPoint fromRenderTarget(FloatPoint _point, const RenderTargetInfo &_info, IntPoint _origin={})
FloatPoint toRenderTarget(FloatPoint _point, const RenderTargetInfo &_info, IntPoint _origin={})
T parseValue(std::string_view value)
constexpr float ALPHA_MAX
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TCoord< float > FloatCoord
Definition MyGUI_Types.h:36
std::map< std::string, std::string, std::less<> > MapString
Definition MyGUI_Types.h:39
std::vector< WidgetInfo > VectorWidgetInfo
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
constexpr size_t ITEM_NONE
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
std::vector< ChildSkinInfo > VectorChildSkinInfo