diff --git a/assets/buttons/button_minus_hovered.png b/assets/buttons/button_minus_hovered.png new file mode 100644 index 00000000..f07e6896 Binary files /dev/null and b/assets/buttons/button_minus_hovered.png differ diff --git a/assets/buttons/button_plus_hovered.png b/assets/buttons/button_plus_hovered.png new file mode 100644 index 00000000..d4ef106e Binary files /dev/null and b/assets/buttons/button_plus_hovered.png differ diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index 35fa2c53..3c91d142 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -76,4 +76,9 @@ namespace WAL { this->_scene._componentRemoved(*this, type); } + + bool Entity::operator==(const Entity &other) const + { + return other.getUid() == this->_uid; + } } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Entity/Entity.hpp b/lib/wal/sources/Entity/Entity.hpp index 25234f31..648b7d21 100644 --- a/lib/wal/sources/Entity/Entity.hpp +++ b/lib/wal/sources/Entity/Entity.hpp @@ -165,5 +165,8 @@ namespace WAL ~Entity() = default; //! @brief An entity is not assignable Entity &operator=(const Entity &) = delete; + + //! @return true if the two entities hold the same uid + bool operator==(const Entity &) const; }; } // namespace WAL \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index c1d60ba5..43d0ca24 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -182,15 +182,6 @@ namespace BBM { auto scene = std::make_shared(); - scene->addEntity("Control entity") - .addComponent() - .addComponent(); - scene->addEntity("background") - .addComponent() - .addComponent("assets/plain_menu_background.png"); - scene->addEntity("logo") - .addComponent(1920 / 3, 180, 0) - .addComponent("assets/logo_small.png"); WAL::Entity music(*scene, "music text"); music.addComponent(1920 / 2.5, 1080 - 540, 0) @@ -205,6 +196,40 @@ namespace BBM entity.getComponent().drawable->setColor(ORANGE); }); + WAL::Entity musicUp(*scene, "music up button"); + musicUp.addComponent(1920 / 3, 1080 - 540, 0) + .addComponent("assets/buttons/button_plus.png") + .addComponent() + .addComponent([](WAL::Entity &entity) + { + RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); + + texture->use("assets/buttons/button_plus.png"); + }) + .addComponent([](WAL::Entity &entity) + { + RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); + + texture->use("assets/buttons/button_plus_hovered.png"); + }); + + WAL::Entity musicDown(*scene, "music down button"); + musicDown.addComponent(1920 / 1.5, 1080 - 540, 0) + .addComponent("assets/buttons/button_minus.png") + .addComponent() + .addComponent([](WAL::Entity &entity) + { + RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); + + texture->use("assets/buttons/button_minus.png"); + }) + .addComponent([](WAL::Entity &entity) + { + RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); + + texture->use("assets/buttons/button_minus_hovered.png"); + }); + WAL::Entity sound(*scene, "sound text"); sound.addComponent(1920 / 2.5, 1080 - 360, 0) .addComponent("Sound Volume", 70, RAY::Vector2(), ORANGE) @@ -244,9 +269,30 @@ namespace BBM // back button asset //music //sound + + music.getComponent().setButtonLinks(&debug, &sound, &musicUp, &musicDown); + musicUp.getComponent().setButtonLinks(&debug, &sound, nullptr, &music); + musicDown.getComponent().setButtonLinks(&debug, &sound, &music, nullptr); + debug.getComponent().setButtonLinks(&sound, &music); + sound.getComponent().setButtonLinks(&music, &debug); + std::cout << music.getName() << std::endl; + std::cout << music.getUid() << std::endl; + printf("%p\n", &music); + scene->getEntities().push_back(music); + scene->getEntities().push_back(musicUp); + scene->getEntities().push_back(musicDown); scene->getEntities().push_back(sound); scene->getEntities().push_back(debug); + scene->addEntity("Control entity") + .addComponent() + .addComponent(); + scene->addEntity("background") + .addComponent() + .addComponent("assets/plain_menu_background.png"); + scene->addEntity("logo") + .addComponent(1920 / 3, 180, 0) + .addComponent("assets/logo_small.png"); return scene; } diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index 9b6331a1..d1e5d548 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -12,17 +12,20 @@ namespace BBM { MenuControllableSystem::MenuControllableSystem(WAL::Wal &wal) - : System(wal), wal(wal) + : System(wal), wal(wal), currentButton() {} - void MenuControllableSystem::updateButtonIndex(int length) + void MenuControllableSystem::updateCurrentButton() { - _buttonIndex -= (move.y > 0); - _buttonIndex += (move.y < 0); - if (_buttonIndex < 0) - _buttonIndex = length - 1; - if (_buttonIndex == length) - _buttonIndex = 0; + auto buttonComponent = this->currentButton->getComponent(); + if (move.y > 0 && buttonComponent._up) + this->currentButton = buttonComponent._up; + if (move.y < 0 && buttonComponent._down) + this->currentButton = buttonComponent._down; + if (move.x > 0 && buttonComponent._right) + this->currentButton = buttonComponent._right; + if (move.x < 0 && buttonComponent._left) + this->currentButton = buttonComponent._left; } @@ -37,19 +40,17 @@ namespace BBM move = controllable.move; select = controllable.bomb; - auto &buttons = wal.scene->view(); - ssize_t index = 0; - //std::sort(buttons.begin(), buttons.end(), - //[](WAL::Entity &first, WAL::Entity &second) { - // auto &posA = first.getComponent(); - // auto &posB = second.getComponent(); -// - // return (posA.position.y < posB.position.y); - //}); - updateButtonIndex(buttons.size()); + auto &buttons = _wal.scene->view(); + if (currentButton == nullptr && buttons.size()) { + currentButton = &static_cast(buttons.front()); + std::cout << currentButton->getName() << std::endl; + std::cout << currentButton->getUid() << std::endl; + printf("%p\n", currentButton); + } + this->updateCurrentButton(); for (auto &button : buttons) { auto &buttonEntity = static_cast(button); - if (index++ == _buttonIndex) { + if (buttonEntity == *currentButton) { buttonEntity.getComponent().onEvent(button); if (select) button.get().onEvent(button); diff --git a/sources/System/MenuControllable/MenuControllableSystem.hpp b/sources/System/MenuControllable/MenuControllableSystem.hpp index 8d9149a6..7e80a773 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.hpp +++ b/sources/System/MenuControllable/MenuControllableSystem.hpp @@ -18,7 +18,7 @@ namespace BBM WAL::Wal &wal; //! @brief index of the current button selected - int _buttonIndex = 0; + WAL::Entity *currentButton; //! @brief move vector Vector2f move; @@ -29,9 +29,8 @@ namespace BBM //! @brief Cancel action bool cancel = false; - //! @brief update button index - //! @param length length of the button set - void updateButtonIndex(int length); + //! @brief update current button reference + void updateCurrentButton(); //! @brief time (in mili second) since last check std::chrono::time_point _now;