BSOne.SFC/Tocsg.Module/ImageMasking/ManagerImgMskData.pas

141 lines
3.2 KiB
Plaintext

{*******************************************************}
{ }
{ ManagerImgMskData }
{ }
{ Copyright (C) 2024 kku }
{ }
{*******************************************************}
unit ManagerImgMskData;
interface
uses
Tocsg.Obj, System.SysUtils, Winapi.Windows, System.Generics.Collections,
System.Classes;
const
SP_ENT = '|§*';
SP_COL = '$|§';
type
PMskEnt = ^TMskEnt;
TMskEnt = record
sWord: String;
x, y, w, h: Integer;
end;
TMskEntList = TList<PMskEnt>;
TManagerImgMskData = class(TTgObject)
private
EntList_: TMskEntList;
procedure OnEntNotify(Sender: TObject; const Item: PMskEnt; Action: TCollectionNotification);
public
Constructor Create(sPath: String = '');
Destructor Destroy; override;
procedure LoadFromFile(sPath: String);
function MatchToList(sFindStr: String; aList: TMskEntList): Integer;
end;
implementation
uses
Tocsg.Safe, Tocsg.Exception, Tocsg.Strings;
Constructor TManagerImgMskData.Create(sPath: String = '');
begin
Inherited Create;
EntList_ := TList<PMskEnt>.Create;
EntList_.OnNotify := OnEntNotify;
if sPath <> '' then
LoadFromFile(sPath);
end;
Destructor TManagerImgMskData.Destroy;
begin
FreeAndNil(EntList_);
Inherited;
end;
procedure TManagerImgMskData.OnEntNotify(Sender: TObject; const Item: PMskEnt; Action: TCollectionNotification);
begin
if Action = cnRemoved then
Dispose(Item);
end;
procedure TManagerImgMskData.LoadFromFile(sPath: String);
var
StrList,
EntList: TStringList;
i, n: Integer;
sProc, sEnt: String;
pEnt: PMskEnt;
begin
EntList_.Clear;
if not FileExists(sPath) then
exit;
try
Guard(StrList, TStringList.Create);
Guard(EntList, TStringList.Create);
sProc := '';
StrList.LoadFromFile(sPath, TEncoding.UTF8);
for i := 0 to StrList.Count - 1 do
begin
sProc := sProc + StrList[i];
while sProc <> '' do
begin
n := Pos(SP_ENT, sProc);
if n > 0 then
begin
sEnt := Copy(sProc, 1, n - 1);
Delete(sProc, 1, n + 2);
SplitString(sEnt, SP_COL, EntList);
if EntList.Count > 4 then
begin
New(pEnt);
pEnt.sWord := EntList[0];
pEnt.x := StrToIntDef(EntList[1], 0);
pEnt.y := StrToIntDef(EntList[2], 0);
pEnt.w := StrToIntDef(EntList[3], 0);
pEnt.h := StrToIntDef(EntList[4], 0);
EntList_.Add(pEnt);
end;
end else break;
end;
end;
except
on E: Exception do
ETgException.TraceException(Self, E, 'Fail .. LoadFromFile()');
end;
end;
function TManagerImgMskData.MatchToList(sFindStr: String; aList: TMskEntList): Integer;
var
i: Integer;
begin
Result := 0;
aList.Clear;
try
for i := 0 to EntList_.Count - 1 do
begin
if Pos(EntList_[i].sWord, sFindStr) > 0 then
aList.Add(EntList_[i]);
end;
Result := aList.Count;
except
on E: Exception do
ETgException.TraceException(Self, E, 'Fail .. MatchToList()');
end;
end;
end.