From e3c779a68417cee6f81b30a30421af2bfca66070 Mon Sep 17 00:00:00 2001 From: fallahn Date: Tue, 8 Oct 2024 11:57:24 +0100 Subject: [PATCH] add navigation icons to options menu --- samples/golf/src/golf/OptionsState.cpp | 158 ++++++++++++++++++++++--- 1 file changed, 143 insertions(+), 15 deletions(-) diff --git a/samples/golf/src/golf/OptionsState.cpp b/samples/golf/src/golf/OptionsState.cpp index 43a1b6498..ffeac5591 100644 --- a/samples/golf/src/golf/OptionsState.cpp +++ b/samples/golf/src/golf/OptionsState.cpp @@ -91,6 +91,15 @@ using namespace cl; namespace { + struct LastInput final + { + enum + { + XBox, PS,Keyboard + }; + }; + std::int32_t lastInput = LastInput::Keyboard; + constexpr float CameraDepth = 3.f; constexpr float BackgroundDepth = -0.5f; @@ -359,6 +368,8 @@ bool OptionsState::handleEvent(const cro::Event& evt) hasPSLayout(controllerID) ? LayoutID::PS : LayoutID::XBox; m_layoutEnt.getComponent().play(i); } + + lastInput = hasPSLayout(controllerID) ? LastInput::PS : LastInput::XBox; m_scene.getActiveCamera().getComponent().active = true; //forces refresh }; @@ -431,6 +442,8 @@ bool OptionsState::handleEvent(const cro::Event& evt) } else if (evt.type == SDL_KEYDOWN) { + lastInput = LastInput::Keyboard; + switch (evt.key.keysym.sym) { default: break; @@ -441,6 +454,21 @@ bool OptionsState::handleEvent(const cro::Event& evt) cro::App::getWindow().setMouseCaptured(true); break; } + + if (!m_updatingKeybind) + { + if (evt.key.keysym.sym == m_sharedData.inputBinding.keys[InputBinding::PrevClub]) + { + m_currentTabFunction = (m_currentTabFunction + (m_tabFunctions.size() - 1)) % m_tabFunctions.size(); + m_tabFunctions[m_currentTabFunction](); + } + else if (evt.key.keysym.sym == m_sharedData.inputBinding.keys[InputBinding::NextClub]) + { + m_currentTabFunction = (m_currentTabFunction + 1) % m_tabFunctions.size(); + m_tabFunctions[m_currentTabFunction](); + } + m_scene.getActiveCamera().getComponent().active = true; //forces refresh + } } else if (evt.type == SDL_CONTROLLERBUTTONUP) { @@ -496,22 +524,12 @@ bool OptionsState::handleEvent(const cro::Event& evt) toggleControllerIcon(cro::GameController::controllerID(evt.caxis.which)); cro::App::getWindow().setMouseCaptured(true); - - if (evt.caxis.axis == cro::GameController::AxisRightY) - { - /*if (evt.caxis.value > 0) - { - scrollMenu(1); - } - else - { - scrollMenu(0); - }*/ - } } } else if (evt.type == SDL_MOUSEBUTTONDOWN) { + lastInput = LastInput::Keyboard; + if (evt.button.button == SDL_BUTTON_LEFT && !m_updatingKeybind) { @@ -551,11 +569,15 @@ bool OptionsState::handleEvent(const cro::Event& evt) } else if (evt.type == SDL_MOUSEMOTION) { + lastInput = LastInput::Keyboard; + updateSlider(); cro::App::getWindow().setMouseCaptured(false); } else if (evt.type == SDL_MOUSEWHEEL) { + lastInput = LastInput::Keyboard; + if (evt.wheel.y > 0) { //up @@ -954,6 +976,103 @@ void OptionsState::buildScene() auto bgEnt = entity; auto bgSize = glm::vec2(bounds.width, bounds.height); + const auto& largeFont = m_sharedData.sharedResources->fonts.get(FontID::UI); + + //icons for paging tabs + static constexpr float IconOffset = 30.f; + entity = m_scene.createEntity(); + entity.addComponent().setPosition({ IconOffset, 338.f, 0.f }); + entity.addComponent(); + entity.addComponent(largeFont).setCharacterSize(UITextSize); + entity.getComponent().setFillColour(TextNormalColour); + entity.getComponent().setString("<" + cro::Keyboard::keyString(m_sharedData.inputBinding.keys[InputBinding::PrevClub])); + entity.getComponent().setAlignment(cro::Text::Alignment::Centre); + entity.addComponent().active = true; + entity.getComponent().function = + [&](cro::Entity e, float) + { + if (lastInput == LastInput::Keyboard) + { + e.getComponent().setScale(glm::vec2(1.f)); + e.getComponent().setString("<" + cro::Keyboard::keyString(m_sharedData.inputBinding.keys[InputBinding::PrevClub])); + } + else + { + e.getComponent().setScale(glm::vec2(0.f)); + } + }; + bgEnt.getComponent().addChild(entity.getComponent()); + + entity = m_scene.createEntity(); + entity.addComponent().setPosition({ bgSize.x - IconOffset, 338.f, 0.f }); + entity.addComponent(); + entity.addComponent(largeFont).setCharacterSize(UITextSize); + entity.getComponent().setFillColour(TextNormalColour); + entity.getComponent().setString(cro::Keyboard::keyString(m_sharedData.inputBinding.keys[InputBinding::NextClub]) + ">"); + entity.getComponent().setAlignment(cro::Text::Alignment::Centre); + entity.addComponent().active = true; + entity.getComponent().function = + [&](cro::Entity e, float) + { + if (lastInput == LastInput::Keyboard) + { + e.getComponent().setScale(glm::vec2(1.f)); + e.getComponent().setString(cro::Keyboard::keyString(m_sharedData.inputBinding.keys[InputBinding::NextClub]) + ">"); + } + else + { + e.getComponent().setScale(glm::vec2(0.f)); + } + }; + bgEnt.getComponent().addChild(entity.getComponent()); + + + entity = m_scene.createEntity(); + entity.addComponent().setPosition({ IconOffset, 338.f, 0.f }); + entity.addComponent(); + entity.addComponent() = spriteSheet.getSprite("lb"); + entity.addComponent().play(1); + bounds = entity.getComponent().getTextureBounds(); + entity.getComponent().setOrigin({ bounds.width / 2.f, bounds.height }); + entity.addComponent().active = true; + entity.getComponent().function = + [](cro::Entity e, float) + { + if (lastInput != LastInput::Keyboard) + { + e.getComponent().setScale(glm::vec2(1.f)); + e.getComponent().play(lastInput); + } + else + { + e.getComponent().setScale(glm::vec2(0.f)); + } + }; + bgEnt.getComponent().addChild(entity.getComponent()); + + entity = m_scene.createEntity(); + entity.addComponent().setPosition({ bgSize.x - IconOffset, 338.f, 0.f }); + entity.addComponent(); + entity.addComponent() = spriteSheet.getSprite("rb"); + entity.addComponent().play(1); + bounds = entity.getComponent().getTextureBounds(); + entity.getComponent().setOrigin({ bounds.width / 2.f, bounds.height }); + entity.addComponent().active = true; + entity.getComponent().function = + [](cro::Entity e, float) + { + if (lastInput != LastInput::Keyboard) + { + e.getComponent().setScale(glm::vec2(1.f)); + e.getComponent().play(lastInput); + } + else + { + e.getComponent().setScale(glm::vec2(0.f)); + } + }; + bgEnt.getComponent().addChild(entity.getComponent()); + auto& uiSystem = *m_scene.getSystem(); auto selectedID = uiSystem.addCallback([](cro::Entity e) {e.getComponent().setColour(cro::Colour::White); e.getComponent().play(); }); @@ -967,6 +1086,7 @@ void OptionsState::buildScene() spriteSheet.loadFromFile("assets/golf/sprites/options.spt", m_sharedData.sharedResources->textures); + //video options entity = m_scene.createEntity(); entity.addComponent().setPosition(PanelPosition); @@ -1124,11 +1244,19 @@ void OptionsState::buildScene() { auto ent = m_scene.createEntity(); ent.addComponent().active = true; + ent.getComponent().setUserData(0.1f); ent.getComponent().function = - [&](cro::Entity e, float) + [&](cro::Entity e, float dt) { m_scene.getActiveCamera().getComponent().active = true; - m_scene.destroyEntity(e); + + //sometimes 1 frame isn't enough... + auto& t = e.getComponent().getUserData(); + t -= dt; + if (t < 0) + { + m_scene.destroyEntity(e); + } }; }; @@ -1343,7 +1471,7 @@ void OptionsState::buildScene() //tool tips for options - auto& font = m_sharedData.sharedResources->fonts.get(FontID::Info); + const auto& font = m_sharedData.sharedResources->fonts.get(FontID::Info); auto createToolTip = [&](const cro::String& tip) { auto entity = m_scene.createEntity();