Skip to content

Commit

Permalink
Call widget update method of selected widget on user input
Browse files Browse the repository at this point in the history
Linked: #92
  • Loading branch information
AndreasLrx committed Jun 9, 2022
1 parent b6de59a commit 5772347
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 48 deletions.
14 changes: 11 additions & 3 deletions src/game/components/Controlable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@

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

namespace game
{
/// Controlable entity, may be a widget or a player
struct Controlable : public ecs::Component {
using Callback = std::function<bool(const Users::ActionEvent &)>;
/// Callback called when an action with the matching @c userId is detected.
///
/// @param ecs::Entity entity @a owning the controlable component.
/// @param ecs::SystemData view on the world.
/// @param const Users::ActionEvent& informations about the action detected.
/// @retval true if the action was consumed.
/// @retval false otherwise.
using ActionCallback = std::function<bool(ecs::Entity, ecs::SystemData, const Users::ActionEvent &)>;

/// Id of the user controlling the widget
User::UserId userId;
Callback callback;
ActionCallback callback;

Controlable(User::UserId id) : userId(id) {}
Controlable(User::UserId id, Callback pcallback) : userId(id), callback(pcallback) {}
Controlable(User::UserId id, ActionCallback pcallback) : userId(id), callback(pcallback) {}
};
} // namespace game

Expand Down
2 changes: 1 addition & 1 deletion src/game/gui/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set (SRC
${SRC}
${SRCROOT}/Checkable.hpp
${SRCROOT}/Clickable.hpp
${SRCROOT}/Selectable.hpp
${SRCROOT}/Widget.cpp
${SRCROOT}/Widget.hpp
PARENT_SCOPE)

Expand Down
25 changes: 0 additions & 25 deletions src/game/gui/components/Selectable.hpp

This file was deleted.

27 changes: 27 additions & 0 deletions src/game/gui/components/Widget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2022
** Bomberman
** File description:
** Widget
*/

#include "Widget.hpp"

#include "logger/Logger.hpp"

namespace game
{
namespace gui
{
bool Widget::update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event)
{
(void)self;
(void)data;
(void)event;

Logger::logger.log(Logger::Severity::Debug, "Update widget");

return false;
}
} // namespace gui
} // namespace game
7 changes: 5 additions & 2 deletions src/game/gui/components/Widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ namespace game

WidgetTag previous;
WidgetTag next;
bool selected;

Widget(WidgetTag ptag, WidgetTag pprevious = NullTag, WidgetTag pnext = NullTag)
: tag(ptag), previous(pprevious), next(pnext)
Widget(WidgetTag ptag, WidgetTag pprevious = NullTag, WidgetTag pnext = NullTag, bool pselected = false)
: tag(ptag), previous(pprevious), next(pnext), selected(pselected)
{
}

bool update(ecs::Entity self, ecs::SystemData data, const Users::ActionEvent &event);
};
} // namespace gui
} // namespace game
Expand Down
17 changes: 9 additions & 8 deletions src/game/systems/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ namespace game
{
auto optionnalWidgets = ecs::maybe(data.getStorage<gui::Widget>());

for (auto [widget, controlable, _] :
for (auto [widget, controlable, entity] :
ecs::join(optionnalWidgets, data.getStorage<Controlable>(), data.getResource<ecs::Entities>())) {
/// This entity doesn't listen the sender of the event.
if (controlable.userId != event.user)
continue;
if (!widget) {
Logger::logger.log(Logger::Severity::Debug, "non Widget event");
if (controlable.callback && controlable.callback(event))
return;
} else {
Logger::logger.log(Logger::Severity::Debug, "Widget event");
}
/// The widget consumed the event.
if (widget && widget->selected && widget->update(entity, data, event))
return;
/// The entity isn't a widget (or hasn't consumed the event) and has a custom callback.
if (controlable.callback && controlable.callback(entity, data, event))
return;
}
}

} // namespace game
20 changes: 11 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,16 @@ int main()
.with<game::Position>(0.f, 0.f)
.with<game::Textual>("Hello ECS", 40, raylib::core::Color::RED)
.with<game::Controlable>(game::User::UserId::User1)
.with<game::gui::Widget>(0, game::gui::Widget::NullTag, 1)
.build();

auto widget2 = world.addEntity()
.with<game::Position>(0.f, 0.f)
.with<game::Textual>("Hello Widgets", 40, raylib::core::Color::RED)
.with<game::Controlable>(game::User::UserId::User1)
.with<game::gui::Widget>(1, 0)
.with<game::gui::Widget>(0, game::gui::Widget::NullTag, 1, true)
.build();

auto text = world.addEntity()
.with<game::Position>(0.f, 0.f)
.with<game::Textual>("Hello ECS", 40, raylib::core::Color::RED)
.with<game::Controlable>(game::User::UserId::User1,
[](const game::Users::ActionEvent &event) {
[](ecs::Entity self, ecs::SystemData data, const game::Users::ActionEvent &event) {
(void)self;
(void)data;
(void)event;
Logger::logger.log(Logger::Severity::Debug, [&](std::ostream &writer) {
writer << "Text control! " << event.value << ", " << static_cast<size_t>(event.action);
Expand All @@ -134,6 +129,13 @@ int main()
})
.build();

auto widget2 = world.addEntity()
.with<game::Position>(0.f, 0.f)
.with<game::Textual>("Hello Widgets", 40, raylib::core::Color::RED)
.with<game::Controlable>(game::User::UserId::User1)
.with<game::gui::Widget>(1, 0)
.build();

#if defined(PLATFORM_WEB)
// We cannot use the WindowShouldClose() loop on the web,
// since there is no such thing as a window.
Expand Down

0 comments on commit 5772347

Please sign in to comment.