From e9952ca1bec02eb600c1f3961bc122826a359e7e Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Wed, 18 Oct 2023 18:07:10 +0000 Subject: [PATCH] An inventory scrollable component window should not be the same as an inventory window as they would layout things differently. Separated into proper hierarchy. Co-authored-by: sigonasr2 --- Crawler/Crawler.vcxproj | 1 + Crawler/Crawler.vcxproj.filters | 3 + Crawler/InventoryScrollableWindowComponent.h | 86 ++++++++++++++++++++ Crawler/InventoryWindow.cpp | 4 +- Crawler/MenuComponent.h | 1 + Crawler/ScrollableWindowComponent.h | 77 +----------------- 6 files changed, 95 insertions(+), 77 deletions(-) create mode 100644 Crawler/InventoryScrollableWindowComponent.h diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index ce25bf43..287a0a9e 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -296,6 +296,7 @@ + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index c61829dc..8a4137f5 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -192,6 +192,9 @@ Header Files\Interface + + Header Files\Interface + diff --git a/Crawler/InventoryScrollableWindowComponent.h b/Crawler/InventoryScrollableWindowComponent.h new file mode 100644 index 00000000..d8000d9c --- /dev/null +++ b/Crawler/InventoryScrollableWindowComponent.h @@ -0,0 +1,86 @@ +#pragma once +#include "Menu.h" +#include "MenuComponent.h" +#include "MenuItemButton.h" +#include "Crawler.h" +#include "ScrollableWindowComponent.h" + +typedef Attribute A; + +class InventoryScrollableWindowComponent:public ScrollableWindowComponent{ +protected: +public: + inline InventoryScrollableWindowComponent(MenuType parent,geom2d::rectrect,Decal*icon,MenuFunc onClick) + :ScrollableWindowComponent(parent,rect,icon,onClick){} +protected: + virtual inline void RemoveButton(MenuComponent*button){ + std::vector&buttonList=Menu::menus[button->parentMenu]->buttons.at(button->GetPos().y); + std::vector&keyboardButtonList=Menu::menus[button->parentMenu]->keyboardButtons.at(button->GetPos().y); + size_t removedCount=0; + removedCount+=std::erase(buttonList,button); + removedCount+=std::erase(keyboardButtonList,button); + if(removedCount!=2){ + std::cout<<"WARNING! Attempted to remove buttons from button listing, but not found!"; + throw; + } + if(buttonList.size()==0){ + if(!Menu::menus[button->parentMenu]->buttons.erase(button->GetPos().y)){ + std::cout<<"WARNING! Attempted to erase key "<GetPos().y<<" from button map, but the list still exists!"; + throw; + } + } + if(keyboardButtonList.size()==0){ + if(!Menu::menus[button->parentMenu]->keyboardButtons.erase(button->GetPos().y)){ + std::cout<<"WARNING! Attempted to erase key "<GetPos().y<<" from button map, but the list still exists!"; + throw; + } + } + } + void inline RemoveEmptySlots(){ + //Algorithm will iterate through all slots, finding blank slots. Each time a blank slot is found, all items will shift over by one, and then the last item will be removed. Repeat until all slots iterated through. + for(int i=0;iUpdate(game); //We have to call update to update the validation state. + //HACK ALERT!! This only gets called on inventories...And only on inventory items, which would have the valid flag set. We only care about components that are inventory slots. + if(!button->valid){ + for(int j=i;jparentMenu]->components.at(components[j]->name)=components[j+1]; + components[j]=components[j+1]; + } + MenuComponent*lastButton=Menu::menus[components[components.size()-1]->parentMenu]->components.at(components[components.size()-1]->name); + //Now we have to fix up the keyboard button list. + RemoveButton(lastButton); + Menu::menus[components[components.size()-1]->parentMenu]->components.erase(components[components.size()-1]->name); + //Now delete the last slot. + components.erase(components.end()-1); + i--; //Subtract one from the index so we don't accidently skip slots. + } + } + bounds=CalculateBounds(); //Recalculate the bounds as it's possible the width/height of the component has changed. + } + virtual void OnInventorySlotsUpdate(ITCategory cat)override{ + std::vector&inv=Inventory::get(cat); + //We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory. + if(components.size()UseItem(); + }; + + MenuItemButton*button=new MenuItemButton{parentMenu,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc}; + AddComponent(Menu::menus[parentMenu],"item"+std::to_string(itemIndex),button); + }else + if(components.size()>inv.size()){ //There are empty spots, so let's clean up. + RemoveEmptySlots(); + } + } +}; \ No newline at end of file diff --git a/Crawler/InventoryWindow.cpp b/Crawler/InventoryWindow.cpp index 3ff4fa20..4113765b 100644 --- a/Crawler/InventoryWindow.cpp +++ b/Crawler/InventoryWindow.cpp @@ -5,7 +5,7 @@ #include "Item.h" #include "MenuItemButton.h" #include "MenuLabel.h" -#include "ScrollableWindowComponent.h" +#include "InventoryScrollableWindowComponent.h" typedef Attribute A; @@ -21,7 +21,7 @@ void Menu::InitializeInventoryWindow(){ Menu*inventoryWindow=CreateMenu(INVENTORY,CENTERED,windowSize); - ScrollableWindowComponent*inventory=new ScrollableWindowComponent(INVENTORY,{{1,0},{windowSize.x,float(totalSpacing*3-itemSpacing)}},nullptr,[](MenuFuncData data){}); + InventoryScrollableWindowComponent*inventory=new InventoryScrollableWindowComponent(INVENTORY,{{1,0},{windowSize.x,float(totalSpacing*3-itemSpacing)}},nullptr,[](MenuFuncData data){}); inventoryWindow->AddComponent("inventory",inventory); Menu::AddInventoryListener(inventory,"Consumables"); Menu::AddInventoryListener(inventory,"Equipment"); diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h index 0c8e45b6..6e668d52 100644 --- a/Crawler/MenuComponent.h +++ b/Crawler/MenuComponent.h @@ -5,6 +5,7 @@ class MenuComponent:IAttributable{ friend class Menu; friend class MenuItemButton; friend class ScrollableWindowComponent; + friend class InventoryScrollableWindowComponent; MenuType menuDest; private: float hoverEffect=0; diff --git a/Crawler/ScrollableWindowComponent.h b/Crawler/ScrollableWindowComponent.h index d0fb836f..cd0cc4ea 100644 --- a/Crawler/ScrollableWindowComponent.h +++ b/Crawler/ScrollableWindowComponent.h @@ -17,7 +17,7 @@ protected: float scrollBarTop=0; bool scrollBarSelected=false; float scrollBarHoverTime=0; -private: +protected: inline bool OnScreen(MenuComponent*component){ return geom2d::overlaps(rect,geom2d::rect{component->rect.pos+V(A::SCROLL_OFFSET)+vf2d{2,2},component->rect.size-vf2d{2,2}}); } @@ -30,7 +30,7 @@ public: virtual inline ~ScrollableWindowComponent(){ } -private: +protected: virtual inline void AfterCreate()override{ upButton=new MenuComponent(parentMenu,{vf2d{rect.size.x-12,0},{12,12}},"^",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y+="ThemeGlobal.MenuButtonScrollSpeed"_I;},true,false); downButton=new MenuComponent(parentMenu,{rect.size-vf2d{12,12},{12,12}},"v",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y-="ThemeGlobal.MenuButtonScrollSpeed"_I;},true,false); @@ -189,77 +189,4 @@ public: V(A::SCROLL_OFFSET).y+=rect.size.y/2; return true; }; - virtual inline size_t ComponentCount(){ - return components.size(); - } - virtual inline void RemoveButton(MenuComponent*button){ - std::vector&buttonList=Menu::menus[button->parentMenu]->buttons.at(button->GetPos().y); - std::vector&keyboardButtonList=Menu::menus[button->parentMenu]->keyboardButtons.at(button->GetPos().y); - size_t removedCount=0; - removedCount+=std::erase(buttonList,button); - removedCount+=std::erase(keyboardButtonList,button); - if(removedCount!=2){ - std::cout<<"WARNING! Attempted to remove buttons from button listing, but not found!"; - throw; - } - if(buttonList.size()==0){ - if(!Menu::menus[button->parentMenu]->buttons.erase(button->GetPos().y)){ - std::cout<<"WARNING! Attempted to erase key "<GetPos().y<<" from button map, but the list still exists!"; - throw; - } - } - if(keyboardButtonList.size()==0){ - if(!Menu::menus[button->parentMenu]->keyboardButtons.erase(button->GetPos().y)){ - std::cout<<"WARNING! Attempted to erase key "<GetPos().y<<" from button map, but the list still exists!"; - throw; - } - } - } - void inline RemoveEmptySlots(){ - //Algorithm will iterate through all slots, finding blank slots. Each time a blank slot is found, all items will shift over by one, and then the last item will be removed. Repeat until all slots iterated through. - for(int i=0;iUpdate(game); //We have to call update to update the validation state. - //HACK ALERT!! This only gets called on inventories...And only on inventory items, which would have the valid flag set. We only care about components that are inventory slots. - if(!button->valid){ - for(int j=i;jparentMenu]->components.at(components[j]->name)=components[j+1]; - components[j]=components[j+1]; - } - MenuComponent*lastButton=Menu::menus[components[components.size()-1]->parentMenu]->components.at(components[components.size()-1]->name); - //Now we have to fix up the keyboard button list. - RemoveButton(lastButton); - Menu::menus[components[components.size()-1]->parentMenu]->components.erase(components[components.size()-1]->name); - //Now delete the last slot. - components.erase(components.end()-1); - i--; //Subtract one from the index so we don't accidently skip slots. - } - } - bounds=CalculateBounds(); //Recalculate the bounds as it's possible the width/height of the component has changed. - } - virtual void OnInventorySlotsUpdate(ITCategory cat)override{ - std::vector&inv=Inventory::get(cat); - //We only want to refresh the inventory slots if the component count no longer matches what's actually in our inventory. - if(ComponentCount()UseItem(); - }; - - MenuItemButton*button=new MenuItemButton{parentMenu,{{float(totalSpacing*x),float(totalSpacing*y)},{float(buttonSize),float(buttonSize)}},Inventory::get("Consumables"),itemIndex,useItemFunc}; - AddComponent(Menu::menus[parentMenu],"item"+std::to_string(itemIndex),button); - }else - if(ComponentCount()>inv.size()){ //There are empty spots, so let's clean up. - RemoveEmptySlots(); - } - } }; \ No newline at end of file