unit UFormMove;

{$mode delphi}

interface

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

type

  { TFormMove }

  TFormMove = class(TForm)
    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 ButtonRepeatMinClick(Sender: TObject);
    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;
    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
  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 := True;
    TrackBarOnce.Enabled := True;
    SpinEditOnce.SetFocus;
  end;
  UpdateView;
end;

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

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

procedure TFormMove.FormCreate(Sender: TObject);
begin

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.

