unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, Unit3, ExtCtrls;

type
  TFrmSettings = class(TForm)
    Label1: TLabel;
    cmbOrientation: TComboBox;
    Label2: TLabel;
    txtFPS: TSpinEdit;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    txtSoundFreq: TSpinEdit;
    Label4: TLabel;
    cmbBps: TComboBox;
    chkStereo: TCheckBox;
    Label5: TLabel;
    Label6: TLabel;
    txtMaxMusics: TSpinEdit;
    Label7: TLabel;
    txtMaxSounds: TSpinEdit;
    GroupBox2: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Label8: TLabel;
    txtProjectName: TEdit;
    Label9: TLabel;
    txtProjectAuhtor: TEdit;
    GroupBox3: TGroupBox;
    chkAudioExtensions: TCheckBox;
    Label10: TLabel;
    chkFontsEstensions: TCheckBox;
    GroupBox4: TGroupBox;
    Label11: TLabel;
    txtStartCol1: TEdit;
    txtStartCol2: TEdit;
    txtStartCol3: TEdit;
    Label12: TLabel;
    txtTrans1: TEdit;
    txtTrans2: TEdit;
    txtTrans3: TEdit;
    chkvga: TCheckBox;
    cmbResolution: TComboBox;
    Label13: TLabel;
    Label15: TLabel;
    GroupBox5: TGroupBox;
    resw: TListBox;
    resh: TListBox;
    Label14: TLabel;
    imgIcon: TImage;
    txtNewIcon: TLabel;
    dlgSelectIcon: TOpenDialog;
    Shape1: TShape;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure chkvgaClick(Sender: TObject);
    procedure cmbOrientationChange(Sender: TObject);
    procedure imgIconClick(Sender: TObject);
  private
    { Private declarations }
    procedure UpdateResCombo();
  public
    { Public declarations }
  end;

var
  FrmSettings: TFrmSettings;

implementation

{$R *.dfm}

procedure TFrmSettings.UpdateResCombo();
begin
  cmbResolution.Clear;
  resw.Clear;
  resh.Clear;
  // cambia le risoluzioni disponibili in base al checkbox "VGA" e all'orientamento
  if (cmbOrientation.ItemIndex = 0) or (cmbOrientation.ItemIndex = 1) then begin
    if chkvga.Checked then begin
      cmbResolution.Items.Add('Normal VGA 480x640'); resw.Items.Add('480'); resh.Items.Add('640');
    end;
    cmbResolution.Items.Add('Normal QVGA 240x320'); resw.Items.Add('240'); resh.Items.Add('320');
  end else begin
    if chkvga.Checked then begin
      cmbResolution.Items.Add('Landscape VGA 640x480'); resw.Items.Add('640'); resh.Items.Add('480');
    end;
    cmbResolution.Items.Add('Landscape QVGA 320x240'); resw.Items.Add('320'); resh.Items.Add('240');
  end;
  if chkvga.Checked then begin
      cmbResolution.Items.Add('Square VGA 480x480'); resw.Items.Add('480'); resh.Items.Add('480');
  end;
  cmbResolution.Items.Add('Square QVGA 240x240'); resw.Items.Add('240'); resh.Items.Add('240');
  cmbResolution.ItemIndex:=0;
end;

procedure TFrmSettings.FormCreate(Sender: TObject);
var
a: Integer;
begin
  // carica la form dai settings
  cmbOrientation.ItemIndex :=StrToInt(SETTINGS[SETTINGS_ORIENTATION]);
  txtFPS.Text :=SETTINGS[SETTINGS_FPS];
  txtSoundFreq.Text :=SETTINGS[SETTINGS_SOUNDFREQ];
  if SETTINGS[SETTINGS_SOUNDBPS]='16' then begin
    cmbBps.ItemIndex:=0;
  end else begin
    cmbBps.ItemIndex:=1;
  end;
  if SETTINGS[SETTINGS_SOUNDSTEREO]='1' then begin
    chkstereo.Checked := true;
  end else begin
    chkstereo.Checked := false;
  end;
  txtMaxMusics.Text :=SETTINGS[SETTINGS_SOUNDMAXMUSCHANNELS];
  txtMaxSounds.Text :=SETTINGS[SETTINGS_SOUNDMAXSFXCHANNELS];
  txtProjectName.Text:=SETTINGS[SETTINGS_PROJECTNAME];
  txtProjectAuhtor.Text:=SETTINGS[SETTINGS_PROJECTAUHTOR];
  if SETTINGS[SETTINGS_EXT_SOUNDS]='1' then begin
    chkAudioExtensions.Checked := true;
  end else begin
    chkAudioExtensions.Checked := false;
  end;
  if SETTINGS[SETTINGS_EXT_TTF]='1' then begin
    chkFontsEstensions.Checked := true;
  end else begin
    chkFontsEstensions.Checked := false;
  end;
  if SETTINGS[SETTINGS_VGA]='1' then begin
    chkvga.Checked := true;
  end else begin
    chkvga.Checked := false;
  end;
  txtStartCol1.Text := SETTINGS[SETTINGS_START_BACKCOL1];
  txtStartCol2.Text := SETTINGS[SETTINGS_START_BACKCOL2];
  txtStartCol3.Text := SETTINGS[SETTINGS_START_BACKCOL3];

  txtTrans1.Text := SETTINGS[SETTINGS_TRANS1];
  txtTrans2.Text := SETTINGS[SETTINGS_TRANS2];
  txtTrans3.Text := SETTINGS[SETTINGS_TRANS3];

  // aggiorna combo resolutions
  UpdateResCombo();
  // seleziona risoluzione giusta nel combo in base a width e height del conf
  if resw.Items.Count > 0 then begin
    for a:=0 to resw.Items.Count-1 do begin
      if (resw.Items.Strings[a] = SETTINGS[SETTINGS_WINDOWS_WIDTH]) and (resh.Items.Strings[a] = SETTINGS[SETTINGS_WINDOWS_HEIGHT]) then begin
        cmbResolution.ItemIndex:=a;
        break;
      end;
    end;
  end;

  // carica icona selezionata
  txtNewIcon.Caption := '';
  if Length(SETTINGS[SETTINGS_APPLICATION_ICON]) > 0 then begin
    try
      txtNewIcon.Caption := SETTINGS[SETTINGS_APPLICATION_ICON];
      imgIcon.Picture.LoadFromFile(txtNewIcon.Caption);
    except
      txtNewIcon.Caption := '';
      SETTINGS[SETTINGS_APPLICATION_ICON]:='';
    end;
  end;

end;

procedure TFrmSettings.Button1Click(Sender: TObject);
begin
  self.Close;
end;

procedure TFrmSettings.Button2Click(Sender: TObject);
begin
  // salva i settings dalla form
  SETTINGS[SETTINGS_ORIENTATION] := IntToStr( cmbOrientation.ItemIndex );
  SETTINGS[SETTINGS_FPS] := txtFPS.Text;
  SETTINGS[SETTINGS_SOUNDFREQ] := txtSoundFreq.Text;
  SETTINGS[SETTINGS_SOUNDBPS] := cmbBps.Text;
  if chkstereo.Checked then begin
    SETTINGS[SETTINGS_SOUNDSTEREO] := '1';
  end else begin
    SETTINGS[SETTINGS_SOUNDSTEREO] := '0';
  end;

  if chkvga.Checked then begin
    SETTINGS[SETTINGS_VGA] := '1';
  end else begin
    SETTINGS[SETTINGS_VGA] := '0';
  end;
  SETTINGS[SETTINGS_WINDOWS_WIDTH] := resw.Items.Strings[cmbResolution.ItemIndex];
  SETTINGS[SETTINGS_WINDOWS_HEIGHT] := resh.Items.Strings[cmbResolution.ItemIndex];

  SETTINGS[SETTINGS_APPLICATION_ICON] := txtNewIcon.Caption;

  SETTINGS[SETTINGS_SOUNDMAXMUSCHANNELS] := txtMaxMusics.Text;
  SETTINGS[SETTINGS_SOUNDMAXSFXCHANNELS] := txtMaxSounds.Text;
  SETTINGS[SETTINGS_PROJECTNAME] := txtProjectName.Text;
  SETTINGS[SETTINGS_PROJECTAUHTOR] := txtProjectAuhtor.Text;

  SETTINGS[SETTINGS_START_BACKCOL1] := txtStartCol1.Text;
  SETTINGS[SETTINGS_START_BACKCOL2] := txtStartCol2.Text;
  SETTINGS[SETTINGS_START_BACKCOL3] := txtStartCol3.Text;

  SETTINGS[SETTINGS_TRANS1] := txtTrans1.Text;
  SETTINGS[SETTINGS_TRANS2] := txtTrans2.Text;
  SETTINGS[SETTINGS_TRANS3] := txtTrans3.Text;

  if chkAudioExtensions.Checked then begin
    SETTINGS[SETTINGS_EXT_SOUNDS] := '1';
  end else begin
    SETTINGS[SETTINGS_EXT_SOUNDS] := '0';
  end;
  if chkFontsEstensions.Checked then begin
    SETTINGS[SETTINGS_EXT_TTF] := '1';
  end else begin
    SETTINGS[SETTINGS_EXT_TTF] := '0';
  end;

  self.Close;
end;

procedure TFrmSettings.chkvgaClick(Sender: TObject);
begin
  UpdateResCombo();
end;

procedure TFrmSettings.cmbOrientationChange(Sender: TObject);
begin
  UpdateResCombo();
end;

procedure TFrmSettings.imgIconClick(Sender: TObject);
begin
  if dlgSelectIcon.Execute = false then exit;
  try
    imgIcon.Picture.LoadFromFile(dlgSelectIcon.FileName);
    txtNewIcon.Caption := dlgSelectIcon.FileName;
  except
    txtNewIcon.Caption := '';
  end;

end;

end.
