Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
interaction [2020/05/25 11:55] – [HitTest Property] admin | interaction [2020/05/26 12:58] – [GamePad Interaction] 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 mouse, keyboard |
In case you'll need extended features, have a look at the InputController implementation: | In case you'll need extended features, have a look at the InputController implementation: | ||
Line 162: | Line 162: | ||
end; | end; | ||
</ | </ | ||
+ | |||
+ | Because setting up a bounding volume hierarchy causes some overhead data you should keep in mind to use this optimization only where it's useful. | ||
===== Keyboard Interaction ===== | ===== Keyboard Interaction ===== | ||
+ | In case you'll need keyboard interaction, | ||
+ | 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. | ||
+ | |||
+ | <file pascal> | ||
+ | uses | ||
+ | [...], | ||
+ | Gorilla.Sphere, | ||
+ | | ||
+ | type | ||
+ | TForm1 = class(TForm) | ||
+ | GorillaSphere1: | ||
+ | GorillaPhysicsSystem1: | ||
+ | | ||
+ | procedure FormKeyDown(Sender: | ||
+ | Shift: TShiftState); | ||
+ | | ||
+ | [...] | ||
+ | end; | ||
+ | |||
+ | [...] | ||
+ | | ||
+ | procedure TForm1.FormKeyDown(Sender: | ||
+ | Shift: TShiftState); | ||
+ | var LImpulse : TPoint3D; | ||
+ | LBody : TQ3Body; | ||
+ | begin | ||
+ | if (Key = vkLEFT) then | ||
+ | LImpulse := TPoint3D.Create(-1, | ||
+ | else if (Key = vkRIGHT) then | ||
+ | LImpulse := TPoint3D.Create(1, | ||
+ | else if (Key = vkUP) then | ||
+ | LImpulse := TPoint3D.Create(0, | ||
+ | else if (Key = vkDOWN) then | ||
+ | LImpulse := TPoint3D.Create(0, | ||
+ | |||
+ | // apply impulse on sphere | ||
+ | GorillaPhysicsSystem1.RemoteBodyImpulse(GorillaSphere1, | ||
+ | end; | ||
+ | </ | ||
+ | |||
+ | 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 // | ||
+ | |||
+ | <file pascal> | ||
+ | type | ||
+ | TForm1 = class(TForm) | ||
+ | GorillaViewport1: | ||
+ | GestureManager1: | ||
+ | GorillaModel1: | ||
+ | | ||
+ | procedure GorillaViewport1Gesture(Sender: | ||
+ | const EventInfo: TGestureEventInfo; | ||
+ | | ||
+ | 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: | ||
+ | const EventInfo: TGestureEventInfo; | ||
+ | |||
+ | 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, | ||
+ | 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; | ||
+ | </ | ||
+ | ===== 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, | ||
+ | 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: [[android|Android]] |