Add default button member to menus.

pull/35/head
sigonasr2 11 months ago
parent bc1a1c6876
commit 97ce43ed21
  1. 10
      Adventures in Lestoria/MainMenuWindow.cpp
  2. 26
      Adventures in Lestoria/Menu.cpp
  3. 3
      Adventures in Lestoria/Menu.h
  4. 2
      Adventures in Lestoria/Version.h

@ -47,7 +47,7 @@ using A=Attribute;
void Menu::InitializeMainMenuWindow(){ void Menu::InitializeMainMenuWindow(){
Menu*mainMenuWindow=CreateMenu(MAIN_MENU,vi2d{132,120},vi2d{96,96}); Menu*mainMenuWindow=CreateMenu(MAIN_MENU,vi2d{132,120},vi2d{96,96});
mainMenuWindow->ADD("New Game Button",MenuComponent)(geom2d::rect<float>{{12,4},{72,24}},"New Game",[&](MenuFuncData data){ auto newGameButton=mainMenuWindow->ADD("New Game Button",MenuComponent)(geom2d::rect<float>{{12,4},{72,24}},"New Game",[&](MenuFuncData data){
game->TextEntryEnable(true); game->TextEntryEnable(true);
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
data.menu.S(A::NEXT_MENU)="New Game"; data.menu.S(A::NEXT_MENU)="New Game";
@ -61,7 +61,7 @@ void Menu::InitializeMainMenuWindow(){
#endif #endif
return true; return true;
})END; })END;
mainMenuWindow->ADD("Load Game Button",MenuComponent)(geom2d::rect<float>{{12,36},{72,24}},"Load Game",[](MenuFuncData data){ auto loadGameButton=mainMenuWindow->ADD("Load Game Button",MenuComponent)(geom2d::rect<float>{{12,36},{72,24}},"Load Game",[](MenuFuncData data){
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
data.menu.S(A::NEXT_MENU)="Load Game"; data.menu.S(A::NEXT_MENU)="Load Game";
if(SaveFile::GetUserID().length()==0){ if(SaveFile::GetUserID().length()==0){
@ -81,4 +81,10 @@ void Menu::InitializeMainMenuWindow(){
game->EndGame(); game->EndGame();
return true; return true;
})END; })END;
if(SaveFile::GetSaveFileCount()>0){
mainMenuWindow->SetDefaultButton(loadGameButton);
}else{
mainMenuWindow->SetDefaultButton(newGameButton);
}
} }

@ -112,6 +112,23 @@ void Menu::InitializeMenus(){
if(menus.count(type)==0){ if(menus.count(type)==0){
ERR("WARNING! Menu Type "<<type<<" does not exist!") ERR("WARNING! Menu Type "<<type<<" does not exist!")
} }
if(menus[type]->defaultButton.expired()){
bool foundComponent=false;
if(menus[type]->componentCount>0){
for(auto&[key,component]:menus[type]->components){
if(component->selectable){
menus[type]->defaultButton=component;
std::cout<<std::format("WARNING! Menu {} does not have a default button! Automatically choosing {}",int(type),component->GetName())<<std::endl;
foundComponent=true;
break;
}
}
}
if(!foundComponent){
std::cout<<std::format("WARNING! Menu {} does not have a default button and has no default components to set it automatically!",int(type))<<std::endl;
}
}
for(auto&[key,value]:menus[type]->components){ for(auto&[key,value]:menus[type]->components){
value->AfterCreate(); value->AfterCreate();
} }
@ -166,7 +183,7 @@ void Menu::Update(AiL*game){
HoverMenuSelect(game); HoverMenuSelect(game);
} }
if(!UsingMouseNavigation()&&geom2d::line<float>(lastActiveMousePos,game->GetMousePos()).length()>="ThemeGlobal.MouseActivationDistance"_F||UsingMouseNavigation()){ if(!UsingMouseNavigation()&&geom2d::line<float>(lastActiveMousePos,game->GetMousePos()).length()>="ThemeGlobal.MouseActivationDistance"_F){
SetMouseNavigation(true); SetMouseNavigation(true);
} }
@ -186,7 +203,6 @@ void Menu::Update(AiL*game){
} }
} }
}else{ }else{
selection={};
for(auto&[key,component]:components){ for(auto&[key,component]:components){
if(component->selectable){ if(component->selectable){
if(!component->disabled&&!component->grayedOut){ if(!component->disabled&&!component->grayedOut){
@ -294,9 +310,14 @@ void Menu::Draw(AiL*game){
void Menu::OpenMenu(MenuType menu,bool cover){ void Menu::OpenMenu(MenuType menu,bool cover){
menus[menu]->cover=cover; menus[menu]->cover=cover;
menus[menu]->selection=menus[menu]->defaultButton;
stack.push_back(menus[menu]); stack.push_back(menus[menu]);
} }
void Menu::SetDefaultButton(std::weak_ptr<MenuComponent>button){
defaultButton=button;
}
void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){
std::weak_ptr<MenuComponent>prevSelection=selection; std::weak_ptr<MenuComponent>prevSelection=selection;
@ -420,7 +441,6 @@ bool Menu::UsingMouseNavigation(){
void Menu::SetMouseNavigation(bool mouseNavigation){ void Menu::SetMouseNavigation(bool mouseNavigation){
if(MOUSE_NAVIGATION&&!mouseNavigation){ if(MOUSE_NAVIGATION&&!mouseNavigation){
//When mouse navigation was enabled and now needs to be disabled, we store the mouse position. //When mouse navigation was enabled and now needs to be disabled, we store the mouse position.
INCLUDE_game
lastActiveMousePos=game->GetMousePos(); lastActiveMousePos=game->GetMousePos();
} }
MOUSE_NAVIGATION=mouseNavigation; MOUSE_NAVIGATION=mouseNavigation;

@ -186,8 +186,11 @@ public:
//X (0-2), Y (0-2) for specific 9-patch tile (tiled version). //X (0-2), Y (0-2) for specific 9-patch tile (tiled version).
static Renderable&GetPatchPart(int x,int y); static Renderable&GetPatchPart(int x,int y);
void RecalculateComponentCount(); void RecalculateComponentCount();
void SetDefaultButton(std::weak_ptr<MenuComponent>button);
private: private:
Menu(vf2d pos,vf2d size); Menu(vf2d pos,vf2d size);
std::weak_ptr<MenuComponent>defaultButton;
static MenuType lastMenuTypeCreated; static MenuType lastMenuTypeCreated;
static std::string lastRegisteredComponent; static std::string lastRegisteredComponent;
void HoverMenuSelect(AiL*game); void HoverMenuSelect(AiL*game);

@ -39,7 +39,7 @@ All rights reserved.
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 1 #define VERSION_PATCH 1
#define VERSION_BUILD 5950 #define VERSION_BUILD 5958
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a

Loading…
Cancel
Save