Keyboard/Controller input left direction bugfix. Using setter/getter for mouse navigation to handl storing of position whenever mouse navigation is called.

This commit is contained in:
2023-10-17 01:21:00 -05:00
parent 340690a26b
commit 6656e3d036
4 changed files with 44 additions and 17 deletions
+36 -15
View File
@@ -100,6 +100,10 @@ void Menu::Update(Crawler*game){
HoverMenuSelect(game);
}
if(!UsingMouseNavigation()&&geom2d::line<float>(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.x<keyboardButtons[selection.y].size()){
found=true;
}
}
@@ -323,8 +330,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}
if(game->GetKey(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.x<keyboardButtons[selection.y].size()){
break;
}
prevInd=key.first;
@@ -366,8 +374,8 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}
if(game->GetMouse(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;
}
}
}
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;
};