diff --git a/Crawler/Menu.cpp b/Crawler/Menu.cpp index 7121b584..0e590de6 100644 --- a/Crawler/Menu.cpp +++ b/Crawler/Menu.cpp @@ -100,6 +100,10 @@ void Menu::Update(Crawler*game){ HoverMenuSelect(game); } + if(!UsingMouseNavigation()&&geom2d::line(lastActiveMousePos,game->GetMousePos()).length()>="ThemeGlobal.MouseActivationDistance"_F||UsingMouseNavigation()){ + SetMouseNavigation(true); + } + for(auto&key:buttons){ for(auto&button:key.second){ if(!button->disabled){ @@ -110,7 +114,7 @@ void Menu::Update(Crawler*game){ bool itemHovered=false; - if(!MOUSE_NAVIGATION){ + if(!UsingMouseNavigation()){ if(selection!=vi2d{-1,-1}){ buttons[selection.y][selection.x]->hovered=true; itemHovered=true; @@ -133,7 +137,7 @@ void Menu::Update(Crawler*game){ } } - if(itemHovered&&draggingComponent==nullptr&&selection!=vi2d{-1,-1}&&(((!MOUSE_NAVIGATION&&(game->GetKey(ENTER).bHeld)||game->GetKey(SPACE).bHeld))||game->GetMouse(Mouse::LEFT).bHeld)){ + if(itemHovered&&draggingComponent==nullptr&&selection!=vi2d{-1,-1}&&(((!UsingMouseNavigation()&&(game->GetKey(ENTER).bHeld)||game->GetKey(SPACE).bHeld))||game->GetMouse(Mouse::LEFT).bHeld)){ buttonHoldTime+=game->GetElapsedTime(); }else{ buttonHoldTime=0; @@ -151,7 +155,7 @@ void Menu::Update(Crawler*game){ draggingComponent=nullptr; }; - if(!MOUSE_NAVIGATION){ + if(!UsingMouseNavigation()){ if(game->GetKey(ENTER).bReleased||game->GetKey(SPACE).bReleased){ if(selectedComponent==nullptr){//Dropping over an empty area. ClearDraggingComponent(); @@ -239,7 +243,7 @@ void Menu::Draw(Crawler*game){ game->SetPixelMode(Pixel::MASK); game->Clear(BLANK); vf2d offsetPos=draggingComponent->rect.pos; - if(!MOUSE_NAVIGATION){ + if(!UsingMouseNavigation()){ MenuComponent*selectedComponent=buttons[selection.y][selection.x]; vf2d drawOffset{}; if(selectedComponent->parentComponent!=nullptr){ @@ -253,7 +257,7 @@ void Menu::Draw(Crawler*game){ game->SetDrawTarget(nullptr); overlay.Decal()->Update(); game->DrawDecal({0,0},overlay.Decal()); - if(!MOUSE_NAVIGATION){ + if(!UsingMouseNavigation()){ MenuComponent*selectedComponent=buttons[selection.y][selection.x]; vf2d drawOffset{}; if(selectedComponent->parentComponent!=nullptr){ @@ -275,17 +279,18 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ vi2d prevSelection=selection; if(game->GetKey(RIGHT).bPressed){ if(selection==vi2d{-1,-1})return; - MOUSE_NAVIGATION=false; + SetMouseNavigation(false); selection.x=(size_t(selection.x)+1)%keyboardButtons[selection.y].size(); } if(game->GetKey(LEFT).bPressed){ if(selection==vi2d{-1,-1})return; selection.x--; + SetMouseNavigation(false); if(selection.x<0)selection.x+=keyboardButtons[selection.y].size(); } if(game->GetKey(DOWN).bPressed||game->GetKey(UP).bPressed){ if(game->GetKey(DOWN).bPressed){ - MOUSE_NAVIGATION=false; + SetMouseNavigation(false); bool found=false; bool selectedItem=false; if(selection==vi2d{-1,-1}){ @@ -310,7 +315,9 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ selectedItem=true; break; } - if(key.first==selection.y){ + 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.xGetKey(UP).bPressed){ - MOUSE_NAVIGATION=false; - + SetMouseNavigation(false); if(selection==vi2d{-1,-1}){ //Highlight last item. for(auto&key:keyboardButtons){ @@ -333,7 +339,9 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ }else{ int prevInd=-1; for(auto&key:keyboardButtons){ - if(key.first==selection.y){ + 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.xGetMouse(0).bPressed||game->GetKey(ENTER).bPressed||game->GetKey(SPACE).bPressed){ - MOUSE_NAVIGATION=game->GetMouse(0).bPressed; //If a click occurs we use mouse controls. - if(!MOUSE_NAVIGATION){ + SetMouseNavigation(game->GetMouse(0).bPressed); //If a click occurs we use mouse controls. + if(!UsingMouseNavigation()){ buttonHoldTime=0; //Key presses automatically highlight the first button if it's not highlighted. if(selection==vi2d{-1,-1}&&buttons.size()>0){ @@ -403,7 +411,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ if(prevSelection!=selection){ if(selection!=vi2d{-1,-1}&&buttons[selection.y][selection.x]->disabled){ bool handled=false; - if(!MOUSE_NAVIGATION){ + if(!UsingMouseNavigation()){ //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])){ @@ -507,4 +515,17 @@ bool Menu::HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton){ }else{ return false; } -} \ No newline at end of file +} + + +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; +}; \ No newline at end of file diff --git a/Crawler/Menu.h b/Crawler/Menu.h index 4799bc76..87891d44 100644 --- a/Crawler/Menu.h +++ b/Crawler/Menu.h @@ -23,13 +23,13 @@ enum MenuType{ class Menu:IAttributable{ friend class Crawler; friend class Player; - static bool MOUSE_NAVIGATION; float buttonHoldTime=0; std::map>buttons; //Buttons are stored in rows followed by their column order. std::map>keyboardButtons; //Button ordered storage for keyboard/menu std::vectordisplayComponents; //Components that are only for displaying purposes. vi2d selection={-1,-1}; + vi2d lastActiveMousePos={}; MenuComponent*draggingComponent=nullptr; Renderable r,overlay; @@ -52,6 +52,8 @@ public: vf2d size; //Size in tiles (24x24), every menu will be tile-based static Theme&GetCurrentTheme(); + bool UsingMouseNavigation(); + void SetMouseNavigation(bool mouseNavigation); private: Menu(vf2d pos,vf2d size); @@ -76,6 +78,7 @@ private: bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton); Pixel GetRenderColor(); + static bool MOUSE_NAVIGATION; }; struct MenuFuncData{ diff --git a/Crawler/Version.h b/Crawler/Version.h index 4d9a41a9..f2a7629d 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 2121 +#define VERSION_BUILD 2127 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Crawler/assets/config/gfx/themes.txt b/Crawler/assets/config/gfx/themes.txt index 7a8bcf7d..31100bf1 100644 --- a/Crawler/assets/config/gfx/themes.txt +++ b/Crawler/assets/config/gfx/themes.txt @@ -10,6 +10,9 @@ ThemeGlobal MenuScrollWheelSpeed = 8 # How fast the menu scrolls via clicking the Scroll Arrow Buttons MenuButtonScrollSpeed = 16 + + # How far the mouse has to move before mouse mode becomes active from keyboard/controller input mode. + MouseActivationDistance = 8 } Themes