Debian Codegraph @

src/scene/x3d/x3dnodes_standard_keydevicesensor.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2008-2021 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 type for all sensor node types that operate using key devices. }
19 TAbstractKeyDeviceSensorNode = class(TAbstractSensorNode)
20 public
21 { Send key down/up events.
22
23 This is used internally by TCastleSceneCore to notify this key sensor
24 that it should generate proper X3D events.
25
26 Semantics follow TInputPressRelease behavior for EventType = itKey.
27 This means that
28
29 - Key may be keyNone (if not representable as TKey)
30
31 - KeyString may be '' (if not representable as String)
32
33 - But you cannot have both (Key=keyNone) and (KeyString='') at the same time.
34 At least one of them must contain something useful.
35
36 - key presses are affected
37 by the "key repeat" feature of the OS (windowing system actually), so when
38 user holds down a key --- we get many key down messages.
39
40 - KeyString is a UTF-8 encoded String representation of the key user pressed.
41
42 @groupBegin }
43 procedure KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime); virtual;
44 procedure KeyUp(const Key: TKey; const KeyString: String; const Time: TX3DTime); virtual;
45 { @groupEnd }
46
47 {$I auto_generated_node_helpers/x3dnodes_x3dkeydevicesensornode.inc}
48 end;
49
50 { Generate events when user key presses or releases keys on the keyboard. }
51 TKeySensorNode = class(TAbstractKeyDeviceSensorNode)
52 strict private
53 AltKey, ControlKey, ShiftKey: boolean;
54 public
55 procedure KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime); override;
56 procedure KeyUp(const Key: TKey; const KeyString: String; const Time: TX3DTime); override;
57 {$I auto_generated_node_helpers/x3dnodes_keysensor.inc}
58 end;
59
60 { Generates events as the user inputs a longer string on the keyboard,
61 useful for example to simulate "input fields" in 3D. }
62 TStringSensorNode = class(TAbstractKeyDeviceSensorNode)
63 strict private
64 EnteredText: string;
65 Active: boolean;
66 public
67 procedure KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime); override;
68 {$I auto_generated_node_helpers/x3dnodes_stringsensor.inc}
69 end;
70
71 {$endif read_interface}
72
73 {$ifdef read_implementation}
74
75 { internal utils ------------------------------------------------------------- }
76
77 { Convert TKey to X3D "action" key code.
78 As defined by X3D KeySensor node specification. }
79 function KeyToActionKey(const Key: TKey; out ActionKey: Integer): boolean;
80 begin
81 Result := true;
82 case Key of
83 keyF1 .. keyF12 : ActionKey := Ord(Key) - Ord(keyF1) + 1;
84 keyHome : ActionKey := 13;
85 keyEnd : ActionKey := 14;
86 keyPageUp : ActionKey := 15;
87 keyPageDown : ActionKey := 16;
88 keyArrowUp : ActionKey := 17;
89 keyArrowDown : ActionKey := 18;
90 keyArrowLeft : ActionKey := 19;
91 keyArrowRight : ActionKey := 20;
92 else Result := false;
93 end;
94 end;
95
96 { TAbstractKeyDeviceSensorNode ----------------------------------------------- }
97
98 procedure TAbstractKeyDeviceSensorNode.KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime);
99 begin
100 { Nothing to do in this class. }
101 end;
102
103 procedure TAbstractKeyDeviceSensorNode.KeyUp(const Key: TKey; const KeyString: String; const Time: TX3DTime);
104 begin
105 { Nothing to do in this class. }
106 end;
107
108 { TKeySensorNode ------------------------------------------------------------- }
109
110 procedure TKeySensorNode.KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime);
111
112 procedure HandleSpecial(var SpecialPressed: boolean; SpecialEvent: TSFBoolEvent);
113 begin
114 if not SpecialPressed then
115 begin
116 SpecialEvent.Send(true, Time);
117 EventIsActive.Send(true, Time);
118 SpecialPressed := true;
119 end;
120 end;
121
122 var
123 ActionKey: Integer;
124 begin
125 inherited;
126 if FdEnabled.Value then
127 begin
128 { For now, actionKeyPress and keyPress are simply always accompanied by isActive := true.
129 We ignore the trouble with key repeat, as spec doesn't say what to do
130 (suggesting that we should make event only on the actual press, but this
131 would be poor --- key repeat is useful for users).
132
133 We track ctrl / alt / shift state and avoid key repeat for them.
134 Reason: 1. for them, key repeat seems really not useful.
135 2. on Unix, windowing system (or at least GTK) doesn't even do key repeat
136 for them. So this makes Windows and Unix behave more commonly. }
137 if KeyToActionKey(Key, ActionKey) then
138 begin
139 EventActionKeyPress.Send(ActionKey, Time);
140 EventIsActive.Send(true, Time);
141 end;
142 if KeyString <> '' then
143 begin
144 EventKeyPress.Send(KeyString, Time);
145 EventIsActive.Send(true, Time);
146 end;
147 case Key of
148 keyAlt : HandleSpecial(AltKey, EventAltKey);
149 keyCtrl : HandleSpecial(ControlKey, EventControlKey);
150 keyShift: HandleSpecial(ShiftKey, EventShiftKey);
151 else ;
152 end;
153 end;
154 end;
155
156 procedure TKeySensorNode.KeyUp(const Key: TKey; const KeyString: String; const Time: TX3DTime);
157
158 procedure HandleSpecial(var SpecialPressed: boolean; SpecialEvent: TSFBoolEvent);
159 begin
160 if SpecialPressed then
161 begin
162 SpecialEvent.Send(false, Time);
163 EventIsActive.Send(false, Time);
164 SpecialPressed := false;
165 end;
166 end;
167
168 var
169 ActionKey: Integer;
170 begin
171 if FdEnabled.Value then
172 begin
173 if KeyToActionKey(Key, ActionKey) then
174 begin
175 EventActionKeyRelease.Send(ActionKey, Time);
176 EventIsActive.Send(false, Time);
177 end;
178 if KeyString <> '' then
179 begin
180 EventKeyRelease.Send(KeyString, Time);
181 EventIsActive.Send(false, Time);
182 end;
183 case Key of
184 keyAlt : HandleSpecial(AltKey, EventAltKey);
185 keyCtrl : HandleSpecial(ControlKey, EventControlKey);
186 keyShift: HandleSpecial(ShiftKey, EventShiftKey);
187 else ;
188 end;
189 end;
190 inherited;
191 end;
192
193 { TStringSensorNode ---------------------------------------------------------- }
194
195 procedure TStringSensorNode.KeyDown(const Key: TKey; const KeyString: String; const Time: TX3DTime);
196 begin
197 inherited;
198
199 if FdEnabled.Value and ( (KeyString <> '') or (Key in [keyBackSpace, keyEnter]) ) then
200 begin
201 if not Active then
202 begin
203 Active := true;
204 EventIsActive.Send(Active, Time);
205 end;
206
207 { Catch both CharBackspace and keyBackSpace.
208 Catching CharBackspace is not enough, because it's also equal to Ctrl+H.
209 Same for CharEnter: it's also equal to Ctrl+M.
210 And Ctrl+... may be often used by some menu items, see e.g. castle-model-viewer. }
211
212 if (KeyString = CharBackspace) or (Key = keyBackSpace) then
213 begin
214 if FdDeletionAllowed.Value and (EnteredText <> '') then
215 begin
216 SetLength(EnteredText, Length(EnteredText) - 1);
217 EventEnteredText.Send(EnteredText, Time);
218 end;
219 end else
220 if (KeyString = CharEnter) or (Key = keyEnter) then
221 begin
222 EventFinalText.Send(EnteredText, Time);
223 { X3D spec says explicitly that in this case, enteredText event
224 is not generated although internal value is empty. }
225 EnteredText := '';
226 Active := false;
227 EventIsActive.Send(Active, Time);
228 end else
229 begin
230 EnteredText := EnteredText + KeyString;
231 EventEnteredText.Send(EnteredText, Time);
232 end;
233 end;
234 end;
235
236 procedure RegisterKeyDeviceSensorNodes;
237 begin
238 NodesManager.RegisterNodeClasses([
239 TKeySensorNode,
240 TStringSensorNode
241 ]);
242 end;
243
244 {$endif read_implementation}