From 340690a26bb6d5a7f5979204031f59aa97a02a28 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 17 Oct 2023 00:50:58 -0500 Subject: [PATCH] Added keyboard/controller controls for scrollable menu components beyond bounds for disabled controls. --- Crawler/Menu.cpp | 22 ++++++++++++++++++++-- Crawler/Menu.h | 3 +++ Crawler/MenuComponent.cpp | 6 +++++- Crawler/MenuComponent.h | 2 ++ Crawler/ScrollableWindowComponent.h | 8 +++++++- Crawler/Version.h | 2 +- 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index 2490f746..7121b584 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -402,9 +402,19 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ } if(prevSelection!=selection){ if(selection!=vi2d{-1,-1}&&buttons[selection.y][selection.x]->disabled){ - selection=prevSelection; + bool handled=false; + if(!MOUSE_NAVIGATION){ + //Let's transfer some information about our selection being off the screen. Our intention with keyboard controls is that the screen will scroll to the correct location instead. + //If we return false, then we handled it ourselves, no need to go back to the previous selection. + if(HandleOutsideDisabledButtonSelection(buttons[selection.y][selection.x])){ + handled=true; + } + } + if(!handled){ + // If the new selection of a button on this frame is disabled for some reason and we didn't handle it, we need to go back to what we had selected before. + selection=prevSelection; + } } - // If the new selection of a button on this frame is disabled for some reason, we need to go back to what we had selected before. } } @@ -489,4 +499,12 @@ Pixel Menu::GetRenderColor(){ col=WHITE*"ThemeGlobal.MenuUnfocusedColorMult"_F; } return col; +} + +bool Menu::HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton){ + if(disabledButton->parentComponent!=nullptr){ + return disabledButton->parentComponent->HandleOutsideDisabledButtonSelection(disabledButton); + }else{ + return false; + } } \ No newline at end of file diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 5bb809e4..4799bc76 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -72,6 +72,9 @@ private: void DrawScaledWindowBorder(Crawler*game,vf2d menuPos); void DrawTiledWindowBorder(Crawler*game,vf2d menuPos); + //This triggers if we use a keyboard/controller input to try and select some off-screen menu item. We should ideally follow the menu cursor. + bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton); + Pixel GetRenderColor(); }; diff --git a/Crawler/MenuComponent.cpp b/Crawler/MenuComponent.cpp index eb869857..6f11b9b4 100644 --- a/Crawler/MenuComponent.cpp +++ b/Crawler/MenuComponent.cpp @@ -74,4 +74,8 @@ bool MenuComponent::PointWithinParent(MenuComponent*child,vi2d drawPos){ }else{ return true; } -} \ No newline at end of file +} + +bool MenuComponent::HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton){ + return false; +}; \ No newline at end of file diff --git a/Crawler/MenuComponent.h b/Crawler/MenuComponent.h index 0a30fe7f..5f2cefd9 100644 --- a/Crawler/MenuComponent.h +++ b/Crawler/MenuComponent.h @@ -41,4 +41,6 @@ public: virtual MenuComponent*PickUpDraggableItem(); //We are attempting to drop draggable onto this item. If it's not allowed, return false. virtual bool DropDraggableItem(MenuComponent*draggable); + //A notification that a button outside the region has been selected. Return false if it's not allowed. + virtual bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton); }; \ No newline at end of file diff --git a/Crawler/ScrollableWindowComponent.h b/Crawler/ScrollableWindowComponent.h index 7148ea8f..cbeeff7f 100644 --- a/Crawler/ScrollableWindowComponent.h +++ b/Crawler/ScrollableWindowComponent.h @@ -18,7 +18,7 @@ protected: float scrollBarHoverTime=0; private: inline bool OnScreen(MenuComponent*component){ - return geom2d::overlaps(rect,geom2d::rect{component->rect.pos+V(A::SCROLL_OFFSET),component->rect.size}); + return geom2d::overlaps(rect,geom2d::rect{component->rect.pos+V(A::SCROLL_OFFSET)+vf2d{2,2},component->rect.size-vf2d{2,2}}); } public: inline ScrollableWindowComponent(MenuType parent,geom2d::rectrect,Decal*icon,MenuFunc onClick) @@ -157,4 +157,10 @@ public: virtual inline bool PointWithinParent(MenuComponent*child,vi2d drawPos)override{ return geom2d::overlaps(geom2d::rect{Menu::menus[parentMenu]->pos+rect.pos,rect.size},drawPos); } + virtual inline bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton)override{ + //Set the offset so the center is highlighted by this button. + V(A::SCROLL_OFFSET).y=-(disabledButton->rect.pos.y-disabledButton->rect.size.y/2); + V(A::SCROLL_OFFSET).y+=rect.size.y/2; + return true; + }; }; \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index 42635952..4d9a41a9 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 2116 +#define VERSION_BUILD 2121 #define stringify(a) stringify_(a) #define stringify_(a) #a