Skip to content

Commit

Permalink
Add checkable widget component
Browse files Browse the repository at this point in the history
Linked: #92
  • Loading branch information
AndreasLrx committed Jun 9, 2022
1 parent c6a861a commit 52a9cc0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/game/gui/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ set(SRCROOT ${SRCROOT}/components)

set (SRC
${SRC}
${SRCROOT}/Checkable.cpp
${SRCROOT}/Checkable.hpp
${SRCROOT}/Clickable.cpp
${SRCROOT}/Clickable.hpp
${SRCROOT}/IWidgetComponent.hpp
${SRCROOT}/Widget.cpp
${SRCROOT}/Widget.hpp
PARENT_SCOPE)
Expand Down
27 changes: 27 additions & 0 deletions src/game/gui/components/Checkable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2022
** Bomberman
** File description:
** Checkable
*/

#include "Checkable.hpp"

namespace game
{
namespace gui
{
bool Checkable::update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event)
{
if (event.action != GameAction::ACTION)
return false;

if (event.value == 0.f) {
checked = !checked;
if (onStateChanged)
onStateChanged(self, checked);
}
return true;
};
} // namespace gui
} // namespace game
22 changes: 15 additions & 7 deletions src/game/gui/components/Checkable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@
** Checkable
*/

#ifndef GAME_GUI_COMPONENTS_CLICKABLE_HPP_
#define GAME_GUI_COMPONENTS_CLICKABLE_HPP_
#ifndef GAME_GUI_COMPONENTS_CHECKABLE_HPP_
#define GAME_GUI_COMPONENTS_CHECKABLE_HPP_

#include "ecs/Component.hpp"
#include "ecs/Entity.hpp"
#include <functional>
#include "IWidgetComponent.hpp"

namespace game
{
namespace gui
{
struct Checkable : public ecs::Component {
struct Checkable final : public IWidgetComponent {
using OnStateChanged = std::function<void(ecs::Entity, bool)>;

OnStateChanged onStateChanged;
bool checked;

Checkable(bool startChecked = false) : checked(startChecked) {}
Checkable(OnStateChanged ponStateChanged, bool startChecked = false)
: onStateChanged(ponStateChanged), checked(startChecked)
{
}

bool update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event) override final;
};
} // namespace gui
} // namespace game

#endif /* !GAME_GUI_COMPONENTS_CLICKABLE_HPP_ */
#endif /* !GAME_GUI_COMPONENTS_CHECKABLE_HPP_ */
5 changes: 2 additions & 3 deletions src/game/gui/components/Clickable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ namespace game
onStateChanged(self, state);
if (newState == State::Default && onClick)
onClick(self);
return true;
} else
return false;
}
return true;
};
} // namespace gui
} // namespace game
9 changes: 3 additions & 6 deletions src/game/gui/components/Clickable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
#define GAME_GUI_COMPONENTS_CLICKABLE_HPP_

#include <functional>
#include "ecs/Component.hpp"
#include "ecs/Entity.hpp"
#include "ecs/System.hpp"
#include "game/Users.hpp"
#include "IWidgetComponent.hpp"

namespace game
{
namespace gui
{
struct Clickable : public ecs::Component {
struct Clickable final : public IWidgetComponent {
enum class State { Default, Pressed };

using OnClick = std::function<void(ecs::Entity)>;
Expand All @@ -34,7 +31,7 @@ namespace game
{
}

bool update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event);
bool update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event) override final;
};
} // namespace gui
} // namespace game
Expand Down
28 changes: 28 additions & 0 deletions src/game/gui/components/IWidgetComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
** EPITECH PROJECT, 2022
** Bomberman
** File description:
** IWidgetComponent
*/

#ifndef GAME_GUI_IWIDGETCOMPONENT_HPP_
#define GAME_GUI_IWIDGETCOMPONENT_HPP_

#include "ecs/Component.hpp"
#include "ecs/Entity.hpp"
#include "ecs/System.hpp"
#include "game/Users.hpp"


namespace game
{
namespace gui
{
class IWidgetComponent : public ecs::Component {
public:
virtual bool update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event) = 0;
};
} // namespace gui
} // namespace game

#endif /* !GAME_GUI_IWIDGETCOMPONENT_HPP_ */
5 changes: 3 additions & 2 deletions src/game/gui/components/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

#include "Widget.hpp"

#include "Checkable.hpp"
#include "Clickable.hpp"

#include "logger/Logger.hpp"

namespace game
{
namespace gui
Expand All @@ -19,6 +18,8 @@ namespace game
{
if (tryUpdate<Clickable>(self, data, event))
return true;
if (tryUpdate<Checkable>(self, data, event))
return true;
return false;
}
} // namespace gui
Expand Down
2 changes: 0 additions & 2 deletions src/game/systems/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include "game/Users.hpp"
#include "game/gui/components/Widget.hpp"

#include "logger/Logger.hpp"

namespace game
{
void InputManager::run(ecs::SystemData data)
Expand Down

0 comments on commit 52a9cc0

Please sign in to comment.