Converted boolean flags for component creation in windows to using enum bitwise attributes.

This commit is contained in:
sigonasr2 2023-11-06 01:00:17 -06:00
parent c82a3c5980
commit 2e248e30b4
11 changed files with 79 additions and 31 deletions

View File

@ -14,7 +14,7 @@ class CharacterAbilityPreviewComponent:public MenuLabel{
Ability*ability; Ability*ability;
public: public:
inline CharacterAbilityPreviewComponent(MenuType parent,geom2d::rect<float>rect,Ability*ability) inline CharacterAbilityPreviewComponent(MenuType parent,geom2d::rect<float>rect,Ability*ability)
:MenuLabel(parent,rect,"",1,true,false,true,true),ability(ability){} :MenuLabel(parent,rect,"",1,ComponentAttr::OUTLINE|ComponentAttr::BACKGROUND),ability(ability){}
protected: protected:
virtual void inline Update(Crawler*game)override{ virtual void inline Update(Crawler*game)override{
MenuLabel::Update(game); MenuLabel::Update(game);

View File

@ -17,7 +17,7 @@ void Menu::InitializeClassInfoWindow(){
Menu*classSelectionWindow=Menu::menus[CLASS_SELECTION]; Menu*classSelectionWindow=Menu::menus[CLASS_SELECTION];
ClassInfo data=classutils::GetClassInfo(classSelectionWindow->S(A::CLASS_SELECTION)); ClassInfo data=classutils::GetClassInfo(classSelectionWindow->S(A::CLASS_SELECTION));
MenuLabel*label=new MenuLabel(CLASS_INFO,{{0,0},{classInfoWindow->size.x-1,24}},data.className,2,true,true,true,true); MenuLabel*label=new MenuLabel(CLASS_INFO,{{0,0},{classInfoWindow->size.x-1,24}},data.className,2,ComponentAttr::SHADOW|ComponentAttr::OUTLINE|ComponentAttr::BACKGROUND);
classInfoWindow->AddComponent("Class Name",label); classInfoWindow->AddComponent("Class Name",label);
@ -28,10 +28,10 @@ void Menu::InitializeClassInfoWindow(){
vf2d healthDisplayLabelPos={classInfoWindow->size.x/3,label->GetPos().y+24}; vf2d healthDisplayLabelPos={classInfoWindow->size.x/3,label->GetPos().y+24};
vf2d labelSize={2*classInfoWindow->size.x/3-1,16}; vf2d labelSize={2*classInfoWindow->size.x/3-1,16};
MenuLabel*baseStatsLabel=new MenuLabel(CLASS_INFO,{{0,label->GetPos().y+24},{classInfoWindow->size.x/3,labelSize.y}},"Base Stats",1,true,true,true); MenuLabel*baseStatsLabel=new MenuLabel(CLASS_INFO,{{0,label->GetPos().y+24},{classInfoWindow->size.x/3,labelSize.y}},"Base Stats",1,ComponentAttr::SHADOW|ComponentAttr::OUTLINE);
MenuLabel*healthDisplayLabel=new MenuLabel(CLASS_INFO,{healthDisplayLabelPos+vf2d{0,16*0},labelSize},"Health: "+std::to_string(data.baseHealth)+" + "+std::to_string(data.healthGrowthRate).substr(0,3)+" per level",1,false,true,true); MenuLabel*healthDisplayLabel=new MenuLabel(CLASS_INFO,{healthDisplayLabelPos+vf2d{0,16*0},labelSize},"Health: "+std::to_string(data.baseHealth)+" + "+std::to_string(data.healthGrowthRate).substr(0,3)+" per level",1,ComponentAttr::LEFT_ALIGN|ComponentAttr::SHADOW|ComponentAttr::OUTLINE);
MenuLabel*atkDisplayLabel=new MenuLabel(CLASS_INFO,{healthDisplayLabelPos+vf2d{0,16*1},labelSize},"Attack: "+std::to_string(data.baseAtk)+" + "+std::to_string(data.atkGrowthRate).substr(0,3)+" per level",1,false,true,true); MenuLabel*atkDisplayLabel=new MenuLabel(CLASS_INFO,{healthDisplayLabelPos+vf2d{0,16*1},labelSize},"Attack: "+std::to_string(data.baseAtk)+" + "+std::to_string(data.atkGrowthRate).substr(0,3)+" per level",1,ComponentAttr::LEFT_ALIGN|ComponentAttr::SHADOW|ComponentAttr::OUTLINE);
classInfoWindow->AddComponent("Base Stats Text",baseStatsLabel); classInfoWindow->AddComponent("Base Stats Text",baseStatsLabel);
classInfoWindow->AddComponent("Health Display Text",healthDisplayLabel); classInfoWindow->AddComponent("Health Display Text",healthDisplayLabel);

View File

@ -17,10 +17,10 @@ void Menu::InitializeClassSelectionWindow(){
vf2d outlineSize=classSelectionWindow->size-vf2d{13,13}; vf2d outlineSize=classSelectionWindow->size-vf2d{13,13};
MenuLabel*classSelectionLabel=new MenuLabel(CLASS_SELECTION,{{4,20},{outlineSize.x,32}},"Choose a Character Class",2,true,true,true,true); MenuLabel*classSelectionLabel=new MenuLabel(CLASS_SELECTION,{{4,20},{outlineSize.x,32}},"Choose a Character Class",2,ComponentAttr::SHADOW|ComponentAttr::OUTLINE|ComponentAttr::BACKGROUND);
classSelectionWindow->AddComponent("Class Selection Title Label",classSelectionLabel); classSelectionWindow->AddComponent("Class Selection Title Label",classSelectionLabel);
MenuLabel*outline=new MenuLabel(CLASS_SELECTION,{{4,4},outlineSize},"",1,true,false,true,false); MenuLabel*outline=new MenuLabel(CLASS_SELECTION,{{4,4},outlineSize},"",1,ComponentAttr::OUTLINE);
classSelectionWindow->AddComponent("Outline Border",outline); classSelectionWindow->AddComponent("Outline Border",outline);
@ -74,8 +74,8 @@ void Menu::InitializeClassSelectionWindow(){
buttonStartPos.y+(buttonSize.y+buttonPadding.y+2*outlineSize.y/9)*float(i/3), buttonStartPos.y+(buttonSize.y+buttonPadding.y+2*outlineSize.y/9)*float(i/3),
}; };
vf2d backgroundSize={floor(outlineSize.y/3-buttonPadding.y*3),outlineSize.y/3-buttonPadding.y*3}; //The floor is for fixing a small pixel rounding bug. vf2d backgroundSize={floor(outlineSize.y/3-buttonPadding.y*3),outlineSize.y/3-buttonPadding.y*3}; //The floor is for fixing a small pixel rounding bug.
MenuLabel*backgroundOutline=new MenuLabel(CLASS_SELECTION,{backgroundOffsetPos,backgroundSize},"",1,true,false,true,true); MenuLabel*backgroundOutline=new MenuLabel(CLASS_SELECTION,{backgroundOffsetPos,backgroundSize},"",1,ComponentAttr::OUTLINE|ComponentAttr::BACKGROUND);
MenuLabel*classLabel=new MenuLabel(CLASS_SELECTION,{backgroundOffsetPos,buttonSize},className,1,true,true); MenuLabel*classLabel=new MenuLabel(CLASS_SELECTION,{backgroundOffsetPos,buttonSize},className,1,ComponentAttr::SHADOW);
MenuAnimatedIconToggleButton*classSprite=new MenuAnimatedIconToggleButton(CLASS_SELECTION,{backgroundOffsetPos+vf2d{0,12},backgroundSize+vf2d{0,-buttonSize.y-12}},classAnimationName,[](MenuFuncData data){ MenuAnimatedIconToggleButton*classSprite=new MenuAnimatedIconToggleButton(CLASS_SELECTION,{backgroundOffsetPos+vf2d{0,12},backgroundSize+vf2d{0,-buttonSize.y-12}},classAnimationName,[](MenuFuncData data){
data.menu.components["Confirm"]->Enable(true); data.menu.components["Confirm"]->Enable(true);
data.menu.components["Confirm"]->S(A::CLASS_SELECTION)=data.component->S(A::CLASS_SELECTION); data.menu.components["Confirm"]->S(A::CLASS_SELECTION)=data.component->S(A::CLASS_SELECTION);

View File

@ -30,8 +30,8 @@ void Menu::InitializeInventoryWindow(){
//We don't have to actually populate the inventory list because now when an item gets added, it will automatically add the correct component in for us. //We don't have to actually populate the inventory list because now when an item gets added, it will automatically add the correct component in for us.
MenuLabel*itemNameLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing-16)},{windowSize.x-4,windowSize.y-108}),"",1,false,true}; MenuLabel*itemNameLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing-16)},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW};
inventoryWindow->AddComponent("itemName",itemNameLabel); inventoryWindow->AddComponent("itemName",itemNameLabel);
MenuLabel*itemDescriptionLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing)},{windowSize.x-4,windowSize.y-108}),"",1,true,true}; MenuLabel*itemDescriptionLabel=new MenuLabel{INVENTORY,geom2d::rect<float>(vf2d{2,float(initialInvHeight*totalSpacing+itemSpacing)},{windowSize.x-4,windowSize.y-108}),"",1,ComponentAttr::SHADOW};
inventoryWindow->AddComponent("itemDescription",itemDescriptionLabel); inventoryWindow->AddComponent("itemDescription",itemDescriptionLabel);
} }

View File

@ -11,8 +11,8 @@ protected:
private: private:
float animationTime=0; float animationTime=0;
public: public:
inline MenuAnimatedIconButton(MenuType parent,geom2d::rect<float>rect,std::string animation,MenuFunc onClick,bool selectable=true) inline MenuAnimatedIconButton(MenuType parent,geom2d::rect<float>rect,std::string animation,MenuFunc onClick,IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuIconButton(parent,rect,nullptr,onClick,selectable),animation(animation){} :MenuIconButton(parent,rect,nullptr,onClick,attributes),animation(animation){}
protected: protected:
virtual inline void Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuIconButton::Update(game); MenuIconButton::Update(game);

View File

@ -3,13 +3,13 @@
typedef Attribute A; typedef Attribute A;
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable,bool selectableViaKeyboard) MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,ButtonAttr attributes)
:parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable),selectableViaKeyboard(selectableViaKeyboard),memoryLeakInfo(Menu::GetMemoryLeakReportInfo()){ :parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(!(attributes&ButtonAttr::UNSELECTABLE)),selectableViaKeyboard(!(attributes&ButtonAttr::UNSELECTABLE_VIA_KEYBOARD)),memoryLeakInfo(Menu::GetMemoryLeakReportInfo()){
Menu::unhandledComponents.push_back(this); Menu::unhandledComponents.push_back(this);
} }
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable,bool selectableViaKeyboard) MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,ButtonAttr attributes)
:MenuComponent(parent,rect,label,onClick,selectable,selectableViaKeyboard){ :MenuComponent(parent,rect,label,onClick,attributes){
//NOTE: This constructor also calls the other constructor above! //NOTE: This constructor also calls the other constructor above!
this->menuDest=menuDest; this->menuDest=menuDest;
} }

View File

@ -1,6 +1,20 @@
#pragma once #pragma once
#include "Menu.h" #include "Menu.h"
enum class ButtonAttr{
NONE=0b00,
UNSELECTABLE=0b01, //Makes the component unselectable.
UNSELECTABLE_VIA_KEYBOARD=0b10, //Makes the component unselectable via keyboard.
};
enum class ComponentAttr{
NONE=0b0000,
LEFT_ALIGN=0b0001, //Labels are centered by default.
SHADOW=0b0010, //Adds shadows to the label text.
OUTLINE=0b0100, //Adds an outline around the component.
BACKGROUND=0b1000, //Renders the background of the menu theme for this component.
};
class MenuComponent:IAttributable{ class MenuComponent:IAttributable{
friend class Menu; friend class Menu;
friend class MenuItemButton; friend class MenuItemButton;
@ -36,8 +50,8 @@ protected:
virtual bool GetHoverState(Crawler*game,MenuComponent*child); virtual bool GetHoverState(Crawler*game,MenuComponent*child);
virtual void AfterCreate(); //Called after the creation of all menus finish. virtual void AfterCreate(); //Called after the creation of all menus finish.
public: public:
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true,bool selectableViaKeyboard=true); MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,ButtonAttr attributes=ButtonAttr::NONE);
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true,bool selectableViaKeyboard=true); MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,ButtonAttr attributes=ButtonAttr::NONE);
virtual ~MenuComponent(); virtual ~MenuComponent();
void _Update(Crawler*game); void _Update(Crawler*game);
void _Draw(Crawler*game); void _Draw(Crawler*game);
@ -56,4 +70,24 @@ public:
virtual void OnInventorySlotsUpdate(ITCategory cat); virtual void OnInventorySlotsUpdate(ITCategory cat);
std::string GetLabel(); std::string GetLabel();
void Enable(bool enabled); void Enable(bool enabled);
}; };
constexpr auto operator|(ButtonAttr attribute,ButtonAttr attribute2) noexcept
{
return ButtonAttr(static_cast<std::underlying_type_t<ButtonAttr>>(attribute)|static_cast<std::underlying_type_t<ButtonAttr>>(attribute2));
}
constexpr auto operator&(ButtonAttr attribute,ButtonAttr attribute2) noexcept
{
return static_cast<std::underlying_type_t<ButtonAttr>>(attribute)&static_cast<std::underlying_type_t<ButtonAttr>>(attribute2);
}
constexpr auto operator|(ComponentAttr attribute,ComponentAttr attribute2) noexcept
{
return ComponentAttr(static_cast<std::underlying_type_t<ComponentAttr>>(attribute)|static_cast<std::underlying_type_t<ComponentAttr>>(attribute2));
}
constexpr auto operator&(ComponentAttr attribute,ComponentAttr attribute2) noexcept
{
return static_cast<std::underlying_type_t<ComponentAttr>>(attribute)&static_cast<std::underlying_type_t<ComponentAttr>>(attribute2);
}

View File

@ -5,12 +5,17 @@
INCLUDE_game INCLUDE_game
enum class IconButtonAttr{
SELECTABLE=0b0,
NOT_SELECTABLE=int(ButtonAttr::UNSELECTABLE),
};
class MenuIconButton:public MenuComponent{ class MenuIconButton:public MenuComponent{
protected: protected:
Decal*icon; Decal*icon;
public: public:
inline MenuIconButton(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick,bool selectable=true) inline MenuIconButton(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick,IconButtonAttr attributes=IconButtonAttr::SELECTABLE)
:MenuComponent(parent,rect,"",onClick,selectable),icon(icon){} :MenuComponent(parent,rect,"",onClick,ButtonAttr(attributes)),icon(icon){}
protected: protected:
virtual inline void Update(Crawler*game)override{ virtual inline void Update(Crawler*game)override{
MenuComponent::Update(game); MenuComponent::Update(game);
@ -21,4 +26,13 @@ protected:
game->DrawSprite(parentPos+rect.middle()-icon->sprite->Size()/2,icon->sprite,1,Sprite::Flip::NONE); game->DrawSprite(parentPos+rect.middle()-icon->sprite->Size()/2,icon->sprite,1,Sprite::Flip::NONE);
} }
} }
}; };
constexpr auto operator|(IconButtonAttr attribute,IconButtonAttr attribute2) noexcept
{
return IconButtonAttr(static_cast<std::underlying_type_t<IconButtonAttr>>(attribute)|static_cast<std::underlying_type_t<IconButtonAttr>>(attribute2));
}
constexpr auto operator&(IconButtonAttr attribute,IconButtonAttr attribute2) noexcept
{
return static_cast<std::underlying_type_t<IconButtonAttr>>(attribute)&static_cast<std::underlying_type_t<IconButtonAttr>>(attribute2);
}

View File

@ -11,10 +11,10 @@ class MenuLabel:public MenuComponent{
bool centered=true; bool centered=true;
int scale=1; int scale=1;
public: public:
inline MenuLabel(MenuType parent,geom2d::rect<float>rect,std::string label,int scale=1,bool centered=true,bool shadow=false,bool outline=false,bool background=false) inline MenuLabel(MenuType parent,geom2d::rect<float>rect,std::string label,int scale=1,ComponentAttr attributes=ComponentAttr::NONE)
:MenuComponent(parent,rect,label,MenuFunc{},false),scale(scale),centered(centered),shadow(shadow){ :MenuComponent(parent,rect,label,MenuFunc{},ButtonAttr::UNSELECTABLE|ButtonAttr::UNSELECTABLE_VIA_KEYBOARD),scale(scale),centered(!(attributes&ComponentAttr::LEFT_ALIGN)),shadow(attributes&ComponentAttr::SHADOW){
border=outline; border=attributes&ComponentAttr::OUTLINE;
this->background=background; this->background=attributes&ComponentAttr::BACKGROUND;
showDefaultLabel=false; showDefaultLabel=false;
} }
inline void SetLabel(std::string text){ inline void SetLabel(std::string text){

View File

@ -23,7 +23,7 @@ protected:
} }
public: public:
inline ScrollableWindowComponent(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick) inline ScrollableWindowComponent(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick)
:MenuComponent(parent,rect,"",onClick,false){ :MenuComponent(parent,rect,"",onClick,ButtonAttr::UNSELECTABLE|ButtonAttr::UNSELECTABLE_VIA_KEYBOARD){
r.Create(rect.size.x,rect.size.y); r.Create(rect.size.x,rect.size.y);
} }
@ -35,8 +35,8 @@ public:
} }
protected: protected:
virtual inline void AfterCreate()override{ 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); upButton=new MenuComponent(parentMenu,{vf2d{rect.size.x-12,0},{12,12}},"^",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y+="ThemeGlobal.MenuButtonScrollSpeed"_I;},ButtonAttr::UNSELECTABLE_VIA_KEYBOARD);
downButton=new MenuComponent(parentMenu,{rect.size-vf2d{12,12},{12,12}},"v",[&](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;},ButtonAttr::UNSELECTABLE_VIA_KEYBOARD);
//Let's use the internal name of this component to add unique names for sub-components. //Let's use the internal name of this component to add unique names for sub-components.
Menu::menus[parentMenu]->AddComponent(name+upButton->rect.pos.str()+"_"+upButton->rect.size.str(),upButton); Menu::menus[parentMenu]->AddComponent(name+upButton->rect.pos.str()+"_"+upButton->rect.size.str(),upButton);
Menu::menus[parentMenu]->AddComponent(name+downButton->rect.pos.str()+"_"+downButton->rect.size.str(),downButton); Menu::menus[parentMenu]->AddComponent(name+downButton->rect.pos.str()+"_"+downButton->rect.size.str(),downButton);

View File

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 2 #define VERSION_MINOR 2
#define VERSION_PATCH 1 #define VERSION_PATCH 1
#define VERSION_BUILD 2541 #define VERSION_BUILD 2548
#define stringify(a) stringify_(a) #define stringify(a) stringify_(a)
#define stringify_(a) #a #define stringify_(a) #a