From 99c24b9c378386f9e9b8acec2433f347e2f9552b Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 7 Oct 2023 15:47:26 -0500 Subject: [PATCH] Setup internal menu components so they know what button was clicked on a menu function. Compacted the on click menu function for menu buttons. Refactored some item convenience functions. --- Crawler/Attributable.h | 12 ------- Crawler/Crawler.vcxproj | 3 +- Crawler/Crawler.vcxproj.filters | 9 ++---- Crawler/DEFINES.h | 4 +-- Crawler/InventoryWindow.cpp | 21 ++++++++++--- Crawler/Item.cpp | 56 +++++++++++++++++++++++++++++++-- Crawler/Item.h | 21 +++++++++---- Crawler/Menu.cpp | 2 +- Crawler/Menu.h | 22 ++++++++++++- Crawler/MenuComponent.cpp | 2 +- Crawler/MenuComponent.h | 7 +---- Crawler/MenuIconButton.h | 2 +- Crawler/MenuItemButton.h | 13 +++++--- Crawler/MenuType.h | 16 ---------- Crawler/MonsterAttribute.h | 1 - Crawler/Player.h | 12 +++---- Crawler/TestMenu.cpp | 12 +++---- Crawler/TestSubMenu.cpp | 25 +++++++-------- Crawler/Version.h | 2 +- 19 files changed, 148 insertions(+), 94 deletions(-) delete mode 100644 Crawler/MenuType.h diff --git a/Crawler/Attributable.h b/Crawler/Attributable.h index b31d7f37..3b90cbb1 100644 --- a/Crawler/Attributable.h +++ b/Crawler/Attributable.h @@ -37,16 +37,4 @@ protected: } return std::get(attributes[a]); }; - inline std::vector&GetIntVec(Attribute a){ - if(attributes.count(a)==0){ - attributes[a]=std::vector{}; - } - return std::get>(attributes[a]); - }; - inline std::vector&GetStringVec(Attribute a){ - if(attributes.count(a)==0){ - attributes[a]=std::vector{}; - } - return std::get>(attributes[a]); - }; }; \ No newline at end of file diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index eb466818..42db6863 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -273,14 +273,13 @@ + - - diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index 2cef1dea..70b8f184 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -156,18 +156,12 @@ Header Files - - Header Files - Header Files Header Files\Interface - - Header Files\Interface - Header Files\Interface @@ -186,6 +180,9 @@ Header Files\Interface + + Header Files\Interface + diff --git a/Crawler/DEFINES.h b/Crawler/DEFINES.h index f2ba7514..781681be 100644 --- a/Crawler/DEFINES.h +++ b/Crawler/DEFINES.h @@ -13,12 +13,12 @@ #define INCLUDE_STRATEGY_ID_DATA extern safemapSTRATEGY_ID_DATA; #define INCLUDE_TILE_ANIMATION_DATA extern std::map>>TILE_ANIMATION_DATA; #define INCLUDE_GFX extern safemapGFX; -#define INCLUDE_ITEM_DATA extern safemapITEM_DATA; +#define INCLUDE_ITEM_DATA extern safemapITEM_DATA; #define INCLUDE_ITEM_CATEGORIES extern safemap>ITEM_CATEGORIES; #define ACCESS_PLAYER Player*p=game->GetPlayer(); -#define VARIANTS float,int,std::string,bool,vf2d,std::vector,std::vector +#define VARIANTS float,int,std::string,bool,vf2d #define INFINITE 999999 diff --git a/Crawler/InventoryWindow.cpp b/Crawler/InventoryWindow.cpp index d19ea625..91b6388c 100644 --- a/Crawler/InventoryWindow.cpp +++ b/Crawler/InventoryWindow.cpp @@ -1,10 +1,10 @@ #pragma once #include "Crawler.h" -#include "Menu.h" #include "DEFINES.h" #include "olcPixelGameEngine.h" #include "safemap.h" #include "Item.h" +#include "MenuItemButton.h" INCLUDE_GFX typedef Attribute A; @@ -14,11 +14,22 @@ const Menu Menu::InitializeInventoryWindow(){ constexpr int invHeight=3; constexpr int totalItemSlots=invWidth*invHeight; - Menu inventoryWindow(CENTERED,{24*invWidth,24*(invHeight+1)}); + constexpr int itemSpacing=8; + constexpr int buttonSize=24; + constexpr int totalSpacing=buttonSize+itemSpacing; - for(int i=0;iITEM_DATA; safemapITEM_SCRIPTS; safemap>ITEM_CATEGORIES; +Item Item::BLANK; +std::mapInventory::_inventory; +std::map>Inventory::sortedInv; ItemInfo::ItemInfo() :customProps({nullptr,nullptr}),img(nullptr){} @@ -104,10 +107,13 @@ void ItemInfo::InitializeScripts(){ std::cout<=_inventory.at(it).Amt()){ @@ -176,4 +187,43 @@ bool Inventory::SwapItems(IT it,IT it2){ auto index2=std::find(inv.begin(),inv.end(),it2); std::swap(*index1,*index2); return true; +} + +uint32_t Item::Amt(){ + return amt; +}; +std::string Item::Name(){ + return it->Name(); +}; +std::string Item::Description(){ + return it->Description(); +}; +ITCategory Item::Category(){ + return it->Category(); +}; +Decal*Item::Decal(){ + return it->Decal(); +}; +ItemScript&Item::OnUseAction(){ + return it->OnUseAction(); +}; + +std::string ItemInfo::Name(){ + return name; +}; +std::string ItemInfo::Description(){ + return description; +}; +ITCategory ItemInfo::Category(){ + return category; +}; +Decal*ItemInfo::Decal(){ + return img; +}; +ItemScript&ItemInfo::OnUseAction(){ + return ITEM_SCRIPTS.at(useFunc); +}; + +bool Item::IsBlank(){ + return amt==0||it==nullptr; } \ No newline at end of file diff --git a/Crawler/Item.h b/Crawler/Item.h index 879b7ea3..171db194 100644 --- a/Crawler/Item.h +++ b/Crawler/Item.h @@ -8,16 +8,20 @@ class Crawler; class ItemInfo; +class ItemProps; typedef std::string IT; typedef std::string ITCategory; +typedef std::function ItemScript; + class Item{ friend class Inventory; private: uint32_t amt; ItemInfo*it; public: + Item(); Item(uint32_t amt,IT item); uint32_t Amt(); std::string Name(); @@ -25,16 +29,18 @@ public: ITCategory Category(); Decal*Decal(); ItemScript&OnUseAction(); + bool IsBlank(); + static Item BLANK; }; class Inventory{ public: - static void AddItem(IT it,int amt=1); + static void AddItem(IT it,uint32_t amt=1); static uint32_t GetItemCount(IT it); - static Item GetItem(IT it)const; + static Item GetItem(IT it); //Auto-executes its use function and removes the amt specified from the inventory. Multiple amounts will cause the item to execute its useFunc multiple times. - static void UseItem(IT it,int amt=1); - static void RemoveItem(IT it,int amt=1); + static void UseItem(IT it,uint32_t amt=1); + static void RemoveItem(IT it,uint32_t amt=1); static std::vector&get(ITCategory itemCategory); static bool SwapItems(IT it,IT it2); @@ -56,8 +62,6 @@ public: std::string GetStringProp(std::string prop); }; -typedef std::function ItemScript; - class ItemInfo{ friend class Inventory; std::string name; @@ -75,6 +79,11 @@ private: public: static void InitializeItems(); ItemInfo(); + std::string Name(); + std::string Description(); + ITCategory Category(); + Decal*Decal(); + ItemScript&OnUseAction(); /* For the useFunc, return true if the item can be used, false otherwise. */ diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index b4457fa5..649bbbf4 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -65,7 +65,7 @@ void Menu::HoverMenuSelect(Crawler*game){ void Menu::MenuSelect(Crawler*game){ if(selection==vi2d{-1,-1})return; - buttons[selection.y][selection.x]->onClick(*this,game); + buttons[selection.y][selection.x]->onClick(MenuFuncData{*this,game,buttons[selection.y][selection.x]}); 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. diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 6fd5ec51..d8283d50 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -1,12 +1,24 @@ #pragma once -#include "MenuComponent.h" #include "olcPixelGameEngine.h" #include #include "safemap.h" #include "Theme.h" #include "Attributable.h" +#include "olcUTIL_Geometry2D.h" +#include class Crawler; +class MenuComponent; + +enum MenuType{ + TEST, + TEST_2, + INVENTORY, + + /////////////////////////////////////////////////////////// + /*DO NOT REMOVE!!*/ENUM_END//////////////////////////////// + /////////////////////////////////////////////////////////// +}; class Menu:IAttributable{ friend class Crawler; @@ -36,6 +48,7 @@ public: static std::string themeSelection; static safeunorderedmapthemes; static const vf2d CENTERED; + private: void HoverMenuSelect(Crawler*game); void MenuSelect(Crawler*game); @@ -55,3 +68,10 @@ private: Pixel GetRenderColor(); }; +struct MenuFuncData{ + Menu&menu; + Crawler*game; + MenuComponent*component; +}; + +typedef std::function MenuFunc; \ No newline at end of file diff --git a/Crawler/MenuComponent.cpp b/Crawler/MenuComponent.cpp index f56c6bbf..19375c8b 100644 --- a/Crawler/MenuComponent.cpp +++ b/Crawler/MenuComponent.cpp @@ -1,5 +1,5 @@ #include "Crawler.h" -#include "Menu.h" +#include "MenuComponent.h" MenuComponent::MenuComponent(geom2d::rectrect,std::string label,MenuFunc onClick,bool selectable) :rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable){} diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h index b3f2b842..9b6d00ea 100644 --- a/Crawler/MenuComponent.h +++ b/Crawler/MenuComponent.h @@ -1,10 +1,5 @@ #pragma once -#include "olcUTIL_Geometry2D.h" -#include "MenuType.h" -#include - -class Menu; -class Crawler; +#include "Menu.h" class MenuComponent{ friend class Menu; diff --git a/Crawler/MenuIconButton.h b/Crawler/MenuIconButton.h index a1f28dce..bb844bc9 100644 --- a/Crawler/MenuIconButton.h +++ b/Crawler/MenuIconButton.h @@ -6,7 +6,7 @@ INCLUDE_game class MenuIconButton:public MenuComponent{ -private: +protected: Decal*icon; public: inline MenuIconButton(geom2d::rectrect,Decal*icon,MenuFunc onClick) diff --git a/Crawler/MenuItemButton.h b/Crawler/MenuItemButton.h index 2ea2e26c..aa1d77de 100644 --- a/Crawler/MenuItemButton.h +++ b/Crawler/MenuItemButton.h @@ -3,21 +3,24 @@ #include "DEFINES.h" #include "Crawler.h" #include "Item.h" +#include "safemap.h" INCLUDE_game +INCLUDE_ITEM_DATA class MenuItemButton:public MenuIconButton{ private: - Decal*icon; + std::vector&invRef; + int inventoryIndex=0; public: - inline MenuItemButton(geom2d::rectrect,Decal*icon,MenuFunc onClick) - :MenuIconButton(rect,icon,onClick){} + inline MenuItemButton(geom2d::rectrect,std::vector&invRef,int invIndex,MenuFunc onClick) + :MenuIconButton(rect,ITEM_DATA.at(invRef[invIndex]).Decal(),onClick),invRef(invRef),inventoryIndex(invIndex){} protected: virtual void inline Update(Crawler*game)override{ - MenuComponent::Update(game); + MenuIconButton::Update(game); } virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{ - MenuComponent::Draw(game,parentPos,focused); + MenuIconButton::Draw(game,parentPos,focused); game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2,{1,1},focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F); } }; \ No newline at end of file diff --git a/Crawler/MenuType.h b/Crawler/MenuType.h deleted file mode 100644 index f21a463e..00000000 --- a/Crawler/MenuType.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -class Menu; -class Crawler; - -typedef std::function MenuFunc; - -enum MenuType{ - TEST, - TEST_2, - INVENTORY, - - /////////////////////////////////////////////////////////// - /*DO NOT REMOVE!!*/ENUM_END//////////////////////////////// - /////////////////////////////////////////////////////////// -}; \ No newline at end of file diff --git a/Crawler/MonsterAttribute.h b/Crawler/MonsterAttribute.h index 5fb10cad..aadcf67d 100644 --- a/Crawler/MonsterAttribute.h +++ b/Crawler/MonsterAttribute.h @@ -28,5 +28,4 @@ enum class Attribute{ JUMP_TOWARDS_PLAYER, HITS_UNTIL_DEATH, //When this is set, it is reduced by 1 each time the monster is hit. INDEXED_THEME, - INDEXED_ITEMS, }; \ No newline at end of file diff --git a/Crawler/Player.h b/Crawler/Player.h index e51999aa..ce343dd1 100644 --- a/Crawler/Player.h +++ b/Crawler/Player.h @@ -21,12 +21,12 @@ struct CastInfo{ struct Player{ friend class Crawler; friend class sig::Animation; - friend class Warrior; - friend class Thief; - friend class Ranger; - friend class Trapper; - friend class Wizard; - friend class Witch; + friend struct Warrior; + friend struct Thief; + friend struct Ranger; + friend struct Trapper; + friend struct Wizard; + friend struct Witch; friend class State_GameRun; private: int hp="Player.BaseHealth"_I,maxhp=hp; diff --git a/Crawler/TestMenu.cpp b/Crawler/TestMenu.cpp index 4c035619..e2ec5a10 100644 --- a/Crawler/TestMenu.cpp +++ b/Crawler/TestMenu.cpp @@ -1,21 +1,21 @@ #include "Crawler.h" -#include "Menu.h" +#include "MenuComponent.h" const Menu Menu::InitializeTestMenu(){ Menu testMenu(CENTERED,{24*8,24*6}); - MenuFunc quitWindow=[](Menu&menu,Crawler*game){ - menu.stack.clear(); + MenuFunc quitWindow=[](MenuFuncData data){ + data.menu.stack.clear(); }; testMenu.AddComponent("Close",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Close",quitWindow)); - MenuFunc doNothing=[](Menu&menu,Crawler*game){}; + MenuFunc doNothing=[](MenuFuncData data){}; testMenu.AddComponent("Test",new MenuComponent({{24*4,24*1},{24*3,24*1}},"Test",doNothing)); - MenuFunc HurtPlayer=[](Menu&menu,Crawler*game){ - game->GetPlayer()->Hurt(20,game->GetPlayer()->OnUpperLevel(),game->GetPlayer()->GetZ()); + MenuFunc HurtPlayer=[](MenuFuncData data){ + data.game->GetPlayer()->Hurt(20,data.game->GetPlayer()->OnUpperLevel(),data.game->GetPlayer()->GetZ()); }; testMenu.AddComponent("Hurt Player",new MenuComponent({{24*4,24*3},{24*3,24*1}},"Hurt Player",HurtPlayer)); diff --git a/Crawler/TestSubMenu.cpp b/Crawler/TestSubMenu.cpp index 4bfb28dc..ec4a5372 100644 --- a/Crawler/TestSubMenu.cpp +++ b/Crawler/TestSubMenu.cpp @@ -1,5 +1,4 @@ #include "Crawler.h" -#include "Menu.h" #include "DEFINES.h" #include "olcPixelGameEngine.h" #include "safemap.h" @@ -12,8 +11,8 @@ typedef Attribute A; const Menu Menu::InitializeTestSubMenu(){ Menu testSubMenu({30,30},{24*4,24*5}); - MenuFunc goBack=[](Menu&menu,Crawler*game){ - menu.stack.pop_back(); + MenuFunc goBack=[](MenuFuncData data){ + data.menu.stack.pop_back(); }; testSubMenu.AddComponent("BACK",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Go Back",goBack)); @@ -27,17 +26,17 @@ const Menu Menu::InitializeTestSubMenu(){ index++; } - MenuFunc themePrev=[](Menu&menu,Crawler*game){ + MenuFunc themePrev=[](MenuFuncData data){ bool found=false; - menu.I(A::INDEXED_THEME)--; - if(menu.I(A::INDEXED_THEME)<0){ - menu.I(A::INDEXED_THEME)=themes.size()-1; + data.menu.I(A::INDEXED_THEME)--; + if(data.menu.I(A::INDEXED_THEME)<0){ + data.menu.I(A::INDEXED_THEME)=themes.size()-1; } int index=0; for(auto&theme:Menu::themes){ - if(index==menu.I(A::INDEXED_THEME)){ + if(index==data.menu.I(A::INDEXED_THEME)){ Menu::themeSelection=theme.displayName; - ((MenuLabel*)(menu.components["THEME_DISPLAY"]))->SetLabel("Theme\n"+Menu::themes[themeSelection].GetThemeName()); + ((MenuLabel*)(data.menu.components["THEME_DISPLAY"]))->SetLabel("Theme\n"+Menu::themes[themeSelection].GetThemeName()); break; } index++; @@ -48,13 +47,13 @@ const Menu Menu::InitializeTestSubMenu(){ testSubMenu.AddComponent("THEME_DISPLAY",new MenuLabel({{24*0.5,24*3},{24*3,24*1}},"Theme\n"+Menu::themes[themeSelection].GetThemeName())); - MenuFunc themeNext=[](Menu&menu,Crawler*game){ - menu.I(A::INDEXED_THEME)=(size_t(menu.I(A::INDEXED_THEME))+1)%themes.size(); + MenuFunc themeNext=[](MenuFuncData data){ + data.menu.I(A::INDEXED_THEME)=(size_t(data.menu.I(A::INDEXED_THEME))+1)%themes.size(); int index=0; for(auto&theme:Menu::themes){ - if(index==menu.I(A::INDEXED_THEME)){ + if(index==data.menu.I(A::INDEXED_THEME)){ Menu::themeSelection=theme.displayName; - ((MenuLabel*)(menu.components["THEME_DISPLAY"]))->SetLabel("Theme\n"+Menu::themes[themeSelection].GetThemeName()); + ((MenuLabel*)(data.menu.components["THEME_DISPLAY"]))->SetLabel("Theme\n"+Menu::themes[themeSelection].GetThemeName()); break; } index++; diff --git a/Crawler/Version.h b/Crawler/Version.h index 1fbfd29a..51bed69b 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -2,7 +2,7 @@ #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 0 -#define VERSION_BUILD 1814 +#define VERSION_BUILD 1839 #define stringify(a) stringify_(a) #define stringify_(a) #a