/kivy/input/providers/androidjoystick.py

https://bitbucket.org/pnpranavrao/kivy · Python · 98 lines · 78 code · 15 blank · 5 comment · 15 complexity · f9426176b656ac8a736052d1fc426a1c MD5 · raw file

  1. __all__ = ('AndroidMotionEventProvider', )
  2. import os
  3. try:
  4. __import__('android')
  5. except ImportError:
  6. if 'KIVY_DOC' not in os.environ:
  7. raise Exception('android lib not found.')
  8. from kivy.logger import Logger
  9. from kivy.input.provider import MotionEventProvider
  10. from kivy.input.factory import MotionEventFactory
  11. from kivy.input.shape import ShapeRect
  12. from kivy.input.motionevent import MotionEvent
  13. import pygame.joystick
  14. class AndroidMotionEvent(MotionEvent):
  15. def depack(self, args):
  16. self.is_touch = True
  17. self.profile = ['pos', 'pressure', 'shape']
  18. self.sx, self.sy, self.pressure, radius = args
  19. self.shape = ShapeRect()
  20. self.shape.width = radius
  21. self.shape.height = radius
  22. super(AndroidMotionEvent, self).depack(args)
  23. class AndroidMotionEventProvider(MotionEventProvider):
  24. def __init__(self, device, args):
  25. super(AndroidMotionEventProvider, self).__init__(device, args)
  26. self.joysticks = []
  27. self.touches = {}
  28. self.uid = 0
  29. self.window = None
  30. def create_joystick(self, index):
  31. Logger.info('Android: create joystick <%d>' % index)
  32. js = pygame.joystick.Joystick(index)
  33. js.init()
  34. if js.get_numbuttons() == 0:
  35. Logger.info('Android: discard joystick <%d> cause no button' %
  36. index)
  37. return
  38. self.joysticks.append(js)
  39. def start(self):
  40. pygame.joystick.init()
  41. Logger.info('Android: found %d joystick' % pygame.joystick.get_count())
  42. for i in xrange(pygame.joystick.get_count()):
  43. self.create_joystick(i)
  44. def stop(self):
  45. self.joysticks = []
  46. def update(self, dispatch_fn):
  47. if not self.window:
  48. from kivy.core.window import Window
  49. self.window = Window
  50. w, h = self.window.system_size
  51. touches = self.touches
  52. for joy in self.joysticks:
  53. jid = joy.get_id()
  54. pressed = joy.get_button(0)
  55. x = joy.get_axis(0) * 32768. / w
  56. y = 1. - (joy.get_axis(1) * 32768. / h)
  57. pressure = joy.get_axis(2)
  58. radius = joy.get_axis(3)
  59. # new touche ?
  60. if pressed and jid not in touches:
  61. self.uid += 1
  62. touch = AndroidMotionEvent(self.device, self.uid,
  63. [x, y, pressure, radius])
  64. touches[jid] = touch
  65. dispatch_fn('begin', touch)
  66. # update touch
  67. elif pressed:
  68. touch = touches[jid]
  69. # avoid same touch position
  70. if touch.sx == x and touch.sy == y \
  71. and touch.pressure == pressure:
  72. #print 'avoid moving.', touch.uid, x, y, pressure, radius
  73. continue
  74. touch.move([x, y, pressure, radius])
  75. dispatch_fn('update', touch)
  76. # disapear
  77. elif not pressed and jid in touches:
  78. touch = touches[jid]
  79. touch.move([x, y, pressure, radius])
  80. touch.update_time_end()
  81. dispatch_fn('end', touch)
  82. touches.pop(jid)
  83. MotionEventFactory.register('android', AndroidMotionEventProvider)