treewars/menubutton.cpp

129 lines
2.6 KiB
C++
Raw Normal View History

#include "menubutton.h"
#include "drawutils.h"
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->colour = colour;
this->hover_colour = hover_colour;
this->selected_colour = selected_colour;
this->background_colour = background_colour;
this->action = action;
}
bool MenuButton::init()
{
hover = false;
selected = false;
visible = true;
enabled = true;
return true;
}
void MenuButton::set_selected(bool is_selected)
{
selected = is_selected;
}
void MenuButton::set_visible(bool is_visible)
{
visible = is_visible;
}
void MenuButton::iterate()
{
int x, y;
SDL_GetMouseState(&x, &y);
if (is_at(x, y)) hover = true;
else hover = false;
}
void MenuButton::render(SDL_Surface* display)
{
if (!visible) return;
int colour_to_use = 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};
SDL_FillRect(display, &pen, background_colour);
int temp_colour = 0x000000;
DrawUtils::draw_text(display, text, x + 40, y + 10, font, colour_to_use,
1, 1);
}
bool MenuButton::is_at(int test_x, int test_y)
{
if (!visible) return false;
2011-07-06 19:06:23 +00:00
return test_x > x && test_y > y && test_x < x + 80 && test_y < y + 20;
}
void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current,
bool can_build)
{
selected = false;
visible = false;
enabled = true;
if (action.action & BUTTON_END_TURN)
{
visible = true;
return;
}
if (action.mode == mode || action.type == type)
selected = true;
// no optional buttons if current == NULL, period
if (current == NULL) return;
// If we have selected one of our vertices, and we're one of the three
// main buttons, be visible
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.type != VERTEX_NONE)
{
visible = true;
if (!can_build)
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;
}