forked from gbegreg/GBE3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GBEJoystick.pas
311 lines (280 loc) · 11 KB
/
GBEJoystick.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
unit GBEJoystick;
interface
uses
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.Layouts, GBEPlayerPosition, System.Math.Vectors, system.types,
FMX.Viewport3D, System.UITypes, FMX.Dialogs, FMX.Objects, FMX.Graphics, FMX.Ani, uGBEUtils3D;
type
TGBEJoystickType = (jtOrientation, jtDeplacement, jtOrientationDeplacement);
TGBEJoystick = class(TLayout)
private
{ Déclarations privées }
fPlayerPosition : TGBEPlayerPosition;
FPosDepartCurseur: TPointF; // Position du pointeur de souris au début du mouvement de la souris
fViewport3D : TViewport3D;
fCircle, fCircle2 : TCircle;
fSensitivity: integer;
fShowIntegrateJoystick, useJoystick, fMouseCapture : boolean;
fPoint : TPoint3D;
fJoystickType : TGBEJoystickType;
Offset: TPointF; // Décallage entre l'endroit du clic et le centre du cercle du joystick
fAcceleration : single;
procedure SetAngleDeVue(const Value: TPointF); // Modification de l'angle de vue
function GetDirection: TPoint3D;
procedure setShowIntegrateJoystick(const Value: boolean);
procedure setJoystickType(const Value : TGBEJoystickType);
function GetDirectionSidewayRight: TPoint3D;
function GetDirectionSidewayLeft: TPoint3D;
function getMouseCapture: boolean;
procedure setMouseCapture(const Value: boolean);
protected
{ Déclarations protégées }
public
{ Déclarations publiques }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
procedure MouseMove(Shift: TShiftState; X, Y: Single); override;
procedure DoMouseLeave; override;
procedure Resize; override;
procedure Paint; override;
procedure initialiserJoystick;
function OrientationKeyManagement(rightKey, leftKey, upKey, downKey, goUp, goDown, sideWayRight, sideWayLeft: boolean; sensibility, speed, maxspeed: single): single;
published
{ Déclarations publiées }
property PlayerPosition : TGBEPlayerPosition read fPlayerPosition write fPlayerPosition;
property JoystickType : TGBEJoystickType read fJoystickType write setJoystickType;
property angleDeVue : TPointF write SetAngleDeVue; // Propriété de l'angle de vue
property direction : TPoint3D read GetDirection; // Propriété de la direction
property directionSidewayRight : TPoint3D read GetDirectionSidewayRight;
property directionSidewayLeft : TPoint3D read GetDirectionSidewayLeft;
property deplacement : TPoint3D read fPoint write fPoint;
property HitTest default true;
property Viewport3D : TViewport3D read fViewport3D write fViewport3D;
property ShowIntegrateJoystick : boolean read fShowIntegrateJoystick write setShowIntegrateJoystick;
property Acceleration : single read fAcceleration write fAcceleration;
property Sensitivity : integer read fSensitivity write fSensitivity;
property MouseCapture : boolean read getMouseCapture write setMouseCapture;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('GBE3D', [TGBEJoystick]);
end;
{ TGBEJoystick }
constructor TGBEJoystick.Create(AOwner: TComponent);
begin
inherited;
hitTest := true;
fCircle := TCircle.Create(nil);
fCircle.Parent := self;
fCircle.Stored := false;
fCircle.Locked := true;
fCircle.Fill.Kind := TBrushKind.Gradient;
fCircle.Fill.Gradient.Color := $FFB6B6B6;
fCircle.Fill.Gradient.Color1 := $FF888888;
fCircle.Fill.Gradient.Style := TGradientStyle.Linear;
fCircle.HitTest := false;
fCircle2 := TCircle.Create(nil);
fCircle2.Parent := fCircle;
fCircle2.Stored := false;
fCircle2.Locked := true;
fCircle2.Fill.Kind := TBrushKind.Gradient;
fCircle2.Fill.Gradient.Color := $FF888888;
fCircle2.Fill.Gradient.Color1 := $FFB6B6B6;
fCircle2.Fill.Gradient.Style := TGradientStyle.Linear;
fCircle.Stroke.Thickness := 2;
fCircle2.width := fCircle.Width -20;
fCircle2.height := fCircle.height - 20;
fCircle2.position.X := (fCircle.Width - fCircle2.Width)/2 ;
fCircle2.position.Y := (fCircle.Height - fCircle2.Height)/2;
fCircle2.HitTest := false;
fCircle2.Opacity := 0.7;
fShowIntegrateJoystick := true;
fSensitivity := 90;
fCircle.Align := TAlignLayout.Client;
fPoint := Point3D(1,0,1);
fAcceleration := 0;
useJoystick := false;
fMouseCapture := false;
fJoystickType := TGBEJoystickType.jtDeplacement;
end;
function TGBEJoystick.GetDirection: TPoint3D;
begin
if (fJoystickType = jtDeplacement) or (fJoystickType = jtOrientationDeplacement) then
begin
if assigned(fPlayerPosition) then
begin
result := fPoint * (fPlayerPosition.getPositionDirection.AbsolutePosition - fPlayerPosition.AbsolutePosition).Normalize;
end
else result := fPoint;
end
else result := Point3D(0,0,0);
end;
function TGBEJoystick.GetDirectionSidewayRight: TPoint3D;
begin
result := getDirection.Rotate(Point3D(0,1,0), -Pi * 0.5);
// if (fJoystickType = jtDeplacement) or (fJoystickType = jtOrientationDeplacement) then
// begin
// if assigned(fPlayerPosition) then
// begin
// result := fPoint * (fPlayerPosition.getSidewayRightDirection.AbsolutePosition - fPlayerPosition.AbsolutePosition).Normalize;
// end
// else result := fPoint;
// end
// else result := Point3D(0,0,0);
end;
function TGBEJoystick.getMouseCapture: boolean;
begin
result := fMouseCapture;
end;
function TGBEJoystick.GetDirectionSidewayLeft: TPoint3D;
begin
result := getDirection.Rotate(Point3D(0,1,0), Pi * 0.5);
// if (fJoystickType = jtDeplacement) or (fJoystickType = jtOrientationDeplacement) then
// begin
// if assigned(fPlayerPosition) then
// begin
// result := fPoint * (fPlayerPosition.getSidewayLeftDirection.AbsolutePosition - fPlayerPosition.AbsolutePosition).Normalize;
// end
// else result := fPoint;
// end
// else result := Point3D(0,0,0);
end;
procedure TGBEJoystick.initialiserJoystick;
begin
useJoystick := false;
TAnimator.AnimateFloat(fCircle2, 'Position.X', (fCircle.Width - fCircle2.Width)/2);
TAnimator.AnimateFloat(fCircle2, 'Position.Y', (fCircle.Height - fCircle2.Height)/2);
if (fJoystickType = jtDeplacement) or (fJoystickType = jtOrientationDeplacement) then fAcceleration := 0;
end;
procedure TGBEJoystick.SetAngleDeVue(const Value: TPointF);
var
ptA,ptD,S : TPointF; // ptA point d'arrivé, ptD point de départ, S la sensibilité
begin
if assigned(fPlayerPosition) then
begin
if assigned(fViewport3D) then
begin
S.X := fSensitivity / self.Width; // Réglage de la sensibilité pour l'orientation droite/gauche
S.Y := fSensitivity / self.Height;// Réglage de la sensibilité pour l'orientation haut/bas
ptA := Value * S; // Point d'arrivée adapté à la sensibilité
ptD := fPosDepartCurseur * S; // Point de départ adapté à la sensibilité
// Vue droite/gauche
with fPlayerPosition.RotationAngle do y := y + (ptA.X - ptD.X); // orientation droite/gauche (axe y) en fonction du déplacement de la souris en X
// Vue Haut/Bas
with fPlayerPosition.getDummyOrientation.RotationAngle do x:= x + (ptD.Y - ptA.Y); // de même pour l'orientation haut/bas en adaptant (rotation sur l'axe x, e fonction du d'déplacement de la souris en Y
fPosDepartCurseur := Value; // la position du curseur lorsque l'utilisateur a cliqué (l'origine de la direction), est mis à jour avec la nouvelle position du curseur : au prochain appel de OnMouseMove, la position de départ doit être la position d'arrivée du coup précédent
end;
end;
end;
procedure TGBEJoystick.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
inherited;
if ssLeft in shift then
begin
fPosDepartCurseur := PointF(X,Y);
useJoystick := true;
end;
Offset.X := X;
Offset.Y := Y;
end;
procedure TGBEJoystick.DoMouseLeave;
begin
inherited;
initialiserJoystick;
end;
procedure TGBEJoystick.MouseMove(Shift: TShiftState; X, Y: Single);
begin
inherited;
if ssLeft in shift then
begin
if (Viewport3D <> nil) and (PlayerPosition <> nil) then
begin
if (fJoystickType = jtOrientation) or (fJoystickType = jtOrientationDeplacement) then angleDeVue := PointF(X,Y);
fCircle2.Position.X := x - offset.x;
fCircle2.Position.y := Y - offset.y;
interactionIHM(Viewport3D);
end;
end;
end;
procedure TGBEJoystick.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
begin
inherited;
initialiserJoystick;
end;
procedure TGBEJoystick.Paint;
begin
inherited;
// if useJoystick then
// begin
// if (fJoystickType = jtDeplacement) or (fJoystickType = jtOrientationDeplacement) then
// begin
// if assigned(fPlayerPosition) then
// begin
//// FAcceleration := FAcceleration + ((fCircle.Height - fCircle2.Height)*0.5 + fCircle2.position.Y) / Sensitivity;
//// fPlayerPosition.RotationAngle.Y := fPlayerPosition.RotationAngle.Y - ((fCircle.Width - fCircle2.Width)*0.5 - fCircle2.Position.X) / Sensitivity;
// end;
// end;
// end;
end;
procedure TGBEJoystick.Resize;
begin
inherited;
fCircle2.width := fCircle.Width -20;
fCircle2.height := fCircle.height -20;
fCircle2.position.X := (fCircle.Width - fCircle2.Width)*0.5;
fCircle2.position.Y := (fCircle.Height - fCircle2.Height)*0.5;
end;
procedure TGBEJoystick.setJoystickType(const Value: TGBEJoystickType);
begin
fJoystickType := value;
case value of
jtOrientation: begin // A améliorer
end;
jtDeplacement: begin // A améliorer
end;
jtOrientationDeplacement: begin // A améliorer
end;
end;
end;
procedure TGBEJoystick.setMouseCapture(const Value: boolean);
begin
if value <> fMouseCapture then begin
fMouseCapture := value;
AutoCapture := value;
end;
end;
procedure TGBEJoystick.setShowIntegrateJoystick(
const Value: boolean);
begin
fShowIntegrateJoystick := Value;
fCircle.Visible := fShowIntegrateJoystick;
fCircle2.Visible := fShowIntegrateJoystick;
end;
destructor TGBEJoystick.Destroy;
begin
DoDeleteChildren;
inherited;
end;
function TGBEJoystick.OrientationKeyManagement(rightKey, leftKey, upKey, downKey, goUp, goDown, sideWayRight, sideWayLeft : boolean; sensibility, speed, maxspeed : single):single;
begin
if assigned(PlayerPosition) then begin
if rightKey then PlayerPosition.RotationAngle.Y := PlayerPosition.RotationAngle.Y + sensibility;
if leftKey then PlayerPosition.RotationAngle.Y := PlayerPosition.RotationAngle.Y - sensibility;
if goUp then PlayerPosition.getDummyOrientation.RotationAngle.X := PlayerPosition.getDummyOrientation.RotationAngle.X + sensibility;
if goDown then PlayerPosition.getDummyOrientation.RotationAngle.X := PlayerPosition.getDummyOrientation.RotationAngle.X - sensibility;
if upKey or sideWayRight or sideWayLeft then begin
if speed > -maxSpeed then speed := speed - sensibility
else speed := -maxSpeed;
end;
if downKey then begin
if speed < maxSpeed then speed := speed + sensibility
else speed := maxSpeed;
end;
end;
result := speed;
end;
end.