touch.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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
  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
  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. For Local i:=0 Until 10
  47. _fingers[i].pressed=False
  48. Next
  49. End
  50. #rem monkeydoc @hidden
  51. #end
  52. Method EventLocation:Vec2i( tevent:SDL_TouchFingerEvent Ptr )
  53. Local window:=App.ActiveWindow
  54. Local p:=New Vec2i( tevent->x * window.Frame.Width,tevent->y * window.Frame.Height )
  55. Return window.TransformPointFromView( p,Null )
  56. End
  57. #rem monkeydoc @hidden
  58. #end
  59. Method SendEvent( event:SDL_Event Ptr )
  60. If Not App.ActiveWindow Return
  61. Select event->type
  62. Case SDL_FINGERDOWN
  63. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  64. Local id:=-1
  65. For Local i:=0 Until 10
  66. If _fingers[i].down Continue
  67. _fingers[i].id=tevent->fingerId
  68. id=i
  69. Exit
  70. Next
  71. If id=-1 Return
  72. _fingers[id].down=True
  73. _fingers[id].pressed=True
  74. _fingers[id].pressure=tevent->pressure
  75. _fingers[id].location=EventLocation( tevent )
  76. Case SDL_FINGERUP
  77. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  78. Local id:=-1
  79. For Local i:=0 Until 10
  80. If Not _fingers[i].down Or _fingers[i].id<>tevent->fingerId Continue
  81. id=i
  82. Exit
  83. Next
  84. If id=-1 Return
  85. _fingers[id].down=False
  86. _fingers[id].released=False
  87. _fingers[id].pressure=0
  88. _fingers[id].location=EventLocation( tevent )
  89. Case SDL_FINGERMOTION
  90. Local tevent:=Cast<SDL_TouchFingerEvent Ptr>( event )
  91. Local id:=-1
  92. For Local i:=0 Until 10
  93. If Not _fingers[i].down Or _fingers[i].id<>tevent->fingerId Continue
  94. id=i
  95. Exit
  96. Next
  97. If id=-1 Return
  98. _fingers[id].pressure=tevent->pressure
  99. _fingers[id].location=EventLocation( tevent )
  100. End
  101. End
  102. Private
  103. Struct FingerState
  104. Field id:Int
  105. Field down:Bool
  106. Field pressed:Bool
  107. Field released:Bool
  108. Field pressure:Float
  109. Field location:Vec2i
  110. End
  111. Field _fingers:=New FingerState[10]
  112. Method New()
  113. End
  114. End