diff --git a/Crawler/MainMenuWindow.cpp b/Crawler/MainMenuWindow.cpp index a67e8aad..d74814a8 100644 --- a/Crawler/MainMenuWindow.cpp +++ b/Crawler/MainMenuWindow.cpp @@ -47,8 +47,9 @@ using A=Attribute; void Menu::InitializeMainMenuWindow(){ Menu*mainMenuWindow=CreateMenu(MAIN_MENU,CENTERED,vi2d{96,96}); - mainMenuWindow->ADD("New Game Button",MenuComponent)({{12,4},{72,24}},"New Game",[](MenuFuncData data){ + mainMenuWindow->ADD("New Game Button",MenuComponent)({{12,4},{72,24}},"New Game",[&](MenuFuncData data){ #ifdef __EMSCRIPTEN__ + data.menu.S(A::NEXT_MENU)="New Game"; if(SaveFile::GetUserID().length()==0){ game->TextEntryEnable(true); Menu::OpenMenu(USER_ID); @@ -63,6 +64,7 @@ void Menu::InitializeMainMenuWindow(){ mainMenuWindow->ADD("Load Game Button",MenuComponent)({{12,36},{72,24}},"Load Game",[](MenuFuncData data){ SaveFile::UpdateSaveGameData(); #ifdef __EMSCRIPTEN__ + data.menu.S(A::NEXT_MENU)="Load Game"; if(SaveFile::GetUserID().length()==0){ game->TextEntryEnable(true); Menu::OpenMenu(USER_ID); diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index 6bb8a786..ca723862 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -705,7 +705,7 @@ void Menu::RecalculateComponentCount(){ componentCount=displayComponents.size()+buttons.size(); } -MenuType Menu::GetType(){ +const MenuType Menu::GetType()const{ return type; } @@ -719,4 +719,8 @@ void Menu::AddChapterListener(MenuComponent*component){ ERR("WARNING! Component "<name<<" has already been added to the Chapter listener list! There should not be any duplicates!!") } chapterListeners.push_back(component); -} \ No newline at end of file +} + + +MenuFuncData::MenuFuncData(Menu&menu,Crawler*const game,MenuComponent*const component,ScrollableWindowComponent*const parentComponent) + :menu(menu),game(game),component(component),parentComponent(parentComponent){} \ No newline at end of file diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 0230f695..df2571b7 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -198,7 +198,7 @@ public: static std::vectorunhandledComponents; //This list contains MenuComponents that are created and haven't been assigned via _AddComponent. If we get to the end of menu initialization and there are any components in this vector, we have leaked memory and will report this. static const vf2d CENTERED; static bool IsMenuOpen(); - MenuType GetType(); + const MenuType GetType()const; safemapcomponents; //A friendly way to interrogate any component we are interested in. std::vectordisplayComponents; //Components that are only for displaying purposes. static std::mapmenus; @@ -260,9 +260,10 @@ T*Component(MenuType menu,std::string componentName){ struct MenuFuncData{ Menu&menu; - Crawler*game; - MenuComponent*component; - ScrollableWindowComponent*parentComponent; + Crawler*const game; + MenuComponent*const component; + ScrollableWindowComponent*const parentComponent=nullptr; + MenuFuncData(Menu&menu,Crawler*const game,MenuComponent*const component,ScrollableWindowComponent*const parentComponent=nullptr); }; using MenuFunc=std::function; \ No newline at end of file diff --git a/Crawler/MenuComponent.cpp b/Crawler/MenuComponent.cpp index 52a2bb57..cb81c8a4 100644 --- a/Crawler/MenuComponent.cpp +++ b/Crawler/MenuComponent.cpp @@ -256,4 +256,8 @@ void MenuComponent::SetGrayedOut(bool grayedOut){ void MenuComponent::OnPlayerMoneyUpdate(uint32_t newMoney){} -void MenuComponent::OnChapterUpdate(uint8_t newChapter){} \ No newline at end of file +void MenuComponent::OnChapterUpdate(uint8_t newChapter){} + +void MenuComponent::Click(){ + onClick(MenuFuncData{*Menu::menus[parentMenu],game,this}); +} \ No newline at end of file diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h index f5a347f1..24b03b81 100644 --- a/Crawler/MenuComponent.h +++ b/Crawler/MenuComponent.h @@ -153,4 +153,5 @@ public: void SetGrayedOut(bool grayedOut); virtual void OnPlayerMoneyUpdate(uint32_t newMoney); virtual void OnChapterUpdate(uint8_t newChapter); + virtual void Click(); }; \ No newline at end of file diff --git a/Crawler/SaveFileWindow.cpp b/Crawler/SaveFileWindow.cpp index 8721683a..b4607606 100644 --- a/Crawler/SaveFileWindow.cpp +++ b/Crawler/SaveFileWindow.cpp @@ -43,7 +43,7 @@ All rights reserved. void Menu::InitializeSaveFileWindow(){ Menu*saveFileWindow=CreateMenu(SAVE_FILE_NAME,CENTERED,vi2d{96,96}); - saveFileWindow->ADD("Save File Name Text Entry",TextEntryLabel)({{-8,36},{112,24}},false,16U,2.f,ComponentAttr::FIT_TO_LABEL|ComponentAttr::OUTLINE|ComponentAttr::SHADOW|ComponentAttr::BACKGROUND)END; + saveFileWindow->ADD("Save File Name Text Entry",TextEntryLabel)({{-8,36},{112,24}},TEXTCHANGE_DONOTHING,false,16U,2.f,ComponentAttr::FIT_TO_LABEL|ComponentAttr::OUTLINE|ComponentAttr::SHADOW|ComponentAttr::BACKGROUND)END; saveFileWindow->ADD("Back Button",MenuComponent)({{-8,68},{48,12}},"Back",[](MenuFuncData data){Menu::CloseMenu();game->TextEntryEnable(false);return true;})END; saveFileWindow->ADD("Continue Button",MenuComponent)({{56,68},{48,12}},"Begin",MenuType::CLASS_SELECTION,[](MenuFuncData data){ SaveFile::SetSaveFileName(game->TextEntryGetString()); diff --git a/Crawler/TextEntryLabel.h b/Crawler/TextEntryLabel.h index b081dfc0..3dfe85d3 100644 --- a/Crawler/TextEntryLabel.h +++ b/Crawler/TextEntryLabel.h @@ -39,16 +39,18 @@ All rights reserved. #include "MenuLabel.h" +#define TEXTCHANGE_DONOTHING [](std::string_view updatedLabel){} + class TextEntryLabel:public MenuLabel{ protected: - std::string enteredText; + std::string lastTextEntryStr; float blinkTimer=0; uint8_t charLimit=16; bool censored=false; + std::functiononTextChangeCallbackFunc; public: - inline TextEntryLabel(geom2d::rectrect,const bool censored=false,const uint8_t charLimit=16,float scale=1,ComponentAttr attributes=ComponentAttr::NONE) - :MenuLabel(rect,"",scale,attributes),censored(censored),charLimit(charLimit){} - + inline TextEntryLabel(geom2d::rectrect,std::functiononTextChangeCallbackFunc=TEXTCHANGE_DONOTHING,const bool censored=false,const uint8_t charLimit=16,float scale=1,ComponentAttr attributes=ComponentAttr::NONE) + :MenuLabel(rect,"",scale,attributes),onTextChangeCallbackFunc(onTextChangeCallbackFunc),censored(censored),charLimit(charLimit){} inline virtual void Update(Crawler*game)override{ MenuLabel::Update(game); @@ -58,6 +60,10 @@ public: std::string censoredTextEntry; std::string_view textEntry=game->TextEntryGetString(); + if(textEntry!=lastTextEntryStr){ + lastTextEntryStr=textEntry; + onTextChangeCallbackFunc(lastTextEntryStr); + } censoredTextEntry=std::accumulate(textEntry.begin(),textEntry.end(),""s,[](std::string currentStr,const char&c){return std::move(currentStr)+'*';}); std::string_view finalLabel=censored?censoredTextEntry:textEntry; diff --git a/Crawler/UserIDMenu.cpp b/Crawler/UserIDMenu.cpp index d91efc57..5f916714 100644 --- a/Crawler/UserIDMenu.cpp +++ b/Crawler/UserIDMenu.cpp @@ -39,10 +39,32 @@ All rights reserved. #include "Menu.h" #include "MenuLabel.h" #include "TextEntryLabel.h" +#include "SaveFile.h" + +using A=Attribute; void Menu::InitializeUserIDWindow(){ Menu*userIDWindow=CreateMenu(USER_ID,CENTERED,vi2d{168,120}); userIDWindow->ADD("User ID Creation Explanation",MenuLabel)({{0,-4},{168,92}},"user_id_message"_FS+"\n\n"+"user_id_message2"_FS,1.f,ComponentAttr::SHADOW|ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE)END; - userIDWindow->ADD("User ID Input",TextEntryLabel)({{36,94},{96,12}},true,24U,1.f,ComponentAttr::BACKGROUND|ComponentAttr::FIT_TO_LABEL|ComponentAttr::OUTLINE|ComponentAttr::SHADOW)END; + userIDWindow->ADD("User ID Input",TextEntryLabel)({{36,94},{96,12}},[](std::string_view newLabel){ + Component(USER_ID,"Submit Button")->SetGrayedOut(newLabel.length()==0); + },true,24U,1.f,ComponentAttr::BACKGROUND|ComponentAttr::FIT_TO_LABEL|ComponentAttr::OUTLINE|ComponentAttr::SHADOW)END; + userIDWindow->ADD("Back Button",MenuComponent)({{18,110},{48,12}},"Back",[](MenuFuncData data){ + Menu::CloseMenu(); + return true; + })END; + userIDWindow->ADD("Submit Button",MenuComponent)({{102,110},{48,12}},"Submit",[](MenuFuncData data){ + SaveFile::SetUserID(Component(USER_ID,"User ID Input")->GetLabel()); + if(Menu::menus[MAIN_MENU]->S(A::NEXT_MENU)=="New Game"){ + Component(MAIN_MENU,"New Game Button")->Click(); + }else + if(Menu::menus[MAIN_MENU]->S(A::NEXT_MENU)=="Load Game"){ + Component(MAIN_MENU,"Load Game Button")->Click(); + }else{ + ERR("WARNING! Unknown Next Menu set! Current Value:"<S(A::NEXT_MENU))); + } + return true; + })END + ->SetGrayedOut(true); } \ No newline at end of file