Added rudimentary menu buttons, not yet connected to anything... it feels a bit hackish, to be honest, but I don't think we'll ever have too many to worry about, and if so, we can think about refactoring then

This commit is contained in:
Anna Rose 2011-07-02 17:34:11 -04:00
parent c1ea399448
commit d04cde49c8
5 changed files with 144 additions and 2 deletions

View File

@ -8,6 +8,7 @@ OBJECTS=\
main.o \ main.o \
drawutils.o mathutils.o timer.o itos.o \ drawutils.o mathutils.o timer.o itos.o \
graph.o gamedata.o player.o \ graph.o gamedata.o player.o \
menubutton.o \
mainevent.o gamestate.o game.o titlescreen.o mainevent.o gamestate.o game.o titlescreen.o
all: $(PROJECT) all: $(PROJECT)

View File

@ -41,6 +41,13 @@ bool Game::init()
return false; return false;
} }
move_button = MenuButton("Move", font, 155, display->h - 95, BUTTON_MOVE);
attack_button = MenuButton("Attack", font, 260, display->h - 95,
BUTTON_ATTACK);
build_button = MenuButton("Build", font, 155, display->h - 50,
BUTTON_BUILD);
return GameState::init(); return GameState::init();
} }
@ -112,15 +119,43 @@ void Game::render()
} }
// draw the rest of the bottom menu // draw the rest of the bottom menu
// horizontal line across the whole thing
DrawUtils::draw_line(display, 0, display->h - 100, DrawUtils::draw_line(display, 0, display->h - 100,
display->w, display->h - 100, 2, 0x000000); display->w, display->h - 100, 2, 0x000000);
// vertical line to separate info pane from button pane
DrawUtils::draw_line(display, 150, display->h - 100, 150, display->h, 2, DrawUtils::draw_line(display, 150, display->h - 100, 150, display->h, 2,
0x000000); 0x000000);
if (data.get_current_vertex() != NULL)
{
draw_button(&move_button);
draw_button(&build_button);
draw_button(&attack_button);
}
SDL_Flip(display); SDL_Flip(display);
} }
void Game::draw_button(MenuButton* button)
{
int colour = 0x000000;
ButtonAction action = button->get_action();
Mode mode = data.get_mode();
// fixme - there's really got to be a better way...
if ((action == BUTTON_BUILD && mode == MODE_BUILD) ||
(action == BUTTON_ATTACK && mode == MODE_ATTACK) ||
(action == BUTTON_MOVE && mode == MODE_MOVE))
colour = 0x0000ff;
else if (button->is_at(cursor_x, cursor_y)) colour = 0xff0000;
button->draw(display, colour);
}
void Game::draw_stats(Vertex* v) void Game::draw_stats(Vertex* v)
{ {
int num_lines = 4; int num_lines = 4;
@ -171,6 +206,14 @@ void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
#endif #endif
} }
void Game::on_mouse_move(int mX, int mY, int relX, int relY, bool left, bool right, bool middle)
{
cursor_x = mX;
cursor_y = mY;
}
#ifdef DEBUG #ifdef DEBUG
void Game::print_debug_info() void Game::print_debug_info()
{ {

16
game.h
View File

@ -9,6 +9,7 @@
#include "gamedata.h" #include "gamedata.h"
#include "gamestate.h" #include "gamestate.h"
#include "menubutton.h"
#include <stack> #include <stack>
#include <SDL_ttf.h> #include <SDL_ttf.h>
@ -31,19 +32,30 @@ class Game : public GameState
void on_lbutton_down(int x, int y); void on_lbutton_down(int x, int y);
void on_rbutton_down(int mX, int mY); void on_rbutton_down(int mX, int mY);
void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode); void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode);
void on_mouse_move(int mX, int mY, int relX, int relY, bool left,
bool right, bool middle);
private: private:
void draw_stats(Vertex* v); void draw_stats(Vertex* v);
void draw_button(MenuButton* button);
// data // data
GameData data; GameData data;
// the x,y position of the mouse cursor
static int NODE_RADIUS; int cursor_x;
int cursor_y;
// surfaces containing textures to draw // surfaces containing textures to draw
SDL_Surface* background; SDL_Surface* background;
TTF_Font* font; TTF_Font* font;
// menu buttons
MenuButton move_button;
MenuButton build_button;
MenuButton attack_button;
static int NODE_RADIUS;
#ifdef DEBUG #ifdef DEBUG
void print_debug_info(); void print_debug_info();
#endif #endif

39
menubutton.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "menubutton.h"
#include "drawutils.h"
MenuButton::MenuButton(string text, TTF_Font* font, int x, int y,
ButtonAction action)
{
this->text = text;
this->x = x;
this->y = y;
this->font = font;
this->action = action;
hover = false;
}
void MenuButton::set_hover(bool is_hovering)
{
hover = is_hovering;
}
void MenuButton::draw(SDL_Surface* display, int colour, int background_colour)
{
SDL_Rect pen = {x, y, 100, 40};
SDL_FillRect(display, &pen, background_colour);
int temp_colour = 0x000000;
DrawUtils::draw_text(display, text, x + 50, y + 20, font, colour, 1, 1);
}
bool MenuButton::is_at(int test_x, int test_y)
{
return test_x >= x && test_y >= y && test_x <= x + 100 && test_y <= y + 40;
}

47
menubutton.h Normal file
View File

@ -0,0 +1,47 @@
/* A clickable button widget
*
*/
#ifndef _MENUBUTTON_H_
#define _MENUBUTTON_H_
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
using std::string;
// fixme: there's probably a better way to do this, but SDL's event model
// has me hard-pressed to figure out what it is.
enum ButtonAction {BUTTON_BUILD, BUTTON_ATTACK, BUTTON_MOVE,
BUTTON_BUILD_ATTACKER, BUTTON_BUILD_DEFENDER,
BUTTON_BUILD_PRODUCER};
class MenuButton
{
public:
MenuButton(string text = "", TTF_Font* font = NULL, int x = 0, int y = 0,
ButtonAction action = BUTTON_BUILD);
// Selectable buttons change colour for the duration they are selected,
// and should ignore clicks while selected
void set_hover(bool is_hovering);
void draw(SDL_Surface* display, int colour = 0x000000,
int background_colour = 0x888888);
bool is_at(int test_x, int test_y);
ButtonAction get_action() const { return action; }
private:
string text;
int x;
int y;
bool hover;
TTF_Font* font;
ButtonAction action;
};
#endif