diff --git a/Makefile b/Makefile index 6b9f8e1..3ab429e 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ OBJECTS=\ main.o \ drawutils.o mathutils.o timer.o itos.o \ graph.o gamedata.o player.o \ +menubutton.o \ mainevent.o gamestate.o game.o titlescreen.o all: $(PROJECT) diff --git a/game.cpp b/game.cpp index 84dce84..e41ca49 100644 --- a/game.cpp +++ b/game.cpp @@ -41,6 +41,13 @@ bool Game::init() 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(); } @@ -112,15 +119,43 @@ void Game::render() } // draw the rest of the bottom menu + + // horizontal line across the whole thing DrawUtils::draw_line(display, 0, display->h - 100, 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, 0x000000); + if (data.get_current_vertex() != NULL) + { + draw_button(&move_button); + draw_button(&build_button); + draw_button(&attack_button); + } + 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) { int num_lines = 4; @@ -171,6 +206,14 @@ void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode) #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 void Game::print_debug_info() { diff --git a/game.h b/game.h index b6b53d1..2580747 100644 --- a/game.h +++ b/game.h @@ -9,6 +9,7 @@ #include "gamedata.h" #include "gamestate.h" +#include "menubutton.h" #include #include @@ -31,19 +32,30 @@ class Game : public GameState void on_lbutton_down(int x, int y); void on_rbutton_down(int mX, int mY); 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: void draw_stats(Vertex* v); + void draw_button(MenuButton* button); // data GameData data; - - static int NODE_RADIUS; + // the x,y position of the mouse cursor + int cursor_x; + int cursor_y; // surfaces containing textures to draw SDL_Surface* background; TTF_Font* font; + // menu buttons + MenuButton move_button; + MenuButton build_button; + MenuButton attack_button; + + static int NODE_RADIUS; + #ifdef DEBUG void print_debug_info(); #endif diff --git a/menubutton.cpp b/menubutton.cpp new file mode 100644 index 0000000..500c917 --- /dev/null +++ b/menubutton.cpp @@ -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; +} diff --git a/menubutton.h b/menubutton.h new file mode 100644 index 0000000..3c96fef --- /dev/null +++ b/menubutton.h @@ -0,0 +1,47 @@ +/* A clickable button widget + * + */ + +#ifndef _MENUBUTTON_H_ +#define _MENUBUTTON_H_ + +#include +#include +#include + +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