unit flat_button;

interface

uses OpenGL, textures, constants, variables, font, meshes, buildings,
     vfm, global;

type
  TFlatButton = class
    x, y, width, height : integer;
    texture : integer;
    num : integer;
    text, hint : string;
    text_left, text_top, hint_left, hint_top : integer;
    visible, colorize, enabled, draw_by_gui, click_by_gui : boolean;
  public
    constructor Create(n, x0, y0, w, h, texture_num : integer);
    procedure Redraw(mode : integer);
  end;

var
  buttons : array[1..max_buttons] of TFlatButton;
  total_buttons : integer = 0;
  all_buttons_textures : array[1..max_buttons_textures] of TTexture;

procedure Click(n : integer);
procedure Move(n : integer);

implementation

procedure Click(n : integer);
begin
if not buttons[n].visible then exit;
if not buttons[n].enabled then exit;
case n of
  1..4 : tower_creation_mode := n - 1;
  13 : with gTurrels[selected_tower].base do
         begin
           world.camera.eye.x := map_x * landscape.sx;
           world.camera.eye.y := map_y * landscape.sy - 250;
         end;
  19 : begin
         showed_weapon := showed_weapon + 1;
         if showed_weapon > gTurrels[selected_tower].params.weapons_aviable then
           showed_weapon := 1;
       end;
  20 : with gTurrels[selected_tower].params.weapons[showed_weapon] do
         begin
           primary := primary + 1;
           if primary > 2 then primary := 0;
       end;
end;
play_interface_click := play_interface_click + 1;
end;

procedure Move(n : integer);
begin
if not buttons[n].visible then exit;
if n = interface_selected then exit;
interface_selected := n;
play_interface_move := play_interface_move + 1;
end;

constructor TFlatButton.Create(n, x0, y0, w, h, texture_num : integer);
begin
inherited Create;
total_buttons := total_buttons + 1;
x := x0; y := y0;
width := round(w * interface_scale_x);
height := round(h * interface_scale_y);
texture := texture_num;
colorize := true;
enabled := true;
text_left := w + 5;
text_top := 22;
hint_left := w + 5;
hint_top := h + 15;
num := n;
visible := true;
draw_by_gui := true;
click_by_gui := true;
end;

procedure TFlatButton.Redraw(mode : integer);
var
pos : integer;
begin
if not visible then exit;
glPushMatrix;
glTranslatef(x, y, 0);

if mode = 1 then
  begin
    glColor4f(1, 1, 1, interface_opacity);
    pos := 0; if not enabled then pos := 2;
    if interface_selected = num then
      begin
        glColor4f(1, 0.5, 0.25, interface_opacity); 
        if enabled and colorize then pos := -2;
      end;
    if not enabled then glColor4f(0.5, 0.5, 0.5, interface_opacity);
    if not colorize then glColor4f(1, 1, 1, interface_opacity);

    all_buttons_textures[texture].Enable;
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(pos, pos);
    glTexCoord2f(0, 1); glVertex2f(pos, height + pos);
    glTexCoord2f(1, 1); glVertex2f(width + pos, height + pos);
    glTexCoord2f(1, 0); glVertex2f(width + pos, pos);
    glEnd;
  end;

if (interface_selected = num) and (mode = 2) then
  begin
    glColor4f(1, 0.5, 0.25, interface_opacity);
    if y + hint_top + 10 < screen_height then
      WriteText(hint_left, hint_top, hint)
    else WriteText(hint_left, height - hint_top + 8, hint)
  end;

if mode = 2 then
  begin
    glColor4f(1,1,1,interface_opacity);
    WriteText(text_left, text_top, text);
  end;

glPopMatrix;
end;

end.
