From 37dbfa7fa7b4efb0c65b0ac99f6f2cc06f37ee13 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Mon, 7 Jun 2021 15:56:52 +0200 Subject: [PATCH] button componenent now a templated class --- CMakeLists.txt | 1 - sources/Component/Button/ButtonComponent.cpp | 29 ----------- sources/Component/Button/ButtonComponent.hpp | 46 +++++++++-------- sources/Runner/Runner.cpp | 49 ++++--------------- .../MenuControllableSystem.cpp | 9 ++-- 5 files changed, 40 insertions(+), 94 deletions(-) delete mode 100644 sources/Component/Button/ButtonComponent.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bad7a7c6..0c07cfbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,6 @@ set(SOURCES sources/Component/Collision/CollisionComponent.hpp sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.cpp - sources/Component/Button/ButtonComponent.cpp sources/Component/Button/ButtonComponent.hpp sources/System/MenuControllable/MenuControllableSystem.cpp sources/System/MenuControllable/MenuControllableSystem.hpp diff --git a/sources/Component/Button/ButtonComponent.cpp b/sources/Component/Button/ButtonComponent.cpp deleted file mode 100644 index 88c2fb3f..00000000 --- a/sources/Component/Button/ButtonComponent.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by Louis Auzuret on 06/03/21. -// - -#include "ButtonComponent.hpp" - -namespace BBM -{ - ButtonComponent::ButtonComponent(WAL::Entity &entity) - : WAL::Component(entity), onIdle(), onHover(), onClick() - { } - - WAL::Component *ButtonComponent::clone(WAL::Entity &entity) const - { - return new ButtonComponent(entity, onIdle, onHover, onClick, onHold); - } - - ButtonComponent::ButtonComponent(WAL::Entity &entity, WAL::Callback idleCallback, - WAL::Callback hoverCallback, WAL::Callback clickCallback, WAL::Callback holdCallback) - : WAL::Component(entity), - onIdle(idleCallback), - onHover(hoverCallback), - onClick(clickCallback), - onHold(holdCallback) - { } - - void ButtonComponent::emptyButtonCallback(WAL::Entity &) - { } -} \ No newline at end of file diff --git a/sources/Component/Button/ButtonComponent.hpp b/sources/Component/Button/ButtonComponent.hpp index db023383..3ce02284 100644 --- a/sources/Component/Button/ButtonComponent.hpp +++ b/sources/Component/Button/ButtonComponent.hpp @@ -10,40 +10,44 @@ namespace BBM { - class ButtonComponent : public WAL::Component + enum ButtonComponentType { IDLE, CLICK, HOVER }; + + template + class ButtonComponent: public WAL::Component { public: - //! @brief onIdle callback - WAL::Callback onIdle; - - //! @brief onHover callback - WAL::Callback onHover; - - //! @brief onClick callback, when the mouse button is released - WAL::Callback onClick; - - //! @brief onHold callback, when the mouse button is pressed - WAL::Callback onHold; + //! @brief onEvent callback + WAL::Callback onEvent; - //! @inherit - WAL::Component *clone(WAL::Entity &entity) const override; + WAL::Component *clone(WAL::Entity &entity) const override + { + return new ButtonComponent(entity, onEvent); + } //! @brief Initialize a new Button component. - explicit ButtonComponent(WAL::Entity &entity); + explicit ButtonComponent(WAL::Entity &entity) + : WAL::Component(entity), onEvent() + { } //! @brief Constructor with the 3 callback - ButtonComponent(WAL::Entity &entity, WAL::Callback idleCallback, WAL::Callback hoverCallback, - WAL::Callback clickCallback, WAL::Callback holdCallback); + ButtonComponent(WAL::Entity &entity, WAL::Callback callback) + : WAL::Component(entity), + onEvent(callback) + { } //! @brief A Controllable component is copy constructable. - ButtonComponent(const ButtonComponent &) = default; + ButtonComponent(const ButtonComponent &) = default; //! @brief default destructor ~ButtonComponent() override = default; //! @brief A Button component default assign operator - ButtonComponent &operator=(const ButtonComponent &) = default; + ButtonComponent &operator=(const ButtonComponent &) = default; //! @brief Empty button callback - static void emptyButtonCallback(WAL::Entity &); + static void emptyButtonCallback(WAL::Entity &) + { } }; -} \ No newline at end of file + typedef ButtonComponent OnIdleComponent; + typedef ButtonComponent OnClickComponent; + typedef ButtonComponent OnHoverComponent; +} diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 4fcda23f..bcd442b7 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -80,16 +80,14 @@ namespace BBM scene->addEntity("text_prompt") .addComponent(1920 / 5, 1080 - 180, 0) .addComponent("Press any button to continue", 70, RAY::Vector2(), WHITE) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity) { entity.getComponent().drawable->setColor(WHITE); - }, - [](WAL::Entity &entity) + }) + .addComponent([](WAL::Entity &entity) { entity.getComponent().drawable->setColor(ORANGE); - }, - ButtonComponent::emptyButtonCallback, - ButtonComponent::emptyButtonCallback); + }); //needed material //music //sound @@ -112,51 +110,24 @@ namespace BBM scene->addEntity("play button") .addComponent(1920 / 2.5, 1080 - 540, 0) .addComponent("assets/buttons/button_new_game.png") - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_new_game.png"); - }, [](WAL::Entity &entity) + }) + .addComponent([](WAL::Entity &entity) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_new_game_hovered.png"); - }, - ButtonComponent::emptyButtonCallback, - ButtonComponent::emptyButtonCallback); + }); scene->addEntity("settings button") .addComponent(1920 / 2.5, 1080 - 360, 0) - .addComponent("assets/buttons/button_settings.png") - .addComponent([](WAL::Entity &entity) - { - RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); - - texture->use("assets/buttons/button_settings.png"); - }, [](WAL::Entity &entity) - { - RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); - - texture->use("assets/buttons/button_settings_hovered.png"); - }, - ButtonComponent::emptyButtonCallback, - ButtonComponent::emptyButtonCallback); + .addComponent("assets/buttons/button_settings.png"); scene->addEntity("exit button") .addComponent(1920 / 2.5, 1080 - 180, 0) - .addComponent("assets/buttons/button_exit.png") - .addComponent([](WAL::Entity &entity) - { - RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); - - texture->use("assets/buttons/button_exit.png"); - }, [](WAL::Entity &entity) - { - RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); - - texture->use("assets/buttons/button_exit_hovered.png"); - }, - ButtonComponent::emptyButtonCallback, - ButtonComponent::emptyButtonCallback); + .addComponent("assets/buttons/button_exit.png"); //needed material //music //sound diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index b46d5a8b..9b6331a1 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -37,7 +37,7 @@ namespace BBM move = controllable.move; select = controllable.bomb; - auto &buttons = wal.scene->view(); + auto &buttons = wal.scene->view(); ssize_t index = 0; //std::sort(buttons.begin(), buttons.end(), //[](WAL::Entity &first, WAL::Entity &second) { @@ -48,13 +48,14 @@ namespace BBM //}); updateButtonIndex(buttons.size()); for (auto &button : buttons) { + auto &buttonEntity = static_cast(button); if (index++ == _buttonIndex) { - button.get().onHover(button); + buttonEntity.getComponent().onEvent(button); if (select) - button.get().onClick(button); + button.get().onEvent(button); continue; } - button.get().onIdle(button); + buttonEntity.getComponent().onEvent(button); } }