touch.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. Namespace mojo.input
  2. #rem monkeydoc Global instance of the TouchDevice class.
  3. #end
  4. Const Touch:=New TouchDevice
  5. #rem monkeydoc The TouchDevice class.
  6. To access the touch device, use the global [[Touch]] constant.
  7. The touch device should only used after a new [[app.AppInstance]] is created.
  8. #end
  9. Class TouchDevice Extends InputDevice
  10. Method FingerDown:Bool( finger:Int )
  11. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  12. Return _fingers[finger].down
  13. End
  14. Method FingerPressed:Bool( finger:Int )
  15. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  16. Return _fingers[finger].pressed=_frame
  17. End
  18. Method FingerReleased:Bool( finger:Int )
  19. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  20. Return _fingers[finger].released=_frame
  21. End
  22. Method FingerPressure:Float( finger:Int )
  23. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  24. Return _fingers[finger].pressure
  25. End
  26. Method FingerX:Int( finger:Int )
  27. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  28. Return _fingers[finger].location.x
  29. End
  30. Method FingerY:Int( finger:Int )
  31. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  32. Return _fingers[finger].location.y
  33. End
  34. Method FingerLocation:Vec2i( finger:Int )
  35. DebugAssert( finger>=0 And finger<10,"Finger index out of range" )
  36. Return _fingers[finger].location
  37. End
  38. '***** INTERNAL *****
  39. #rem monkeydoc @hidden
  40. #end
  41. Method Init()
  42. End
  43. #rem monkeydoc @hidden
  44. #end
  45. Method Update()
  46. _frame+=1
  47. End
  48. #rem monkeydoc @hidden
  49. #end
  50. Method EventLocation:Vec2i( tevent:SDL_TouchFingerEvent Ptr )
  51. Local window:=App.ActiveWindow
  52. Local p:=New Vec2i( tevent->x * window.Frame.Width,tevent->y * window.Frame.Height )
  53. Return window.TransformPointFromView( p,Null )
  54. End
  55. #rem monkeydoc @hidden
  56. #end
  57. Method SendEvent( event:SDL_Event Ptr )
  58. If Not App.ActiveWindow Return
  59. Select event->type
  60. Case SDL_FINGERDOWN
  61. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  62. Local id:=-1
  63. For Local i:=0 Until 10
  64. If _fingers[i].down Continue
  65. _fingers[i].id=tevent->fingerId
  66. id=i
  67. Exit
  68. Next
  69. If id=-1 Return
  70. _fingers[id].down=True
  71. _fingers[id].pressed=_frame
  72. _fingers[id].pressure=tevent->pressure
  73. _fingers[id].location=EventLocation( tevent )
  74. Case SDL_FINGERUP
  75. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  76. Local id:=-1
  77. For Local i:=0 Until 10
  78. If Not _fingers[i].down Or _fingers[i].id<>tevent->fingerId Continue
  79. id=i
  80. Exit
  81. Next
  82. If id=-1 Return
  83. _fingers[id].down=False
  84. _fingers[id].released=_frame
  85. _fingers[id].pressure=0
  86. _fingers[id].location=EventLocation( tevent )
  87. Case SDL_FINGERMOTION
  88. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  89. Local id:=-1
  90. For Local i:=0 Until 10
  91. If Not _fingers[i].down Or _fingers[i].id<>tevent->fingerId Continue
  92. id=i
  93. Exit
  94. Next
  95. If id=-1 Return
  96. _fingers[id].pressure=tevent->pressure
  97. _fingers[id].location=EventLocation( tevent )
  98. End
  99. End
  100. Private
  101. Struct FingerState
  102. Field id:Int
  103. Field down:Bool
  104. Field pressed:Int
  105. Field released:Int
  106. Field pressure:Float
  107. Field location:Vec2i
  108. End
  109. Field _frame:Int=1
  110. Field _fingers:=New FingerState[10]
  111. Method New()
  112. End
  113. End