unit UFormSettings;

{$mode delphi}

interface

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

type

  { TFormSettings }

  TFormSettings = class(TForm)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    CheckBox2: TCheckBox;
    CheckBoxAutoSaveTurn: TCheckBox;
    CheckBoxDevelMode: TCheckBox;
    CheckBoxReopenLastFile: TCheckBox;
    ComboBoxLanguage: TComboBox;
    ComboBoxTheme: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    PageControl1: TPageControl;
    Panel1: TPanel;
    Panel2: TPanel;
    SpinEditAnimSpeed: TSpinEdit;
    SpinEditX: TSpinEdit;
    SpinEditY: TSpinEdit;
    TabSheetGeneral: TTabSheet;
    TabSheetDebug: TTabSheet;
    procedure CheckBox2Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    procedure Load;
    procedure Save;
  end;

var
  FormSettings: TFormSettings;

implementation

{$R *.lfm}

uses
  UCore, ULanguages, UTheme;

{ TFormSettings }

procedure TFormSettings.CheckBox2Change(Sender: TObject);
begin
  SpinEditX.Enabled := not CheckBox2.Checked;
  SpinEditY.Enabled := not CheckBox2.Checked;
end;

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

procedure TFormSettings.FormCreate(Sender: TObject);
begin
  PageControl1.TabIndex := 0;
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
  Core.ThemeManager1.Themes.LoadToStrings(ComboBoxTheme.Items);
  Core.CoolTranslator1.LanguageListToStrings(ComboBoxLanguage.Items);
end;

procedure TFormSettings.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  {$IFDEF DEBUG}
  TabSheetDebug.TabVisible := True;
  {$ELSE}
  TabSheetDebug.TabVisible := False;
  {$ENDIF}
end;

procedure TFormSettings.Load;
begin
  ComboBoxLanguage.ItemIndex := ComboBoxLanguage.Items.IndexOfObject(Core.CoolTranslator1.Language);
  if ComboBoxLanguage.ItemIndex = -1 then ComboBoxLanguage.ItemIndex := 0;
  ComboBoxTheme.ItemIndex := ComboBoxTheme.Items.IndexOfObject(Core.ThemeManager1.Theme);
  if ComboBoxTheme.ItemIndex = -1 then ComboBoxTheme.ItemIndex := 0;
  CheckBoxDevelMode.Checked := Core.DevelMode;
  SpinEditAnimSpeed.Value := Core.AnimationSpeed;
  CheckBox2.Checked := Core.ScaleDPI1.AutoDetect;
  SpinEditX.Value := Core.ScaleDPI1.DPI.X;
  SpinEditY.Value := Core.ScaleDPI1.DPI.Y;
  CheckBoxAutoSaveTurn.Checked := Core.AutoSaveEnabled;
  CheckBoxReopenLastFile.Checked := Core.ReopenLastFile;
end;

procedure TFormSettings.Save;
begin
  if ComboBoxLanguage.ItemIndex <> -1 then
    Core.CoolTranslator1.Language := TLanguage(ComboBoxLanguage.Items.Objects[ComboBoxLanguage.ItemIndex]);
   if ComboBoxTheme.ItemIndex <> -1 then
    Core.ThemeManager1.Theme := TTheme(ComboBoxTheme.Items.Objects[ComboBoxTheme.ItemIndex]);
  Core.DevelMode := CheckBoxDevelMode.Checked;
  Core.AnimationSpeed := SpinEditAnimSpeed.Value;
  Core.ScaleDPI1.AutoDetect := CheckBox2.Checked;
  Core.ScaleDPI1.DPI := Point(SpinEditX.Value, SpinEditY.Value);
  Core.AutoSaveEnabled := CheckBoxAutoSaveTurn.Checked;
  Core.ReopenLastFile := CheckBoxReopenLastFile.Checked;
end;

end.

