From 8a4ed42ab144642f278620e48973b1e37637b4fb Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Wed, 6 Jul 2011 16:47:34 -0400 Subject: [PATCH] Added some code for disabling button visually, but it doesn't quite work --- game.cpp | 3 ++- menubutton.cpp | 18 ++++++++++++++++-- menubutton.h | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/game.cpp b/game.cpp index a1be683..0dd8aff 100644 --- a/game.cpp +++ b/game.cpp @@ -373,7 +373,8 @@ void Game::mode_changed() cursor != buttons.end(); cursor++) { (*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())); } } diff --git a/menubutton.cpp b/menubutton.cpp index e86e1e0..94c7232 100644 --- a/menubutton.cpp +++ b/menubutton.cpp @@ -24,6 +24,7 @@ bool MenuButton::init() hover = false; selected = false; visible = true; + enabled = true; return true; } @@ -54,7 +55,8 @@ void MenuButton::render(SDL_Surface* display) if (!visible) return; int colour_to_use = colour; - if (selected) colour_to_use = selected_colour; + if (!enabled) colour_to_use = 0xbbbbbb; + else if (selected) colour_to_use = selected_colour; else if (hover) colour_to_use = hover_colour; SDL_Rect pen = {x, y, 80, 20}; @@ -75,10 +77,12 @@ bool MenuButton::is_at(int test_x, int test_y) } -void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current) +void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current, + bool can_build) { selected = false; visible = false; + enabled = true; if (action & BUTTON_END_TURN) { @@ -108,6 +112,16 @@ void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current) if (mode & MODE_BUILD && action & (BUTTON_BUILD_ATTACKER | BUTTON_BUILD_DEFENDER | BUTTON_BUILD_PRODUCER)) + { 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))) + enabled = false; + } + + if (action == BUTTON_ATTACK && current->attacked) enabled = false; } diff --git a/menubutton.h b/menubutton.h index 794316a..5d79b7f 100644 --- a/menubutton.h +++ b/menubutton.h @@ -44,7 +44,8 @@ class MenuButton : public Entity // To get some complexity out of the Game class, let's teach MenuButton // how to handle her own state, based on the current Mode and VertexType // This isn't wonderful coupling, but it'll do - void set_state(Mode mode, VertexType type, GameVertex* current); + void set_state(Mode mode, VertexType type, GameVertex* current, + bool can_build); private: string text; @@ -59,6 +60,7 @@ class MenuButton : public Entity bool hover; bool selected; bool visible; + bool enabled; TTF_Font* font;