Debian Codegraph @

src/base/castleinternalclassutils.pas

raw
1 {
2 Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijański.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Internal utilities. }
17 unit CastleInternalClassUtils;
18
19 interface
20
21 uses SysUtils, Classes, Generics.Collections;
22
23 type
24 TComponentEvent = procedure(const Component: TComponent) of object;
25
26 TCastleComponentNotification = class(TComponent)
27 private
28 FEventList: {$ifdef FPC}specialize{$endif} TList<TComponentEvent>;
29 FFilterList: {$ifdef FPC}specialize{$endif} TList<TComponentClass>;
30 protected
31 procedure Notification(AComponent: TComponent; Operation: TOperation); override;
32
33 public
34 { Adds callback for without class filter so you will get notifications for
35 all classes (the same like AddNotification(AEvent, TComponent)) }
36 procedure AddNotification(const AEvent: TComponentEvent); overload;
37 { Adds callback and gives you choise about what classes you want be notified
38 e.g. AddNotification(AEvent, TCastleBehavior); }
39 procedure AddNotification(const AEvent: TComponentEvent;
40 const ClassFilter: TComponentClass); overload;
41 { Removes all occurrences of the AEvent }
42 procedure RemoveNotification(const AEvent: TComponentEvent); overload;
43 { Removes only occurrences of the AEvent with specified ClassFilter }
44 procedure RemoveNotification(const AEvent: TComponentEvent;
45 const ClassFilter: TComponentClass); overload;
46
47 { Procedure to send notification }
48 procedure Notify(const AComponent: TComponent);
49
50 destructor Destroy; override;
51 end;
52
53 { Raised when trying add event callback to TCastleComponentNotification that
54 parent is not TComponent }
55 ECastleComponentNotification = class (Exception);
56
57 implementation
58
59 { TCastleComponentNotification -------------------------------------------------------- }
60
61 procedure TCastleComponentNotification.Notification(AComponent: TComponent;
62 Operation: TOperation);
63 var
64 I: Integer;
65 begin
66 inherited Notification(AComponent, Operation);
67
68 if Operation = opRemove then
69 begin
70 for I := FEventList.Count - 1 downto 0 do
71 begin
72 if TComponent(TMethod(FEventList[I]).Data) = AComponent then
73 FEventList.Delete(I);
74 end;
75 end;
76 end;
77
78 procedure TCastleComponentNotification.AddNotification(const AEvent: TComponentEvent);
79 begin
80 AddNotification(AEvent, TComponent);
81 end;
82
83 procedure TCastleComponentNotification.AddNotification(
84 const AEvent: TComponentEvent; const ClassFilter: TComponentClass);
85 var
86 EventComponent: TComponent;
87 begin
88 EventComponent := TObject(TMethod(AEvent).Data) as TComponent;
89
90 if EventComponent = nil then
91 raise ECastleComponentNotification.Create(
92 'You can only add callbacks form TComponent instances to TCastleComponentNotification.');
93
94 if FEventList = nil then
95 begin
96 FEventList := {$ifdef FPC}specialize{$endif} TList<TComponentEvent>.Create;
97 FFilterList := {$ifdef FPC}specialize{$endif} TList<TComponentClass>.Create;
98 end;
99
100 FEventList.Add(AEvent);
101 FFilterList.Add(ClassFilter);
102 EventComponent.FreeNotification(Self);
103 end;
104
105 procedure TCastleComponentNotification.RemoveNotification(
106 const AEvent: TComponentEvent);
107 var
108 EventComponent: TComponent;
109 Index: Integer;
110 begin
111 if FEventList = nil then
112 Exit;
113
114 EventComponent := TObject(TMethod(AEvent).Data) as TComponent;
115 if EventComponent = nil then
116 Exit;
117
118 Index := FEventList.IndexOf(AEvent);
119 while Index <> -1 do
120 begin
121 FEventList.Delete(Index);
122 FFilterList.Delete(Index);
123
124 Index := FEventList.IndexOf(AEvent);
125 end;
126 EventComponent.RemoveFreeNotification(Self);
127 end;
128
129 procedure TCastleComponentNotification.RemoveNotification(
130 const AEvent: TComponentEvent; const ClassFilter: TComponentClass);
131 var
132 EventComponent: TComponent;
133 I: Integer;
134 begin
135 if FEventList = nil then
136 Exit;
137
138 EventComponent := TObject(TMethod(AEvent).Data) as TComponent;
139 if EventComponent = nil then
140 Exit;
141
142 for I := FEventList.Count - 1 downto 0 do
143 begin
144 if TComponent(TMethod(FEventList[I]).Data) is ClassFilter then
145 begin
146 FEventList.Delete(I);
147 FFilterList.Delete(I);
148 end;
149 end;
150
151 if FEventList.IndexOf(AEvent) < 0 then
152 EventComponent.RemoveFreeNotification(Self);
153 end;
154
155 procedure TCastleComponentNotification.Notify(const AComponent: TComponent);
156 var
157 I: Integer;
158 begin
159 for I := 0 to FEventList.Count -1 do
160 begin
161 if AComponent is FFilterList[I] then
162 FEventList[I](AComponent);
163 end;
164 end;
165
166 destructor TCastleComponentNotification.Destroy;
167 var
168 I: integer;
169 begin
170 for I := 0 to FEventList.Count - 1 do
171 TComponent(TMethod(FEventList[i]).Data).RemoveFreeNotification(Self);
172
173 FreeAndNil(FEventList);
174 FreeAndNil(FFilterList);
175 inherited Destroy;
176 end;
177
178 end.