#pragma region License /* License (OLC-3) ~~~~~~~~~~~~~~~ Copyright 2024 Joshua Sigona Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions or derivations of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions or derivative works in binary form must reproduce the above copyright notice. This list of conditions and the following disclaimer must be reproduced in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Portions of this software are copyright © 2024 The FreeType Project (www.freetype.org). Please see LICENSE_FT.txt for more information. All rights reserved. */ #pragma endregion #include "AdventuresInLestoria.h" #include "MenuComponent.h" #include "DEFINES.h" #include "safemap.h" #include "Item.h" #include "MenuItemButton.h" #include "ScrollableWindowComponent.h" #include "RowInventoryScrollableWindowComponent.h" #include "MenuItemItemButton.h" #include #include "SoundEffect.h" #include "LoadingScreen.h" INCLUDE_ITEM_CATEGORIES INCLUDE_DATA bool Menu::MOUSE_NAVIGATION=true; bool Menu::nextMouseNavigationIsIgnored=false; vi2d Menu::lastActiveMousePos{}; std::vectorMenu::stack; std::mapMenu::menus; std::string Menu::themeSelection="BlueDefault"; safeunorderedmapMenu::themes; safemap>>Menu::inventoryListeners; safemap>>Menu::merchantInventoryListeners; std::vector>Menu::equipStatListeners; std::vector>Menu::chapterListeners; const vf2d Menu::CENTERED = {-456,-456}; MenuType Menu::lastMenuTypeCreated; std::string Menu::lastRegisteredComponent; bool Menu::alreadyClicked=false; bool Menu::scrolling=false; std::weak_ptrMenu::lastHover; INCLUDE_game INCLUDE_GFX INCLUDE_WINDOW_SIZE using A=Attribute; Menu::Menu(vf2d pos,vf2d size) :pos(pos==CENTERED?vi2d{WINDOW_SIZE/2-size/2}:vi2d{pos}),size(size){ this->window=ViewPort::rectViewPort({-24,-24},this->size+vi2d{48,48},this->pos); } void Menu::InitializeMenus(){ std::for_each(menus.begin(),menus.end(),[](std::pairdata){delete data.second;}); //EW RAW POINTER. menus.clear(); for(auto&[key,value]:DATA["ItemCategory"].GetKeys()){ inventoryListeners[key].clear(); merchantInventoryListeners[key].clear(); } equipStatListeners.clear(); chapterListeners.clear(); stack.clear(); stack.reserve(MAX_MENUS); InitializeConsumableInventoryWindow(); InitializeClassSelectionWindow(); InitializeClassInfoWindow(); InitializeMainMenuWindow(); InitializeOverworldMapLevelWindow(); InitializeItemLoadoutWindow(); InitializeLevelCompleteWindow(); InitializeOverworldMenuWindow(); InitializeCharacterMenuWindow(); InitializeInventoryWindow(); InitializeMerchantWindow(); InitializeBuyItemWindow(); InitializeSellItemWindow(); InitializeBlacksmithCraftingWindow(); InitializeCraftItemWindow(); InitializeConsumableCraftingWindow(); InitializeConsumableCraftItemWindow(); InitializeSaveFileWindow(); InitializeLoadGameWindow(); InitializeUserIDWindow(); InitializeSettingsWindow(); InitializeShermanWindow(); InitializeKeyboardInputWindow(); InitializeNewKeybindInputWindow(); InitializePauseWindow(); InitializeDeathWindow(); InitializeCreditsWindow(); InitializeArtificerWindow(); InitializeArtificerRefineWindow(); InitializeArtificerRefineResultWindow(); InitializeArtificerDisassembleWindow(); InitializeArtificerEnchantWindow(); InitializeArtificerEnchantConfirmWindow(); for(MenuType type=MenuType(int(MenuType::ENUM_START)+1);typecomponents){ value->AfterCreate(); } if(menus.size()>MAX_MENUS)ERR("WARNING! Exceeded maximum expected menu count of "<(CREDITS,"Display Text")->SetLabel(credits); } Menu*Menu::CreateMenu(MenuType type,vf2d pos,vf2d size){ menus[type]=NEW Menu(pos,size); menus[type]->type=type; lastMenuTypeCreated=type; return menus.at(type); } void Menu::CheckClickAndPerformMenuSelect(AiL*game){ if(game->KEY_CONFIRM.Released()&&!Menu::alreadyClicked&&(MOUSE_NAVIGATION||(!MOUSE_NAVIGATION&&!game->GetMouse(Mouse::LEFT).bReleased&&!game->GetMouse(Mouse::RIGHT).bReleased&&!game->GetMouse(Mouse::MIDDLE).bReleased))){ if(!GetSelection().expired()){ //If we are on controller/gamepad it's possible we haven't highlighted a button yet, so don't click this button right away. Menu*prevMenu=Menu::IsMenuOpen()?Menu::stack.back():nullptr; MenuSelect(game); Menu*currentMenu=Menu::IsMenuOpen()?Menu::stack.back():nullptr; if(prevMenu!=currentMenu)Menu::alreadyClicked=true; //The logic here is that only when we navigate between menus do we care about setting the already clicked flag to prevent further clicks going into the new screen. If we're still on the same menu, then we will proceed with regular inputs. } } } void Menu::HoverMenuSelect(AiL*game){ if(!game->IsFocused()||selection.expired()||selection.lock()->disable||selection.lock()->disableOutsideWindow||selection.lock()->grayedOut||Menu::alreadyClicked||Menu::scrolling)return; if(selection.lock()->draggable){ if(buttonHoldTime<"ThemeGlobal.MenuHoldTime"_F){ CheckClickAndPerformMenuSelect(game); }else{ draggingComponent=std::move(selection.lock()->PickUpDraggableItem()); buttonHoldTime=0; } }else{ CheckClickAndPerformMenuSelect(game); } } void Menu::MenuSelect(AiL*game){ if(!game->IsFocused()||selection.expired()||selection.lock()->disable||selection.lock()->disableOutsideWindow||selection.lock()->grayedOut)return; selection.lock()->onClick(MenuFuncData{*this,game,selection,dynamic_pointer_cast(selection.lock()->parentComponent.lock())}); } void Menu::Update(AiL*game){ if(!draggingComponent){ HoverMenuSelect(game); } if(!UsingMouseNavigation()&&geom2d::line(lastActiveMousePos,game->GetMousePos()).length()>="ThemeGlobal.MouseActivationDistance"_F){ if(!nextMouseNavigationIsIgnored){ SetMouseNavigation(true); }else{ //Update the last active mouse position as if we were going to change to mouse navigation mode because we want to ignore this one and not trigger it until we move the mouse on purpose. lastActiveMousePos=game->GetMousePos(); nextMouseNavigationIsIgnored=false; } } for(auto&[key,button]:components){ if(button->F(A::RUMBLE_TIMER)>0.f){ button->F(A::RUMBLE_TIMER)=std::max(button->F(A::RUMBLE_TIMER)-game->GetElapsedTime(),0.f); if(button->F(A::RUMBLE_TIMER)==0.f){ Input::StopVibration(); } } if(!button->disable||!button->disableOutsideWindow){ button->hovered=false; } } bool itemHovered=false; if(!UsingMouseNavigation()){ if(!selection.expired()){ selection.lock()->hovered=true; itemHovered=true; } }else{ selection={}; std::vector>allComponents; std::for_each(components.begin(),components.end(),[&](auto&pair){allComponents.push_back(pair.second);}); std::sort(allComponents.begin(),allComponents.end(),[](std::weak_ptrc1,std::weak_ptrc2){return c1.lock()->depthdepth;}); for(const auto&component:allComponents){ if(component.lock()->selectable){ if(!component.lock()->disable&&!component.lock()->disableOutsideWindow&&!component.lock()->grayedOut){ if(component.lock()->GetHoverState(game)){ if(!Menu::lastHover.expired()&&&*component.lock()!=&*Menu::lastHover.lock())SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED); Menu::lastHover=component.lock(); component.lock()->hovered=true; itemHovered=true; SetSelection(std::weak_ptr(component.lock())); break; } } } } } if(itemHovered&&draggingComponent&&!selection.expired()&&!UsingMouseNavigation()&&(game->KEY_CONFIRM.Held())){ buttonHoldTime+=game->GetElapsedTime(); }else{ buttonHoldTime=0; } if(draggingComponent){ auto ClearDraggingComponent=[&](){ draggingComponent={}; }; if(!UsingMouseNavigation()){ if(game->KEY_CONFIRM.Released()){ if(selection.expired()){//Dropping over an empty area. ClearDraggingComponent(); }else if(selection.lock()->DropDraggableItem(std::move(draggingComponent))){ ClearDraggingComponent(); } } }else{ if(game->KEY_CONFIRM.Released()){ if(selection.expired()){//Dropping over an empty area. ClearDraggingComponent(); }else if(selection.lock()->DropDraggableItem(std::move(draggingComponent))){ ClearDraggingComponent(); } } } } KeyboardButtonNavigation(game,pos); for(auto&[key,component]:components){ if(component->renderInMain){ component->_BeforeUpdate(game); } } for(auto&[key,component]:components){ if(component->renderInMain){ component->_Update(game); } } }; void Menu::Draw(AiL*game){ if(GetCurrentTheme().IsScaled()){ DrawScaledWindowBackground(game,pos,size,GetRenderColor()); }else{ DrawTiledWindowBackground(game,pos,size,GetRenderColor()); } if(GetCurrentTheme().IsScaled()){ DrawScaledWindowBorder(game,pos,size,GetRenderColor()); }else{ DrawTiledWindowBorder(game,pos,size,GetRenderColor()); } std::vector>allComponents; std::for_each(components.begin(),components.end(),[&](auto&pair){allComponents.push_back(pair.second);}); std::sort(allComponents.begin(),allComponents.end(),[](std::weak_ptrc1,std::weak_ptrc2){return c1.lock()->depth>c2.lock()->depth;}); for(const auto&component:allComponents){ if(!component.expired()&&component.lock()->renderInMain){ component.lock()->_DrawDecal(window,this==Menu::stack.back()); } } if(draggingComponent){ vf2d offsetPos=draggingComponent->rect.pos; if(!UsingMouseNavigation()){ vf2d drawOffset{}; if(!selection.expired()){ if(!selection.lock()->parentComponent.expired()){ std::weak_ptrscrollableComponent=DYNAMIC_POINTER_CAST(selection.lock()->parentComponent.lock()); drawOffset+=scrollableComponent.lock()->GetScrollAmount(); } draggingComponent->V(A::DRAW_OFFSET)=drawOffset+pos-offsetPos+selection.lock()->rect.pos+vi2d{1,-4}; } draggingComponent->DrawDecal(window,this==Menu::stack.back()); }else{ draggingComponent->V(A::DRAW_OFFSET)-offsetPos+game->GetMousePos(); draggingComponent->DrawDecal(window,this==Menu::stack.back()); } } if(Menu::stack.back()==this){ helpDisplay.Draw(); } }; void Menu::OpenMenu(MenuType menu,bool cover){ Menu::alreadyClicked=true; menus[menu]->cover=cover; if(menus[menu]->onOpenFunc){ Data returnData; menus[menu]->onOpenFunc(menu,returnData); menus[menu]->defaultButton=returnData; if(std::holds_alternative(returnData)&&std::get(returnData).length()>0||std::holds_alternative>(returnData))menus[menu]->SetSelection(returnData,false); } stack.push_back(menus[menu]); Audio::UpdateBGMVolume(); //If we open the pause menu, it forces the bgm volume to be halved. So we notify the engine that this is happening. } void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){ std::weak_ptrprevSelection=selection; if(game->KEY_UP.Released()||game->KEY_RIGHT.Released()||game->KEY_LEFT.Released()||game->KEY_DOWN.Released()|| game->KEY_BACK.Released()||game->KEY_CONFIRM.Released()||game->KEY_START.Released()||game->KEY_SELECT.Released()|| game->KEY_SCROLLDOWN.Released()||game->KEY_SCROLLUP.Released()||game->KEY_SCROLLVERT.Analog()!=0.f||game->KEY_MENU.Released()){ SetMouseNavigation(game->GetMouse(Mouse::LEFT).bReleased||game->GetMouse(Mouse::RIGHT).bReleased||game->GetMouse(Mouse::MIDDLE).bReleased); //If a click occurs we use mouse controls. buttonHoldTime=0; } if(!Menu::UsingMouseNavigation()&&keyboardSelection.expired()){ //Since our keyboard selection expired, we need something selected here since we're in keyboard navigation mode. if(onOpenFunc){ Data returnData; onOpenFunc(GetType(),returnData); defaultButton=returnData; if(std::holds_alternative(returnData)&&std::get(returnData).length()>0||std::holds_alternative>(returnData))SetSelection(returnData,true); } } for(auto&[input,data]:inputGroups){ if(Menu::alreadyClicked)break; bool activated=false; switch(input.GetEngageType()){ case Released:{ activated=input.GetGroup().Released(); }break; case Pressed:{ activated=input.GetGroup().Pressed(); }break; case PressedDAS:{ activated=input.GetGroup().PressedDAS(); }break; case Held:{ activated=input.GetGroup().Held(); }break; case Analog:{ activated=input.GetGroup().Analog()!=0.f; }break; [[unlikely]]default:ERR(std::format("WARNING! Unhandled input engage type {}! THIS SHOULD NOT BE HAPPENING!",int(input.GetEngageType()))); } if(activated){ auto&action=data.second; if(std::holds_alternative(action))Component(type,std::get(action))->Click(); else if(std::holds_alternative>(action))std::get>(action)(type); else{ ERR("WARNING! Navigation data has an unrecognized type or is empty! This should not be happening!") } } } if(!selection.expired()){ const int MAX_ITERATIONS=10; //Skip a maximum amount of items in case everything gets disabled somehow, prevents an infinite loop. int iterationCount=0; do{ if(navigationGroups.size()==0)break; //We don't do anything here when there are no navigation groups. std::string selectionButtonName=selection.lock()->GetName(); if(!selection.lock()->parentComponent.expired()){ //If a component has a parent, then we use the parent as the identifier for what should happen next. selectionButtonName=selection.lock()->parentComponent.lock()->GetName(); if(!navigationGroups.count(selectionButtonName)){ //When there are no navigation groups, we want it to behave as if the other navigation groups defined for each button exists. This is more useful when the scrollable component does not have dynamic items, like the settings window. selectionButtonName=selection.lock()->GetName(); //Default back to our name as the navigation group handler. } } if(navigationGroups.count(selectionButtonName)){ Navigation nav=navigationGroups[selectionButtonName]; const float scrollVertAnalogDAS{game->KEY_SCROLLVERT_L.AnalogDAS(0.5f)}; if(game->KEY_UP.PressedDAS()||scrollVertAnalogDAS<-0.5f){ SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED); SetMouseNavigation(false); if(std::holds_alternative(nav.up)&&std::get(nav.up).length()>0)SetSelection(std::string_view(std::get(nav.up))); else if(std::holds_alternative(nav.up)){ Data returnData; std::get(nav.up)(type,returnData); if(!std::holds_alternative(returnData)&&!std::holds_alternative>(returnData))ERR(std::format("WARNING! No valid return type set for up button navigation in menu {}",int(type))); SetSelection(returnData); } } if(game->KEY_DOWN.PressedDAS()||scrollVertAnalogDAS>0.5f){ SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED); SetMouseNavigation(false); if(std::holds_alternative(nav.down)&&std::get(nav.down).length()>0)SetSelection(std::string_view(std::get(nav.down))); else if(std::holds_alternative(nav.down)){ Data returnData; std::get(nav.down)(type,returnData); if(!std::holds_alternative(returnData)&&!std::holds_alternative>(returnData))ERR(std::format("WARNING! No valid return type set for down button navigation in menu {}",int(type))); SetSelection(returnData); } } if(game->KEY_LEFT.PressedDAS()||game->KEY_SCROLLLEFT.AnalogDAS(0.5f)<-0.5f){ SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED); SetMouseNavigation(false); if(std::holds_alternative(nav.left)&&std::get(nav.left).length()>0)SetSelection(std::string_view(std::get(nav.left))); else if(std::holds_alternative(nav.left)){ Data returnData; std::get(nav.left)(type,returnData); if(!std::holds_alternative(returnData)&&!std::holds_alternative>(returnData))ERR(std::format("WARNING! No valid return type set for left button navigation in menu {}",int(type))); SetSelection(returnData); } } if(game->KEY_RIGHT.PressedDAS()||game->KEY_SCROLLRIGHT.AnalogDAS(0.5f)>0.5f){ SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED); SetMouseNavigation(false); if(std::holds_alternative(nav.right)&&std::get(nav.right).length()>0)SetSelection(std::string_view(std::get(nav.right))); else if(std::holds_alternative(nav.right)){ Data returnData; std::get(nav.right)(type,returnData); if(!std::holds_alternative(returnData)&&!std::holds_alternative>(returnData))ERR(std::format("WARNING! No valid return type set for right button navigation in menu {}",int(type))); SetSelection(returnData); } } }else if(!Menu::UsingMouseNavigation()){ if(onOpenFunc){ Data returnData; onOpenFunc(GetType(),returnData); defaultButton=returnData; if(std::holds_alternative(returnData)&&std::get(returnData).length()>0||std::holds_alternative>(returnData))SetSelection(returnData,true); } }else{ break; //There's no reason to be doing anything here if we navigate with the mouse. } iterationCount++; if(iterationCount==MAX_ITERATIONS){ if(selection.lock()->IsGreyedOut()||selection.lock()->IsDisabled()){ // If the button is greyed out or disabled, this is an invalid selection. We will try to reset to a default, but we need to break out of this loop. if(onOpenFunc){ Data returnData; onOpenFunc(GetType(),returnData); defaultButton=returnData; if(std::holds_alternative(returnData)&&std::get(returnData).length()>0||std::holds_alternative>(returnData))SetSelection(returnData,true); } break; } } }while(iterationCountgrayedOut||selection.lock()->disable)); } if(&*prevSelection.lock()!=&*selection.lock()){ if(!selection.expired()&&selection.lock()->disableOutsideWindow){ bool handled=false; if(!UsingMouseNavigation()){ //Let's transfer some information about our selection being off the screen. Our intention with keyboard controls is that the screen will scroll to the correct location instead. //If we return false, then we handled it ourselves, no need to go back to the previous selection. if(HandleOutsideDisabledButtonSelection(selection)){ handled=true; } } if(!handled){ // If the new selection of a button on this frame is disabled for some reason and we didn't handle it, we need to go back to what we had selected before. SetSelection(prevSelection); } } } } void Menu::DrawScaledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){ vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]}; //Upper-Left game->DrawPartialDecal(menuPos-patchSize,patchSize,GetPatchPart(0,0).Decal(),{patchSize.x*0,patchSize.y*0},patchSize,renderColor); //Upper-Right game->DrawPartialDecal(menuPos+vf2d{size.x,-patchSize.y},patchSize,GetPatchPart(2,0).Decal(),{patchSize.x*2,patchSize.y*0},patchSize,renderColor); //Bottom-Left game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,size.y},patchSize,GetPatchPart(0,2).Decal(),{patchSize.x*0,patchSize.y*2},patchSize,renderColor); //Bottom-Right game->DrawPartialDecal(menuPos+vf2d{size.x,size.y},patchSize,GetPatchPart(2,2).Decal(),{patchSize.x*2,patchSize.y*2},patchSize,renderColor); //Top game->DrawPartialDecal(menuPos+vf2d{0,-patchSize.y},vf2d{size.x,patchSize.y},GetPatchPart(1,0).Decal(),{patchSize.x*1,patchSize.y*0},patchSize,renderColor); //Left game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,0},vf2d{patchSize.x,size.y},GetPatchPart(0,1).Decal(),{patchSize.x*0,patchSize.y*1},patchSize,renderColor); //Right game->DrawPartialDecal(menuPos+vf2d{size.x,0},vf2d{patchSize.x,size.y},GetPatchPart(2,1).Decal(),{patchSize.x*2,patchSize.y*1},patchSize,renderColor); //Bottom game->DrawPartialDecal(menuPos+vf2d{0,size.y},vf2d{size.x,patchSize.y},GetPatchPart(1,2).Decal(),{patchSize.x*1,patchSize.y*2},patchSize,renderColor); } void Menu::DrawTiledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){ vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]}; //Upper-Left game->DrawPartialDecal(menuPos-patchSize,patchSize,GetPatchPart(0,0).Decal(),{0,0},patchSize,renderColor); //Upper-Right game->DrawPartialDecal(menuPos+vf2d{size.x,-patchSize.y},patchSize,GetPatchPart(2,0).Decal(),{0,0},patchSize,renderColor); //Bottom-Left game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,size.y},patchSize,GetPatchPart(0,2).Decal(),{0,0},patchSize,renderColor); //Bottom-Right game->DrawPartialDecal(menuPos+vf2d{size.x,size.y},patchSize,GetPatchPart(2,2).Decal(),{0,0},patchSize,renderColor); //Top game->DrawPartialDecal(menuPos+vf2d{0,-patchSize.y},vf2d{size.x,patchSize.y},GetPatchPart(1,0).Decal(),{0,0},vf2d{size.x,patchSize.y},renderColor); //Left game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,0},vf2d{patchSize.x,size.y},GetPatchPart(0,1).Decal(),{0,0},vf2d{patchSize.x,size.y},renderColor); //Right game->DrawPartialDecal(menuPos+vf2d{size.x,0},vf2d{patchSize.x,size.y},GetPatchPart(2,1).Decal(),{0,0},vf2d{patchSize.x,size.y},renderColor); //Bottom game->DrawPartialDecal(menuPos+vf2d{0,size.y},vf2d{size.x,patchSize.y},GetPatchPart(1,2).Decal(),{0,0},vf2d{size.x,patchSize.y},renderColor); } void Menu::DrawScaledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){ vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]}; //Center if(GetCurrentTheme().HasBackground()){ Decal*back=GetCurrentTheme().GetBackground(); game->DrawPartialDecal(menuPos,size,back,{0,0},back->sprite->Size(),renderColor); }else{ game->DrawPartialDecal(menuPos,size,GetPatchPart(1,1).Decal(),{patchSize.x*1,patchSize.y*1},patchSize,renderColor); } } void Menu::DrawTiledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){ vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]}; //Center if(GetCurrentTheme().HasBackground()){ Decal*back=GetCurrentTheme().GetBackground(); game->DrawPartialDecal(menuPos,size,back,{0,0},size,renderColor); }else{ game->DrawPartialDecal(menuPos,size,GetPatchPart(1,1).Decal(),{0,0},patchSize,renderColor); } } Renderable&Menu::GetPatchPart(int x,int y){ return GFX[themeSelection+"_"+std::to_string(x)+std::to_string(y)+".png"]; } Theme&Menu::GetCurrentTheme(){ return themes[themeSelection]; } Pixel Menu::GetRenderColor(){ bool focused=Menu::stack.back()==this; Pixel col=WHITE; if(!focused){ col=WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F; } return col; } bool Menu::HandleOutsideDisabledButtonSelection(std::weak_ptrdisabledButton){ if(!disabledButton.expired()&&!disabledButton.lock()->parentComponent.expired()){ return disabledButton.lock()->parentComponent.lock()->HandleOutsideDisabledButtonSelection(disabledButton); }else{ return false; } } bool Menu::UsingMouseNavigation(){ return MOUSE_NAVIGATION; }; void Menu::SetMouseNavigation(bool mouseNavigation){ if(MOUSE_NAVIGATION&&!mouseNavigation){ //When mouse navigation was enabled and now needs to be disabled, we store the mouse position. lastActiveMousePos=game->GetMousePos(); if(!keyboardSelection.expired()){ SetSelection(keyboardSelection); } } MOUSE_NAVIGATION=mouseNavigation; }; void Menu::InventorySlotsUpdated(ITCategory cat){ //Update the inventory with a new inventory slot, since there's one additional item to interact with now. for(std::weak_ptrcomponent:inventoryListeners.at(cat)){ std::weak_ptrcomp=DYNAMIC_POINTER_CAST(component.lock()); //HACK ALERT! We're assuming that these must be these classes otherwise they have no reason to even be using these listeners. Make sure that the lowest base class that requires these implements these functions!!! comp.lock()->OnInventorySlotsUpdate(cat); } } void Menu::MerchantInventorySlotsUpdated(ITCategory cat){ //Update the inventory with a new inventory slot, since there's one additional item to interact with now. for(std::weak_ptrcomponent:merchantInventoryListeners.at(cat)){ std::weak_ptrcomp=DYNAMIC_POINTER_CAST(component.lock()); //HACK ALERT! We're assuming that these must be these classes otherwise they have no reason to even be using these listeners. Make sure that the lowest base class that requires these implements these functions!!! comp.lock()->OnInventorySlotsUpdate(cat); } } void Menu::AddInventoryListener(std::weak_ptrcomponent,ITCategory category){ if(inventoryListeners.count(category)){ std::vector>&listenerList=inventoryListeners.at(category); if(std::find_if(listenerList.begin(),listenerList.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=listenerList.end()){ ERR("WARNING! Component "<name<<" has already been added to the "<component,ITCategory category){ if(merchantInventoryListeners.count(category)){ std::vector>&listenerList=merchantInventoryListeners.at(category); if(std::find_if(listenerList.begin(),listenerList.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=listenerList.end()){ ERR("WARNING! Component "<name<<" has already been added to the "<component){ if(std::find_if(equipStatListeners.begin(),equipStatListeners.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=equipStatListeners.end()){ ERR("WARNING! Component "<name<<" has already been added to the Equip Stat listener list! There should not be any duplicates!!") } equipStatListeners.push_back(component); } vf2d Menu::center(){ return size/2; } void Menu::CloseMenu(){ Menu::alreadyClicked=true; game->TextEntryEnable(false); if(stack.size()>0){ stack.pop_back(); }else{ ERR("WARNING! Trying to close out no menu?? Why are we doing this?") } Audio::UpdateBGMVolume(); //If we close the pause menu, the bgm volume was halved. So we notify the engine that this is happening so it can correct it. } std::pairMenu::GetMemoryLeakReportInfo(){ return {lastMenuTypeCreated,lastRegisteredComponent}; } void Menu::CloseAllMenus(){ if(stack.size()==0)ERR("WARNING! Trying to close out no menu?? Why are we doing this?"); while(stack.size()>0){ CloseMenu(); } } bool Menu::IsMenuOpen(){ return stack.size()>0; } void Menu::CleanupAllMenus(){ for(auto&[key,value]:Menu::menus){ Menu*menu=value; for(auto&[name,component]:menu->components){ component->Cleanup(); } menu->components.Reset(); menu->Cleanup(); delete menu; } Menu::menus.clear(); } void Menu::Cleanup(){} void Menu::DrawThemedWindow(vf2d menuPos,vf2d size,Pixel renderColor){ if(GetCurrentTheme().IsScaled()){ DrawScaledWindowBackground(game,menuPos,size,renderColor); DrawScaledWindowBorder(game,menuPos,size,renderColor); }else{ DrawTiledWindowBackground(game,menuPos,size,renderColor); DrawTiledWindowBorder(game,menuPos,size,renderColor); } } const MenuType Menu::GetType()const{ return type; } void Menu::LockInListeners(){ inventoryListeners.SetInitialized(); merchantInventoryListeners.SetInitialized(); } void Menu::AddChapterListener(std::weak_ptrcomponent){ if(std::find_if(chapterListeners.begin(),chapterListeners.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=chapterListeners.end()){ ERR("WARNING! Component "<name<<" has already been added to the Chapter listener list! There should not be any duplicates!!") } chapterListeners.push_back(component); } ToggleFuncData::ToggleFuncData(Menu&menu,AiL*const game,std::weak_ptrcomponent,std::weak_ptrparentComponent,bool checked) :MenuFuncData(menu,game,component,parentComponent),checked(checked){} void Menu::SetupKeyboardNavigation(MenuDataFunc onOpen,MenuInputGroups inputGroups,ButtonNavigationGroups navigationGroups){ this->onOpenFunc=onOpen; this->inputGroups=inputGroups; helpDisplay.Initialize(inputGroups); this->navigationGroups=navigationGroups; } const std::weak_ptrMenu::GetSelection()const{ return selection; } const std::weak_ptrMenu::GetKeySelection()const{ return keyboardSelection; } void Menu::SetSelection(std::weak_ptrbutton,const bool scroll,const bool reset){ if(button.expired())return; selection=button; if(navigationGroups.count(button.lock()->GetName())|| !button.lock()->parentComponent.expired()&&navigationGroups.count(button.lock()->parentComponent.lock()->GetName())){ keyboardSelection=button; } if(!selection.lock()->parentComponent.expired()&&reset){ auto scrollWindow=selection.lock()->parentComponent.lock(); scrollWindow->targetScrollOffset.y=0.f; scrollWindow->scrollOffset.y=0.f; scrollWindow->selectionIndex=0.f; } if(scroll&&!UsingMouseNavigation()&&!button.lock()->parentComponent.expired()){ auto scrollWindow=button.lock()->parentComponent.lock(); scrollWindow->HandleOutsideDisabledButtonSelection(Menu::menus[button.lock()->parentMenu]->components[button.lock()->GetName()]); scrollWindow->selectionIndex=scrollWindow->GetComponentIndex(selection); } } void Menu::SetSelection(std::string_view button,const bool scroll,const bool reset){ selection=Component(type,std::string(button)); if(navigationGroups.count(std::string(button))|| !selection.lock()->parentComponent.expired()&&navigationGroups.count(selection.lock()->parentComponent.lock()->GetName())){ keyboardSelection=selection; } if(scroll&&!UsingMouseNavigation()&&!selection.lock()->parentComponent.expired()){ auto scrollWindow=selection.lock()->parentComponent.lock(); scrollWindow->HandleOutsideDisabledButtonSelection(Menu::menus[selection.lock()->parentMenu]->components[selection.lock()->GetName()]); scrollWindow->selectionIndex=scrollWindow->GetComponentIndex(selection); scrollWindow->SetScrollAmount(scrollWindow->GetScrollAmount()-scrollWindow->GetComponents()[scrollWindow->selectionIndex].lock()->GetPos()); } } void Menu::SetSelection(Data button,const bool reset){ if(std::holds_alternative(button)){ SetSelection(std::string_view(std::get(button)),true,reset); }else if(std::holds_alternative>(button)){ SetSelection(std::get>(button),true,reset); }else{ ERR("WARNING! Specified menu opening function does not hold neither a string nor a pointer to a component to use!"); } } void Menu::IgnoreNextMouseNavigationAttempt(){ nextMouseNavigationIsIgnored=true; } bool Menu::IsCurrentlyActive(MenuType type){ if(Menu::stack.size()==0)return false; return Menu::stack.back()->GetType()==type; } void Menu::ReInitializeInputGroup(){ helpDisplay.Initialize(inputGroups); } const bool Menu::GameInitialized()const{ return game->GameInitialized(); } const bool Menu::IsMouseOverMenu(){ if(Menu::stack.size()==0)return false; return geom2d::overlaps(game->GetMousePos(),geom2d::rect{Menu::stack.back()->pos-"Interface.9PatchSize"_V.x,Menu::stack.back()->size+"Interface.9PatchSize"_V.y*2}); } void Menu::ScrollUp(const MenuType type,const std::string&componentName,Data&returnData,const std::optionalwrapDestination){ if(!Menu::menus[type]->GetSelection().expired()){ auto selection=Menu::menus[type]->GetSelection().lock(); size_t index=Component(type,componentName)->GetComponentIndex(selection); index--; if(index>=Component(type,componentName)->GetComponents().size()){ if(wrapDestination)returnData=wrapDestination.value(); else returnData=Component(type,componentName)->GetComponents().back(); }else{ returnData=Component(type,componentName)->GetComponents()[index]; } } } void Menu::ScrollDown(const MenuType type,const std::string&componentName,Data&returnData,const std::optionalwrapDestination){ if(!Menu::menus[type]->GetSelection().expired()){ auto selection=Menu::menus[type]->GetSelection().lock(); size_t index=Component(type,componentName)->GetComponentIndex(selection); index++; if(index>=Component(type,componentName)->GetComponents().size()){ if(wrapDestination)returnData=wrapDestination.value(); else returnData=Component(type,componentName)->GetComponents().front(); }else{ returnData=Component(type,componentName)->GetComponents()[index]; } } }