-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCEGUIManipulator.cpp
More file actions
1316 lines (1145 loc) · 47.8 KB
/
Copy pathCEGUIManipulator.cpp
File metadata and controls
1316 lines (1145 loc) · 47.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "src/cegui/CEGUIManipulator.h"
#include "src/cegui/CEGUIUtils.h"
#include "src/cegui/CEGUIManager.h"
#include "src/cegui/QtnPropertyUVector2.h"
#include "src/cegui/QtnPropertyUVector3.h"
#include "src/cegui/QtnPropertyUSize.h"
#include "src/cegui/QtnPropertyURect.h"
#include "src/cegui/QtnPropertyUBox.h"
#include "src/cegui/QtnPropertyColourRect.h"
#include "src/cegui/QtnPropertyGlmVec2.h"
#include "src/cegui/QtnProperty2DRotation.h"
#include "src/cegui/QtnPropertySizef.h"
#include "src/cegui/QtnPropertyRectf.h"
#include "src/ui/CEGUIGraphicsScene.h"
#include "src/util/Settings.h"
#include "src/Application.h"
#include <qgraphicsscene.h>
#include <qpainter.h>
#include <qmessagebox.h>
#include <CEGUI/widgets/TabControl.h>
#include <CEGUI/widgets/ScrollablePane.h>
#include <CEGUI/widgets/ScrolledContainer.h>
#include <CEGUI/widgets/GridLayoutContainer.h>
#include <CEGUI/WindowManager.h>
#include <CEGUI/GUIContext.h>
#include <CEGUI/CoordConverter.h>
#include "QtnProperty/PropertySet.h"
#include "QtnProperty/Core/PropertyQString.h"
#include "QtnProperty/Core/PropertyBool.h"
#include "QtnProperty/Core/PropertyInt.h"
#include "QtnProperty/Core/PropertyUInt.h"
#include "QtnProperty/Core/PropertyFloat.h"
#include "QtnProperty/Core/PropertyDouble.h"
#include "QtnProperty/Core/PropertyEnum.h"
#include "QtnProperty/PropertyInt64.h"
#include "QtnProperty/PropertyUInt64.h"
#include "QtnProperty/Delegates/Core/PropertyDelegateQString.h"
CEGUIManipulator::CEGUIManipulator(QGraphicsItem* parent, CEGUI::Window* widget)
: ResizableRectItem(parent)
, _widget(widget)
{
setFlags(ItemIsFocusable | ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
createPropertySet();
}
CEGUIManipulator::~CEGUIManipulator()
{
delete _propertySet;
}
void CEGUIManipulator::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
painter->save();
if (preventManipulatorOverlap())
{
// NOTE: This is just an option because it's very performance intensive, most people editing big layouts will
// want this disabled. But it makes editing nicer and fancier :-)
// We are drawing the outlines after CEGUI has already been rendered so he have to clip overlapping parts
// we basically query all items colliding with ourselves and if that's a manipulator and is over us we subtract
// that from the clipped path.
QPainterPath clipPath;
clipPath.addRect(QRectF(-scenePos().x(), -scenePos().y(), scene()->sceneRect().width(), scene()->sceneRect().height()));
// FIXME: I used self.collidingItems() but that seems way way slower than just going over everything on the scene
// in reality we need siblings of ancestors recursively up to the top
//
// this just begs for optimisation in the future
auto collidingItems = scene()->items();
for (QGraphicsItem* item : collidingItems)
{
if (!item->isVisible() || item == this) continue;
CEGUIManipulator* manipulator = dynamic_cast<CEGUIManipulator*>(item);
if (!manipulator) continue;
// Check if the item is above us
// FIXME: nasty nasty way to do this
bool isAbove = false;
for (QGraphicsItem* itm : scene()->items())
{
if (itm == this)
{
isAbove = true;
break;
}
else if (itm == item) break;
}
if (isAbove)
{
QPainterPath boundingClipPath;
boundingClipPath.addRect(item->boundingRect());
clipPath = clipPath.subtracted(boundingClipPath.translated(item->scenePos() - scenePos()));
}
}
// We clip using stencil buffers to prevent overlapping outlines appearing
// FIXME: This could potentially get very slow for huge layouts
painter->setClipPath(clipPath);
}
impl_paint(painter, option, widget);
painter->restore();
}
void CEGUIManipulator::impl_paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
ResizableRectItem::paint(painter, option, widget);
if (!_widget) return;
const bool drawGuides = (isSelected() || _resizeInProgress || isAnyHandleSelected());
if (!drawGuides) return;
// Draw guides
const auto baseSize = getBaseSize();
const auto widgetPosition = _widget->getPosition();
const auto widgetSize = _widget->getSize();
const auto scaleXInPixels = static_cast<qreal>(CEGUI::CoordConverter::asAbsolute(CEGUI::UDim(widgetPosition.d_x.d_scale, 0.f), baseSize.d_width));
const auto offsetXInPixels = static_cast<qreal>(widgetPosition.d_x.d_offset);
const auto scaleYInPixels = static_cast<qreal>(CEGUI::CoordConverter::asAbsolute(CEGUI::UDim(widgetPosition.d_y.d_scale, 0.f), baseSize.d_height));
const auto offsetYInPixels = static_cast<qreal>(widgetPosition.d_y.d_offset);
const auto scaleWidthInPixels = static_cast<qreal>(CEGUI::CoordConverter::asAbsolute(CEGUI::UDim(widgetSize.d_width.d_scale, 0.f), baseSize.d_width));
const auto offsetWidthInPixels = static_cast<qreal>(widgetSize.d_width.d_offset);
const auto scaleHeightInPixels = static_cast<qreal>(CEGUI::CoordConverter::asAbsolute(CEGUI::UDim(widgetSize.d_height.d_scale, 0.f), baseSize.d_height));
const auto offsetHeightInPixels = static_cast<qreal>(widgetSize.d_height.d_offset);
QPointF startXPoint(0.0, 0.0);
QPointF startHPoint(0.0, 0.0);
qreal hOffsetX = 1.0;
switch (_widget->getHorizontalAlignment())
{
case CEGUI::HorizontalAlignment::Left:
{
startXPoint = (rect().topLeft() + rect().bottomLeft()) / 2.0;
startHPoint = rect().topRight();
break;
}
case CEGUI::HorizontalAlignment::Centre:
{
startXPoint = rect().center();
startHPoint = rect().topRight();
break;
}
case CEGUI::HorizontalAlignment::Right:
{
startXPoint = (rect().topRight() + rect().bottomRight()) / 2.0;
startHPoint = rect().topLeft();
hOffsetX = -1.0;
break;
}
}
QPointF startYPoint(0.0, 0.0);
QPointF startWPoint(0.0, 0.0);
qreal wOffsetY = 1.0;
switch (_widget->getVerticalAlignment())
{
case CEGUI::VerticalAlignment::Top:
{
startYPoint = (rect().topLeft() + rect().topRight()) / 2.0;
startWPoint = rect().bottomLeft();
break;
}
case CEGUI::VerticalAlignment::Centre:
{
startYPoint = rect().center();
startWPoint = rect().bottomLeft();
break;
}
case CEGUI::VerticalAlignment::Bottom:
{
startYPoint = (rect().bottomLeft() + rect().bottomRight()) / 2.0;
startWPoint = rect().topLeft();
wOffsetY = -1.0;
break;
}
}
const auto midXPoint = startXPoint - QPointF(offsetXInPixels, 0.0);
const auto endXPoint = midXPoint - QPointF(scaleXInPixels, 0.0);
const auto xOffset = QPointF(0.0, (scaleXInPixels * offsetXInPixels < 0.0) ? 1.0 : 0.0);
const auto midYPoint = startYPoint - QPointF(0.0, offsetYInPixels);
const auto endYPoint = midYPoint - QPointF(0.0, scaleYInPixels);
const auto yOffset = QPointF((scaleYInPixels * offsetYInPixels < 0) ? 1.0 : 0.0, 0.0);
const auto midWPoint = startWPoint + QPointF(scaleWidthInPixels, 0.0);
const auto endWPoint = midWPoint + QPointF(offsetWidthInPixels, 0.0);
const auto wOffset = QPointF(0.0, (scaleWidthInPixels * offsetWidthInPixels < 0.0) ? wOffsetY : 0.0);
const auto midHPoint = startHPoint + QPointF(0.0, scaleHeightInPixels);
const auto endHPoint = midHPoint + QPointF(0.0, offsetHeightInPixels);
const auto hOffset = QPointF((scaleHeightInPixels * offsetHeightInPixels < 0.0) ? hOffsetX : 0.0, 0.0);
QPen pen(QColor(0, 255, 0, 255), 0); // Width 0 means 1 px size no matter the transformation
painter->setPen(pen);
painter->drawLine(startXPoint, midXPoint);
painter->drawLine(startYPoint, midYPoint);
painter->drawLine(midWPoint + wOffset, endWPoint + wOffset);
painter->drawLine(midHPoint + hOffset, endHPoint + hOffset);
pen.setColor(QColor(255, 0, 0, 255));
painter->setPen(pen);
painter->drawLine(startWPoint, midWPoint);
painter->drawLine(startHPoint, midHPoint);
painter->drawLine(midXPoint + xOffset, endXPoint + xOffset);
painter->drawLine(midYPoint + yOffset, endYPoint + yOffset);
}
bool CEGUIManipulator::isLayoutContainer() const
{
return !!dynamic_cast<CEGUI::LayoutContainer*>(_widget);
}
bool CEGUIManipulator::isInLayoutContainer() const
{
return dynamic_cast<CEGUI::LayoutContainer*>(_widget->getParent());
}
bool CEGUIManipulator::isInTabControl() const
{
// TabControl stores its tabs in a content pane auto-widget
return _widget->getParent() && dynamic_cast<CEGUI::TabControl*>(_widget->getParent()->getParent());
}
QSizeF CEGUIManipulator::getMinSize() const
{
if (_widget)
{
auto size = CEGUI::CoordConverter::asAbsolute(_widget->getMinSize(), _widget->getRootContainerSize(), false);
return QSizeF(static_cast<qreal>(size.d_width), static_cast<qreal>(size.d_height));
}
return QSizeF();
}
QSizeF CEGUIManipulator::getMaxSize() const
{
if (_widget)
{
auto size = CEGUI::CoordConverter::asAbsolute(_widget->getMaxSize(), _widget->getRootContainerSize(), false);
return QSizeF(static_cast<qreal>(size.d_width), static_cast<qreal>(size.d_height));
}
return QSizeF();
}
CEGUI::Sizef CEGUIManipulator::getBaseSize() const
{
if (_widget && _widget->getParent() && !_widget->isNonClient())
return _widget->getParent()->getUnclippedInnerRect().get().getSize();
else
return _widget->getParentPixelSize();
}
float CEGUIManipulator::getAnchorMinX() const
{
return _widget->getPosition().d_x.d_scale;
}
float CEGUIManipulator::getAnchorMaxX() const
{
return getAnchorMinX() + _widget->getSize().d_width.d_scale;
}
float CEGUIManipulator::getAnchorMinY() const
{
return _widget->getPosition().d_y.d_scale;
}
float CEGUIManipulator::getAnchorMaxY() const
{
return getAnchorMinY() + _widget->getSize().d_height.d_scale;
}
// Returns an effective parent rect in a scene coord system
QRectF CEGUIManipulator::getParentSceneRect() const
{
if (!_widget || !scene()) return QRectF();
if (_widget->getParent())
{
//const CEGUI::Rectf& parentRect = _widget->getParent()->getUnclippedInnerRect().get();
const CEGUI::Rectf& parentRect = _widget->getParent()->getChildContentArea(_widget->isNonClient()).get();
return QRectF(static_cast<qreal>(parentRect.left()),
static_cast<qreal>(parentRect.top()),
static_cast<qreal>(parentRect.getWidth()),
static_cast<qreal>(parentRect.getHeight()));
}
else
{
const CEGUI::Sizef& ctxSize = _widget->getGUIContext().getSurfaceSize();
return QRectF(0.0, 0.0, static_cast<qreal>(ctxSize.d_width), static_cast<qreal>(ctxSize.d_height));
}
}
// Calculates a rect composed of anchor limits (relative parts of the widget area) in a scene coord system
QRectF CEGUIManipulator::getAnchorsSceneRect() const
{
QRectF parentRect = getParentSceneRect();
// LC children have only sizing anchors. These anchors are visually based on the widget position
// rather than on the parent position. It makes sizing more intuitive.
if (isInLayoutContainer())
parentRect.moveTo(scenePos());
const auto& widgetPos = getWidget()->getPosition();
const auto& widgetSize = getWidget()->getSize();
return QRectF(parentRect.x() + parentRect.width() * static_cast<qreal>(widgetPos.d_x.d_scale),
parentRect.y() + parentRect.height() * static_cast<qreal>(widgetPos.d_y.d_scale),
parentRect.width() * static_cast<qreal>(widgetSize.d_width.d_scale),
parentRect.height() * static_cast<qreal>(widgetSize.d_height.d_scale));
}
QPointF CEGUIManipulator::scenePixelToAnchor(QPointF scenePixel) const
{
QRectF parentRect = getParentSceneRect();
// LC children have only sizing anchors. These anchors are visually based on the widget position
// rather than on the parent position. It makes sizing more intuitive.
if (isInLayoutContainer())
parentRect.moveTo(scenePos());
return QPointF(qFuzzyIsNull(parentRect.width()) ? 0.0 : (scenePixel.x() - parentRect.x()) / parentRect.width(),
qFuzzyIsNull(parentRect.height()) ? 0.0 : (scenePixel.y() - parentRect.y()) / parentRect.height());
}
void CEGUIManipulator::setAnchors(float minX, float maxX, float minY, float maxY, bool preserveEffectiveSize)
{
const CEGUI::Sizef baseSize = getBaseSize();
CEGUI::UVector2 deltaPos;
CEGUI::USize deltaSize;
// Min X
{
float deltaMinX = minX - getAnchorMinX();
if (preserveEffectiveSize)
{
float pixelDelta = deltaMinX * baseSize.d_width;
if (useIntegersForAbsoluteResize())
{
pixelDelta = std::round(pixelDelta);
deltaMinX = qFuzzyIsNull(baseSize.d_width) ? 0.f : pixelDelta / baseSize.d_width;
}
deltaPos.d_x.d_offset -= pixelDelta;
deltaSize.d_width.d_offset += pixelDelta;
}
deltaPos.d_x.d_scale += deltaMinX;
deltaSize.d_width.d_scale -= deltaMinX;
}
// Min Y
{
float deltaMinY = minY - getAnchorMinY();
if (preserveEffectiveSize)
{
float pixelDelta = deltaMinY * baseSize.d_height;
if (useIntegersForAbsoluteResize())
{
pixelDelta = std::round(pixelDelta);
deltaMinY = qFuzzyIsNull(baseSize.d_height) ? 0.f : pixelDelta / baseSize.d_height;
}
deltaPos.d_y.d_offset -= pixelDelta;
deltaSize.d_height.d_offset += pixelDelta;
}
deltaPos.d_y.d_scale += deltaMinY;
deltaSize.d_height.d_scale -= deltaMinY;
}
// Max X
{
float deltaMaxX = maxX - getAnchorMaxX();
if (preserveEffectiveSize)
{
float pixelDelta = deltaMaxX * baseSize.d_width;
if (useIntegersForAbsoluteResize())
{
pixelDelta = std::round(pixelDelta);
deltaMaxX = qFuzzyIsNull(baseSize.d_width) ? 0.f : pixelDelta / baseSize.d_width;
}
deltaSize.d_width.d_offset -= pixelDelta;
}
deltaSize.d_width.d_scale += deltaMaxX;
}
// Max Y
{
float deltaMaxY = maxY - getAnchorMaxY();
if (preserveEffectiveSize)
{
float pixelDelta = deltaMaxY * baseSize.d_height;
if (useIntegersForAbsoluteResize())
{
pixelDelta = std::round(pixelDelta);
deltaMaxY = qFuzzyIsNull(baseSize.d_height) ? 0.f : pixelDelta / baseSize.d_height;
}
deltaSize.d_height.d_offset -= pixelDelta;
}
deltaSize.d_height.d_scale += deltaMaxY;
}
adjustPositionDeltaOnResize(deltaPos, deltaSize);
CEGUIUtils::setWidgetArea(_widget, _widget->getPosition() + deltaPos, _widget->getSize() + deltaSize);
updateFromWidget();
updatePropertiesFromWidget({"Size", "Position", "Area"});
}
void CEGUIManipulator::notifyHandleSelected(ResizingHandle* handle)
{
ResizableRectItem::notifyHandleSelected(handle);
moveToFront();
}
void CEGUIManipulator::notifyResizeStarted()
{
ResizableRectItem::notifyResizeStarted();
_resizeStarted = true;
_prevPos = _widget->getPosition();
_prevSize = _widget->getSize();
for (QGraphicsItem* childItem : childItems())
{
if (dynamic_cast<CEGUIManipulator*>(childItem))
childItem->setVisible(false);
}
// Hide siblings in the same layout container
if (isInLayoutContainer())
for (auto item : parentItem()->childItems())
if (item != this && dynamic_cast<CEGUIManipulator*>(item))
item->setVisible(false);
}
static inline void roundPointFloor(QPointF& point)
{
point.setX(std::floor(point.x()));
point.setY(std::floor(point.y()));
}
static inline void roundSizeFloor(QSizeF& size)
{
size.setWidth(std::floor(size.width()));
size.setHeight(std::floor(size.height()));
}
void CEGUIManipulator::notifyResizeProgress(QPointF newPos, QSizeF newSize)
{
assert(_resizeStarted);
ResizableRectItem::notifyResizeProgress(newPos, newSize);
// Absolute pixel deltas
auto pixelDeltaPos = newPos - _resizeStartPos;
auto pixelDeltaSize = newSize - _resizeStartRect.size();
CEGUI::UVector2 deltaPos;
CEGUI::USize deltaSize;
if (useAbsoluteCoordsForResize())
{
if (useIntegersForAbsoluteResize())
{
roundPointFloor(pixelDeltaPos);
roundSizeFloor(pixelDeltaSize);
}
deltaPos = CEGUI::UVector2(
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaPos.x())),
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaPos.y())));
deltaSize = CEGUI::USize(
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaSize.width())),
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaSize.height())));
}
else
{
auto baseSize = getBaseSize();
deltaPos = CEGUI::UVector2(
CEGUI::UDim(static_cast<float>(pixelDeltaPos.x()) / baseSize.d_width, 0.f),
CEGUI::UDim(static_cast<float>(pixelDeltaPos.y()) / baseSize.d_height, 0.f));
deltaSize = CEGUI::USize(
CEGUI::UDim(static_cast<float>(pixelDeltaSize.width()) / baseSize.d_width, 0.f),
CEGUI::UDim(static_cast<float>(pixelDeltaSize.height()) / baseSize.d_height, 0.f));
}
adjustPositionDeltaOnResize(deltaPos, deltaSize);
CEGUIUtils::setWidgetArea(_widget, _prevPos + deltaPos, _prevSize + deltaSize);
updatePropertiesFromWidget({"Size", "Position", "Area"});
}
void CEGUIManipulator::notifyResizeFinished(QPointF newPos, QSizeF newSize)
{
ResizableRectItem::notifyResizeFinished(newPos, newSize);
updateFromWidget();
for (QGraphicsItem* childItem : childItems())
{
CEGUIManipulator* child = dynamic_cast<CEGUIManipulator*>(childItem);
if (child)
{
child->updateFromWidget();
child->setVisible(true);
}
}
// Show siblings in the same layout container
if (isInLayoutContainer())
for (auto item : parentItem()->childItems())
if (item != this && dynamic_cast<CEGUIManipulator*>(item))
item->setVisible(true);
if (parentItem())
static_cast<CEGUIManipulator*>(parentItem())->updateFromWidget(true);
}
void CEGUIManipulator::notifyMoveStarted()
{
ResizableRectItem::notifyMoveStarted();
_moveStarted = true;
_prevPos = _widget->getPosition();
for (QGraphicsItem* childItem : childItems())
{
CEGUIManipulator* child = dynamic_cast<CEGUIManipulator*>(childItem);
if (child) child->setVisible(false);
}
}
void CEGUIManipulator::notifyMoveProgress(QPointF newPos)
{
assert(_moveStarted);
ResizableRectItem::notifyMoveProgress(newPos);
// Absolute pixel deltas
// NB: sometimes we move pos (self), sometimes rect (child handles), so we must take both into account
auto pixelDeltaPos = newPos - _moveStartPos;
CEGUI::UVector2 deltaPos;
if (useAbsoluteCoordsForMove())
{
if (useIntegersForAbsoluteMove()) roundPointFloor(pixelDeltaPos);
deltaPos = CEGUI::UVector2(
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaPos.x())),
CEGUI::UDim(0.f, static_cast<float>(pixelDeltaPos.y())));
}
else
{
auto baseSize = getBaseSize();
deltaPos = CEGUI::UVector2(
CEGUI::UDim(static_cast<float>(pixelDeltaPos.x()) / baseSize.d_width, 0.f),
CEGUI::UDim(static_cast<float>(pixelDeltaPos.y()) / baseSize.d_height, 0.f));
}
_widget->setPosition(_prevPos + deltaPos);
updatePropertiesFromWidget({"Position", "Area"});
}
void CEGUIManipulator::notifyMoveFinished(QPointF newPos)
{
ResizableRectItem::notifyMoveFinished(newPos);
updateFromWidget();
for (QGraphicsItem* childItem : childItems())
{
CEGUIManipulator* child = dynamic_cast<CEGUIManipulator*>(childItem);
if (child)
{
child->updateFromWidget();
child->setVisible(true);
}
}
}
// Updates this manipulator with associated widget properties. Mainly position and size.
// callUpdate - if True we also call update on the widget itself before
// querying its properties
// updateParentLCs - if True we update ancestor layout containers
void CEGUIManipulator::updateFromWidget(bool callUpdate, bool updateAncestorLCs)
{
assert(_widget);
if (!_widget) return;
if (updateAncestorLCs)
{
// We are trying to find a topmost LC (in case of nested LCs) and recursively update it
QGraphicsItem* topmostLC = nullptr;
auto item = parentItem();
while (item && static_cast<CEGUIManipulator*>(item)->isLayoutContainer())
{
topmostLC = item;
item = item->parentItem();
}
if (topmostLC)
{
static_cast<CEGUIManipulator*>(topmostLC)->updateFromWidget(true, false);
// No need to continue, this method will get called again with updateAncestorLCs = false
return;
}
}
if (callUpdate) _widget->update(0.f);
// Just in case widget or widget name changed. Also updates a tooltip.
onWidgetNameChanged();
auto unclippedOuterRect = _widget->getUnclippedOuterRect().getFresh(true);
auto pos = unclippedOuterRect.getPosition();
auto size = unclippedOuterRect.getSize();
// Translate position to the parent manipulator space. Note that this may not be a direct parent widget!
// Also note that we use parent outer rect, not a content area, because outer rects are used for manipulators.
if (parentItem())
if (auto parentWidget = static_cast<CEGUIManipulator*>(parentItem())->getWidget())
pos -= parentWidget->getUnclippedOuterRect().get().getPosition();
_ignoreGeometryChanges = true;
setPos(QPointF(static_cast<qreal>(pos.x), static_cast<qreal>(pos.y)));
setRect(QRectF(0.0, 0.0, static_cast<qreal>(size.d_width), static_cast<qreal>(size.d_height)));
_ignoreGeometryChanges = false;
// If we are updating top to bottom we don't need to update ancestor
// layout containers, they will already be updated
for (auto item : childItems())
if (auto manip = dynamic_cast<CEGUIManipulator*>(item))
manip->updateFromWidget(callUpdate, false);
}
// Detaches itself from the GUI hierarchy and the manipulator hierarchy.
// detachWidget - should we detach the CEGUI widget as well?
// destroyWidget - should we destroy the CEGUI widget after it's detached?
// recursive - recurse into children?
// This method doesn't destroy this instance immediately but it will be destroyed automatically
// when nothing is referencing it.
void CEGUIManipulator::detach(bool detachWidget, bool destroyWidget, bool recursive)
{
// Don't detach or destroy auto windows and their hierarchies
detachWidget = (detachWidget && !_widget->isAutoWindow());
// Descend if recursive
if (recursive)
{
for (QGraphicsItem* childItem : childItems())
{
CEGUIManipulator* child = dynamic_cast<CEGUIManipulator*>(childItem);
if (child)
{
child->detach(detachWidget, destroyWidget, true);
delete child;
}
}
}
// Detach from the GUI hierarchy
if (detachWidget) CEGUIUtils::removeChild(_widget);
// Detach from the parent manipulator
scene()->removeItem(this);
if (detachWidget && destroyWidget)
{
CEGUI::WindowManager::getSingleton().destroyWindow(_widget);
_widget = nullptr;
updateTooltip();
}
}
QString CEGUIManipulator::getWidgetName() const
{
return _widget ? CEGUIUtils::stringToQString(_widget->getName()) : "<Unknown>";
}
QString CEGUIManipulator::getWidgetType() const
{
return _widget ? CEGUIUtils::stringToQString(_widget->getType()) : "<Unknown>";
}
QString CEGUIManipulator::getWidgetFactoryType() const
{
return _widget ? CEGUIUtils::stringToQString(_widget->getFactoryType()) : "<Unknown>";
}
QString CEGUIManipulator::getWidgetPath(bool excludeAutoWidgets) const
{
if (!_widget) return "<Unknown>";
if (!excludeAutoWidgets) return CEGUIUtils::stringToQString(_widget->getNamePath());
// Terminal widget is always present even if it is an auto-widget
QString name = CEGUIUtils::stringToQString(_widget->getName());
auto w = _widget->getParent();
while (w)
{
if (!w->isAutoWindow())
name = CEGUIUtils::stringToQString(w->getName()) + "/" + name;
w = w->getParent();
}
return name;
}
size_t CEGUIManipulator::getWidgetChildCount() const
{
//???handle auto-containers for TabControl and ScrollablePane?
return _widget ? _widget->getChildCount() : 0;
}
size_t CEGUIManipulator::getWidgetIndexInParent() const
{
return (_widget && _widget->getParent()) ? _widget->getParent()->getChildIndex(_widget) : 0;
}
// Creates a child manipulator suitable for a child widget of manipulated widget
// This is there to allow overriding (if user subclasses the Manipulator, child manipulators are likely to be also subclassed)
CEGUIManipulator* CEGUIManipulator::createChildManipulator(CEGUI::Window* childWidget)
{
return new CEGUIManipulator(this, childWidget);
}
void CEGUIManipulator::getChildManipulators(std::vector<CEGUIManipulator*>& outList, bool recursive)
{
for (QGraphicsItem* item : childItems())
{
CEGUIManipulator* manipulator = dynamic_cast<CEGUIManipulator*>(item);
if (manipulator)
{
outList.push_back(manipulator);
if (recursive) manipulator->getChildManipulators(outList, true);
}
}
}
// Retrieves a manipulator relative to this manipulator by given widget path
CEGUIManipulator* CEGUIManipulator::getManipulatorByPath(QStringRef widgetPath) const
{
if (widgetPath.isEmpty()) return const_cast<CEGUIManipulator*>(this);
int sepPos = widgetPath.indexOf('/');
QStringRef baseName = (sepPos < 0) ? widgetPath : widgetPath.left(sepPos);
QStringRef remainder = (sepPos < 0) ? QStringRef() : widgetPath.mid(sepPos + 1);
// Some widgets store their children in auto container, but CEED treats them as
// immediate children. Autocontainer therefore is skipped here.
const QString autoContainerName =
dynamic_cast<CEGUI::TabControl*>(_widget) ? CEGUIUtils::stringToQString(CEGUI::TabControl::ContentPaneName) :
dynamic_cast<CEGUI::ScrollablePane*>(_widget) ? CEGUIUtils::stringToQString(CEGUI::ScrollablePane::ScrolledContainerName) :
"";
if (baseName == autoContainerName)
{
if (remainder.isEmpty()) return const_cast<CEGUIManipulator*>(this);
sepPos = remainder.indexOf('/');
baseName = (sepPos < 0) ? remainder : remainder.left(sepPos);
remainder = (sepPos < 0) ? QStringRef() : remainder.mid(sepPos + 1);
}
for (QGraphicsItem* item : childItems())
{
if (auto manipulator = dynamic_cast<CEGUIManipulator*>(item))
if (manipulator->getWidgetName() == baseName)
return manipulator->getManipulatorByPath(remainder);
}
return nullptr;
}
// TODO: to CEGUIUtils? Pass widget instead of manipulator?
void CEGUIManipulator::forEachChildWidget(std::function<void (CEGUI::Window*)> callback) const
{
if (auto tabControl = dynamic_cast<CEGUI::TabControl*>(_widget))
{
const size_t count = tabControl->getTabCount();
for (size_t i = 0; i < count; ++i)
callback(tabControl->getTabContentsAtIndex(i));
}
else if (auto scrollablePane = dynamic_cast<CEGUI::ScrollablePane*>(_widget))
{
const size_t count = scrollablePane->getContentPane()->getChildCount();
for (size_t i = 0; i < count; ++i)
callback(scrollablePane->getContentPane()->getChildAtIndex(i));
}
else
{
const size_t count = _widget->getChildCount();
for (size_t i = 0; i < count; ++i)
callback(_widget->getChildAtIndex(i));
}
}
// Goes through child widgets of the manipulated widget and creates manipulator for each one.
// recursive - recurse into children?
// skipAutoWidgets - if true, auto widgets will be skipped over
// checkExisting - hint to skip search, useful for initial construction
void CEGUIManipulator::createChildManipulators(bool recursive, bool skipAutoWidgets, bool checkExisting)
{
forEachChildWidget([this, skipAutoWidgets, recursive, checkExisting](CEGUI::Window* childWidget)
{
if (checkExisting && getManipulatorByPath(CEGUIUtils::stringToQString(childWidget->getName())))
return;
if (childWidget->isAutoWindow())
{
// Grid LC creates dummy placeholder auto-widgets. We don't want manipulators for them.
if (skipAutoWidgets || dynamic_cast<CEGUI::GridLayoutContainer*>(_widget))
return;
}
auto childManipulator = createChildManipulator(childWidget);
childManipulator->updateFromWidget();
if (recursive)
childManipulator->createChildManipulators(true, skipAutoWidgets, checkExisting);
});
}
void CEGUIManipulator::moveToFront()
{
_widget->moveToFront();
if (!parentItem()) return;
for (auto&& item : parentItem()->childItems())
if (item != this)
item->stackBefore(this);
static_cast<CEGUIManipulator*>(parentItem())->moveToFront();
}
static void updatePropertyFromWidget(CEGUI::Window& widget, const CEGUI::Property& ceguiProp, QtnProperty& prop)
{
// FIXME: Qtn doesn't allow to update immutable property even if it is changed in object.
// For now we hack this by temporarily enable writing.
if (!ceguiProp.isWritable())
prop.removeState(QtnPropertyStateImmutable);
prop.fromStr(CEGUIUtils::stringToQString(ceguiProp.get(&widget)));
// FIXME: see above
if (!ceguiProp.isWritable())
prop.addState(QtnPropertyStateImmutable);
}
// Notify the property manager that the values of the given properties have changed for this widget
void CEGUIManipulator::updatePropertiesFromWidget(const QStringList& propertyNames)
{
for (const QString& propertyName : propertyNames)
{
auto it = _propertyMap.find(propertyName);
if (it != _propertyMap.end())
{
updatePropertyFromWidget(*_widget, *it->second.first, *it->second.second);
if (propertyName == "Name") onWidgetNameChanged();
}
}
}
void CEGUIManipulator::updateAllPropertiesFromWidget()
{
for (const auto& pair : _propertyMap)
updatePropertyFromWidget(*_widget, *pair.second.first, *pair.second.second);
onWidgetNameChanged();
}
void CEGUIManipulator::createPropertySet()
{
_propertyMap.clear();
if (_propertySet) delete _propertySet;
if (!_widget)
{
_propertySet = nullptr;
return;
}
_propertySet = new QtnPropertySet(nullptr);
std::map<QString, QtnPropertySet*> subsets;
auto it = _widget->getPropertyIterator();
while (!it.isAtEnd())
{
CEGUI::Property* ceguiProp = it.getCurrentValue();
if (!ceguiProp->isReadable())
{
++it;
continue;
}
// Categorize properties by CEGUI property origin
QtnPropertySet* parentSet = _propertySet;
QString propName = CEGUIUtils::stringToQString(ceguiProp->getName());
QString category = CEGUIUtils::stringToQString(ceguiProp->getOrigin());
if (category.startsWith("CEGUI/")) category = category.mid(6);
if (!category.isEmpty())
{
auto it = subsets.find(category);
if (it == subsets.end())
{
// Insertion into _propertySet delayed for sorting, see below
parentSet = new QtnPropertySet(QtnPropertySet::SortOrder::Ascend);
parentSet->setParent(_propertySet);
parentSet->setName(category);
subsets.emplace(std::move(category), parentSet);
}
else parentSet = it->second;
}
QtnProperty* prop = nullptr;
const auto& propertyDataType = ceguiProp->getDataType(); // could be overridden through a property map
if (propertyDataType == "bool")
prop = new QtnPropertyBool(parentSet);
else if (propertyDataType == "std::uint32_t")
{
auto typedProp = new QtnPropertyUInt(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<std::uint32_t>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "std::uint64_t")
{
auto typedProp = new QtnPropertyUInt64(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<std::uint64_t>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "int16")
{
auto typedProp = new QtnPropertyInt(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<std::int16_t>().fromString(ceguiProp->getDefault(_widget)));
typedProp->setMinValue(std::numeric_limits<uint16_t>().min());
typedProp->setMaxValue(std::numeric_limits<uint16_t>().max());
prop = typedProp;
}
else if (propertyDataType == "int32")
{
auto typedProp = new QtnPropertyInt(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<std::int32_t>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "int64")
{
auto typedProp = new QtnPropertyInt64(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<std::int64_t>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "float")
{
auto typedProp = new QtnPropertyFloat(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<float>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "double")
{
auto typedProp = new QtnPropertyDouble(parentSet);
typedProp->setDefaultValue(CEGUI::PropertyHelper<double>().fromString(ceguiProp->getDefault(_widget)));
prop = typedProp;
}
else if (propertyDataType == "HorizontalAlignment")
{
auto enumProp = new QtnPropertyEnum(parentSet);
enumProp->setEnumInfo(&CEGUIManager::Instance().enumHorizontalAlignment());
prop = enumProp;
}
else if (propertyDataType == "VerticalAlignment")
{
auto enumProp = new QtnPropertyEnum(parentSet);
enumProp->setEnumInfo(&CEGUIManager::Instance().enumVerticalAlignment());
prop = enumProp;
}
else if (propertyDataType == "AspectMode")
{
auto enumProp = new QtnPropertyEnum(parentSet);
enumProp->setEnumInfo(&CEGUIManager::Instance().enumAspectMode());
prop = enumProp;
}
else if (propertyDataType == "DefaultParagraphDirection")
{
auto enumProp = new QtnPropertyEnum(parentSet);
enumProp->setEnumInfo(&CEGUIManager::Instance().enumDefaultParagraphDirection());
prop = enumProp;
}
else if (propertyDataType == "WindowUpdateMode")