Debian Codegraph @

src/scene/castleviewport_touchnavigation.inc

raw
1 {%MainUnit castleviewport.pas}
2 {
3 Copyright 2013-2024 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 type
20 { Value for @link(TCastleTouchNavigation.TouchInterface). }
21 TTouchInterface = (
22 tiNone,
23 tiWalk,
24 tiWalkRotate,
25 tiFlyWalk,
26 tiPan
27 );
28
29 { Show draggable controls in the corner, to navigate
30 in the viewport comfortably on touch devices.
31
32 Depending on @link(AutoTouchInterface) and @link(TouchInterface),
33 we will show 0, 1 or 2 controls to navigate at the bottom,
34 in the left-bottom and right-bottom.
35 It is easiest to just set
36
37 @longCode(#
38 MyTouchNavigation.TouchInterface := tiNone; // default
39 MyTouchNavigation.AutoTouchInterface := ApplicationProperties.TouchDevice;
40 #)
41
42 to activate this automatically on mobile devices, leaving @code(MyTouchNavigation.TouchInterface)
43 equal tiNone on non-mobile devices.
44 Alternatively, you can do something like this to control it manually on mobile devices:
45
46 @longCode(#
47 MyTouchNavigation.AutoTouchInterface := false; // default
48 MyTouchNavigation.Exists := ApplicationProperties.TouchDevice;
49 MyTouchNavigation.TouchInterface := tiSomething;
50 #)
51
52 The size and position of this control determines where the controls appear.
53 Typically this control is added as a direct child of TCastleViewport
54 and has @link(FullSize) = @true, this way it just fills the TCastleViewport.
55
56 The @link(Viewport) determines the viewport where navigation is affected
57 by this control.
58 Typically the @link(Viewport) is also our visual parent, but it doesn't have to be.
59 }
60 TCastleTouchNavigation = class(TCastleUserInterface)
61 strict private
62 type
63 { Possible touch control UI, for @link(TCastleTouchControl.TouchMode). }
64 TCastleTouchCtlMode = (
65 ctcmWalking, ctcmWalkWithSideRot, ctcmHeadRotation, ctcmFlyUpdown, ctcmPanXY);
66 TCastleTouchPosition = (tpManual, tpLeft, tpRight);
67
68 { Touch user interface to navigate in a 3D world.
69 Shows one "lever", that can be moved up/down/left/right,
70 and controls the movement while Walking or Flying. }
71 TCastleTouchControl = class(TCastleUserInterface)
72 strict private
73 FTouchMode: TCastleTouchCtlMode;
74 FLeverOffset: TVector2;
75 FDragging: Integer; //< finger index that started drag, -1 if none
76 FPosition: TCastleTouchPosition;
77 FScale: Single;
78 function TotalScale: Single;
79 procedure SetPosition(const Value: TCastleTouchPosition);
80 procedure SetScale(const Value: Single);
81 function MaxOffsetDist: Single;
82 protected
83 procedure PreferredSize(var PreferredWidth, PreferredHeight: Single); override;
84 public
85 constructor Create(AOwner: TComponent); override;
86 procedure Render; override;
87 function Press(const Event: TInputPressRelease): boolean; override;
88 function Release(const Event: TInputPressRelease): boolean; override;
89 function Motion(const Event: TInputMotion): boolean; override;
90 procedure SetTouchMode(const Value: TCastleTouchCtlMode);
91 procedure GetSensorRotation(var X, Y, Z, Angle: Double);
92 procedure GetSensorTranslation(var X, Y, Z, Length: Double);
93 published
94 property TouchMode: TCastleTouchCtlMode
95 read FTouchMode write SetTouchMode default ctcmWalking;
96
97 { Set position of touch control. Right now this simply sets
98 the anchor using @link(TCastleUserInterface.Anchor) and friends.
99 Tip: Use @link(TCastleContainer.UIScaling) to have the anchors automatically
100 scale with screen size.
101
102 The size of the control is set to be constant physical size,
103 so it's not affected by @link(TCastleContainer.UIScaling), only by
104 @link(TCastleContainer.Dpi). }
105 property Position: TCastleTouchPosition
106 read FPosition write SetPosition default tpManual;
107
108 property Scale: Single read FScale write SetScale {$ifdef FPC}default 1{$endif};
109 end;
110
111 var
112 FViewport: TCastleViewport;
113 FLastSeenNavigationType: TNavigationType;
114 FAutoTouchInterface: Boolean;
115 FControl: array [Boolean { right side? }] of TCastleTouchControl;
116 FTouchInterface: TTouchInterface;
117 FAutoWalkTouchInterface, FAutoExamineTouchInterface: TTouchInterface;
118 FScale: Single;
119
120 { Apply current TouchInterface value. Call when TouchInterface value changed. }
121 procedure UpdateTouchInterface;
122 { Update current TouchInterface if AutoTouchInterface enabled.
123 Call when AutoTouchInterface, Viewport,
124 AutoExamineTouchInterface, AutoWalkTouchInterface, current navigation changed. }
125 procedure UpdateAutoTouchInterface;
126 procedure SetViewport(const Value: TCastleViewport);
127 procedure SetTouchInterface(const Value: TTouchInterface);
128 procedure SetAutoTouchInterface(const Value: Boolean);
129 procedure SetAutoWalkTouchInterface(const Value: TTouchInterface);
130 procedure SetAutoExamineTouchInterface(const Value: TTouchInterface);
131 function TouchInterfaceStored: Boolean;
132 procedure SetScale(const Value: Single);
133 public
134 const
135 DefaultAutoWalkTouchInterface = tiWalk;
136 DefaultAutoExamineTouchInterface = tiPan;
137 constructor Create(AOwner: TComponent); override;
138 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
139 function PropertySections(const PropertyName: String): TPropertySections; override;
140 published
141 { Viewport where navigation is affected by this control.
142
143 You must set this property to something non-nil,
144 otherwise this UI control doesn't do much, it will not affect anything. }
145 property Viewport: TCastleViewport read FViewport write SetViewport;
146
147 { Configure controls to be visible and available to navigate.
148 This automatically manages under the hood 0, 1 or 2
149 controls to navigate, placing them at suitable positions
150 and handling their operations.
151
152 Note that you can set AutoTouchInterface = @true to have this property
153 automatically adjusted. (In which case you should not set this directly.) }
154 property TouchInterface: TTouchInterface
155 read FTouchInterface write SetTouchInterface stored TouchInterfaceStored
156 default tiNone;
157
158 { Automatically adjust @link(TouchInterface) (showing / hiding proper
159 touch controls) based on the detected current @link(Viewport) navigation type. }
160 property AutoTouchInterface: boolean
161 read FAutoTouchInterface write SetAutoTouchInterface
162 default false;
163
164 { When using AutoTouchInterface = @true,
165 which touch interface should be used when walking
166 (since there are multiple sensible choices).
167 Select between tiWalkRotate or tiWalk (default).}
168 property AutoWalkTouchInterface: TTouchInterface
169 read FAutoWalkTouchInterface write SetAutoWalkTouchInterface
170 default DefaultAutoWalkTouchInterface;
171
172 { When using AutoTouchInterface = @true,
173 which touch interface should be used in examine camera
174 (since examine camera can use multi-touch gesture instead).
175 Select between tiPan (default) or tiNone.}
176 property AutoExamineTouchInterface: TTouchInterface
177 read FAutoExamineTouchInterface write SetAutoExamineTouchInterface
178 default DefaultAutoExamineTouchInterface;
179
180 { Visual scale of the device. }
181 property Scale: Single read FScale write SetScale {$ifdef FPC}default 1.0{$endif};
182
183 { Detect if mouse is currently over one of the touch control gizmos. }
184 class function MouseOverTouchGizmo(const AContainer: TCastleContainer): Boolean;
185 end;
186
187 {$endif read_interface}
188
189 {$ifdef read_implementation}
190
191 { TCastleTouchNavigation.TCastleTouchControl --------------------------------- }
192
193 constructor TCastleTouchNavigation.TCastleTouchControl.Create(AOwner: TComponent);
194 begin
195 inherited;
196 FDragging := -1;
197 FScale := 1;
198 end;
199
200 procedure TCastleTouchNavigation.TCastleTouchControl.SetScale(const Value: Single);
201 begin
202 if FScale <> Value then
203 begin
204 FScale := Value;
205 VisibleChange([chRectangle]);
206 end;
207 end;
208
209 function TCastleTouchNavigation.TCastleTouchControl.TotalScale: Single;
210 begin
211 { We used to have here
212
213 Scale * Container.Dpi / 96
214
215 and we ignored UIScale. This makes it adjust to physical size.
216 But it's not so obviously good idea -- the control could get very large
217 (obscuring view) on small devices.
218 Better to default to our standard scaling. This also allows to clearly see
219 the visual size of this on desktops.
220
221 User can always set "TouchControl.UIScaling := false; TouchControl.Scale := Container.Dpi / 96;"
222 to get this effect. }
223
224 Result := Scale * UIScale;
225 end;
226
227 procedure TCastleTouchNavigation.TCastleTouchControl.PreferredSize(var PreferredWidth, PreferredHeight: Single);
228 begin
229 inherited;
230 // do not apply UIScale here, it is already inside TotalScale
231 PreferredWidth := Theme.ImagesPersistent[tiTouchCtlOuter].Width * TotalScale;
232 PreferredHeight := Theme.ImagesPersistent[tiTouchCtlOuter].Height * TotalScale;
233 end;
234
235 procedure TCastleTouchNavigation.TCastleTouchControl.SetPosition(const Value: TCastleTouchPosition);
236 const
237 CtlBorder = 24;
238 begin
239 if FPosition <> Value then
240 begin
241 FPosition := Value;
242 case Position of
243 tpManual:
244 begin
245 Anchor(hpLeft, 0);
246 Anchor(vpBottom, 0);
247 end;
248 tpLeft:
249 begin
250 Anchor(hpLeft, CtlBorder);
251 Anchor(vpBottom, CtlBorder);
252 end;
253 tpRight:
254 begin
255 Anchor(hpRight, -CtlBorder);
256 Anchor(vpBottom, CtlBorder);
257 end;
258 end;
259 // VisibleChange([chRectangle]); // no need to call this, Anchor already called it
260 end;
261 end;
262
263 function TCastleTouchNavigation.TCastleTouchControl.MaxOffsetDist: Single;
264 begin
265 Result := TotalScale *
266 (Theme.ImagesPersistent[tiTouchCtlOuter].Width -
267 Theme.ImagesPersistent[tiTouchCtlInner].Width) / 2;
268 end;
269
270 procedure TCastleTouchNavigation.TCastleTouchControl.Render;
271 var
272 LevOffsetTrimmedX, LevOffsetTrimmedY, MaxDist: Single;
273 LeverDist: Double;
274 InnerRect: TFloatRectangle;
275 ImageInner, ImageOuter: TThemeImage;
276 SR: TFloatRectangle;
277 begin
278 inherited;
279 SR := RenderRect;
280
281 if FTouchMode = ctcmFlyUpdown then
282 begin
283 ImageInner := tiTouchCtlFlyInner;
284 ImageOuter := tiTouchCtlFlyOuter;
285 end else
286 begin
287 ImageInner := tiTouchCtlInner;
288 ImageOuter := tiTouchCtlOuter;
289 end;
290 Theme.Draw(SR, ImageOuter, UIScale);
291
292 // compute lever offset (must not move outside outer ring)
293 LeverDist := FLeverOffset.Length;
294 MaxDist := MaxOffsetDist;
295 if LeverDist <= MaxDist then
296 begin
297 LevOffsetTrimmedX := FLeverOffset[0];
298 LevOffsetTrimmedY := FLeverOffset[1];
299 end else
300 begin
301 LevOffsetTrimmedX := (FLeverOffset[0] * MaxDist) / LeverDist;
302 LevOffsetTrimmedY := (FLeverOffset[1] * MaxDist) / LeverDist;
303 end;
304 if FTouchMode = ctcmFlyUpdown then
305 LevOffsetTrimmedX := 0;
306
307 // draw lever
308 InnerRect := FloatRectangle(Theme.ImagesPersistent[ImageInner].Image.Rect); // rectangle at (0,0)
309 InnerRect.Width := InnerRect.Width * TotalScale;
310 InnerRect.Height := InnerRect.Height * TotalScale;
311 InnerRect.Left := SR.Left + (SR.Width - InnerRect.Width ) / 2 + LevOffsetTrimmedX;
312 InnerRect.Bottom := SR.Bottom + (SR.Height - InnerRect.Height) / 2 + LevOffsetTrimmedY;
313
314 Theme.Draw(InnerRect, ImageInner, UIScale);
315 end;
316
317 procedure TCastleTouchNavigation.TCastleTouchControl.SetTouchMode(const Value: TCastleTouchCtlMode);
318 begin
319 if FTouchMode <> Value then
320 begin
321 FTouchMode := Value;
322 { we may swap outer image depending on the TouchMode in some later version }
323 VisibleChange([chRectangle]);
324 end;
325 end;
326
327 function TCastleTouchNavigation.TCastleTouchControl.Press(const Event: TInputPressRelease): boolean;
328 begin
329 Result := inherited;
330 if Result or (Event.EventType <> itMouseButton) then Exit;
331
332 Result := true;
333 FDragging := Event.FingerIndex;
334 FLeverOffset := TVector2.Zero;
335 end;
336
337 function TCastleTouchNavigation.TCastleTouchControl.Release(const Event: TInputPressRelease): boolean;
338 begin
339 Result := inherited;
340 if Result or (Event.EventType <> itMouseButton) then Exit;
341
342 if FDragging = Event.FingerIndex then
343 begin
344 Result := true;
345
346 FDragging := -1;
347 FLeverOffset := TVector2.Zero;
348 VisibleChange([chRender]); { repaint with lever back in the center }
349 end;
350 end;
351
352 function TCastleTouchNavigation.TCastleTouchControl.Motion(const Event: TInputMotion): boolean;
353 begin
354 Result := inherited;
355
356 if (not Result) and (FDragging = Event.FingerIndex) then
357 begin
358 FLeverOffset := FLeverOffset + Event.Position - Event.OldPosition;
359 VisibleChange([chRender]);
360 Result := true;
361 end;
362 end;
363
364 procedure TCastleTouchNavigation.TCastleTouchControl.GetSensorRotation(var X, Y, Z, Angle: Double);
365 var
366 FxConst: Double;
367 begin
368 FxConst := 10 / MaxOffsetDist;
369 if FTouchMode = ctcmHeadRotation then
370 begin
371 X := FLeverOffset[1] * FxConst;
372 Y := -FLeverOffset[0] * FxConst;
373 Angle := Pi * 1 / 180;
374 end else
375 if FTouchMode = ctcmWalkWithSideRot then
376 begin
377 Y := -FLeverOffset[0] * FxConst;
378 Angle := Pi * 1 / 180;
379 end;
380 end;
381
382 procedure TCastleTouchNavigation.TCastleTouchControl.GetSensorTranslation(var X, Y, Z, Length: Double);
383 var
384 FxConst: Double;
385 begin
386 FxConst := 200/MaxOffsetDist;
387 case FTouchMode of
388 ctcmWalking:
389 begin
390 X := FLeverOffset[0] * FxConst / 1.5; { walking to the sides should be slower }
391 Z := -FLeverOffset[1] * FxConst;
392 Length := 20;
393 end;
394 ctcmWalkWithSideRot:
395 begin
396 Z := -FLeverOffset[1] * FxConst;
397 Length := 20;
398 end;
399 ctcmFlyUpdown:
400 begin
401 Y := FLeverOffset[1] * FxConst;
402 Length := 20;
403 end;
404 ctcmPanXY:
405 begin
406 X := -FLeverOffset[0] * FxConst;
407 Y := -FLeverOffset[1] * FxConst;
408 Length := 5;
409 end;
410 else ;
411 end;
412 end;
413
414 { TCastleTouchNavigation ----------------------------------------------------- }
415
416 constructor TCastleTouchNavigation.Create(AOwner: TComponent);
417 begin
418 inherited;
419 FAutoWalkTouchInterface := DefaultAutoWalkTouchInterface;
420 FAutoExamineTouchInterface := DefaultAutoExamineTouchInterface;
421 FScale := 1.0;
422 end;
423
424 procedure TCastleTouchNavigation.Update(const SecondsPassed: Single; var HandleInput: Boolean);
425 var
426 Tx, Ty, Tz, TLength, Rx, Ry, Rz, RAngle: Double;
427 RightSide: Boolean;
428 begin
429 inherited;
430
431 if (FControl[false] <> nil) or
432 (FControl[true] <> nil) then
433 begin
434 Tx := 0; Ty := 0; Tz := 0; TLength := 0;
435 Rx := 0; Ry := 0; Rz := 0; RAngle := 0;
436
437 for RightSide := Low(Boolean) to High(Boolean) do
438 if FControl[RightSide] <> nil then
439 begin
440 FControl[RightSide].GetSensorTranslation(Tx, Ty, Tz, TLength);
441 FControl[RightSide].GetSensorRotation(Rx, Ry, Rz, RAngle);
442 end;
443
444 {$warnings off} // ignore using deprecated Navigation, TODO: we should fix the way we pass 3D mouse events
445 if (Viewport <> nil) and
446 (Viewport.Navigation <> nil) then
447 begin
448 Viewport.Navigation.SensorTranslation(Tx, Ty, Tz, TLength, SecondsPassed);
449 Viewport.Navigation.SensorRotation(Rx, Ry, Rz, RAngle, SecondsPassed);
450 end;
451 {$warnings on}
452 end;
453
454 {$warnings off} // ignore using deprecated NavigationType
455 if (Viewport <> nil) and
456 (FLastSeenNavigationType <> TCastleAutoNavigationViewport.InternalGetNavigationType(Viewport)) then
457 begin
458 UpdateAutoTouchInterface; // will update FLastSeenNavigationType
459 end;
460 {$warnings on}
461 end;
462
463 procedure TCastleTouchNavigation.SetTouchInterface(const Value: TTouchInterface);
464 begin
465 if FTouchInterface <> Value then
466 begin
467 FTouchInterface := Value;
468 UpdateTouchInterface;
469 end;
470 end;
471
472 procedure TCastleTouchNavigation.UpdateTouchInterface;
473
474 procedure UpdateTouchController(
475 const RightSide, CtlVisible: Boolean; const Mode: TCastleTouchCtlMode);
476 var
477 NewControl: TCastleTouchControl;
478 begin
479 if FControl[RightSide] <> nil then
480 begin
481 if CtlVisible then
482 FControl[RightSide].TouchMode := Mode
483 else
484 FreeAndNil(FControl[RightSide]); // this automatically removes FControl[RightSide] from Controls list
485 end else
486 if CtlVisible then
487 begin
488 NewControl := TCastleTouchControl.Create(Self);
489 NewControl.Scale := Scale;
490 NewControl.SetTransient;
491 NewControl.TouchMode := Mode;
492 if not RightSide then
493 NewControl.Position := tpLeft
494 else
495 NewControl.Position := tpRight;
496 InsertFront(NewControl);
497 FControl[RightSide] := NewControl;
498 end;
499 end;
500
501 procedure UpdateTouchControllers(
502 const LeftVisible, RightVisible: Boolean;
503 const LeftMode: TCastleTouchCtlMode = ctcmWalking;
504 const RightMode: TCastleTouchCtlMode = ctcmWalking);
505 begin
506 UpdateTouchController(false, LeftVisible , LeftMode);
507 UpdateTouchController(true , RightVisible, RightMode);
508 end;
509
510 begin
511 case TouchInterface of
512 tiNone:
513 UpdateTouchControllers(false, false);
514 tiWalkRotate:
515 UpdateTouchControllers(true, true, ctcmWalking, ctcmHeadRotation);
516 tiWalk:
517 UpdateTouchControllers(false, true, ctcmWalking, ctcmWalking);
518 tiFlyWalk:
519 UpdateTouchControllers(true, true, ctcmFlyUpdown, ctcmWalking);
520 tiPan:
521 UpdateTouchControllers(false, true, ctcmPanXY, ctcmPanXY);
522 {$ifndef COMPILER_CASE_ANALYSIS}
523 else raise EInternalError.Create('TouchInterface?');
524 {$endif}
525 end;
526 end;
527
528 function TCastleTouchNavigation.TouchInterfaceStored: Boolean;
529 begin
530 Result := not AutoTouchInterface;
531 end;
532
533 procedure TCastleTouchNavigation.UpdateAutoTouchInterface;
534 begin
535 if (Viewport <> nil) and AutoTouchInterface then
536 begin
537 {$warnings off} // ignore using deprecated NavigationType
538 FLastSeenNavigationType := TCastleAutoNavigationViewport.InternalGetNavigationType(Viewport);
539 {$warnings on}
540 case FLastSeenNavigationType of
541 ntNone: TouchInterface := tiNone;
542 ntWalk: TouchInterface := FAutoWalkTouchInterface;
543 ntFly: TouchInterface := tiFlyWalk;
544 ntExamine: TouchInterface := FAutoExamineTouchInterface;
545 ntTurntable: TouchInterface := FAutoExamineTouchInterface;
546 // nt2D: TODO
547 {$ifndef COMPILER_CASE_ANALYSIS}
548 else raise EInternalError.Create('TCastleTouchNavigation.UpdateAutoTouchInterface not implemented for this NavigationType value');
549 {$endif}
550 end;
551 end;
552 end;
553
554 procedure TCastleTouchNavigation.SetAutoTouchInterface(const Value: Boolean);
555 begin
556 if FAutoTouchInterface <> Value then
557 begin
558 FAutoTouchInterface := Value;
559 { change TouchInterface immediately, in case we just set
560 AutoTouchInterface := true }
561 UpdateAutoTouchInterface;
562 end;
563 end;
564
565 procedure TCastleTouchNavigation.SetAutoWalkTouchInterface(const Value: TTouchInterface);
566 begin
567 if FAutoWalkTouchInterface <> Value then
568 begin
569 FAutoWalkTouchInterface := Value;
570 UpdateAutoTouchInterface;
571 end;
572 end;
573
574 procedure TCastleTouchNavigation.SetAutoExamineTouchInterface(const Value: TTouchInterface);
575 begin
576 if FAutoExamineTouchInterface <> Value then
577 begin
578 FAutoExamineTouchInterface := Value;
579 UpdateAutoTouchInterface;
580 end;
581 end;
582
583 procedure TCastleTouchNavigation.SetViewport(const Value: TCastleViewport);
584 begin
585 if FViewport <> Value then
586 begin
587 FViewport := Value;
588 UpdateAutoTouchInterface;
589 end;
590 end;
591
592 function TCastleTouchNavigation.PropertySections(
593 const PropertyName: String): TPropertySections;
594 begin
595 if (PropertyName = 'Viewport') or
596 (PropertyName = 'TouchInterface') or
597 (PropertyName = 'AutoTouchInterface') or
598 (PropertyName = 'Scale') then
599 Result := [psBasic]
600 else
601 Result := inherited PropertySections(PropertyName);
602 end;
603
604 procedure TCastleTouchNavigation.SetScale(const Value: Single);
605 var
606 RightSide: Boolean;
607 begin
608 if FScale <> Value then
609 begin
610 FScale := Value;
611 for RightSide := Low(Boolean) to High(Boolean) do
612 if FControl[RightSide] <> nil then
613 FControl[RightSide].Scale := FScale;
614 end;
615 end;
616
617 class function TCastleTouchNavigation.MouseOverTouchGizmo(
618 const AContainer: TCastleContainer): Boolean;
619 var
620 F: TCastleUserInterface;
621 begin
622 { Note that we process all AContainer.Focus, not only AContainer.FocusFront,
623 since TCastleTouchNavigation may be either in front or behind
624 TCastleWalkNavigation. }
625 for F in AContainer.Focus do
626 if F is TCastleTouchControl then
627 Exit(true);
628 Result := false;
629 end;
630
631 {$endif read_implementation}