The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'! https://forums.lestoria.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
AdventuresInLestoria/Crawler/Menu.cpp

104 lines
2.6 KiB

#include "Crawler.h"
#include "Menu.h"
bool Menu::MOUSE_NAVIGATION=true;
std::vector<Menu*>Menu::stack;
std::map<MenuType,Menu>Menu::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;type<MenuType::ENUM_END;type=MenuType(int(type+1))){
if(menus.count(type)==0){
std::cout<<"WARNING! Menu Type "<<type<<" does not exist!"<<std::endl;
throw;
}
}
}
void Menu::AddButton(const MenuButton&button){
buttons.push_back(button);
}
void Menu::MenuSelect(Crawler*game){
if(selection==-1)return;
buttons[selection].onClick(*this,game);
if(buttons[selection].menuDest!=MenuType::ENUM_END){
if(stack.size()<32){
stack.push_back(&menus[buttons[selection].menuDest]);//Navigate to the next menu.
}else{
std::cout<<"WARNING! Exceeded menu stack size limit!"<<std::endl;
throw;
}
}
}
void Menu::Update(Crawler*game){
vf2d upperLeftPos=game->GetScreenSize()/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<float>{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<float>{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]));
}