Some drastic refactoring to reduce complexity for buttons, but now all the primary buttons always look selected...

This commit is contained in:
Anna Rose 2011-07-06 17:25:12 -04:00
parent 8a4ed42ab1
commit 825a3f8e4a
6 changed files with 78 additions and 74 deletions

View File

@ -47,20 +47,27 @@ bool Game::init()
int col3 = 355; int col3 = 355;
int col4 = 455; int col4 = 455;
buttons.push_back(new MenuButton("Move (m)", font, col1, row1, ButtonAction action;
BUTTON_MOVE)); action.mode = MODE_MOVE;
buttons.push_back(new MenuButton("Build (b)", font, col1, row2, buttons.push_back(new MenuButton(action, "Move (m)", font, col1, row1));
BUTTON_BUILD)); action.mode = MODE_BUILD;
buttons.push_back(new MenuButton("Attack (a)", font, col1, row3, buttons.push_back(new MenuButton(action, "Build (b)", font, col1, row2));
BUTTON_ATTACK)); action.mode = MODE_ATTACK;
buttons.push_back(new MenuButton("Attacker (t)", font, col2, row1, buttons.push_back(new MenuButton(action, "Attack (a)", font, col1, row3));
BUTTON_BUILD_ATTACKER)); action.mode = MODE_NONE;
buttons.push_back(new MenuButton("Defender (d)", font, col2, row2, action.type = VERTEX_ATTACKER;
BUTTON_BUILD_DEFENDER)); buttons.push_back(new MenuButton(action, "Attacker (t)", font, col2,
buttons.push_back(new MenuButton("Producer (p)", font, col2, row3, row1));
BUTTON_BUILD_PRODUCER)); action.type = VERTEX_DEFENDER;
buttons.push_back(new MenuButton("End Turn (e)", font, col4, row3, buttons.push_back(new MenuButton(action, "Defender (d)", font, col2,
BUTTON_END_TURN)); row2));
action.type = VERTEX_PRODUCER;
buttons.push_back(new MenuButton(action, "Producer (p)", font, col2,
row3));
action.type = VERTEX_NONE;
action.action = BUTTON_END_TURN;
buttons.push_back(new MenuButton(action, "End Turn (e)", font, col4,
row3));
for (list<MenuButton*>::iterator cursor = buttons.begin(); for (list<MenuButton*>::iterator cursor = buttons.begin();
@ -68,13 +75,13 @@ bool Game::init()
{ {
if (!(*cursor)->init()) if (!(*cursor)->init())
{ {
debug("Failed to initialize a button"); debug("Game::init(): Failed to initialize a button");
throw StateExit(); throw StateExit();
} }
} }
mode_changed(); mode_changed();
data.set_first_turn();
return GameState::init(); return GameState::init();
} }
@ -219,30 +226,9 @@ void Game::draw_player_info()
void Game::handle_button_press(ButtonAction action) void Game::handle_button_press(ButtonAction action)
{ {
switch (action) if (action.mode != MODE_NONE) data.set_mode(action.mode);
{ else if (action.type != VERTEX_NONE) data.set_build_type(action.type);
case BUTTON_BUILD: else if (action.action == BUTTON_END_TURN) data.toggle_turn();
data.set_mode(MODE_BUILD);
break;
case BUTTON_ATTACK:
data.set_mode(MODE_ATTACK);
break;
case BUTTON_MOVE:
data.set_mode(MODE_MOVE);
break;
case BUTTON_BUILD_ATTACKER:
data.set_build_type(VERTEX_ATTACKER);
break;
case BUTTON_BUILD_DEFENDER:
data.set_build_type(VERTEX_DEFENDER);
break;
case BUTTON_BUILD_PRODUCER:
data.set_build_type(VERTEX_PRODUCER);
break;
case BUTTON_END_TURN:
data.toggle_turn();
break;
}
} }
@ -272,6 +258,9 @@ void Game::draw_stats(GameVertex* v)
case VERTEX_PRODUCER: case VERTEX_PRODUCER:
type = "producer"; type = "producer";
break; break;
default:
type = "unknown";
break;
} }
DrawUtils::draw_text(display, type, x + 50, adj_y, font); DrawUtils::draw_text(display, type, x + 50, adj_y, font);
@ -374,7 +363,7 @@ void Game::mode_changed()
{ {
(*cursor)->set_state(data.get_mode(), data.get_build_type(), (*cursor)->set_state(data.get_mode(), data.get_build_type(),
data.get_current_vertex(), data.get_current_vertex(),
data.can_build(data.get_build_type())); data.can_build((*cursor)->get_action().type));
} }
} }

View File

@ -335,3 +335,9 @@ void GameData::set_build_type(VertexType type)
{ {
if (can_build(type)) build_type = type; if (can_build(type)) build_type = type;
} }
void GameData::set_first_turn()
{
build_type = VERTEX_PRODUCER;
}

View File

@ -10,7 +10,8 @@
#include "player.h" #include "player.h"
#include "gamevertex.h" #include "gamevertex.h"
enum Mode {MODE_MOVE=0x1, MODE_ATTACK=0x2, MODE_BUILD=0x4, MODE_SELECT=0x8}; enum Mode {MODE_NONE=0x0, MODE_MOVE=0x1, MODE_ATTACK=0x2, MODE_BUILD=0x4,
MODE_SELECT=0x8};
class GameData : public Graph class GameData : public Graph
{ {
@ -33,6 +34,7 @@ class GameData : public Graph
VertexType get_build_type() const { return build_type; } VertexType get_build_type() const { return build_type; }
void set_build_type(VertexType type); void set_build_type(VertexType type);
void set_first_turn();
// returns the move/attack range for the specified node // returns the move/attack range for the specified node
// (or the selected node if node == NULL) // (or the selected node if node == NULL)

View File

@ -15,8 +15,8 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
enum VertexType {VERTEX_NONE=0x1, VERTEX_ATTACKER=0x2, VERTEX_DEFENDER=0x4, enum VertexType {VERTEX_NONE=0x0, VERTEX_ATTACKER=0x1, VERTEX_DEFENDER=0x2,
VERTEX_PRODUCER=0x8}; VERTEX_PRODUCER=0x4};
class GameVertex : public Vertex, public Entity class GameVertex : public Vertex, public Entity
{ {

View File

@ -2,20 +2,28 @@
#include "drawutils.h" #include "drawutils.h"
MenuButton::MenuButton(string text, TTF_Font* font, int x, int y, ButtonAction::ButtonAction()
ButtonAction action, int colour, int hover_colour, {
mode = MODE_NONE;
type = VERTEX_NONE;
action = BUTTON_NONE;
}
MenuButton::MenuButton(ButtonAction action, string text, TTF_Font* font,
int x, int y, int colour, int hover_colour,
int selected_colour, int background_colour) int selected_colour, int background_colour)
{ {
this->text = text; this->text = text;
this->x = x; this->x = x;
this->y = y; this->y = y;
this->font = font; this->font = font;
this->action = action;
this->colour = colour; this->colour = colour;
this->hover_colour = hover_colour; this->hover_colour = hover_colour;
this->selected_colour = selected_colour; this->selected_colour = selected_colour;
this->background_colour = background_colour; this->background_colour = background_colour;
this->action = action;
} }
@ -84,19 +92,13 @@ void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current,
visible = false; visible = false;
enabled = true; enabled = true;
if (action & BUTTON_END_TURN) if (action.action & BUTTON_END_TURN)
{ {
visible = true; visible = true;
return; return;
} }
if ((mode & MODE_ATTACK && action & BUTTON_ATTACK) || if (action.mode == mode || action.type == type)
(mode & MODE_MOVE && action & BUTTON_MOVE) ||
(mode & MODE_BUILD && action & BUTTON_BUILD) ||
(type & VERTEX_ATTACKER && action & BUTTON_BUILD_ATTACKER) ||
(type & VERTEX_DEFENDER && action & BUTTON_BUILD_DEFENDER) ||
(type & VERTEX_PRODUCER && action & BUTTON_BUILD_PRODUCER)
)
selected = true; selected = true;
// no optional buttons if current == NULL, period // no optional buttons if current == NULL, period
@ -104,24 +106,23 @@ void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current,
// If we have selected one of our vertices, and we're one of the three // If we have selected one of our vertices, and we're one of the three
// main buttons, be visible // main buttons, be visible
if (action & (BUTTON_ATTACK | BUTTON_MOVE | if (action.mode != MODE_NONE)
BUTTON_BUILD))
visible = true; visible = true;
// If we're one of the other three, and we're in MODE_BUILD, we're visible // If we're one of the other three, and we're in MODE_BUILD, we're visible
if (mode & MODE_BUILD && action & (BUTTON_BUILD_ATTACKER | if (mode & MODE_BUILD && action.type != VERTEX_NONE)
BUTTON_BUILD_DEFENDER |
BUTTON_BUILD_PRODUCER))
{ {
visible = true; visible = true;
if (!can_build && if (!can_build)
((action == BUTTON_BUILD_ATTACKER && type == VERTEX_ATTACKER) ||
(action == BUTTON_BUILD_DEFENDER && type == VERTEX_DEFENDER) ||
(action == BUTTON_BUILD_PRODUCER && type == VERTEX_PRODUCER)))
enabled = false; enabled = false;
} }
if (action == BUTTON_ATTACK && current->attacked) enabled = false; if (action.type == VERTEX_ATTACKER && current->attacked) enabled = false;
}
if (action.mode == MODE_MOVE && current->player->get_energy() <
current->r) enabled = false;
if (action.mode == MODE_BUILD && current->player->get_energy() < 25)
enabled = false;
}

View File

@ -14,17 +14,23 @@
using std::string; using std::string;
// fixme: there's probably a better way to do this, but SDL's event model enum CustomButtonAction {BUTTON_NONE=0x0, BUTTON_END_TURN=0x1};
// has me hard-pressed to figure out what it is. class ButtonAction
enum ButtonAction {BUTTON_BUILD=0x1, BUTTON_ATTACK=0x2, BUTTON_MOVE=0x4, {
BUTTON_BUILD_ATTACKER=0x8, BUTTON_BUILD_DEFENDER=0x10, public:
BUTTON_BUILD_PRODUCER=0x20, BUTTON_END_TURN=0x40}; ButtonAction();
Mode mode;
VertexType type;
CustomButtonAction action;
};
class MenuButton : public Entity class MenuButton : public Entity
{ {
public: public:
MenuButton(string text = "", TTF_Font* font = NULL, int x = 0, int y = 0, MenuButton(ButtonAction action, string text = "", TTF_Font* font = NULL,
ButtonAction action = BUTTON_BUILD, int colour=0x000000, int x = 0, int y = 0, int colour=0x000000,
int hover_colour=0xff0000, int selected_colour=0x0000ff, int hover_colour=0xff0000, int selected_colour=0x0000ff,
int background_colour=0x888888); int background_colour=0x888888);