Files
CentrED/Client/USelectionHelper.pas
aksdb 997aa4991a 🚨 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.
2026-11-15 22:06:51 +01:00

112 lines
2.6 KiB
ObjectPascal

(*
* CDDL HEADER START
*
* 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.
*
* 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.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying * information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2015 Andreas Schneider
*)
unit USelectionHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TSelectedRangeCallback = procedure(AX1, AY1, AX2, AY2: Word) of object;
procedure SelectRange(ACallback: TSelectedRangeCallback);
implementation
uses
UfrmMain, UWorldItem, math;
type
{ TRangeSelectionHelper }
TRangeSelectionHelper = class
constructor Create(ACallback: TSelectedRangeCallback);
destructor Destroy; override;
protected
FCallback: TSelectedRangeCallback;
FHasFirst: Boolean;
FX1, FY1: Word;
procedure TileSelected(AWorldItem: TWorldItem);
procedure Finish(AX2, AY2: Word);
public
procedure Run;
end;
var
ActiveHelper: TRangeSelectionHelper = nil;
procedure SelectRange(ACallback: TSelectedRangeCallback);
begin
FreeAndNil(ActiveHelper);
ActiveHelper := TRangeSelectionHelper.Create(ACallback);
ActiveHelper.Run;
end;
{ TRangeSelectionHelper }
constructor TRangeSelectionHelper.Create(ACallback: TSelectedRangeCallback);
begin
FCallback := ACallback;
FHasFirst := False;
end;
destructor TRangeSelectionHelper.Destroy;
begin
if frmMain <> nil then
frmMain.UnregisterSelectionListener(@TileSelected);
inherited Destroy;
end;
procedure TRangeSelectionHelper.TileSelected(AWorldItem: TWorldItem);
begin
if not FHasFirst then
begin
FHasFirst := True;
FX1 := AWorldItem.X;
FY1 := AWorldItem.Y;
end else
Finish(AWorldItem.X, AWorldItem.Y);
end;
procedure TRangeSelectionHelper.Finish(AX2, AY2: Word);
begin
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
frmMain.RegisterSelectionListener(@TileSelected);
frmMain.SwitchToSelection;
end;
end.