@ -49,6 +49,7 @@ INCLUDE_ITEM_CATEGORIES
INCLUDE_DATA
bool Menu : : MOUSE_NAVIGATION = true ;
vi2d Menu : : lastActiveMousePos { } ;
std : : vector < Menu * > Menu : : stack ;
std : : map < MenuType , Menu * > Menu : : menus ;
std : : string Menu : : themeSelection = " BlueDefault " ;
@ -112,23 +113,6 @@ void Menu::InitializeMenus(){
if ( menus . count ( type ) = = 0 ) {
ERR ( " WARNING! Menu Type " < < type < < " does not exist! " )
}
if ( menus [ type ] - > defaultButton . expired ( ) ) {
bool foundComponent = false ;
if ( menus [ type ] - > componentCount > 0 ) {
for ( auto & [ key , component ] : menus [ type ] - > components ) {
if ( component - > selectable ) {
menus [ type ] - > defaultButton = component ;
std : : cout < < std : : format ( " WARNING! Menu {} does not have a default button! Automatically choosing {} " , int ( type ) , component - > GetName ( ) ) < < std : : endl ;
foundComponent = true ;
break ;
}
}
}
if ( ! foundComponent ) {
std : : cout < < std : : format ( " WARNING! Menu {} does not have a default button and has no default components to set it automatically! " , int ( type ) ) < < std : : endl ;
}
}
for ( auto & [ key , value ] : menus [ type ] - > components ) {
value - > AfterCreate ( ) ;
}
@ -210,7 +194,7 @@ void Menu::Update(AiL*game){
if ( component - > GetHoverState ( game ) ) {
component - > hovered = true ;
itemHovered = true ;
selection = component ;
SetSelection ( std : : weak_ptr < MenuComponent > ( component ) ) ;
}
}
}
@ -311,14 +295,12 @@ void Menu::Draw(AiL*game){
void Menu : : OpenMenu ( MenuType menu , bool cover ) {
menus [ menu ] - > cover = cover ;
menus [ menu ] - > onOpenFunc ( menu ) ;
Data returnData ;
menus [ menu ] - > onOpenFunc ( menu , returnData ) ;
menus [ menu ] - > SetSelection ( returnData ) ;
stack . push_back ( menus [ menu ] ) ;
}
void Menu : : SetDefaultButton ( std : : weak_ptr < MenuComponent > button ) {
defaultButton = button ;
}
void Menu : : KeyboardButtonNavigation ( AiL * game , vf2d menuPos ) {
std : : weak_ptr < MenuComponent > prevSelection = selection ;
@ -333,33 +315,51 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){
if ( game - > KEY_UP . Pressed ( ) ) {
SetMouseNavigation ( false ) ;
if ( std : : holds_alternative < std : : string > ( nav . up ) & & std : : get < std : : string > ( nav . up ) . length ( ) > 0 ) selection = components [ std : : get < std : : string > ( nav . up ) ] ;
if ( std : : holds_alternative < std : : string > ( nav . up ) & & std : : get < std : : string > ( nav . up ) . length ( ) > 0 ) SetSelection ( std : : string_view ( std : : get < std : : string > ( nav . up ) ) ) ;
else
if ( std : : holds_alternative < MenuDataFunc > ( nav . up ) ) std : : get < MenuDataFunc > ( nav . up ) ( type ) ;
if ( std : : holds_alternative < MenuDataFunc > ( nav . up ) ) {
Data returnData ;
std : : get < MenuDataFunc > ( nav . up ) ( type , returnData ) ;
SetSelection ( returnData ) ;
}
}
if ( game - > KEY_DOWN . Pressed ( ) ) {
SetMouseNavigation ( false ) ;
if ( std : : holds_alternative < std : : string > ( nav . down ) & & std : : get < std : : string > ( nav . down ) . length ( ) > 0 ) selection = components [ std : : get < std : : string > ( nav . down ) ] ;
if ( std : : holds_alternative < std : : string > ( nav . down ) & & std : : get < std : : string > ( nav . down ) . length ( ) > 0 ) SetSelection ( std : : string_view ( std : : get < std : : string > ( nav . down ) ) ) ;
else
if ( std : : holds_alternative < MenuDataFunc > ( nav . down ) ) std : : get < MenuDataFunc > ( nav . down ) ( type ) ;
if ( std : : holds_alternative < MenuDataFunc > ( nav . down ) ) {
Data returnData ;
std : : get < MenuDataFunc > ( nav . down ) ( type , returnData ) ;
SetSelection ( returnData ) ;
}
}
if ( game - > KEY_LEFT . Pressed ( ) ) {
SetMouseNavigation ( false ) ;
if ( std : : holds_alternative < std : : string > ( nav . left ) & & std : : get < std : : string > ( nav . left ) . length ( ) > 0 ) selection = components [ std : : get < std : : string > ( nav . left ) ] ;
if ( std : : holds_alternative < std : : string > ( nav . left ) & & std : : get < std : : string > ( nav . left ) . length ( ) > 0 ) SetSelection ( std : : string_view ( std : : get < std : : string > ( nav . left ) ) ) ;
else
if ( std : : holds_alternative < MenuDataFunc > ( nav . left ) ) std : : get < MenuDataFunc > ( nav . left ) ( type ) ;
if ( std : : holds_alternative < MenuDataFunc > ( nav . left ) ) {
Data returnData ;
std : : get < MenuDataFunc > ( nav . left ) ( type , returnData ) ;
SetSelection ( returnData ) ;
}
}
if ( game - > KEY_RIGHT . Pressed ( ) ) {
SetMouseNavigation ( false ) ;
if ( std : : holds_alternative < std : : string > ( nav . right ) & & std : : get < std : : string > ( nav . right ) . length ( ) > 0 ) selection = components [ std : : get < std : : string > ( nav . right ) ] ;
if ( std : : holds_alternative < std : : string > ( nav . right ) & & std : : get < std : : string > ( nav . right ) . length ( ) > 0 ) SetSelection ( std : : string_view ( std : : get < std : : string > ( nav . right ) ) ) ;
else
if ( std : : holds_alternative < MenuDataFunc > ( nav . right ) ) std : : get < MenuDataFunc > ( nav . right ) ( type ) ;
if ( std : : holds_alternative < MenuDataFunc > ( nav . right ) ) {
Data returnData ;
std : : get < MenuDataFunc > ( nav . right ) ( type , returnData ) ;
SetSelection ( returnData ) ;
}
}
}
}
if ( game - > KEY_CONFIRM . Pressed ( ) ) {
SetMouseNavigation ( game - > GetMouse ( 0 ) . bPressed ) ; //If a click occurs we use mouse controls.
if ( game - > KEY_UP . Pressed ( ) | | game - > KEY_RIGHT . Pressed ( ) | | game - > KEY_LEFT . Pressed ( ) | | game - > KEY_DOWN . Pressed ( ) | |
game - > KEY_BACK . Pressed ( ) | | game - > KEY_CONFIRM . Pressed ( ) | | game - > KEY_START . Pressed ( ) | | game - > KEY_SELECT . Pressed ( ) | |
game - > KEY_SCROLLDOWN . Pressed ( ) | | game - > KEY_SCROLLUP . Pressed ( ) ) {
SetMouseNavigation ( game - > GetMouse ( Mouse : : LEFT ) . bPressed | | game - > GetMouse ( Mouse : : RIGHT ) . bPressed | | game - > GetMouse ( Mouse : : MIDDLE ) . bPressed ) ; //If a click occurs we use mouse controls.
buttonHoldTime = 0 ;
}
if ( & * prevSelection . lock ( ) ! = & * selection . lock ( ) ) {
@ -374,7 +374,7 @@ void Menu::KeyboardButtonNavigation(AiL*game,vf2d menuPos){
}
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 ;
SetSelection ( prevSelection ) ;
}
}
}
@ -479,6 +479,9 @@ void Menu::SetMouseNavigation(bool mouseNavigation){
if ( MOUSE_NAVIGATION & & ! mouseNavigation ) {
//When mouse navigation was enabled and now needs to be disabled, we store the mouse position.
lastActiveMousePos = game - > GetMousePos ( ) ;
if ( ! keyboardSelection . expired ( ) ) {
SetSelection ( keyboardSelection ) ;
}
}
MOUSE_NAVIGATION = mouseNavigation ;
} ;
@ -613,10 +616,6 @@ void Menu::AddChapterListener(std::weak_ptr<MenuComponent>component){
MenuFuncData : : MenuFuncData ( Menu & menu , AiL * const game , std : : weak_ptr < MenuComponent > component , std : : weak_ptr < ScrollableWindowComponent > parentComponent )
: menu ( menu ) , game ( game ) , component ( component ) , parentComponent ( parentComponent ) { }
void Menu : : SetSelection ( std : : weak_ptr < MenuComponent > button ) {
selection = button ;
}
void Menu : : SetupKeyboardNavigation ( MenuDataFunc onOpen , MenuInputGroups inputGroups , ButtonNavigationGroups navigationGroups ) {
this - > onOpenFunc = onOpen ;
this - > inputGroups = inputGroups ;
@ -627,7 +626,33 @@ const std::weak_ptr<MenuComponent>Menu::GetSelection()const{
return selection ;
}
const std : : weak_ptr < MenuComponent > Menu : : GetKeySelection ( ) const {
return keyboardSelection ;
}
void Menu : : SetSelection ( std : : weak_ptr < MenuComponent > button ) {
selection = button ;
if ( navigationGroups . count ( button . lock ( ) - > GetName ( ) ) | |
! button . lock ( ) - > parentComponent . expired ( ) & & navigationGroups . count ( button . lock ( ) - > parentComponent . lock ( ) - > GetName ( ) ) ) {
keyboardSelection = button ;
}
}
void Menu : : SetSelection ( std : : string_view button ) {
selection = Component < MenuComponent > ( type , button ) ;
selection = Component < MenuComponent > ( type , std : : string ( button ) ) ;
if ( navigationGroups . count ( std : : string ( button ) ) | |
! selection . lock ( ) - > parentComponent . expired ( ) & & navigationGroups . count ( selection . lock ( ) - > parentComponent . lock ( ) - > GetName ( ) ) ) {
keyboardSelection = selection ;
}
}
void Menu : : SetSelection ( std : : variant < std : : string , std : : weak_ptr < MenuComponent > > button ) {
if ( std : : holds_alternative < std : : string > ( button ) ) {
SetSelection ( std : : string_view ( std : : get < std : : string > ( button ) ) ) ;
} else
if ( std : : holds_alternative < std : : weak_ptr < MenuComponent > > ( button ) ) {
SetSelection ( std : : get < std : : weak_ptr < MenuComponent > > ( button ) ) ;
} else {
ERR ( " WARNING! Specified menu opening function does not hold neither a string nor a pointer to a component to use! " ) ;
}
}