Switched menu button system to component inheritance system.

This commit is contained in:
2023-10-01 01:48:27 -05:00
parent 2914aa1972
commit 3723835cc7
15 changed files with 127 additions and 79 deletions
+16 -16
View File
@@ -23,16 +23,16 @@ void Menu::InitializeMenus(){
}
}
void Menu::AddButton(const MenuButton&button){
buttons[button.rect.pos.y].push_back(button);
void Menu::AddComponent(MenuComponent*button){
buttons[button->rect.pos.y].push_back(button);
}
void Menu::MenuSelect(Crawler*game){
if(selection==vi2d{-1,-1})return;
buttons[selection.y][selection.x].onClick(*this,game);
if(buttons[selection.y][selection.x].menuDest!=MenuType::ENUM_END){
buttons[selection.y][selection.x]->onClick(*this,game);
if(buttons[selection.y][selection.x]->menuDest!=MenuType::ENUM_END){
if(stack.size()<32){
stack.push_back(&menus[buttons[selection.y][selection.x].menuDest]);//Navigate to the next menu.
stack.push_back(&menus[buttons[selection.y][selection.x]->menuDest]);//Navigate to the next menu.
}else{
std::cout<<"WARNING! Exceeded menu stack size limit!"<<std::endl;
throw;
@@ -45,16 +45,16 @@ void Menu::Update(Crawler*game){
for(auto&key:buttons){
for(auto&button:key.second){
button.hovered=false;
button->hovered=false;
}
}
if(!MOUSE_NAVIGATION){
if(selection!=vi2d{-1,-1})buttons[selection.y][selection.x].hovered=true;
if(selection!=vi2d{-1,-1})buttons[selection.y][selection.x]->hovered=true;
}else{
for(auto&key:buttons){
for(auto&button:key.second){
if(geom2d::overlaps(geom2d::rect<float>{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){
button.hovered=true;
if(geom2d::overlaps(geom2d::rect<float>{button->rect.pos+upperLeftPos,button->rect.size},game->GetMousePos())){
button->hovered=true;
}
}
}
@@ -84,7 +84,7 @@ void Menu::Update(Crawler*game){
for(auto&key:buttons){
int index=0;
for(auto&button:key.second){
if(geom2d::overlaps(geom2d::rect<float>{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){
if(geom2d::overlaps(geom2d::rect<float>{button->rect.pos+upperLeftPos,button->rect.size},game->GetMousePos())){
selection={index,key.first};
break;
}
@@ -96,7 +96,7 @@ void Menu::Update(Crawler*game){
}
for(auto&key:buttons){
for(auto&button:key.second){
button.Update(game);
button->Update(game);
}
}
};
@@ -106,7 +106,7 @@ void Menu::Draw(Crawler*game){
game->FillRectDecal(upperLeftPos,size,VERY_DARK_BLUE);
for(auto&key:buttons){
for(auto&button:key.second){
button.Draw(game,upperLeftPos);
button->Draw(game,upperLeftPos);
}
}
};
@@ -141,11 +141,11 @@ void Menu::KeyboardButtonNavigation(Crawler*game){
}else{
for(auto&key:buttons){
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=buttons[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.
if(previousButtonX==button.rect.pos.x){
if(previousButtonX==button->rect.pos.x){
selection.x=index;
break;
}
@@ -183,11 +183,11 @@ void Menu::KeyboardButtonNavigation(Crawler*game){
prevInd=key.first;
}
if(prevInd!=-1){
int previousButtonX=buttons[selection.y][selection.x].rect.pos.x;
int previousButtonX=buttons[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.
if(previousButtonX==button.rect.pos.x){
if(previousButtonX==button->rect.pos.x){
selection.x=index;
break;
}