🚨 Stop the range selection helper from leaking
Abandoning a selection after the first click leaked the helper and kept it registered for tile clicks. The helper now tracks the active instance, copies coordinates instead of holding tile references that can be evicted, and the listener dispatch tolerates removal during iteration.
This commit is contained in:
+33
-35
@@ -3,13 +3,13 @@
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
* (the "License"). You may not use this file except in compliance with
|
||||
* the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
@@ -43,28 +43,29 @@ uses
|
||||
UfrmMain, UWorldItem, math;
|
||||
|
||||
type
|
||||
|
||||
{ TRangeSelectionHelper }
|
||||
|
||||
TRangeSelectionHelper = class
|
||||
constructor Create(ACallback: TSelectedRangeCallback);
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
FCallback: TSelectedRangeCallback;
|
||||
FItem1: TWorldItem;
|
||||
FItem2: TWorldItem;
|
||||
FHasFirst: Boolean;
|
||||
FX1, FY1: Word;
|
||||
procedure TileSelected(AWorldItem: TWorldItem);
|
||||
procedure Finish;
|
||||
procedure Finish(AX2, AY2: Word);
|
||||
public
|
||||
procedure Run;
|
||||
end;
|
||||
|
||||
procedure SelectRange(ACallback: TSelectedRangeCallback);
|
||||
var
|
||||
helper: TRangeSelectionHelper;
|
||||
ActiveHelper: TRangeSelectionHelper = nil;
|
||||
|
||||
procedure SelectRange(ACallback: TSelectedRangeCallback);
|
||||
begin
|
||||
helper := TRangeSelectionHelper.Create(ACallback);
|
||||
helper.Run;
|
||||
//Cleanup will follow asynchroneously
|
||||
FreeAndNil(ActiveHelper);
|
||||
ActiveHelper := TRangeSelectionHelper.Create(ACallback);
|
||||
ActiveHelper.Run;
|
||||
end;
|
||||
|
||||
{ TRangeSelectionHelper }
|
||||
@@ -72,42 +73,39 @@ end;
|
||||
constructor TRangeSelectionHelper.Create(ACallback: TSelectedRangeCallback);
|
||||
begin
|
||||
FCallback := ACallback;
|
||||
FItem1 := nil;
|
||||
FItem2 := nil;
|
||||
FHasFirst := False;
|
||||
end;
|
||||
|
||||
destructor TRangeSelectionHelper.Destroy;
|
||||
begin
|
||||
if frmMain <> nil then
|
||||
frmMain.UnregisterSelectionListener(@TileSelected);
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TRangeSelectionHelper.TileSelected(AWorldItem: TWorldItem);
|
||||
begin
|
||||
if FItem1 = nil then
|
||||
FItem1 := AWorldItem
|
||||
else if FItem2 = nil then
|
||||
if not FHasFirst then
|
||||
begin
|
||||
FItem2 := AWorldItem;
|
||||
Finish;
|
||||
end;
|
||||
FHasFirst := True;
|
||||
FX1 := AWorldItem.X;
|
||||
FY1 := AWorldItem.Y;
|
||||
end else
|
||||
Finish(AWorldItem.X, AWorldItem.Y);
|
||||
end;
|
||||
|
||||
procedure TRangeSelectionHelper.Finish;
|
||||
var
|
||||
minX, minY: Word;
|
||||
maxX, maxY: Word;
|
||||
procedure TRangeSelectionHelper.Finish(AX2, AY2: Word);
|
||||
begin
|
||||
frmMain.UnregisterSelectionListener(@TileSelected);
|
||||
minX := Min(FItem1.X, FItem2.X);
|
||||
minY := Min(FItem1.Y, FItem2.Y);
|
||||
maxX := Max(FItem1.X, FItem2.X);
|
||||
maxY := Max(FItem1.Y, FItem2.Y);
|
||||
FCallback(minX, minY, maxX, maxY);
|
||||
Free; //We use this class only once, so it can cleanup after itself
|
||||
FCallback(Min(FX1, AX2), Min(FY1, AY2), Max(FX1, AX2), Max(FY1, AY2));
|
||||
if ActiveHelper = Self then
|
||||
ActiveHelper := nil;
|
||||
Free;
|
||||
end;
|
||||
|
||||
procedure TRangeSelectionHelper.Run;
|
||||
begin
|
||||
//TODO show indicator and option to cancel
|
||||
//TODO keep track of instance (global variable maybe?)
|
||||
frmMain.RegisterSelectionListener(@TileSelected);
|
||||
frmMain.SwitchToSelection;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
+3
-3
@@ -729,7 +729,7 @@ var
|
||||
tileX, tileY, newX, newY: Word;
|
||||
targetBlocks: TBlockInfoList;
|
||||
targetTile: TWorldItem;
|
||||
selectionListener: TSelectionListener;
|
||||
listenerIndex: Integer;
|
||||
begin
|
||||
Logger.EnterMethod([lcClient, lcDebug], 'MouseUp');
|
||||
if Button <> mbLeft then
|
||||
@@ -757,9 +757,9 @@ begin
|
||||
mnuGrabTileIDClick(nil);
|
||||
end;
|
||||
|
||||
for selectionListener in FSelectionListeners do
|
||||
for listenerIndex := FSelectionListeners.Count - 1 downto 0 do
|
||||
begin
|
||||
selectionListener(CurrentTile);
|
||||
FSelectionListeners[listenerIndex](CurrentTile);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user