Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
interaction [2020/05/26 11:44] – [Keyboard Interaction] admininteraction [2020/11/06 09:23] – [Input-Controller] admin
Line 1: Line 1:
 ====== Interaction ====== ====== Interaction ======
  
-In the following the implementation of mouse and keyboard interaction with default FMX methods will be described.+In the following the implementation of mousekeyboard and gesture interaction with default FMX methods will be described.
  
 In case you'll need extended features, have a look at the InputController implementation: [[inputpolling|Input Polling]] In case you'll need extended features, have a look at the InputController implementation: [[inputpolling|Input Polling]]
Line 166: Line 166:
 ===== Keyboard Interaction ===== ===== Keyboard Interaction =====
  
 +In case you'll need keyboard interaction, best performance will be reached when key events are declared in the form itself.
 +In the following example we setup a physics demo with a sphere. When cursor keys are down, an impulse will be applied to the sphere rigid body, which moves the object in space.
  
-Next step: [[android|Android]]+<file pascal> 
 +uses 
 +  [...], 
 +  Gorilla.Sphere, Gorilla.Physics; 
 +   
 +type 
 +TForm1 = class(TForm) 
 +  GorillaSphere1: TGorillaSphere; 
 +  GorillaPhysicsSystem1: TGorillaPhysicsSystem; 
 +     
 +  procedure FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; 
 +    Shift: TShiftState); 
 +   
 +  [...] 
 +end; 
 + 
 +[...] 
 +   
 +procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; 
 +  Shift: TShiftState); 
 +var LImpulse : TPoint3D; 
 +    LBody : TQ3Body; 
 +begin 
 +  if (Key = vkLEFT) then 
 +    LImpulse := TPoint3D.Create(-1, 0, 0) 
 +  else if (Key = vkRIGHT) then 
 +    LImpulse := TPoint3D.Create(1, 0, 0) 
 +  else if (Key = vkUP) then 
 +    LImpulse := TPoint3D.Create(0, 0, 1) 
 +  else if (Key = vkDOWN) then 
 +    LImpulse := TPoint3D.Create(0, 0, -1); 
 + 
 +  // apply impulse on sphere 
 +  GorillaPhysicsSystem1.RemoteBodyImpulse(GorillaSphere1, LImpulse); 
 +end; 
 +</file> 
 + 
 +The usage of form key events is very rudimentary and may reach its limits very fast. 
 + 
 +===== Gesture Interaction ===== 
 + 
 +Gesture handling is supported for Gorilla3D viewport. To allow gesture management you will need a TGestureManager component on your form and you have to link it to the TGorillaViewport component. 
 + 
 +In the example below it is described, how to rotate and zoom a model. Therefor we use the interactive gestures: ZOOM and PAN. 
 +In the //OnGesture// event of our GorillaViewport1 we setup a callback procedure to handle gesture input. Simple check for the supplied interactive gesture event: //igiZoom// or //igiPan// and execute the very simple //DoZoom// and //DoRotate// sub-routines. 
 + 
 +<file pascal> 
 +type 
 +  TForm1 = class(TForm) 
 +    GorillaViewport1: TGorillaViewport; 
 +    GestureManager1: TGestureManager; 
 +    GorillaModel1: TGorillaModel; 
 +     
 +    procedure GorillaViewport1Gesture(Sender: TObject; 
 +      const EventInfo: TGestureEventInfo; var Handled: Boolean); 
 +       
 +    protected 
 +      FLastPos : TPointF; 
 +       
 +    [...] 
 +  end; 
 +   
 +procedure TForm1.FormCreate(Sender : TObject); 
 +begin 
 +  GorillaViewport1.Touch.GestureManager := GestureManager1; 
 +  GorillaViewport1.Touch.InteractiveGestures := [Zoom, Pan]; 
 +  GorillaViewport1.OnGesture := GorillaViewport1Gesture; 
 +  [...] 
 +end; 
 + 
 +procedure TForm1.GorillaViewport1Gesture(Sender: TObject; 
 +  const EventInfo: TGestureEventInfo; var Handled: Boolean); 
 + 
 +  procedure DoZoom(); 
 +  var LScale : Single; 
 +  begin 
 +    if not (TInteractiveGestureFlag.gfBegin in EventInfo.Flags) 
 +    and not (TInteractiveGestureFlag.gfEnd in EventInfo.Flags) then 
 +    begin 
 +      LScale := EventInfo.Distance / 100; 
 +      GorillaModel1.Scale.Point := Point3D(LScale, LScale, LScale); 
 +    end; 
 +  end; 
 + 
 +  procedure DoRotate(); 
 +  var LDist : Single; 
 +      LDiff, 
 +      LNew, 
 +      LPos  : TPointF; 
 +  begin 
 +    LNew := EventInfo.Location; 
 + 
 +    if not Assigned(GorillaModel1) then 
 +    begin 
 +      FLastPos := LNew; 
 +      Exit; 
 +    end; 
 + 
 +    if not (TInteractiveGestureFlag.gfBegin in EventInfo.Flags) 
 +    and not (TInteractiveGestureFlag.gfEnd in EventInfo.Flags) then 
 +    begin 
 +      LPos := EventInfo.Location; 
 +      LDiff := FLastPos - LPos; 
 + 
 +      LDist := LDiff.X / 5; 
 + 
 +      GorillaModel1.RotationAngle.Y := GorillaModel1.RotationAngle.Y + LDist; 
 +    end; 
 + 
 +    FLastPos := LNew; 
 +  end; 
 + 
 +begin 
 +  // just a dirty and quick solution for gesture handling 
 +  case EventInfo.GestureID of 
 +    igiZoom : 
 +      begin 
 +        // zoom 
 +        DoZoom(); 
 +        Handled := true; 
 +      end; 
 + 
 +    igiPan : 
 +      begin 
 +        // rotate model 
 +        DoRotate(); 
 +        Handled := true; 
 +      end; 
 + 
 +    else 
 +      begin 
 +        Handled := false; 
 +      end; 
 +  end; 
 +end; 
 +</file> 
 +===== GamePad Interaction ===== 
 + 
 +Interacting with gamepads in Firemonkey is not possible out of the box. But of course you can build your own component. 
 + 
 +But before bothering, have a look at the TGorillaInputController of Gorilla3D, which already provides support for gamepads. It is very easy to use and allows combinations of mouse and keyboard input. 
 + 
 +Read more: [[inputpolling|Input Polling]] 
 +===== Input-Controller ===== 
 + 
 +Because most games and applications use mouse and keyboard in combination, it can be become complex very fast. It's also a bad idea to handle inputs in mainthread an reduce rendering performance by that. 
 +Besides that, it's very often a hassle to manage lots of hotkeys or input-sequences. Take a further read on the TGorillaInputController to learn how you could handle those things much easier. 
 + 
 +Read more: [[inputpolling|Input Polling]] 
 + 
 +Next step: [[charactercontrolling|CharacterControlling]]