Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
GUIObjectEventBroadcaster.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "precompiled.h"
19
20#ifndef GUIOBJECTEVENTBROADCASTER
21#define GUIOBJECTEVENTBROADCASTER
22
24#include "lib/alignment.h"
27
28#include <algorithm>
29#include <utility>
30#include <vector>
31
32namespace
33{
35 {
37 // Index of the object in a depth-first search inside GUI tree.
38 std::uint32_t index;
39 // Cached value of GetBufferedZ to avoid recursive calls in a deep hierarchy.
40 float bufferedZ;
41 };
42
43 template<class Container>
44 void CollectVisibleObjectsRecursively(const std::vector<IGUIObject*>& objects, Container* visibleObjects)
45 {
46 for (IGUIObject* const& object : objects)
47 if (!object->IsHidden())
48 {
49 visibleObjects->emplace_back(VisibleObject{ object,
50 static_cast<std::uint32_t>(visibleObjects->size()), 0.0f });
51 CollectVisibleObjectsRecursively(object->GetVisibleChildren(), visibleObjects);
52 }
53 }
54}
55
57{
58public:
59 template<typename... Args>
60 static void RecurseVisibleObject(IGUIObject* object, void(IGUIObject::* callbackFunction)(Args... args), Args&&... args)
61 {
62 PS::Memory::LinearAllocator allocator{128 * KiB, 4 * MiB};
64
65 std::vector<VisibleObject, ObjectListAllocator> visibleObjects((ObjectListAllocator(allocator)));
66 CollectVisibleObjectsRecursively(object->GetVisibleChildren(), &visibleObjects);
67 for (VisibleObject& visibleObject : visibleObjects)
68 visibleObject.bufferedZ = visibleObject.object->GetBufferedZ();
69
70 std::sort(visibleObjects.begin(), visibleObjects.end(), [](const VisibleObject& visibleObject1, const VisibleObject& visibleObject2) -> bool {
71 if (visibleObject1.bufferedZ != visibleObject2.bufferedZ)
72 return visibleObject1.bufferedZ < visibleObject2.bufferedZ;
73 return visibleObject1.index < visibleObject2.index;
74 });
75
76 for (const VisibleObject& visibleObject : visibleObjects)
77 (visibleObject.object->*callbackFunction)(std::forward<Args>(args)...);
78 }
79};
80
81#endif // !GUIOBJECTEVENTBROADCASTER
static const size_t KiB
Definition: alignment.h:107
static const size_t MiB
Definition: alignment.h:108
Definition: GUIObjectEventBroadcaster.h:57
static void RecurseVisibleObject(IGUIObject *object, void(IGUIObject::*callbackFunction)(Args... args), Args &&... args)
Definition: GUIObjectEventBroadcaster.h:60
GUI object such as a button or an input-box.
Definition: IGUIObject.h:63
virtual const std::vector< IGUIObject * > & GetVisibleChildren() const
Definition: IGUIObject.h:226
Provides a way to use untyped allocators like LinearAllocator for containers.
Definition: AllocatorAdapter.h:43
Linear allocator (also known as an arena allocator or bump allocator) is a simple and fast system tha...
Definition: LinearAllocator.h:50
void CollectVisibleObjectsRecursively(const std::vector< IGUIObject * > &objects, Container *visibleObjects)
Definition: GUIObjectEventBroadcaster.h:44
Definition: GUIObjectEventBroadcaster.h:35
IGUIObject * object
Definition: GUIObjectEventBroadcaster.h:36
float bufferedZ
Definition: GUIObjectEventBroadcaster.h:40
std::uint32_t index
Definition: GUIObjectEventBroadcaster.h:38