unit UFormMain;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  UGame, LCLType, Menus, ActnList, ComCtrls, dateutils, XMLConf, DOM;

const
  ZoomFactor = 1.5;
  MouseMinDiff = 0.1;

type

  { TFormMain }

  TFormMain = class(TForm)
    AUnitShapeVisible: TAction;
    AMapGridVisible: TAction;
    AStatusBarVisible: TAction;
    AToolBarVisible: TAction;
    AToolBarBigIcons: TAction;
    ActionList1: TActionList;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem11: TMenuItem;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem19: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem20: TMenuItem;
    MenuItem21: TMenuItem;
    MenuItem22: TMenuItem;
    MenuItem23: TMenuItem;
    MenuItem24: TMenuItem;
    MenuItem25: TMenuItem;
    MenuItem26: TMenuItem;
    MenuItem27: TMenuItem;
    MenuItem28: TMenuItem;
    MenuItem29: TMenuItem;
    MenuItem30: TMenuItem;
    MenuItem32: TMenuItem;
    MenuItem33: TMenuItem;
    MenuItem34: TMenuItem;
    MenuItem35: TMenuItem;
    MenuItemDebug: TMenuItem;
    MenuItem31: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItemLoadRecent: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    PanelMain: TPanel;
    PopupMenuToolbar: TPopupMenu;
    Timer1: TTimer;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    ToolButton13: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton9: TToolButton;
    procedure AMapGridVisibleExecute(Sender: TObject);
    procedure AStatusBarVisibleExecute(Sender: TObject);
    procedure AToolBarBigIconsExecute(Sender: TObject);
    procedure AToolBarVisibleExecute(Sender: TObject);
    procedure AUnitShapeVisibleExecute(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure EraseBackground(DC: HDC); override;
    procedure Timer1Timer(Sender: TObject);
  private
    FormActivated: Boolean;
    FormShown: Boolean;
    procedure ApplicationStart(Ptr: IntPtr);
    procedure UpdateClientForms;
  public
    procedure LoadConfig(Config: TXmlConfig; Path: string);
    procedure SaveConfig(Config: TXmlConfig; Path: string);
    procedure ReloadView;
  end;

var
  FormMain: TFormMain;

implementation

uses
  UCore, UFormClient;

resourcestring
  STurn = 'turn';

{$R *.lfm}

{ TFormMain }

procedure TFormMain.EraseBackground(DC: HDC);
begin
  // Do nothing, all background space covered by controls
end;

procedure TFormMain.Timer1Timer(Sender: TObject);
var
  NewCaption: string;
begin
  NewCaption := 'xTactics';
  if Assigned(Core.Game.CurrentPlayer) then
    NewCaption := Core.Game.CurrentPlayer.Name + ' - ' + STurn + ' ' + IntToStr(Core.Game.TurnCounter) + ' - ' + NewCaption;
  Caption := NewCaption;
end;

procedure TFormMain.LoadConfig(Config: TXmlConfig; Path: string);
begin
  with Config do begin
    AToolBarBigIcons.Checked := GetValue(DOMString(Path + '/LargeIcons'), False);
    AToolBarVisible.Checked := GetValue(DOMString(Path + '/ToolBarVisible'), True);
    AStatusBarVisible.Checked := GetValue(DOMString(Path + '/StatusBarVisible'), False);
  end;
end;

procedure TFormMain.SaveConfig(Config: TXmlConfig; Path: string);
begin
  with Config do begin
    SetValue(DOMString(Path + '/LargeIcons'), AToolBarBigIcons.Checked);
    SetValue(DOMString(Path + '/ToolBarVisible'), AToolBarVisible.Checked);
    SetValue(DOMString(Path + '/StatusBarVisible'), AStatusBarVisible.Checked);
  end;
end;

procedure TFormMain.ReloadView;
begin
  if AToolBarBigIcons.Checked then begin
    ToolBar1.Images := Core.ImageListLarge;
    ToolBar1.ButtonWidth := Core.ImageListLarge.Width + 7;
    ToolBar1.ButtonHeight := Core.ImageListLarge.Height + 6;
    ToolBar1.Width := Core.ImageListLarge.Width + 10;
    ToolBar1.Height := Core.ImageListLarge.Height + 10;
  end else begin
    ToolBar1.Images := Core.ImageListSmall;
    ToolBar1.ButtonWidth := Core.ImageListSmall.Width + 7;
    ToolBar1.ButtonHeight := Core.ImageListSmall.Height + 6;
    ToolBar1.Width := Core.ImageListSmall.Width + 10;
    ToolBar1.Height := Core.ImageListSmall.Height + 10;
  end;
  ToolBar1.Visible := AToolBarVisible.Checked;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  FormClient := TFormClient.Create(nil);
  FormClient.ManualDock(PanelMain, nil, alClient);
  FormClient.Align := alClient;
  {$IFDEF DEBUG}
  MenuItemDebug.Visible := True;
  MenuItemDebug.Enabled := True;
  {$ELSE}
  MenuItemDebug.Visible := False;
  MenuItemDebug.Enabled := False;
  {$ENDIF}
end;

procedure TFormMain.AStatusBarVisibleExecute(Sender: TObject);
begin
  AStatusBarVisible.Checked := not AStatusBarVisible.Checked;
  UpdateClientForms;
end;

procedure TFormMain.AMapGridVisibleExecute(Sender: TObject);
begin
  AMapGridVisible.Checked := not AMapGridVisible.Checked;
  UpdateClientForms;
end;

procedure TFormMain.AToolBarBigIconsExecute(Sender: TObject);
begin
  AToolBarBigIcons.Checked := not AToolBarBigIcons.Checked;
  ReloadView;
  UpdateClientForms;
end;

procedure TFormMain.AToolBarVisibleExecute(Sender: TObject);
begin
  AToolBarVisible.Checked := not AToolBarVisible.Checked;
  ReloadView;
  UpdateClientForms;
end;

procedure TFormMain.AUnitShapeVisibleExecute(Sender: TObject);
begin
  AUnitShapeVisible.Checked := not AUnitShapeVisible.Checked;
  UpdateClientForms;
end;

procedure TFormMain.FormActivate(Sender: TObject);
begin
  if not FormActivated then begin
    FormActivated := True;
  end;
end;

procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  // Pass KeyUp event to docked form
  FormClient.FormKeyUp(Sender, Key, Shift);
end;

procedure TFormMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  Core.Game.Running := False;
  Core.PersistentForm.Save(Self);
  SaveConfig(Core.XMLConfig1, 'FormMain');
  Core.CellGridVisible := AMapGridVisible.Checked;
  Core.UnitShapeVisible := AUnitShapeVisible.Checked;
  Core.Done;
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  FormClient.Free;
end;

procedure TFormMain.ApplicationStart(Ptr: IntPtr);
begin
  Core.ReopenGameOnInit;
end;

procedure TFormMain.UpdateClientForms;
var
  I: Integer;
begin
  FormClient.AToolBarBigIcons.Checked := AToolBarBigIcons.Checked;
  FormClient.AToolBarBigIcons.Update;
  FormClient.AToolBarVisible.Checked := AToolBarVisible.Checked;
  FormClient.AToolBarVisible.Update;
  FormClient.AStatusBarVisible.Checked := AStatusBarVisible.Checked;
  FormClient.AStatusBarVisible.Update;
  Core.CellGridVisible := AMapGridVisible.Checked;
  Core.UnitShapeVisible := AUnitShapeVisible.Checked;
  if Assigned(FormClient.Client) then begin
    FormClient.Client.CellGridVisible := Core.CellGridVisible;
    FormClient.Client.UnitShapeVisible := Core.UnitShapeVisible;
  end;
  FormClient.Redraw;
  for I := 0 to Core.FormClients.Count - 1 do begin
    Core.FormClients[I].AToolBarBigIcons.Checked := AToolBarBigIcons.Checked;
    Core.FormClients[I].AToolBarBigIcons.Update;
    Core.FormClients[I].AToolBarVisible.Checked := AToolBarVisible.Checked;
    Core.FormClients[I].AToolBarVisible.Update;
    Core.FormClients[I].AStatusBarVisible.Checked := AStatusBarVisible.Checked;
    Core.FormClients[I].AStatusBarVisible.Update;
    if Assigned(Core.FormClients[I].Client) then begin
      Core.FormClients[I].Client.CellGridVisible := Core.CellGridVisible;
      Core.FormClients[I].Client.UnitShapeVisible := Core.UnitShapeVisible;
    end;
    Core.FormClients[I].Redraw;
  end;
end;

procedure TFormMain.FormShow(Sender: TObject);
begin
  if not FormShown then begin
    Core.LoadConfig;
    AMapGridVisible.Checked := Core.CellGridVisible;
    AUnitShapeVisible.Checked := Core.UnitShapeVisible;
    Core.ScaleDPI;
    Core.PersistentForm.Load(Self, True);
    Core.ThemeManager1.UseTheme(Self);
    Core.Init;
    LoadConfig(Core.XMLConfig1, 'FormMain');
    ReloadView;
    FormClient.Show;
    UpdateClientForms;
    Application.QueueAsyncCall(ApplicationStart, 0);
    FormShown := True;
  end;
end;

end.

