2023-09-29 00:03:20 -05:00
#include "Crawler.h"
2023-10-07 16:26:03 -05:00
#include "MenuComponent.h"
2023-10-02 16:50:53 +00:00
#include "DEFINES.h"
#include "safemap.h"
2023-10-17 05:35:19 -05:00
#include "Item.h"
#include "MenuItemButton.h"
#include "ScrollableWindowComponent.h"
2023-09-29 00:03:20 -05:00
bool Menu :: MOUSE_NAVIGATION = true ;
std :: vector < Menu *> Menu :: stack ;
2023-10-11 16:20:07 -05:00
std :: map < MenuType , Menu *> Menu :: menus ;
2023-10-03 04:09:42 -05:00
std :: string Menu :: themeSelection = "BlueDefault" ;
safeunorderedmap < std :: string , Theme > Menu :: themes ;
2023-10-18 17:51:08 +00:00
safemap < ITCategory , std :: vector < MenuComponent *>> Menu :: inventoryListeners ;
2023-10-06 15:00:27 -05:00
const vf2d Menu :: CENTERED = { - 456 , - 456 };
2023-10-22 23:19:47 -05:00
std :: vector < MenuComponent *> Menu :: unhandledComponents ;
2023-10-24 03:03:34 -05:00
//////////////////////////////////////////////////////////////////////////////////////////////////
///__/////////////////////////////////////////////////////////////////////////////////////////////
//| |////////////////////////////////////////////////////////////////////////////////////////////
//| |/////WARNING! If you are adding something here you likely are adding another container with MenuComponent pointers in it right now.
//| |/////Because we are handling raw pointers, you must also add this container to the list of iterating search removal contains that occur in the
//| |/////DESTRUCTOR of MenuComponents!!!!! THIS IS NOT A DRILL!
//|__|////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
///__/////////////////////////////////////////////////////////////////////////////////////////////
//|__/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
2023-10-23 00:05:30 -05:00
MenuType Menu :: lastMenuTypeCreated ;
std :: string Menu :: lastRegisteredComponent ;
2023-09-29 00:03:20 -05:00
2023-10-02 16:50:53 +00:00
INCLUDE_GFX
2023-10-06 15:00:27 -05:00
extern vi2d WINDOW_SIZE ;
2023-10-02 16:50:53 +00:00
2023-10-12 21:12:10 -05:00
typedef Attribute A ;
2023-10-06 15:00:27 -05:00
Menu :: Menu ( vf2d pos , vf2d size )
2023-10-11 16:20:07 -05:00
: pos ( pos == CENTERED ? WINDOW_SIZE / 2 - size / 2 : vi2d { pos }), size ( size ){
2023-10-11 19:50:12 -05:00
r . Create ( size . x , size . y );
overlay . Create ( WINDOW_SIZE . x , WINDOW_SIZE . y );
2023-10-11 16:20:07 -05:00
}
2023-09-29 00:03:20 -05:00
2023-10-22 23:19:47 -05:00
Menu ::~ Menu (){
for ( auto key : components ){
delete key . second ;
}
}
2023-09-29 00:03:20 -05:00
void Menu :: InitializeMenus (){
stack . reserve ( 32 );
2023-10-15 12:58:39 -05:00
InitializeTestMenu ();
InitializeTestSubMenu ();
InitializeInventoryWindow ();
2023-10-20 22:49:12 -05:00
InitializeClassSelectionWindow ();
2023-10-22 02:07:03 -05:00
InitializeClassInfoWindow ();
2023-10-20 22:49:12 -05:00
InitializeMainMenuWindow ();
2023-09-29 00:03:20 -05:00
for ( MenuType type = TEST ; type < MenuType :: ENUM_END ; type = MenuType ( int ( type + 1 ))){
if ( menus . count ( type ) == 0 ){
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Menu Type " << type << " does not exist!" )
2023-09-29 00:03:20 -05:00
}
2023-10-17 05:35:19 -05:00
//Lock up everything once it's done.
menus [ type ] -> buttons . SetInitialized ();
menus [ type ] -> keyboardButtons . SetInitialized ();
2023-10-15 12:58:39 -05:00
for ( auto & key : menus [ type ] -> components ){
MenuComponent * component = key . second ;
component -> AfterCreate ();
}
2023-09-29 00:03:20 -05:00
}
2023-10-22 23:19:47 -05:00
if ( Menu :: unhandledComponents . size () > 0 ){
std :: cout << "WARNING! There are " << Menu :: unhandledComponents . size () << " components that were not added to any Menu! These have been leaked and should not be happening!" << std :: endl ;
std :: cout << "See below for a report of unhandled components:" << std :: endl ;
int count = 0 ;
for ( MenuComponent * component : Menu :: unhandledComponents ){
std :: cout << " \t Component " << ( count + 1 ) << ": " << std :: endl ;
2023-10-23 00:05:30 -05:00
std :: cout << " \t -Parent Menu:" << component -> parentMenu << " Label: " << component -> label << std :: endl ;
std :: cout << " \t\t Created Inside of Menu: " << component -> memoryLeakInfo . first << " // Last Component: " << component -> memoryLeakInfo . second << std :: endl ;
2023-10-22 23:19:47 -05:00
count ++ ;
}
2023-11-11 00:54:50 -06:00
ERR ( "" )
2023-10-22 23:19:47 -05:00
}
2023-09-29 00:03:20 -05:00
}
2023-10-15 12:58:39 -05:00
Menu * Menu :: CreateMenu ( MenuType type , vf2d pos , vf2d size ){
2023-11-11 00:54:50 -06:00
menus [ type ] = NEW Menu ( pos , size );
2023-10-23 00:05:30 -05:00
lastMenuTypeCreated = type ;
2023-10-15 12:58:39 -05:00
return menus . at ( type );
}
2023-10-03 02:34:26 -05:00
void Menu :: AddComponent ( std :: string key , MenuComponent * button ){
if ( button -> selectable ){
2023-10-17 05:35:19 -05:00
buttons . Unlock ();
if ( buttons . count ( button -> rect . pos . y )){
buttons . at ( button -> rect . pos . y ). push_back ( button );
} else {
buttons [ button -> rect . pos . y ]. push_back ( button );
}
2023-10-17 00:31:56 -05:00
if ( button -> selectableViaKeyboard ){
2023-10-17 05:35:19 -05:00
keyboardButtons . Unlock ();
if ( keyboardButtons . count ( button -> rect . pos . y )){
keyboardButtons . at ( button -> rect . pos . y ). push_back ( button );
} else {
keyboardButtons [ button -> rect . pos . y ]. push_back ( button );
}
}
//We must lock the values before calling sort. Sort seems to try and create new accesses.
buttons . SetInitialized ();
keyboardButtons . SetInitialized ();
//We make an assumption that menu components are supposed to be in left-to-right order. Sometimes we may add things out-of-order, so this fixes the problem by sorting the items afterwards.
std :: sort ( buttons [ button -> rect . pos . y ]. begin (), buttons [ button -> rect . pos . y ]. end (),[]( MenuComponent * c1 , MenuComponent * c2 ){
return c1 -> GetPos (). x < c2 -> GetPos (). x ;
});
if ( keyboardButtons . count ( button -> rect . pos . y )){ //Keyboard buttons may not necessarily contain this key...Let's be sure.
std :: sort ( keyboardButtons [ button -> rect . pos . y ]. begin (), keyboardButtons [ button -> rect . pos . y ]. end (),[]( MenuComponent * c1 , MenuComponent * c2 ){
return c1 -> GetPos (). x < c2 -> GetPos (). x ;
});
2023-10-17 00:31:56 -05:00
}
2023-10-03 02:34:26 -05:00
} else {
displayComponents . push_back ( button );
}
if ( components . count ( key )){
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Key " << key << " for this sub-menu already exists! Key names must be unique!" )
2023-10-03 02:34:26 -05:00
}
2023-10-15 14:59:35 -05:00
button -> name = key ;
2023-10-17 05:35:19 -05:00
components . Unlock (); //It's possible we can add a component later on, so we will make sure we remove the lock first.
2023-10-03 02:34:26 -05:00
components [ key ] = button ;
2023-10-17 05:35:19 -05:00
components . SetInitialized ();
2023-10-23 00:05:30 -05:00
lastRegisteredComponent = key ;
2023-10-22 23:19:47 -05:00
std :: erase_if ( Menu :: unhandledComponents ,[ & ]( MenuComponent * b1 ){ return b1 == button ;});
2023-09-29 00:03:20 -05:00
}
2023-10-06 17:19:02 -05:00
void Menu :: CheckClickAndPerformMenuSelect ( Crawler * game ){
if ( game -> GetMouse ( Mouse :: LEFT ). bReleased || game -> GetKey ( SPACE ). bReleased || game -> GetKey ( ENTER ). bReleased ){
MenuSelect ( game );
}
}
void Menu :: HoverMenuSelect ( Crawler * game ){
2023-10-12 19:35:16 -05:00
if ( selection == vi2d { - 1 , - 1 } || buttons [ selection . y ][ selection . x ] -> disabled ) return ;
2023-10-15 12:58:39 -05:00
if ( buttons [ selection . y ][ selection . x ] -> draggable ){
if ( buttonHoldTime < "ThemeGlobal.MenuHoldTime" _F ){
CheckClickAndPerformMenuSelect ( game );
} else {
2023-10-07 18:28:19 -05:00
draggingComponent = buttons [ selection . y ][ selection . x ] -> PickUpDraggableItem ();
buttonHoldTime = 0 ;
2023-10-06 17:19:02 -05:00
}
2023-10-15 12:58:39 -05:00
} else {
CheckClickAndPerformMenuSelect ( game );
}
2023-10-06 17:19:02 -05:00
}
2023-09-29 00:03:20 -05:00
void Menu :: MenuSelect ( Crawler * game ){
2023-10-12 19:35:16 -05:00
if ( selection == vi2d { - 1 , - 1 } || buttons [ selection . y ][ selection . x ] -> disabled ) return ;
2023-10-24 03:22:25 -05:00
buttons [ selection . y ][ selection . x ] -> onClick ( MenuFuncData { * this , game , buttons [ selection . y ][ selection . x ]});
2023-10-01 01:48:27 -05:00
if ( buttons [ selection . y ][ selection . x ] -> menuDest != MenuType :: ENUM_END ){
2023-10-01 01:11:53 -05:00
if ( stack . size () < 32 ){
2023-10-11 16:20:07 -05:00
stack . push_back ( menus [ buttons [ selection . y ][ selection . x ] -> menuDest ]); //Navigate to the next menu.
2023-10-01 01:11:53 -05:00
} else {
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Exceeded menu stack size limit!" )
2023-10-01 01:11:53 -05:00
}
}
2023-09-29 00:03:20 -05:00
}
void Menu :: Update ( Crawler * game ){
2023-10-07 18:28:19 -05:00
if ( draggingComponent == nullptr ){
HoverMenuSelect ( game );
}
2023-10-06 17:19:02 -05:00
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation () && geom2d :: line < float > ( lastActiveMousePos , game -> GetMousePos ()). length () >= "ThemeGlobal.MouseActivationDistance" _F || UsingMouseNavigation ()){
SetMouseNavigation ( true );
}
2023-10-01 01:11:53 -05:00
for ( auto & key : buttons ){
for ( auto & button : key . second ){
2023-10-12 19:35:16 -05:00
if ( ! button -> disabled ){
button -> hovered = false ;
}
2023-10-01 01:11:53 -05:00
}
2023-09-29 00:03:20 -05:00
}
2023-10-15 14:59:35 -05:00
bool itemHovered = false ;
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation ()){
2023-10-15 14:59:35 -05:00
if ( selection != vi2d { - 1 , - 1 }){
buttons [ selection . y ][ selection . x ] -> hovered = true ;
itemHovered = true ;
}
2023-09-29 00:03:20 -05:00
} else {
2023-10-12 21:12:10 -05:00
selection = { - 1 , - 1 };
2023-10-01 01:11:53 -05:00
for ( auto & key : buttons ){
2023-10-07 18:28:19 -05:00
int index = 0 ;
2023-10-01 01:11:53 -05:00
for ( auto & button : key . second ){
2023-10-12 19:35:16 -05:00
if ( ! button -> disabled ){
if ( button -> GetHoverState ( game )){
button -> hovered = true ;
2023-10-15 14:59:35 -05:00
itemHovered = true ;
2023-10-12 19:35:16 -05:00
selection . y = key . first ;
selection . x = index ;
}
2023-10-07 18:28:19 -05:00
}
2023-10-15 12:58:39 -05:00
index ++ ;
2023-10-07 18:28:19 -05:00
}
}
}
2023-10-17 01:21:00 -05:00
if ( itemHovered && draggingComponent == nullptr && selection != vi2d { - 1 , - 1 } && ((( ! UsingMouseNavigation () && ( game -> GetKey ( ENTER ). bHeld ) || game -> GetKey ( SPACE ). bHeld )) || game -> GetMouse ( Mouse :: LEFT ). bHeld )){
2023-10-15 14:59:35 -05:00
buttonHoldTime += game -> GetElapsedTime ();
} else {
buttonHoldTime = 0 ;
}
if ( draggingComponent != nullptr ){
MenuComponent * selectedComponent = nullptr ;
if ( selection != vi2d { - 1 , - 1 }){
selectedComponent = buttons [ selection . y ][ selection . x ];
}
auto ClearDraggingComponent = [ & ](){
delete draggingComponent ; //We know we allocated a new instance of this, so we will now free it.
draggingComponent = nullptr ;
};
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation ()){
2023-10-07 18:28:19 -05:00
if ( game -> GetKey ( ENTER ). bReleased || game -> GetKey ( SPACE ). bReleased ){
2023-10-15 14:59:35 -05:00
if ( selectedComponent == nullptr ){ //Dropping over an empty area.
ClearDraggingComponent ();
} else
2023-10-07 18:28:19 -05:00
if ( selectedComponent -> DropDraggableItem ( draggingComponent )){
2023-10-15 14:59:35 -05:00
ClearDraggingComponent ();
2023-10-07 18:28:19 -05:00
}
}
} else {
if ( game -> GetMouse ( Mouse :: LEFT ). bReleased ){
2023-10-15 14:59:35 -05:00
if ( selectedComponent == nullptr ){ //Dropping over an empty area.
ClearDraggingComponent ();
} else
2023-10-07 18:28:19 -05:00
if ( selectedComponent -> DropDraggableItem ( draggingComponent )){
2023-10-15 14:59:35 -05:00
ClearDraggingComponent ();
2023-10-01 01:11:53 -05:00
}
2023-09-29 00:03:20 -05:00
}
}
}
2023-10-01 01:11:53 -05:00
2023-10-06 15:00:27 -05:00
KeyboardButtonNavigation ( game , pos );
2023-10-01 01:11:53 -05:00
for ( auto & key : buttons ){
for ( auto & button : key . second ){
2023-10-12 18:35:45 -05:00
if ( button -> renderInMain ){
2023-10-12 19:35:16 -05:00
button -> _Update ( game );
2023-10-12 18:35:45 -05:00
}
2023-10-01 01:11:53 -05:00
}
2023-09-29 00:03:20 -05:00
}
2023-10-03 02:34:26 -05:00
for ( auto & component : displayComponents ){
2023-10-12 18:35:45 -05:00
if ( component -> renderInMain ){
2023-10-12 19:35:16 -05:00
component -> _Update ( game );
2023-10-12 18:35:45 -05:00
}
2023-10-03 02:34:26 -05:00
}
2023-09-29 00:03:20 -05:00
};
void Menu :: Draw ( Crawler * game ){
2023-10-03 04:09:42 -05:00
if ( GetCurrentTheme (). IsScaled ()){
2023-10-11 19:50:12 -05:00
DrawScaledWindowBackground ( game , pos );
2023-10-02 19:54:21 +00:00
} else {
2023-10-11 19:50:12 -05:00
DrawTiledWindowBackground ( game , pos );
2023-10-02 19:54:21 +00:00
}
2023-10-02 16:50:53 +00:00
2023-10-11 19:50:12 -05:00
game -> SetDrawTarget ( r . Sprite ());
Pixel :: Mode prevMode = game -> GetPixelMode ();
game -> SetPixelMode ( Pixel :: MASK );
game -> Clear ( BLANK );
2023-10-15 12:58:39 -05:00
for ( auto & component : displayComponents ){
if ( component -> renderInMain ){
2023-11-06 00:05:09 -06:00
component -> _Draw ( game );
2023-10-15 12:58:39 -05:00
}
}
2023-10-11 19:50:12 -05:00
for ( auto & key : buttons ){
for ( auto & button : key . second ){
2023-10-12 18:35:45 -05:00
if ( button -> renderInMain ){
2023-11-06 00:05:09 -06:00
button -> _Draw ( game );
2023-10-12 18:35:45 -05:00
}
2023-10-11 19:50:12 -05:00
}
}
game -> SetPixelMode ( prevMode );
game -> SetDrawTarget ( nullptr );
r . Decal () -> Update ();
game -> DrawDecal ( pos , r . Decal ());
2023-10-15 12:58:39 -05:00
for ( auto & component : displayComponents ){
if ( component -> renderInMain ){
2023-11-06 00:05:09 -06:00
component -> _DrawDecal ( game , this == Menu :: stack . back ());
2023-10-15 12:58:39 -05:00
}
}
2023-10-01 01:11:53 -05:00
for ( auto & key : buttons ){
for ( auto & button : key . second ){
2023-10-12 18:35:45 -05:00
if ( button -> renderInMain ){
2023-11-06 00:05:09 -06:00
button -> _DrawDecal ( game , this == Menu :: stack . back ());
2023-10-12 18:35:45 -05:00
}
2023-10-01 01:11:53 -05:00
}
2023-09-29 00:03:20 -05:00
}
2023-10-11 19:50:12 -05:00
if ( GetCurrentTheme (). IsScaled ()){
DrawScaledWindowBorder ( game , pos );
} else {
DrawTiledWindowBorder ( game , pos );
2023-10-03 02:34:26 -05:00
}
2023-10-07 18:28:19 -05:00
if ( draggingComponent != nullptr ){
2023-10-11 19:50:12 -05:00
game -> SetDrawTarget ( overlay . Sprite ());
Pixel :: Mode prevMode = game -> GetPixelMode ();
game -> SetPixelMode ( Pixel :: MASK );
game -> Clear ( BLANK );
2023-10-07 18:28:19 -05:00
vf2d offsetPos = draggingComponent -> rect . pos ;
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation ()){
2023-10-07 18:28:19 -05:00
MenuComponent * selectedComponent = buttons [ selection . y ][ selection . x ];
2023-10-12 21:12:10 -05:00
vf2d drawOffset {};
if ( selectedComponent -> parentComponent != nullptr ){
drawOffset += selectedComponent -> parentComponent -> V ( A :: SCROLL_OFFSET );
}
2023-10-24 04:52:24 -05:00
draggingComponent -> Draw ( game , drawOffset + pos - offsetPos + selectedComponent -> rect . pos + vi2d { 1 , - 4 });
2023-10-07 18:28:19 -05:00
} else {
2023-10-24 04:52:24 -05:00
draggingComponent -> Draw ( game , - offsetPos + game -> GetMousePos ());
2023-10-07 18:28:19 -05:00
}
2023-10-11 19:50:12 -05:00
game -> SetPixelMode ( prevMode );
game -> SetDrawTarget ( nullptr );
overlay . Decal () -> Update ();
2023-10-24 04:52:24 -05:00
game -> DrawDecal ({ 0 , 0 }, overlay . Decal (),{ 1 , 1 }, this == Menu :: stack . back () ? WHITE : WHITE * "ThemeGlobal.MenuUnfocusedColorMult" _F );
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation ()){
2023-10-11 19:50:12 -05:00
MenuComponent * selectedComponent = buttons [ selection . y ][ selection . x ];
2023-10-12 21:12:10 -05:00
vf2d drawOffset {};
if ( selectedComponent -> parentComponent != nullptr ){
drawOffset += selectedComponent -> parentComponent -> V ( A :: SCROLL_OFFSET );
}
draggingComponent -> DrawDecal ( game , drawOffset + pos - offsetPos + selectedComponent -> rect . pos + vi2d { 1 , - 4 }, this == Menu :: stack . back ());
2023-10-11 19:50:12 -05:00
} else {
2023-10-12 18:35:45 -05:00
draggingComponent -> DrawDecal ( game , - offsetPos + game -> GetMousePos (), this == Menu :: stack . back ());
2023-10-11 19:50:12 -05:00
}
2023-10-07 18:28:19 -05:00
}
2023-09-29 00:03:20 -05:00
};
void Menu :: OpenMenu ( MenuType menu ){
stack . clear ();
2023-10-11 16:20:07 -05:00
stack . push_back ( menus [ menu ]);
2023-10-01 01:11:53 -05:00
}
2023-10-02 16:50:53 +00:00
void Menu :: KeyboardButtonNavigation ( Crawler * game , vf2d menuPos ){
2023-10-12 19:35:16 -05:00
vi2d prevSelection = selection ;
2023-10-01 01:11:53 -05:00
if ( game -> GetKey ( RIGHT ). bPressed ){
if ( selection == vi2d { - 1 , - 1 }) return ;
2023-10-17 01:21:00 -05:00
SetMouseNavigation ( false );
2023-10-17 00:31:56 -05:00
selection . x = ( size_t ( selection . x ) + 1 ) % keyboardButtons [ selection . y ]. size ();
2023-10-01 01:11:53 -05:00
}
if ( game -> GetKey ( LEFT ). bPressed ){
if ( selection == vi2d { - 1 , - 1 }) return ;
selection . x -- ;
2023-10-17 01:21:00 -05:00
SetMouseNavigation ( false );
2023-10-17 00:31:56 -05:00
if ( selection . x < 0 ) selection . x += keyboardButtons [ selection . y ]. size ();
2023-10-01 01:11:53 -05:00
}
if ( game -> GetKey ( DOWN ). bPressed || game -> GetKey ( UP ). bPressed ){
if ( game -> GetKey ( DOWN ). bPressed ){
2023-10-17 01:21:00 -05:00
SetMouseNavigation ( false );
2023-10-01 01:11:53 -05:00
bool found = false ;
bool selectedItem = false ;
if ( selection == vi2d { - 1 , - 1 }){
//Highlight first item.
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-01 01:11:53 -05:00
selection . y = key . first ;
break ;
}
} else {
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-01 01:11:53 -05:00
if ( found ){ //Once we discover the previous element, the next element becomes our next selection.
2023-10-17 00:31:56 -05:00
int previousButtonX = keyboardButtons [ selection . y ][ selection . x ] -> rect . pos . x ;
2023-10-01 01:11:53 -05:00
selection . y = key . first ;
2023-10-01 01:18:27 -05:00
int index = 0 ;
for ( auto & button : key . second ){ //Try to match a button in the same column as this button first.
2023-10-01 01:48:27 -05:00
if ( previousButtonX == button -> rect . pos . x ){
2023-10-01 01:18:27 -05:00
selection . x = index ;
break ;
}
index ++ ;
}
2023-10-01 01:11:53 -05:00
selectedItem = true ;
break ;
}
2023-10-17 05:35:19 -05:00
if ( key . first == selection . y
2023-10-17 01:21:00 -05:00
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
2023-10-17 05:35:19 -05:00
&& selection . x >= 0 && selection . x < keyboardButtons [ selection . y ]. size ()){
2023-10-01 01:11:53 -05:00
found = true ;
}
}
if ( ! selectedItem ){ //This means we need to loop around instead and pick the first one.
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-01 01:11:53 -05:00
selection . y = key . first ;
break ;
}
}
}
}
if ( game -> GetKey ( UP ). bPressed ){
2023-10-17 01:21:00 -05:00
SetMouseNavigation ( false );
2023-10-01 01:11:53 -05:00
if ( selection == vi2d { - 1 , - 1 }){
//Highlight last item.
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-01 01:11:53 -05:00
selection . y = key . first ;
}
} else {
int prevInd =- 1 ;
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-17 01:21:00 -05:00
if ( key . first == selection . y &&
//It's entirely possible this button was selected from the button selection list and may be out-of-bounds here.
selection . x >= 0 && selection . x < keyboardButtons [ selection . y ]. size ()){
2023-10-01 01:11:53 -05:00
break ;
}
prevInd = key . first ;
}
if ( prevInd !=- 1 ){
2023-10-17 00:31:56 -05:00
int previousButtonX = keyboardButtons [ selection . y ][ selection . x ] -> rect . pos . x ;
2023-10-01 01:11:53 -05:00
selection . y = prevInd ;
2023-10-01 01:18:27 -05:00
int index = 0 ;
2023-10-17 00:31:56 -05:00
for ( auto & button : keyboardButtons [ prevInd ]){ //Try to match a button in the same column as this button first.
2023-10-01 01:48:27 -05:00
if ( previousButtonX == button -> rect . pos . x ){
2023-10-01 01:18:27 -05:00
selection . x = index ;
break ;
}
index ++ ;
}
2023-10-01 01:11:53 -05:00
} else { //Since we didn't find it, it means we're at the top of the list or the list is empty. Go to the last element and use that one.
int lastInd =- 1 ;
2023-10-17 00:31:56 -05:00
for ( auto & key : keyboardButtons ){
2023-10-01 01:11:53 -05:00
lastInd = key . first ;
}
selection . y = lastInd ;
}
}
}
//In both cases, we should clamp the X index to make sure it's still valid.
if ( selection . y !=- 1 ){
2023-10-17 00:31:56 -05:00
selection . x = std :: clamp ( selection . x , 0 , int ( keyboardButtons [ selection . y ]. size ()) - 1 );
2023-10-01 01:11:53 -05:00
} else {
selection . x =- 1 ;
}
}
2023-10-02 16:50:53 +00:00
if ( game -> GetMouse ( 0 ). bPressed || game -> GetKey ( ENTER ). bPressed || game -> GetKey ( SPACE ). bPressed ){
2023-10-17 01:21:00 -05:00
SetMouseNavigation ( game -> GetMouse ( 0 ). bPressed ); //If a click occurs we use mouse controls.
if ( ! UsingMouseNavigation ()){
2023-10-06 17:19:02 -05:00
buttonHoldTime = 0 ;
2023-10-02 16:50:53 +00:00
//Key presses automatically highlight the first button if it's not highlighted.
if ( selection == vi2d { - 1 , - 1 } && buttons . size () > 0 ){
//Find the first possible button entry in the map...
int firstInd =- 1 ;
for ( auto & key : buttons ){
if ( buttons [ key . first ]. size () > 0 ){
firstInd = key . first ;
break ;
}
}
if ( firstInd !=- 1 ){ //This means we found a valid menu item. If we didn't find one don't highlight any menu item...
selection = { 0 , firstInd };
}
}
} else { //Mouse click.
selection = { - 1 , - 1 };
for ( auto & key : buttons ){
int index = 0 ;
for ( auto & button : key . second ){
2023-10-12 21:12:10 -05:00
if ( ! button -> disabled ){
if ( geom2d :: overlaps ( geom2d :: rect < float > { button -> rect . pos + menuPos , button -> rect . size }, game -> GetMousePos ())){
selection = { index , key . first };
break ;
}
2023-10-02 16:50:53 +00:00
}
index ++ ;
}
}
2023-10-06 17:19:02 -05:00
buttonHoldTime = 0 ;
2023-10-02 16:50:53 +00:00
}
}
2023-10-12 19:35:16 -05:00
if ( prevSelection != selection ){
2023-10-15 12:58:39 -05:00
if ( selection != vi2d { - 1 , - 1 } && buttons [ selection . y ][ selection . x ] -> disabled ){
2023-10-17 00:50:58 -05:00
bool handled = false ;
2023-10-17 01:21:00 -05:00
if ( ! UsingMouseNavigation ()){
2023-10-17 00:50:58 -05:00
//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 ;
}
2023-10-15 12:58:39 -05:00
}
2023-10-12 19:35:16 -05:00
}
2023-10-02 19:54:21 +00:00
}
2023-10-11 19:50:12 -05:00
void Menu :: DrawScaledWindowBorder ( Crawler * game , vf2d menuPos ){
2023-10-02 19:54:21 +00:00
vf2d patchSize = { "Interface.9PatchSize" _f [ 0 ], "Interface.9PatchSize" _f [ 1 ]};
//Upper-Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos - patchSize , patchSize , GetPatchPart ( 0 , 0 ). Decal (),{ patchSize . x * 0 , patchSize . y * 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Upper-Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , - patchSize . y }, patchSize , GetPatchPart ( 2 , 0 ). Decal (),{ patchSize . x * 2 , patchSize . y * 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom-Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { - patchSize . x , size . y }, patchSize , GetPatchPart ( 0 , 2 ). Decal (),{ patchSize . x * 0 , patchSize . y * 2 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom-Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , size . y }, patchSize , GetPatchPart ( 2 , 2 ). Decal (),{ patchSize . x * 2 , patchSize . y * 2 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Top
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { 0 , - patchSize . y }, vf2d { size . x , patchSize . y }, GetPatchPart ( 1 , 0 ). Decal (),{ patchSize . x * 1 , patchSize . y * 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { - patchSize . x , 0 }, vf2d { patchSize . x , size . y }, GetPatchPart ( 0 , 1 ). Decal (),{ patchSize . x * 0 , patchSize . y * 1 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , 0 }, vf2d { patchSize . x , size . y }, GetPatchPart ( 2 , 1 ). Decal (),{ patchSize . x * 2 , patchSize . y * 1 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { 0 , size . y }, vf2d { size . x , patchSize . y }, GetPatchPart ( 1 , 2 ). Decal (),{ patchSize . x * 1 , patchSize . y * 2 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
}
2023-10-11 19:50:12 -05:00
void Menu :: DrawTiledWindowBorder ( Crawler * game , vf2d menuPos ){
2023-10-02 19:54:21 +00:00
vf2d patchSize = { "Interface.9PatchSize" _f [ 0 ], "Interface.9PatchSize" _f [ 1 ]};
//Upper-Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos - patchSize , patchSize , GetPatchPart ( 0 , 0 ). Decal (),{ 0 , 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Upper-Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , - patchSize . y }, patchSize , GetPatchPart ( 2 , 0 ). Decal (),{ 0 , 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom-Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { - patchSize . x , size . y }, patchSize , GetPatchPart ( 0 , 2 ). Decal (),{ 0 , 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom-Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , size . y }, patchSize , GetPatchPart ( 2 , 2 ). Decal (),{ 0 , 0 }, patchSize , GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Top
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { 0 , - patchSize . y }, vf2d { size . x , patchSize . y }, GetPatchPart ( 1 , 0 ). Decal (),{ 0 , 0 }, vf2d { size . x , patchSize . y }, GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Left
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { - patchSize . x , 0 }, vf2d { patchSize . x , size . y }, GetPatchPart ( 0 , 1 ). Decal (),{ 0 , 0 }, vf2d { patchSize . x , size . y }, GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Right
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { size . x , 0 }, vf2d { patchSize . x , size . y }, GetPatchPart ( 2 , 1 ). Decal (),{ 0 , 0 }, vf2d { patchSize . x , size . y }, GetRenderColor ());
2023-10-02 19:54:21 +00:00
//Bottom
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos + vf2d { 0 , size . y }, vf2d { size . x , patchSize . y }, GetPatchPart ( 1 , 2 ). Decal (),{ 0 , 0 }, vf2d { size . x , patchSize . y }, GetRenderColor ());
2023-10-11 19:50:12 -05:00
}
void Menu :: DrawScaledWindowBackground ( Crawler * game , vf2d menuPos ){
vf2d patchSize = { "Interface.9PatchSize" _f [ 0 ], "Interface.9PatchSize" _f [ 1 ]};
//Center
if ( GetCurrentTheme (). HasBackground ()){
Decal * back = GetCurrentTheme (). GetBackground ();
game -> DrawPartialDecal ( menuPos , size , back ,{ 0 , 0 }, back -> sprite -> Size (), GetRenderColor ());
} else {
game -> DrawPartialDecal ( menuPos , size , GetPatchPart ( 1 , 1 ). Decal (),{ patchSize . x * 1 , patchSize . y * 1 }, patchSize , GetRenderColor ());
}
}
void Menu :: DrawTiledWindowBackground ( Crawler * game , vf2d menuPos ){
vf2d patchSize = { "Interface.9PatchSize" _f [ 0 ], "Interface.9PatchSize" _f [ 1 ]};
2023-10-02 19:54:21 +00:00
//Center
2023-10-03 04:09:42 -05:00
if ( GetCurrentTheme (). HasBackground ()){
Decal * back = GetCurrentTheme (). GetBackground ();
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos , size , back ,{ 0 , 0 }, size , GetRenderColor ());
2023-10-03 04:09:42 -05:00
} else {
2023-10-05 00:42:28 -05:00
game -> DrawPartialDecal ( menuPos , size , GetPatchPart ( 1 , 1 ). Decal (),{ 0 , 0 }, patchSize , GetRenderColor ());
2023-10-03 04:09:42 -05:00
}
2023-10-02 19:54:21 +00:00
}
Renderable & Menu :: GetPatchPart ( int x , int y ){
2023-10-03 02:34:26 -05:00
return GFX [ themeSelection + "_" + std :: to_string ( x ) + std :: to_string ( y ) + ".png" ];
2023-10-03 04:09:42 -05:00
}
Theme & Menu :: GetCurrentTheme (){
return themes [ themeSelection ];
2023-10-05 00:42:28 -05:00
}
Pixel Menu :: GetRenderColor (){
bool focused = Menu :: stack . back () == this ;
Pixel col = WHITE ;
if ( ! focused ){
col = WHITE * "ThemeGlobal.MenuUnfocusedColorMult" _F ;
}
return col ;
2023-10-17 00:50:58 -05:00
}
bool Menu :: HandleOutsideDisabledButtonSelection ( MenuComponent * disabledButton ){
if ( disabledButton -> parentComponent != nullptr ){
return disabledButton -> parentComponent -> HandleOutsideDisabledButtonSelection ( disabledButton );
} else {
return false ;
}
2023-10-17 01:21:00 -05:00
}
bool Menu :: UsingMouseNavigation (){
return MOUSE_NAVIGATION ;
};
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.
INCLUDE_game
lastActiveMousePos = game -> GetMousePos ();
}
MOUSE_NAVIGATION = mouseNavigation ;
2023-10-17 05:35:19 -05:00
};
void Menu :: InventorySlotsUpdated ( ITCategory cat ){
//Update the inventory with a new inventory slot, since there's one additional item to interact with now.
std :: vector < std :: string >& inv = Inventory :: get ( cat );
2023-10-18 16:06:08 +00:00
for ( MenuComponent * component : inventoryListeners . at ( cat )){
component -> OnInventorySlotsUpdate ( cat );
}
}
void Menu :: AddInventoryListener ( MenuComponent * component , ITCategory category ){
if ( inventoryListeners . count ( category )){
std :: vector < MenuComponent *>& listenerList = inventoryListeners . at ( category );
if ( std :: find ( listenerList . begin (), listenerList . end (), component ) != listenerList . end ()){
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Component " << component -> name << " has already been added to the " << category << " listener list! There should not be any duplicates!!" )
2023-10-18 16:06:08 +00:00
}
listenerList . push_back ( component );
2023-10-17 05:35:19 -05:00
} else {
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Inventory category " << category << " does not exist!" )
2023-10-17 05:35:19 -05:00
}
2023-10-20 22:49:12 -05:00
}
vf2d Menu :: center (){
2023-10-21 00:33:46 -05:00
return size / 2 ;
2023-10-22 21:56:09 -05:00
}
void Menu :: CloseMenu (){
if ( stack . size () > 0 ){
stack . pop_back ();
} else {
2023-11-11 00:54:50 -06:00
ERR ( "WARNING! Trying to close out no menu?? Why are we doing this?" )
2023-10-22 21:56:09 -05:00
}
2023-10-23 00:05:30 -05:00
}
std :: pair < MenuType , std :: string > Menu :: GetMemoryLeakReportInfo (){
return { lastMenuTypeCreated , lastRegisteredComponent };
2023-10-24 03:22:25 -05:00
}
void Menu :: CloseAllMenus (){
2023-11-11 00:54:50 -06:00
if ( stack . size () > 0 ){
stack . clear ();
} else {
ERR ( "WARNING! Trying to close out no menu?? Why are we doing this?" )
}
}
bool Menu :: IsMenuOpen (){
return stack . size () > 0 ;
}
void Menu :: CleanupAllMenus (){
2023-10-17 05:35:19 -05:00
}