Debian Codegraph @

src/scene/x3d/castlefields_x3devent.inc

raw
1 {%MainUnit x3dfields.pas}
2 {
3 Copyright 2007-2018 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 { X3D event. }
20 TX3DEvent = class(TX3DFieldOrEvent)
21 strict private
22 type
23 TX3DEventReceiveList = class({$ifdef FPC}specialize{$endif} TList<TX3DEventReceive>)
24 public
25 { Call all functions. }
26 procedure ExecuteAll(const Event: TX3DEvent; const Value: TX3DField;
27 const Time: TX3DTime);
28 end;
29
30 var
31 FFieldClass: TX3DFieldClass;
32 FInEvent: boolean;
33 { To avoid allocating memory for FNotifications list in the most-common case
34 when 0 or 1 notification is enough, store 1st notification in special
35 field instead. }
36 FFirstNotification: TX3DEventReceive;
37 FNotifications: TX3DEventReceiveList;
38 FParentExposedField: TX3DField;
39 Temp: TX3DField;
40 TempUsed: boolean;
41
42 SendLevel: Cardinal;
43 NotificationsToRemove: TX3DEventReceiveList;
44
45 procedure RemoveNotificationNow(const Notification: TX3DEventReceive);
46 public
47 constructor Create(AParentNode: TX3DFileItem;
48 const AName: string;
49 const AFieldClass: TX3DFieldClass; const AInEvent: boolean);
50 destructor Destroy; override;
51
52 { Internal helper utility for SendXxx implementations. @exclude }
53 function CreateTemp(const PartialSend: TPartialSend): TX3DField;
54 { Internal helper utility for SendXxx implementations. @exclude }
55 procedure FreeTemp(const Field: TX3DField);
56
57 property FieldClass: TX3DFieldClass read FFieldClass;
58
59 { @abstract(Is it "in" or "out" event ?) }
60 property InEvent: boolean read FInEvent;
61
62 { This only reads (optional) "IS" clause of the event, as may occur
63 in VRML nodeBodyStatement. }
64 procedure Parse(Lexer: TX3DLexer);
65
66 procedure SaveToStream(Writer: TX3DWriter); override;
67 function SaveToXml: TSaveToXmlMethod; override;
68
69 { If this event is an exposed event belonging to some field,
70 this references parent field. Otherwise it's @nil. }
71 property ParentExposedField: TX3DField
72 read FParentExposedField write FParentExposedField;
73
74 { Send a value to event. For input fields, this is used by routes, scripts
75 etc. to send an event to a field. For output fields, this is used by
76 node itself to send event to routes, scripts etc.
77
78 Field must be non-nil, of class FieldClass.
79
80 The Field instance doesn't become owned in any way by the TX3DEvent.
81 That is, it's the caller responsibility to free the Field instance
82 at any comfortable time, possibly right after calling Send.
83
84 Overloaded versions without explicit Time parameter just take time
85 from ParentNode.Scene.GetTime. If ParentNode is @nil
86 (which should not happen with normal fields within nodes)
87 or if ParentNode.Scene is @nil (which may happen only if events
88 processing is not turned on, that is TCastleSceneCore.ProcessEvents is @false)
89 then event is not send. }
90 procedure Send(Field: TX3DField; const Time: TX3DTime); overload;
91 procedure Send(Field: TX3DField); overload;
92
93 { Add notification about receiving new value. For input events,
94 these are used by node itself to listen to events send to it.
95 For output events, these are used by routes, scripts to listen
96 to events occuring.
97
98 When you handle this callback to send another X3D event,
99 you should pass around the same @code(Time) value.
100 This makes sure that X3D route loop detection mechanism works fine.
101 For example, here's how handling TimeSensor.elapsedTime_receive event
102 sends fraction_changed event:
103
104 @longCode(#
105 procedure TTimeSensorNode.EventElapsedTimeReceive(
106 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
107 var
108 Fraction: Single;
109 begin
110 if FdEnabled.Value then
111 begin
112 Fraction := ...; // doesn't matter here how this is calculated
113 // Pass "Time" parameter further by a call below
114 Eventfraction_changed.Send(Fraction, Time);
115 end;
116 end;
117 #)
118
119 All callbacks registered here are simply called by @link(Send) method. }
120 procedure AddNotification(const Notification: TX3DEventReceive);
121
122 { Remove notification (previously added by @link(AddNotification))
123 about receiving new value.
124
125 This is guaranteed to work correctly even if we're right now
126 in the middle of this event's processing (which means that we may
127 be iterating over a list of notifications).
128 The handler may be actually removed a bit later (when it's safe).
129
130 Note that adding a notification two-times makes it called two times.
131 Removing a notification removes one copy of a notification,
132 so it should then be removed two-times too. }
133 procedure RemoveNotification(const Notification: TX3DEventReceive);
134
135 { @abstract(Is anything actually listening on this events @link(Send)?)
136
137 Sometimes, even preparing a value to Send is quite time-consuming
138 (example: CoordinateInterpolator, where a long MFVec3f value
139 has to be computer). Then checking SendNeeded is useful:
140 if SendNeeded = @false, you know that there's no point in preparing
141 Value to send, because actually Send will do nothing.
142
143 For example, event out to which no ROUTE is connected, and no Script
144 can listen to it.
145
146 For now, this simply returns whether any callback is registered
147 by AddNotification. }
148 function SendNeeded: boolean;
149
150 { Assign from another event. The FieldClass and InEvent is copied.
151
152 The notifications (from @link(AddNotification))
153 and ParentExposedField are not copied, since they shouldn't
154 be copied so easily (ParentExposedField is related to hierarchy
155 of containers, copying notification would be surprising to the
156 notification listener). }
157 procedure Assign(Source: TPersistent); override;
158 end;
159
160 TX3DEventList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DEvent>)
161 strict private
162 function GetByName(const AName: string): TX3DEvent;
163 public
164 { Access event by name.
165 Raises EX3DNotFound if the given Name doesn't exist. }
166 property ByName[const AName: string]: TX3DEvent read GetByName;
167
168 { Find event index on the list. -1 if not found. }
169 function IndexOfName(const Name: string): Integer;
170 end;
171
172 {$endif read_interface}
173
174 {$ifdef read_implementation}
175
176 { TX3DEvent.TX3DEventReceiveList -------------------------------------------------- }
177
178 procedure TX3DEvent.TX3DEventReceiveList.ExecuteAll(
179 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
180 var
181 I: Integer;
182 begin
183 for I := 0 to Count - 1 do
184 Items[I](Event, Value, Time);
185 end;
186
187 { TX3DEvent ----------------------------------------------------------------- }
188
189 constructor TX3DEvent.Create(AParentNode: TX3DFileItem;
190 const AName: string; const AFieldClass: TX3DFieldClass;
191 const AInEvent: boolean);
192 begin
193 inherited Create(AParentNode, AName);
194 FFieldClass := AFieldClass;
195 FInEvent := AInEvent;
196 end;
197
198 destructor TX3DEvent.Destroy;
199 begin
200 FreeAndNil(Temp);
201 FreeAndNil(NotificationsToRemove);
202 FreeAndNil(FNotifications);
203 inherited;
204 end;
205
206 procedure TX3DEvent.Parse(Lexer: TX3DLexer);
207 begin
208 ParseIsClause(Lexer);
209 end;
210
211 procedure TX3DEvent.SaveToStream(Writer: TX3DWriter);
212 begin
213 { saves nothing. IS clauses are saved separately by SaveToStreamIsClauses }
214 end;
215
216 function TX3DEvent.SaveToXml: TSaveToXmlMethod;
217 begin
218 Result := sxNone;
219 end;
220
221 procedure TX3DEvent.Send(Field: TX3DField; const Time: TX3DTime);
222
223 procedure RemoveNotifications;
224 var
225 I: Integer;
226 begin
227 for I := 0 to NotificationsToRemove.Count - 1 do
228 RemoveNotificationNow(NotificationsToRemove.Items[I]);
229 // clear NotificationsToRemove
230 NotificationsToRemove.Count := 0;
231 end;
232
233 begin
234 Assert(Field <> nil);
235 Assert(Field is FieldClass);
236
237 Inc(SendLevel);
238 try
239 if Assigned(FFirstNotification) then
240 FFirstNotification(Self, Field, Time);
241 if FNotifications <> nil then
242 FNotifications.ExecuteAll(Self, Field, Time);
243 finally
244 Dec(SendLevel);
245 if (NotificationsToRemove <> nil) and
246 (NotificationsToRemove.Count <> 0) and
247 (SendLevel = 0) then
248 RemoveNotifications;
249 end;
250 end;
251
252 procedure TX3DEvent.Send(Field: TX3DField);
253 begin
254 if (ParentNode <> nil) and
255 (TX3DNode(ParentNode).Scene <> nil) then
256 Send(Field, TX3DNode(ParentNode).Scene.NextEventTime);
257 end;
258
259 function TX3DEvent.SendNeeded: boolean;
260 begin
261 Result :=
262 Assigned(FFirstNotification) or
263 ((FNotifications <> nil) and (FNotifications.Count <> 0));
264 end;
265
266 procedure TX3DEvent.Assign(Source: TPersistent);
267 begin
268 if Source is TX3DEvent then
269 begin
270 FieldOrEventAssignCommon(TX3DEvent(Source));
271
272 FFieldClass := TX3DEvent(Source).FieldClass;
273 FInEvent := TX3DEvent(Source).InEvent;
274 end;
275 end;
276
277 function TX3DEvent.CreateTemp(const PartialSend: TPartialSend): TX3DField;
278 begin
279 if not TempUsed then
280 begin
281 if Temp = nil then
282 Temp := FieldClass.CreateUndefined(ParentNode, false, X3DName);
283 Result := Temp;
284 TempUsed := true;
285 end else
286 Result := FieldClass.CreateUndefined(ParentNode, false, X3DName);
287 Result.InternalPartialSend := PartialSend;
288 end;
289
290 procedure TX3DEvent.FreeTemp(const Field: TX3DField);
291 begin
292 if Field = Temp then
293 TempUsed := false else
294 Field.Free;
295 end;
296
297 procedure TX3DEvent.AddNotification(const Notification: TX3DEventReceive);
298 begin
299 if not Assigned(FFirstNotification) then
300 begin
301 FFirstNotification := Notification;
302 end else
303 begin
304 { create FNotifications on-demand, as in cases of models with a large number
305 of nodes (or using prototypes a lot, which also creates a large number of nodes)
306 we have a lot of TX3DEvent instances, and they eat a significant part of memory }
307 if FNotifications = nil then
308 FNotifications := TX3DEventReceiveList.Create;
309 FNotifications.Add(Notification);
310 end;
311 end;
312
313 procedure TX3DEvent.RemoveNotificationNow(const Notification: TX3DEventReceive);
314 begin
315 { The "else" below is important to remove only the first notification present,
316 if you added multiple notifications. }
317 if SameMethods(TMethod(FFirstNotification), TMethod(Notification)) then
318 FFirstNotification := nil
319 else
320 if FNotifications <> nil then
321 FNotifications.Remove(Notification);
322 end;
323
324 procedure TX3DEvent.RemoveNotification(const Notification: TX3DEventReceive);
325 begin
326 if SendLevel = 0 then
327 begin
328 RemoveNotificationNow(Notification);
329 end else
330 begin
331 if NotificationsToRemove = nil then
332 NotificationsToRemove := TX3DEventReceiveList.Create;
333 NotificationsToRemove.Add(Notification);
334 end;
335 end;
336
337 { TX3DEventList ------------------------------------------------------------ }
338
339 function TX3DEventList.IndexOfName(const Name: string): Integer;
340 begin
341 for Result := 0 to Count - 1 do
342 if Items[Result].IsName(Name) then Exit;
343 Result := -1;
344 end;
345
346 function TX3DEventList.GetByName(const AName: string): TX3DEvent;
347 var
348 I: integer;
349 begin
350 I := IndexOfName(AName);
351 if I <> -1 then
352 Result := Items[I] else
353 raise EX3DNotFound.CreateFmt('Event name "%s" not found', [AName]);
354 end;
355
356 {$endif read_implementation}