unit uZiNcSettings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, GR32_RangeBars, ComCtrls,
  ExtCtrls, IniFiles, ToolWin;

type
  TFormZiNcSettings = class(TForm)
    PageControlZiNcSettings: TPageControl;
    TabSheetGeneral: TTabSheet;
    TabSheetOpenGL_Direct3DRenderer: TTabSheet;
    LabelResolution: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    LabelFramerateManual: TLabel;
    Label4: TLabel;
    LabelTextureCache: TLabel;
    LabelTextureCacheValue: TLabel;
    LabelTurnDisplay: TLabel;
    LabelTurnDisplayValue: TLabel;
    Resolution: TComboBox;
    FullScreen: TCheckBox;
    ColorDepth: TComboBox;
    Scanlines: TComboBox;
    Blending: TComboBox;
    Dithering: TCheckBox;
    ShowFPS: TCheckBox;
    FrameLimitation: TCheckBox;
    FrameSkip: TCheckBox;
    FramerateDetection: TCheckBox;
    TextureType: TComboBox;
    TextureCache: TGaugeBar;
    TurnDisplay: TGaugeBar;
    LabelFolderROMs: TLabel;
    FolderROMs: TEdit;
    FolderROMsButtonSelect: TButton;
    LabelControllerZNC: TLabel;
    ControllerZNC: TEdit;
    ControllerZNCButtonSelect: TButton;
    ControllerConfigFileButtonSelect: TButton;
    ControllerConfigFile: TEdit;
    Label11: TLabel;
    LabelTextureFilter: TLabel;
    TextureFilter: TGaugeBar;
    LabelTextureFilterValue: TLabel;
    TabSheetGlideRenderer: TTabSheet;
    Label5: TLabel;
    Glide_Resolution: TComboBox;
    Label6: TLabel;
    Glide_TextureFilter: TComboBox;
    Glide_ShowFPS: TCheckBox;
    Glide_FrameLimitation: TCheckBox;
    Glide_FrameSkip: TCheckBox;
    Glide_FramerateDetection: TCheckBox;
    LabelGlide_FramerateManual: TLabel;
    TabSheet1: TTabSheet;
    Label12: TLabel;
    LabelSoundLiteSurroundMultiplier: TLabel;
    LabelSoundLiteSurroundMultiplierValue: TLabel;
    Sound: TCheckBox;
    SoundFilter: TCheckBox;
    SoundFilterCutoff: TComboBox;
    SoundLiteSurround: TCheckBox;
    SoundLiteSurroundMultiplier: TGaugeBar;
    SoundStereoExciter: TCheckBox;
    Label10: TLabel;
    LabelNetGameConnect: TLabel;
    Rotate: TComboBox;
    SlowGeometry: TCheckBox;
    NetGameServer: TCheckBox;
    NetGameConnect: TEdit;
    MemoryPrediction: TCheckBox;
    StackInRAMHack: TCheckBox;
    LabelRendererZNC: TLabel;
    RendererZNCButtonSelect: TButton;
    Label8: TLabel;
    RendererConfigFileButtonSelect: TButton;
    RendererZNC: TEdit;
    RendererConfigFile: TEdit;
    GroupBoxKeyCodes: TGroupBox;
    Label14: TLabel;
    Glide_Key_TextureFilter: TEdit;
    ButtonGlide_Key_TextureFilter: TButton;
    Label15: TLabel;
    Glide_Key_ShowFPS: TEdit;
    ButtonGlide_Key_ShowFPS: TButton;
    Label16: TLabel;
    Glide_Key_FrameLimitation: TEdit;
    ButtonGlide_Key_FrameLimitation: TButton;
    Label17: TLabel;
    Glide_Key_FrameSkip: TEdit;
    ButtonGlide_Key_FrameSkip: TButton;
    Label18: TLabel;
    Glide_Key_FramerateDetection: TEdit;
    ButtonGlide_Key_FramerateDetection: TButton;
    LabelGlide_Key_TextureFilter: TLabel;
    LabelGlide_Key_ShowFPS: TLabel;
    LabelGlide_Key_FrameLimitation: TLabel;
    LabelGlide_Key_FrameSkip: TLabel;
    LabelGlide_Key_FramerateDetection: TLabel;
    ReadZINcCFG: TButton;
    ButtonOk: TButton;
    ButtonCancel: TButton;
    Glide_FramerateManual: TGaugeBar;
    LabelGlide_FramerateManualValue: TLabel;
    FramerateManual: TGaugeBar;
    LabelFramerateManualValue: TLabel;
    procedure ButtonCancelClick(Sender: TObject);
    procedure ReadZINcCFGClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SoundLiteSurroundMultiplierChange(Sender: TObject);
    procedure TextureCacheChange(Sender: TObject);
    procedure TurnDisplayChange(Sender: TObject);
    procedure TextureFilterChange(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure ButtonGlide_Key_ShowFPSClick(Sender: TObject);
    procedure Glide_Key_TextureFilterExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Glide_Key_TextureFilterEnter(Sender: TObject);
    procedure Glide_Key_TextureFilterKeyPress(Sender: TObject;
      var Key: Char);
    procedure Glide_Key_TextureFilterKeyDown(Sender: TObject;
      var Key: Word; Shift: TShiftState);
    procedure ButtonGlide_Key_TextureFilterClick(Sender: TObject);
    procedure ButtonGlide_Key_FrameLimitationClick(Sender: TObject);
    procedure ButtonGlide_Key_FrameSkipClick(Sender: TObject);
    procedure ButtonGlide_Key_FramerateDetectionClick(Sender: TObject);
    procedure RendererZNCChange(Sender: TObject);
    procedure FolderROMsButtonSelectClick(Sender: TObject);
    procedure ControllerZNCButtonSelectClick(Sender: TObject);
    procedure ControllerConfigFileButtonSelectClick(Sender: TObject);
    procedure RendererZNCButtonSelectClick(Sender: TObject);
    procedure RendererConfigFileButtonSelectClick(Sender: TObject);
    procedure Glide_FramerateManualChange(Sender: TObject);
    procedure FramerateManualChange(Sender: TObject);
  private
    { Private declarations }
    KeysListGlide: THashedStringList;
  public
    zGameName: String;
    OpenGL_D3DRendererType: Boolean;
    { Public declarations }
  end;

var
  FormZiNcSettings: TFormZiNcSettings;

implementation

uses uMain, uAbout, uFilesUtil;

{$R *.dfm}

procedure TFormZiNcSettings.ButtonCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFormZiNcSettings.ReadZINcCFGClick(Sender: TObject);
begin
  if Tag = 1 then
     FormMain.ReadZiNc_cfg('zinc_el', False);
  FormMain.ReadZiNc_cfg(zGameName, (Tag = 1));
end;

procedure TFormZiNcSettings.FormShow(Sender: TObject);
begin
  if Tag = 1 then
     begin
       if FormMain.GetGameData(FormMain.SelectedNode) then
          FormMain.GetGameIcon(FormMain.IL_StandardIconsLarge, FormMain.IL_StandardIconsStandard, FormMain.IL_StandardIconsSmall, FormMain.GameInfoData.eImageIndex, Icon);
     end;
  ReadZINcCFG.Click;
end;

procedure TFormZiNcSettings.SoundLiteSurroundMultiplierChange(
  Sender: TObject);
begin
  LabelSoundLiteSurroundMultiplierValue.Caption:= IntToStr(SoundLiteSurroundMultiplier.Position);
end;

procedure TFormZiNcSettings.TextureCacheChange(Sender: TObject);
begin
  LabelTextureCacheValue.Caption:= IntToStr(TextureCache.Position);
end;

procedure TFormZiNcSettings.TurnDisplayChange(Sender: TObject);
begin
  LabelTurnDisplayValue.Caption:= IntToStr(TurnDisplay.Position);
end;

procedure TFormZiNcSettings.TextureFilterChange(Sender: TObject);
begin
  LabelTextureFilterValue.Caption:= IntToStr(TextureFilter.Position);
end;

procedure TFormZiNcSettings.ButtonOkClick(Sender: TObject);
begin
  FormMain.UpdateZiNc_cfg(zGameName, False);
  Close;
end;

procedure TFormZiNcSettings.ButtonGlide_Key_ShowFPSClick(Sender: TObject);
begin
  Glide_Key_ShowFPS.Color:= clHighlight;
  Glide_Key_ShowFPS.SetFocus;
end;

procedure TFormZiNcSettings.Glide_Key_TextureFilterExit(Sender: TObject);
begin
  if TEdit(Sender).Color <> clWindow then
     TEdit(Sender).Color:= clWindow;
end;

procedure TFormZiNcSettings.FormCreate(Sender: TObject);
begin
  KeysListGlide:= THashedStringList.Create;
  KeysListGlide.Add('0x08=Back Space');
  KeysListGlide.Add('0x09=Tab');
  KeysListGlide.Add('0x0C=Clear');
  KeysListGlide.Add('0x0D=Return');
  KeysListGlide.Add('0x10=Shift');
  KeysListGlide.Add('0x11=Control');
  KeysListGlide.Add('0x12=Menu');
  KeysListGlide.Add('0x13=Pause');
  KeysListGlide.Add('0x14=Caps Lock');
  KeysListGlide.Add('0x1B=Escape');
  KeysListGlide.Add('0x20=Space Bar');
  KeysListGlide.Add('0x21=Page Up');
  KeysListGlide.Add('0x22=Page Down');
  KeysListGlide.Add('0x23=End');
  KeysListGlide.Add('0x24=Home');
  KeysListGlide.Add('0x25=Left Arrow');
  KeysListGlide.Add('0x26=Up Arrow');
  KeysListGlide.Add('0x27=Right Arrow');
  KeysListGlide.Add('0x28=Down Arrow');
  KeysListGlide.Add('0x29=Select');
  KeysListGlide.Add('0x2A=Print');
  KeysListGlide.Add('0x2B=Execute');
  KeysListGlide.Add('0x2C=Print Screen');
  KeysListGlide.Add('0x2D=Insert');
  KeysListGlide.Add('0x2E=Delete');
  KeysListGlide.Add('0x2F=Help');
  KeysListGlide.Add('0x5B=Left Win');
  KeysListGlide.Add('0x5C=Right Win');
  KeysListGlide.Add('0x5D=Apps Win');
  KeysListGlide.Add('0x5F=Sleep');
  KeysListGlide.Add('0x60=Numeric Pad 0');
  KeysListGlide.Add('0x61=Numeric Pad 1');
  KeysListGlide.Add('0x62=Numeric Pad 2');
  KeysListGlide.Add('0x63=Numeric Pad 3');
  KeysListGlide.Add('0x64=Numeric Pad 4');
  KeysListGlide.Add('0x65=Numeric Pad 5');
  KeysListGlide.Add('0x66=Numeric Pad 6');
  KeysListGlide.Add('0x67=Numeric Pad 7');
  KeysListGlide.Add('0x68=Numeric Pad 8');
  KeysListGlide.Add('0x69=Numeric Pad 9');
  KeysListGlide.Add('0x6A=Numeric Pad Multiply');
  KeysListGlide.Add('0x6B=Numeric Pad Add');
  KeysListGlide.Add('0x6C=Numeric Pad Separator');
  KeysListGlide.Add('0x6D=Numeric Pad Subtract');
  KeysListGlide.Add('0x6E=Numeric Pad Decimal');
  KeysListGlide.Add('0x6F=Numeric Pad Divide');
  KeysListGlide.Add('0x70=F1');
  KeysListGlide.Add('0x71=F2');
  KeysListGlide.Add('0x72=F3');
  KeysListGlide.Add('0x73=F4');
  KeysListGlide.Add('0x74=F5');
  KeysListGlide.Add('0x75=F6');
  KeysListGlide.Add('0x76=F7');
  KeysListGlide.Add('0x77=F8');
  KeysListGlide.Add('0x78=F9');
  KeysListGlide.Add('0x79=F10');
  KeysListGlide.Add('0x7A=F11');
  KeysListGlide.Add('0x7B=F12');
  KeysListGlide.Add('0x90=Num Lock');
  KeysListGlide.Add('0x91=Scroll Lock');
  // Custom Keys
  KeysListGlide.Add('0xC2=Numeric Pad Comma');
end;

procedure TFormZiNcSettings.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  FreeAndNil(KeysListGlide);
end;

procedure TFormZiNcSettings.Glide_Key_TextureFilterEnter(Sender: TObject);
begin
  if TEdit(Sender).Color = clHighlight then
     TEdit(Sender).Text:= 'Press a key';
end;

procedure TFormZiNcSettings.Glide_Key_TextureFilterKeyPress(
  Sender: TObject; var Key: Char);
begin
  Key:= Char(0);
end;

procedure TFormZiNcSettings.Glide_Key_TextureFilterKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var
  Index: Integer;
begin
  if TEdit(Sender).Color = clHighlight then
     begin
       Index:= KeysListGlide.IndexOfName('0x'+(IntToHex(Key, 2)));
       if Index > -1 then
          TEdit(Sender).Text:= KeysListGlide.ValueFromIndex[Index]
       else
          begin
            if (Char(Key) in ['A'..'Z', 'a'..'z', '0'..'9']) then
               TEdit(Sender).Text:= UpperCase(Char(Key))
            else
               TEdit(Sender).Text:= 'Unknown';
          end;
       case TEdit(Sender).Tag of
         0: LabelGlide_Key_TextureFilter.Caption:= '0x'+(IntToHex(Key, 2));
         1: LabelGlide_Key_ShowFPS.Caption:= '0x'+(IntToHex(Key, 2));
         2: LabelGlide_Key_FrameLimitation.Caption:= '0x'+(IntToHex(Key, 2));
         3: LabelGlide_Key_FrameSkip.Caption:= '0x'+(IntToHex(Key, 2));
         4: LabelGlide_Key_FramerateDetection.Caption:= '0x'+(IntToHex(Key, 2));
       end;
       GroupBoxKeyCodes.SetFocus;
     end;
end;

procedure TFormZiNcSettings.ButtonGlide_Key_TextureFilterClick(
  Sender: TObject);
begin
  Glide_Key_TextureFilter.Color:= clHighlight;
  Glide_Key_TextureFilter.SetFocus;
end;

procedure TFormZiNcSettings.ButtonGlide_Key_FrameLimitationClick(Sender: TObject);
begin
  Glide_Key_FrameLimitation.Color:= clHighlight;
  Glide_Key_FrameLimitation.SetFocus;
end;

procedure TFormZiNcSettings.ButtonGlide_Key_FrameSkipClick(Sender: TObject);
begin
  Glide_Key_FrameSkip.Color:= clHighlight;
  Glide_Key_FrameSkip.SetFocus;
end;

procedure TFormZiNcSettings.ButtonGlide_Key_FramerateDetectionClick(Sender: TObject);
begin
  Glide_Key_FramerateDetection.Color:= clHighlight;
  Glide_Key_FramerateDetection.SetFocus;
end;

procedure TFormZiNcSettings.RendererZNCChange(Sender: TObject);
begin
  case FileExists(RendererZNC.Text) of
    True : LabelRendererZNC.Caption:= 'Renderer ['+GetFileInfo(RendererZNC.Text, VersionInfo[2])+']';
    False: LabelRendererZNC.Caption:= 'Renderer';
  end;
  OpenGL_D3DRendererType:= Pos('glide', LowerCase(RendererZNC.Text)) = 0;
end;

procedure TFormZiNcSettings.FolderROMsButtonSelectClick(Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderROMs, False);
end;

procedure TFormZiNcSettings.ControllerZNCButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogOpenFile(9, 'Select a controller plugin', ControllerZNC, False, False);
end;

procedure TFormZiNcSettings.ControllerConfigFileButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogOpenFile(10, 'Select a controller config file', ControllerConfigFile, False, False);
end;

procedure TFormZiNcSettings.RendererZNCButtonSelectClick(Sender: TObject);
begin
  FormMain.DialogOpenFile(9, 'Select a video renderer plugin', RendererZNC, False, False);
end;

procedure TFormZiNcSettings.RendererConfigFileButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogOpenFile(10, 'Select a video renderer config file', RendererConfigFile, False, False);
end;

procedure TFormZiNcSettings.Glide_FramerateManualChange(Sender: TObject);
begin
  LabelGlide_FramerateManualValue.Caption:= IntToStr(Glide_FramerateManual.Position);
end;

procedure TFormZiNcSettings.FramerateManualChange(Sender: TObject);
begin
  LabelFramerateManualValue.Caption:= IntToStr(FramerateManual.Position);
end;

end.
