unit uPlayStationCDGameEditor;

interface

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

type
  TFormPlayStationCDGameEditor = class(TForm)
    PageControlPSX: TPageControl;
    TabSheetPSX: TTabSheet;
    GameFrameBox: TGroupBox;
    LabelNumberCDs: TLabel;
    GameDescription: TLabeledEdit;
    ButtonAdd: TSpeedButton;
    NumberCDs: TjkSpinEdit;
    LabelGamesList: TLabel;
    GamesList: TListView;
    ButtonAddGamesToList: TSpeedButton;
    ButtonCancel: TSpeedButton;
    procedure ButtonCancelClick(Sender: TObject);
    procedure ButtonAddClick(Sender: TObject);
    procedure ButtonAddGamesToListClick(Sender: TObject);
    procedure GameDescriptionKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormPlayStationCDGameEditor: TFormPlayStationCDGameEditor;

implementation

uses uMain, uCommon;

{$R *.dfm}

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

procedure TFormPlayStationCDGameEditor.ButtonAddClick(Sender: TObject);
begin
  if GamesList.FindCaption(0, GameDescription.Text, False, True, False) = nil then
     begin
       with GamesList.Items.Add do
       begin
         Caption:= GameDescription.Text;
         SubItems.Add(FloatToStr(NumberCDs.Value));
       end;
     end
  else
     GenerateMessage('Game Not Added', 'This game is already in the list', 2);
  GameDescription.SetFocus;
  GameDescription.SelectAll;
end;

procedure TFormPlayStationCDGameEditor.ButtonAddGamesToListClick(
  Sender: TObject);
var
  datFile: THashedStringList;
  Loop, Loop2, GamesAdded: Integer;
  AddGame: Boolean;
begin
  if GamesList.Items.Count > 0 then
     begin
       Screen.Cursor:= crHourGlass;
       if FileExists(FormMain.AppPath+'games\playstation.dat') then
          begin
            datFile:= THashedStringList.Create;
            datFile.LoadFromFile(FormMain.AppPath+'games\playstation.dat');
            if datFile.Count = 0 then
               FreeAndNil(datFile);
          end;

       AddGame:= True;
       GamesAdded:= 0;
       for Loop:=0 to GamesList.Items.Count-1 do
       begin
         if Assigned(datFile) then
            begin
              for Loop2:=0 to datFile.Count-1 do
              begin
                if Pos('¬'+GamesList.Items[Loop].Caption+'¬', datFile[Loop2]) <> 0 then
                   begin
                     AddGame:= False;
                     Break;
                   end;
              end;
            end;
         if AddGame then
            begin
              datFile.BeginUpdate;
              datFile.Add('21¬'+GamesList.Items[Loop].Caption+'¬'+GamesList.Items[Loop].SubItems[0]+'¬¬CD;');
              datFile.EndUpdate;
              Inc(GamesAdded);
            end;
       end;
       Screen.Cursor:= crDefault;
       if GamesAdded > 0 then
          begin
            datFile.SaveToFile(FormMain.AppPath+'games\playstation.dat');
            FreeAndNil(datFile);
            FormMain.LoadGames(True);
          end
       else
          FreeAndNil(datFile);
     end;
  Close;
end;

procedure TFormPlayStationCDGameEditor.GameDescriptionKeyPress(
  Sender: TObject; var Key: Char);
begin
  if Key = #13 then
     ButtonAdd.Click;
end;

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

procedure TFormPlayStationCDGameEditor.FormShow(Sender: TObject);
begin
  FormMain.LoadButtonIcon(ButtonAddGamesToList, 6, False);
  FormMain.LoadButtonIcon(ButtonCancel, 7, False);
  FormMain.LoadButtonIcon(ButtonAdd, 17, False);
end;

end.
