diff --git a/Crawler/InventoryWindow.cpp b/Crawler/InventoryWindow.cpp index 6fbd174f..5860ea34 100644 --- a/Crawler/InventoryWindow.cpp +++ b/Crawler/InventoryWindow.cpp @@ -5,6 +5,7 @@ #include "safemap.h" #include "Item.h" #include "MenuItemButton.h" +#include "MenuLabel.h" INCLUDE_GFX typedef Attribute A; @@ -28,10 +29,15 @@ const Menu Menu::InitializeInventoryWindow(){ for(int y=0;yGetKey(ENTER).bReleased||game->GetKey(SPACE).bReleased){ if(selectedComponent->DropDraggableItem(draggingComponent)){ + delete draggingComponent; //We know we allocated a new instance of this, so we will now free it. draggingComponent=nullptr; } } }else{ if(game->GetMouse(Mouse::LEFT).bReleased){ if(selectedComponent->DropDraggableItem(draggingComponent)){ + delete draggingComponent; //We know we allocated a new instance of this, so we will now free it. draggingComponent=nullptr; } } diff --git a/Crawler/Menu.h b/Crawler/Menu.h index d8283d50..37706d73 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -30,7 +30,6 @@ class Menu:IAttributable{ std::map>buttons; //Buttons are stored in rows followed by their column order. std::map>newButtonArrangement; //The new setup that the buttons will be at if a drag operation completes. std::vectordisplayComponents; //Components that are only for displaying purposes. - safemapcomponents; //A friendly way to interrogate any component we are interested in. vi2d selection={-1,-1}; 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 @@ -48,6 +47,7 @@ public: static std::string themeSelection; static safeunorderedmapthemes; static const vf2d CENTERED; + safemapcomponents; //A friendly way to interrogate any component we are interested in. private: void HoverMenuSelect(Crawler*game); diff --git a/Crawler/MenuComponent.cpp b/Crawler/MenuComponent.cpp index 3699ef98..af5f5925 100644 --- a/Crawler/MenuComponent.cpp +++ b/Crawler/MenuComponent.cpp @@ -1,11 +1,11 @@ #include "Crawler.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){} +MenuComponent::MenuComponent(MenuType parent,geom2d::rectrect,std::string label,MenuFunc onClick,bool selectable) + :parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable){} -MenuComponent::MenuComponent(geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable) - :rect(rect),label(label),menuDest(menuDest),onClick(onClick),hoverEffect(0),selectable(selectable){} +MenuComponent::MenuComponent(MenuType parent,geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable) + :parentMenu(parent),rect(rect),label(label),menuDest(menuDest),onClick(onClick),hoverEffect(0),selectable(selectable){} void MenuComponent::Update(Crawler*game){ if(hovered){ diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h index 06a6d711..f2677c84 100644 --- a/Crawler/MenuComponent.h +++ b/Crawler/MenuComponent.h @@ -3,8 +3,8 @@ class MenuComponent{ friend class Menu; + friend class MenuItemButton; MenuType menuDest; - bool hovered=false; bool selectable=true; private: float hoverEffect=0; @@ -14,12 +14,15 @@ protected: bool border=true; bool draggable=false; MenuFunc onClick; + MenuType parentMenu=MenuType::ENUM_END; + bool hovered=false; public: - MenuComponent(geom2d::rectrect,std::string label,MenuFunc onClick,bool selectable=true); - MenuComponent(geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true); + MenuComponent(MenuType parent,geom2d::rectrect,std::string label,MenuFunc onClick,bool selectable=true); + MenuComponent(MenuType parent,geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true); virtual void Update(Crawler*game); virtual void Draw(Crawler*game,vf2d parentPos,bool focused); //We picked up a draggable component, we should make a copy and return it here. If a nullptr is returned here, the pickup is not allowed. + //WARNING!!! This allocates a brand new component when successful!!! Be prepared to clear it! virtual MenuComponent*PickUpDraggableItem(); //We are attempting to drop draggable onto this item. If it's not allowed, return false. virtual bool DropDraggableItem(MenuComponent*draggable); diff --git a/Crawler/MenuIconButton.h b/Crawler/MenuIconButton.h index 3d4db499..9b9f639f 100644 --- a/Crawler/MenuIconButton.h +++ b/Crawler/MenuIconButton.h @@ -9,8 +9,8 @@ class MenuIconButton:public MenuComponent{ protected: Decal*icon; public: - inline MenuIconButton(geom2d::rectrect,Decal*icon,MenuFunc onClick) - :MenuComponent(rect,"",onClick),icon(icon){} + inline MenuIconButton(MenuType parent,geom2d::rectrect,Decal*icon,MenuFunc onClick) + :MenuComponent(parent,rect,"",onClick),icon(icon){} protected: virtual inline void Update(Crawler*game)override{ MenuComponent::Update(game); diff --git a/Crawler/MenuItemButton.h b/Crawler/MenuItemButton.h index da470396..ff6e934d 100644 --- a/Crawler/MenuItemButton.h +++ b/Crawler/MenuItemButton.h @@ -14,8 +14,8 @@ private: int inventoryIndex=0; bool valid=false; public: - inline MenuItemButton(geom2d::rectrect,std::vector&invRef,int invIndex,MenuFunc onClick) - :MenuIconButton(rect,invRef.size()>invIndex?ITEM_DATA.at(invRef[invIndex]).Decal():nullptr,onClick),invRef(invRef),inventoryIndex(invIndex){ + inline MenuItemButton(MenuType parent,geom2d::rectrect,std::vector&invRef,int invIndex,MenuFunc onClick) + :MenuIconButton(parent,rect,invRef.size()>invIndex?ITEM_DATA.at(invRef[invIndex]).Decal():nullptr,onClick),invRef(invRef),inventoryIndex(invIndex){ draggable=true; valid=invRef.size()>invIndex; } @@ -30,18 +30,44 @@ protected: virtual inline void Update(Crawler*game)override{ MenuIconButton::Update(game); valid=invRef.size()>inventoryIndex&&ITEM_DATA.count(invRef[inventoryIndex]); + Menu*menu=Menu::stack.back(); if(valid){ icon=ITEM_DATA.at(invRef[inventoryIndex]).Decal(); + if(hovered){ + switch(parentMenu){ + case INVENTORY:{ + //There should be an itemName label to modify. + menu->components.at("itemName")->label=ITEM_DATA.at(invRef[inventoryIndex]).Name(); + //There should be an itemDescription label to modify. + menu->components.at("itemDescription")->label=ITEM_DATA.at(invRef[inventoryIndex]).Description(); + }break; + } + } }else{ icon=nullptr; + if(hovered){ + switch(parentMenu){ + case INVENTORY:{ + //There should be an itemName label to modify. + menu->components.at("itemName")->label=""; + //There should be an itemDescription label to modify. + menu->components.at("itemDescription")->label=""; + }break; + } + } } } virtual inline void Draw(Crawler*game,vf2d parentPos,bool focused)override{ MenuIconButton::Draw(game,parentPos,focused); + if(valid){ + std::string quantityText="x"+std::to_string(Inventory::GetItemCount(invRef.at(inventoryIndex))); + vf2d textSize=vf2d(game->GetTextSizeProp(quantityText))*0.5; + game->DrawShadowStringDecal(parentPos+rect.pos+rect.size-textSize,quantityText,WHITE,BLACK,{0.5,0.5},0.5); + } } virtual inline MenuComponent*PickUpDraggableItem()override{ if(valid){ - MenuItemButton*pickUp=new MenuItemButton(rect,invRef,inventoryIndex,onClick); + MenuItemButton*pickUp=new MenuItemButton(parentMenu,rect,invRef,inventoryIndex,onClick); valid=false; return pickUp; }else{ diff --git a/Crawler/MenuLabel.h b/Crawler/MenuLabel.h index 96105a07..64b1bc18 100644 --- a/Crawler/MenuLabel.h +++ b/Crawler/MenuLabel.h @@ -6,9 +6,11 @@ INCLUDE_game class MenuLabel:public MenuComponent{ + bool shadow=false; + bool centered=true; public: - inline MenuLabel(geom2d::rectrect,std::string label) - :MenuComponent(rect,label,MenuFunc{},false){ + inline MenuLabel(MenuType parent,geom2d::rectrect,std::string label,bool centered=true,bool shadow=false) + :MenuComponent(parent,rect,label,MenuFunc{},false),centered(centered),shadow(shadow){ border=false; } inline void SetLabel(std::string text){ @@ -19,6 +21,14 @@ protected: MenuComponent::Update(game); } virtual void inline Draw(Crawler*game,vf2d parentPos,bool focused)override{ - MenuComponent::Draw(game,parentPos,focused); + vf2d drawPos=rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2; //Assume centered. + if(!centered){ + drawPos=rect.pos+parentPos; + } + if(shadow){ + game->DrawShadowStringPropDecal(drawPos,label,focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F); + }else{ + game->DrawStringPropDecal(drawPos,label,focused?WHITE:WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F); + } } }; \ No newline at end of file diff --git a/Crawler/TestMenu.cpp b/Crawler/TestMenu.cpp index e2ec5a10..97ca8947 100644 --- a/Crawler/TestMenu.cpp +++ b/Crawler/TestMenu.cpp @@ -8,19 +8,19 @@ const Menu Menu::InitializeTestMenu(){ data.menu.stack.clear(); }; - testMenu.AddComponent("Close",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Close",quitWindow)); + testMenu.AddComponent("Close",new MenuComponent(TEST,{{24*1,24*1},{24*2,24*1}},"Close",quitWindow)); MenuFunc doNothing=[](MenuFuncData data){}; - testMenu.AddComponent("Test",new MenuComponent({{24*4,24*1},{24*3,24*1}},"Test",doNothing)); + testMenu.AddComponent("Test",new MenuComponent(TEST,{{24*4,24*1},{24*3,24*1}},"Test",doNothing)); 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)); + testMenu.AddComponent("Hurt Player",new MenuComponent(TEST,{{24*4,24*3},{24*3,24*1}},"Hurt Player",HurtPlayer)); - testMenu.AddComponent("Open SubMenu",new MenuComponent({{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,doNothing)); + testMenu.AddComponent("Open SubMenu",new MenuComponent(TEST,{{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,doNothing)); return testMenu; } \ No newline at end of file diff --git a/Crawler/TestSubMenu.cpp b/Crawler/TestSubMenu.cpp index ec4a5372..82d8fb34 100644 --- a/Crawler/TestSubMenu.cpp +++ b/Crawler/TestSubMenu.cpp @@ -15,7 +15,7 @@ const Menu Menu::InitializeTestSubMenu(){ data.menu.stack.pop_back(); }; - testSubMenu.AddComponent("BACK",new MenuComponent({{24*1,24*1},{24*2,24*1}},"Go Back",goBack)); + testSubMenu.AddComponent("BACK",new MenuComponent(TEST_2,{{24*1,24*1},{24*2,24*1}},"Go Back",goBack)); int index=0; for(auto&theme:Menu::themes){ @@ -43,9 +43,9 @@ const Menu Menu::InitializeTestSubMenu(){ } }; - testSubMenu.AddComponent("PREV_THEME",new MenuComponent({{24*-0.5,24*3},{24*1,24*1}},"<",themePrev)); + testSubMenu.AddComponent("PREV_THEME",new MenuComponent(TEST_2,{{24*-0.5,24*3},{24*1,24*1}},"<",themePrev)); - testSubMenu.AddComponent("THEME_DISPLAY",new MenuLabel({{24*0.5,24*3},{24*3,24*1}},"Theme\n"+Menu::themes[themeSelection].GetThemeName())); + testSubMenu.AddComponent("THEME_DISPLAY",new MenuLabel(TEST_2,{{24*0.5,24*3},{24*3,24*1}},"Theme\n"+Menu::themes[themeSelection].GetThemeName())); MenuFunc themeNext=[](MenuFuncData data){ data.menu.I(A::INDEXED_THEME)=(size_t(data.menu.I(A::INDEXED_THEME))+1)%themes.size(); @@ -60,7 +60,7 @@ const Menu Menu::InitializeTestSubMenu(){ } }; - testSubMenu.AddComponent("NEXT_THEME",new MenuComponent({{24*3.5,24*3},{24*1,24*1}},">",themeNext)); + testSubMenu.AddComponent("NEXT_THEME",new MenuComponent(TEST_2,{{24*3.5,24*3},{24*1,24*1}},">",themeNext)); return testSubMenu; } \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index 88fa2226..d56c8bf3 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 1881 +#define VERSION_BUILD 1895 #define stringify(a) stringify_(a) #define stringify_(a) #a