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.
726 lines
26 KiB
726 lines
26 KiB
1 year ago
|
#pragma region License
|
||
1 year ago
|
/*
|
||
|
License (OLC-3)
|
||
|
~~~~~~~~~~~~~~~
|
||
|
|
||
11 months ago
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
||
1 year ago
|
|
||
|
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.
|
||
1 year ago
|
|
||
|
Portions of this software are copyright © 2023 The FreeType
|
||
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
||
|
All rights reserved.
|
||
1 year ago
|
*/
|
||
1 year ago
|
#pragma endregion
|
||
11 months ago
|
#include "AdventuresInLestoria.h"
|
||
1 year ago
|
#include "MenuComponent.h"
|
||
1 year ago
|
#include "DEFINES.h"
|
||
|
#include "safemap.h"
|
||
1 year ago
|
#include "Item.h"
|
||
|
#include "MenuItemButton.h"
|
||
|
#include "ScrollableWindowComponent.h"
|
||
11 months ago
|
#include "RowInventoryScrollableWindowComponent.h"
|
||
|
#include "MenuItemItemButton.h"
|
||
|
|
||
|
INCLUDE_ITEM_CATEGORIES
|
||
|
INCLUDE_DATA
|
||
1 year ago
|
|
||
|
bool Menu::MOUSE_NAVIGATION=true;
|
||
|
std::vector<Menu*>Menu::stack;
|
||
1 year ago
|
std::map<MenuType,Menu*>Menu::menus;
|
||
1 year ago
|
std::string Menu::themeSelection="BlueDefault";
|
||
|
safeunorderedmap<std::string,Theme>Menu::themes;
|
||
1 year ago
|
safemap<ITCategory,std::vector<MenuComponent*>>Menu::inventoryListeners;
|
||
11 months ago
|
safemap<ITCategory,std::vector<MenuComponent*>>Menu::merchantInventoryListeners;
|
||
12 months ago
|
std::vector<MenuComponent*>Menu::equipStatListeners;
|
||
11 months ago
|
std::vector<MenuComponent*>Menu::chapterListeners;
|
||
1 year ago
|
const vf2d Menu::CENTERED = {-456,-456};
|
||
1 year ago
|
std::vector<MenuComponent*>Menu::unhandledComponents;
|
||
1 year ago
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
///__/////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//| |////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//| |/////WARNING! If you are adding something here you likely are adding another container with MenuComponent pointers in it right now.
|
||
1 year ago
|
//| |/////Because we are handling raw pointers, you must also add this container to the list of iterating search removal containers that occur in the
|
||
|
//| |/////DESTRUCTOR of MenuComponents!!!!! (Go to MenuComponent::~MenuComponent()) THIS IS NOT A DRILL!
|
||
1 year ago
|
//|__|////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
///__/////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//|__/////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||
1 year ago
|
MenuType Menu::lastMenuTypeCreated;
|
||
|
std::string Menu::lastRegisteredComponent;
|
||
1 year ago
|
|
||
1 year ago
|
INCLUDE_game
|
||
1 year ago
|
INCLUDE_GFX
|
||
12 months ago
|
INCLUDE_WINDOW_SIZE
|
||
1 year ago
|
|
||
12 months ago
|
using A=Attribute;
|
||
1 year ago
|
|
||
1 year ago
|
Menu::Menu(vf2d pos,vf2d size)
|
||
1 year ago
|
:pos(pos==CENTERED?WINDOW_SIZE/2-size/2:vi2d{pos}),size(size){
|
||
12 months ago
|
this->window=ViewPort::rectViewPort({-24,-24},this->size+vi2d{48,48},this->pos);
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
Menu::~Menu(){
|
||
12 months ago
|
for(auto&[key,value]:components){
|
||
|
delete value;
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
void Menu::InitializeMenus(){
|
||
12 months ago
|
#define MAX_MENUS 32
|
||
|
stack.reserve(MAX_MENUS);
|
||
1 year ago
|
InitializeConsumableInventoryWindow();
|
||
1 year ago
|
InitializeClassSelectionWindow();
|
||
1 year ago
|
InitializeClassInfoWindow();
|
||
1 year ago
|
InitializeMainMenuWindow();
|
||
1 year ago
|
InitializeOverworldMapLevelWindow();
|
||
1 year ago
|
InitializeItemLoadoutWindow();
|
||
1 year ago
|
InitializeLevelCompleteWindow();
|
||
1 year ago
|
InitializeOverworldMenuWindow();
|
||
|
InitializeCharacterMenuWindow();
|
||
12 months ago
|
InitializeInventoryWindow();
|
||
11 months ago
|
InitializeMerchantWindow();
|
||
|
InitializeBuyItemWindow();
|
||
|
InitializeSellItemWindow();
|
||
11 months ago
|
InitializeBlacksmithCraftingWindow();
|
||
11 months ago
|
InitializeCraftItemWindow();
|
||
11 months ago
|
InitializeConsumableCraftingWindow();
|
||
11 months ago
|
InitializeConsumableCraftItemWindow();
|
||
11 months ago
|
InitializeSaveFileWindow();
|
||
11 months ago
|
InitializeLoadGameWindow();
|
||
11 months ago
|
InitializeUserIDWindow();
|
||
1 year ago
|
|
||
12 months ago
|
for(MenuType type=MenuType(int(MenuType::ENUM_START)+1);type<MenuType::ENUM_END;type=MenuType(int(type+1))){
|
||
1 year ago
|
if(menus.count(type)==0){
|
||
1 year ago
|
ERR("WARNING! Menu Type "<<type<<" does not exist!")
|
||
1 year ago
|
}
|
||
1 year ago
|
//Lock up everything once it's done.
|
||
|
menus[type]->buttons.SetInitialized();
|
||
|
menus[type]->keyboardButtons.SetInitialized();
|
||
12 months ago
|
for(auto&[key,value]:menus[type]->components){
|
||
|
MenuComponent*component=value;
|
||
1 year ago
|
component->AfterCreate();
|
||
|
}
|
||
12 months ago
|
if(menus.size()>MAX_MENUS)ERR("WARNING! Exceeded maximum expected menu count of "<<MAX_MENUS<<"!");
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
if(Menu::unhandledComponents.size()>0){
|
||
|
std::cout<<"WARNING! There are "<<Menu::unhandledComponents.size()<<" components that were not added to any Menu! These have been leaked and should not be happening!"<<std::endl;
|
||
|
std::cout<<"See below for a report of unhandled components:"<<std::endl;
|
||
|
int count=0;
|
||
|
for(MenuComponent*component:Menu::unhandledComponents){
|
||
|
std::cout<<"\tComponent "<<(count+1)<<": "<<std::endl;
|
||
1 year ago
|
std::cout<<"\t-Parent Menu:"<<component->parentMenu<<" Label: "<<component->label<<std::endl;
|
||
|
std::cout<<"\t\tCreated Inside of Menu: "<<component->memoryLeakInfo.first<<" // Last Component: "<<component->memoryLeakInfo.second<<std::endl;
|
||
1 year ago
|
count++;
|
||
|
}
|
||
1 year ago
|
ERR("")
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
Menu*Menu::CreateMenu(MenuType type,vf2d pos,vf2d size){
|
||
1 year ago
|
menus[type]=NEW Menu(pos,size);
|
||
12 months ago
|
menus[type]->type=type;
|
||
1 year ago
|
lastMenuTypeCreated=type;
|
||
1 year ago
|
return menus.at(type);
|
||
|
}
|
||
|
|
||
11 months ago
|
void Menu::CheckClickAndPerformMenuSelect(AiL*game){
|
||
1 year ago
|
if(game->GetMouse(Mouse::LEFT).bReleased||game->GetKey(SPACE).bReleased||game->GetKey(ENTER).bReleased){
|
||
|
MenuSelect(game);
|
||
|
}
|
||
|
}
|
||
|
|
||
11 months ago
|
void Menu::HoverMenuSelect(AiL*game){
|
||
11 months ago
|
if(!game->IsFocused()||selection==vi2d{-1,-1}||buttons[selection.y][selection.x]->disabled)return;
|
||
1 year ago
|
if(buttons[selection.y][selection.x]->draggable){
|
||
|
if(buttonHoldTime<"ThemeGlobal.MenuHoldTime"_F){
|
||
|
CheckClickAndPerformMenuSelect(game);
|
||
|
}else{
|
||
1 year ago
|
draggingComponent=buttons[selection.y][selection.x]->PickUpDraggableItem();
|
||
|
buttonHoldTime=0;
|
||
1 year ago
|
}
|
||
1 year ago
|
}else{
|
||
|
CheckClickAndPerformMenuSelect(game);
|
||
|
}
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::MenuSelect(AiL*game){
|
||
11 months ago
|
if(!game->IsFocused()||selection==vi2d{-1,-1}||(buttons[selection.y][selection.x]->disabled||buttons[selection.y][selection.x]->grayedOut))return;
|
||
1 year ago
|
bool buttonStillValid=buttons[selection.y][selection.x]->onClick(MenuFuncData{*this,game,buttons[selection.y][selection.x],(ScrollableWindowComponent*)buttons[selection.y][selection.x]->parentComponent});
|
||
1 year ago
|
if(buttonStillValid){
|
||
|
if(buttons[selection.y][selection.x]->menuDest!=MenuType::ENUM_END){
|
||
|
if(stack.size()<32){
|
||
|
stack.push_back(menus[buttons[selection.y][selection.x]->menuDest]);//Navigate to the next menu.
|
||
|
}else{
|
||
|
ERR("WARNING! Exceeded menu stack size limit!")
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::Update(AiL*game){
|
||
11 months ago
|
if(buttons.count(selection.y)==0)selection={-1,-1};
|
||
11 months ago
|
if(selection.x<0||selection.x>=buttons[selection.y].size()){selection={-1,-1};}
|
||
|
|
||
1 year ago
|
if(draggingComponent==nullptr){
|
||
|
HoverMenuSelect(game);
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
if(!UsingMouseNavigation()&&geom2d::line<float>(lastActiveMousePos,game->GetMousePos()).length()>="ThemeGlobal.MouseActivationDistance"_F||UsingMouseNavigation()){
|
||
|
SetMouseNavigation(true);
|
||
|
}
|
||
|
|
||
12 months ago
|
for(auto&[key,value]:buttons){
|
||
|
for(auto&button:value){
|
||
1 year ago
|
if(!button->disabled){
|
||
|
button->hovered=false;
|
||
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
bool itemHovered=false;
|
||
|
|
||
1 year ago
|
if(!UsingMouseNavigation()){
|
||
11 months ago
|
if(!game->IsTextEntryEnabled()){
|
||
|
if(selection!=vi2d{-1,-1}){
|
||
|
buttons[selection.y][selection.x]->hovered=true;
|
||
|
itemHovered=true;
|
||
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
}else{
|
||
1 year ago
|
selection={-1,-1};
|
||
12 months ago
|
for(auto&[key,value]:buttons){
|
||
1 year ago
|
int index=0;
|
||
12 months ago
|
for(auto&button:value){
|
||
11 months ago
|
if(!button->disabled&&!button->grayedOut){
|
||
1 year ago
|
if(button->GetHoverState(game)){
|
||
|
button->hovered=true;
|
||
1 year ago
|
itemHovered=true;
|
||
12 months ago
|
selection.y=key;
|
||
1 year ago
|
selection.x=index;
|
||
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
index++;
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
if(itemHovered&&draggingComponent==nullptr&&selection!=vi2d{-1,-1}&&(((!UsingMouseNavigation()&&(game->GetKey(ENTER).bHeld)||game->GetKey(SPACE).bHeld))||game->GetMouse(Mouse::LEFT).bHeld)){
|
||
1 year ago
|
buttonHoldTime+=game->GetElapsedTime();
|
||
|
}else{
|
||
|
buttonHoldTime=0;
|
||
|
}
|
||
|
|
||
|
if(draggingComponent!=nullptr){
|
||
|
MenuComponent*selectedComponent=nullptr;
|
||
|
if(selection!=vi2d{-1,-1}){
|
||
|
selectedComponent=buttons[selection.y][selection.x];
|
||
|
}
|
||
|
|
||
|
|
||
|
auto ClearDraggingComponent=[&](){
|
||
|
delete draggingComponent; //We know we allocated a new instance of this, so we will now free it.
|
||
|
draggingComponent=nullptr;
|
||
|
};
|
||
|
|
||
1 year ago
|
if(!UsingMouseNavigation()){
|
||
11 months ago
|
if(!game->IsTextEntryEnabled()){
|
||
|
if(game->GetKey(ENTER).bReleased||game->GetKey(SPACE).bReleased){
|
||
|
if(selectedComponent==nullptr){//Dropping over an empty area.
|
||
|
ClearDraggingComponent();
|
||
|
}else
|
||
|
if(selectedComponent->DropDraggableItem(draggingComponent)){
|
||
|
ClearDraggingComponent();
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}else{
|
||
|
if(game->GetMouse(Mouse::LEFT).bReleased){
|
||
1 year ago
|
if(selectedComponent==nullptr){//Dropping over an empty area.
|
||
|
ClearDraggingComponent();
|
||
|
}else
|
||
1 year ago
|
if(selectedComponent->DropDraggableItem(draggingComponent)){
|
||
1 year ago
|
ClearDraggingComponent();
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
11 months ago
|
|
||
|
if(!game->IsTextEntryEnabled()){
|
||
|
KeyboardButtonNavigation(game,pos);
|
||
|
}
|
||
11 months ago
|
|
||
12 months ago
|
for(auto&[key,value]:buttons){
|
||
|
for(auto&button:value){
|
||
|
if(button->renderInMain){
|
||
12 months ago
|
button->_BeforeUpdate(game);
|
||
12 months ago
|
}
|
||
|
}
|
||
|
}
|
||
|
for(auto&component:displayComponents){
|
||
|
if(component->renderInMain){
|
||
12 months ago
|
component->_BeforeUpdate(game);
|
||
12 months ago
|
}
|
||
|
}
|
||
12 months ago
|
for(auto&[key,value]:buttons){
|
||
|
for(auto&button:value){
|
||
1 year ago
|
if(button->renderInMain){
|
||
1 year ago
|
button->_Update(game);
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
for(auto&component:displayComponents){
|
||
1 year ago
|
if(component->renderInMain){
|
||
1 year ago
|
component->_Update(game);
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
};
|
||
|
|
||
11 months ago
|
void Menu::Draw(AiL*game){
|
||
1 year ago
|
if(GetCurrentTheme().IsScaled()){
|
||
1 year ago
|
DrawScaledWindowBackground(game,pos,size,GetRenderColor());
|
||
1 year ago
|
}else{
|
||
1 year ago
|
DrawTiledWindowBackground(game,pos,size,GetRenderColor());
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
12 months ago
|
std::vector<MenuComponent*>allComponents;
|
||
|
std::copy(displayComponents.begin(),displayComponents.end(),std::back_inserter(allComponents));
|
||
|
std::for_each(buttons.begin(),buttons.end(),[&](auto&pair){std::copy(pair.second.begin(),pair.second.end(),std::back_inserter(allComponents));});
|
||
|
std::sort(allComponents.begin(),allComponents.end(),[](MenuComponent*c1,MenuComponent*c2){return c1->depth>c2->depth;});
|
||
12 months ago
|
|
||
|
if(GetCurrentTheme().IsScaled()){
|
||
|
DrawScaledWindowBorder(game,pos,size,GetRenderColor());
|
||
|
}else{
|
||
|
DrawTiledWindowBorder(game,pos,size,GetRenderColor());
|
||
|
}
|
||
|
|
||
12 months ago
|
for(const auto&component:allComponents){
|
||
1 year ago
|
if(component->renderInMain){
|
||
12 months ago
|
component->_DrawDecal(window,this==Menu::stack.back());
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
if(draggingComponent!=nullptr){
|
||
|
vf2d offsetPos=draggingComponent->rect.pos;
|
||
1 year ago
|
if(!UsingMouseNavigation()){
|
||
1 year ago
|
MenuComponent*selectedComponent=buttons[selection.y][selection.x];
|
||
1 year ago
|
vf2d drawOffset{};
|
||
|
if(selectedComponent->parentComponent!=nullptr){
|
||
11 months ago
|
ScrollableWindowComponent*scrollableComponent=DYNAMIC_CAST<ScrollableWindowComponent*>(selectedComponent->parentComponent);
|
||
12 months ago
|
if(scrollableComponent!=nullptr){
|
||
|
drawOffset+=scrollableComponent->GetScrollAmount();
|
||
|
}
|
||
1 year ago
|
}
|
||
12 months ago
|
draggingComponent->V(A::DRAW_OFFSET)=drawOffset+pos-offsetPos+selectedComponent->rect.pos+vi2d{1,-4};
|
||
|
draggingComponent->DrawDecal(window,this==Menu::stack.back());
|
||
1 year ago
|
}else{
|
||
12 months ago
|
draggingComponent->V(A::DRAW_OFFSET)-offsetPos+game->GetMousePos();
|
||
|
draggingComponent->DrawDecal(window,this==Menu::stack.back());
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
};
|
||
|
|
||
1 year ago
|
void Menu::OpenMenu(MenuType menu,bool cover){
|
||
1 year ago
|
menus[menu]->cover=cover;
|
||
1 year ago
|
stack.push_back(menus[menu]);
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){
|
||
1 year ago
|
vi2d prevSelection=selection;
|
||
1 year ago
|
if(game->GetKey(RIGHT).bPressed){
|
||
|
if(selection==vi2d{-1,-1})return;
|
||
1 year ago
|
SetMouseNavigation(false);
|
||
1 year ago
|
selection.x=(size_t(selection.x)+1)%keyboardButtons[selection.y].size();
|
||
1 year ago
|
}
|
||
|
if(game->GetKey(LEFT).bPressed){
|
||
|
if(selection==vi2d{-1,-1})return;
|
||
|
selection.x--;
|
||
1 year ago
|
SetMouseNavigation(false);
|
||
1 year ago
|
if(selection.x<0)selection.x+=int32_t(keyboardButtons[selection.y].size());
|
||
1 year ago
|
}
|
||
|
if(game->GetKey(DOWN).bPressed||game->GetKey(UP).bPressed){
|
||
|
if(game->GetKey(DOWN).bPressed){
|
||
1 year ago
|
SetMouseNavigation(false);
|
||
1 year ago
|
bool found=false;
|
||
|
bool selectedItem=false;
|
||
|
if(selection==vi2d{-1,-1}){
|
||
|
//Highlight first item.
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
|
selection.y=key;
|
||
1 year ago
|
break;
|
||
|
}
|
||
|
}else{
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
1 year ago
|
if(found){ //Once we discover the previous element, the next element becomes our next selection.
|
||
1 year ago
|
int previousButtonX=int(keyboardButtons[selection.y][selection.x]->rect.pos.x);
|
||
12 months ago
|
selection.y=key;
|
||
1 year ago
|
int index=0;
|
||
12 months ago
|
for(auto&button:value){ //Try to match a button in the same column as this button first.
|
||
1 year ago
|
if(previousButtonX==button->rect.pos.x){
|
||
1 year ago
|
selection.x=index;
|
||
|
break;
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
1 year ago
|
selectedItem=true;
|
||
|
break;
|
||
|
}
|
||
12 months ago
|
if(key==selection.y
|
||
1 year ago
|
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
|
||
1 year ago
|
&&selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
|
||
1 year ago
|
found=true;
|
||
|
}
|
||
|
}
|
||
|
if(!selectedItem){ //This means we need to loop around instead and pick the first one.
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
|
selection.y=key;
|
||
1 year ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if(game->GetKey(UP).bPressed){
|
||
1 year ago
|
SetMouseNavigation(false);
|
||
1 year ago
|
if(selection==vi2d{-1,-1}){
|
||
|
//Highlight last item.
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
|
selection.y=key;
|
||
1 year ago
|
}
|
||
|
}else{
|
||
|
int prevInd=-1;
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
|
if(key==selection.y&&
|
||
1 year ago
|
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
|
||
|
selection.x>=0&&selection.x<keyboardButtons[selection.y].size()){
|
||
1 year ago
|
break;
|
||
|
}
|
||
12 months ago
|
prevInd=key;
|
||
1 year ago
|
}
|
||
|
if(prevInd!=-1){
|
||
1 year ago
|
int previousButtonX=int(keyboardButtons[selection.y][selection.x]->rect.pos.x);
|
||
1 year ago
|
selection.y=prevInd;
|
||
1 year ago
|
int index=0;
|
||
1 year ago
|
for(auto&button:keyboardButtons[prevInd]){ //Try to match a button in the same column as this button first.
|
||
1 year ago
|
if(previousButtonX==button->rect.pos.x){
|
||
1 year ago
|
selection.x=index;
|
||
|
break;
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
1 year ago
|
}else{ //Since we didn't find it, it means we're at the top of the list or the list is empty. Go to the last element and use that one.
|
||
|
int lastInd=-1;
|
||
12 months ago
|
for(auto&[key,value]:keyboardButtons){
|
||
|
lastInd=key;
|
||
1 year ago
|
}
|
||
|
selection.y=lastInd;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
//In both cases, we should clamp the X index to make sure it's still valid.
|
||
|
if(selection.y!=-1){
|
||
1 year ago
|
selection.x=std::clamp(selection.x,0,int(keyboardButtons[selection.y].size())-1);
|
||
1 year ago
|
}else{
|
||
|
selection.x=-1;
|
||
|
}
|
||
|
}
|
||
1 year ago
|
if(game->GetMouse(0).bPressed||game->GetKey(ENTER).bPressed||game->GetKey(SPACE).bPressed){
|
||
1 year ago
|
SetMouseNavigation(game->GetMouse(0).bPressed); //If a click occurs we use mouse controls.
|
||
|
if(!UsingMouseNavigation()){
|
||
1 year ago
|
buttonHoldTime=0;
|
||
1 year ago
|
//Key presses automatically highlight the first button if it's not highlighted.
|
||
|
if(selection==vi2d{-1,-1}&&buttons.size()>0){
|
||
|
//Find the first possible button entry in the map...
|
||
|
int firstInd=-1;
|
||
12 months ago
|
for(auto&[key,value]:buttons){
|
||
|
if(buttons[key].size()>0){
|
||
|
firstInd=key;
|
||
1 year ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
if(firstInd!=-1){ //This means we found a valid menu item. If we didn't find one don't highlight any menu item...
|
||
|
selection={0,firstInd};
|
||
|
}
|
||
|
}
|
||
12 months ago
|
}else{
|
||
1 year ago
|
buttonHoldTime=0;
|
||
1 year ago
|
}
|
||
|
}
|
||
1 year ago
|
if(prevSelection!=selection){
|
||
11 months ago
|
if(selection!=vi2d{-1,-1}&&(buttons[selection.y][selection.x]->disabled||buttons[selection.y][selection.x]->grayedOut)){
|
||
1 year ago
|
bool handled=false;
|
||
1 year ago
|
if(!UsingMouseNavigation()){
|
||
1 year ago
|
//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(buttons[selection.y][selection.x])){
|
||
|
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.
|
||
|
selection=prevSelection;
|
||
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::DrawScaledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){
|
||
1 year ago
|
vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]};
|
||
|
|
||
|
//Upper-Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos-patchSize,patchSize,GetPatchPart(0,0).Decal(),{patchSize.x*0,patchSize.y*0},patchSize,renderColor);
|
||
1 year ago
|
//Upper-Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,-patchSize.y},patchSize,GetPatchPart(2,0).Decal(),{patchSize.x*2,patchSize.y*0},patchSize,renderColor);
|
||
1 year ago
|
//Bottom-Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,size.y},patchSize,GetPatchPart(0,2).Decal(),{patchSize.x*0,patchSize.y*2},patchSize,renderColor);
|
||
1 year ago
|
//Bottom-Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,size.y},patchSize,GetPatchPart(2,2).Decal(),{patchSize.x*2,patchSize.y*2},patchSize,renderColor);
|
||
1 year ago
|
//Top
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{0,-patchSize.y},vf2d{size.x,patchSize.y},GetPatchPart(1,0).Decal(),{patchSize.x*1,patchSize.y*0},patchSize,renderColor);
|
||
1 year ago
|
//Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,0},vf2d{patchSize.x,size.y},GetPatchPart(0,1).Decal(),{patchSize.x*0,patchSize.y*1},patchSize,renderColor);
|
||
1 year ago
|
//Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,0},vf2d{patchSize.x,size.y},GetPatchPart(2,1).Decal(),{patchSize.x*2,patchSize.y*1},patchSize,renderColor);
|
||
1 year ago
|
//Bottom
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{0,size.y},vf2d{size.x,patchSize.y},GetPatchPart(1,2).Decal(),{patchSize.x*1,patchSize.y*2},patchSize,renderColor);
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::DrawTiledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){
|
||
1 year ago
|
vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]};
|
||
|
|
||
|
//Upper-Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos-patchSize,patchSize,GetPatchPart(0,0).Decal(),{0,0},patchSize,renderColor);
|
||
1 year ago
|
//Upper-Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,-patchSize.y},patchSize,GetPatchPart(2,0).Decal(),{0,0},patchSize,renderColor);
|
||
1 year ago
|
//Bottom-Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,size.y},patchSize,GetPatchPart(0,2).Decal(),{0,0},patchSize,renderColor);
|
||
1 year ago
|
//Bottom-Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,size.y},patchSize,GetPatchPart(2,2).Decal(),{0,0},patchSize,renderColor);
|
||
1 year ago
|
//Top
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{0,-patchSize.y},vf2d{size.x,patchSize.y},GetPatchPart(1,0).Decal(),{0,0},vf2d{size.x,patchSize.y},renderColor);
|
||
1 year ago
|
//Left
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{-patchSize.x,0},vf2d{patchSize.x,size.y},GetPatchPart(0,1).Decal(),{0,0},vf2d{patchSize.x,size.y},renderColor);
|
||
1 year ago
|
//Right
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{size.x,0},vf2d{patchSize.x,size.y},GetPatchPart(2,1).Decal(),{0,0},vf2d{patchSize.x,size.y},renderColor);
|
||
1 year ago
|
//Bottom
|
||
1 year ago
|
game->DrawPartialDecal(menuPos+vf2d{0,size.y},vf2d{size.x,patchSize.y},GetPatchPart(1,2).Decal(),{0,0},vf2d{size.x,patchSize.y},renderColor);
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::DrawScaledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){
|
||
1 year ago
|
vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]};
|
||
|
|
||
|
//Center
|
||
|
if(GetCurrentTheme().HasBackground()){
|
||
|
Decal*back=GetCurrentTheme().GetBackground();
|
||
1 year ago
|
game->DrawPartialDecal(menuPos,size,back,{0,0},back->sprite->Size(),renderColor);
|
||
1 year ago
|
}else{
|
||
1 year ago
|
game->DrawPartialDecal(menuPos,size,GetPatchPart(1,1).Decal(),{patchSize.x*1,patchSize.y*1},patchSize,renderColor);
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
11 months ago
|
void Menu::DrawTiledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor){
|
||
1 year ago
|
vf2d patchSize={"Interface.9PatchSize"_f[0],"Interface.9PatchSize"_f[1]};
|
||
|
|
||
1 year ago
|
//Center
|
||
1 year ago
|
if(GetCurrentTheme().HasBackground()){
|
||
|
Decal*back=GetCurrentTheme().GetBackground();
|
||
1 year ago
|
game->DrawPartialDecal(menuPos,size,back,{0,0},size,renderColor);
|
||
1 year ago
|
}else{
|
||
1 year ago
|
game->DrawPartialDecal(menuPos,size,GetPatchPart(1,1).Decal(),{0,0},patchSize,renderColor);
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
Renderable&Menu::GetPatchPart(int x,int y){
|
||
1 year ago
|
return GFX[themeSelection+"_"+std::to_string(x)+std::to_string(y)+".png"];
|
||
1 year ago
|
}
|
||
|
|
||
|
Theme&Menu::GetCurrentTheme(){
|
||
|
return themes[themeSelection];
|
||
1 year ago
|
}
|
||
|
|
||
|
Pixel Menu::GetRenderColor(){
|
||
|
bool focused=Menu::stack.back()==this;
|
||
|
Pixel col=WHITE;
|
||
|
if(!focused){
|
||
|
col=WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F;
|
||
|
}
|
||
|
return col;
|
||
1 year ago
|
}
|
||
|
|
||
|
bool Menu::HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton){
|
||
|
if(disabledButton->parentComponent!=nullptr){
|
||
|
return disabledButton->parentComponent->HandleOutsideDisabledButtonSelection(disabledButton);
|
||
|
}else{
|
||
|
return false;
|
||
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
|
||
|
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.
|
||
|
INCLUDE_game
|
||
|
lastActiveMousePos=game->GetMousePos();
|
||
|
}
|
||
|
MOUSE_NAVIGATION=mouseNavigation;
|
||
1 year ago
|
};
|
||
|
|
||
|
void Menu::InventorySlotsUpdated(ITCategory cat){
|
||
|
//Update the inventory with a new inventory slot, since there's one additional item to interact with now.
|
||
1 year ago
|
for(MenuComponent*component:inventoryListeners.at(cat)){
|
||
11 months ago
|
InventoryScrollableWindowComponent*comp=DYNAMIC_CAST<InventoryScrollableWindowComponent*>(component); //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->OnInventorySlotsUpdate(cat);
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
11 months ago
|
void Menu::MerchantInventorySlotsUpdated(ITCategory cat){
|
||
|
//Update the inventory with a new inventory slot, since there's one additional item to interact with now.
|
||
|
for(MenuComponent*component:merchantInventoryListeners.at(cat)){
|
||
11 months ago
|
InventoryScrollableWindowComponent*comp=DYNAMIC_CAST<InventoryScrollableWindowComponent*>(component); //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->OnInventorySlotsUpdate(cat);
|
||
11 months ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
void Menu::AddInventoryListener(MenuComponent*component,ITCategory category){
|
||
|
if(inventoryListeners.count(category)){
|
||
|
std::vector<MenuComponent*>&listenerList=inventoryListeners.at(category);
|
||
|
if(std::find(listenerList.begin(),listenerList.end(),component)!=listenerList.end()){
|
||
1 year ago
|
ERR("WARNING! Component "<<component->name<<" has already been added to the "<<category<<" listener list! There should not be any duplicates!!")
|
||
1 year ago
|
}
|
||
|
listenerList.push_back(component);
|
||
1 year ago
|
}else{
|
||
1 year ago
|
ERR("WARNING! Inventory category "<<category<<" does not exist!")
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
11 months ago
|
void Menu::InitializeMenuListenerCategory(const std::string&category){
|
||
|
inventoryListeners[category];
|
||
|
merchantInventoryListeners[category];
|
||
|
}
|
||
|
|
||
|
void Menu::AddMerchantInventoryListener(MenuComponent*component,ITCategory category){
|
||
|
if(merchantInventoryListeners.count(category)){
|
||
|
std::vector<MenuComponent*>&listenerList=merchantInventoryListeners.at(category);
|
||
|
if(std::find(listenerList.begin(),listenerList.end(),component)!=listenerList.end()){
|
||
|
ERR("WARNING! Component "<<component->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!")
|
||
|
}
|
||
|
}
|
||
|
|
||
12 months ago
|
void Menu::AddEquipStatListener(MenuComponent*component){
|
||
|
if(std::find(equipStatListeners.begin(),equipStatListeners.end(),component)!=equipStatListeners.end()){
|
||
|
ERR("WARNING! Component "<<component->name<<" has already been added to the Equip Stat listener list! There should not be any duplicates!!")
|
||
|
}
|
||
|
equipStatListeners.push_back(component);
|
||
|
}
|
||
|
|
||
1 year ago
|
vf2d Menu::center(){
|
||
1 year ago
|
return size/2;
|
||
1 year ago
|
}
|
||
|
|
||
|
void Menu::CloseMenu(){
|
||
|
if(stack.size()>0){
|
||
|
stack.pop_back();
|
||
|
}else{
|
||
1 year ago
|
ERR("WARNING! Trying to close out no menu?? Why are we doing this?")
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
std::pair<MenuType,std::string>Menu::GetMemoryLeakReportInfo(){
|
||
|
return {lastMenuTypeCreated,lastRegisteredComponent};
|
||
1 year ago
|
}
|
||
|
|
||
|
void Menu::CloseAllMenus(){
|
||
1 year ago
|
if(stack.size()>0){
|
||
|
stack.clear();
|
||
|
}else{
|
||
|
ERR("WARNING! Trying to close out no menu?? Why are we doing this?")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool Menu::IsMenuOpen(){
|
||
|
return stack.size()>0;
|
||
|
}
|
||
|
|
||
|
void Menu::CleanupAllMenus(){
|
||
12 months ago
|
for(auto&[key,value]:Menu::menus){
|
||
|
Menu*menu=value;
|
||
1 year ago
|
for(auto&componentKey:menu->components){
|
||
1 year ago
|
MenuComponent*component=componentKey.second;
|
||
|
component->Cleanup();
|
||
|
delete component;
|
||
|
}
|
||
|
menu->components.Reset();
|
||
|
menu->Cleanup();
|
||
|
delete menu;
|
||
|
}
|
||
|
Menu::menus.clear();
|
||
|
}
|
||
|
|
||
1 year ago
|
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);
|
||
|
}
|
||
12 months ago
|
}
|
||
12 months ago
|
|
||
|
|
||
|
void Menu::RecalculateComponentCount(){
|
||
|
componentCount=displayComponents.size()+buttons.size();
|
||
12 months ago
|
}
|
||
|
|
||
11 months ago
|
const MenuType Menu::GetType()const{
|
||
12 months ago
|
return type;
|
||
11 months ago
|
}
|
||
|
|
||
|
void Menu::LockInListeners(){
|
||
|
inventoryListeners.SetInitialized();
|
||
|
merchantInventoryListeners.SetInitialized();
|
||
11 months ago
|
}
|
||
|
|
||
|
void Menu::AddChapterListener(MenuComponent*component){
|
||
|
if(std::find(chapterListeners.begin(),chapterListeners.end(),component)!=chapterListeners.end()){
|
||
|
ERR("WARNING! Component "<<component->name<<" has already been added to the Chapter listener list! There should not be any duplicates!!")
|
||
|
}
|
||
|
chapterListeners.push_back(component);
|
||
11 months ago
|
}
|
||
|
|
||
|
|
||
11 months ago
|
MenuFuncData::MenuFuncData(Menu&menu,AiL*const game,MenuComponent*const component,ScrollableWindowComponent*const parentComponent)
|
||
11 months ago
|
:menu(menu),game(game),component(component),parentComponent(parentComponent){}
|