[ADD] In-game menu navigation using arrow keys.

This commit is contained in:
Tamir Atias 2012-01-10 04:20:52 +02:00
parent 3ae7e7dc17
commit dba8045011
5 changed files with 105 additions and 9 deletions

View File

@ -285,10 +285,6 @@
RelativePath="..\..\..\src\libUnuk\MemManager.h" RelativePath="..\..\..\src\libUnuk\MemManager.h"
> >
</File> </File>
<File
RelativePath="..\..\..\src\libUnuk\Node.h"
>
</File>
<File <File
RelativePath="..\..\..\src\libUnuk\NPC.cpp" RelativePath="..\..\..\src\libUnuk\NPC.cpp"
> >

View File

@ -1,7 +1,8 @@
#include "Button.h" #include "Button.h"
Button::Button(void) { Button::Button(void) {
_highlighted = false;
_mouseOver = false;
} }
Button::~Button(void) { Button::~Button(void) {
@ -30,6 +31,16 @@ void Button::SetOverRGB(SDL_Color colour) {
_mouseOverColour = colour; _mouseOverColour = colour;
} }
void Button::SetHighlightRGB(Uint8 r, Uint8 g, Uint8 b) {
_highlightColour.r = r;
_highlightColour.g = g;
_highlightColour.b = b;
}
void Button::SetHighlightRGB(SDL_Color colour) {
_highlightColour = colour;
}
void Button::SetXY(int xArg, int yArg) { void Button::SetXY(int xArg, int yArg) {
x = xArg; x = xArg;
y = yArg; y = yArg;
@ -62,14 +73,33 @@ void Button::SetText(string textArg) {
bool Button::CheckMouseOver(void) { bool Button::CheckMouseOver(void) {
if(event.motion.x > _button.GetX() && event.motion.x < _button.GetX() + _button.GetWidth()) { if(event.motion.x > _button.GetX() && event.motion.x < _button.GetX() + _button.GetWidth()) {
if(event.motion.y > _button.GetY() && event.motion.y < _button.GetY() + _button.GetHeight()) { if(event.motion.y > _button.GetY() && event.motion.y < _button.GetY() + _button.GetHeight()) {
_button.SetRGB(_mouseOverColour.r, _mouseOverColour.g, _mouseOverColour.b); if (!_highlighted) {
_button.SetRGB(_mouseOverColour.r, _mouseOverColour.g, _mouseOverColour.b);
}
return true; return true;
} }
} }
_button.SetRGB(_mouseOutColour); if (!_highlighted) {
_button.SetRGB(_mouseOutColour);
}
return false; return false;
} }
void Button::SetHighlighted(bool highlighted) {
if(_highlighted != highlighted) {
if(highlighted) {
_button.SetRGB(_highlightColour);
} else {
if(_mouseOver) {
_button.SetRGB(_mouseOverColour);
} else {
_button.SetRGB(_textColour);
}
}
}
_highlighted = highlighted;
}
void Button::Render(void) { void Button::Render(void) {
_button.Draw(); _button.Draw();
_text.Render(); _text.Render();

View File

@ -22,6 +22,11 @@ public:
void SetTextRGB(SDL_Color); void SetTextRGB(SDL_Color);
void SetText(string textArg); void SetText(string textArg);
void SetHighlightRGB(Uint8 r, Uint8 g, Uint8 b);
void SetHighlightRGB(SDL_Color);
void SetHighlighted(bool highlighted);
void SetXY(int xArg, int yArg); void SetXY(int xArg, int yArg);
int GetX(void) const { return x; } int GetX(void) const { return x; }
@ -29,6 +34,8 @@ public:
int GetWidth(void) const { return w; } int GetWidth(void) const { return w; }
int GetHeight(void) const { return h; } int GetHeight(void) const { return h; }
bool GetHighlighted(void) { return _highlighted; }
bool CheckMouseOver(void); bool CheckMouseOver(void);
void Render(void); void Render(void);
@ -40,6 +47,7 @@ private:
SDL_Color _mouseOutColour; SDL_Color _mouseOutColour;
SDL_Color _mouseOverColour; SDL_Color _mouseOverColour;
SDL_Color _textColour; SDL_Color _textColour;
SDL_Color _highlightColour;
int x; int x;
int y; int y;
@ -48,6 +56,10 @@ private:
Rect _button; Rect _button;
Text _text; Text _text;
bool _highlighted;
bool _mouseOver;
}; };
#endif #endif

View File

@ -2,35 +2,46 @@
IngameMenu::IngameMenu(void) { IngameMenu::IngameMenu(void) {
_active = false; _active = false;
_highlightedButton = 0;
btnResume.SetOutRGB(200, 200, 200); btnResume.SetOutRGB(200, 200, 200);
btnResume.SetOverRGB(255, 255, 255); btnResume.SetOverRGB(255, 255, 255);
btnResume.SetTextRGB(0, 0, 0); btnResume.SetTextRGB(0, 0, 0);
btnResume.SetText("Resume Game"); btnResume.SetText("Resume Game");
btnResume.SetHighlightRGB(255, 128, 0);
btnResume.SetHighlighted(true);
btnResume.SetXY(SCREEN_WIDTH / 2 - btnResume.GetWidth() / 2, 50); btnResume.SetXY(SCREEN_WIDTH / 2 - btnResume.GetWidth() / 2, 50);
btnSaveGame.SetOutRGB(200, 200, 200); btnSaveGame.SetOutRGB(200, 200, 200);
btnSaveGame.SetOverRGB(255, 255, 255); btnSaveGame.SetOverRGB(255, 255, 255);
btnSaveGame.SetTextRGB(0, 0, 0); btnSaveGame.SetTextRGB(0, 0, 0);
btnSaveGame.SetText("SaveGame"); btnSaveGame.SetText("SaveGame");
btnSaveGame.SetHighlightRGB(255, 128, 0);
btnSaveGame.SetHighlighted(false);
btnSaveGame.SetXY(SCREEN_WIDTH / 2 - btnSaveGame.GetWidth() / 2, 100); btnSaveGame.SetXY(SCREEN_WIDTH / 2 - btnSaveGame.GetWidth() / 2, 100);
btnLoadGame.SetOutRGB(200, 200, 200); btnLoadGame.SetOutRGB(200, 200, 200);
btnLoadGame.SetOverRGB(255, 255, 255); btnLoadGame.SetOverRGB(255, 255, 255);
btnLoadGame.SetTextRGB(0, 0, 0); btnLoadGame.SetTextRGB(0, 0, 0);
btnLoadGame.SetText("LoadGame"); btnLoadGame.SetText("LoadGame");
btnLoadGame.SetHighlightRGB(255, 128, 0);
btnLoadGame.SetHighlighted(false);
btnLoadGame.SetXY(SCREEN_WIDTH / 2 - btnLoadGame.GetWidth() / 2, 150); btnLoadGame.SetXY(SCREEN_WIDTH / 2 - btnLoadGame.GetWidth() / 2, 150);
btnOptions.SetOutRGB(200, 200, 200); btnOptions.SetOutRGB(200, 200, 200);
btnOptions.SetOverRGB(255, 255, 255); btnOptions.SetOverRGB(255, 255, 255);
btnOptions.SetTextRGB(0, 0, 0); btnOptions.SetTextRGB(0, 0, 0);
btnOptions.SetText("Options"); btnOptions.SetText("Options");
btnOptions.SetHighlightRGB(255, 128, 0);
btnOptions.SetHighlighted(false);
btnOptions.SetXY(SCREEN_WIDTH / 2 - btnOptions.GetWidth() / 2, 200); btnOptions.SetXY(SCREEN_WIDTH / 2 - btnOptions.GetWidth() / 2, 200);
btnExitToMenu.SetOutRGB(200, 200, 200); btnExitToMenu.SetOutRGB(200, 200, 200);
btnExitToMenu.SetOverRGB(255, 255, 255); btnExitToMenu.SetOverRGB(255, 255, 255);
btnExitToMenu.SetTextRGB(0, 0, 0); btnExitToMenu.SetTextRGB(0, 0, 0);
btnExitToMenu.SetText("Exit To Main Menu"); btnExitToMenu.SetText("Exit To Main Menu");
btnExitToMenu.SetHighlightRGB(255, 128, 0);
btnExitToMenu.SetHighlighted(false);
btnExitToMenu.SetXY(SCREEN_WIDTH / 2 - btnExitToMenu.GetWidth() / 2, 250); btnExitToMenu.SetXY(SCREEN_WIDTH / 2 - btnExitToMenu.GetWidth() / 2, 250);
} }
@ -45,10 +56,23 @@ ingameMenuNavVal_t IngameMenu::HandleInput(void) {
btnLoadGame.CheckMouseOver(); btnLoadGame.CheckMouseOver();
btnOptions.CheckMouseOver(); btnOptions.CheckMouseOver();
btnExitToMenu.CheckMouseOver(); btnExitToMenu.CheckMouseOver();
if(event.key.type == SDL_KEYDOWN) { if(event.key.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == SDLK_ESCAPE) if(event.key.keysym.sym == SDLK_ESCAPE) {
return ingameMenuResume; return ingameMenuResume;
} else if(event.key.keysym.sym == SDLK_DOWN) {
HighlightNewSelection(_highlightedButton + 1);
} else if(event.key.keysym.sym == SDLK_UP) {
HighlightNewSelection(_highlightedButton - 1);
} else if(event.key.keysym.sym == SDLK_RETURN) {
switch(_highlightedButton) {
case 0: return ingameMenuResume;
case 1: return ingameMenuSaveGame;
case 2: return ingameMenuLoadGame;
case 3: return ingameMenuOptions;
case 4: return ingameMenuMainMenu;
}
}
} }
else if(event.type == SDL_MOUSEBUTTONUP) { else if(event.type == SDL_MOUSEBUTTONUP) {
if(event.button.button == SDL_BUTTON_LEFT) { if(event.button.button == SDL_BUTTON_LEFT) {
@ -75,3 +99,33 @@ void IngameMenu::Render(void) {
btnOptions.RenderLiteral(); btnOptions.RenderLiteral();
btnExitToMenu.RenderLiteral(); btnExitToMenu.RenderLiteral();
} }
void IngameMenu::HighlightNewSelection(int newButton) {
// Turn of highlight for currently highlighted button
switch(_highlightedButton) {
case 0: btnResume.SetHighlighted(false); break;
case 1: btnSaveGame.SetHighlighted(false); break;
case 2: btnLoadGame.SetHighlighted(false); break;
case 3: btnOptions.SetHighlighted(false); break;
case 4: btnExitToMenu.SetHighlighted(false); break;
}
_highlightedButton = newButton;
// If < 0 then up was pressed when first index was selected
// If > 0 then down was pressed when last index was selected
if(_highlightedButton < 0) {
_highlightedButton = 4;
} else if(_highlightedButton > 4) {
_highlightedButton = 0;
}
// Highlight new selection
switch(_highlightedButton) {
case 0: btnResume.SetHighlighted(true); break;
case 1: btnSaveGame.SetHighlighted(true); break;
case 2: btnLoadGame.SetHighlighted(true); break;
case 3: btnOptions.SetHighlighted(true); break;
case 4: btnExitToMenu.SetHighlighted(true); break;
}
}

View File

@ -27,6 +27,8 @@ public:
bool GetStatus(void) { return _active; } bool GetStatus(void) { return _active; }
private: private:
void HighlightNewSelection(int newButton);
bool _active; bool _active;
Button btnResume; Button btnResume;
@ -34,6 +36,8 @@ private:
Button btnLoadGame; Button btnLoadGame;
Button btnOptions; Button btnOptions;
Button btnExitToMenu; Button btnExitToMenu;
int _highlightedButton;
}; };
#endif #endif