unit UFormGameSystem;

{$mode delphi}

interface

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

type

  { TFormGameSystem }

  TFormGameSystem = class(TForm)
    ButtonCancel: TButton;
    ButtonOk: TButton;
    ButtonSave: TButton;
    ButtonLoad: TButton;
    CheckBoxEmptyCellsNeutral: TCheckBox;
    CheckBoxUnitsMoveImmediately: TCheckBox;
    CheckBoxUnitsSplitMerge: TCheckBox;
    OpenDialog1: TOpenDialog;
    PageControl1: TPageControl;
    SaveDialog1: TSaveDialog;
    TabSheetBuildings: TTabSheet;
    TabSheetNations: TTabSheet;
    TabSheetGeneral: TTabSheet;
    TabSheetUnits: TTabSheet;
    procedure ButtonLoadClick(Sender: TObject);
    procedure ButtonSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FGameSystem: TGameSystem;
    FormUnitKinds: TFormList;
    FormNations: TFormList;
    FormBuildingKinds: TFormList;
    procedure SetGameSystem(AValue: TGameSystem);
  public
    property GameSystem: TGameSystem read FGameSystem write SetGameSystem;
    procedure LoadData(GameSystem: TGameSystem);
    procedure SaveData(GameSystem: TGameSystem);
  end;

var
  FormGameSystem: TFormGameSystem;


implementation

{$R *.lfm}

uses
  UCore;

resourcestring
  SFileDialogFilter = 'xTactics game system (.xts)|*.xts|All files|*.*';

{ TFormGameSystem }

procedure TFormGameSystem.ButtonLoadClick(Sender: TObject);
begin
  OpenDialog1.Filter := SFileDialogFilter;
  OpenDialog1.InitialDir := ExtractFileDir(GameSystem.FileName);
  OpenDialog1.FileName := ExtractFileName(GameSystem.FileName);
  if OpenDialog1.Execute then begin
    GameSystem.LoadFromFile(OpenDialog1.FileName);
    LoadData(GameSystem);
  end;
end;

procedure TFormGameSystem.ButtonSaveClick(Sender: TObject);
begin
  SaveDialog1.Filter := SFileDialogFilter;
  SaveDialog1.InitialDir := ExtractFileDir(GameSystem.FileName);
  SaveDialog1.FileName := ExtractFileName(GameSystem.FileName);
  SaveDialog1.DefaultExt := GameSystemExt;
  if SaveDialog1.Execute then begin
    GameSystem.SaveToFile(SaveDialog1.FileName);
  end;
end;

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

procedure TFormGameSystem.FormCreate(Sender: TObject);
begin
  Core.ThemeManager1.UseTheme(Self);
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
  FormUnitKinds := TFormList.Create(nil);
  FormUnitKinds.ManualDock(TabSheetUnits, nil, alClient);
  FormUnitKinds.Align := alClient;
  FormUnitKinds.Visible := True;
  FormNations := TFormList.Create(nil);
  FormNations.ManualDock(TabSheetNations, nil, alClient);
  FormNations.Align := alClient;
  FormNations.Visible := True;
  FormBuildingKinds := TFormList.Create(nil);
  FormBuildingKinds.ManualDock(TabSheetBuildings, nil, alClient);
  FormBuildingKinds.Align := alClient;
  FormBuildingKinds.Visible := True;
end;

procedure TFormGameSystem.FormDestroy(Sender: TObject);
begin
  GameSystem := nil;
  FreeAndNil(FormNations);
  FreeAndNil(FormUnitKinds);
  FreeAndNil(FormBuildingKinds);
end;

procedure TFormGameSystem.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
end;

procedure TFormGameSystem.SetGameSystem(AValue: TGameSystem);
begin
  if FGameSystem = AValue then Exit;
  if Assigned(FGameSystem) then begin
    FormUnitKinds.List := nil;
    FormNations.List := nil;
    FormBuildingKinds.List := nil;
  end;
  FGameSystem := AValue;
  if Assigned(FGameSystem) then begin
    FormUnitKinds.List := GameSystem.UnitKinds;
    FormNations.List := GameSystem.Nations;
    FormBuildingKinds.List := GameSystem.BuildingKinds;
  end;
end;

procedure TFormGameSystem.LoadData(GameSystem: TGameSystem);
begin
  Self.GameSystem := GameSystem;
  CheckBoxEmptyCellsNeutral.Checked := GameSystem.EmptyCellsNeutral;
  CheckBoxUnitsSplitMerge.Checked := GameSystem.UnitsSplitMerge;
  CheckBoxUnitsMoveImmediately.Checked := GameSystem.UnitsMoveImmediately;
  FormNations.UpdateList;
  FormNations.UpdateInterface;
  FormUnitKinds.UpdateList;
  FormUnitKinds.UpdateInterface;
  FormBuildingKinds.UpdateList;
  FormBuildingKinds.UpdateInterface;
  Caption := ExtractFileName(Self.GameSystem.FileName) + ' - ' + SGameSystem;
end;

procedure TFormGameSystem.SaveData(GameSystem: TGameSystem);
begin
  GameSystem.EmptyCellsNeutral := CheckBoxEmptyCellsNeutral.Checked;
  GameSystem.UnitsSplitMerge := CheckBoxUnitsSplitMerge.Checked;
  GameSystem.UnitsMoveImmediately := CheckBoxUnitsMoveImmediately.Checked;
end;

end.

