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 col4 = 455;
buttons.push_back(new MenuButton("Move (m)", font, col1, row1,
BUTTON_MOVE));
buttons.push_back(new MenuButton("Build (b)", font, col1, row2,
BUTTON_BUILD));
buttons.push_back(new MenuButton("Attack (a)", font, col1, row3,
BUTTON_ATTACK));
buttons.push_back(new MenuButton("Attacker (t)", font, col2, row1,
BUTTON_BUILD_ATTACKER));
buttons.push_back(new MenuButton("Defender (d)", font, col2, row2,
BUTTON_BUILD_DEFENDER));
buttons.push_back(new MenuButton("Producer (p)", font, col2, row3,
BUTTON_BUILD_PRODUCER));
buttons.push_back(new MenuButton("End Turn (e)", font, col4, row3,
BUTTON_END_TURN));
ButtonAction action;
action.mode = MODE_MOVE;
buttons.push_back(new MenuButton(action, "Move (m)", font, col1, row1));
action.mode = MODE_BUILD;
buttons.push_back(new MenuButton(action, "Build (b)", font, col1, row2));
action.mode = MODE_ATTACK;
buttons.push_back(new MenuButton(action, "Attack (a)", font, col1, row3));
action.mode = MODE_NONE;
action.type = VERTEX_ATTACKER;
buttons.push_back(new MenuButton(action, "Attacker (t)", font, col2,
row1));
action.type = VERTEX_DEFENDER;
buttons.push_back(new MenuButton(action, "Defender (d)", font, col2,
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();
@ -68,13 +75,13 @@ bool Game::init()
{
if (!(*cursor)->init())
{
debug("Failed to initialize a button");
debug("Game::init(): Failed to initialize a button");
throw StateExit();
}
}
mode_changed();
data.set_first_turn();
return GameState::init();
}
@ -219,30 +226,9 @@ void Game::draw_player_info()
void Game::handle_button_press(ButtonAction action)
{
switch (action)
{
case BUTTON_BUILD:
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;
}
if (action.mode != MODE_NONE) data.set_mode(action.mode);
else if (action.type != VERTEX_NONE) data.set_build_type(action.type);
else if (action.action == BUTTON_END_TURN) data.toggle_turn();
}
@ -272,6 +258,9 @@ void Game::draw_stats(GameVertex* v)
case VERTEX_PRODUCER:
type = "producer";
break;
default:
type = "unknown";
break;
}
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(),
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;
}
void GameData::set_first_turn()
{
build_type = VERTEX_PRODUCER;
}

View File

@ -10,7 +10,8 @@
#include "player.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
{
@ -33,6 +34,7 @@ class GameData : public Graph
VertexType get_build_type() const { return build_type; }
void set_build_type(VertexType type);
void set_first_turn();
// returns the move/attack range for the specified node
// (or the selected node if node == NULL)

View File

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

View File

@ -2,20 +2,28 @@
#include "drawutils.h"
MenuButton::MenuButton(string text, TTF_Font* font, int x, int y,
ButtonAction action, int colour, int hover_colour,
ButtonAction::ButtonAction()
{
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)
{
this->text = text;
this->x = x;
this->y = y;
this->font = font;
this->action = action;
this->colour = colour;
this->hover_colour = hover_colour;
this->selected_colour = selected_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;
enabled = true;
if (action & BUTTON_END_TURN)
if (action.action & BUTTON_END_TURN)
{
visible = true;
return;
}
if ((mode & MODE_ATTACK && action & BUTTON_ATTACK) ||
(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)
)
if (action.mode == mode || action.type == type)
selected = true;
// 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
// main buttons, be visible
if (action & (BUTTON_ATTACK | BUTTON_MOVE |
BUTTON_BUILD))
if (action.mode != MODE_NONE)
visible = true;
// 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 |
BUTTON_BUILD_DEFENDER |
BUTTON_BUILD_PRODUCER))
if (mode & MODE_BUILD && action.type != VERTEX_NONE)
{
visible = true;
if (!can_build &&
((action == BUTTON_BUILD_ATTACKER && type == VERTEX_ATTACKER) ||
(action == BUTTON_BUILD_DEFENDER && type == VERTEX_DEFENDER) ||
(action == BUTTON_BUILD_PRODUCER && type == VERTEX_PRODUCER)))
if (!can_build)
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;
// 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=0x1, BUTTON_ATTACK=0x2, BUTTON_MOVE=0x4,
BUTTON_BUILD_ATTACKER=0x8, BUTTON_BUILD_DEFENDER=0x10,
BUTTON_BUILD_PRODUCER=0x20, BUTTON_END_TURN=0x40};
enum CustomButtonAction {BUTTON_NONE=0x0, BUTTON_END_TURN=0x1};
class ButtonAction
{
public:
ButtonAction();
Mode mode;
VertexType type;
CustomButtonAction action;
};
class MenuButton : public Entity
{
public:
MenuButton(string text = "", TTF_Font* font = NULL, int x = 0, int y = 0,
ButtonAction action = BUTTON_BUILD, int colour=0x000000,
MenuButton(ButtonAction action, string text = "", TTF_Font* font = NULL,
int x = 0, int y = 0, int colour=0x000000,
int hover_colour=0xff0000, int selected_colour=0x0000ff,
int background_colour=0x888888);