unit UFormMove;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Spin,
  StdCtrls, ComCtrls;

type

  { TFormMove }

  TFormMove = class(TForm)
    ButtonRemove: TButton;
    ButtonRepeatMax: TButton;
    ButtonOnceMin: TButton;
    ButtonOnceMax: TButton;
    ButtonOk: TButton;
    ButtonCancel: TButton;
    ButtonRepeatMin: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    LabelWinProbability: TLabel;
    SpinEditOnce: TSpinEdit;
    SpinEditRepeat: TSpinEdit;
    TrackBarOnce: TTrackBar;
    TrackBarRepeat: TTrackBar;
    procedure ButtonOnceMaxClick(Sender: TObject);
    procedure ButtonRemoveClick(Sender: TObject);
    procedure ButtonRepeatMinClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ButtonOnceMinClick(Sender: TObject);
    procedure ButtonRepeatMaxClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure FormShow(Sender: TObject);
    procedure SpinEditOnceChange(Sender: TObject);
    procedure SpinEditRepeatChange(Sender: TObject);
    procedure TrackBarOnceChange(Sender: TObject);
    procedure TrackBarRepeatChange(Sender: TObject);
  private
    { private declarations }
  public
    DefendCount: Integer;
    AttackCount: Integer;
    ShowWinProbability: Boolean;
    AllowChangeOnce: Boolean;
    procedure UpdateView;
  end;

var
  FormMove: TFormMove;

implementation

{$R *.lfm}

uses
  UCore;

{ TFormMove }

procedure TFormMove.TrackBarOnceChange(Sender: TObject);
begin
  if SpinEditOnce.Value <> TrackBarOnce.Position then
    SpinEditOnce.Value := TrackBarOnce.Position;
end;

procedure TFormMove.TrackBarRepeatChange(Sender: TObject);
begin
  if SpinEditRepeat.Value <> TrackBarRepeat.Position then
    SpinEditRepeat.Value := TrackBarRepeat.Position;
end;

procedure TFormMove.UpdateView;
begin
  LabelWinProbability.Visible := ShowWinProbability;
  Label3.Visible := ShowWinProbability;
  LabelWinProbability.Caption :=
    IntToStr(Round(Core.Game.AttackProbability(AttackCount + SpinEditOnce.Value,
    DefendCount) * 100)) + ' %';
end;

procedure TFormMove.SpinEditOnceChange(Sender: TObject);
begin
  if SpinEditOnce.Value <> TrackBarOnce.Position then
    TrackBarOnce.Position := SpinEditOnce.Value;
  UpdateView;
end;

procedure TFormMove.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  if SpinEditOnce.MaxValue < SpinEditOnce.Value then begin
    SpinEditOnce.Enabled := False;
    SpinEditOnce.Value := 0;
    TrackBarOnce.Enabled := False;
    TrackBarOnce.Max := 0;
    SpinEditRepeat.SetFocus;
  end else begin
    SpinEditOnce.Enabled := AllowChangeOnce;
    TrackBarOnce.Enabled := AllowChangeOnce;
    ButtonOnceMax.Enabled := AllowChangeOnce;
    ButtonOnceMin.Enabled := AllowChangeOnce;
    if SpinEditOnce.Enabled then SpinEditOnce.SetFocus
    else if SpinEditRepeat.Enabled then SpinEditRepeat.SetFocus
    else ButtonOk.SetFocus;
  end;
  UpdateView;
end;

procedure TFormMove.ButtonOnceMaxClick(Sender: TObject);
begin
  TrackBarOnce.Position := TrackBarRepeat.Max;
end;

procedure TFormMove.ButtonRemoveClick(Sender: TObject);
begin
  SpinEditOnce.Value := 0;
  SpinEditRepeat.Value := 0;
  ModalResult := mrOk;
end;

procedure TFormMove.ButtonRepeatMinClick(Sender: TObject);
begin
  TrackBarRepeat.Position := TrackBarRepeat.Min;
end;

procedure TFormMove.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  Core.PersistentForm.Save(Self);
end;

procedure TFormMove.FormCreate(Sender: TObject);
begin
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
end;

procedure TFormMove.ButtonOnceMinClick(Sender: TObject);
begin
  TrackBarOnce.Position := TrackBarOnce.Min;
end;

procedure TFormMove.ButtonRepeatMaxClick(Sender: TObject);
begin
  TrackBarRepeat.Position := TrackBarRepeat.Max;
end;

procedure TFormMove.FormKeyPress(Sender: TObject; var Key: char);
begin
  if Key = #13 then ButtonOk.Click;
  if Key = #27 then ButtonCancel.Click;
end;

procedure TFormMove.SpinEditRepeatChange(Sender: TObject);
begin
  if SpinEditRepeat.Value <> TrackBarRepeat.Position then
    TrackBarRepeat.Position := SpinEditRepeat.Value;
end;

end.

