From 574d767d54edbca6d4a01e6ab876a26f7478ff5f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Jan 2022 01:46:50 +0000 Subject: Process UI layers in reverse, so it's stack like --- lib/collection.hpp | 69 ++++++++++++++++++++++++++++++++++++------------ test/test-collection.cpp | 10 +++++++ ui/gameMainWindow.cpp | 3 ++- ui/window.cpp | 2 +- 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/lib/collection.hpp b/lib/collection.hpp index a80faa2..8b39ae4 100644 --- a/lib/collection.hpp +++ b/lib/collection.hpp @@ -26,29 +26,32 @@ public: } } - template + template auto - apply(const M & m, Params &&... params) const + apply(const auto & m, Params &&... params) const { - return std::count_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast(op.get())) { - std::invoke(m, o, std::forward(params)...); - return true; - } - return false; - }); + return apply_internal(objects.begin(), objects.end(), m, std::forward(params)...); } - template + template auto - applyOne(const M & m, Params &&... params) const + rapply(const auto & m, Params &&... params) const { - return std::find_if(objects.begin(), objects.end(), [&m, ¶ms...](auto && op) { - if (auto o = dynamic_cast(op.get())) { - return std::invoke(m, o, std::forward(params)...); - } - return false; - }); + return apply_internal(objects.rbegin(), objects.rend(), m, std::forward(params)...); + } + + template + auto + applyOne(const auto & m, Params &&... params) const + { + return applyOne_internal(objects.begin(), objects.end(), m, std::forward(params)...); + } + + template + auto + rapplyOne(const auto & m, Params &&... params) const + { + return applyOne_internal(objects.rbegin(), objects.rend(), m, std::forward(params)...); } template @@ -67,4 +70,36 @@ public: { return objects.end(); } + + auto + rend() const + { + return objects.rend(); + } + +protected: + template + auto + apply_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::count_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast(op.get())) { + std::invoke(m, o, std::forward(params)...); + return true; + } + return false; + }); + } + + template + auto + applyOne_internal(const auto begin, const auto end, const auto & m, Params &&... params) const + { + return std::find_if(begin, end, [&m, ¶ms...](auto && op) { + if (auto o = dynamic_cast(op.get())) { + return std::invoke(m, o, std::forward(params)...); + } + return false; + }); + } }; diff --git a/test/test-collection.cpp b/test/test-collection.cpp index 6e54da3..f31b3a6 100644 --- a/test/test-collection.cpp +++ b/test/test-collection.cpp @@ -35,6 +35,7 @@ public: using TestCollection = Collection; BOOST_TEST_DONT_PRINT_LOG_VALUE(Collection::Objects::const_iterator) +BOOST_TEST_DONT_PRINT_LOG_VALUE(Collection::Objects::const_reverse_iterator) BOOST_FIXTURE_TEST_SUITE(tc, TestCollection) @@ -54,6 +55,15 @@ BOOST_AUTO_TEST_CASE(a_base) BOOST_CHECK_EQUAL(i, end()); } +BOOST_AUTO_TEST_CASE(a_rbase) +{ + auto b = create(); + BOOST_REQUIRE(rapply(&Base::add)); + BOOST_CHECK_EQUAL(b->total, 1); + const auto i = rapplyOne(&Base::add); + BOOST_CHECK_EQUAL(i, rend()); +} + BOOST_AUTO_TEST_CASE(a_sub) { auto s = create(); diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index 3f9ce8c..b559341 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -75,6 +75,7 @@ public: else { clicked.clear(); } + return true; } } return false; @@ -88,9 +89,9 @@ private: GameMainWindow::GameMainWindow(size_t w, size_t h) : Window {w, h, "I Like Trains"}, camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, rdiv(w, h), 0.1F, 10000.0F} { + uiComponents.create(glm::vec2 {-1150, -1150}); uiComponents.create(&camera, glm::vec2 {w, h}); uiComponents.create(); - uiComponents.create(glm::vec2 {-1150, -1150}); shader.setUniform("lightDirection", glm::normalize(glm::vec3 {1, 0, -1})); shader.setUniform("lightColor", {.6, .6, .6}); diff --git a/ui/window.cpp b/ui/window.cpp index 74a3a2f..9ecc88c 100644 --- a/ui/window.cpp +++ b/ui/window.cpp @@ -63,7 +63,7 @@ Window::handleInput(const SDL_Event & e) eAdjusted.button.y = size.y - e.button.y; break; } - uiComponents.applyOne(&UIComponent::handleInput, eAdjusted, UIComponent::Position {{}, size}); + uiComponents.rapplyOne(&UIComponent::handleInput, eAdjusted, UIComponent::Position {{}, size}); return true; } return false; -- cgit v1.2.3