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.
268 lines
12 KiB
268 lines
12 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
|
||
1 year ago
|
#pragma once
|
||
1 year ago
|
#include "Item.h"
|
||
1 year ago
|
#include <stack>
|
||
1 year ago
|
#include "safemap.h"
|
||
|
#include "Theme.h"
|
||
|
#include "Attributable.h"
|
||
1 year ago
|
#include "olcUTIL_Geometry2D.h"
|
||
12 months ago
|
#include "olcPGEX_ViewPort.h"
|
||
1 year ago
|
|
||
11 months ago
|
class AiL;
|
||
1 year ago
|
class MenuComponent;
|
||
1 year ago
|
class ScrollableWindowComponent;
|
||
1 year ago
|
|
||
12 months ago
|
//Add a component to a menu using this macro. Follow-up with END at the end of it.
|
||
12 months ago
|
#define ADD(key,componentType) _AddComponent<componentType>(key,NEW componentType
|
||
12 months ago
|
#define END )
|
||
|
#define DEPTH ,
|
||
|
|
||
12 months ago
|
#define DEFAULT_DEPTH -999999
|
||
|
#define STARTING_DEPTH 999999
|
||
|
|
||
1 year ago
|
enum MenuType{
|
||
12 months ago
|
///////////////////////////////////////////////////////////
|
||
|
/*DO NOT REMOVE!!*/ENUM_START,///////////////////////////////
|
||
|
///////////////////////////////////////////////////////////
|
||
|
INVENTORY_CONSUMABLES,
|
||
1 year ago
|
CLASS_INFO,
|
||
|
CLASS_SELECTION,
|
||
|
MAIN_MENU,
|
||
1 year ago
|
OVERWORLD_LEVEL_SELECT,
|
||
1 year ago
|
ITEM_LOADOUT,
|
||
1 year ago
|
LEVEL_COMPLETE,
|
||
1 year ago
|
OVERWORLD_MENU,
|
||
|
CHARACTER_MENU,
|
||
12 months ago
|
INVENTORY,
|
||
11 months ago
|
MERCHANT,
|
||
|
BUY_ITEM,
|
||
|
SELL_ITEM,
|
||
11 months ago
|
BLACKSMITH,
|
||
11 months ago
|
CRAFT_ITEM,
|
||
11 months ago
|
CRAFT_CONSUMABLE,
|
||
11 months ago
|
CONSUMABLE_CRAFT_ITEM,
|
||
11 months ago
|
SAVE_FILE_NAME,
|
||
11 months ago
|
LOAD_GAME,
|
||
11 months ago
|
USER_ID,
|
||
1 year ago
|
///////////////////////////////////////////////////////////
|
||
|
/*DO NOT REMOVE!!*/ENUM_END////////////////////////////////
|
||
|
///////////////////////////////////////////////////////////
|
||
|
};
|
||
1 year ago
|
|
||
1 year ago
|
class Menu:public IAttributable{
|
||
12 months ago
|
static void InitializeConsumableInventoryWindow();
|
||
|
static void InitializeClassInfoWindow();
|
||
|
static void InitializeClassSelectionWindow();
|
||
|
static void InitializeMainMenuWindow();
|
||
|
static void InitializeOverworldMapLevelWindow();
|
||
|
static void InitializeItemLoadoutWindow();
|
||
|
static void InitializeLevelCompleteWindow();
|
||
|
static void InitializeOverworldMenuWindow();
|
||
|
static void InitializeCharacterMenuWindow();
|
||
|
static void InitializeInventoryWindow();
|
||
11 months ago
|
static void InitializeMerchantWindow();
|
||
|
static void InitializeBuyItemWindow();
|
||
|
static void InitializeSellItemWindow();
|
||
11 months ago
|
static void InitializeBlacksmithCraftingWindow();
|
||
11 months ago
|
static void InitializeCraftItemWindow();
|
||
11 months ago
|
static void InitializeConsumableCraftingWindow();
|
||
11 months ago
|
static void InitializeConsumableCraftItemWindow();
|
||
11 months ago
|
static void InitializeSaveFileWindow();
|
||
11 months ago
|
static void InitializeLoadGameWindow();
|
||
11 months ago
|
static void InitializeUserIDWindow();
|
||
12 months ago
|
|
||
11 months ago
|
friend class AiL;
|
||
1 year ago
|
friend class ItemInfo;
|
||
11 months ago
|
friend class EntityStats;
|
||
1 year ago
|
|
||
1 year ago
|
float buttonHoldTime=0;
|
||
1 year ago
|
vi2d selection={-1,-1};
|
||
1 year ago
|
vi2d lastActiveMousePos={};
|
||
11 months ago
|
int componentCount=0;
|
||
1 year ago
|
|
||
|
MenuComponent*draggingComponent=nullptr;
|
||
12 months ago
|
ViewPort window;
|
||
1 year ago
|
static safemap<ITCategory,std::vector<MenuComponent*>>inventoryListeners; //All menu components that care about inventory updates subscribe to this list indirectly (See Menu::AddInventoryListener()).
|
||
11 months ago
|
static safemap<ITCategory,std::vector<MenuComponent*>>merchantInventoryListeners; //All menu components that care about merchant inventory updates subscribe to this list indirectly (See Menu::AddMerchantInventoryListener()).
|
||
12 months ago
|
static std::vector<MenuComponent*>equipStatListeners; //All menu components that care about stat/equip updates subscribe to this list indirectly (See Menu::AddStatListener()).
|
||
11 months ago
|
static std::vector<MenuComponent*>chapterListeners; //All menu components that care about story chapter updates subscribe to this list indirectly (See Menu::AddChapterListener()).
|
||
1 year ago
|
public:
|
||
1 year ago
|
//The constructor is private. Use CreateMenu() instead!
|
||
|
Menu()=default;
|
||
1 year ago
|
~Menu();
|
||
12 months ago
|
//DO NOT USE DIRECTLY! You should be utilizing the ADD macro for adding components.
|
||
|
template<class T>
|
||
12 months ago
|
T*_AddComponent(std::string componentKey,T*component,int depth=DEFAULT_DEPTH){
|
||
12 months ago
|
component->parentMenu=type;
|
||
|
if(depth==DEFAULT_DEPTH){
|
||
|
component->depth=STARTING_DEPTH-componentCount;
|
||
|
}else{
|
||
|
component->depth=depth;
|
||
|
}
|
||
|
if(component->selectable){
|
||
|
buttons.Unlock();
|
||
|
if(buttons.count(int(component->rect.pos.y))){
|
||
|
buttons.at(int(component->rect.pos.y)).push_back(component);
|
||
|
}else{
|
||
|
buttons[int(component->rect.pos.y)].push_back(component);
|
||
|
}
|
||
|
if(component->selectableViaKeyboard){
|
||
|
keyboardButtons.Unlock();
|
||
|
if(keyboardButtons.count(int(component->rect.pos.y))){
|
||
|
keyboardButtons.at(int(component->rect.pos.y)).push_back(component);
|
||
|
}else{
|
||
|
keyboardButtons[int(component->rect.pos.y)].push_back(component);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//We must lock the values before calling sort. Sort seems to try and create new accesses.
|
||
|
buttons.SetInitialized();
|
||
|
keyboardButtons.SetInitialized();
|
||
|
|
||
|
//We make an assumption that menu components are supposed to be in left-to-right order. Sometimes we may add things out-of-order, so this fixes the problem by sorting the items afterwards.
|
||
|
std::sort(buttons[int(component->rect.pos.y)].begin(),buttons[int(component->rect.pos.y)].end(),[](auto c1,auto c2){
|
||
|
return c1->GetPos().x<c2->GetPos().x;
|
||
|
});
|
||
|
if(keyboardButtons.count(int(component->rect.pos.y))){ //Keyboard buttons may not necessarily contain this key...Let's be sure.
|
||
|
std::sort(keyboardButtons[int(component->rect.pos.y)].begin(),keyboardButtons[int(component->rect.pos.y)].end(),[](auto c1,auto c2){
|
||
|
return c1->GetPos().x<c2->GetPos().x;
|
||
|
});
|
||
|
}
|
||
|
}else{
|
||
|
displayComponents.push_back(component);
|
||
|
}
|
||
|
|
||
|
RecalculateComponentCount();
|
||
|
|
||
|
if(components.count(componentKey)){
|
||
|
ERR("WARNING! Key "<<componentKey<<" for this sub-menu already exists! Key names must be unique!")
|
||
|
}
|
||
|
component->name=componentKey;
|
||
|
components.Unlock(); //It's possible we can add a component later on, so we will make sure we remove the lock first.
|
||
|
components[componentKey]=component;
|
||
|
components.SetInitialized();
|
||
|
lastRegisteredComponent=componentKey;
|
||
|
std::erase_if(Menu::unhandledComponents,[&](auto b1){return b1==component;});
|
||
|
|
||
|
return component;
|
||
|
}
|
||
11 months ago
|
void Update(AiL*game);
|
||
|
void Draw(AiL*game);
|
||
11 months ago
|
static void InitializeMenuListenerCategory(const std::string&category);
|
||
1 year ago
|
static void InitializeMenus();
|
||
11 months ago
|
static void LockInListeners();
|
||
1 year ago
|
static void OpenMenu(MenuType menu,bool cover=true);
|
||
1 year ago
|
static void CloseMenu();
|
||
1 year ago
|
static void CloseAllMenus();
|
||
1 year ago
|
static void CleanupAllMenus();
|
||
1 year ago
|
static std::vector<Menu*>stack;
|
||
1 year ago
|
static std::string themeSelection;
|
||
1 year ago
|
static safeunorderedmap<std::string,Theme>themes;
|
||
12 months ago
|
static std::vector<MenuComponent*>unhandledComponents; //This list contains MenuComponents that are created and haven't been assigned via _AddComponent. If we get to the end of menu initialization and there are any components in this vector, we have leaked memory and will report this.
|
||
1 year ago
|
static const vf2d CENTERED;
|
||
1 year ago
|
static bool IsMenuOpen();
|
||
11 months ago
|
const MenuType GetType()const;
|
||
1 year ago
|
safemap<std::string,MenuComponent*>components; //A friendly way to interrogate any component we are interested in.
|
||
1 year ago
|
std::vector<MenuComponent*>displayComponents; //Components that are only for displaying purposes.
|
||
1 year ago
|
static std::map<MenuType,Menu*>menus;
|
||
|
vf2d pos; //Specify the upper-left corner of the window. Using CENTERED will always put this where the upper-left corner would center the window.
|
||
|
vf2d size; //Size in tiles (24x24), every menu will be tile-based
|
||
1 year ago
|
safemap<int/*Y*/,std::vector<MenuComponent*>>buttons; //Buttons are stored in rows followed by their column order.
|
||
|
safemap<int/*Y*/,std::vector<MenuComponent*>>keyboardButtons; //Button ordered storage for keyboard/menu
|
||
1 year ago
|
|
||
1 year ago
|
static Theme&GetCurrentTheme();
|
||
1 year ago
|
bool UsingMouseNavigation();
|
||
|
void SetMouseNavigation(bool mouseNavigation);
|
||
11 months ago
|
static void InventorySlotsUpdated(ITCategory cat); //Called whenever the player's inventory gets modified.
|
||
|
static void MerchantInventorySlotsUpdated(ITCategory cat); //Called whenever a traveling merchant's inventory item gets updated.
|
||
1 year ago
|
static void AddInventoryListener(MenuComponent*component,ITCategory category); //Adds a component to be in a given listener category.
|
||
11 months ago
|
static void AddMerchantInventoryListener(MenuComponent*component,ITCategory category); //Adds a component to be in a given listener category.
|
||
12 months ago
|
static void AddEquipStatListener(MenuComponent*component); //Adds a component to be in an equip stat listener. Will receive updates whenever stats are updated via equips.
|
||
11 months ago
|
static void AddChapterListener(MenuComponent*component); //Adds a component to be in a chapter listener. Will receive updates anytime the chapter in-game changes.
|
||
1 year ago
|
vf2d center();
|
||
1 year ago
|
//Returns the last menu type created and last registered component, in case a component is detected as memory leaking, provides this information to each component for safety.
|
||
|
static std::pair<MenuType,std::string>GetMemoryLeakReportInfo();
|
||
1 year ago
|
virtual void Cleanup();
|
||
1 year ago
|
|
||
|
static void DrawThemedWindow(vf2d menuPos,vf2d size,Pixel renderColor=WHITE);
|
||
1 year ago
|
|
||
|
//X (0-2), Y (0-2) for specific 9-patch tile (tiled version).
|
||
|
static Renderable&GetPatchPart(int x,int y);
|
||
12 months ago
|
void RecalculateComponentCount();
|
||
1 year ago
|
private:
|
||
1 year ago
|
Menu(vf2d pos,vf2d size);
|
||
1 year ago
|
static MenuType lastMenuTypeCreated;
|
||
|
static std::string lastRegisteredComponent;
|
||
11 months ago
|
void HoverMenuSelect(AiL*game);
|
||
|
void MenuSelect(AiL*game);
|
||
|
void CheckClickAndPerformMenuSelect(AiL*game);
|
||
1 year ago
|
//Mandatory before any menu operations! This creates and sets up the menu in memory.
|
||
|
static Menu*CreateMenu(MenuType type,vf2d pos,vf2d size);
|
||
1 year ago
|
|
||
11 months ago
|
void KeyboardButtonNavigation(AiL*game,vf2d menuPos);
|
||
|
static void DrawScaledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor);
|
||
|
static void DrawTiledWindowBackground(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor);
|
||
|
static void DrawScaledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor);
|
||
|
static void DrawTiledWindowBorder(AiL*game,vf2d menuPos,vf2d size,Pixel renderColor);
|
||
1 year ago
|
|
||
1 year ago
|
//This triggers if we use a keyboard/controller input to try and select some off-screen menu item. We should ideally follow the menu cursor.
|
||
|
bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton);
|
||
|
|
||
1 year ago
|
Pixel GetRenderColor();
|
||
12 months ago
|
MenuType type;
|
||
12 months ago
|
|
||
1 year ago
|
static bool MOUSE_NAVIGATION;
|
||
1 year ago
|
bool cover; //A black cover for when a menu pops up to fade out the stuff behind it.
|
||
1 year ago
|
};
|
||
|
|
||
1 year ago
|
template<typename T>
|
||
|
T*Component(MenuType menu,std::string componentName){
|
||
11 months ago
|
T*tmp=DYNAMIC_CAST<T*>(Menu::menus[menu]->components[componentName]);
|
||
|
return tmp;
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
struct MenuFuncData{
|
||
|
Menu&menu;
|
||
11 months ago
|
AiL*const game;
|
||
11 months ago
|
MenuComponent*const component;
|
||
|
ScrollableWindowComponent*const parentComponent=nullptr;
|
||
11 months ago
|
MenuFuncData(Menu&menu,AiL*const game,MenuComponent*const component,ScrollableWindowComponent*const parentComponent=nullptr);
|
||
1 year ago
|
};
|
||
|
|
||
12 months ago
|
using MenuFunc=std::function<bool(MenuFuncData)>;
|