unit VLFToolsForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, ImgList, ExtCtrls, StdCtrls, Buttons,
  CheckLst;

type
  TToolsForm = class(TForm)
    ToolBar1: TToolBar;
    SelectBtn: TToolButton;
    AddOrgBtn: TToolButton;
    EditMapBtn: TToolButton;
    DropperBtn: TToolButton;
    ToolBarImages: TImageList;
    Notebook: TNotebook;
    OrgsList: TListBox;
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    Label2: TLabel;
    OrgPropertiesView: TListView;
    EditKindGroup: TRadioGroup;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    SizeEdit: TEdit;
    SizeUpDown: TUpDown;
    RoundBrushBtn: TSpeedButton;
    SquareBrushBtn: TSpeedButton;
    GroupBox2: TGroupBox;
    ValuePanel: TPanel;
    ValueEdit: TEdit;
    ValueUpDown: TUpDown;
    Label5: TLabel;
    Value0Btn: TButton;
    Value128Btn: TButton;
    Value255Btn: TButton;
    SprayBrushBtn: TSpeedButton;
    MassKillBtn: TToolButton;
    Panel3: TPanel;
    GroupBox3: TGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    RoundBrushBtn2: TSpeedButton;
    SquareBrushBtn2: TSpeedButton;
    SizeEdit2: TEdit;
    SizeUpDown2: TUpDown;
    Label8: TLabel;
    OrgsToKillList: TCheckListBox;
    Panel4: TPanel;
    SelectAllBtn: TButton;
    Label9: TLabel;
    SelectNoneBtn: TButton;
    procedure SelectBtnClick(Sender: TObject);
    procedure AddOrgBtnClick(Sender: TObject);
    procedure EditMapBtnClick(Sender: TObject);
    procedure DropperBtnClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure OrgsListClick(Sender: TObject);
    procedure ValueUpDownChanging(Sender: TObject;
      var AllowChange: Boolean);
    procedure FormShow(Sender: TObject);
    procedure ValueEditChange(Sender: TObject);
    procedure SizeEditChange(Sender: TObject);
    procedure EditKindGroupClick(Sender: TObject);
    procedure Value0BtnClick(Sender: TObject);
    procedure Value128BtnClick(Sender: TObject);
    procedure Value255BtnClick(Sender: TObject);
    procedure MassKillBtnClick(Sender: TObject);
    procedure OrgsListDrawItem(Control: TWinControl; index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure SelectAllBtnClick(Sender: TObject);
    procedure SelectNoneBtnClick(Sender: TObject);
    procedure SizeUpDown2Click(Sender: TObject; Button: TUDBtnType);
    procedure SizeEdit2Change(Sender: TObject);
    procedure SizeUpDownClick(Sender: TObject; Button: TUDBtnType);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpdateOrgProperties;
    procedure UpdateValue;
    procedure UpdateEditKind;
    procedure UpdateTools;
  end;

var
  ToolsForm: TToolsForm;

implementation

uses AppMainForm, VLFObjects, Cursors, Tahoma;

{$R *.dfm}

procedure TToolsForm.UpdateOrgProperties;
var
  I: Integer;
begin
  if Application.Terminated then Exit;
  if Visible and (Notebook.PageIndex = 0) then
    with OrgPropertiesView do
      if MainForm.SelectedOrg <> nil then
      begin
        Enabled := True;
        with MainForm.SelectedOrg do
        begin
          Items[0].SubItems[0] := Species;
          Items[1].SubItems[0] := IntToStr(Mass);
          Items[2].SubItems[0] := IntToStr(FoodIn);
          Items[3].SubItems[0] := IntToStr(Energy);
          Items[4].SubItems[0] := IntToStr(X);
          Items[5].SubItems[0] := IntToStr(Y);
          Items[6].SubItems[0] := IntToStr(Age);
          Items[7].SubItems[0] := IntToStr(GenCount);
          for I := 0 to AllocedURCount - 1 do
            Items[I + 8].SubItems[0] := IntToStr(UR[I]);
          for I := AllocedURCount to MaxURCount - 1 do    // OPT
            Items[I + 8].SubItems[0] := '--';
        end
      end
      else
      begin
        Enabled := False;
      end;
end;

procedure TToolsForm.UpdateValue;
begin
  ValuePanel.Color := $000101 * ValueUpDown.Position;
end;

procedure TToolsForm.UpdateEditKind;
begin
  if EditKindGroup.ItemIndex = 3 then
  begin
    ValueUpDown.Enabled := False;
    ValueEdit.Enabled := False;
    ValueEdit.Color := clBtnFace;
    Value0Btn.Enabled := False;
    Value128Btn.Enabled := False;
    Value255Btn.Enabled := False;
  end
  else
  begin
    ValueUpDown.Enabled := True;
    ValueEdit.Enabled := True;
    ValueEdit.Color := clWindow;
    Value0Btn.Enabled := True;
    Value128Btn.Enabled := True;
    Value255Btn.Enabled := True;
  end;
end;

procedure TToolsForm.UpdateTools;
var
  B: Boolean;
begin
  B := MainForm.BigCursorVisible;
  with MainForm do
    if B then
      HideBigCursor(OldX, OldY);

  if SelectBtn.Down then
  begin
    Notebook.PageIndex := 0;
    MainForm.Tool := tSelect;
    MainForm.PaintBox1.Cursor := crDefault;
    UpdateOrgProperties;
  end;
  if AddOrgBtn.Down then
  begin
    Notebook.PageIndex := 1;
    MainForm.Tool := tAddOrg;
    MainForm.PaintBox1.Cursor := crAddOrg;
  end;
  if EditMapBtn.Down then
  begin
    Notebook.PageIndex := 2;
    MainForm.Tool := tEditMap;
    MainForm.PaintBox1.Cursor := crEditMap;
  end;
  if DropperBtn.Down then
  begin
    Notebook.PageIndex := 3;
    MainForm.Tool := tDropper;
    MainForm.PaintBox1.Cursor := crDropper;
  end;
  if MassKillBtn.Down then
  begin
    Notebook.PageIndex := 4;
    MainForm.Tool := tMassKill;
    MainForm.PaintBox1.Cursor := crMassKill;
  end;

  with MainForm do
    if B then
      ShowBigCursor(OldX, OldY);
end;

procedure TToolsForm.SelectBtnClick(Sender: TObject);
begin
  UpdateTools;
end;

procedure TToolsForm.AddOrgBtnClick(Sender: TObject);
begin
  UpdateTools;
end;

procedure TToolsForm.EditMapBtnClick(Sender: TObject);
begin
  UpdateTools;
end;

procedure TToolsForm.DropperBtnClick(Sender: TObject);
begin
  UpdateTools;
end;

procedure TToolsForm.FormCreate(Sender: TObject);
var
  I: Integer;
  Item: TListItem;
begin
  TahomizeForm(Self);
  OrgPropertiesView.DoubleBuffered := True;

  { Naplníme seznam druhů organismů. }
  with MainForm.Document.Env.Plugins do
    for I := 0 to Count - 1 do
    begin
      OrgsList.Items.Add(PPluginRecord(Items[I])^.Species);
      OrgsToKillList.Items.Add(PPluginRecord(Items[I])^.Species);
    end;

  { Naplníme seznam URek. }
  for I := 0 to MaxURCount - 1 do
  begin
    Item := OrgPropertiesView.Items.Add;
    Item.Caption := Format('UR[%d]', [I]);
    Item.SubItems.Add('')
  end;
end;

procedure TToolsForm.OrgsListClick(Sender: TObject);
begin
  MainForm.AddOrgSpecies := OrgsList.Items[OrgsList.ItemIndex];
end;

procedure TToolsForm.ValueUpDownChanging(Sender: TObject;
  var AllowChange: Boolean);
begin
  UpdateValue;
end;

procedure TToolsForm.FormShow(Sender: TObject);
begin
  UpdateValue;
  UpdateEditKind;
  UpdateTools;
end;

procedure TToolsForm.ValueEditChange(Sender: TObject);
begin
  try
    ValueUpDown.Position := StrToInt(ValueEdit.Text);
  except
    // tichá vyjímka
  end;
end;

procedure TToolsForm.SizeEditChange(Sender: TObject);
begin
  try
    SizeUpDown.Position := StrToInt(SizeEdit.Text);
    SizeUpDown.OnClick(Self, btNext); // aby se překreslil velký kurzor
  except
    // tichá vyjímka
  end;
end;

procedure TToolsForm.EditKindGroupClick(Sender: TObject);
begin
  UpdateEditKind;
end;

procedure TToolsForm.Value0BtnClick(Sender: TObject);
begin
  ValueUpDown.Position := 0;
  UpdateValue;
end;

procedure TToolsForm.Value128BtnClick(Sender: TObject);
begin
  ValueUpDown.Position := 128;
  UpdateValue;
end;

procedure TToolsForm.Value255BtnClick(Sender: TObject);
begin
  ValueUpDown.Position := 255;
  UpdateValue;
end;

procedure TToolsForm.MassKillBtnClick(Sender: TObject);
begin
  UpdateTools;
end;

procedure TToolsForm.OrgsListDrawItem(Control: TWinControl; index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  OldBrushColor: TColor;
begin
  with TListBox(Control), Canvas, Rect do
  begin
    FillRect(Rect);
    OldBrushColor := Brush.Color;
    Brush.Color := MainForm.Document.Env.SpeciesColors[index];
    Rectangle(Left + 2, Top + 2, Left + 13, Top + 13);
    Brush.Color := OldBrushColor;
    TextOut(Left + 20, Top + (16 + Font.Height) div 2 - 1, Items[index]);
  end;
end;

procedure TToolsForm.SelectAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  with OrgsToKillList do
    for I := 0 to Items.Count - 1 do
      Checked[I] := True;
end;

procedure TToolsForm.SelectNoneBtnClick(Sender: TObject);
var
  I: Integer;
begin
  with OrgsToKillList do
    for I := 0 to Items.Count - 1 do
      Checked[I] := False;
end;

procedure TToolsForm.SizeUpDown2Click(Sender: TObject; Button: TUDBtnType);
begin
  with MainForm do
    if BigCursorVisible then
    begin
      HideBigCursor(OldX, OldY);
      ShowBigCursor(OldX, OldY);
    end;
end;

procedure TToolsForm.SizeEdit2Change(Sender: TObject);
begin
  try
    SizeUpDown2.Position := StrToInt(SizeEdit2.Text);
    SizeUpDown2.OnClick(Self, btNext); // aby se překreslil velký kurzor
  except
    // tichá vyjímka
  end;
end;

procedure TToolsForm.SizeUpDownClick(Sender: TObject; Button: TUDBtnType);
begin
  with MainForm do
    if BigCursorVisible then
    begin
      HideBigCursor(OldX, OldY);
      ShowBigCursor(OldX, OldY);
    end;
end;

end.
