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/Adventures in Lestoria/Menu.cpp

792 lines
31 KiB

#pragma region License
/*
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
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 <variant>
#include "SoundEffect.h"
INCLUDE_ITEM_CATEGORIES
INCLUDE_DATA
bool Menu::MOUSE_NAVIGATION=true;
bool Menu::nextMouseNavigationIsIgnored=false;
vi2d Menu::lastActiveMousePos{};
std::vector<Menu*>Menu::stack;
std::map<MenuType,Menu*>Menu::menus;
std::string Menu::themeSelection="BlueDefault";
safeunorderedmap<std::string,Theme>Menu::themes;
safemap<ITCategory,std::vector<std::weak_ptr<MenuComponent>>>Menu::inventoryListeners;
safemap<ITCategory,std::vector<std::weak_ptr<MenuComponent>>>Menu::merchantInventoryListeners;
std::vector<std::weak_ptr<MenuComponent>>Menu::equipStatListeners;
std::vector<std::weak_ptr<MenuComponent>>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_ptr<MenuComponent>Menu::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(){
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();
for(MenuType type=MenuType(int(MenuType::ENUM_START)+1);type<MenuType::ENUM_END;type=MenuType(int(type+1))){
if(menus.count(type)==0){
ERR("WARNING! Menu Type "<<type<<" does not exist!")
}
for(auto&[key,value]:menus[type]->components){
value->AfterCreate();
}
if(menus.size()>MAX_MENUS)ERR("WARNING! Exceeded maximum expected menu count of "<<MAX_MENUS<<"!");
}
//Initialize Credits window.
int line=0;
std::string credits{""};
while(DATA["Credits"].HasProperty(std::format("LINE[{}]",line))){
credits+=DATA["Credits"][std::format("LINE[{}]",line)].GetString()+"\n";
line++;
}
Component<MenuLabel>(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.
MenuSelect(game);
Menu::alreadyClicked=true;
}
}
}
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<ScrollableWindowComponent>(selection.lock()->parentComponent.lock())});
}
void Menu::Update(AiL*game){
if(!draggingComponent){
HoverMenuSelect(game);
}
if(!UsingMouseNavigation()&&geom2d::line<float>(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={};
for(auto&[key,component]:components){
if(component->selectable){
if(!component->disable&&!component->disableOutsideWindow&&!component->grayedOut){
if(component->GetHoverState(game)){
if(!Menu::lastHover.expired()&&&*component!=&*Menu::lastHover.lock())SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED);
Menu::lastHover=component;
component->hovered=true;
itemHovered=true;
SetSelection(std::weak_ptr<MenuComponent>(component));
}
}
}
}
}
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());
}
std::vector<std::weak_ptr<MenuComponent>>allComponents;
std::for_each(components.begin(),components.end(),[&](auto&pair){allComponents.push_back(pair.second);});
std::sort(allComponents.begin(),allComponents.end(),[](std::weak_ptr<MenuComponent>c1,std::weak_ptr<MenuComponent>c2){return c1.lock()->depth>c2.lock()->depth;});
if(GetCurrentTheme().IsScaled()){
DrawScaledWindowBorder(game,pos,size,GetRenderColor());
}else{
DrawTiledWindowBorder(game,pos,size,GetRenderColor());
}
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_ptr<ScrollableWindowComponent>scrollableComponent=DYNAMIC_POINTER_CAST<ScrollableWindowComponent>(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<ButtonName>(returnData)&&std::get<ButtonName>(returnData).length()>0||std::holds_alternative<std::weak_ptr<MenuComponent>>(returnData))menus[menu]->SetSelection(returnData,true);
}
stack.push_back(menus[menu]);
}
void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){
std::weak_ptr<MenuComponent>prevSelection=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<ButtonName>(returnData)&&std::get<ButtonName>(returnData).length()>0||std::holds_alternative<std::weak_ptr<MenuComponent>>(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<ButtonName>(action))Component<MenuComponent>(type,std::get<ButtonName>(action))->Click();
else
if(std::holds_alternative<std::function<void(MenuType)>>(action))std::get<std::function<void(MenuType)>>(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)){
Navigation nav=navigationGroups[selectionButtonName];
if(game->KEY_UP.PressedDAS()||game->KEY_SCROLLUP.AnalogDAS(0.5f)<-0.5f){
SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED);
SetMouseNavigation(false);
if(std::holds_alternative<std::string>(nav.up)&&std::get<std::string>(nav.up).length()>0)SetSelection(std::string_view(std::get<std::string>(nav.up)));
else
if(std::holds_alternative<MenuDataFunc>(nav.up)){
Data returnData;
std::get<MenuDataFunc>(nav.up)(type,returnData);
SetSelection(returnData);
}
}
if(game->KEY_DOWN.PressedDAS()||game->KEY_SCROLLDOWN.AnalogDAS(0.5f)>0.5f){
SoundEffect::PlaySFX("Menu Navigate",SoundEffect::CENTERED);
SetMouseNavigation(false);
if(std::holds_alternative<std::string>(nav.down)&&std::get<std::string>(nav.down).length()>0)SetSelection(std::string_view(std::get<std::string>(nav.down)));
else
if(std::holds_alternative<MenuDataFunc>(nav.down)){
Data returnData;
std::get<MenuDataFunc>(nav.down)(type,returnData);
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<std::string>(nav.left)&&std::get<std::string>(nav.left).length()>0)SetSelection(std::string_view(std::get<std::string>(nav.left)));
else
if(std::holds_alternative<MenuDataFunc>(nav.left)){
Data returnData;
std::get<MenuDataFunc>(nav.left)(type,returnData);
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<std::string>(nav.right)&&std::get<std::string>(nav.right).length()>0)SetSelection(std::string_view(std::get<std::string>(nav.right)));
else
if(std::holds_alternative<MenuDataFunc>(nav.right)){
Data returnData;
std::get<MenuDataFunc>(nav.right)(type,returnData);
SetSelection(returnData);
}
}
}else
if(!Menu::UsingMouseNavigation()){
if(onOpenFunc){
Data returnData;
onOpenFunc(GetType(),returnData);
defaultButton=returnData;
if(std::holds_alternative<ButtonName>(returnData)&&std::get<ButtonName>(returnData).length()>0||std::holds_alternative<std::weak_ptr<MenuComponent>>(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<ButtonName>(returnData)&&std::get<ButtonName>(returnData).length()>0||std::holds_alternative<std::weak_ptr<MenuComponent>>(returnData))SetSelection(returnData,true);
}
break;
}
}
}while(iterationCount<MAX_ITERATIONS&&(selection.lock()->grayedOut||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_ptr<MenuComponent>disabledButton){
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_ptr<MenuComponent>component:inventoryListeners.at(cat)){
std::weak_ptr<InventoryScrollableWindowComponent>comp=DYNAMIC_POINTER_CAST<InventoryScrollableWindowComponent>(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_ptr<MenuComponent>component:merchantInventoryListeners.at(cat)){
std::weak_ptr<InventoryScrollableWindowComponent>comp=DYNAMIC_POINTER_CAST<InventoryScrollableWindowComponent>(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_ptr<MenuComponent>component,ITCategory category){
if(inventoryListeners.count(category)){
std::vector<std::weak_ptr<MenuComponent>>&listenerList=inventoryListeners.at(category);
if(std::find_if(listenerList.begin(),listenerList.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=listenerList.end()){
ERR("WARNING! Component "<<component.lock()->name<<" has already been added to the "<<category<<" listener list! There should not be any duplicates!!")
}
listenerList.push_back(component);
}else{
ERR("WARNING! Inventory category "<<category<<" does not exist!")
}
}
void Menu::InitializeMenuListenerCategory(const std::string&category){
inventoryListeners[category];
merchantInventoryListeners[category];
}
void Menu::AddMerchantInventoryListener(std::weak_ptr<MenuComponent>component,ITCategory category){
if(merchantInventoryListeners.count(category)){
std::vector<std::weak_ptr<MenuComponent>>&listenerList=merchantInventoryListeners.at(category);
if(std::find_if(listenerList.begin(),listenerList.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=listenerList.end()){
ERR("WARNING! Component "<<component.lock()->name<<" has already been added to the "<<category<<" merchant listener list! There should not be any duplicates!!")
}
listenerList.push_back(component);
}else{
ERR("WARNING! Inventory category "<<category<<" does not exist!")
}
}
void Menu::AddEquipStatListener(std::weak_ptr<MenuComponent>component){
if(std::find_if(equipStatListeners.begin(),equipStatListeners.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=equipStatListeners.end()){
ERR("WARNING! Component "<<component.lock()->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?")
}
}
std::pair<MenuType,std::string>Menu::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);
}
}
void Menu::RecalculateComponentCount(){
componentCount=components.size();
}
const MenuType Menu::GetType()const{
return type;
}
void Menu::LockInListeners(){
inventoryListeners.SetInitialized();
merchantInventoryListeners.SetInitialized();
}
void Menu::AddChapterListener(std::weak_ptr<MenuComponent>component){
if(std::find_if(chapterListeners.begin(),chapterListeners.end(),[&](auto&ptr){return &*ptr.lock()==&*component.lock();})!=chapterListeners.end()){
ERR("WARNING! Component "<<component.lock()->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_ptr<MenuComponent>component,std::weak_ptr<ScrollableWindowComponent>parentComponent,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_ptr<MenuComponent>Menu::GetSelection()const{
return selection;
}
const std::weak_ptr<MenuComponent>Menu::GetKeySelection()const{
return keyboardSelection;
}
void Menu::SetSelection(std::weak_ptr<MenuComponent>button,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<MenuComponent>(type,std::string(button));
if(navigationGroups.count(std::string(button))||
!selection.lock()->parentComponent.expired()&&navigationGroups.count(selection.lock()->parentComponent.lock()->GetName())){
keyboardSelection=selection;
}
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()&&!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);
}
}
void Menu::SetSelection(Data button,const bool reset){
if(std::holds_alternative<std::string>(button)){
SetSelection(std::string_view(std::get<std::string>(button)),true,reset);
}else
if(std::holds_alternative<std::weak_ptr<MenuComponent>>(button)){
SetSelection(std::get<std::weak_ptr<MenuComponent>>(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);
}