unit uCustomGamesEditor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, GR32_Image, ComCtrls, VirtualTrees,
  ImgList, ExtCtrls, IniFiles, GR32_RangeBars;

type
  TFormCustomGamesEditor = class(TForm)
    TopImage: TImage32;
    LabelDescription: TLabel;
    ButtonClose: TButton;
    PageControlMain: TPageControl;
    TabSheetEditor: TTabSheet;
    TabSheet5: TTabSheet;
    CustomGamesList: TVirtualStringTree;
    PageControlEditor: TPageControl;
    TabSheetEditorPage1: TTabSheet;
    TabSheetEditorPage2: TTabSheet;
    Label1: TLabel;
    ROM_ID: TComboBoxEx;
    Label3: TLabel;
    MediaType: TComboBoxEx;
    Label2: TLabel;
    Description: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Manufacturer: TEdit;
    Label4: TLabel;
    AudioChannels: TComboBoxEx;
    Label8: TLabel;
    AudioSampleName: TEdit;
    LabelNumberScreens: TLabel;
    PageControlScreens: TPageControl;
    TabSheet1: TTabSheet;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Screen0Type: TComboBoxEx;
    Screen0ResolutionWidth: TEdit;
    Screen0ResolutionHeight: TEdit;
    Screen0RefreshRate: TEdit;
    Screen0Orientation: TComboBoxEx;
    TabSheet2: TTabSheet;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Screen1Type: TComboBoxEx;
    Screen1ResolutionWidth: TEdit;
    Screen1ResolutionHeight: TEdit;
    Screen1RefreshRate: TEdit;
    Screen1Orientation: TComboBoxEx;
    TabSheet3: TTabSheet;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Screen2Type: TComboBoxEx;
    Screen2ResolutionWidth: TEdit;
    Screen2ResolutionHeight: TEdit;
    Screen2RefreshRate: TEdit;
    Screen2Orientation: TComboBoxEx;
    TabSheet4: TTabSheet;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Screen3Type: TComboBoxEx;
    Screen3ResolutionWidth: TEdit;
    Screen3ResolutionHeight: TEdit;
    Screen3RefreshRate: TEdit;
    Screen3Orientation: TComboBoxEx;
    IL_CustomGames: TImageList;
    ControlTypesBox: TGroupBox;
    ControlType: TComboBoxEx;
    ButtonAddControl: TButton;
    ControlTypeList: TListView;
    ButtonDeleteControls: TButton;
    ButtonClearControls: TButton;
    Label9: TLabel;
    GameName: TEdit;
    Label10: TLabel;
    CloneOf: TEdit;
    Label11: TLabel;
    DriverName: TEdit;
    BiosName: TEdit;
    Label12: TLabel;
    Label34: TLabel;
    DriverStatus: TComboBoxEx;
    EmulationStatus: TComboBoxEx;
    Label35: TLabel;
    ColorStatus: TComboBoxEx;
    Label36: TLabel;
    ProtectionStatus: TComboBoxEx;
    Label40: TLabel;
    GraphicsStatus: TComboBoxEx;
    Label38: TLabel;
    SoundStatus: TComboBoxEx;
    Label37: TLabel;
    CocktailStatus: TComboBoxEx;
    Label39: TLabel;
    SaveState: TComboBoxEx;
    Label33: TLabel;
    GameSize: TEdit;
    Label41: TLabel;
    ButtonCancel: TButton;
    NumberPlayers: TEdit;
    Label42: TLabel;
    ButtonNew: TButton;
    ButtonClear: TButton;
    ButtonOk: TButton;
    ButtonEdit: TButton;
    LabelTotalGames: TLabel;
    ButtonDelete: TButton;
    ButtonNew2: TButton;
    ButtonCommandLine: TButton;
    CommandLineType: TRadioGroup;
    ButtonCommandLine2: TButton;
    ButtonEdit2: TButton;
    ButtonReload: TButton;
    Label43: TLabel;
    AudioSamples: TComboBoxEx;
    Year: TEdit;
    NumberScreens: TGaugeBar;
    LabelNumberScreensValue: TLabel;
    procedure ButtonCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonAddControlClick(Sender: TObject);
    procedure ButtonClearControlsClick(Sender: TObject);
    procedure ButtonDeleteControlsClick(Sender: TObject);
    procedure ButtonNew2Click(Sender: TObject);
    procedure ButtonCommandLine2Click(Sender: TObject);
    procedure ButtonEdit2Click(Sender: TObject);
    procedure ButtonReloadClick(Sender: TObject);
    procedure CustomGamesListCompareNodes(Sender: TBaseVirtualTree; Node1,
      Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
    procedure CustomGamesListFreeNode(Sender: TBaseVirtualTree;
      Node: PVirtualNode);
    procedure CustomGamesListGetHint(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex;
      var LineBreakStyle: TVTTooltipLineBreakStyle;
      var HintText: WideString);
    procedure CustomGamesListGetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    procedure CustomGamesListGetImageIndex(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
      var Ghosted: Boolean; var ImageIndex: Integer);
    procedure CustomGamesListHeaderClick(Sender: TVTHeader;
      Column: TColumnIndex; Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    procedure CustomGamesListIncrementalSearch(Sender: TBaseVirtualTree;
      Node: PVirtualNode; const SearchText: WideString;
      var Result: Integer);
    procedure PageControlMainChange(Sender: TObject);
    procedure CustomGamesListChange(Sender: TBaseVirtualTree;
      Node: PVirtualNode);
    procedure ButtonEditClick(Sender: TObject);
    procedure ButtonNewClick(Sender: TObject);
    procedure ButtonDeleteClick(Sender: TObject);
    procedure ButtonCancelClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure NumberScreensChange(Sender: TObject);
  private
    { Private declarations }
    CurrentNode: PVirtualNode;
    procedure PopulateComboBoxEx(ArrayVariable: array of String; ComboBoxExHolder: TComboBoxEx);
    procedure PopulateControlsComboBox(ComboBoxExHolder: TComboBoxEx);
    procedure PopulateOrientationList(ComboBoxExHolder: TComboBoxEx);
    procedure SetOrientationData(ComboBoxExHolder: TComboBoxEx; Rotation: Integer);
    function  AddCustomGames: Boolean;
    procedure LoadGameData;
    procedure UpdateTotalGames;

  public
    { Public declarations }
  end;

var
  FormCustomGamesEditor: TFormCustomGamesEditor;

implementation

uses uMain, uCommon;

{$R *.dfm}

procedure TFormCustomGamesEditor.UpdateTotalGames;
begin
  LabelTotalGames.Caption:= Format('Total Games: %.6u', [CustomGamesList.VisibleCount]);
end;

procedure TFormCustomGamesEditor.PopulateControlsComboBox(ComboBoxExHolder: TComboBoxEx);
var
  Loop: Integer;
begin
  if ComboBoxExHolder.Items.Count > 0 then
     ComboBoxExHolder.Clear;
  for Loop:=Low(aControlType) to High(aControlType) do
  begin
    with ComboBoxExHolder.ItemsEx.Add do
    begin
      ImageIndex:= Loop;
      SelectedImageIndex:= Loop;
      if aControlType[Loop, 0] <> '' then
         Caption:= aControlType[Loop, 0]
      else
         Caption:= 'None'; //Unknown';
    end;
  end;
end;

procedure TFormCustomGamesEditor.PopulateComboBoxEx(ArrayVariable: array of String; ComboBoxExHolder: TComboBoxEx);
var
  Loop: Integer;
begin
  if ComboBoxExHolder.Items.Count > 0 then
     ComboBoxExHolder.Clear;
  for Loop:=Low(ArrayVariable) to High(ArrayVariable) do
  begin
    with ComboBoxExHolder.ItemsEx.Add do
    begin
      ImageIndex:= Loop;
      SelectedImageIndex:= Loop;
      if ArrayVariable[Loop] <> '' then
         Caption:= ArrayVariable[Loop]
      else
         Caption:= 'None'; //Unknown';
    end;
  end;
end;

procedure TFormCustomGamesEditor.PopulateOrientationList(ComboBoxExHolder: TComboBoxEx);

  procedure AddItem(sIndex: Integer; sDescription: String);
  begin
    with ComboBoxExHolder.ItemsEx.Add do
    begin
      ImageIndex:= sIndex;
      SelectedImageIndex:= sIndex;
      Caption:= sDescription;
    end;
  end;

begin
  AddItem( -1, 'Unknown');
  AddItem(  0, 'Rot. 0 (Horizontal)');
  AddItem(  1, 'Rot. 1 (Vertical)');
  AddItem( 90, 'Rot. 90 (Vertical)');
  AddItem(180, 'Rot. 180 (Horizontal)');
  AddItem(270, 'Rot. 270 (Vertical)');
end;

procedure TFormCustomGamesEditor.SetOrientationData(ComboBoxExHolder: TComboBoxEx; Rotation: Integer);
begin
  case Rotation of
     -1: SetComboBoxEx(ComboBoxExHolder, 0, True);
      0: SetComboBoxEx(ComboBoxExHolder, 1, True);
      1: SetComboBoxEx(ComboBoxExHolder, 2, True);
     90: SetComboBoxEx(ComboBoxExHolder, 3, True);
    180: SetComboBoxEx(ComboBoxExHolder, 4, True);
    270: SetComboBoxEx(ComboBoxExHolder, 5, True);
  end;
end;

function TFormCustomGamesEditor.AddCustomGames: Boolean;
var
  Node{, ParentNode}: PVirtualNode;
  Data: pGameInfo;
  Loop: Integer;
  gList: THashedStringList;
  NumScr: ShortInt;
begin
  case FormMain.ValidateFile(FormMain.FrontendPath+GetMachineTypeGamesFolder+GetSystemFileName(1)) of //CustomGames.el') of
    True:
      begin
        Screen.Cursor:= crHourGlass;
        gList:= THashedStringList.Create;
        GetGamesList(gList, 1); // 500);
        if gList.Count > 1 then
           begin
             with CustomGamesList do
             begin
               BeginUpdate;
               Clear;
               NodeDataSize:= -1;
               EndUpdate;
             end;
           end;
        CustomGamesList.NodeDataSize:= SizeOf(TGameInfo);
        CustomGamesList.RootNodeCount:= 0;
        CustomGamesList.BeginUpdate;
        // begin of games loading
        for Loop:=0 to gList.Count-1 do
        begin
          FormMain.ClearMemoryColumnsVariables;
          FormMain.GetROMFields(gList, Loop);

          FormMain.mImageIndex:= FormMain.mROMIdentification;
          FormMain.GetSystemID(FormMain.mROMIdentification);

          FormMain.mMerged:= aMerged;

          {if Assigned(CategoriesList) then
             begin
               if mCategory = '' then
                  mCategory:= CategoriesList.ReadString('Category', mName, '');
               if SystemID_Number = 1 then
                  mVersionAdded:= CategoriesList.ReadString('VerAdded', mName, '');
             end;

          if Assigned(GamePlayTime) then
             begin
               mPlayTime:= GamePlayTime.ReadString(mName, GetSystemFolderName(mROMIdentification)+'_time', '');
               mPlayed:= GamePlayTime.ReadString(mName, GetSystemFolderName(mROMIdentification)+'_played', '');
               if mPlayTime <> '' then
                  mPlayTime:= GetPlayTime(StrToInt64(mPlayTime), True, False);
               if mPlayed <> '' then
                  mPlayed:= Format('%s', [mPlayed]);
             end;}

          //if Assigned(iniNumPlayers) then
          //   mNumberPlayers:= iniNumPlayers.Values[mName];

          if FormMain.mControls <> '' then
             FormMain.mControlType:= StrToInt(FormMain.MountControlTypesList(FormMain.mControls, nil, True));

          NumScr:= -1;
          if FormMain.mRefreshRate1 <> '' then
             NumScr:= 1;
          if FormMain.mRefreshRate2 <> '' then
             Inc(NumScr);
          if FormMain.mRefreshRate3 <> '' then
             Inc(NumScr);
          if FormMain.mRefreshRate4 <> '' then
             Inc(NumScr);

          // add games to list
          Node:= CustomGamesList.AddChild(nil); // standard

          Data:= CustomGamesList.GetNodeData(Node);
          Data.eImageIndex:= FormMain.mImageIndex;
          Data.eParent:= -1;
          Data.eROMIdentification:= FormMain.mROMIdentification;
          Data.eMediaType:= FormMain.mMediaType;
          Data.eIconLoaded:= False;
          Data.eDescription:= FormMain.mDescription;
          Data.eYear:= FormMain.mYear;
          Data.eManufacturer:= FormMain.mManufacturer;
          Data.eAudioChannels:= FormMain.mAudioChannels;
          Data.eAudioSamples:= FormMain.mAudioSamples;
          Data.eAudioSampleName:= FormMain.mAudioSampleName;

          Data.eControls:= FormMain.mControls;
          Data.eControlType:= FormMain.mControlType;
          Data.eNumberScreens:= NumScr;

          Data.eScreenType1:= FormMain.mScreenType1;
          Data.eScreenRotation1:= FormMain.mScreenRotation1;
          Data.eResolutionWidth1:= FormMain.mResolutionWidth1;
          Data.eResolutionHeight1:= FormMain.mResolutionHeight1;
          Data.eRefreshRate1:= FormMain.mRefreshRate1;

          Data.eScreenType2:= FormMain.mScreenType2;
          Data.eScreenRotation2:= FormMain.mScreenRotation2;
          Data.eResolutionWidth2:= FormMain.mResolutionWidth2;
          Data.eResolutionHeight2:= FormMain.mResolutionHeight2;
          Data.eRefreshRate2:= FormMain.mRefreshRate2;

          Data.eScreenType3:= FormMain.mScreenType3;
          Data.eScreenRotation3:= FormMain.mScreenRotation3;
          Data.eResolutionWidth3:= FormMain.mResolutionWidth3;
          Data.eResolutionHeight3:= FormMain.mResolutionHeight3;
          Data.eRefreshRate3:= FormMain.mRefreshRate3;

          Data.eScreenType4:= FormMain.mScreenType4;
          Data.eScreenRotation4:= FormMain.mScreenRotation4;
          Data.eResolutionWidth4:= FormMain.mResolutionWidth4;
          Data.eResolutionHeight4:= FormMain.mResolutionHeight4;
          Data.eRefreshRate4:= FormMain.mRefreshRate4;

          Data.eDriverStatus:= FormMain.mDriverStatus;
          Data.eEmulationStatus:= FormMain.mEmulationStatus;
          Data.eColorStatus:= FormMain.mColorStatus;
          Data.eSoundStatus:= FormMain.mSoundStatus;
          Data.eGraphicStatus:= FormMain.mGraphicStatus;
          Data.eCocktailStatus:= FormMain.mCocktailStatus;
          Data.eProtectionStatus:= FormMain.mProtectionStatus;

          //Data.eCategory:= FormMain.mCategory;
          //Data.eVersionAdded:= FormMain.mVersionAdded;
          Data.eName:= FormMain.mName;
          Data.eClone:= FormMain.mClone;
          Data.eDriverName:= FormMain.mDriverName;
          Data.eBiosName:= FormMain.mBiosName;
          Data.eMerged:= FormMain.mMerged;
          Data.ePlayTime:= FormMain.mPlayTime;
          Data.ePlayed:= FormMain.mPlayed;
          Data.eNumberPlayers:= FormMain.mNumberPlayers;
          Data.eGameSize:= FormMain.mGameSize;
          Data.eSaveState:= FormMain.mSaveState;
        end;
        //FreeAndNil(GamePlayTime);
        //FreeAndNil(CategoriesList);
        //FreeAndNil(iniNumPlayers);
        CustomGamesList.EndUpdate;

        // end of games loading
        CustomGamesList.SortTree(0, sdAscending);
        FreeAndNil(gList);
        UpdateTotalGames;
        Screen.Cursor:= crDefault;
      end;
    False:
      begin
        GenerateMessage('Error', Format('File "%s" was not found. Aborting...',
                                   [FormMain.FrontendPath+GetMachineTypeGamesFolder+'CustomGames.el']), 2);
      end;
  end;
end;

procedure TFormCustomGamesEditor.LoadGameData;
var
  Data: pGameInfo;
begin
  if (CustomGamesList.SelectedCount = 0) or
     (CustomGamesList.SelectedCount > 1) then
     Exit;

  Data:= CustomGamesList.GetNodeData(CurrentNode);
  if Data = nil then
     Exit;

    //Data.eImageIndex:= FormMain.mImageIndex;
    //Data.eParent:= -1;
    //Data.eROMIdentification:= FormMain.mROMIdentification;

  SetComboBoxEx(MediaType, Data.eMediaType+1, True);
  Description.Text:= Data.eDescription;
  Year.Text:= Data.eYear;
  Manufacturer.Text:= Data.eManufacturer;
  SetComboBoxEx(AudioChannels, Data.eAudioChannels, True);
  SetComboBoxEx(AudioSamples, Data.eAudioSamples+1, True);
  AudioSampleName.Text:= Data.eAudioSampleName;

  //FormMain.mControls:= Data.eControls;
    //Data.eControlType:= FormMain.mControlType; ???? what to do here ?

  ControlTypeList.Items.Clear;
  FormMain.MountControlTypesList(Data.eControls, ControlTypeList);
  NumberScreens.Position:= Data.eNumberScreens;

  SetComboBoxEx(Screen0Type, Data.eScreenType1+1, True);
  SetOrientationData(Screen0Orientation, Data.eScreenRotation1);
  Screen0ResolutionWidth.Text:= IntToStr(Data.eResolutionWidth1);
  Screen0ResolutionHeight.Text:= IntToStr(Data.eResolutionHeight1);
  Screen0RefreshRate.Text:= Data.eRefreshRate1;

  SetComboBoxEx(Screen1Type, Data.eScreenType2+1, True);
  SetOrientationData(Screen1Orientation, Data.eScreenRotation2);
  Screen1ResolutionWidth.Text:= IntToStr(Data.eResolutionWidth2);
  Screen1ResolutionHeight.Text:= IntToStr(Data.eResolutionHeight2);
  Screen1RefreshRate.Text:= Data.eRefreshRate2;

  SetComboBoxEx(Screen2Type, Data.eScreenType3+1, True);
  SetOrientationData(Screen2Orientation, Data.eScreenRotation3);
  Screen2ResolutionWidth.Text:= IntToStr(Data.eResolutionWidth3);
  Screen2ResolutionHeight.Text:= IntToStr(Data.eResolutionHeight3);
  Screen2RefreshRate.Text:= Data.eRefreshRate3;

  SetComboBoxEx(Screen2Type, Data.eScreenType4+1, True);
  SetOrientationData(Screen3Orientation, Data.eScreenRotation4);
  Screen3ResolutionWidth.Text:= IntToStr(Data.eResolutionWidth4);
  Screen3ResolutionHeight.Text:= IntToStr(Data.eResolutionHeight4);
  Screen3RefreshRate.Text:= Data.eRefreshRate4;

  SetComboBoxEx(DriverStatus, Data.eDriverStatus+1, True);
  SetComboBoxEx(EmulationStatus, Data.eEmulationStatus+1, True);
  SetComboBoxEx(ColorStatus, Data.eColorStatus+1, True);
  SetComboBoxEx(SoundStatus, Data.eSoundStatus+1, True);
  SetComboBoxEx(GraphicsStatus, Data.eGraphicStatus+1, True);
  SetComboBoxEx(CocktailStatus, Data.eCocktailStatus+1, True);
  SetComboBoxEx(ProtectionStatus, Data.eProtectionStatus+1, True);

  //Data.eCategory:= FormMain.mCategory;
  //Data.eVersionAdded:= FormMain.mVersionAdded;
  GameName.Text:= Data.eName;
  CloneOf.Text:= Data.eClone;
  DriverName.Text:= Data.eDriverName;
  BiosName.Text:= Data.eBiosName;

  NumberPlayers.Text:= Data.eNumberPlayers;
  GameSize.Text:= Data.eGameSize;
  SetComboBoxEx(SaveState, Data.eSaveState+1, True);
end;

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

procedure TFormCustomGamesEditor.FormShow(Sender: TObject);
begin
  FormMain.SetAlphaBlendTreeView(CustomGamesList);
  PopulateComboBoxEx(aMediaType, MediaType);
  PopulateComboBoxEx(aAudioChannels, AudioChannels);

  PopulateControlsComboBox(ControlType);

  PopulateComboBoxEx(aScreenType, Screen0Type);
  PopulateOrientationList(Screen0Orientation);
  PopulateComboBoxEx(aScreenType, Screen1Type);
  PopulateOrientationList(Screen1Orientation);
  PopulateComboBoxEx(aScreenType, Screen2Type);
  PopulateOrientationList(Screen2Orientation);
  PopulateComboBoxEx(aScreenType, Screen3Type);
  PopulateOrientationList(Screen3Orientation);

  PopulateComboBoxEx(aStatus, DriverStatus);
  PopulateComboBoxEx(aStatus, EmulationStatus);
  PopulateComboBoxEx(aStatus, ColorStatus);
  PopulateComboBoxEx(aStatus, SoundStatus);
  PopulateComboBoxEx(aStatus, GraphicsStatus);
  PopulateComboBoxEx(aStatus, CocktailStatus);
  PopulateComboBoxEx(aStatus, ProtectionStatus);

  PopulateComboBoxEx(aSaveState, SaveState);

  SetComboBoxEx(ROM_ID, 0);
  SetComboBoxEx(MediaType, 1);
  SetComboBoxEx(AudioChannels, 2);
  SetComboBoxEx(AudioSamples, 0);
  SetComboBoxEx(ControlType, 0);
  SetComboBoxEx(Screen0Type, 1);
  SetComboBoxEx(Screen1Type, 0);
  SetComboBoxEx(Screen2Type, 0);
  SetComboBoxEx(Screen3Type, 0);
  SetComboBoxEx(Screen0Orientation, 1);
  SetComboBoxEx(Screen1Orientation, 0);
  SetComboBoxEx(Screen2Orientation, 0);
  SetComboBoxEx(Screen3Orientation, 0);

  SetComboBoxEx(DriverStatus, 0);
  SetComboBoxEx(EmulationStatus, 0);
  SetComboBoxEx(ColorStatus, 0);
  SetComboBoxEx(SoundStatus, 0);
  SetComboBoxEx(GraphicsStatus, 0);
  SetComboBoxEx(CocktailStatus, 0);
  SetComboBoxEx(ProtectionStatus, 0);

  SetComboBoxEx(SaveState, 0);
  ButtonReload.Click;
end;

procedure TFormCustomGamesEditor.ButtonAddControlClick(Sender: TObject);
begin
  with ControlTypeList.Items.Add do
  begin
    ImageIndex:= ControlType.ItemsEx[ControlType.ItemIndex].ImageIndex;
    Caption:= ControlType.ItemsEx[ControlType.ItemIndex].Caption;
  end;
end;

procedure TFormCustomGamesEditor.ButtonClearControlsClick(Sender: TObject);
begin
  ControlTypeList.Items.Clear;
end;

procedure TFormCustomGamesEditor.ButtonDeleteControlsClick(
  Sender: TObject);
begin
  ControlTypeList.DeleteSelected;
end;

procedure TFormCustomGamesEditor.ButtonNew2Click(Sender: TObject);
begin
  ButtonNew.Click;
end;

procedure TFormCustomGamesEditor.ButtonCommandLine2Click(Sender: TObject);
begin
  ButtonCommandLine.Click;
end;

procedure TFormCustomGamesEditor.ButtonEdit2Click(Sender: TObject);
begin
  ButtonEdit.Click;
end;

procedure TFormCustomGamesEditor.ButtonReloadClick(Sender: TObject);
begin
  AddCustomGames;
end;

procedure TFormCustomGamesEditor.CustomGamesListCompareNodes(
  Sender: TBaseVirtualTree; Node1, Node2: PVirtualNode;
  Column: TColumnIndex; var Result: Integer);
var
  Data1, Data2: pGameInfo;
begin
  Data1:= Sender.GetNodeData(Node1);
  Data2:= Sender.GetNodeData(Node2);
  case Column of
    0: Result:= CompareText(Data1.eDescription, Data2.eDescription);
    1: Result:= CompareText(Data1.eName, Data2.eName);
    2: Result:= CompareText(Data1.eClone, Data2.eClone);
    3: Result:= CompareText(Data1.eDriverName, Data2.eDriverName);
    4: Result:= CompareIntValue(FormMain.GetSystemID(Data1.eROMIdentification), FormMain.GetSystemID(Data2.eROMIdentification));
  end;
end;

procedure TFormCustomGamesEditor.CustomGamesListFreeNode(
  Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  Data: pGameInfo;
begin
  Data:= CustomGamesList.GetNodeData(Node);
  Finalize(Data^);
end;

procedure TFormCustomGamesEditor.CustomGamesListGetHint(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: WideString);
var
  Data: pGameInfo;
begin
  Data:= CustomGamesList.GetNodeData(Node);
  case Column of
    0: HintText:= Data.eDescription;
    1: HintText:= Data.eName;
    2: HintText:= Data.eClone;
    3: HintText:= Data.eDriverName;
    4: HintText:= FormMain.GetEmuFullDescription(FormMain.GetSystemID(Data.eROMIdentification));
  end;
end;

procedure TFormCustomGamesEditor.CustomGamesListGetText(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType; var CellText: WideString);
var
  Data: pGameInfo;
begin
  Data:= CustomGamesList.GetNodeData(Node);
  case Column of
    0: CellText:= Data.eDescription;
    1: CellText:= Data.eName;
    2: CellText:= Data.eClone;
    3: CellText:= Data.eDriverName;
    4: CellText:= FormMain.GetEmuFullDescription(FormMain.GetSystemID(Data.eROMIdentification));
  end;
end;

procedure TFormCustomGamesEditor.CustomGamesListGetImageIndex(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Kind: TVTImageKind;
  Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: Integer);
var
  Data: pGameInfo;
begin
  if Kind in [ikNormal, ikSelected] then
     begin
       Data:= Sender.GetNodeData(Node);
       case Column of
         0: ImageIndex:= Data.eImageIndex;
         4: ImageIndex:= FormMain.GetSystemID(Data.eROMIdentification)+MaxGameID;
       end;
     end;
end;

procedure TFormCustomGamesEditor.CustomGamesListHeaderClick(
  Sender: TVTHeader; Column: TColumnIndex; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
     begin
       with Sender, Treeview do
       begin
         if SortColumn > NoColumn then
            Columns[SortColumn].Options:= Columns[SortColumn].Options + [coParentColor];
         if (SortColumn = NoColumn) or (SortColumn <> Column) then
            begin
              SortColumn:= Column;
              SortDirection:= sdAscending;
            end
         else
            if SortDirection = sdAscending then
               SortDirection:= sdDescending
            else
               SortDirection:= sdAscending;
         Columns[SortColumn].Color:= $F7F7F7;
         SortTree(SortColumn, SortDirection, False);
         ScrollIntoView(CurrentNode, True);
       end;
     end;
end;

procedure TFormCustomGamesEditor.CustomGamesListIncrementalSearch(
  Sender: TBaseVirtualTree; Node: PVirtualNode;
  const SearchText: WideString; var Result: Integer);
var
  StringsText,
  EntryText: String;
  Data: pGameInfo;
begin
  StringsText:= SearchText;
  Data:= Sender.GetNodeData(Node);
  EntryText:= Data.eDescription;
  Result := AnsiStrLIComp(PChar(StringsText), PChar(EntryText), Length(StringsText));
end;

procedure TFormCustomGamesEditor.PageControlMainChange(Sender: TObject);
begin
  if PageControlMain.ActivePageIndex = 1 then
     LoadGameData;
end;

procedure TFormCustomGamesEditor.CustomGamesListChange(
  Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  if (Sender.SelectedCount = 1) and (Sender.Visible) then
     begin
       if CurrentNode <> Node then
          CurrentNode:= Node;
     end;
end;

procedure TFormCustomGamesEditor.ButtonEditClick(Sender: TObject);
begin
  if CustomGamesList.SelectedCount = 0 then
     begin
       GenerateMessage('Error', 'Please select a game to edit first.', 2);
       Exit;
     end
  else
  if CustomGamesList.SelectedCount > 1 then
     begin
       GenerateMessage('Error', 'Please select only one game. Multiselect is not supported.', 2);
       Exit;
     end;

  TabSheetEditor.Tag:= 1; // edit
  if PageControlMain.ActivePageIndex = 0 then
     PageControlMain.ActivePageIndex:= 1;
  TabSheetEditor.Caption:= 'Editor - [edit mode]';
end;

procedure TFormCustomGamesEditor.ButtonNewClick(Sender: TObject);
begin
  TabSheetEditor.Tag:= 2; // new
  if PageControlMain.ActivePageIndex = 0 then
     PageControlMain.ActivePageIndex:= 1;
  TabSheetEditor.Caption:= 'Editor - [new game mode]';
end;

procedure TFormCustomGamesEditor.ButtonDeleteClick(Sender: TObject);
var
  ModeString: String;
begin
  if TabSheetEditor.Tag <> 0 then
     begin
       case TabSheetEditor.Tag of
         1: ModeString:= 'edit mode';
         2: ModeString:= 'new game mode';
       end;
       GenerateMessage('Error', Format('Cannot delete game while in %s.', [ModeString]), 2);
       Exit;
     end;
  if CustomGamesList.SelectedCount = 0 then
     begin
       GenerateMessage('Error', 'Please select at least one game to delete.', 2);
       Exit;
     end;

  CustomGamesList.BeginUpdate;
  CustomGamesList.DeleteSelectedNodes;
  CustomGamesList.EndUpdate;
  CustomGamesList.SortTree(0, sdAscending);
  CustomGamesList.ScrollIntoView(CurrentNode, True);
  UpdateTotalGames;
end;

procedure TFormCustomGamesEditor.ButtonCancelClick(Sender: TObject);
begin
  TabSheetEditor.Tag:= 0;
  TabSheetEditor.Caption:= 'Editor - [browse mode]';
end;

procedure TFormCustomGamesEditor.ButtonOkClick(Sender: TObject);
begin
  if ControlTypeList.Items.Count = 0 then
     GenerateMessage('Error', 'Controls is empty. Please add at least one control to the list.', 2);
end;

procedure TFormCustomGamesEditor.NumberScreensChange(Sender: TObject);
begin
  LabelNumberScreensValue.Caption:= IntToStr(NumberScreens.Position);
end;

end.
