From 3a40f0aba8d5c4a12fde3c007d76ca2e5a4c0d12 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 8 Jun 2021 17:36:29 +0200 Subject: [PATCH] smarter event application --- sources/Runner/Runner.cpp | 8 ++-- .../MenuControllableSystem.cpp | 39 ++++++++++--------- .../MenuControllableSystem.hpp | 3 +- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index f8678fbc..1b3769d8 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -107,7 +107,7 @@ namespace BBM .addComponent("assets/logo_big.png"); scene->addEntity("text_prompt") .addComponent(1920 / 2.5, 1080 - 130, 0) - .addComponent("Press space", 70, RAY::Vector2(), ORANGE) + .addComponent("Press space", 70, RAY::Vector2(), BLACK) .addComponent() .addComponent() .addComponent([](WAL::Entity &entity, WAL::Wal &) @@ -296,7 +296,7 @@ namespace BBM .addComponent("assets/logo_small.png"); auto &music = scene->addEntity("music text") .addComponent(1920 / 2.5, 1080 - 540, 0) - .addComponent("Music Volume", 70, RAY::Vector2(), ORANGE) + .addComponent("Music Volume", 70, RAY::Vector2(), BLACK) .addComponent() .addComponent([](WAL::Entity &entity, WAL::Wal &) { @@ -343,7 +343,7 @@ namespace BBM auto &sound = scene->addEntity("sound text") .addComponent(1920 / 2.5, 1080 - 360, 0) - .addComponent("Sound Volume", 70, RAY::Vector2(), ORANGE) + .addComponent("Sound Volume", 70, RAY::Vector2(), BLACK) .addComponent() .addComponent([](WAL::Entity &entity, WAL::Wal &) { @@ -390,7 +390,7 @@ namespace BBM auto &debug = scene->addEntity("debug text") .addComponent(1920 / 2.5, 1080 - 180, 0) - .addComponent("Debug Mode: Off", 70, RAY::Vector2(), ORANGE) + .addComponent("Debug Mode: Off", 70, RAY::Vector2(), BLACK) .addComponent([](WAL::Entity &entity, WAL::Wal &wal) { RAY2D::Text *text = dynamic_cast(entity.getComponent().drawable.get()); diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index 81fff19e..1bada121 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -15,18 +15,26 @@ namespace BBM : System(wal), wal(wal), currentButton() {} - void MenuControllableSystem::updateCurrentButton() + void MenuControllableSystem::updateCurrentButton(bool selected) { auto buttonComponent = this->currentButton->getComponent(); + WAL::Entity *newButton = nullptr; + if (move.y > 0 && buttonComponent._up) - this->currentButton = buttonComponent._up; + newButton = buttonComponent._up; if (move.y < 0 && buttonComponent._down) - this->currentButton = buttonComponent._down; + newButton = buttonComponent._down; if (move.x < 0 && buttonComponent._right) - this->currentButton = buttonComponent._right; + newButton = buttonComponent._right; if (move.x > 0 && buttonComponent._left) - this->currentButton = buttonComponent._left; - + newButton = buttonComponent._left; + if (newButton) { + this->currentButton->getComponent().onEvent(*this->currentButton, wal); + this->currentButton = newButton; + this->currentButton->getComponent().onEvent(*this->currentButton, wal); + } + if (selected) + this->currentButton->getComponent().onEvent(*this->currentButton, wal); } void MenuControllableSystem::onFixedUpdate(WAL::ViewEntity &entity) @@ -41,22 +49,17 @@ namespace BBM move = controllable.move; select = controllable.jump; - if (currentButton && currentButton->_scene.getID() != wal.scene->getID()) + if (currentButton && currentButton->_scene.getID() != wal.scene->getID()) { + currentButton->getComponent().onEvent(*this->currentButton, wal); currentButton = nullptr; - if (currentButton == nullptr && buttons.size()) + } + if (currentButton == nullptr && buttons.size()) { currentButton = &(**buttons.begin()); + currentButton->getComponent().onEvent(*this->currentButton, wal); + } if (!currentButton) return; - this->updateCurrentButton(); - for (auto &[buttonEntity, clickComponent, hoverComponent, idleComponent]: buttons) { - if (buttonEntity == *currentButton) { - hoverComponent.onEvent(buttonEntity, wal); - if (select) - clickComponent.onEvent(buttonEntity, wal); - continue; - } - idleComponent.onEvent(buttonEntity, wal); - } + this->updateCurrentButton(select); } void MenuControllableSystem::onSelfUpdate(void) diff --git a/sources/System/MenuControllable/MenuControllableSystem.hpp b/sources/System/MenuControllable/MenuControllableSystem.hpp index 7e80a773..86e16130 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.hpp +++ b/sources/System/MenuControllable/MenuControllableSystem.hpp @@ -30,7 +30,8 @@ namespace BBM bool cancel = false; //! @brief update current button reference - void updateCurrentButton(); + //! @param selected lets know if te new selected button is 'pressed' + void updateCurrentButton(bool selected); //! @brief time (in mili second) since last check std::chrono::time_point _now;