Keyboard menu button storage can now be differentiated from internal button storage.

pull/28/head
sigonasr2 1 year ago
parent daf94db505
commit 958a1866c7
  1. 27
      Crawler/Menu.cpp
  2. 2
      Crawler/Menu.h
  3. 8
      Crawler/MenuComponent.cpp
  4. 5
      Crawler/MenuComponent.h
  5. 6
      Crawler/ScrollableWindowComponent.h
  6. 2
      Crawler/Version.h

@ -48,6 +48,9 @@ Menu*Menu::CreateMenu(MenuType type,vf2d pos,vf2d size){
void Menu::AddComponent(std::string key,MenuComponent*button){
if(button->selectable){
buttons[button->rect.pos.y].push_back(button);
if(button->selectableViaKeyboard){
keyboardButtons[button->rect.pos.y].push_back(button);
}
}else{
displayComponents.push_back(button);
}
@ -273,12 +276,12 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
if(game->GetKey(RIGHT).bPressed){
if(selection==vi2d{-1,-1})return;
MOUSE_NAVIGATION=false;
selection.x=(size_t(selection.x)+1)%buttons[selection.y].size();
selection.x=(size_t(selection.x)+1)%keyboardButtons[selection.y].size();
}
if(game->GetKey(LEFT).bPressed){
if(selection==vi2d{-1,-1})return;
selection.x--;
if(selection.x<0)selection.x+=buttons[selection.y].size();
if(selection.x<0)selection.x+=keyboardButtons[selection.y].size();
}
if(game->GetKey(DOWN).bPressed||game->GetKey(UP).bPressed){
if(game->GetKey(DOWN).bPressed){
@ -287,14 +290,14 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
bool selectedItem=false;
if(selection==vi2d{-1,-1}){
//Highlight first item.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
break;
}
}else{
for(auto&key:buttons){
for(auto&key:keyboardButtons){
if(found){ //Once we discover the previous element, the next element becomes our next selection.
int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x;
int previousButtonX=keyboardButtons[selection.y][selection.x]->rect.pos.x;
selection.y=key.first;
int index=0;
for(auto&button:key.second){ //Try to match a button in the same column as this button first.
@ -312,7 +315,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}
if(!selectedItem){ //This means we need to loop around instead and pick the first one.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
break;
}
@ -324,22 +327,22 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
if(selection==vi2d{-1,-1}){
//Highlight last item.
for(auto&key:buttons){
for(auto&key:keyboardButtons){
selection.y=key.first;
}
}else{
int prevInd=-1;
for(auto&key:buttons){
for(auto&key:keyboardButtons){
if(key.first==selection.y){
break;
}
prevInd=key.first;
}
if(prevInd!=-1){
int previousButtonX=buttons[selection.y][selection.x]->rect.pos.x;
int previousButtonX=keyboardButtons[selection.y][selection.x]->rect.pos.x;
selection.y=prevInd;
int index=0;
for(auto&button:buttons[prevInd]){ //Try to match a button in the same column as this button first.
for(auto&button:keyboardButtons[prevInd]){ //Try to match a button in the same column as this button first.
if(previousButtonX==button->rect.pos.x){
selection.x=index;
break;
@ -348,7 +351,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
}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;
for(auto&key:buttons){
for(auto&key:keyboardButtons){
lastInd=key.first;
}
selection.y=lastInd;
@ -357,7 +360,7 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){
}
//In both cases, we should clamp the X index to make sure it's still valid.
if(selection.y!=-1){
selection.x=std::clamp(selection.x,0,int(buttons[selection.y].size())-1);
selection.x=std::clamp(selection.x,0,int(keyboardButtons[selection.y].size())-1);
}else{
selection.x=-1;
}

@ -27,7 +27,7 @@ class Menu:IAttributable{
float buttonHoldTime=0;
std::map<int/*Y*/,std::vector<MenuComponent*>>buttons; //Buttons are stored in rows followed by their column order.
std::map<int/*Y*/,std::vector<MenuComponent*>>newButtonArrangement; //The new setup that the buttons will be at if a drag operation completes.
std::map<int/*Y*/,std::vector<MenuComponent*>>keyboardButtons; //Button ordered storage for keyboard/menu
std::vector<MenuComponent*>displayComponents; //Components that are only for displaying purposes.
vi2d selection={-1,-1};

@ -1,11 +1,11 @@
#include "Crawler.h"
#include "MenuComponent.h"
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable)
:parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable){}
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable,bool selectableViaKeyboard)
:parentMenu(parent),rect(rect),label(label),menuDest(MenuType::ENUM_END),onClick(onClick),hoverEffect(0),selectable(selectable),selectableViaKeyboard(selectableViaKeyboard){}
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable)
:MenuComponent(parent,rect,label,onClick,selectable){
MenuComponent::MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable,bool selectableViaKeyboard)
:MenuComponent(parent,rect,label,onClick,selectable,selectableViaKeyboard){
this->menuDest=menuDest;
}

@ -22,6 +22,7 @@ protected:
MenuComponent*parentComponent=nullptr;
bool hovered=false;
bool selectable=true;
bool selectableViaKeyboard=true;
bool disabled=false; //If set to true, this component will not be rendered or updated.
bool renderInMain=true; //If set to false, this component is the responsibility of some other windowing system and won't be rendered or updated via the main window loop.
virtual void Update(Crawler*game);
@ -30,8 +31,8 @@ protected:
virtual bool GetHoverState(Crawler*game,MenuComponent*child);
virtual void AfterCreate(); //Called after the creation of all menus finish.
public:
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true);
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true);
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuFunc onClick,bool selectable=true,bool selectableViaKeyboard=true);
MenuComponent(MenuType parent,geom2d::rect<float>rect,std::string label,MenuType menuDest,MenuFunc onClick,bool selectable=true,bool selectableViaKeyboard=true);
void _Update(Crawler*game);
void _Draw(Crawler*game,vf2d parentPos,bool focused);
void _DrawDecal(Crawler*game,vf2d parentPos,bool focused);

@ -31,8 +31,8 @@ public:
}
private:
virtual inline void AfterCreate()override{
upButton=new MenuComponent(parentMenu,{vf2d{rect.size.x-12,0},{12,12}},"^",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y+="ThemeGlobal.MenuButtonScrollSpeed"_I;});
downButton=new MenuComponent(parentMenu,{rect.size-vf2d{12,12},{12,12}},"v",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y-="ThemeGlobal.MenuButtonScrollSpeed"_I;});
upButton=new MenuComponent(parentMenu,{vf2d{rect.size.x-12,0},{12,12}},"^",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y+="ThemeGlobal.MenuButtonScrollSpeed"_I;},true,false);
downButton=new MenuComponent(parentMenu,{rect.size-vf2d{12,12},{12,12}},"v",[&](MenuFuncData dat){V(A::SCROLL_OFFSET).y-="ThemeGlobal.MenuButtonScrollSpeed"_I;},true,false);
//Let's use the internal name of this component to add unique names for sub-components.
Menu::menus[parentMenu]->AddComponent(name+upButton->rect.pos.str()+"_"+upButton->rect.size.str(),upButton);
Menu::menus[parentMenu]->AddComponent(name+downButton->rect.pos.str()+"_"+downButton->rect.size.str(),downButton);
@ -62,7 +62,7 @@ protected:
float scrollBarScale=(spaceBetweenTopAndBottomArrows/totalContentHeight);
//The scroll amount moves centered on the position the mouse is at.
float newScrollbarTop=(game->GetMousePos().y-windowAbsPos.y-12)-scrollBarHeight/2;
V(A::SCROLL_OFFSET).y=(-newScrollbarTop+1)/scrollBarScale;
V(A::SCROLL_OFFSET).y=(-newScrollbarTop+1)/scrollBarScale; //Derived formula from the draw code.
}
}else{
scrollBarHoverTime=std::max(scrollBarHoverTime-game->GetElapsedTime(),0.f);

@ -2,7 +2,7 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_PATCH 0
#define VERSION_BUILD 2114
#define VERSION_BUILD 2116
#define stringify(a) stringify_(a)
#define stringify_(a) #a

Loading…
Cancel
Save