From 3723835cc728419ac6d2b668fbd8512c99984d52 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sun, 1 Oct 2023 01:48:27 -0500 Subject: [PATCH] Switched menu button system to component inheritance system. --- Crawler/Crawler.cpp | 3 ++- Crawler/Crawler.vcxproj | 5 +++-- Crawler/Crawler.vcxproj.filters | 7 +++++-- Crawler/Menu.cpp | 32 +++++++++++++++---------------- Crawler/Menu.h | 8 +++++--- Crawler/MenuButton.cpp | 22 --------------------- Crawler/MenuButton.h | 24 ----------------------- Crawler/MenuComponent.cpp | 22 +++++++++++++++++++++ Crawler/MenuComponent.h | 34 +++++++++++++++++++++++++++++++++ Crawler/MenuIconButton.cpp | 15 +++++++++++++++ Crawler/Player.cpp | 4 ++++ Crawler/Player.h | 1 + Crawler/TestMenu.cpp | 8 ++++---- Crawler/TestSubMenu.cpp | 19 ++++++++++++++---- Crawler/Version.h | 2 +- 15 files changed, 127 insertions(+), 79 deletions(-) delete mode 100644 Crawler/MenuButton.cpp delete mode 100644 Crawler/MenuButton.h create mode 100644 Crawler/MenuComponent.cpp create mode 100644 Crawler/MenuComponent.h create mode 100644 Crawler/MenuIconButton.cpp diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index c276a7a9..96136e5a 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -77,7 +77,6 @@ Crawler::Crawler() bool Crawler::OnUserCreate(){ InitializeDefaultKeybinds(); InitializeLevels(); - Menu::InitializeMenus(); circleCooldownPoints.push_back({0,0}); for(int i=0;i<=360;i+=4){ @@ -107,6 +106,8 @@ bool Crawler::OnUserCreate(){ GFX.SetInitialized(); std::cout< - + @@ -313,7 +313,8 @@ - + + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index 58135953..bf7be552 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -147,7 +147,7 @@ Header Files - + Header Files @@ -266,7 +266,7 @@ Source Files\Interface - + Source Files\Interface @@ -275,6 +275,9 @@ Source Files\Interface + + Source Files\Interface + diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index baa92ad9..2cbf7c41 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -23,16 +23,16 @@ void Menu::InitializeMenus(){ } } -void Menu::AddButton(const MenuButton&button){ - buttons[button.rect.pos.y].push_back(button); +void Menu::AddComponent(MenuComponent*button){ + buttons[button->rect.pos.y].push_back(button); } void Menu::MenuSelect(Crawler*game){ if(selection==vi2d{-1,-1})return; - buttons[selection.y][selection.x].onClick(*this,game); - if(buttons[selection.y][selection.x].menuDest!=MenuType::ENUM_END){ + buttons[selection.y][selection.x]->onClick(*this,game); + 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. + stack.push_back(&menus[buttons[selection.y][selection.x]->menuDest]);//Navigate to the next menu. }else{ std::cout<<"WARNING! Exceeded menu stack size limit!"<hovered=false; } } if(!MOUSE_NAVIGATION){ - if(selection!=vi2d{-1,-1})buttons[selection.y][selection.x].hovered=true; + if(selection!=vi2d{-1,-1})buttons[selection.y][selection.x]->hovered=true; }else{ for(auto&key:buttons){ for(auto&button:key.second){ - if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ - button.hovered=true; + if(geom2d::overlaps(geom2d::rect{button->rect.pos+upperLeftPos,button->rect.size},game->GetMousePos())){ + button->hovered=true; } } } @@ -84,7 +84,7 @@ void Menu::Update(Crawler*game){ for(auto&key:buttons){ int index=0; for(auto&button:key.second){ - if(geom2d::overlaps(geom2d::rect{button.rect.pos+upperLeftPos,button.rect.size},game->GetMousePos())){ + if(geom2d::overlaps(geom2d::rect{button->rect.pos+upperLeftPos,button->rect.size},game->GetMousePos())){ selection={index,key.first}; break; } @@ -96,7 +96,7 @@ void Menu::Update(Crawler*game){ } for(auto&key:buttons){ for(auto&button:key.second){ - button.Update(game); + button->Update(game); } } }; @@ -106,7 +106,7 @@ void Menu::Draw(Crawler*game){ game->FillRectDecal(upperLeftPos,size,VERY_DARK_BLUE); for(auto&key:buttons){ for(auto&button:key.second){ - button.Draw(game,upperLeftPos); + button->Draw(game,upperLeftPos); } } }; @@ -141,11 +141,11 @@ void Menu::KeyboardButtonNavigation(Crawler*game){ }else{ for(auto&key:buttons){ if(found){ //Once we discover the previous element, the next element becomes our next selection. - int previousButtonX=buttons[selection.y][selection.x].rect.pos.x; + int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x; selection.y=key.first; int index=0; for(auto&button:key.second){ //Try to match a button in the same column as this button first. - if(previousButtonX==button.rect.pos.x){ + if(previousButtonX==button->rect.pos.x){ selection.x=index; break; } @@ -183,11 +183,11 @@ void Menu::KeyboardButtonNavigation(Crawler*game){ prevInd=key.first; } if(prevInd!=-1){ - int previousButtonX=buttons[selection.y][selection.x].rect.pos.x; + int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x; selection.y=prevInd; int index=0; for(auto&button:buttons[prevInd]){ //Try to match a button in the same column as this button first. - if(previousButtonX==button.rect.pos.x){ + if(previousButtonX==button->rect.pos.x){ selection.x=index; break; } diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 11b8f382..b902837d 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -1,8 +1,10 @@ #pragma once -#include "MenuButton.h" +#include "MenuComponent.h" #include "olcPixelGameEngine.h" #include +#define MENUFUNC [](Menu&menu,Crawler*game) + class Crawler; class Menu{ @@ -11,13 +13,13 @@ class Menu{ static bool MOUSE_NAVIGATION; static std::mapmenus; - std::map>buttons; //Buttons are stored in rows followed by their column order. + std::map>buttons; //Buttons are stored in rows followed by their column order. vi2d selection={-1,-1}; vf2d size; //Size in tiles (24x24), every menu will be tile-based public: Menu(); Menu(vf2d size); - void AddButton(const MenuButton&button); + void AddComponent(MenuComponent*button); void Update(Crawler*game); void Draw(Crawler*game); static void InitializeMenus(); diff --git a/Crawler/MenuButton.cpp b/Crawler/MenuButton.cpp deleted file mode 100644 index d5a41a05..00000000 --- a/Crawler/MenuButton.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "Crawler.h" -#include "Menu.h" - -MenuButton::MenuButton(geom2d::rectrect,std::string label,Decal*icon,MenuFunc onClick) - :rect(rect),label(label),menuDest(MenuType::ENUM_END),icon(icon),onClick(onClick),hoverEffect(0){} - -MenuButton::MenuButton(geom2d::rectrect,std::string label,MenuType menuDest,Decal*icon,MenuFunc onClick) - :rect(rect),label(label),menuDest(menuDest),icon(icon),onClick(onClick),hoverEffect(0){} - -void MenuButton::Update(Crawler*game){ - if(hovered){ - hoverEffect=std::min(1.f,hoverEffect+game->GetElapsedTime()); - }else{ - hoverEffect=std::max(0.f,hoverEffect-game->GetElapsedTime()); - } -} - -void MenuButton::Draw(Crawler*game,vf2d parentPos){ - game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(VERY_DARK_BLUE,CYAN,hoverEffect)); - game->DrawRectDecal(rect.pos+parentPos,rect.size,GREY); - game->DrawStringPropDecal(rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2,label); -} \ No newline at end of file diff --git a/Crawler/MenuButton.h b/Crawler/MenuButton.h deleted file mode 100644 index fa5b3ea1..00000000 --- a/Crawler/MenuButton.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "olcUTIL_Geometry2D.h" -#include "MenuType.h" -#include - -class Menu; -class Crawler; - -class MenuButton{ - friend class Menu; - MenuType menuDest; - geom2d::rectrect; - std::string label; - Decal*icon; - MenuFunc onClick; - bool hovered=false; -private: - float hoverEffect=0; -public: - MenuButton(geom2d::rectrect,std::string label,Decal*icon,MenuFunc onClick); - MenuButton(geom2d::rectrect,std::string label,MenuType menuDest,Decal* icon,MenuFunc onClick); - void Update(Crawler*game); - void Draw(Crawler*game,vf2d parentPos); -}; \ No newline at end of file diff --git a/Crawler/MenuComponent.cpp b/Crawler/MenuComponent.cpp new file mode 100644 index 00000000..4f3e8e3a --- /dev/null +++ b/Crawler/MenuComponent.cpp @@ -0,0 +1,22 @@ +#include "Crawler.h" +#include "Menu.h" + +MenuComponent::MenuComponent(geom2d::rectrect,std::string label,MenuFunc onClick) + :rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0){} + +MenuComponent::MenuComponent(geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick) + :rect(rect),label(label),menuDest(menuDest),onClick(onClick),hoverEffect(0){} + +void MenuComponent::Update(Crawler*game){ + if(hovered){ + hoverEffect=std::min(1.f,hoverEffect+game->GetElapsedTime()); + }else{ + hoverEffect=std::max(0.f,hoverEffect-game->GetElapsedTime()); + } +} + +void MenuComponent::Draw(Crawler*game,vf2d parentPos){ + game->FillRectDecal(rect.pos+parentPos,rect.size,PixelLerp(VERY_DARK_BLUE,CYAN,hoverEffect)); + game->DrawRectDecal(rect.pos+parentPos,rect.size,GREY); + game->DrawStringPropDecal(rect.pos+parentPos+rect.size/2-game->GetTextSizeProp(label)/2,label); +} \ No newline at end of file diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h new file mode 100644 index 00000000..96fdb4cc --- /dev/null +++ b/Crawler/MenuComponent.h @@ -0,0 +1,34 @@ +#pragma once +#include "olcUTIL_Geometry2D.h" +#include "MenuType.h" +#include + +class Menu; +class Crawler; + +class MenuComponent{ + friend class Menu; + MenuType menuDest; + std::string label; + MenuFunc onClick; + bool hovered=false; +private: + float hoverEffect=0; +protected: + geom2d::rectrect; +public: + MenuComponent(geom2d::rectrect,std::string label,MenuFunc onClick); + MenuComponent(geom2d::rectrect,std::string label,MenuType menuDest,MenuFunc onClick); + virtual void Update(Crawler*game); + virtual void Draw(Crawler*game,vf2d parentPos); +}; + +class MenuIconButton:public MenuComponent{ +private: + Decal*icon; +public: + MenuIconButton(geom2d::rectrect,Decal*icon,MenuFunc onClick); +protected: + virtual void Update(Crawler*game)override; + virtual void Draw(Crawler*game,vf2d parentPos)override; +}; \ No newline at end of file diff --git a/Crawler/MenuIconButton.cpp b/Crawler/MenuIconButton.cpp new file mode 100644 index 00000000..e1377ec0 --- /dev/null +++ b/Crawler/MenuIconButton.cpp @@ -0,0 +1,15 @@ +#include "MenuComponent.h" +#include "Crawler.h" + +MenuIconButton::MenuIconButton(geom2d::rectrect,Decal*icon,MenuFunc onClick) + :MenuComponent(rect,"",onClick),icon(icon) +{} + +void MenuIconButton::Update(Crawler*game){ + MenuComponent::Update(game); +} + +void MenuIconButton::Draw(Crawler*game,vf2d parentPos){ + MenuComponent::Draw(game,parentPos); + game->DrawRotatedDecal(parentPos+rect.middle(),icon,0,icon->sprite->Size()/2); +} \ No newline at end of file diff --git a/Crawler/Player.cpp b/Crawler/Player.cpp index eece8e1f..24363c95 100644 --- a/Crawler/Player.cpp +++ b/Crawler/Player.cpp @@ -698,4 +698,8 @@ void Player::SetAnimationBasedOnTargetingDirection(float targetDirection){ void Player::SetIframes(float duration){ iframe_time=duration; +} + +void Player::SetMana(int mana){ + this->mana=std::clamp(mana,0,maxmana); } \ No newline at end of file diff --git a/Crawler/Player.h b/Crawler/Player.h index 7bd1ea9d..5c244132 100644 --- a/Crawler/Player.h +++ b/Crawler/Player.h @@ -133,6 +133,7 @@ public: bool CanAct(Ability&ability); void Knockback(vf2d vel); void SetIframes(float duration); + void SetMana(int mana); void AddBuff(BuffType type,float duration,float intensity); std::vectorGetBuffs(BuffType buff); diff --git a/Crawler/TestMenu.cpp b/Crawler/TestMenu.cpp index da64c016..0e995f59 100644 --- a/Crawler/TestMenu.cpp +++ b/Crawler/TestMenu.cpp @@ -8,19 +8,19 @@ const Menu Menu::InitializeTestMenu(){ menu.stack.clear(); }; - testMenu.AddButton(MenuButton({{24*1,24*1},{24*2,24*1}},"Close",nullptr,quitWindow)); + testMenu.AddComponent(new MenuComponent({{24*1,24*1},{24*2,24*1}},"Close",quitWindow)); MenuFunc doNothing=[](Menu&menu,Crawler*game){}; - testMenu.AddButton(MenuButton({{24*4,24*1},{24*3,24*1}},"Test",nullptr,doNothing)); + testMenu.AddComponent(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()); }; - testMenu.AddButton(MenuButton({{24*4,24*3},{24*3,24*1}},"Hurt Player",nullptr,HurtPlayer)); + testMenu.AddComponent(new MenuComponent({{24*4,24*3},{24*3,24*1}},"Hurt Player",HurtPlayer)); - testMenu.AddButton(MenuButton({{24*2,24*4.5},{24*4,24*1}},"Open Another\n Menu",TEST_2,nullptr,doNothing)); + testMenu.AddComponent(new MenuComponent({{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 b7afc13a..d31aad1e 100644 --- a/Crawler/TestSubMenu.cpp +++ b/Crawler/TestSubMenu.cpp @@ -1,20 +1,31 @@ #include "Crawler.h" #include "Menu.h" +#include "DEFINES.h" +#include "olcPixelGameEngine.h" +#include "safemap.h" + +INCLUDE_GFX const Menu Menu::InitializeTestSubMenu(){ Menu testSubMenu({24*4,24*5}); - MenuFunc goBack=[](Menu&menu,Crawler*game){ + MenuFunc goBack=MENUFUNC{ menu.stack.pop_back(); }; - testSubMenu.AddButton(MenuButton({{24*1,24*1},{24*2,24*1}},"Go Back",nullptr,goBack)); + testSubMenu.AddComponent(new MenuComponent({{24*1,24*1},{24*2,24*1}},"Go Back",goBack)); - MenuFunc quitWindow=[](Menu&menu,Crawler*game){ + MenuFunc quitWindow=MENUFUNC{ menu.stack.clear(); }; - testSubMenu.AddButton(MenuButton({{24*1,24*3},{24*3,24*1}},"Close Window",nullptr,quitWindow)); + testSubMenu.AddComponent(new MenuComponent({{24*1,24*3},{24*3,24*1}},"Close Window",quitWindow)); + + MenuFunc restoreMana=MENUFUNC{ + game->GetPlayer()->SetMana(game->GetPlayer()->GetMaxMana()); + }; + + testSubMenu.AddComponent(new MenuIconButton({{24*0,24*3},{24*1,24*2}},GFX["mana.png"].Decal(),restoreMana)); return testSubMenu; } \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index 9a4e177a..b641166d 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 1644 +#define VERSION_BUILD 1651 #define stringify(a) stringify_(a) #define stringify_(a) #a