unit options;

interface

uses
  SysUtils, Forms, StdCtrls, variables, Classes, Controls;

type
  Toptions_ = class(TForm)
    sens_x: TScrollBar;
    Label4: TLabel;
    sens_y: TScrollBar;
    Label1: TLabel;
    sens_z: TScrollBar;
    Label2: TLabel;
    music: TCheckBox;
    effects: TCheckBox;
    opacity_i: TScrollBar;
    l4: TLabel;
    reset: TLabel;
    ok: TLabel;
    scale_i_x0: TLabel;
    scale_i_x: TScrollBar;
    scale_i_y0: TLabel;
    scale_i_y: TScrollBar;
    l1: TLabel;
    l2: TLabel;
    l3: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    scale_f_x: TScrollBar;
    scale_f_y: TScrollBar;
    font_n: TEdit;
    Label8: TLabel;
    procedure resetClick(Sender: TObject);
    procedure okClick(Sender: TObject);
    procedure opacity_iChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure resetMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure resetMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  options_: Toptions_;

procedure SaveOptions(file_name : string);
procedure LoadOptions(file_name : string);

implementation

uses launcher, main_;

{$R *.DFM}

procedure SaveOptions(file_name : string);
var
f : textfile;
s : string;
begin
assignfile(f, file_name);
rewrite(f);
writeln(f, launcher_.name.text);
writeln(f, launcher_.level.itemindex);
writeln(f, launcher_.resolution.itemindex);
s := '0'; if filtered_textures then s := '1'; writeln(f, s);

writeln(f, sens_hor : 6 : 3);
writeln(f, sens_ver : 6 : 3);
writeln(f, sens_zoom_height : 6 : 3);
s := '0'; if sound_music then s := '1'; writeln(f, s);
s := '0'; if sound_effects then s := '1'; writeln(f, s);
writeln(f, interface_opacity : 6 : 3);
writeln(f, interface_scale_x : 6 : 3);
writeln(f, interface_scale_y : 6 : 3);
writeln(f, font_name);
writeln(f, font_scale_x);
writeln(f, font_scale_y);
closefile(f);
end;

procedure LoadOptions(file_name : string);
var
f : textfile;
s : single;
i : integer;
n : string;
begin
if not fileexists(file_name) then exit;
assignfile(f, file_name);
reset(f);
try
readln(f, n); launcher_.name.text := n;
readln(f, i); launcher_.level.itemindex := i;
readln(f, i); launcher_.resolution.itemindex := i;
filtered_textures := false;
readln(f, s); if s = 1 then filtered_textures := true;
launcher_.filter.checked := filtered_textures;

readln(f, sens_hor);
readln(f, sens_ver);
readln(f, sens_zoom_height);
sound_music := false; sound_effects := false;
readln(f, s); if s = 1 then sound_music := true;
readln(f, s); if s = 1 then sound_effects := true;
readln(f, interface_opacity);
readln(f, interface_scale_x);
readln(f, interface_scale_y);
readln(f, font_name);
readln(f, font_scale_x);
readln(f, font_scale_y);  
except SaveOptions(file_name);
end;
closefile(f);
end;

procedure Toptions_.resetClick(Sender: TObject);
begin
sens_x.position := 9;
sens_y.position := 9;
sens_z.position := 15;
music.checked := true;
effects.checked := true;
opacity_i.position := 100;
scale_i_x.position := 100;
scale_i_y.position := 100;
font_n.text := 'Impact';
scale_f_x.position := 20;
scale_f_y.position := 7;
end;

procedure Toptions_.okClick(Sender: TObject);
begin
sens_hor := sens_x.position / 100;
sens_ver := sens_y.position / 100;
sens_zoom_height := sens_z.position;
sound_music := music.checked;
sound_effects := effects.checked;
interface_opacity := opacity_i.position / 100;
interface_scale_x := scale_i_x.position / 100;
interface_scale_y := scale_i_y.position / 100;
font_name := font_n.text;
font_scale_x := scale_f_x.position;
font_scale_y := scale_f_y.position;
SaveOptions('options.cfg');
close;
end;

procedure Toptions_.opacity_iChange(Sender: TObject);
begin
l1.caption := inttostr(opacity_i.position) + '%';
l2.caption := inttostr(scale_i_x.position) + '%';
l3.caption := inttostr(scale_i_y.position) + '%';
end;

procedure Toptions_.FormShow(Sender: TObject);
begin
TForm(sender).OnMouseMove(nil, [ssLeft], 1, 1);
LoadOptions('options.cfg');
sens_x.position := round(sens_hor * 100);
sens_y.position := round(sens_ver * 100);
sens_z.position := round(sens_zoom_height);
music.checked := sound_music;
effects.checked := sound_effects;
opacity_i.position := round(interface_opacity * 100);
scale_i_x.position := round(interface_scale_x * 100);
scale_i_y.position := round(interface_scale_y * 100);
font_n.text := font_name;
scale_f_x.position := font_scale_x;
scale_f_y.position := font_scale_y;
opacity_i.OnChange(nil);
end;

procedure Toptions_.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
reset.font.color := launcher_.font.color;
ok.font.color := launcher_.font.color;
end;

procedure Toptions_.resetMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
LabelMouseMove(TLabel(sender), self);
end;

procedure Toptions_.FormCreate(Sender: TObject);
begin
doublebuffered := true;
end;

procedure Toptions_.FormHide(Sender: TObject);
begin
launcher_.show;
end;

procedure Toptions_.resetMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
LabelMouseDown(TLabel(sender), self);
end;

end.
