#include "Crawler.h" #include "Menu.h" bool Menu::MOUSE_NAVIGATION=true; std::vectorMenu::stack; std::mapMenu::menus; Menu::Menu(){} Menu::Menu(vf2d size) :size(size){} void Menu::InitializeMenus(){ stack.reserve(32); menus[TEST]=InitializeTestMenu(); menus[TEST_2]=InitializeTestSubMenu(); for(MenuType type=TEST;typeGetScreenSize()/2-size/2; for(MenuButton&button:buttons){ button.hovered=false; } if(!MOUSE_NAVIGATION){ if(selection!=-1)buttons[selection].hovered=true; }else{ for(MenuButton&button:buttons){ if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ button.hovered=true; } } } if(game->GetKey(RIGHT).bPressed||game->GetKey(DOWN).bPressed){ MOUSE_NAVIGATION=false; selection=(selection+1)%buttons.size(); } if(game->GetKey(LEFT).bPressed||game->GetKey(UP).bPressed){ MOUSE_NAVIGATION=false; selection--; if(selection<0)selection+=buttons.size(); } if(game->GetMouse(0).bPressed||game->GetKey(ENTER).bPressed||game->GetKey(SPACE).bPressed){ MOUSE_NAVIGATION=game->GetMouse(0).bPressed; //If a click occurs we use mouse controls. if(!MOUSE_NAVIGATION){ MenuSelect(game); //Key presses automatically highlight the first button if it's not highlighted. if(selection==-1&&buttons.size()>0){ selection=0; } }else{//Mouse click. selection=-1; int index=0; for(MenuButton&button:buttons){ if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ selection=index; break; } index++; } MenuSelect(game); } } for(MenuButton&button:buttons){ button.Update(game); } }; void Menu::Draw(Crawler*game){ vf2d upperLeftPos=game->GetScreenSize()/2-size/2; game->FillRectDecal(upperLeftPos,size,VERY_DARK_BLUE); for(MenuButton&button:buttons){ button.Draw(game,upperLeftPos); } }; void Menu::OpenMenu(MenuType menu){ stack.clear(); stack.push_back(&(menus[menu])); }