button componenent now a templated class

This commit is contained in:
arthur.jamet
2021-06-07 15:56:52 +02:00
parent 5bba1f68e8
commit 37dbfa7fa7
5 changed files with 40 additions and 94 deletions
-1
View File
@@ -67,7 +67,6 @@ set(SOURCES
sources/Component/Collision/CollisionComponent.hpp sources/Component/Collision/CollisionComponent.hpp
sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.hpp
sources/System/Collision/CollisionSystem.cpp sources/System/Collision/CollisionSystem.cpp
sources/Component/Button/ButtonComponent.cpp
sources/Component/Button/ButtonComponent.hpp sources/Component/Button/ButtonComponent.hpp
sources/System/MenuControllable/MenuControllableSystem.cpp sources/System/MenuControllable/MenuControllableSystem.cpp
sources/System/MenuControllable/MenuControllableSystem.hpp sources/System/MenuControllable/MenuControllableSystem.hpp
@@ -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<WAL::Entity &> idleCallback,
WAL::Callback<WAL::Entity &> hoverCallback, WAL::Callback<WAL::Entity &> clickCallback, WAL::Callback<WAL::Entity &> holdCallback)
: WAL::Component(entity),
onIdle(idleCallback),
onHover(hoverCallback),
onClick(clickCallback),
onHold(holdCallback)
{ }
void ButtonComponent::emptyButtonCallback(WAL::Entity &)
{ }
}
+24 -20
View File
@@ -10,40 +10,44 @@
namespace BBM namespace BBM
{ {
class ButtonComponent : public WAL::Component enum ButtonComponentType { IDLE, CLICK, HOVER };
template<enum ButtonComponentType T>
class ButtonComponent: public WAL::Component
{ {
public: public:
//! @brief onIdle callback //! @brief onEvent callback
WAL::Callback<WAL::Entity &> onIdle; WAL::Callback<WAL::Entity &> onEvent;
//! @brief onHover callback
WAL::Callback<WAL::Entity &> onHover;
//! @brief onClick callback, when the mouse button is released
WAL::Callback<WAL::Entity &> onClick;
//! @brief onHold callback, when the mouse button is pressed
WAL::Callback<WAL::Entity &> onHold;
//! @inherit //! @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. //! @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 //! @brief Constructor with the 3 callback
ButtonComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &> idleCallback, WAL::Callback<WAL::Entity &> hoverCallback, ButtonComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &> callback)
WAL::Callback<WAL::Entity &> clickCallback, WAL::Callback<WAL::Entity &> holdCallback); : WAL::Component(entity),
onEvent(callback)
{ }
//! @brief A Controllable component is copy constructable. //! @brief A Controllable component is copy constructable.
ButtonComponent(const ButtonComponent &) = default; ButtonComponent(const ButtonComponent<T> &) = default;
//! @brief default destructor //! @brief default destructor
~ButtonComponent() override = default; ~ButtonComponent() override = default;
//! @brief A Button component default assign operator //! @brief A Button component default assign operator
ButtonComponent &operator=(const ButtonComponent &) = default; ButtonComponent<T> &operator=(const ButtonComponent<T> &) = default;
//! @brief Empty button callback //! @brief Empty button callback
static void emptyButtonCallback(WAL::Entity &); static void emptyButtonCallback(WAL::Entity &)
{ }
}; };
typedef ButtonComponent<IDLE> OnIdleComponent;
typedef ButtonComponent<CLICK> OnClickComponent;
typedef ButtonComponent<HOVER> OnHoverComponent;
} }
+10 -39
View File
@@ -80,16 +80,14 @@ namespace BBM
scene->addEntity("text_prompt") scene->addEntity("text_prompt")
.addComponent<PositionComponent>(1920 / 5, 1080 - 180, 0) .addComponent<PositionComponent>(1920 / 5, 1080 - 180, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Press any button to continue", 70, RAY::Vector2(), WHITE) .addComponent<Drawable2DComponent, RAY2D::Text>("Press any button to continue", 70, RAY::Vector2(), WHITE)
.addComponent<ButtonComponent>([](WAL::Entity &entity) .addComponent<OnIdleComponent>([](WAL::Entity &entity)
{ {
entity.getComponent<Drawable2DComponent>().drawable->setColor(WHITE); entity.getComponent<Drawable2DComponent>().drawable->setColor(WHITE);
}, })
[](WAL::Entity &entity) .addComponent<OnHoverComponent>([](WAL::Entity &entity)
{ {
entity.getComponent<Drawable2DComponent>().drawable->setColor(ORANGE); entity.getComponent<Drawable2DComponent>().drawable->setColor(ORANGE);
}, });
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
//needed material //needed material
//music //music
//sound //sound
@@ -112,51 +110,24 @@ namespace BBM
scene->addEntity("play button") scene->addEntity("play button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 540, 0) .addComponent<PositionComponent>(1920 / 2.5, 1080 - 540, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_new_game.png") .addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_new_game.png")
.addComponent<ButtonComponent>([](WAL::Entity &entity) .addComponent<OnIdleComponent>([](WAL::Entity &entity)
{ {
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get()); RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_new_game.png"); texture->use("assets/buttons/button_new_game.png");
}, [](WAL::Entity &entity) })
.addComponent<OnHoverComponent>([](WAL::Entity &entity)
{ {
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get()); RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_new_game_hovered.png"); texture->use("assets/buttons/button_new_game_hovered.png");
}, });
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
scene->addEntity("settings button") scene->addEntity("settings button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 360, 0) .addComponent<PositionComponent>(1920 / 2.5, 1080 - 360, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_settings.png") .addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_settings.png");
.addComponent<ButtonComponent>([](WAL::Entity &entity)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_settings.png");
}, [](WAL::Entity &entity)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_settings_hovered.png");
},
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
scene->addEntity("exit button") scene->addEntity("exit button")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 180, 0) .addComponent<PositionComponent>(1920 / 2.5, 1080 - 180, 0)
.addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_exit.png") .addComponent<Drawable2DComponent, RAY::Texture>("assets/buttons/button_exit.png");
.addComponent<ButtonComponent>([](WAL::Entity &entity)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_exit.png");
}, [](WAL::Entity &entity)
{
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.getComponent<Drawable2DComponent>().drawable.get());
texture->use("assets/buttons/button_exit_hovered.png");
},
ButtonComponent::emptyButtonCallback,
ButtonComponent::emptyButtonCallback);
//needed material //needed material
//music //music
//sound //sound
@@ -37,7 +37,7 @@ namespace BBM
move = controllable.move; move = controllable.move;
select = controllable.bomb; select = controllable.bomb;
auto &buttons = wal.scene->view<ButtonComponent>(); auto &buttons = wal.scene->view<OnClickComponent>();
ssize_t index = 0; ssize_t index = 0;
//std::sort(buttons.begin(), buttons.end(), //std::sort(buttons.begin(), buttons.end(),
//[](WAL::Entity &first, WAL::Entity &second) { //[](WAL::Entity &first, WAL::Entity &second) {
@@ -48,13 +48,14 @@ namespace BBM
//}); //});
updateButtonIndex(buttons.size()); updateButtonIndex(buttons.size());
for (auto &button : buttons) { for (auto &button : buttons) {
auto &buttonEntity = static_cast<WAL::Entity &>(button);
if (index++ == _buttonIndex) { if (index++ == _buttonIndex) {
button.get<ButtonComponent>().onHover(button); buttonEntity.getComponent<OnHoverComponent>().onEvent(button);
if (select) if (select)
button.get<ButtonComponent>().onClick(button); button.get<OnClickComponent>().onEvent(button);
continue; continue;
} }
button.get<ButtonComponent>().onIdle(button); buttonEntity.getComponent<OnIdleComponent>().onEvent(button);
} }
} }