Keyboard menu button storage can now be differentiated from internal button storage.

This commit is contained in:
2023-10-17 00:31:56 -05:00
parent daf94db505
commit 958a1866c7
6 changed files with 27 additions and 23 deletions
+15 -12
View File
@@ -48,6 +48,9 @@ Menu*Menu::CreateMenu(MenuType type,vf2d pos,vf2d size){
void Menu::AddComponent(std::string key,MenuComponent*button){
if(button->selectable){
buttons[button->rect.pos.y].push_back(button);
if(button->selectableViaKeyboard){
keyboardButtons[button->rect.pos.y].push_back(button);
}
}else{
displayComponents.push_back(button);
}
@@ -273,12 +276,12 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
if(game->GetKey(RIGHT).bPressed){
if(selection==vi2d{-1,-1})return;
MOUSE_NAVIGATION=false;
selection.x=(size_t(selection.x)+1)%buttons[selection.y].size();
selection.x=(size_t(selection.x)+1)%keyboardButtons[selection.y].size();
}
if(game->GetKey(LEFT).bPressed){
if(selection==vi2d{-1,-1})return;
selection.x--;
if(selection.x<0)selection.x+=buttons[selection.y].size();
if(selection.x<0)selection.x+=keyboardButtons[selection.y].size();
}
if(game->GetKey(DOWN).bPressed||game->GetKey(UP).bPressed){
if(game->GetKey(DOWN).bPressed){
@@ -287,14 +290,14 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
bool selectedItem=false;
if(selection==vi2d{-1,-1}){
//Highlight first item.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
break;
}
}else{
for(auto&key:buttons){
for(auto&key:keyboardButtons){
if(found){ //Once we discover the previous element, the next element becomes our next selection.
int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x;
int previousButtonX=keyboardButtons[selection.y][selection.x]->rect.pos.x;
selection.y=key.first;
int index=0;
for(auto&button:key.second){ //Try to match a button in the same column as this button first.
@@ -312,7 +315,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}
if(!selectedItem){ //This means we need to loop around instead and pick the first one.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
break;
}
@@ -324,22 +327,22 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
if(selection==vi2d{-1,-1}){
//Highlight last item.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
}
}else{
int prevInd=-1;
for(auto&key:buttons){
for(auto&key:keyboardButtons){
if(key.first==selection.y){
break;
}
prevInd=key.first;
}
if(prevInd!=-1){
int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x;
int previousButtonX=keyboardButtons[selection.y][selection.x]->rect.pos.x;
selection.y=prevInd;
int index=0;
for(auto&button:buttons[prevInd]){ //Try to match a button in the same column as this button first.
for(auto&button:keyboardButtons[prevInd]){ //Try to match a button in the same column as this button first.
if(previousButtonX==button->rect.pos.x){
selection.x=index;
break;
@@ -348,7 +351,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}else{ //Since we didn't find it, it means we're at the top of the list or the list is empty. Go to the last element and use that one.
int lastInd=-1;
for(auto&key:buttons){
for(auto&key:keyboardButtons){
lastInd=key.first;
}
selection.y=lastInd;
@@ -357,7 +360,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
//In both cases, we should clamp the X index to make sure it's still valid.
if(selection.y!=-1){
selection.x=std::clamp(selection.x,0,int(buttons[selection.y].size())-1);
selection.x=std::clamp(selection.x,0,int(keyboardButtons[selection.y].size())-1);
}else{
selection.x=-1;
}