unit uConvertImages;

interface

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

type
  pFileInfo = ^TFileInfo;
  TFileInfo = record
    cvImageIndex: Integer;
    cvFileName,
    cvGameName,
    cvFilePath: String;
    cvSystem: ShortInt;
    cvConverted: Boolean;
    cvNotes: String;
  end;

type
  TFormConvertImages = class(TForm)
    TopImage: TImage32;
    LabelDescription: TLabel;
    ImageTypeSelector: TComboBoxEx;
    SystemSelector: TComboBoxEx;
    ButtonScan: TButton;
    ButtonClose: TButton;
    ButtonSelectAll: TButton;
    ButtonSelectNode: TButton;
    ButtonClear: TButton;
    ButtonRename: TButton;
    FilesList: TVirtualStringTree;
    ButtonRemoveDuplicates: TButton;
    procedure FormShow(Sender: TObject);
    procedure ButtonCloseClick(Sender: TObject);
    procedure ButtonSelectAllClick(Sender: TObject);
    procedure ButtonClearClick(Sender: TObject);
    procedure ButtonScanClick(Sender: TObject);
    procedure SystemSelectorSelect(Sender: TObject);
    procedure ImageTypeSelectorSelect(Sender: TObject);
    procedure FilesListCompareNodes(Sender: TBaseVirtualTree; Node1,
      Node2: PVirtualNode; Column: TColumnIndex; var Result: Integer);
    procedure FilesListFreeNode(Sender: TBaseVirtualTree;
      Node: PVirtualNode);
    procedure FilesListGetHint(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex;
      var LineBreakStyle: TVTTooltipLineBreakStyle;
      var HintText: WideString);
    procedure FilesListGetText(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: WideString);
    procedure FilesListGetImageIndex(Sender: TBaseVirtualTree;
      Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
      var Ghosted: Boolean; var ImageIndex: Integer);
    procedure FilesListHeaderClick(Sender: TVTHeader;
      Column: TColumnIndex; Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    procedure FilesListIncrementalSearch(Sender: TBaseVirtualTree;
      Node: PVirtualNode; const SearchText: WideString;
      var Result: Integer);
    procedure ButtonSelectNodeClick(Sender: TObject);
  private
    { Private declarations }
    procedure ScanFiles;
  public
    { Public declarations }
  end;

var
  FormConvertImages: TFormConvertImages;

implementation

uses uMain, uCommon, uStatus;

{$R *.dfm}

procedure TFormConvertImages.ScanFiles;
var
  GamesListDAT: THashedStringList;
  Loop: Integer;
  Folder, tempString: String;
  FilesFound: Boolean;
  Node: PVirtualNode;
  Data: pFileInfo;

  procedure CheckGameFiles(GameName, ImageExt: String);
  var
    Loop2: ShortInt;
  begin
    for Loop2:=1 to 10 do
    begin
      tempString:= IncludeTrailingPathDelimiter(Folder)+
                   Format('%s%s', [FormMain.GetImageName(GameName, Loop2, 1), ImageExt]);
      if FileExists(tempString) then
         begin
           FilesFound:= True;
           Node:= FilesList.AddChild(nil);
           Data:= FilesList.GetNodeData(Node);
           Data.cvImageIndex:= SystemSelector.Tag;
           Data.cvSystem:= SystemSelector.Tag;
           Data.cvFileName:= tempString;
           Data.cvGameName:= GameName;
           Data.cvFilePath:= IncludeTrailingPathDelimiter(Folder);
           Data.cvConverted:= False;
           Data.cvNotes:= '';
           FilesList.CheckType[Node]:= ctCheckBox;
           FilesList.CheckState[Node]:= csCheckedNormal;
         end;
    end;
  end;

begin
  Application.ProcessMessages;
  if FormMain.imgFolderZipFile[SystemSelector.Tag, ImageTypeSelector.Tag] <> '' then
     Folder:= FormMain.GetZipFolderFull(ImageTypeSelector.Tag, SystemSelector.Tag);

  if Folder = '' then
     begin
       GenerateMessage('Information', Format('Folder not defined for "%s" - [%s].',
             [ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].Caption,
              SystemSelector.ItemsEx[SystemSelector.ItemIndex].Caption]), 2);
       Exit;
     end;
  Screen.Cursor:= crHourGlass;
  FormStatus.Show;
  FormStatus.LabelStatusType.Caption:= Format('%s [%s]',
        ['Convert Images',
         ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].Caption]);
  FormStatus.LabelMessage.Caption:= 'Parsing games list.';
  FormStatus.ProgressBar.Progress:= 0;
  FormStatus.Refresh;
  FilesFound:= False;

  if Folder <> '' then
     begin
       Screen.Cursor:= crHourGlass;
       GamesListDAT:= THashedStringList.Create;
       GetGamesList(GamesListDAT, SystemSelector.Tag);
       GamesListDAT.BeginUpdate;
       for Loop:=GamesListDAT.Count-1 downto 0 do
       begin
         FormMain.GetROMFields(GamesListDAT, Loop);
         GamesListDAT[Loop]:= FormMain.mName;
       end;
       GamesListDAT.Sort;
       GamesListDAT.EndUpdate;

       FormStatus.ProgressBar.MaxValue:= GamesListDAT.Count-1;
       FormStatus.LabelMessage.Caption:= 'Scanning files.';
       FormStatus.Refresh;

       if not FormMain.CheckTotal(FilesList) then
          begin
            FilesList.NodeDataSize:= SizeOf(TFileInfo);
            FilesList.RootNodeCount:= 0;
          end;
       FilesList.BeginUpdate;

       for Loop:=0 to GamesListDAT.Count-1 do
       begin
         CheckGameFiles(GamesListDAT[Loop], '.png');
         if not (ImageTypeSelector.Tag in [0, 1]) then
            begin
              CheckGameFiles(GamesListDAT[Loop], '.jpg');
              CheckGameFiles(GamesListDAT[Loop], '.gif');
            end;
         FormStatus.ProgressBar.Progress:= Loop;
       end;

       FilesList.EndUpdate;
     end;

  FormStatus.Close;
  Screen.Cursor:= crDefault;
  FreeAndNil(GamesListDAT);
  if not FilesFound then
     GenerateMessage('Information', Format('No files were found for "%s" - [%s].',
         [ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].Caption,
          SystemSelector.ItemsEx[SystemSelector.ItemIndex].Caption]), 2);
  //ButtonScan.Visible:= True;
  //imgFolderZipFile[1, 1]; // game snapshots [sys, image_type]
end;

procedure TFormConvertImages.FormShow(Sender: TObject);
begin
  FormMain.LoadTopBar(TopImage, 'images.png');
  FormMain.FillEmuDescriptionsSelector(SystemSelector);
  FormMain.UpdateImageTypeDescriptionComboBoxEx(ImageTypeSelector);
  SetComboBoxEx(SystemSelector, 0);
  SetComboBoxEx(ImageTypeSelector, 1);
  FormMain.SetAlphaBlendTreeView(FilesList);
end;

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

procedure TFormConvertImages.ButtonSelectAllClick(Sender: TObject);
var
  Node: PVirtualNode;
begin
  FilesList.BeginUpdate;
  Node:= FilesList.GetFirst;
  repeat
    FilesList.CheckState[Node]:= csCheckedNormal;
    Node:= FilesList.GetNext(Node);
  until Node = nil;
  FilesList.EndUpdate;
end;

procedure TFormConvertImages.ButtonClearClick(Sender: TObject);
begin
  FormMain.ClearTreeView(FilesList);
end;

procedure TFormConvertImages.ButtonScanClick(Sender: TObject);
begin
  ScanFiles;
end;

procedure TFormConvertImages.SystemSelectorSelect(Sender: TObject);
begin
  SystemSelector.Tag:= SystemSelector.ItemsEx[SystemSelector.ItemIndex].ImageIndex;
end;

procedure TFormConvertImages.ImageTypeSelectorSelect(Sender: TObject);
begin
  ImageTypeSelector.Tag:= ImageTypeSelector.ItemsEx[ImageTypeSelector.ItemIndex].ImageIndex;
end;

procedure TFormConvertImages.FilesListCompareNodes(
  Sender: TBaseVirtualTree; Node1, Node2: PVirtualNode;
  Column: TColumnIndex; var Result: Integer);
var
  Data1, Data2: pFileInfo;
begin
  Data1:= Sender.GetNodeData(Node1);
  Data2:= Sender.GetNodeData(Node2);
  case Column of
    0: Result:= CompareText(Data1.cvFileName, Data2.cvFileName);
    1: Result:= CompareText(Data1.cvGameName, Data2.cvGameName);
    2: Result:= CompareText(Data1.cvNotes, Data2.cvNotes);
  end;
end;

procedure TFormConvertImages.FilesListFreeNode(
  Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  Data: pFileInfo;
begin
  Data:= Sender.GetNodeData(Node);
  Finalize(Data^);
end;

procedure TFormConvertImages.FilesListGetHint(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  var LineBreakStyle: TVTTooltipLineBreakStyle; var HintText: WideString);
var
  Data: pFileInfo;
begin
  Data:= Sender.GetNodeData(Node);
  case Column of
    0: HintText:= Data.cvFileName;
    1: HintText:= Data.cvGameName;
    2: HintText:= Data.cvNotes;
  end;
end;

procedure TFormConvertImages.FilesListGetText(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
  TextType: TVSTTextType; var CellText: WideString);
var
  Data: pFileInfo;
begin
  Data:= Sender.GetNodeData(Node);
  case Column of
    0: CellText:= Data.cvFileName;
    1: CellText:= Data.cvGameName;
    2: CellText:= Data.cvNotes;
  end;
end;

procedure TFormConvertImages.FilesListGetImageIndex(
  Sender: TBaseVirtualTree; Node: PVirtualNode; Kind: TVTImageKind;
  Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: Integer);
var
  Data: pFileInfo;
begin
  if Kind in [ikNormal, ikSelected] then
     begin
       if Column = 0 then
          begin
            Data:= Sender.GetNodeData(Node);
            ImageIndex:= Data.cvImageIndex;
          end;
     end;
end;

procedure TFormConvertImages.FilesListHeaderClick(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);
       end;
     end;
end;

procedure TFormConvertImages.FilesListIncrementalSearch(
  Sender: TBaseVirtualTree; Node: PVirtualNode;
  const SearchText: WideString; var Result: Integer);
var
  StringsText,
  EntryText: String;
  Data: pFileInfo;
begin
  StringsText:= SearchText;
  Data:= Sender.GetNodeData(Node);
  EntryText:= Data.cvGameName;
  Result:= AnsiStrLIComp(PChar(StringsText), PChar(EntryText), Length(StringsText));
end;

procedure TFormConvertImages.ButtonSelectNodeClick(Sender: TObject);
begin
  FilesList.BeginUpdate;
  FilesList.ClearChecked;
  FilesList.EndUpdate;
end;

end.
