{*************************************************************************}
{                                                                         }
{ GameSettingsUnit.pas - main game settings window                        }
{                                                                         }
{ 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 GameSettingsUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SynEditHighlighter, SynHighlighterPas, SynEdit, SynMemo, StdCtrls,
  ComCtrls, Spin, JvExControls, JvComponent, JvAnimatedImage, JvGIFCtrl,
  Buttons, ExtCtrls,dqe2_types,alkatraz, GR32_Image,jpeg,jvgif,inifiles,gr32;

type
  TForm_GameSettings = class(TForm)
    Button1: TButton;
    PageControl1: TPageControl;
    TabSheet2: TTabSheet;
    SpeedButton1: TSpeedButton;
    JvGIFAnimator1: TJvGIFAnimator;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    SpinEdit1: TSpinEdit;
    SpinEdit3: TSpinEdit;
    SpinEdit2: TSpinEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label16: TLabel;
    TabSheet1: TTabSheet;
    ComboBox1: TComboBox;
    Label3: TLabel;
    Panel1: TPanel;
    _head: TImage32;
    _top: TImage32;
    _left: TImage32;
    _bag: TImage32;
    _bottom: TImage32;
    _right: TImage32;
    _cancel: TImage32;
    TabSheet3: TTabSheet;
    GroupBox1: TGroupBox;
    ComboBox2: TComboBox;
    GroupBox2: TGroupBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    ComboBox5: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    ComboBox6: TComboBox;
    Label8: TLabel;
    TabSheet4: TTabSheet;
    Label9: TLabel;
    ComboBox7: TComboBox;
    Label10: TLabel;
    ComboBox8: TComboBox;
    Label11: TLabel;
    ComboBox9: TComboBox;
    Label12: TLabel;
    ComboBox10: TComboBox;
    GroupBox3: TGroupBox;
    Label13: TLabel;
    Label14: TLabel;
    SpinEdit4: TSpinEdit;
    TabSheet5: TTabSheet;
    SpinEdit5: TSpinEdit;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure UpdateSkin(SkinName:string);
    procedure FormShow(Sender: TObject);
    procedure UpdateForm;
    procedure UpdateObject;
    procedure Button1Click(Sender: TObject);
    procedure Localize;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_GameSettings: TForm_GameSettings;

implementation

uses ChooseUnit, MainUnit;

{$R *.dfm}

var lng:array of string;

procedure TForm_GameSettings.Button1Click(Sender: TObject);
begin
  UpdateObject;
  close;
end;

procedure TForm_GameSettings.ComboBox1Change(Sender: TObject);
begin
  UpdateSkin(ComboBox1.Text);
end;

procedure TForm_GameSettings.FormCreate(Sender: TObject);
begin
  _head.SetupBitmap(true,clBlack32);
  _left.SetupBitmap(true,clBlack32);
  _right.SetupBitmap(true,clBlack32);
  _top.SetupBitmap(true,clBlack32);
  _bottom.SetupBitmap(true,clBlack32);
  _bag.SetupBitmap(true,clBlack32);
  _cancel.SetupBitmap(true,clBlack32);
  SetLength(lng,27);
end;

procedure TForm_GameSettings.FormShow(Sender: TObject);
var
  f:TSearchRec;
  s:string;
  a:Integer;
begin
  ComboBox2.Clear;ComboBox3.Clear;ComboBox4.Clear;
  ComboBox5.Clear;ComboBox6.Clear;
  for a:=0 to world.Fonts.Count-1 do
  begin
    s:=DQE2_Font(world.Fonts[a]).Name;
    ComboBox2.Items.Add(s);ComboBox3.Items.Add(s);ComboBox4.Items.Add(s);
    ComboBox5.Items.Add(s);ComboBox6.Items.Add(s);
  end;
  ComboBox7.Clear;ComboBox8.Clear;ComboBox9.Clear;ComboBox10.Clear;
  ComboBox7.Items.Add(lng[22]);
  ComboBox8.Items.Add(lng[22]);
  ComboBox9.Items.Add(lng[22]);
  ComboBox10.Items.Add(lng[22]);
  for a:=1 to World.MusicPlayer.MusicCount-1 do
  begin
    s:=ExtractFileName(World.MusicPlayer.Musics[a].FileName);
    ComboBox7.Items.Add(s);ComboBox8.Items.Add(s);
    ComboBox9.Items.Add(s);ComboBox10.Items.Add(s);
  end;
  UpdateForm;
  s:=ComboBox1.Text;
  ComboBox1.Clear;
  if FindFirst(world.SharedDir+'\Skins\*',faDirectory,f)=0 then
  repeat
    if FileExists(world.SharedDir+'\Skins\'+f.Name+'\Skin.ini')then
      ComboBox1.Items.Add(f.Name);
  until FindNext(f)<>0;
  FindClose(f);
  ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(s);
  if ComboBox1.ItemIndex=-1 then
    ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf(world.SkinName);
  if ComboBox1.ItemIndex=-1 then ComboBox1.ItemIndex:=0;
  SpinEdit4.Value:=world.ScreenWidth;SpinEdit5.Value:=world.ScreenHeight;
  UpdateSkin(ComboBox1.Text);
end;

procedure TForm_GameSettings.Localize;
begin
  LoadStringsFromIni(MainUnit.lang_ini,Name,lng);
  Caption:=lng[0];
  TabSheet2.Caption:=lng[1];
  LabeledEdit1.EditLabel.Caption:=lng[2];
  LabeledEdit2.EditLabel.Caption:=lng[3];
  Label1.Caption:=lng[4];
  Label16.Caption:=lng[5];
  Label2.Caption:=lng[6];
  TabSheet1.Caption:=lng[7];
  Label3.Caption:=lng[8];
  TabSheet3.Caption:=lng[9];
  GroupBox1.Caption:=lng[10];
  Label4.Caption:=lng[11];
  Label5.Caption:=lng[12];
  Label6.Caption:=lng[13];
  GroupBox2.Caption:=lng[14];
  Label7.Caption:=lng[15];
  Label8.Caption:=lng[16];
  TabSheet4.Caption:=lng[17];
  Label12.Caption:=lng[18];
  Label9.Caption:=lng[19];
  Label11.Caption:=lng[20];
  Label10.Caption:=lng[21];
  GroupBox3.Caption:=lng[23];
  Label13.Caption:=lng[24];
end;

procedure TForm_GameSettings.UpdateForm;
begin
  LabeledEdit1.Text:=World.Player.Name;
  LabeledEdit2.Text:=World.Player.Sprite.ShortName;
  SpinEdit1.Value:=World.Player.MaxLifes;
  SpinEdit2.Value:=World.Player.MaxEnergy;
  SpinEdit3.Value:=World.Player.Bag.Capacity;
  ComboBox2.ItemIndex:=World.FontPersonName.Index;
  ComboBox3.ItemIndex:=World.FontQuestion.Index;
  ComboBox4.ItemIndex:=World.FontAnswer.Index;
  ComboBox5.ItemIndex:=World.FontItemName.Index;
  ComboBox6.ItemIndex:=World.FontItemText.Index;
  if World.SoundWalk=nil then ComboBox10.ItemIndex:=0 else ComboBox10.ItemIndex:=World.SoundWalk.Index;
  if World.SoundJump=nil then ComboBox7.ItemIndex:=0 else ComboBox7.ItemIndex:=World.SoundJump.Index;
  if World.SoundWound=nil then ComboBox9.ItemIndex:=0 else ComboBox9.ItemIndex:=World.SoundWound.Index;
  if World.SoundDie=nil then ComboBox8.ItemIndex:=0 else ComboBox8.ItemIndex:=World.SoundDie.Index;
  JvGIFAnimator1.Image.LoadFromFile(world.SharedDir+'\'+world.Player.Sprite.Name);
end;

procedure TForm_GameSettings.UpdateObject;
var
  a:Integer;
  z:TStringList;
  zn:DQE2_Zone;
begin
  world.Player.Bag.Capacity:=SpinEdit1.Value;
  world.Player.Name:=LabeledEdit1.Text;
  world.Player.MaxLifes:=SpinEdit1.Value;
  world.Player.MaxEnergy:=SpinEdit2.Value;
  world.Player.Bag.Capacity:=SpinEdit3.Value;
  if(ComboBox1.Text<>'')and(ComboBox1.Text<>world.SkinName)then
  begin
    world.LoadSkin(ComboBox1.Text);
  end;
  world.FontPersonName:=DQE2_Font(world.Fonts[ComboBox2.ItemIndex]);
  world.FontQuestion:=DQE2_Font(world.Fonts[ComboBox3.ItemIndex]);
  world.FontAnswer:=DQE2_Font(world.Fonts[ComboBox4.ItemIndex]);
  world.FontItemName:=DQE2_Font(world.Fonts[ComboBox5.ItemIndex]);
  world.FontItemText:=DQE2_Font(world.Fonts[ComboBox6.ItemIndex]);
  if ComboBox10.ItemIndex=0 then World.SoundWalk:=nil else World.SoundWalk:=World.MusicPlayer.Musics[ComboBox10.ItemIndex];
  if ComboBox7.ItemIndex=0 then World.SoundJump:=nil else World.SoundJump:=World.MusicPlayer.Musics[ComboBox7.ItemIndex];
  if ComboBox9.ItemIndex=0 then World.SoundWound:=nil else World.SoundWound:=World.MusicPlayer.Musics[ComboBox9.ItemIndex];
  if ComboBox8.ItemIndex=0 then World.SoundDie:=nil else World.SoundDie:=World.MusicPlayer.Musics[ComboBox8.ItemIndex];
  //
  if(SpinEdit4.Value<>world.ScreenWidth)or(SpinEdit5.Value<>world.ScreenHeight)then
  begin
    z:=TStringList.Create;
    for a:=0 to World.Zones.Count-1 do
    begin
      zn:=DQE2_Zone(World.Zones[a]);
      if(zn.Width<SpinEdit4.Value)or(zn.Height<SpinEdit4.Value)then
        z.Add(zn.Name);
    end;
    if z.Text<>'' then
    begin
      MessageDlgRus(lng[25],lng[26]+#13#10+z.Text,MB_OK);
      z.Free;
      exit;
    end;
    z.Free;
    World.SetResolution(SpinEdit4.Value,SpinEdit5.Value);
  end;
end;

procedure TForm_GameSettings.UpdateSkin(SkinName: string);
var
  i:TIniFile;
  s:string;
begin
  if SkinName='' then exit;
  s:=world.SharedDir+'\Skins\'+SkinName+'\';
  i:=TIniFile.Create(s+'Skin.ini');
  _head.Bitmap.LoadFromFile(s+i.ReadString('Borders','Head','_head.gif'));
  _left.Bitmap.LoadFromFile(s+i.ReadString('Borders','Left','_left.gif'));
  _right.Bitmap.LoadFromFile(s+i.ReadString('Borders','Right','_right.gif'));
  _top.Bitmap.LoadFromFile(s+i.ReadString('Borders','Top','_top.gif'));
  _bottom.Bitmap.LoadFromFile(s+i.ReadString('Borders','Bottom','_bottom.gif'));
  _bag.Bitmap.LoadFromFile(s+i.ReadString('Windows','Bag','Bag.gif'));
  _cancel.Bitmap.LoadFromFile(s+i.ReadString('Windows','Cancel','Cancel.gif'));
  i.Free;
end;

end.
