Skip to content

Commit

Permalink
add selecting UIInput by specific ID
Browse files Browse the repository at this point in the history
update selection indices of lobby controls
update (re)selection of buttons when switching layout in Stats and Leaderboard browsers
  • Loading branch information
fallahn committed Jul 27, 2023
1 parent 80ffad2 commit 5cb0c3c
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 30 deletions.
8 changes: 8 additions & 0 deletions crogine/include/crogine/ecs/systems/UISystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,17 @@ namespace cro
\param index The index of the input to select
Note that this may not give expected results if multiple inputs
have been manually assigned the same selection index with setSelectionIndex()
or the index is not in range of the group size - in which case prefer
selectByIndex()
*/
void selectAt(std::size_t index);

/*!
\brief Selects at a specific index if a UIInput component has been provided
with one via setSelectionIndex() - otherwise prefer selectAt()
*/
void selectByIndex(std::size_t index);

private:

std::vector<ButtonCallback> m_buttonCallbacks;
Expand Down
19 changes: 19 additions & 0 deletions crogine/src/ecs/systems/UISystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,25 @@ void UISystem::selectAt(std::size_t index)
}
}

void UISystem::selectByIndex(std::size_t index)
{
const auto& entities = m_groups[m_activeGroup];
auto old = m_selectedIndex;

do
{
m_selectedIndex = (m_selectedIndex + 1) % entities.size();
} while (entities[m_selectedIndex].getComponent<cro::UIInput>().getSelectionIndex() != index && m_selectedIndex != old);

//and do selected callback
if (m_selectedIndex != old
&& entities[m_selectedIndex].getComponent<UIInput>().enabled)
{
unselect(old);
select(m_selectedIndex);
}
}

//private
glm::vec2 UISystem::toWorldCoords(std::int32_t x, std::int32_t y)
{
Expand Down
4 changes: 2 additions & 2 deletions samples/golf/buildnumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef BUILD_NUMBER_H_
#define BUILD_NUMBER_H_

#define BUILDNUMBER 960
#define BUILDNUMBER_STR "960"
#define BUILDNUMBER 970
#define BUILDNUMBER_STR "970"

#endif /* BUILD_NUMBER_H_ */
22 changes: 21 additions & 1 deletion samples/golf/src/golf/LeaderboardState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ void LeaderboardState::buildScene()
e.getComponent<cro::Sprite>().setColour(cro::Colour::Transparent);
});


//tab buttons
entity = m_scene.createEntity();
entity.addComponent<cro::Transform>().setPosition({ 50.f, 45.f, 0.2f });
Expand Down Expand Up @@ -1266,6 +1265,19 @@ void LeaderboardState::refreshDisplay()

void LeaderboardState::updateButtonIndices()
{
const auto selectNext = [&](std::int32_t idx)
{
auto e = m_scene.createEntity();
e.addComponent<cro::Callback>().active = true;
e.getComponent<cro::Callback>().function =
[&, idx](cro::Entity f, float)
{
m_scene.getSystem<cro::UISystem>()->selectByIndex(idx);
f.getComponent<cro::Callback>().active = false;
m_scene.destroyEntity(f);
};
};

switch (m_displayContext.boardIndex)
{
default: break;
Expand All @@ -1287,6 +1299,8 @@ void LeaderboardState::updateButtonIndices()

m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setNextIndex(ButtonID::HIO, ButtonID::Nearest);
m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setPrevIndex(ButtonID::Streak, ButtonID::DateRange);

selectNext(ButtonID::HIO);
break;
case BoardIndex::Hio:
m_buttons[DynamicButton::Course].getComponent<cro::UIInput>().setNextIndex(ButtonID::Rank, ButtonID::Rank);
Expand All @@ -1306,6 +1320,8 @@ void LeaderboardState::updateButtonIndices()

m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setNextIndex(ButtonID::Course, ButtonID::Nearest);
m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setPrevIndex(ButtonID::Streak, ButtonID::DateRange);

selectNext(ButtonID::Rank);
break;
case BoardIndex::Rank:
m_buttons[DynamicButton::Course].getComponent<cro::UIInput>().setNextIndex(ButtonID::HIO, ButtonID::HIO);
Expand All @@ -1325,6 +1341,8 @@ void LeaderboardState::updateButtonIndices()

m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setNextIndex(ButtonID::Course, ButtonID::Nearest);
m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setPrevIndex(ButtonID::Streak, ButtonID::DateRange);

selectNext(ButtonID::Streak);
break;
case BoardIndex::Streak:
m_buttons[DynamicButton::Course].getComponent<cro::UIInput>().setNextIndex(ButtonID::HIO, ButtonID::HIO);
Expand All @@ -1344,6 +1362,8 @@ void LeaderboardState::updateButtonIndices()

m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setNextIndex(ButtonID::Course, ButtonID::Nearest);
m_buttons[DynamicButton::Close].getComponent<cro::UIInput>().setPrevIndex(ButtonID::Rank, ButtonID::DateRange);

selectNext(ButtonID::Course);
break;
}
}
Expand Down
Loading

0 comments on commit 5cb0c3c

Please sign in to comment.