Use structured bindings for map k,v pairs. Place counters for for range iterator loops locally inside their scopes.

This commit is contained in:
2023-12-05 22:12:43 -06:00
parent 6d4f024d3c
commit 30287c91d2
7 changed files with 93 additions and 98 deletions
+38 -38
View File
@@ -79,8 +79,8 @@ Menu::Menu(vf2d pos,vf2d size)
}
Menu::~Menu(){
for(auto&key:components){
delete key.second;
for(auto&[key,value]:components){
delete value;
}
}
@@ -105,8 +105,8 @@ void Menu::InitializeMenus(){
//Lock up everything once it's done.
menus[type]->buttons.SetInitialized();
menus[type]->keyboardButtons.SetInitialized();
for(auto&key:menus[type]->components){
MenuComponent*component=key.second;
for(auto&[key,value]:menus[type]->components){
MenuComponent*component=value;
component->AfterCreate();
}
}
@@ -218,8 +218,8 @@ void Menu::Update(Crawler*game){
SetMouseNavigation(true);
}
for(auto&key:buttons){
for(auto&button:key.second){
for(auto&[key,value]:buttons){
for(auto&button:value){
if(!button->disabled){
button->hovered=false;
}
@@ -235,14 +235,14 @@ void Menu::Update(Crawler*game){
}
}else{
selection={-1,-1};
for(auto&key:buttons){
for(auto&[key,value]:buttons){
int index=0;
for(auto&button:key.second){
for(auto&button:value){
if(!button->disabled){
if(button->GetHoverState(game)){
button->hovered=true;
itemHovered=true;
selection.y=key.first;
selection.y=key;
selection.x=index;
}
}
@@ -291,8 +291,8 @@ void Menu::Update(Crawler*game){
}
KeyboardButtonNavigation(game,pos);
for(auto&key:buttons){
for(auto&button:key.second){
for(auto&[key,value]:buttons){
for(auto&button:value){
if(button->renderInMain){
button->_Update(game);
}
@@ -321,8 +321,8 @@ void Menu::Draw(Crawler*game){
component->_Draw(game);
}
}
for(auto&key:buttons){
for(auto&button:key.second){
for(auto&[key,value]:buttons){
for(auto&button:value){
if(button->renderInMain){
button->_Draw(game);
}
@@ -337,8 +337,8 @@ void Menu::Draw(Crawler*game){
component->_DrawDecal(game,this==Menu::stack.back());
}
}
for(auto&key:buttons){
for(auto&button:key.second){
for(auto&[key,value]:buttons){
for(auto&button:value){
if(button->renderInMain){
button->_DrawDecal(game,this==Menu::stack.back());
}
@@ -409,17 +409,17 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
bool selectedItem=false;
if(selection==vi2d{-1,-1}){
//Highlight first item.
for(auto&key:keyboardButtons){
selection.y=key.first;
for(auto&[key,value]:keyboardButtons){
selection.y=key;
break;
}
}else{
for(auto&key:keyboardButtons){
for(auto&[key,value]:keyboardButtons){
if(found){ //Once we discover the previous element, the next element becomes our next selection.
int previousButtonX=int(keyboardButtons[selection.y][selection.x]->rect.pos.x);
selection.y=key.first;
selection.y=key;
int index=0;
for(auto&button:key.second){ //Try to match a button in the same column as this button first.
for(auto&button:value){ //Try to match a button in the same column as this button first.
if(previousButtonX==button->rect.pos.x){
selection.x=index;
break;
@@ -429,15 +429,15 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
selectedItem=true;
break;
}
if(key.first==selection.y
if(key==selection.y
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
&&selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
found=true;
}
}
if(!selectedItem){ //This means we need to loop around instead and pick the first one.
for(auto&key:keyboardButtons){
selection.y=key.first;
for(auto&[key,value]:keyboardButtons){
selection.y=key;
break;
}
}
@@ -447,18 +447,18 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
SetMouseNavigation(false);
if(selection==vi2d{-1,-1}){
//Highlight last item.
for(auto&key:keyboardButtons){
selection.y=key.first;
for(auto&[key,value]:keyboardButtons){
selection.y=key;
}
}else{
int prevInd=-1;
for(auto&key:keyboardButtons){
if(key.first==selection.y&&
for(auto&[key,value]:keyboardButtons){
if(key==selection.y&&
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
break;
}
prevInd=key.first;
prevInd=key;
}
if(prevInd!=-1){
int previousButtonX=int(keyboardButtons[selection.y][selection.x]->rect.pos.x);
@@ -473,8 +473,8 @@ 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:keyboardButtons){
lastInd=key.first;
for(auto&[key,value]:keyboardButtons){
lastInd=key;
}
selection.y=lastInd;
}
@@ -495,9 +495,9 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
if(selection==vi2d{-1,-1}&&buttons.size()>0){
//Find the first possible button entry in the map...
int firstInd=-1;
for(auto&key:buttons){
if(buttons[key.first].size()>0){
firstInd=key.first;
for(auto&[key,value]:buttons){
if(buttons[key].size()>0){
firstInd=key;
break;
}
}
@@ -507,12 +507,12 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}else{//Mouse click.
selection={-1,-1};
for(auto&key:buttons){
for(auto&[key,value]:buttons){
int index=0;
for(auto&button:key.second){
for(auto&button:value){
if(!button->disabled){
if(geom2d::overlaps(geom2d::rect<float>{button->rect.pos+menuPos,button->rect.size},game->GetMousePos())){
selection={index,key.first};
selection={index,key};
break;
}
}
@@ -692,8 +692,8 @@ bool Menu::IsMenuOpen(){
}
void Menu::CleanupAllMenus(){
for(auto&key:Menu::menus){
Menu*menu=key.second;
for(auto&[key,value]:Menu::menus){
Menu*menu=value;
for(auto&componentKey:menu->components){
MenuComponent*component=componentKey.second;
component->Cleanup();