From f56a488a7e21c3746ce0891f2de3bd131fe45108 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Tue, 8 Jun 2021 08:53:13 +0200 Subject: [PATCH] entity from wal now have a comparaison operator using uid --- assets/buttons/button_minus_hovered.png | Bin 0 -> 529 bytes assets/buttons/button_plus_hovered.png | Bin 0 -> 860 bytes lib/wal/sources/Entity/Entity.cpp | 5 ++ lib/wal/sources/Entity/Entity.hpp | 3 + sources/Runner/Runner.cpp | 64 +++++++++++++++--- .../MenuControllableSystem.cpp | 39 +++++------ .../MenuControllableSystem.hpp | 7 +- 7 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 assets/buttons/button_minus_hovered.png create mode 100644 assets/buttons/button_plus_hovered.png diff --git a/assets/buttons/button_minus_hovered.png b/assets/buttons/button_minus_hovered.png new file mode 100644 index 0000000000000000000000000000000000000000..f07e6896f14048f4062f0be42f78fe0cef0ddb50 GIT binary patch literal 529 zcmeAS@N?(olHy`uVBq!ia0vp^DImW5I3ds`J zh?3y^w370~qEv?R@^Zb*yzJuS#DY}4{G#;P?`)(P7#Qbyx;TbZ%z1mm){B`@#Py-H ztiYectQ|9s9O7v5E)W;HrlhpQC@g7ZhGIlYYSfJ-+x=#~I1*&SU9sHaKHI%izB30( z{MNg5A3Rzb^1k-Zq^Yk2f1Oq^U~4qw=y6QoVM-Q|Xg(mpa*S$1CR2~!USFAZIrC|Y zYW8jGmnN%Izk4_Ps88Or*DlmAqNu2P{@TBJH~(Hz@cdL@?zKPsJHx-~KR*|a z|N2=*L4@t&@InFmSGe5h^K$>1v}D`q=O3Tq^yWKv&inmzwwrhEp8oh@SrJp;dL1hJ r2R&?Nq+P6AKKJ~pat%+h+&YGwPb%q)mhvnCMkj-(tDnm{r-UW|&n3PP literal 0 HcmV?d00001 diff --git a/assets/buttons/button_plus_hovered.png b/assets/buttons/button_plus_hovered.png new file mode 100644 index 0000000000000000000000000000000000000000..d4ef106e257fc868f2ac1024aa23f3cdb962f5fb GIT binary patch literal 860 zcmeAS@N?(olHy`uVBq!ia0vp^DImW5I3ds`J zh?3y^w370~qEv?R@^Zb*yzJuS#DY}4{G#;P?`)(P7?@sqx;TbZ%z1lzZ+38?%(0K^ z`c6fGQ&L_o+2;C7OLuOFjEhXnm;D!%n!0y%`gMtj=xk~3nC&6pV$ROW8FA3@sMqb4 zeo9lrxHF5_pWNH&Tquy0e?Is8$>N7Ubz(og{&{fv|)8b60!L z^63-mH8~_%6&EX{h_v)HO$Zb);&O6xJfVSK&}ZLe?woM#oJChNB+ftWmYZ-g;>L-- zD=a_%Ju?-XAKLTW@6KGebrD-OOq5;8$;9yd{5-R&zpt`*7cPy>@eG|1ZN6T5Z(Uv; zd^V_$~;&rKOFj%3?#dM`!NA;KO!Ba*>txZ1@%(|Uc&UtZW zZh>4%>NnwQ`}CA}YFzg1|7iR{@R!}8Usj6}l%1@5%x3OOthv{I`9+Ku=k7Yk&0QQHS?p7+I^Fg7N85{qa>rl(-}Gr- zJM&*>iTN{(=KcL!Ecrg(#&Fim+3^RCT2DTaQhegk=&+K#PX@=7}7*O3XtMqOe!+Ec* zt1cZf`?f^QBeOze>BEbD>l5eBQzR*kAUWj8?Zl1^`|iE8WpS|1ddT1Sa@j4}3r8jZ Pa|VN_tDnm{r-UW|;A(pK literal 0 HcmV?d00001 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;