2 |
2 |
3 unit uTouch; |
3 unit uTouch; |
4 |
4 |
5 interface |
5 interface |
6 |
6 |
7 uses sysutils, math, uConsole, uVariables, SDLh, uTypes, uFloat, uConsts, GLUnit; |
7 uses sysutils, math, uConsole, uVariables, SDLh, uTypes, uFloat, uConsts, uIO, GLUnit; |
8 |
8 |
9 procedure initModule; |
9 procedure initModule; |
10 |
10 |
11 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId); |
11 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId); |
12 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId); |
12 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId); |
13 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId); |
13 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId); |
14 function convertToCursor(scale: LongInt; xy: LongInt): LongInt; |
14 function convertToCursor(scale: LongInt; xy: LongInt): LongInt; |
15 procedure addFinger(id: SDL_FingerId); |
15 procedure addFinger(x,y: Longword; id: SDL_FingerId); |
16 procedure deleteFinger(id: SDL_FingerId); |
16 procedure deleteFinger(id: SDL_FingerId); |
17 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId); |
17 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId); |
18 |
18 |
|
19 function fingerHasMoved(id: SDL_FingerId): boolean; |
19 function calculateDelta(id1, id2: SDL_FingerId): hwFloat; |
20 function calculateDelta(id1, id2: SDL_FingerId): hwFloat; |
20 function getSecondPointer(id: SDL_FingerId): SDL_FingerId; |
21 function getSecondPointer(id: SDL_FingerId): SDL_FingerId; |
21 implementation |
22 implementation |
22 |
23 |
23 const |
24 const |
26 var |
27 var |
27 pointerCount : Longword; |
28 pointerCount : Longword; |
28 xyCoord : array of LongInt; |
29 xyCoord : array of LongInt; |
29 pointerIds : array of SDL_FingerId; |
30 pointerIds : array of SDL_FingerId; |
30 timeSinceDown: array of Longword; |
31 timeSinceDown: array of Longword; |
31 |
32 historicalXY : array of LongInt; |
32 //Pinch to zoom |
33 //Pinch to zoom |
33 pinchSize : hwFloat; |
34 pinchSize : hwFloat; |
34 baseZoomValue: GLFloat; |
35 baseZoomValue: GLFloat; |
35 |
36 |
|
37 invertCursor : boolean; |
|
38 |
36 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId); |
39 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId); |
37 begin |
40 begin |
38 WriteToConsole('down'); |
41 WriteToConsole('down'); |
39 addFinger(pointerId); |
42 addFinger(x,y,pointerId); |
40 xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x); |
43 xyCoord[pointerId*2] := convertToCursor(cScreenWidth,x); |
41 xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y); |
44 xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight,y); |
42 |
45 |
43 case pointerCount of |
46 case pointerCount of |
44 2: |
47 2: |
58 xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight, y); |
61 xyCoord[pointerId*2+1] := convertToCursor(cScreenHeight, y); |
59 |
62 |
60 case pointerCount of |
63 case pointerCount of |
61 1: |
64 1: |
62 begin |
65 begin |
63 CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx); |
66 if invertCursor then |
64 CursorPoint.Y := CursorPoint.Y + convertToCursor(cScreenWidth,dy); |
67 begin |
|
68 CursorPoint.X := CursorPoint.X - convertToCursor(cScreenWidth,dx); |
|
69 CursorPoint.Y := CursorPoint.Y + convertToCursor(cScreenWidth,dy); |
|
70 end |
|
71 else |
|
72 begin |
|
73 CursorPoint.X := CursorPoint.X + convertToCursor(cScreenWidth,dx); |
|
74 CursorPoint.Y := CursorPoint.Y - convertToCursor(cScreenWidth,dy); |
|
75 end; |
65 end; |
76 end; |
66 2: |
77 2: |
67 begin |
78 begin |
68 secondId := getSecondPointer(pointerId); |
79 secondId := getSecondPointer(pointerId); |
69 currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize; |
80 currentPinchDelta := calculateDelta(pointerId, secondId) - pinchSize; |
82 deleteFinger(pointerId); |
93 deleteFinger(pointerId); |
83 end; |
94 end; |
84 |
95 |
85 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId); |
96 procedure onTouchClick(x,y: Longword; pointerId: SDL_FingerId); |
86 begin |
97 begin |
87 WriteToConsole(Format('clicker %d', [SDL_GetTicks])); |
98 if bShowAmmoMenu then |
88 bShowAmmoMenu := not(bShowAmmoMenu); |
99 begin |
|
100 doPut(CursorPoint.X, CursorPoint.Y, false); |
|
101 invertCursor := true; |
|
102 exit |
|
103 end; |
|
104 |
|
105 |
|
106 bShowAmmoMenu := true; |
|
107 invertCursor := false; |
|
108 //WriteToConsole(Format('%s, %s : %d, %d', [cstr(CurrentHedgehog^.Gear^.X), cstr(CurrentHedgehog^.Gear^.Y), x-WorldDX, y-WorldDY])); |
89 end; |
109 end; |
90 |
110 |
91 function convertToCursor(scale: LongInt; xy: LongInt): LongInt; |
111 function convertToCursor(scale: LongInt; xy: LongInt): LongInt; |
92 begin |
112 begin |
93 convertToCursor := round(xy/32768*scale) |
113 convertToCursor := round(xy/32768*scale) |
94 end; |
114 end; |
95 |
115 |
96 procedure addFinger(id: SDL_FingerId); |
116 procedure addFinger(x,y: Longword; id: SDL_FingerId); |
97 var |
117 var |
98 index, tmp: Longword; |
118 index, tmp: Longword; |
99 begin |
119 begin |
100 pointerCount := pointerCount + 1; |
120 pointerCount := pointerCount + 1; |
101 |
121 |
102 //Check array sizes |
122 //Check array sizes |
103 if length(pointerIds) < pointerCount then setLength(pointerIds, length(pointerIds)*2); |
123 if length(pointerIds) < pointerCount then setLength(pointerIds, length(pointerIds)*2); |
104 if length(xyCoord) < pointerCount*2+1 then setLength(xyCoord, length(xyCoord)*2); |
124 if length(xyCoord) < id*2+1 then |
|
125 begin |
|
126 setLength(xyCoord, id*2+1); |
|
127 setLength(historicalXY, id*2+1); |
|
128 end; |
105 if length(timeSinceDown) < id then setLength(timeSinceDown, id); |
129 if length(timeSinceDown) < id then setLength(timeSinceDown, id); |
106 |
|
107 for index := 0 to pointerCount do //place the pointer ids as far back to the left as possible |
130 for index := 0 to pointerCount do //place the pointer ids as far back to the left as possible |
108 begin |
131 begin |
109 if pointerIds[index] = -1 then |
132 if pointerIds[index] = -1 then |
110 begin |
133 begin |
111 pointerIds[index] := id; |
134 pointerIds[index] := id; |
112 break; |
135 break; |
113 end; |
136 end; |
114 end; |
137 end; |
115 //set timestamp |
138 //set timestamp |
116 timeSinceDown[id] := SDL_GetTicks |
139 timeSinceDown[id] := SDL_GetTicks; |
|
140 historicalXY[id*2] := convertToCursor(cScreenWidth,x); |
|
141 historicalXY[id*2+1] := convertToCursor(cScreenHeight,y); |
117 end; |
142 end; |
118 |
143 |
119 procedure deleteFinger(id: SDL_FingerId); |
144 procedure deleteFinger(id: SDL_FingerId); |
120 var |
145 var |
121 index, i : Longint; |
146 index, i : Longint; |
136 begin |
161 begin |
137 pointerIds[index] := pointerIds[i]; |
162 pointerIds[index] := pointerIds[i]; |
138 break; |
163 break; |
139 end; |
164 end; |
140 end; |
165 end; |
141 if (SDL_GetTicks - timeSinceDown[id]) < clickTime then onTouchClick(xyCoord[id*2], xyCoord[id*2+1], id); |
166 if ((SDL_GetTicks - timeSinceDown[id]) < clickTime) AND not(fingerHasMoved(id)) then onTouchClick(xyCoord[id*2], xyCoord[id*2+1], id); |
|
167 end; |
|
168 |
|
169 //Method to calculate the distance this finger has moved since the downEvent |
|
170 function fingerHasMoved(id: SDL_FingerId): boolean; |
|
171 begin |
|
172 // fingerHasMoved := hwAbs(DistanceI(xyCoord[id*2]-historicalXY[id*2], xyCoord[id*2+1]-historicalXY[id*2+1])) > int2hwFloat(2000); // is 1% movement |
|
173 fingerHasMoved := trunc(sqrt(Power(xyCoord[id*2]-historicalXY[id*2],2) + Power(xyCoord[id*2+1]-historicalXY[id*2+1], 2))) > 330; |
142 end; |
174 end; |
143 |
175 |
144 function calculateDelta(id1, id2: SDL_FingerId): hwFloat; |
176 function calculateDelta(id1, id2: SDL_FingerId): hwFloat; |
145 begin |
177 begin |
146 // calculateDelta := Distance(xyCoord[id2*2] - xyCoord[id1*2], xyCoord[id2*2+1] - xyCoord[id1*2+1]); |
178 // calculateDelta := Distance(xyCoord[id2*2] - xyCoord[id1*2], xyCoord[id2*2+1] - xyCoord[id1*2+1]); |
159 var |
191 var |
160 index: Longword; |
192 index: Longword; |
161 begin |
193 begin |
162 setLength(xyCoord, 10); |
194 setLength(xyCoord, 10); |
163 setLength(pointerIds, 5); |
195 setLength(pointerIds, 5); |
164 setLength(timeSinceDown, 5); |
196 setLength(timeSinceDown, 5); |
|
197 setLength(historicalXY, 10); |
165 for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1; |
198 for index := Low(xyCoord) to High(xyCoord) do xyCoord[index] := -1; |
166 for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1; |
199 for index := Low(pointerIds) to High(pointerIds) do pointerIds[index] := -1; |
167 |
200 |
168 end; |
201 end; |
169 |
202 |