smarter event application

This commit is contained in:
arthur.jamet
2021-06-08 17:36:29 +02:00
parent 3e8acb5531
commit 3a40f0aba8
3 changed files with 27 additions and 23 deletions
+4 -4
View File
@@ -107,7 +107,7 @@ namespace BBM
.addComponent<Drawable2DComponent, RAY::Texture>("assets/logo_big.png");
scene->addEntity("text_prompt")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 130, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Press space", 70, RAY::Vector2(), ORANGE)
.addComponent<Drawable2DComponent, RAY2D::Text>("Press space", 70, RAY::Vector2(), BLACK)
.addComponent<OnIdleComponent>()
.addComponent<OnHoverComponent>()
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &)
@@ -296,7 +296,7 @@ namespace BBM
.addComponent<Drawable2DComponent, RAY::Texture>("assets/logo_small.png");
auto &music = scene->addEntity("music text")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 540, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Music Volume", 70, RAY::Vector2(), ORANGE)
.addComponent<Drawable2DComponent, RAY2D::Text>("Music Volume", 70, RAY::Vector2(), BLACK)
.addComponent<OnClickComponent>()
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &)
{
@@ -343,7 +343,7 @@ namespace BBM
auto &sound = scene->addEntity("sound text")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 360, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Sound Volume", 70, RAY::Vector2(), ORANGE)
.addComponent<Drawable2DComponent, RAY2D::Text>("Sound Volume", 70, RAY::Vector2(), BLACK)
.addComponent<OnClickComponent>()
.addComponent<OnIdleComponent>([](WAL::Entity &entity, WAL::Wal &)
{
@@ -390,7 +390,7 @@ namespace BBM
auto &debug = scene->addEntity("debug text")
.addComponent<PositionComponent>(1920 / 2.5, 1080 - 180, 0)
.addComponent<Drawable2DComponent, RAY2D::Text>("Debug Mode: Off", 70, RAY::Vector2(), ORANGE)
.addComponent<Drawable2DComponent, RAY2D::Text>("Debug Mode: Off", 70, RAY::Vector2(), BLACK)
.addComponent<OnClickComponent>([](WAL::Entity &entity, WAL::Wal &wal)
{
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.getComponent<Drawable2DComponent>().drawable.get());
@@ -15,18 +15,26 @@ namespace BBM
: System(wal), wal(wal), currentButton()
{}
void MenuControllableSystem::updateCurrentButton()
void MenuControllableSystem::updateCurrentButton(bool selected)
{
auto buttonComponent = this->currentButton->getComponent<OnClickComponent>();
WAL::Entity *newButton = nullptr;
if (move.y > 0 && buttonComponent._up)
this->currentButton = buttonComponent._up;
newButton = buttonComponent._up;
if (move.y < 0 && buttonComponent._down)
this->currentButton = buttonComponent._down;
newButton = buttonComponent._down;
if (move.x < 0 && buttonComponent._right)
this->currentButton = buttonComponent._right;
newButton = buttonComponent._right;
if (move.x > 0 && buttonComponent._left)
this->currentButton = buttonComponent._left;
newButton = buttonComponent._left;
if (newButton) {
this->currentButton->getComponent<OnIdleComponent>().onEvent(*this->currentButton, wal);
this->currentButton = newButton;
this->currentButton->getComponent<OnHoverComponent>().onEvent(*this->currentButton, wal);
}
if (selected)
this->currentButton->getComponent<OnClickComponent>().onEvent(*this->currentButton, wal);
}
void MenuControllableSystem::onFixedUpdate(WAL::ViewEntity<ControllableComponent> &entity)
@@ -41,22 +49,17 @@ namespace BBM
move = controllable.move;
select = controllable.jump;
if (currentButton && currentButton->_scene.getID() != wal.scene->getID())
if (currentButton && currentButton->_scene.getID() != wal.scene->getID()) {
currentButton->getComponent<OnIdleComponent>().onEvent(*this->currentButton, wal);
currentButton = nullptr;
if (currentButton == nullptr && buttons.size())
}
if (currentButton == nullptr && buttons.size()) {
currentButton = &(**buttons.begin());
currentButton->getComponent<OnHoverComponent>().onEvent(*this->currentButton, wal);
}
if (!currentButton)
return;
this->updateCurrentButton();
for (auto &[buttonEntity, clickComponent, hoverComponent, idleComponent]: buttons) {
if (buttonEntity == *currentButton) {
hoverComponent.onEvent(buttonEntity, wal);
if (select)
clickComponent.onEvent(buttonEntity, wal);
continue;
}
idleComponent.onEvent(buttonEntity, wal);
}
this->updateCurrentButton(select);
}
void MenuControllableSystem::onSelfUpdate(void)
@@ -30,7 +30,8 @@ namespace BBM
bool cancel = false;
//! @brief update current button reference
void updateCurrentButton();
//! @param selected lets know if te new selected button is 'pressed'
void updateCurrentButton(bool selected);
//! @brief time (in mili second) since last check
std::chrono::time_point<std::chrono::steady_clock> _now;