{*************************************************************************}
{                                                                         }
{ OptionsUnit.pas - game options windows                                  }
{                                                                         }
{ This file is a part of the project:                                     }
{   Dizzy Quest Editor - the quest editor for Dizzy-like games            }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This program is free software; you can redistribute it and/or modify    }
{ it under the terms of the GNU General Public License as published by    }
{ the Free Software Foundation; either version 2 of the License, or       }
{ (at your option) any later version.                                     }
{                                                                         }
{ This program is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            }
{ GNU General Public License for more details.                            }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{*************************************************************************}
unit OptionsUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, AsphyreSubsc, AsphyreDevices, Asphyre2D,
  AsphyreCanvas,dqe2_types,alkatraz, ExtCtrls,math, Spin;

type
  TForm_Options = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    GroupBox1: TGroupBox;
    ComboBox1: TComboBox;
    Label1: TLabel;
    CheckBox4: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox5: TCheckBox;
    GroupBox2: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    TabSheet2: TTabSheet;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    GroupBox4: TGroupBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox6: TCheckBox;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    Label11: TLabel;
    TabSheet3: TTabSheet;
    GroupBox5: TGroupBox;
    SpinEdit1: TSpinEdit;
    Label12: TLabel;
    GroupBox6: TGroupBox;
    Label13: TLabel;
    SpinEdit2: TSpinEdit;
    TabSheet4: TTabSheet;
    GroupBox7: TGroupBox;
    CheckBox9: TCheckBox;
    Button1: TButton;
    Button2: TButton;
    Label14: TLabel;
    CheckBox10: TCheckBox;
    CheckBox11: TCheckBox;
    GroupBox8: TGroupBox;
    SpinEdit3: TSpinEdit;
    Label15: TLabel;
    Label16: TLabel;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure UpdateList;
    procedure CheckBox8Click(Sender: TObject);
    procedure CheckBox7Click(Sender: TObject);
    procedure CheckBox6Click(Sender: TObject);
    function GetButNum(But:TSpeedButton):Integer;
    procedure OnKeyNeed(Sender:TObject);
    procedure OnJoyNeed(Sender:TObject);
    procedure BitBtn3Click(Sender: TObject);
    function ShowIt:boolean;
    procedure Localize;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    okey:boolean;
  public
    { Public declarations }
  end;

var
  Form_Options: TForm_Options;
  cnt:DQE2_ControlOptions;
  vid:DQE2_VideoOptions;

implementation

uses WaitUnit, MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_Options.BitBtn1Click(Sender: TObject);
begin
  BitBtn3.Click;
  okey:=true;
  close;
end;

procedure TForm_Options.BitBtn2Click(Sender: TObject);
begin
  close;
end;

procedure TForm_Options.BitBtn3Click(Sender: TObject);
begin
  vid.BitDepth:=(ComboBox1.ItemIndex=1);
  vid.ZBuffer:=CheckBox4.Checked;
  vid.Hardware:=CheckBox3.Checked;
  vid.VSync:=CheckBox5.Checked;
  vid.AntiAliasing:=CheckBox1.Checked;
  vid.Dithering:=CheckBox2.Checked;
  world.VideoOptions:=vid;
  world.ControlOptions:=cnt;
  World.UseCache:=CheckBox9.Checked;
  World.GameSpeed:=SpinEdit3.Value;
end;

procedure TForm_Options.Button1Click(Sender: TObject);
begin
  World.DeleteCache;
end;

procedure TForm_Options.Button2Click(Sender: TObject);
begin
  label14.Visible:=true;
  Application.ProcessMessages;
  World.MakeCache;
  label14.Visible:=false;
end;

procedure TForm_Options.CheckBox6Click(Sender: TObject);
begin
  World.JoystickEnabled:=CheckBox6.Checked;
  CheckBox7.Visible:=World.JoystickEnabled;
  CheckBox8.Visible:=World.JoystickEnabled;
  SpeedButton7.Visible:=World.JoystickEnabled;
  SpeedButton8.Visible:=World.JoystickEnabled;
  SpeedButton9.Visible:=World.JoystickEnabled and CheckBox8.Checked;
  label8.Visible:=World.JoystickEnabled;
  label9.Visible:=World.JoystickEnabled;
  label10.Visible:=World.JoystickEnabled and CheckBox8.Checked;
end;

procedure TForm_Options.CheckBox7Click(Sender: TObject);
begin
  cnt.JoyXasY:=CheckBox7.Checked;
end;

procedure TForm_Options.CheckBox8Click(Sender: TObject);
begin
  cnt.JoyBtnForJump:=CheckBox8.Checked;
  SpeedButton9.Visible:=CheckBox8.Checked;
  label10.Visible:=SpeedButton9.Visible;
end;

procedure TForm_Options.FormCreate(Sender: TObject);
begin
  SetLength(lng,43);
end;

function TForm_Options.GetButNum(But: TSpeedButton): Integer;
begin
  result:=strtoint(but.name[length(but.name)]);
end;

procedure TForm_Options.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  TabSheet1.Caption:=lng[1];
  GroupBox1.Caption:=lng[2];
  Label1.Caption:=lng[3];
  ComboBox1.Items[0]:=lng[4];
  ComboBox1.Items[1]:=lng[5];
  CheckBox4.Caption:=lng[6];
  CheckBox3.Caption:=lng[7];
  CheckBox5.Caption:=lng[8];
  GroupBox2.Caption:=lng[9];
  CheckBox1.Caption:=lng[10];
  CheckBox2.Caption:=lng[11];
  Label11.Caption:=lng[12];
  TabSheet2.Caption:=lng[13];
  GroupBox3.Caption:=lng[14];
  Label2.Caption:=lng[15];
  Label3.Caption:=lng[16];
  Label4.Caption:=lng[17];
  Label5.Caption:=lng[18];
  Label6.Caption:=lng[19];
  Label7.Caption:=lng[20];
  GroupBox4.Caption:=lng[21];
  CheckBox6.Caption:=lng[22];
  CheckBox7.Caption:=lng[23];
  CheckBox8.Caption:=lng[24];
  Label8.Caption:=lng[19];
  Label9.Caption:=lng[20];
  Label10.Caption:=lng[25];
  TabSheet3.Caption:=lng[26];
  GroupBox5.Caption:=lng[27];
  Label12.Caption:=lng[28];
  GroupBox6.Caption:=lng[29];
  Label13.Caption:=lng[30];
  BitBtn2.Caption:=lng[31];
  BitBtn3.Caption:=lng[32];
  TabSheet4.Caption:=lng[34];
  GroupBox7.Caption:=lng[35];
  CheckBox9.Caption:=lng[36];
  Button1.Caption:=lng[37];
  Button2.Caption:=lng[38];
  label14.Caption:=lng[39];
  CheckBox10.Caption:=lng[40];
  CheckBox11.Caption:=lng[41];
  label16.Caption:=lng[42];
end;

procedure TForm_Options.OnJoyNeed(Sender: TObject);
var
  n,n1:integer;
  sp:TSpeedButton;
begin
  Form_Wait.DoWait(false);
  if WaitUnit.res<>-1 then
  begin
    sp:=(Sender as TSpeedButton);
    sp.Caption:=lng[33]+' '+inttostr(WaitUnit.res);
    n:=GetButNum(sp);n1:=(1 shl WaitUnit.res);
    case n of
      7:cnt.JoyFire:=n1;
      8:cnt.JoyCancel:=n1;
      9:cnt.JoyJump:=n1;
    end;
  end;
end;

procedure TForm_Options.OnKeyNeed(Sender: TObject);
var
  n:integer;
  sp:TSpeedButton;
begin
  Form_Wait.DoWait(true);
  if WaitUnit.res<>-1 then
  begin
    sp:=(Sender as TSpeedButton);
    sp.Caption:=GetKeyName(WaitUnit.res);
    n:=GetButNum(sp);
    case n of
      1:cnt.KeyLeft:=WaitUnit.res;
      2:cnt.KeyRight:=WaitUnit.res;
      3:cnt.KeyUp:=WaitUnit.res;
      4:cnt.KeyDown:=WaitUnit.res;
      5:cnt.KeyFire:=WaitUnit.res;
      6:cnt.KeyCancel:=WaitUnit.res;
    end;
  end;
end;

function TForm_Options.ShowIt: boolean;
begin
  okey:=false;
  UpdateList;
  ShowModal;
  result:=okey;
end;

procedure TForm_Options.UpdateList;
begin
  vid:=world.VideoOptions;
  if vid.BitDepth then
    ComboBox1.ItemIndex:=1
  else
    ComboBox1.ItemIndex:=0;
  CheckBox4.Checked:=vid.ZBuffer;
  CheckBox3.Checked:=vid.Hardware;
  CheckBox5.Checked:=vid.VSync;
  CheckBox1.Checked:=vid.AntiAliasing;
  CheckBox2.Checked:=vid.Dithering;
  cnt:=world.ControlOptions;
  SpeedButton1.Caption:=GetKeyName(cnt.KeyLeft);
  SpeedButton2.Caption:=GetKeyName(cnt.KeyRight);
  SpeedButton3.Caption:=GetKeyName(cnt.KeyUp);
  SpeedButton4.Caption:=GetKeyName(cnt.KeyDown);
  SpeedButton5.Caption:=GetKeyName(cnt.KeyFire);
  SpeedButton6.Caption:=GetKeyName(cnt.KeyCancel);
  SpeedButton7.Caption:=lng[33]+' '+inttostr(trunc(Log2(cnt.JoyFire)));
  SpeedButton8.Caption:=lng[33]+' '+inttostr(trunc(Log2(cnt.JoyCancel)));
  SpeedButton9.Caption:=lng[33]+' '+inttostr(trunc(Log2(cnt.JoyJump)));
  CheckBox7.Checked:=cnt.JoyXasY;
  CheckBox8.Checked:=cnt.JoyBtnForJump;
  CheckBox8Click(nil);
  CheckBox6.Checked:=World.JoystickEnabled;
  CheckBox6Click(nil);
  CheckBox9.Checked:=world.UseCache;
  SpinEdit3.Value:=world.GameSpeed;
  GroupBox4.Visible:=World.JoystickFound;
  Label16.Visible:=not GroupBox4.Visible;
end;

end.
