[Add] Arrow navigation in menus.
This commit is contained in:
parent
c55b71e07f
commit
ae5152e7e4
@ -6,6 +6,7 @@ Button::Button(void) {
|
|||||||
_text = "";
|
_text = "";
|
||||||
_font = NULL;
|
_font = NULL;
|
||||||
_highlighted = false;
|
_highlighted = false;
|
||||||
|
_selected = false;
|
||||||
_triggered = false;
|
_triggered = false;
|
||||||
x = 0;
|
x = 0;
|
||||||
y = 0;
|
y = 0;
|
||||||
@ -33,7 +34,7 @@ void Button::Update(void) {
|
|||||||
|
|
||||||
void Button::Render(void) {
|
void Button::Render(void) {
|
||||||
if(_font) {
|
if(_font) {
|
||||||
if(_highlighted) {
|
if(_highlighted || _selected) {
|
||||||
_font->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
|
_font->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
} else {
|
} else {
|
||||||
_font->SetColor(0.5f, 0.5f, 0.5f, 1.0f);
|
_font->SetColor(0.5f, 0.5f, 0.5f, 1.0f);
|
||||||
|
@ -19,6 +19,9 @@ public:
|
|||||||
bool IsHighlighted(void) const { return _highlighted; }
|
bool IsHighlighted(void) const { return _highlighted; }
|
||||||
void SetHighlighted(bool highlighted) { _highlighted = highlighted; }
|
void SetHighlighted(bool highlighted) { _highlighted = highlighted; }
|
||||||
|
|
||||||
|
bool IsSelected(void) const { return _selected; }
|
||||||
|
void SetSelected(bool selected) { _selected = selected; }
|
||||||
|
|
||||||
bool Triggered(void) const { return _triggered; }
|
bool Triggered(void) const { return _triggered; }
|
||||||
|
|
||||||
int GetX(void) const { return x; }
|
int GetX(void) const { return x; }
|
||||||
@ -35,6 +38,7 @@ private:
|
|||||||
String _text;
|
String _text;
|
||||||
|
|
||||||
bool _highlighted;
|
bool _highlighted;
|
||||||
|
bool _selected;
|
||||||
bool _triggered;
|
bool _triggered;
|
||||||
|
|
||||||
int x;
|
int x;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
#include "Button.h"
|
#include "Button.h"
|
||||||
|
#include "../IO/Input.h"
|
||||||
|
|
||||||
Menu::Menu(void) {
|
Menu::Menu(void) {
|
||||||
_triggeredButton = -1;
|
_triggeredButton = -1;
|
||||||
|
_selectedButton = 0;
|
||||||
x = 0;
|
x = 0;
|
||||||
y = 0;
|
y = 0;
|
||||||
}
|
}
|
||||||
@ -15,6 +17,11 @@ Menu::~Menu(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Menu::AddButton(Button* button) {
|
void Menu::AddButton(Button* button) {
|
||||||
|
// Select first button.
|
||||||
|
if(_buttons.empty()) {
|
||||||
|
button->SetSelected(true);
|
||||||
|
}
|
||||||
|
|
||||||
_buttons.push_back(button);
|
_buttons.push_back(button);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +40,15 @@ void Menu::AlignButtons(int how) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Menu::SelectButton(int index) {
|
||||||
|
int buttonsIndex = 0;
|
||||||
|
for(std::list<Button*>::iterator i = _buttons.begin(); i != _buttons.end(); ++i) {
|
||||||
|
(*i)->SetSelected(buttonsIndex == index);
|
||||||
|
buttonsIndex++;
|
||||||
|
}
|
||||||
|
_selectedButton = index;
|
||||||
|
}
|
||||||
|
|
||||||
void Menu::Update(void) {
|
void Menu::Update(void) {
|
||||||
_triggeredButton = -1;
|
_triggeredButton = -1;
|
||||||
|
|
||||||
@ -52,6 +68,25 @@ void Menu::Update(void) {
|
|||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(KeyDown(SDLK_DOWN)) {
|
||||||
|
_selectedButton++;
|
||||||
|
if(_selectedButton == _buttons.size()) {
|
||||||
|
_selectedButton = 0;
|
||||||
|
}
|
||||||
|
SelectButton(_selectedButton);
|
||||||
|
}
|
||||||
|
else if(KeyDown(SDLK_UP)) {
|
||||||
|
_selectedButton--;
|
||||||
|
if(_selectedButton < 0) {
|
||||||
|
_selectedButton = _buttons.size() - 1;
|
||||||
|
}
|
||||||
|
SelectButton(_selectedButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(KeyDown(SDLK_RETURN)) {
|
||||||
|
_triggeredButton = _selectedButton;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::Render(void) {
|
void Menu::Render(void) {
|
||||||
|
@ -16,6 +16,7 @@ public:
|
|||||||
|
|
||||||
void AddButton(Button* button);
|
void AddButton(Button* button);
|
||||||
void AlignButtons(int how);
|
void AlignButtons(int how);
|
||||||
|
void SelectButton(int index);
|
||||||
|
|
||||||
void Update(void);
|
void Update(void);
|
||||||
void Render(void);
|
void Render(void);
|
||||||
@ -31,6 +32,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::list<Button*> _buttons;
|
std::list<Button*> _buttons;
|
||||||
int _triggeredButton;
|
int _triggeredButton;
|
||||||
|
int _selectedButton;
|
||||||
|
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
Loading…
Reference in New Issue
Block a user