forked from texus/TGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.cpp
1556 lines (1236 loc) · 60.8 KB
/
Container.cpp
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2022 Bruno Van de Velde ([email protected])
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <TGUI/Container.hpp>
#include <TGUI/ToolTip.hpp>
#include <TGUI/Backend/Window/BackendGui.hpp>
#include <TGUI/Widgets/RadioButton.hpp>
#include <TGUI/SubwidgetContainer.hpp>
#include <TGUI/Loading/WidgetFactory.hpp>
#include <TGUI/Filesystem.hpp>
#include <fstream>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void getAllRenderers(std::vector<RendererData*>& orderedRenderers, std::map<RendererData*, std::vector<const Widget*>>& rendererToWidgetsMap, const Container* container)
{
const auto addRenderer = [&](RendererData* rendererData, const Widget* widget){
auto it = rendererToWidgetsMap.find(rendererData);
if (it != rendererToWidgetsMap.end())
it->second.push_back(widget);
else
{
// Add the renderer to the orderedRenderers list when it occurs the first time.
// This allows renderers to be saved in order of appearance instead of in a random order.
rendererToWidgetsMap[rendererData].push_back(widget);
orderedRenderers.push_back(rendererData);
}
};
for (const auto& child : container->getWidgets())
{
addRenderer(child->getSharedRenderer()->getData().get(), child.get());
if (child->getToolTip())
addRenderer(child->getToolTip()->getSharedRenderer()->getData().get(), child->getToolTip().get());
Container* childContainer = dynamic_cast<Container*>(child.get());
if (childContainer)
getAllRenderers(orderedRenderers, rendererToWidgetsMap, childContainer);
else
{
SubwidgetContainer* subWidgetContainer = dynamic_cast<SubwidgetContainer*>(child.get());
if (subWidgetContainer)
{
addRenderer(subWidgetContainer->getContainer()->getSharedRenderer()->getData().get(), subWidgetContainer->getContainer());
getAllRenderers(orderedRenderers, rendererToWidgetsMap, subWidgetContainer->getContainer());
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<DataIO::Node> saveRenderer(RendererData* renderer, const String& name)
{
auto node = std::make_unique<DataIO::Node>();
node->name = name;
for (const auto& pair : renderer->propertyValuePairs)
{
if (pair.second.getType() == ObjectConverter::Type::RendererData)
{
std::stringstream ss{ObjectConverter{pair.second}.getString().toStdString()};
auto rendererRootNode = DataIO::parse(ss);
// If there are braces around the renderer string, then the child node is the one we need
if (rendererRootNode->propertyValuePairs.empty() && (rendererRootNode->children.size() == 1))
rendererRootNode = std::move(rendererRootNode->children[0]);
rendererRootNode->name = pair.first;
node->children.push_back(std::move(rendererRootNode));
}
else
{
String value = ObjectConverter{pair.second}.getString();
// Skip empty values
if (value.empty())
continue;
// Skip "Font = null"
if (pair.first == "Font" && value == "null")
continue;
node->propertyValuePairs[pair.first] = std::make_unique<DataIO::ValueNode>(value);
}
}
return node;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void makePathsRelativeToForm(const std::unique_ptr<DataIO::Node>& node, const String& formPath)
{
for (const auto& pair : node->propertyValuePairs)
{
if (!pair.first.starts_with(U"Texture") && (pair.first != U"Font") && (pair.first != U"Image") && (pair.first != U"Icon"))
continue;
if (pair.second->value.empty() || pair.second->value.equalIgnoreCase(U"none") || pair.second->value.equalIgnoreCase(U"null") || pair.second->value.equalIgnoreCase(U"nullptr"))
continue;
String filename;
if (pair.second->value[0] != '"')
filename = pair.second->value;
else
{
// The filename is surrounded by quotes, with optional options behind it
const auto endQuotePos = pair.second->value.find('"', 1);
assert(endQuotePos != String::npos); // DataIO wouldn't have accepted the file if there is no close quote
filename = pair.second->value.substr(1, endQuotePos - 1);
}
// Make the path relative to the form file
if (filename.starts_with(formPath))
{
if (pair.second->value[0] != '"')
pair.second->value.erase(0, formPath.length());
else
pair.second->value.erase(1, formPath.length());
}
}
for (const auto& childNode : node->children)
makePathsRelativeToForm(childNode, formPath);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container::Container(const char* typeName, bool initRenderer) :
Widget{typeName, initRenderer}
{
m_containerWidget = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container::Container(const Container& other) :
Widget{other}
{
// Widgets with layouts that refer to each other need to be added simultaneously.
// They all need to be in m_widgets before setParent is called on the first widget,
// which is why we can't just use call add(widget) for each widget.
m_widgets.reserve(other.m_widgets.size());
for (const auto& widget : other.m_widgets)
m_widgets.emplace_back(widget->clone());
for (const auto& widget : m_widgets)
widgetAdded(widget);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container::Container(Container&& other) noexcept :
Widget {std::move(other)},
m_widgets {std::move(other.m_widgets)},
m_widgetBelowMouse {std::move(other.m_widgetBelowMouse)},
m_widgetWithLeftMouseDown {std::move(other.m_widgetWithLeftMouseDown)},
m_widgetWithRightMouseDown{std::move(other.m_widgetWithRightMouseDown)},
m_focusedWidget {std::move(other.m_focusedWidget)}
{
// Parent of all widgets should be set to nullptr first, in case widgets have layouts depending on each other.
// Otherwise calling setParent on one widget could cause another widget's position to be recalculated which could
// give a warning if it still has its old parent where it won't find any siblings.
for (auto& widget : m_widgets)
widget->setParent(nullptr);
for (auto& widget : m_widgets)
widget->setParent(this);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container::~Container()
{
for (const auto& widget : m_widgets)
{
if (widget->getParent() == this)
widget->setParent(nullptr);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container& Container::operator= (const Container& right)
{
// Make sure it is not the same widget
if (this != &right)
{
Widget::operator=(right);
m_widgetBelowMouse = nullptr;
m_widgetWithLeftMouseDown = nullptr;
m_widgetWithRightMouseDown = nullptr;
m_focusedWidget = nullptr;
// Remove all the old widgets
Container::removeAllWidgets();
// Widgets with layouts that refer to each other need to be added simultaneously.
// They all need to be in m_widgets before setParent is called on the first widget,
// which is why we can't just use call add(widget) for each widget.
m_widgets.reserve(right.m_widgets.size());
for (auto& widget : right.m_widgets)
m_widgets.emplace_back(widget->clone());
for (auto& widget : m_widgets)
widgetAdded(widget);
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Container& Container::operator= (Container&& right) noexcept
{
// Make sure it is not the same widget
if (this != &right)
{
m_widgets = std::move(right.m_widgets);
m_widgetBelowMouse = std::move(right.m_widgetBelowMouse);
m_widgetWithLeftMouseDown = std::move(right.m_widgetWithLeftMouseDown);
m_widgetWithRightMouseDown = std::move(right.m_widgetWithRightMouseDown);
m_focusedWidget = std::move(right.m_focusedWidget);
Widget::operator=(std::move(right));
// Parent of all widgets should be set to nullptr first, in case widgets have layouts depending on each other.
// Otherwise calling setParent on one widget could cause another widget's position to be recalculated which could
// give a warning if it still has its old parent where it won't find any siblings.
for (auto& widget : m_widgets)
widget->setParent(nullptr);
for (auto& widget : m_widgets)
widget->setParent(this);
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::setSize(const Layout2d& size)
{
if (size.getValue() != m_prevSize)
{
Widget::setSize(size);
m_prevInnerSize = getInnerSize();
}
else // Size didn't change, but also check the inner size in case the borders or padding changed
{
Widget::setSize(size);
if (getInnerSize() != m_prevInnerSize)
{
m_prevInnerSize = getInnerSize();
for (auto& layout : m_boundSizeLayouts)
layout->recalculateValue();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::add(const Widget::Ptr& widgetPtr, const String& widgetName)
{
TGUI_ASSERT(widgetPtr != nullptr, "Can't add nullptr to container");
m_widgets.push_back(widgetPtr);
if (!widgetName.empty())
widgetPtr->setWidgetName(widgetName);
widgetAdded(widgetPtr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Widget::Ptr Container::get(const String& widgetName) const
{
// First search for direct children
for (const auto& child : m_widgets)
{
if (child->getWidgetName() == widgetName)
return child;
}
// If no widget was found then search recursively
for (const auto& child : m_widgets)
{
if (child->isContainer())
{
Widget::Ptr widget = std::static_pointer_cast<Container>(child)->get(widgetName);
if (widget != nullptr)
return widget;
}
}
// If we still couldn't find it then check if there are any SubwidgetContainer widgets and search their subwidgets
for (const auto& child : m_widgets)
{
auto subWidgetContainer = dynamic_cast<const SubwidgetContainer*>(child.get());
if (subWidgetContainer)
{
Widget::Ptr widget = subWidgetContainer->getContainer()->get(widgetName);
if (widget != nullptr)
return widget;
}
}
return nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::remove(const Widget::Ptr& widget)
{
// Loop through every widget
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] != widget)
continue;
if (widget == m_widgetBelowMouse && m_parentGui && (widget->getMouseCursor() != m_mouseCursor))
m_parentGui->requestMouseCursor(m_mouseCursor);
if (m_widgetBelowMouse == widget)
m_widgetBelowMouse = nullptr;
if (m_widgetWithLeftMouseDown == widget)
m_widgetWithLeftMouseDown = nullptr;
if (m_widgetWithRightMouseDown == widget)
m_widgetWithRightMouseDown = nullptr;
if (widget == m_focusedWidget)
{
m_focusedWidget = nullptr;
widget->setFocused(false);
}
// Remove the widget
widget->setParent(nullptr);
m_widgets.erase(m_widgets.begin() + static_cast<std::ptrdiff_t>(i));
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::removeAllWidgets()
{
if (m_mouseHover && m_parentGui && (m_mouseCursor != Cursor::Type::Arrow))
m_parentGui->requestMouseCursor(m_mouseCursor);
for (const auto& widget : m_widgets)
widget->setParent(nullptr);
m_widgets.clear();
m_widgetBelowMouse = nullptr;
m_widgetWithLeftMouseDown = nullptr;
m_widgetWithRightMouseDown = nullptr;
m_focusedWidget = nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Vector2f Container::getInnerSize() const
{
return getSize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::updateTextSize()
{
if (m_textSizeCached == 0)
return;
for (const auto& widget : m_widgets)
widget->setTextSize(m_textSizeCached);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::loadWidgetsFromFile(const String& filename, bool replaceExisting)
{
auto oldTheme = tgui::Theme::getDefault();
tgui::Theme::setDefault(nullptr);
// If a resource path is set then place it in front of the filename (unless the filename is an absolute path)
String filenameInResources = filename;
if (!getResourcePath().isEmpty())
filenameInResources = (getResourcePath() / filename).asString();
std::size_t fileSize;
auto fileContents = readFileToMemory(filenameInResources, fileSize);
if (!fileContents)
throw Exception{"Failed to open '" + filenameInResources + "' to load the widgets from it."};
/// TODO: Optimize this (parse function should be able to use a string view directly on file contents)
std::stringstream stream{std::string{reinterpret_cast<const char*>(fileContents.get()), fileSize}};
const auto rootNode = DataIO::parse(stream);
// All files need to be loaded relative to the form file
const auto& parentPath = Filesystem::Path(filename).getParentPath();
if (!parentPath.isEmpty())
{
std::map<String, bool> checkedFilenames;
injectFormFilePath(rootNode, parentPath.asString(), checkedFilenames);
}
loadWidgetsFromNodeTree(rootNode, replaceExisting);
tgui::Theme::setDefault(oldTheme);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::saveWidgetsToFile(const String& filename)
{
// If a resource path is set then place it in front of the filename (unless the filename is an absolute path)
String filenameInResources = filename;
if (!getResourcePath().isEmpty())
filenameInResources = (getResourcePath() / filename).asString();
const String formFileDir = Filesystem::Path(filename).getParentPath().asString();
std::stringstream stream;
saveWidgetsToStream(stream, formFileDir);
if (!writeFile(filenameInResources, stream))
throw Exception{"Failed to write '" + filenameInResources + "' while trying to save widgets in it."};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::loadWidgetsFromStream(std::stringstream& stream, bool replaceExisting)
{
const auto rootNode = DataIO::parse(stream);
loadWidgetsFromNodeTree(rootNode, replaceExisting);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::loadWidgetsFromStream(std::stringstream&& stream, bool replaceExisting)
{
loadWidgetsFromStream(stream, replaceExisting);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::saveWidgetsToStream(std::stringstream& stream, const String& rootDirectory) const
{
auto rootNode = saveWidgetsToNodeTree(rootDirectory);
DataIO::emit(rootNode, stream);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::loadWidgetsFromNodeTree(const std::unique_ptr<DataIO::Node>& rootNode, bool replaceExisting)
{
// Replace the existing widgets by the ones that will be loaded if requested
if (replaceExisting)
removeAllWidgets();
if (rootNode->propertyValuePairs.size() != 0)
Widget::load(rootNode, {});
std::vector<std::pair<Widget::Ptr, std::reference_wrapper<const std::unique_ptr<DataIO::Node>>>> widgetsToLoad;
std::map<String, std::shared_ptr<RendererData>> availableRenderers;
for (const auto& node : rootNode->children)
{
auto nameSeparator = node->name.find('.');
auto widgetType = node->name.substr(0, nameSeparator);
String objectName;
if (nameSeparator != String::npos)
objectName = Deserializer::deserialize(ObjectConverter::Type::String, node->name.substr(nameSeparator + 1)).getString();
if (widgetType == "Renderer")
{
if (!objectName.empty())
availableRenderers[objectName] = RendererData::createFromDataIONode(node.get());
}
else // Section describes a widget
{
const auto& constructor = WidgetFactory::getConstructFunction(widgetType);
if (constructor)
{
Widget::Ptr widget = constructor();
add(widget, objectName);
// We delay loading of widgets until they have all been added to the container.
// Otherwise there would be issues if their position and size layouts refer to
// widgets that have not yet been loaded.
widgetsToLoad.emplace_back(widget, std::cref(node));
}
else
throw Exception{"No construct function exists for widget type '" + widgetType + "'."};
}
}
for (auto& pair : widgetsToLoad)
{
Widget::Ptr& widget = pair.first;
const auto& node = pair.second.get();
widget->load(node, availableRenderers);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<DataIO::Node> Container::saveWidgetsToNodeTree(const String& rootDirectory) const
{
auto rootNode = std::make_unique<DataIO::Node>();
std::vector<RendererData*> orderedRenderers;
std::map<RendererData*, std::vector<const Widget*>> rendererToWidgetsMap;
getAllRenderers(orderedRenderers, rendererToWidgetsMap, this);
unsigned int id = 0;
SavingRenderersMap renderersMap;
for (const auto& renderer : orderedRenderers)
{
assert(rendererToWidgetsMap.find(renderer) != rendererToWidgetsMap.end());
const auto& widgetsUsingRenderer = rendererToWidgetsMap[renderer];
// The renderer can remain inside the widget if it is not shared, so provide the node to be included inside the widget
if (widgetsUsingRenderer.size() == 1)
{
renderersMap[widgetsUsingRenderer[0]] = {saveRenderer(renderer, "Renderer"), ""};
continue;
}
// When the widget is shared, only provide the id instead of the node itself
++id;
const String idStr = String::fromNumber(id);
rootNode->children.push_back(saveRenderer(renderer, "Renderer." + idStr));
for (const auto& child : widgetsUsingRenderer)
renderersMap[child] = std::make_pair(nullptr, idStr); // Did not compile with VS2015 Update 2 when using braces
}
for (const auto& child : getWidgets())
rootNode->children.emplace_back(child->save(renderersMap));
if (!rootDirectory.empty())
{
if ((rootDirectory.back() != '/') && (rootDirectory.back() != '\\'))
makePathsRelativeToForm(rootNode, rootDirectory + U'/');
else
makePathsRelativeToForm(rootNode, rootDirectory);
}
return rootNode;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::moveWidgetToFront(const Widget::Ptr& widget)
{
// Loop through all widgets
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] != widget)
continue;
// Copy the widget
m_widgets.push_back(m_widgets[i]);
// Remove the old widget
m_widgets.erase(m_widgets.begin() + static_cast<std::ptrdiff_t>(i));
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::moveWidgetToBack(const Widget::Ptr& widget)
{
// Loop through all widgets
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] != widget)
continue;
// Copy the widget
const Widget::Ptr obj = m_widgets[i];
m_widgets.insert(m_widgets.begin(), obj);
// Remove the old widget
m_widgets.erase(m_widgets.begin() + static_cast<std::ptrdiff_t>(i + 1));
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::size_t Container::moveWidgetForward(const Widget::Ptr& widget)
{
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] != widget)
continue;
// If the widget is already at the front then we can't move it further forward
if (i == m_widgets.size() - 1)
return m_widgets.size() - 1;
std::swap(m_widgets[i], m_widgets[i+1]);
return i + 1;
}
// The widget wasn't found in this container
return m_widgets.size();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::size_t Container::moveWidgetBackward(const Widget::Ptr& widget)
{
for (std::size_t i = m_widgets.size(); i > 0; --i)
{
if (m_widgets[i-1] != widget)
continue;
// If the widget is already at the back then we can't move it further backward
if (i-1 == 0)
return 0;
std::swap(m_widgets[i-2], m_widgets[i-1]);
return i-2;
}
// The widget wasn't found in this container
return m_widgets.size();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::setWidgetIndex(const Widget::Ptr& widget, std::size_t index)
{
if (index >= m_widgets.size())
return false;
std::size_t currentWidgetIndex = m_widgets.size();
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] == widget)
{
currentWidgetIndex = i;
break;
}
}
if (currentWidgetIndex == m_widgets.size())
return false;
if (index == currentWidgetIndex)
return true;
// Move the widget to the new index
m_widgets.erase(m_widgets.begin() + static_cast<std::ptrdiff_t>(currentWidgetIndex));
m_widgets.insert(m_widgets.begin() + static_cast<std::ptrdiff_t>(index), widget);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Container::getWidgetIndex(const Widget::Ptr& widget) const
{
for (std::size_t i = 0; i < m_widgets.size(); ++i)
{
if (m_widgets[i] == widget)
return static_cast<int>(i);
}
return -1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Widget::Ptr Container::getFocusedChild() const
{
return m_focusedWidget;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Widget::Ptr Container::getFocusedLeaf() const
{
if (!m_focusedWidget || !m_focusedWidget->isContainer())
return m_focusedWidget;
auto leafWidget = std::static_pointer_cast<Container>(m_focusedWidget)->getFocusedLeaf();
// If the container has no focused child then the container itself is the leaf
if (!leafWidget)
return m_focusedWidget;
return leafWidget;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Widget::Ptr Container::getWidgetAtPosition(Vector2f pos) const
{
pos -= getPosition() + getChildWidgetsOffset();
for (auto it = m_widgets.crbegin(); it != m_widgets.crend(); ++it)
{
const auto& widget = *it;
// Look for a visible widget below the mouse
if (!widget->isVisible())
continue;
if (!widget->isMouseOnWidget(transformMousePos(widget, pos)))
continue;
// If the widget is a container then look inside it
if (widget->isContainer())
{
Container::Ptr container = std::static_pointer_cast<Container>(widget);
auto childWidget = container->getWidgetAtPosition(transformMousePos(widget, pos));
if (childWidget)
return childWidget;
}
// If the widget isn't a container, or there were no child widgets inside it, then return this widget
return widget;
}
// No visible widgets were found at the queried position
return nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::focusNextWidget(bool recursive)
{
// If the focused widget is a container then try to focus the next widget inside it
if (recursive && m_focusedWidget && m_focusedWidget->isContainer())
{
auto focusedContainer = std::static_pointer_cast<Container>(m_focusedWidget);
if (focusedContainer->focusNextWidget(true))
return true;
}
// Loop all widgets behind the focused one
const std::size_t focusedWidgetIndex = getFocusedWidgetIndex();
for (std::size_t i = focusedWidgetIndex; i < m_widgets.size(); ++i)
{
if (tryFocusWidget(m_widgets[i], false, recursive))
return true;
}
// If we are not an isolated focus group then the focus will be given to the group behind us
if (recursive && !m_isolatedFocus)
return false;
// None of the widgets behind the focused one could be focused, so loop the ones before it
if (!m_focusedWidget)
return false;
// Also include the focused widget since it may be a container that didn't have its first widget focused
for (std::size_t i = 0; i < focusedWidgetIndex; ++i)
{
if (tryFocusWidget(m_widgets[i], false, recursive))
return true;
}
// No other widget could be focused
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::focusPreviousWidget(bool recursive)
{
// If the focused widget is a container then try to focus the previous widget inside it
if (recursive && m_focusedWidget && m_focusedWidget->isContainer())
{
auto focusedContainer = std::static_pointer_cast<Container>(m_focusedWidget);
if (focusedContainer->focusPreviousWidget())
return true;
}
// Loop all widgets before the focused one
const std::size_t focusedWidgetIndex = getFocusedWidgetIndex();
if (focusedWidgetIndex > 0)
{
for (std::size_t i = focusedWidgetIndex - 1; i > 0; --i)
{
if (tryFocusWidget(m_widgets[i-1], true, recursive))
return true;
}
// If we are not an isolated focus group then the focus will be given to the group before us
if (recursive && !m_isolatedFocus)
return false;
}
// None of the widgets before the focused one could be focused, so loop the ones after it.
for (std::size_t i = m_widgets.size(); i > focusedWidgetIndex; --i)
{
if (tryFocusWidget(m_widgets[i-1], true, recursive))
return true;
}
// Also include the focused widget since it may be a container that didn't have its last widget focused.
if (focusedWidgetIndex > 0)
{
if (tryFocusWidget(m_widgets[focusedWidgetIndex-1], true, recursive))
return true;
}
// No other widget could be focused
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::setFocused(bool focused)
{
if (m_focusedWidget && (focused != m_focusedWidget->isFocused()))
m_focusedWidget->setFocused(focused);
Widget::setFocused(focused);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::childWidgetFocused(const Widget::Ptr& child)
{
if (m_focusedWidget != child)
{
if (m_focusedWidget)
m_focusedWidget->setFocused(false);
m_focusedWidget = child;
}
if (!isFocused())
setFocused(true);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::leftMousePressed(Vector2f pos)
{
Widget::leftMousePressed(pos);
processMousePressEvent(Event::MouseButton::Left, pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::rightMousePressed(Vector2f pos)
{
Widget::rightMousePressed(pos);
processMousePressEvent(Event::MouseButton::Right, pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::leftMouseReleased(Vector2f pos)
{
Widget::leftMouseReleased(pos);
processMouseReleaseEvent(Event::MouseButton::Left, pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::rightMouseReleased(Vector2f pos)
{
Widget::rightMouseReleased(pos);
processMouseReleaseEvent(Event::MouseButton::Right, pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::mouseMoved(Vector2f pos)
{
Widget::mouseMoved(pos);
processMouseMoveEvent(pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::keyPressed(const Event::KeyEvent& event)
{
processKeyPressEvent(event);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::textEntered(char32_t key)
{
processTextEnteredEvent(key);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::mouseWheelScrolled(float delta, Vector2f pos)
{
return processMouseWheelScrollEvent(delta, pos - getPosition() - getChildWidgetsOffset());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::mouseNoLongerOnWidget()
{
if (!m_mouseHover)
return;
mouseLeftWidget();
if (m_widgetBelowMouse)
{
m_widgetBelowMouse->mouseNoLongerOnWidget();
m_widgetBelowMouse = nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::leftMouseButtonNoLongerDown()
{
Widget::leftMouseButtonNoLongerDown();
if (m_widgetWithLeftMouseDown)
{
m_widgetWithLeftMouseDown->leftMouseButtonNoLongerDown();
m_widgetWithLeftMouseDown = nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::rightMouseButtonNoLongerDown()
{
Widget::rightMouseButtonNoLongerDown();
if (m_widgetWithRightMouseDown)
{
m_widgetWithRightMouseDown->rightMouseButtonNoLongerDown();
m_widgetWithRightMouseDown = nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Widget::Ptr Container::askToolTip(Vector2f mousePos)
{
if (!isMouseOnWidget(mousePos))
return nullptr;
// We shouldn't show tooltips when dragging something
if (m_widgetWithLeftMouseDown && (m_widgetWithLeftMouseDown->isDraggableWidget() || m_widgetWithLeftMouseDown->isContainer()))
return nullptr;
Widget::Ptr toolTip = nullptr;
mousePos -= getPosition() + getChildWidgetsOffset();
Widget::Ptr widget = getWidgetBelowMouse(mousePos);
if (widget && (widget->isEnabled() || ToolTip::getShowOnDisabledWidget()))
{
toolTip = widget->askToolTip(transformMousePos(widget, mousePos));
if (toolTip)