Line data Source code
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 : 23 : #include "gui/ObjectBases/IGUIObject.h" 24 : #include "lib/alignment.h" 25 : #include "ps/memory/AllocatorAdapter.h" 26 : #include "ps/memory/LinearAllocator.h" 27 : 28 : #include <algorithm> 29 : #include <utility> 30 : #include <vector> 31 : 32 : namespace 33 : { 34 : struct VisibleObject 35 : { 36 : IGUIObject* object; 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 0 : void CollectVisibleObjectsRecursively(const std::vector<IGUIObject*>& objects, Container* visibleObjects) 45 : { 46 0 : for (IGUIObject* const& object : objects) 47 0 : if (!object->IsHidden()) 48 : { 49 0 : visibleObjects->emplace_back(VisibleObject{ object, 50 0 : static_cast<std::uint32_t>(visibleObjects->size()), 0.0f }); 51 0 : CollectVisibleObjectsRecursively(object->GetVisibleChildren(), visibleObjects); 52 : } 53 0 : } 54 : } 55 : 56 : class CGUIObjectEventBroadcaster 57 : { 58 : public: 59 : template<typename... Args> 60 0 : static void RecurseVisibleObject(IGUIObject* object, void(IGUIObject::* callbackFunction)(Args... args), Args&&... args) 61 : { 62 0 : PS::Memory::LinearAllocator allocator{128 * KiB, 4 * MiB}; 63 : using ObjectListAllocator = PS::Memory::AllocatorAdapter<VisibleObject, PS::Memory::LinearAllocator>; 64 : 65 0 : std::vector<VisibleObject, ObjectListAllocator> visibleObjects((ObjectListAllocator(allocator))); 66 0 : CollectVisibleObjectsRecursively(object->GetVisibleChildren(), &visibleObjects); 67 0 : for (VisibleObject& visibleObject : visibleObjects) 68 0 : visibleObject.bufferedZ = visibleObject.object->GetBufferedZ(); 69 : 70 0 : std::sort(visibleObjects.begin(), visibleObjects.end(), [](const VisibleObject& visibleObject1, const VisibleObject& visibleObject2) -> bool { 71 0 : if (visibleObject1.bufferedZ != visibleObject2.bufferedZ) 72 0 : return visibleObject1.bufferedZ < visibleObject2.bufferedZ; 73 0 : return visibleObject1.index < visibleObject2.index; 74 : }); 75 : 76 0 : for (const VisibleObject& visibleObject : visibleObjects) 77 0 : (visibleObject.object->*callbackFunction)(std::forward<Args>(args)...); 78 0 : } 79 : }; 80 : 81 : #endif // !GUIOBJECTEVENTBROADCASTER