unit uAtari2600_z26_Settings;

interface

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

type
  TFormAtari2600_z26_Settings = class(TForm)
    PageControlEmulatorOptionsAtari2600: TPageControl;
    TabSheet7: TTabSheet;
    LabelColorPalette: TLabel;
    LabelDSPProcessingLevel: TLabel;
    LabelPhosphorecent: TLabel;
    LabelOverrideGameType: TLabel;
    LabelScanlines: TLabel;
    LabelNarrowVideoModes: TLabel;
    RunInterlacedGames: TCheckBox;
    Player1HardMode: TCheckBox;
    Player2HardMode: TCheckBox;
    PressAllDirectionsSameTime: TCheckBox;
    BlackWhiteMode: TCheckBox;
    Phosphorecent: TScrollBar;
    OverrideGameType: TComboBox;
    Scanlines: TScrollBar;
    TabSheet10: TTabSheet;
    LabelEmulatePaddleMouse: TLabel;
    LabelPaddleKeyboardSensivity: TLabel;
    EmulatePaddleMouse: TScrollBar;
    PaddleKeyboardSensivity: TScrollBar;
    LabelEmulate2PaddlesMouse: TGroupBox;
    LabelEmulate2PaddlesMouseX: TLabel;
    LabelEmulate2PaddlesMouseY: TLabel;
    Emulate2PaddlesMouseX: TScrollBar;
    Emulate2PaddlesMouseY: TScrollBar;
    Emulate2PaddlesMouse: TCheckBox;
    MouseFullControl: TCheckBox;
    LabelEmulateLightgun: TGroupBox;
    LabelLightGunAdjustHorizontal: TLabel;
    LabelLightGunAdjustVertical: TLabel;
    EmulateLightgun: TCheckBox;
    LightGunAdjustVertical: TScrollBar;
    TabSheet19: TTabSheet;
    LabelSoundDelay: TLabel;
    LabelSyncMonitor: TLabel;
    NoSound: TCheckBox;
    SoundDelay: TScrollBar;
    SyncMonitor: TScrollBar;
    WriteLOG: TCheckBox;
    PrintCartridgeChecksum: TCheckBox;
    ButtonSaveAtari2600: TSpeedButton;
    ButtonDeleteAtari2600: TSpeedButton;
    SynchronizeGameToSoundCard: TCheckBox;
    ShowScanlineFPS: TCheckBox;
    SimulatePALColorLoss: TCheckBox;
    SaveBMPScreenshots: TCheckBox;
    FullScreen: TCheckBox;
    DisableFastCopyRoutines: TCheckBox;
    LabelVideoModes: TLabel;
    VideoModes: TComboBox;
    UpperScanLine: TScrollBar;
    LabelUpperScanLine: TLabel;
    ButtonClose: TSpeedButton;
    LabelMidlinkController: TLabel;
    LightGunAdjustHorizontal: TScrollBar;
    LabelLeftPortController: TLabel;
    LabelRightPortController: TLabel;
    LeftPortController: TComboBox;
    RightPortController: TComboBox;
    ColorPalette: TComboBox;
    DSPProcessingLevel: TComboBox;
    NarrowVideoModes: TComboBox;
    InactivePCController: TComboBox;
    LabelPCJoystickSensivity: TLabel;
    PCJoystickSensivity: TScrollBar;
    LabelPCJoystickSensivityValue: TLabel;
    SwapAtariControllerPorts: TCheckBox;
    procedure PhosphorecentChange(Sender: TObject);
    procedure ScanlinesChange(Sender: TObject);
    procedure PaddleKeyboardSensivityChange(Sender: TObject);
    procedure EmulatePaddleMouseChange(Sender: TObject);
    procedure Emulate2PaddlesMouseClick(Sender: TObject);
    procedure Emulate2PaddlesMouseXChange(Sender: TObject);
    procedure Emulate2PaddlesMouseYChange(Sender: TObject);
    procedure MouseFullControlClick(Sender: TObject);
    procedure LightGunAdjustHorizontalChange(Sender: TObject);
    procedure LightGunAdjustVerticalChange(Sender: TObject);
    procedure EmulateLightgunClick(Sender: TObject);
    procedure SoundDelayChange(Sender: TObject);
    procedure SyncMonitorChange(Sender: TObject);
    procedure UpperScanLineChange(Sender: TObject);
    procedure ButtonDeleteAtari2600Click(Sender: TObject);
    procedure ButtonSaveAtari2600Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonCloseClick(Sender: TObject);
    procedure PCJoystickSensivityChange(Sender: TObject);
  private
    { Private declarations }
    procedure LoadSettingsAtari2600;
    procedure SaveSettingsAtari2600;
    procedure ReadControllerPort(ctrlrValue: String; ctrlrHolder: TComboBox);
    function  WriteControllerPort(ctrlrHolder: TComboBox): String;

  public
    EmulatorFile: String;
    { Public declarations }
  end;

var
  FormAtari2600_z26_Settings: TFormAtari2600_z26_Settings;

implementation

{$R *.dfm}

uses uFilesUtil;

procedure TFormAtari2600_z26_Settings.ReadControllerPort(ctrlrValue: String; ctrlrHolder: TComboBox);
begin
  if ctrlrValue = 'JS' then
     ctrlrHolder.ItemIndex:= 1
  else
  if ctrlrValue = 'PC' then
     ctrlrHolder.ItemIndex:= 2
  else
  if ctrlrValue = 'KP' then
     ctrlrHolder.ItemIndex:= 3
  else
  if ctrlrValue = 'DC' then
     ctrlrHolder.ItemIndex:= 4
  else
  if ctrlrValue = 'LG' then
     ctrlrHolder.ItemIndex:= 5
  else
  if ctrlrValue = 'CM' then
     ctrlrHolder.ItemIndex:= 6
  else
  if ctrlrValue = 'KV' then
     ctrlrHolder.ItemIndex:= 7
  else
  if ctrlrValue = 'ML' then
     ctrlrHolder.ItemIndex:= 8
  else
  if ctrlrValue = 'ST' then
     ctrlrHolder.ItemIndex:= 9
  else
  if ctrlrValue = 'TB' then
     ctrlrHolder.ItemIndex:= 10
  else
  if ctrlrValue = 'AM' then
     ctrlrHolder.ItemIndex:= 11
  else
  if ctrlrValue = 'NC' then
     ctrlrHolder.ItemIndex:= 12
  else
     ctrlrHolder.ItemIndex:= 0;
end;

function TFormAtari2600_z26_Settings.WriteControllerPort(ctrlrHolder: TComboBox): String;
begin
  case ctrlrHolder.ItemIndex of
    1: Result:= 'JS';
    2: Result:= 'PC';
    3: Result:= 'KP';
    4: Result:= 'DC';
    5: Result:= 'LG';
    6: Result:= 'CM';
    7: Result:= 'KV';
    8: Result:= 'ML';
    9: Result:= 'ST';
   10: Result:= 'TB';
   11: Result:= 'AM';
   12: Result:= 'NC';
  end;
end;

procedure TFormAtari2600_z26_Settings.LoadSettingsAtari2600;
var
  SettingFile: THashedStringList;
  SettingString, OptionString, ValueStr: String;
  Position, Value: Integer;
begin
  if not FileExists(ExtractFilePath(EmulatorFile)+'z26.cli') then
     Exit;

  if GetFileSize(ExtractFilePath(EmulatorFile)+'z26.cli') = 0 then
     Exit;

  SettingFile:= THashedStringList.Create;
  SettingFile.LoadFromFile(ExtractFilePath(EmulatorFile)+'z26.cli');
  SettingString:= SettingFile[0];
  FreeAndNil(SettingFile);
  if SettingString = '' then
     Exit;

  Position:= 1;
  OptionString:= '';
  repeat
    if SettingString[Position] = '-' then
       begin
         OptionString:= '';
         repeat
           Inc(Position);
           if SettingString[Position] <> ' ' then
              OptionString:= OptionString+SettingString[Position];
         until
           (SettingString[Position] = ' ') or (Position >= Length(SettingString));

         if OptionString[1] = ')' then
            begin
              ValueStr:= Copy(OptionString, 2, Length(OptionString));
              ReadControllerPort(ValueStr, LeftPortController);
            end
         else
         if OptionString[1] = '(' then
            begin
              ValueStr:= Copy(OptionString, 2, Length(OptionString));
              ReadControllerPort(ValueStr, RightPortController);
            end
         else
         if OptionString[1] = '!' then
            RunInterlacedGames.Checked:= True
         else
         if OptionString[1] = '0' then
            Player1HardMode.Checked:= True
         else
         if OptionString[1] = '1' then
            Player2HardMode.Checked:= True
         else
         if OptionString[1] = '4' then
            PressAllDirectionsSameTime.Checked:= True
         else
         if OptionString[1] = 'b' then
            BlackWhiteMode.Checked:= True
         else
         if OptionString[1] = 'B' then
            SaveBMPScreenshots.Checked:= True
         else
         if OptionString[1] = 'c' then
            ColorPalette.ItemIndex:= (StrToInt(Copy(OptionString, 2, Length(OptionString)))+1)
         else
         if OptionString[1] = 'd' then
            DSPProcessingLevel.ItemIndex:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'e' then
            NarrowVideoModes.ItemIndex:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'f' then
            Phosphorecent.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'g' then
            OverrideGameType.ItemIndex:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'h' then
            Scanlines.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'i' then
            begin
              ValueStr:= Copy(OptionString, 2, Length(OptionString));
              if ValueStr = 'K' then
                 InactivePCController.ItemIndex:= 1
              else
              if ValueStr = 'M' then
                 InactivePCController.ItemIndex:= 2
              else
              if ValueStr = 'J' then
                 InactivePCController.ItemIndex:= 3
              else
              if ValueStr = 'S' then
                 InactivePCController.ItemIndex:= 4;
            end
         else
         if OptionString[1] = 'p' then
            PaddleKeyboardSensivity.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'P' then
            PCJoystickSensivity.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'm' then
            begin
              Value:= StrToInt(Copy(OptionString, 2, Length(OptionString)));
              if Value < 100 then
                 EmulatePaddleMouse.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
              else
                 begin
                   Emulate2PaddlesMouse.Checked:= True;
                   Emulate2PaddlesMouseX.Position:= StrToInt(OptionString[4]);
                   Emulate2PaddlesMouseY.Position:= StrToInt(OptionString[5]);
                 end;
            end
         else
         if OptionString[1] = 'M' then
            begin
             if OptionString[2] = '0' then
                MouseFullControl.State:= cbGrayed
             else
             if OptionString[2] = '1' then
                MouseFullControl.State:= cbChecked;
            end
         else
         if OptionString[1] = 'l' then
            begin
              EmulateLightGun.Checked:= True;
              if Length(OptionString) > 1 then
                 LightGunAdjustHorizontal.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
            end
         else
         if OptionString[1] = 'a' then
            begin
              EmulateLightGun.Checked:= True;
              LightGunAdjustVertical.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)));
            end
         else
         if OptionString[1] = 'n' then
            ShowScanlineFPS.Checked:= True
         else
         if OptionString[1] = 'o' then
            SimulatePALColorLoss.Checked:= True
         else
         if OptionString[1] = 'q' then
            NoSound.Checked:= True
         else
         if OptionString[1] = 's' then
            SoundDelay.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'r' then
            begin
              if Length(OptionString) > 1 then
                 SyncMonitor.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
              else
                 SyncMonitor.Position:= -1;
            end
         else
         if OptionString[1] = 'R' then
            SynchronizeGameToSoundCard.Checked:= True
         else
         if OptionString[1] = 't' then
            WriteLOG.Checked:= True
         else
         if OptionString[1] = 'u' then
            UpperScanLine.Position:= StrToInt(Copy(OptionString, 2, Length(OptionString)))
         else
         if OptionString[1] = 'v' then
            begin
              Value:= StrToInt(Copy(OptionString, 2, Length(OptionString)));
              FullScreen.Checked:= (Value < 10);
              case FullScreen.Checked of
                True : VideoModes.ItemIndex:= StrToInt(Copy(OptionString, 2, Length(OptionString)));
                False: VideoModes.ItemIndex:= StrToInt(Copy(OptionString, 3, Length(OptionString)));
              end;
            end
         else
         if OptionString[1] = 'w' then
            SwapAtariControllerPorts.Checked:= True
         else
         if OptionString[1] = 'x' then
            PrintCartridgeChecksum.Checked:= True
         else
         if OptionString[1] = 'z' then
            DisableFastCopyRoutines.Checked:= True;
       end;
    Inc(Position);
  until
    Position >= Length(SettingString);
end;

procedure TFormAtari2600_z26_Settings.SaveSettingsAtari2600;
var
  SettingFile: THashedStringList;
  SettingString: String;

  procedure AddOption(OptionString: String);
  begin
    if SettingString <> '' then
       SettingString:= SettingString+' ';
    SettingString:= SettingString+OptionString;
  end;

begin
  if FileExists(ExtractFilePath(EmulatorFile)+'z26.cli') then
     DeleteFile(ExtractFilePath(EmulatorFile)+'z26.cli');

  if LeftPortController.ItemIndex > 0 then
     AddOption('-)'+WriteControllerPort(LeftPortController));

  if RightPortController.ItemIndex > 0 then
     AddOption('-('+WriteControllerPort(RightPortController));
     
  if RunInterlacedGames.Checked then
     AddOption('-!');

  if Player1HardMode.Checked then
     AddOption('-0');

  if Player2HardMode.Checked then
     AddOption('-1');

  if PressAllDirectionsSameTime.Checked then
     AddOption('-4');

  if BlackWhiteMode.Checked then
     AddOption('-b');

  if SaveBMPScreenshots.Checked then
     AddOption('-B');

  if ColorPalette.ItemIndex > 0 then
     AddOption('-c'+IntToStr(ColorPalette.ItemIndex-1));

  if DSPProcessingLevel.ItemIndex > 0 then
     AddOption('-d'+IntToStr(DSPProcessingLevel.ItemIndex));

  if NarrowVideoModes.ItemIndex > 0 then
     AddOption('-e'+IntToStr(NarrowVideoModes.ItemIndex));

  if Phosphorecent.Position > -1 then
     AddOption('-f'+IntToStr(Phosphorecent.Position));

  if OverrideGameType.ItemIndex > 0 then
     AddOption('-g'+IntToStr(OverrideGameType.ItemIndex));

  if Scanlines.Position > -1 then
     AddOption('-h'+IntToStr(Scanlines.Position));

  if InactivePCController.ItemIndex > 0 then
     AddOption('-i'+InactivePCController.Text[1]);

  if PaddleKeyboardSensivity.Position > 0 then
     AddOption('-p'+IntToStr(PaddleKeyboardSensivity.Position));

  if PCJoystickSensivity.Position > 0 then
     AddOption('-P'+IntToStr(PCJoystickSensivity.Position));

  if EmulatePaddleMouse.Position > 0 then
     AddOption('-m'+IntToStr(EmulatePaddleMouse.Position));

  if Emulate2PaddlesMouse.Checked then
     AddOption(Format('-m1%d%d', [Emulate2PaddlesMouseX.Position, Emulate2PaddlesMouseY.Position]));

  if MouseFullControl.State <> cbUnchecked then
     AddOption('-M'+ IntToStr(Ord(MouseFullControl.State = cbChecked)));

  if EmulateLightGun.Checked then
     begin
       if LightGunAdjustHorizontal.Position = -1 then
          AddOption('-l')
       else
          AddOption('-l'+IntToStr(LightGunAdjustHorizontal.Position));

       if LightGunAdjustVertical.Position > -1 then
          AddOption('-a'+IntToStr(LightGunAdjustVertical.Position));
     end;

  if ShowScanlineFPS.Checked then
     AddOption('-n');

  if SimulatePALColorLoss.Checked then
     AddOption ('-o');

  if NoSound.Checked then
     AddOption('-q');

  if SoundDelay.Position > -1 then
     AddOption('-s'+IntToStr(SoundDelay.Position));

  if SyncMonitor.Position > -2 then
     begin
       if SyncMonitor.Position = -1 then
          AddOption('-r')
       else
          AddOption('-r'+IntToStr(SyncMonitor.Position));
     end;

  if SynchronizeGameToSoundCard.Checked then
     AddOption('-R');

  if WriteLOG.Checked then
     AddOption('-t');

  if UpperScanLine.Position > -1 then
     AddOption('-u'+IntToStr(UpperScanLine.Position));

  if VideoModes.ItemIndex < 9 then
     begin
       case FullScreen.Checked of
         True : AddOption('-v'+IntToStr(VideoModes.ItemIndex));
         False: AddOption('-v1'+IntToStr(VideoModes.ItemIndex));
       end;
     end;

  if SwapAtariControllerPorts.Checked then
     AddOption('-w');

  if PrintCartridgeChecksum.Checked then
     AddOption('-x');

  if DisableFastCopyRoutines.Checked then
     AddOption('-z');

  if SettingString <> '' then
     begin
       SettingFile:= THashedStringList.Create;
       SettingFile.Add(SettingString);
       SettingFile.SaveToFile(ExtractFilePath(EmulatorFile)+'z26.cli');
       FreeAndNil(SettingFile);
     end;
end;

procedure TFormAtari2600_z26_Settings.PhosphorecentChange(Sender: TObject);
begin
  if Phosphorecent.Position = -1 then
     LabelPhosphorecent.Caption:= 'Phosphorecent (off)'
  else
     LabelPhosphorecent.Caption:= Format('Phosphorecent (%d)', [Phosphorecent.Position]);

end;

procedure TFormAtari2600_z26_Settings.ScanlinesChange(Sender: TObject);
begin
  if Scanlines.Position = -1 then
     LabelScanlines.Caption:= 'Scanlines (off)'
  else
     LabelScanlines.Caption:= Format('Scanlines (%d)', [Scanlines.Position]);
end;

procedure TFormAtari2600_z26_Settings.PaddleKeyboardSensivityChange(Sender: TObject);
begin
  if PaddleKeyboardSensivity.Position = 0 then
     LabelPaddleKeyboardSensivity.Caption:= 'Paddle Sensivity (off)'
  else
     LabelPaddleKeyboardSensivity.Caption:= Format('Paddle Sensivity (%d)', [PaddleKeyboardSensivity.Position]);
end;

procedure TFormAtari2600_z26_Settings.EmulatePaddleMouseChange(Sender: TObject);
begin
  if EmulatePaddleMouse.Position = -1 then
     LabelEmulatePaddleMouse.Caption:= 'Emulate Paddle Mouse (off)'
  else
     LabelEmulatePaddleMouse.Caption:= Format('Emulate Paddle Mouse (%d)', [EmulatePaddleMouse.Position]);
end;

procedure TFormAtari2600_z26_Settings.Emulate2PaddlesMouseClick(Sender: TObject);
begin
  Emulate2PaddlesMouseX.Enabled:= Emulate2PaddlesMouse.Checked;
  Emulate2PaddlesMouseY.Enabled:= Emulate2PaddlesMouse.Checked;
end;

procedure TFormAtari2600_z26_Settings.Emulate2PaddlesMouseXChange(Sender: TObject);
begin
  LabelEmulate2PaddlesMouseX.Caption:= Format('X Coordinate (%d)', [Emulate2PaddlesMouseX.Position]);
end;

procedure TFormAtari2600_z26_Settings.Emulate2PaddlesMouseYChange(Sender: TObject);
begin
  LabelEmulate2PaddlesMouseY.Caption:= Format('Y Coordinate (%d)', [Emulate2PaddlesMouseY.Position]);
end;

procedure TFormAtari2600_z26_Settings.MouseFullControlClick(Sender: TObject);
begin
  case MouseFullControl.State of
    cbUnchecked: MouseFullControl.Caption:= 'Mouse Full Control (disabled)';
    cbGrayed: MouseFullControl.Caption:= 'Mouse Full Control (off)';
    cbChecked: MouseFullControl.Caption:= 'Mouse Full Control (on)';
  end;
end;

procedure TFormAtari2600_z26_Settings.LightGunAdjustHorizontalChange(Sender: TObject);
begin
  if LightGunAdjustHorizontal.Position = -1 then
     LabelLightGunAdjustHorizontal.Caption:= 'Adjust Horizontally (off)'
  else
     LabelLightGunAdjustHorizontal.Caption:= Format('Adjust Horizontally (%d)', [LightGunAdjustHorizontal.Position]);
end;

procedure TFormAtari2600_z26_Settings.LightGunAdjustVerticalChange(Sender: TObject);
begin
  if LightGunAdjustVertical.Position = -1 then
     LabelLightGunAdjustVertical.Caption:= 'Adjust Vertically (off)'
  else
     LabelLightGunAdjustVertical.Caption:= Format('Adjust Vertically (%d)', [LightGunAdjustVertical.Position]);
end;

procedure TFormAtari2600_z26_Settings.EmulateLightgunClick(Sender: TObject);
begin
  LightGunAdjustHorizontal.Enabled:= EmulateLightGun.Checked;
  LightGunAdjustVertical.Enabled:= EmulateLightGun.Checked;
end;

procedure TFormAtari2600_z26_Settings.SoundDelayChange(Sender: TObject);
begin
  if SoundDelay.Position = -1 then
     LabelSoundDelay.Caption:= 'Sound Delay (off)'
  else
     LabelSoundDelay.Caption:= Format('Sound Delay (%d)', [SoundDelay.Position]);
end;

procedure TFormAtari2600_z26_Settings.SyncMonitorChange(Sender: TObject);
begin
  case SyncMonitor.Position of
   -2: LabelSyncMonitor.Caption:= 'Sync (off)';
   -1: LabelSyncMonitor.Caption:= 'Sync (Monitor Speed)';
  else
       LabelSyncMonitor.Caption:= Format('Sync (%d FPS)', [SyncMonitor.Position]);
  end;
end;

procedure TFormAtari2600_z26_Settings.UpperScanLineChange(Sender: TObject);
begin
  if UpperScanLine.Position = -1 then
     LabelUpperScanLine.Caption:= 'Upper Scan Line (off)'
  else
     LabelUpperScanLine.Caption:= Format('Upper Scan Line (%d)', [UpperScanLine.Position]);
end;

procedure TFormAtari2600_z26_Settings.ButtonDeleteAtari2600Click(
  Sender: TObject);
begin
  if GenerateMessage('Warning', 'This will delete file "z26.cli". Are you sure ?', 1) = mrYes then
     begin
       case DeleteFile(ExtractFilePath(EmulatorFile)+'z26.cli') of
         True : GenerateMessage('Delete File', 'File "z26.cli" successfully deleted!', 2);
         False: GenerateMessage('Error', 'Could not delete file "z26.cli"!', 2);
       end;
     end;
end;

procedure TFormAtari2600_z26_Settings.ButtonSaveAtari2600Click(
  Sender: TObject);
begin
  SaveSettingsAtari2600;
end;

procedure TFormAtari2600_z26_Settings.FormShow(Sender: TObject);
begin
  LoadSettingsAtari2600;
end;

procedure TFormAtari2600_z26_Settings.ButtonCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TFormAtari2600_z26_Settings.PCJoystickSensivityChange(Sender: TObject);
begin
  if PCJoystickSensivity.Position = 0 then
     LabelPCJoystickSensivityValue.Caption:= '(none)'
  else
     LabelPCJoystickSensivityValue.Caption:= IntToStr(PCJoystickSensivity.Position);
end;

end.
