Posted By

ZeroByte on 07/10/08


Tagged

interaction panda3d userinterface mousegestures


Versions (?)

Mouse Gesture Integration With Panda3D


 / Published in: Python
 

Usage example of my mouse gesture class (http://snipplr.com/view/7216/mouse-gestures/) in Panda3d

  1. from MouseGest import Gesture
  2. import direct.directbase.DirectStart
  3. from BasePolling import interface
  4.  
  5. test_gestures = {}
  6. test_gestures['right'] = Gesture()
  7. test_gestures['right'].add_stroke( [(0, 0), (1, 0)] )
  8.  
  9. test_gestures['left'] = Gesture()
  10. test_gestures['left'].add_stroke( [(0, 0), (-1, 0)] )
  11.  
  12. test_gestures['up'] = Gesture()
  13. test_gestures['up'].add_stroke( [(0, 0), (0, 1)] )
  14.  
  15. test_gestures['down'] = Gesture()
  16. test_gestures['down'].add_stroke( [(0, 0), (0, -1)] )
  17.  
  18. test_gestures['box'] = Gesture()
  19. test_gestures['box'].add_stroke([(0, 0), (0, 1)])
  20. test_gestures['box'].add_stroke([(0, 1), (1, 1)])
  21. test_gestures['box'].add_stroke([(1, 1), (1, 0)])
  22. test_gestures['box'].add_stroke([(1, 0), (0, 0)])
  23. for key in test_gestures:
  24. test_gestures[key].normalize()
  25.  
  26. class MouseTest:
  27.  
  28. def __init__(self):
  29. self.lastAct = 0
  30. self.gestureDelay = 2
  31. self.drawingFlag = False
  32. self.matchedFlag = True
  33. taskMgr.add(self.drawTask, "Drawing task")
  34.  
  35. self.currGest = None
  36. self.currStroke = None
  37.  
  38. def drawTask(self, task):
  39.  
  40. if task.time - self.lastAct >= self.gestureDelay and not self.matchedFlag:
  41. self.currGest.normalize()
  42. print "--Evaluating gesture--"
  43. for gesture_name in test_gestures:
  44. if self.currGest == test_gestures[gesture_name]:
  45. print "Gesture:", gesture_name
  46. self.currGest = None
  47. self.matchedFlag = True
  48.  
  49. if interface.getMouseHit('left'):
  50. if self.currGest is None:
  51. self.currGest = Gesture()
  52. self.currStroke = self.currGest.add_stroke()
  53. print "Starting new stroke"
  54. self.drawingFlag = True
  55. self.matchedFlag = False
  56. self.lastAct = task.time
  57.  
  58. if self.drawingFlag:
  59. if not interface.getMouse('left'):
  60. self.drawingFlag = False
  61. return task.cont
  62. else:
  63. self.lastAct = task.time
  64. mousePos = interface.getMousePos()
  65. self.currStroke.points.append(Gesture.Point(mousePos[0], mousePos[1]))
  66.  
  67. return task.cont
  68.  
  69. mouse = MouseTest()
  70. run()

Report this snippet  

You need to login to post a comment.