treewars/menubutton.cpp

111 lines
2.4 KiB
C++

#include "menubutton.h"
#include "drawutils.h"
MenuButton::MenuButton(string text, TTF_Font* font, int x, int y,
ButtonAction action, 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;
}
bool MenuButton::init()
{
hover = false;
selected = false;
visible = 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 (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;
return test_x >= x && test_y >= y && test_x <= x + 100 && test_y <= y + 40;
}
void MenuButton::set_state(Mode mode, VertexType type, GameVertex* current)
{
selected = false;
visible = false;
if (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)
)
selected = true;
// If we have selected one of our vertices, and we're one of the three
// main buttons, be visible
if (current != NULL && action & (BUTTON_ATTACK | BUTTON_MOVE |
BUTTON_BUILD))
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))
visible = true;
}