diff --git a/src/game/components/Controlable.hpp b/src/game/components/Controlable.hpp index b866b359..4cd021e7 100644 --- a/src/game/components/Controlable.hpp +++ b/src/game/components/Controlable.hpp @@ -10,20 +10,28 @@ #include #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; + /// 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; /// 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 diff --git a/src/game/gui/components/CMakeLists.txt b/src/game/gui/components/CMakeLists.txt index 67a4a189..768f07be 100644 --- a/src/game/gui/components/CMakeLists.txt +++ b/src/game/gui/components/CMakeLists.txt @@ -5,7 +5,7 @@ set (SRC ${SRC} ${SRCROOT}/Checkable.hpp ${SRCROOT}/Clickable.hpp - ${SRCROOT}/Selectable.hpp + ${SRCROOT}/Widget.cpp ${SRCROOT}/Widget.hpp PARENT_SCOPE) diff --git a/src/game/gui/components/Selectable.hpp b/src/game/gui/components/Selectable.hpp deleted file mode 100644 index e43cd23d..00000000 --- a/src/game/gui/components/Selectable.hpp +++ /dev/null @@ -1,25 +0,0 @@ -/* -** EPITECH PROJECT, 2022 -** Bomberman -** File description: -** Selectable -*/ - -#ifndef GAME_GUI_SELECTABLE_HPP_ -#define GAME_GUI_SELECTABLE_HPP_ - -#include "ecs/Component.hpp" - -namespace game -{ - namespace gui - { - struct Selectable : public ecs::Component { - bool selected; - - Selectable(bool pselected = false) : selected(pselected) {} - }; - } // namespace gui -} // namespace game - -#endif /* !GAME_GUI_SELECTABLE_HPP_ */ diff --git a/src/game/gui/components/Widget.cpp b/src/game/gui/components/Widget.cpp new file mode 100644 index 00000000..c3b42b30 --- /dev/null +++ b/src/game/gui/components/Widget.cpp @@ -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 \ No newline at end of file diff --git a/src/game/gui/components/Widget.hpp b/src/game/gui/components/Widget.hpp index 6612ddc4..77b1f0bd 100644 --- a/src/game/gui/components/Widget.hpp +++ b/src/game/gui/components/Widget.hpp @@ -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 diff --git a/src/game/systems/InputManager.cpp b/src/game/systems/InputManager.cpp index aa3be955..1def062a 100644 --- a/src/game/systems/InputManager.cpp +++ b/src/game/systems/InputManager.cpp @@ -37,17 +37,18 @@ namespace game { auto optionnalWidgets = ecs::maybe(data.getStorage()); - for (auto [widget, controlable, _] : + for (auto [widget, controlable, entity] : ecs::join(optionnalWidgets, data.getStorage(), data.getResource())) { + /// 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 diff --git a/src/main.cpp b/src/main.cpp index 5504c762..98de3f0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,21 +111,16 @@ int main() .with(0.f, 0.f) .with("Hello ECS", 40, raylib::core::Color::RED) .with(game::User::UserId::User1) - .with(0, game::gui::Widget::NullTag, 1) - .build(); - - auto widget2 = world.addEntity() - .with(0.f, 0.f) - .with("Hello Widgets", 40, raylib::core::Color::RED) - .with(game::User::UserId::User1) - .with(1, 0) + .with(0, game::gui::Widget::NullTag, 1, true) .build(); auto text = world.addEntity() .with(0.f, 0.f) .with("Hello ECS", 40, raylib::core::Color::RED) .with(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(event.action); @@ -134,6 +129,13 @@ int main() }) .build(); + auto widget2 = world.addEntity() + .with(0.f, 0.f) + .with("Hello Widgets", 40, raylib::core::Color::RED) + .with(game::User::UserId::User1) + .with(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.