Skip to content

Commit

Permalink
Add WidgetTag for widget component
Browse files Browse the repository at this point in the history
Also push buggy code with maybe joins in InputManager.cpp
Linked: #92
  • Loading branch information
AndreasLrx committed Jun 9, 2022
1 parent 3184548 commit b6de59a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/game/gui/components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set (SRC
${SRC}
${SRCROOT}/Checkable.hpp
${SRCROOT}/Clickable.hpp
${SRCROOT}/Selectable.hpp
${SRCROOT}/Widget.hpp
PARENT_SCOPE)

Expand Down
25 changes: 25 additions & 0 deletions src/game/gui/components/Selectable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** 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_ */
15 changes: 12 additions & 3 deletions src/game/gui/components/Widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@

#include "ecs/Component.hpp"
#include "ecs/Entity.hpp"
#include "ecs/World.hpp"
#include "game/components/Controlable.hpp"

namespace game
{
namespace gui
{
struct Widget : public ecs::Component {
ecs::Entity::Index previous;
bool selected;
using WidgetTag = size_t;
static constexpr WidgetTag NullTag = -1;
WidgetTag tag;

Widget(ecs::Entity::Index previousWidget) : previous(previousWidget) { selected = false; }
WidgetTag previous;
WidgetTag next;

Widget(WidgetTag ptag, WidgetTag pprevious = NullTag, WidgetTag pnext = NullTag)
: tag(ptag), previous(pprevious), next(pnext)
{
}
};
} // namespace gui
} // namespace game
Expand Down
18 changes: 15 additions & 3 deletions src/game/systems/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ecs/join.hpp"
#include "ecs/system/SystemData.hpp"
#include "game/Users.hpp"
#include "game/gui/components/Widget.hpp"

#include "logger/Logger.hpp"

Expand All @@ -34,8 +35,19 @@ namespace game

void InputManager::handleEvent(const Users::ActionEvent &event, ecs::SystemData data)
{
for (auto [controlable] : ecs::join(data.getStorage<Controlable>()))
if (controlable.userId == event.user && controlable.callback && controlable.callback(event))
return;
auto optionnalWidgets = ecs::maybe(data.getStorage<gui::Widget>());

for (auto [widget, controlable, _] :
ecs::join(optionnalWidgets, data.getStorage<Controlable>(), data.getResource<ecs::Entities>())) {
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");
}
}
}
} // namespace game
47 changes: 46 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
#include "raylib/model/Model.hpp"
#include "raylib/raylib.hpp"

#include "ecs/Storage.hpp"
#include "game/Users.hpp"
#include "game/components/Controlable.hpp"
#include "game/components/Position.hpp"
#include "game/components/Textual.hpp"
#include "game/systems/DrawText.hpp"
#include "game/systems/InputManager.hpp"

#include "game/gui/components/Widget.hpp"

#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif

constexpr int WIDTH(500);
constexpr int HEIGHT(500);

ecs::World world;

static raylib::model::Model &getTestingModel()
{
static const std::filesystem::path testModelPath =
Expand Down Expand Up @@ -57,14 +69,16 @@ static void drawFrame(void *arg)
testingModel.draw(pos, rotationAxis, -90, scale, raylib::core::Color::RED);
};

world.runSystems();

// DrawText("<insert great game here>", WIDTH / 2 - 120, HEIGHT / 2 - 1, 20, LIGHTGRAY);
raylib::core::Window::drawFPS(10, 10);
}

static void setupLogger()
{
// Setup the logger parameters
Logger::logger.setLogLevel(Logger::Severity::Information);
Logger::logger.setLogLevel(Logger::Severity::Debug);
Logger::logger.setLogInfo(Logger::LogInfo::Time);
Logger::logger.setName("main");
raylib::initLogger(LOG_INFO);
Expand All @@ -89,6 +103,37 @@ int main()
raylib::core::Camera3D camera;
camera.setMode(raylib::core::Camera3D::CameraMode::ORBITAL);

world.addResource<game::Users>();
world.addSystem<game::DrawText>();
world.addSystem<game::InputManager>();

auto widget1 = 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)
.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)
.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) {
(void)event;
Logger::logger.log(Logger::Severity::Debug, [&](std::ostream &writer) {
writer << "Text control! " << event.value << ", " << static_cast<size_t>(event.action);
});
return false;
})
.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 b6de59a

Please sign in to comment.