Debian Codegraph @

src/scene/x3d/x3dnodes_standard_eventutilities.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2008-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 { Base node type from which all sequencers (like @link(TIntegerSequencerNode)
19 and @link(TBooleanSequencerNode)) are derived. }
20 TAbstractSequencerNode = class(TAbstractChildNode)
21 strict private
22 procedure EventSet_FractionReceive(const Event: TX3DEvent; const Value: TX3DField;
23 const Time: TX3DTime);
24 procedure EventNextReceive(const Event: TX3DEvent; const Value: TX3DField;
25 const Time: TX3DTime);
26 procedure EventPreviousReceive(const Event: TX3DEvent; const Value: TX3DField;
27 const Time: TX3DTime);
28 strict private
29 FCurrentKeyRange: Integer;
30 strict protected
31 { Analogous to TAbstractInterpolatorNode.KeyRange. }
32 function KeyRange(const Fraction: Single): Integer;
33
34 { In what key range we currently are. In other words, which
35 keyValue was last send (passed to SendValueChanged).
36
37 We have to keep track of this information, per each node,
38 as specification clearly says that value_changed should not be
39 generated continuously on each set_fraction, but only when value
40 actually changes. (Although we make an exception for this
41 with our extension @link(ForceContinuousValue_Changed)).
42
43 Valid values here are between -1 and FdKey.Count - 1.
44 Values between 0 and FdKey.Count - 1 indicate which keyValue was
45 last send. Value -1 means that SendValueChanged was not yet called,
46 this is the default value (after construction). }
47 property CurrentKeyRange: Integer read FCurrentKeyRange;
48 procedure SetCurrentKeyRange(const Value: Integer; const Time: TX3DTime;
49 const PartialSend: TPartialSend);
50
51 { Override this is descendants to actually send value_changed event
52 witk keyValue[ValueIndex]. You are guaranteed that ValueIndex
53 is between 0 and FdKey.Count - 1, but you have to check yourself
54 whether it's also between 0 and FdKeyValue.Count - 1
55 (although spec requires that key and keyValue fields have the same size,
56 we have to protect against invalid files). }
57 procedure SendValueChanged(const ValueIndex: Integer;
58 const Time: TX3DTime; const PartialSend: TPartialSend); virtual; abstract;
59 public
60 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
61
62 {$I auto_generated_node_helpers/x3dnodes_x3dsequencernode.inc}
63 end;
64
65 { Base type from which all trigger nodes are derived. }
66 TAbstractTriggerNode = class(TAbstractChildNode)
67 public
68 {$I auto_generated_node_helpers/x3dnodes_x3dtriggernode.inc}
69 end;
70
71 { Filters boolean events, allowing for selective routing
72 of TRUE or FALSE values and negation. }
73 TBooleanFilterNode = class(TAbstractChildNode)
74 strict private
75 procedure EventSet_BooleanReceive(const Event: TX3DEvent; const Value: TX3DField;
76 const Time: TX3DTime);
77 public
78 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
79 {$I auto_generated_node_helpers/x3dnodes_booleanfilter.inc}
80 end;
81
82 { Generate sequential boolean events. }
83 TBooleanSequencerNode = class(TAbstractSequencerNode)
84 strict protected
85 procedure SendValueChanged(const ValueIndex: Integer;
86 const Time: TX3DTime; const PartialSend: TPartialSend); override;
87 public
88 {$I auto_generated_node_helpers/x3dnodes_booleansequencer.inc}
89 end;
90
91 { Stores a boolean value for toggling on/off. }
92 TBooleanToggleNode = class(TAbstractChildNode)
93 strict private
94 procedure EventSet_BooleanReceive(const Event: TX3DEvent; const Value: TX3DField;
95 const Time: TX3DTime);
96 public
97 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
98 {$I auto_generated_node_helpers/x3dnodes_booleantoggle.inc}
99 end;
100
101 { Trigger node that generates Boolean events upon receiving time events. }
102 TBooleanTriggerNode = class(TAbstractTriggerNode)
103 strict private
104 procedure EventSet_triggerTimeReceive(const Event: TX3DEvent; const Value: TX3DField;
105 const Time: TX3DTime);
106 public
107 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
108 {$I auto_generated_node_helpers/x3dnodes_booleantrigger.inc}
109 end;
110
111 { Generate sequential integer events.
112 For example, it can be driven by a TimeSensor and iterate over a series
113 of nodes by animating Switch.whichChoice. }
114 TIntegerSequencerNode = class(TAbstractSequencerNode)
115 strict protected
116 procedure SendValueChanged(const ValueIndex: Integer;
117 const Time: TX3DTime; const PartialSend: TPartialSend); override;
118 public
119 {$I auto_generated_node_helpers/x3dnodes_integersequencer.inc}
120 end;
121
122 { Generate an integer upon receiving any boolean value. }
123 TIntegerTriggerNode = class(TAbstractTriggerNode)
124 strict private
125 procedure EventSet_BooleanReceive(const Event: TX3DEvent; const Value: TX3DField;
126 const Time: TX3DTime);
127 public
128 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
129 {$I auto_generated_node_helpers/x3dnodes_integertrigger.inc}
130 end;
131
132 { Generate time events upon receiving any boolean value. }
133 TTimeTriggerNode = class(TAbstractTriggerNode)
134 strict private
135 procedure EventSet_BooleanReceive(const Event: TX3DEvent; const Value: TX3DField;
136 const Time: TX3DTime);
137 public
138 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
139 {$I auto_generated_node_helpers/x3dnodes_timetrigger.inc}
140 end;
141
142 { Generate events upon receiving a boolean TRUE value. }
143 TValueTriggerNode = class(TAbstractTriggerNode)
144 strict private
145 procedure EventTriggerReceive(const Event: TX3DEvent; const Value: TX3DField;
146 const Time: TX3DTime);
147 public
148 constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override;
149 {$I auto_generated_node_helpers/x3dnodes_valuetrigger.inc}
150 end;
151
152 {$endif read_interface}
153
154 {$ifdef read_implementation}
155
156 { TAbstractSequencerNode ----------------------------------------------------- }
157
158 constructor TAbstractSequencerNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
159 begin
160 inherited;
161
162 FCurrentKeyRange := -1;
163
164 EventNext.AddNotification({$ifdef FPC}@{$endif} EventNextReceive);
165 EventPrevious.AddNotification({$ifdef FPC}@{$endif} EventPreviousReceive);
166 EventSet_Fraction.AddNotification({$ifdef FPC}@{$endif} EventSet_FractionReceive);
167 end;
168
169 function TAbstractSequencerNode.KeyRange(
170 const Fraction: Single): Integer;
171 var
172 T: Single;
173 begin
174 { T is simply ignored, as it's useless for sequences (since they generate
175 discrete events from float fraction, then they are not interested
176 in T value). }
177
178 Result := X3DNodes.KeyRange(FdKey.Items, Fraction, T);
179 end;
180
181 procedure TAbstractSequencerNode.SetCurrentKeyRange(const Value: Integer;
182 const Time: TX3DTime; const PartialSend: TPartialSend);
183 begin
184 if (FCurrentKeyRange <> Value) or FdForceContinuousValue_Changed.Value then
185 begin
186 Assert(Value >= -1);
187 Assert(Value < FdKey.Count);
188 FCurrentKeyRange := Value;
189 SendValueChanged(Value, Time, PartialSend);
190 end;
191 end;
192
193 procedure TAbstractSequencerNode.EventSet_FractionReceive(const Event: TX3DEvent; const Value: TX3DField;
194 const Time: TX3DTime);
195 var
196 KeyCount: Integer;
197 NewKeyRange: Integer;
198 begin
199 KeyCount := FdKey.Items.Count;
200 if KeyCount <> 0 then { for KeyCount = 0, do nothing }
201 begin
202 Assert(Value is TSFFloat);
203 Assert(not IsNan((Value as TSFFloat).Value));
204 NewKeyRange := KeyRange((Value as TSFFloat).Value);
205
206 { For NewKeyRange = 0 (before 1st key), leave it as 0.
207 For NewKeyRange = KeyCount, this is after last key, so we want
208 NewKeyRange = KeyCount - 1.
209 For other values, we're within some range, and NewKeyRange is index
210 of the right value --- we want to decrease it, so become index of the left
211 value. }
212 if NewKeyRange <> 0 then
213 Dec(NewKeyRange);
214
215 { Set CurrentKeyRange and eventually send value_changed }
216 SetCurrentKeyRange(NewKeyRange, Time, Value.InternalPartialSend);
217 end;
218 end;
219
220 procedure TAbstractSequencerNode.EventNextReceive(const Event: TX3DEvent; const Value: TX3DField;
221 const Time: TX3DTime);
222 var
223 KeyCount, NewKeyRange: Integer;
224 begin
225 KeyCount := FdKey.Items.Count;
226 if (Value as TSFBool).Value and (KeyCount <> 0) then
227 begin
228 { Note that CurrentKeyRange may be -1. Code below will still work then
229 (as CurrentKeyRange + 1 is always >= 0).
230 This also means that even when KeyCount = 1, calling "next" event
231 may be meaningfull: it will cause generation of the first keyvalue. }
232
233 NewKeyRange := CurrentKeyRange + 1;
234 if NewKeyRange = KeyCount then
235 NewKeyRange := 0;
236
237 { Set CurrentKeyRange and eventually send value_changed }
238 SetCurrentKeyRange(NewKeyRange, Time, Value.InternalPartialSend);
239 end;
240 end;
241
242 procedure TAbstractSequencerNode.EventPreviousReceive(const Event: TX3DEvent; const Value: TX3DField;
243 const Time: TX3DTime);
244 var
245 KeyCount, NewKeyRange: Integer;
246 begin
247 KeyCount := FdKey.Items.Count;
248 if (Value as TSFBool).Value and (KeyCount <> 0) then
249 begin
250 { Note that CurrentKeyRange may be -1. Code below will still work then
251 (as CurrentKeyRange - 1 is then -2, so we will jump to last item).
252 This also means that even when KeyCount = 1, calling "previous" event
253 may be meaningfull: it will cause generation of the last keyvalue. }
254
255 NewKeyRange := CurrentKeyRange - 1;
256 if NewKeyRange < 0 then
257 NewKeyRange := KeyCount - 1;
258
259 { Set CurrentKeyRange and eventually send value_changed }
260 SetCurrentKeyRange(NewKeyRange, Time, Value.InternalPartialSend);
261 end;
262 end;
263
264 { TBooleanFilterNode --------------------------------------------------------- }
265
266 constructor TBooleanFilterNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
267 begin
268 inherited;
269 FEventSet_boolean.AddNotification({$ifdef FPC}@{$endif} EventSet_BooleanReceive);
270 end;
271
272 procedure TBooleanFilterNode.EventSet_BooleanReceive(
273 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
274 var
275 B: boolean;
276 begin
277 B := (Value as TSFBool).Value;
278 if B then
279 EventInputTrue.Send(B, Time, Value.InternalPartialSend)
280 else
281 EventInputFalse.Send(B, Time, Value.InternalPartialSend);
282 EventInputNegate.Send(not B, Time, Value.InternalPartialSend);
283 end;
284
285 { TBooleanSequencerNode ------------------------------------------------------ }
286
287 procedure TBooleanSequencerNode.SendValueChanged(const ValueIndex: Integer;
288 const Time: TX3DTime; const PartialSend: TPartialSend);
289 begin
290 if FdKeyValue.Count <> FdKey.Count then
291 WritelnWarning('VRML/X3D', 'BooleanSequencer node: key and keyValue fields should have the same size');
292 if ValueIndex < FdKeyValue.Count then
293 EventValue_Changed.Send(FdKeyValue.Items.Items[ValueIndex], Time, PartialSend);
294 end;
295
296 { TBooleanToggleNode --------------------------------------------------------- }
297
298 constructor TBooleanToggleNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
299 begin
300 inherited;
301 FEventSet_boolean.AddNotification({$ifdef FPC}@{$endif} EventSet_BooleanReceive);
302 end;
303
304 procedure TBooleanToggleNode.EventSet_BooleanReceive(
305 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
306 var
307 B: boolean;
308 begin
309 B := (Value as TSFBool).Value;
310 if B then
311 FdToggle.Send(not FdToggle.Value, Value.InternalPartialSend);
312 end;
313
314 { TBooleanTriggerNode -------------------------------------------------------- }
315
316 constructor TBooleanTriggerNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
317 begin
318 inherited;
319 FEventSet_triggerTime.AddNotification({$ifdef FPC}@{$endif} EventSet_triggerTimeReceive);
320 end;
321
322 procedure TBooleanTriggerNode.EventSet_triggerTimeReceive(
323 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
324 begin
325 EventTriggerTrue.Send(true, Time, Value.InternalPartialSend);
326 end;
327
328 { TIntegerSequencerNode ------------------------------------------------------ }
329
330 procedure TIntegerSequencerNode.SendValueChanged(const ValueIndex: Integer;
331 const Time: TX3DTime; const PartialSend: TPartialSend);
332 begin
333 if FdKeyValue.Count <> FdKey.Count then
334 WritelnWarning('VRML/X3D', 'IntegerSequencer node: key and keyValue fields should have the same size');
335 if ValueIndex < FdKeyValue.Count then
336 EventValue_Changed.Send(FdKeyValue.Items.Items[ValueIndex], Time, PartialSend);
337 end;
338
339 { TIntegerTriggerNode -------------------------------------------------------- }
340
341 constructor TIntegerTriggerNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
342 begin
343 inherited;
344 FEventSet_boolean.AddNotification({$ifdef FPC}@{$endif} EventSet_booleanReceive);
345 end;
346
347 procedure TIntegerTriggerNode.EventSet_BooleanReceive(
348 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
349 begin
350 EventtriggerValue.Send(FdintegerKey.Value, Time, Value.InternalPartialSend);
351 end;
352
353 { TTimeTriggerNode ----------------------------------------------------------- }
354
355 constructor TTimeTriggerNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
356 begin
357 inherited;
358 FEventSet_boolean.AddNotification({$ifdef FPC}@{$endif} EventSet_BooleanReceive);
359 end;
360
361 procedure TTimeTriggerNode.EventSet_BooleanReceive(
362 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
363 begin
364 EventTriggerTime.Send(Time.Seconds, Time, Value.InternalPartialSend);
365 end;
366
367 { TValueTriggerNode ---------------------------------------------------------- }
368
369 constructor TValueTriggerNode.Create(const AX3DName: string = ''; const ABaseUrl: String = '');
370 begin
371 inherited;
372 FEventTrigger.AddNotification({$ifdef FPC}@{$endif} EventTriggerReceive);
373 HasInterfaceDeclarations := AllAccessTypes;
374 end;
375
376 procedure TValueTriggerNode.EventTriggerReceive(const Event: TX3DEvent; const Value: TX3DField;
377 const Time: TX3DTime);
378 var
379 B: Boolean;
380 I: Integer;
381 F: TX3DField;
382 begin
383 B := (Value as TSFBool).Value;
384 if B and Enabled then
385 begin
386 for I := 0 to FieldsCount - 1 do
387 begin
388 F := Fields[I];
389 { For "dynamic" (coming from "interface declaration") and exposed fields,
390 send an event with their current value. }
391 if (F.ParentInterfaceDeclaration <> nil) and
392 (F.EventOut <> nil) then
393 begin
394 // since we send F, it must have Value.InternalPartialSend set, to account for cross-fading
395 F.InternalPartialSend := Value.InternalPartialSend;
396 F.EventOut.Send(F, Time);
397 F.InternalPartialSend := nil;
398 end;
399 end;
400 end;
401 end;
402
403 procedure RegisterEventUtilitiesNodes;
404 begin
405 NodesManager.RegisterNodeClasses([
406 TBooleanFilterNode,
407 TBooleanSequencerNode,
408 TBooleanToggleNode,
409 TBooleanTriggerNode,
410 TIntegerSequencerNode,
411 TIntegerTriggerNode,
412 TTimeTriggerNode,
413 TValueTriggerNode
414 ]);
415 end;
416
417 {$endif read_implementation}