22 |
22 |
23 interface |
23 interface |
24 |
24 |
25 uses sysutils, math, uConsole, uVariables, SDLh, uFloat, uConsts, uIO, GLUnit, uTypes; |
25 uses sysutils, math, uConsole, uVariables, SDLh, uFloat, uConsts, uIO, GLUnit, uTypes; |
26 |
26 |
27 // TODO: this type should be Int64 |
|
28 // TODO: this type should be named TSDL_FingerId |
|
29 type SDL_FingerId = LongInt; |
|
30 |
|
31 type |
|
32 PTouch_Finger = ^Touch_Finger; |
|
33 Touch_Finger = record |
|
34 id : SDL_FingerId; |
|
35 x,y : LongInt; |
|
36 dx,dy : LongInt; |
|
37 historicalX, historicalY : LongInt; |
|
38 timeSinceDown : Longword; |
|
39 end; |
|
40 |
27 |
41 procedure initModule; |
28 procedure initModule; |
42 |
29 |
43 procedure ProcessTouch; |
30 procedure ProcessTouch; |
44 procedure onTouchDown(x,y: Longword; pointerId: SDL_FingerId); |
31 procedure onTouchDown(x,y: Longword; pointerId: TSDL_FingerId); |
45 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: SDL_FingerId); |
32 procedure onTouchMotion(x,y: Longword; dx,dy: LongInt; pointerId: TSDL_FingerId); |
46 procedure onTouchUp(x,y: Longword; pointerId: SDL_FingerId); |
33 procedure onTouchUp(x,y: Longword; pointerId: TSDL_FingerId); |
47 function convertToCursorX(x: LongInt): LongInt; |
34 function convertToCursorX(x: LongInt): LongInt; |
48 function convertToCursorY(y: LongInt): LongInt; |
35 function convertToCursorY(y: LongInt): LongInt; |
49 function convertToCursorDeltaX(x: LongInt): LongInt; |
36 function convertToCursorDeltaX(x: LongInt): LongInt; |
50 function convertToCursorDeltaY(y: LongInt): LongInt; |
37 function convertToCursorDeltaY(y: LongInt): LongInt; |
51 function addFinger(x,y: Longword; id: SDL_FingerId): PTouch_Finger; |
38 function addFinger(x,y: Longword; id: TSDL_FingerId): PTouch_Data; |
52 function updateFinger(x,y,dx,dy: Longword; id: SDL_FingerId): PTouch_Finger; |
39 function updateFinger(x,y,dx,dy: Longword; id: TSDL_FingerId): PTouch_Data; |
53 procedure deleteFinger(id: SDL_FingerId); |
40 procedure deleteFinger(id: TSDL_FingerId); |
54 procedure onTouchClick(finger: Touch_Finger); |
41 procedure onTouchClick(finger: TTouch_Data); |
55 procedure onTouchDoubleClick(finger: Touch_Finger); |
42 procedure onTouchDoubleClick(finger: TTouch_Data); |
56 |
43 |
57 function findFinger(id: SDL_FingerId): PTouch_Finger; |
44 function findFinger(id: TSDL_FingerId): PTouch_Data; |
58 procedure aim(finger: Touch_Finger); |
45 procedure aim(finger: TTouch_Data); |
59 function isOnCrosshair(finger: Touch_Finger): boolean; |
46 function isOnCrosshair(finger: TTouch_Data): boolean; |
60 function isOnCurrentHog(finger: Touch_Finger): boolean; |
47 function isOnCurrentHog(finger: TTouch_Data): boolean; |
61 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger); |
48 procedure convertToWorldCoord(var x,y: hwFloat; finger: TTouch_Data); |
62 procedure convertToFingerCoord(var x,y: hwFloat; oldX, oldY: hwFloat); |
49 procedure convertToFingerCoord(var x,y: hwFloat; oldX, oldY: hwFloat); |
63 function fingerHasMoved(finger: Touch_Finger): boolean; |
50 function fingerHasMoved(finger: TTouch_Data): boolean; |
64 function calculateDelta(finger1, finger2: Touch_Finger): hwFloat; |
51 function calculateDelta(finger1, finger2: TTouch_Data): hwFloat; |
65 function getSecondFinger(finger: Touch_Finger): PTouch_Finger; |
52 function getSecondFinger(finger: TTouch_Data): PTouch_Data; |
66 function isOnRect(widget: TOnScreenWidget; finger: Touch_Finger): boolean; |
53 function isOnRect(widget: TOnScreenWidget; finger: TTouch_Data): boolean; |
67 function isOnRect(rect: TSDL_Rect; finger: Touch_Finger): boolean; |
54 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean; |
68 procedure printFinger(finger: Touch_Finger); |
55 procedure printFinger(finger: TTouch_Data); |
69 implementation |
56 implementation |
70 |
57 |
71 const |
58 const |
72 clicktime = 200; |
59 clicktime = 200; |
73 nilFingerId = High(SDL_FingerId); |
60 nilFingerId = High(TSDL_FingerId); |
74 |
61 |
75 var |
62 var |
76 pointerCount : Longword; |
63 pointerCount : Longword; |
77 fingers: array of Touch_Finger; |
64 fingers: array of TTouch_Data; |
78 moveCursor : boolean; |
65 moveCursor : boolean; |
79 invertCursor : boolean; |
66 invertCursor : boolean; |
80 |
67 |
81 xTouchClick,yTouchClick : LongInt; |
68 xTouchClick,yTouchClick : LongInt; |
82 timeSinceClick : Longword; |
69 timeSinceClick : Longword; |
474 tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048; |
461 tmpAngle:= DeltaY / Distance(deltaX, deltaY) *_2048; |
475 targetAngle:= (hwRound(tmpAngle) + 2048) div 2; |
462 targetAngle:= (hwRound(tmpAngle) + 2048) div 2; |
476 end; //if CurrentHedgehog^.Gear <> nil |
463 end; //if CurrentHedgehog^.Gear <> nil |
477 end; |
464 end; |
478 |
465 |
479 //These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system |
466 // These 4 convertToCursor functions convert xy coords from the SDL coordinate system to our CursorPoint coor system: |
480 // the SDL coordinate system goes from 0 to 32768 on the x axis and 0 to 32768 on the y axis, (0,0) being top left. |
467 // - the SDL coordinate system goes from 0 to 32768 on the x axis and 0 to 32768 on the y axis, (0,0) being top left; |
481 // the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis |
468 // - the CursorPoint coordinate system goes from -cScreenWidth/2 to cScreenWidth/2 on the x axis |
482 // and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left, |
469 // and 0 to cScreenHeight on the x axis, (-cScreenWidth, cScreenHeight) being top left. |
483 function convertToCursorX(x: LongInt): LongInt; |
470 function convertToCursorX(x: LongInt): LongInt; |
484 begin |
471 begin |
485 convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1); |
472 convertToCursorX := round((x/32768)*cScreenWidth) - (cScreenWidth shr 1); |
486 end; |
473 end; |
487 |
474 |
524 begin |
511 begin |
525 x := oldX + int2hwFloat(WorldDx); |
512 x := oldX + int2hwFloat(WorldDx); |
526 y := int2hwFloat(cScreenHeight) - (oldY + int2hwFloat(WorldDy)); |
513 y := int2hwFloat(cScreenHeight) - (oldY + int2hwFloat(WorldDy)); |
527 end; |
514 end; |
528 |
515 |
529 procedure convertToWorldCoord(var x,y: hwFloat; finger: Touch_Finger); |
516 procedure convertToWorldCoord(var x,y: hwFloat; finger: TTouch_Data); |
530 begin |
517 begin |
531 //if x <> nil then |
518 //if x <> nil then |
532 x := int2hwFloat((finger.x-WorldDx)); |
519 x := int2hwFloat((finger.x-WorldDx)); |
533 //if y <> nil then |
520 //if y <> nil then |
534 y := int2hwFloat((cScreenHeight - finger.y)-WorldDy); |
521 y := int2hwFloat((cScreenHeight - finger.y)-WorldDy); |
535 end; |
522 end; |
536 |
523 |
537 //Method to calculate the distance this finger has moved since the downEvent |
524 //Method to calculate the distance this finger has moved since the downEvent |
538 function fingerHasMoved(finger: Touch_Finger): boolean; |
525 function fingerHasMoved(finger: TTouch_Data): boolean; |
539 begin |
526 begin |
540 fingerHasMoved := trunc(sqrt(Power(finger.X-finger.historicalX,2) + Power(finger.y-finger.historicalY, 2))) > 330; |
527 fingerHasMoved := trunc(sqrt(Power(finger.X-finger.historicalX,2) + Power(finger.y-finger.historicalY, 2))) > 330; |
541 end; |
528 end; |
542 |
529 |
543 function calculateDelta(finger1, finger2: Touch_Finger): hwFloat; inline; |
530 function calculateDelta(finger1, finger2: TTouch_Data): hwFloat; inline; |
544 begin |
531 begin |
545 calculateDelta := DistanceI(finger2.x-finger1.x, finger2.y-finger1.y); |
532 calculateDelta := DistanceI(finger2.x-finger1.x, finger2.y-finger1.y); |
546 end; |
533 end; |
547 |
534 |
548 // Under the premise that all pointer ids in pointerIds:SDL_FingerId are packed to the far left. |
535 // Under the premise that all pointer ids in pointerIds:TSDL_FingerId are packed to the far left. |
549 // If the pointer to be ignored is not pointerIds[0] the second must be there |
536 // If the pointer to be ignored is not pointerIds[0] the second must be there |
550 function getSecondFinger(finger: Touch_Finger): PTouch_Finger; |
537 function getSecondFinger(finger: TTouch_Data): PTouch_Data; |
551 begin |
538 begin |
552 if fingers[0].id = finger.id then |
539 if fingers[0].id = finger.id then |
553 getSecondFinger := @fingers[1] |
540 getSecondFinger := @fingers[1] |
554 else |
541 else |
555 getSecondFinger := @fingers[0]; |
542 getSecondFinger := @fingers[0]; |
556 end; |
543 end; |
557 |
544 |
558 function isOnRect(rect: TSDL_Rect; finger: Touch_Finger): boolean; |
545 function isOnRect(rect: TSDL_Rect; finger: TTouch_Data): boolean; |
559 var widget: TOnScreenWidget; |
546 var widget: TOnScreenWidget; |
560 begin |
547 begin |
561 widget.x:= rect.x; |
548 widget.x:= rect.x; |
562 widget.y:= rect.y; |
549 widget.y:= rect.y; |
563 widget.width:= rect.w; |
550 widget.width:= rect.w; |
565 widget.hOffset:= 0; |
552 widget.hOffset:= 0; |
566 widget.vOffset:= 0; |
553 widget.vOffset:= 0; |
567 exit(isOnRect(widget, finger)); |
554 exit(isOnRect(widget, finger)); |
568 end; |
555 end; |
569 |
556 |
570 function isOnRect(widget: TOnScreenWidget; finger: Touch_Finger): boolean; |
557 function isOnRect(widget: TOnScreenWidget; finger: TTouch_Data): boolean; |
571 begin |
558 begin |
572 with widget do |
559 with widget do |
573 isOnRect:= (finger.x > x + hOffset) and |
560 isOnRect:= (finger.x > x + hOffset) and |
574 (finger.x < x + width + hOffset) and |
561 (finger.x < x + width + hOffset) and |
575 (cScreenHeight - finger.y > y + vOffset) and |
562 (cScreenHeight - finger.y > y + vOffset) and |
576 (cScreenHeight - finger.y < y + height + vOffset); |
563 (cScreenHeight - finger.y < y + height + vOffset); |
577 end; |
564 end; |
578 |
565 |
579 procedure printFinger(finger: Touch_Finger); |
566 procedure printFinger(finger: TTouch_Data); |
580 begin |
567 begin |
581 WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown])); |
568 WriteToConsole(Format('id:%d, (%d,%d), (%d,%d), time: %d', [finger.id, finger.x, finger.y, finger.historicalX, finger.historicalY, finger.timeSinceDown])); |
582 end; |
569 end; |
583 |
570 |
584 procedure initModule; |
571 procedure initModule; |