- Replaced array with generic list (TfrmMain.FAccessChangedListeners)

This commit is contained in:
Andreas Schneider 2009-12-19 14:43:22 +01:00
parent a9f278ae21
commit 9ab8e5901b
1 changed files with 8 additions and 27 deletions

View File

@ -45,6 +45,7 @@ type
TGhostTile = class(TStaticItem); TGhostTile = class(TStaticItem);
TPacketList = specialize TFPGObjectList<TPacket>; TPacketList = specialize TFPGObjectList<TPacket>;
TAccessChangedListeners = specialize TFPGList<TAccessChangedListener>;
TSelectionListeners = specialize TFPGList<TSelectionListener>; TSelectionListeners = specialize TFPGList<TSelectionListener>;
{ TfrmMain } { TfrmMain }
@ -309,7 +310,7 @@ type
FRandomPresetsFile: string; FRandomPresetsFile: string;
FRandomPresetsDoc: TXMLDocument; FRandomPresetsDoc: TXMLDocument;
FLastDraw: TDateTime; FLastDraw: TDateTime;
FAccessChangedListeners: array of TAccessChangedListener; FAccessChangedListeners: TAccessChangedListeners;
FRepaintNeeded: Boolean; FRepaintNeeded: Boolean;
FSelection: TRect; FSelection: TRect;
FUndoList: TPacketList; FUndoList: TPacketList;
@ -916,6 +917,7 @@ begin
DoubleBuffered := True; DoubleBuffered := True;
pnlBottom.DoubleBuffered := True; pnlBottom.DoubleBuffered := True;
FAccessChangedListeners := TAccessChangedListeners.Create;
FSelectionListeners := TSelectionListeners.Create; FSelectionListeners := TSelectionListeners.Create;
FLastDraw := Now; FLastDraw := Now;
@ -1244,6 +1246,7 @@ begin
FreeAndNil(FUndoList); FreeAndNil(FUndoList);
FreeAndNil(FGLFont); FreeAndNil(FGLFont);
FreeAndNil(FRandomPresetsDoc); FreeAndNil(FRandomPresetsDoc);
FreeAndNil(FAccessChangedListeners);
FreeAndNil(FSelectionListeners); FreeAndNil(FSelectionListeners);
RegisterPacketHandler($0C, nil); RegisterPacketHandler($0C, nil);
@ -1815,14 +1818,9 @@ end;
procedure TfrmMain.RegisterAccessChangedListener( procedure TfrmMain.RegisterAccessChangedListener(
AListener: TAccessChangedListener); AListener: TAccessChangedListener);
var
i: Integer;
begin begin
for i := Low(FAccessChangedListeners) to High(FAccessChangedListeners) do if FAccessChangedListeners.IndexOf(AListener) < 0 then
if FAccessChangedListeners[i] = AListener then FAccessChangedListeners.Add(AListener);
Exit; //Prevent duplicates
SetLength(FAccessChangedListeners, Length(FAccessChangedListeners) + 1);
FAccessChangedListeners[High(FAccessChangedListeners)] := AListener;
end; end;
procedure TfrmMain.RegisterSelectionListener(AListener: TSelectionListener); procedure TfrmMain.RegisterSelectionListener(AListener: TSelectionListener);
@ -1833,25 +1831,8 @@ end;
procedure TfrmMain.UnregisterAccessChangedListener( procedure TfrmMain.UnregisterAccessChangedListener(
AListener: TAccessChangedListener); AListener: TAccessChangedListener);
var
i: Integer;
found: Boolean;
begin begin
i := Low(FAccessChangedListeners); FAccessChangedListeners.Remove(AListener);
found := False;
while (i <= High(FAccessChangedListeners)) and (not found) do
begin
if FAccessChangedListeners[i] = AListener then
begin
if i < High(FAccessChangedListeners) then
Move(FAccessChangedListeners[i+1], FAccessChangedListeners[i],
(High(FAccessChangedListeners) - Low(FAccessChangedListeners) - i) *
SizeOf(TAccessChangedListener)); //move subsequent entries
SetLength(FAccessChangedListeners, Length(FAccessChangedListeners) - 1);
found := True;
end else
Inc(i);
end;
end; end;
procedure TfrmMain.UnregisterSelectionListener(AListener: TSelectionListener); procedure TfrmMain.UnregisterSelectionListener(AListener: TSelectionListener);
@ -2951,7 +2932,7 @@ begin
end; end;
end; end;
for i := Low(FAccessChangedListeners) to High(FAccessChangedListeners) do for i := FAccessChangedListeners.Count - 1 downto 0 do
FAccessChangedListeners[i](accessLevel); FAccessChangedListeners[i](accessLevel);
end; end;
end; end;