Package libavg :: Module eventList

Source Code for Module libavg.eventList

  1  # libavg - Media Playback Engine. 
  2  # Copyright (C) 2003-2008 Ulrich von Zadow 
  3  # 
  4  # This library is free software; you can redistribute it and/or 
  5  # modify it under the terms of the GNU Lesser General Public 
  6  # License as published by the Free Software Foundation; either 
  7  # version 2 of the License, or (at your option) any later version. 
  8  # 
  9  # This library is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  # Lesser General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU Lesser General Public 
 15  # License along with this library; if not, write to the Free Software 
 16  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 17  # 
 18  # Current versions can be found at www.libavg.de 
 19  # 
 20  # Original author of this file is Martin Heistermann <mh at sponc dot de> 
 21  # 
 22   
 23  from libavg import avg 
 24   
25 -class Cursor(object):
26 - def __init__(self):
27 self._pos = None 28 self.resetMotion()
29
30 - def resetMotion(self):
31 self._startPos = self._pos
32
33 - def getDelta (self):
34 return self._pos - self._startPos
35
36 - def getStartPos (self):
37 return self._startPos
38
39 - def getPos (self):
40 return self._pos
41
42 - def setPos (self, newPos):
43 self._pos = newPos 44 if not self._startPos: 45 self._startPos = self._pos
46
47 - def getX(self):
48 return self._pos.x
49 x = property(getX) 50
51 - def getY(self):
52 return self._pos.y
53 y = property(getY) 54
55 - def __unicode__ (self):
56 print "cursor %s->%s" % (self.getStartPos(), self.getPos())
57
58 -class EventCursor(Cursor):
59 - def __init__(self, event):
60 super(EventCursor, self).__init__() 61 self.__cursorid = event.cursorid 62 self.update (event)
63
64 - def update (self, event):
65 assert event.cursorid == self.__cursorid 66 self.setPos(event.pos) 67 self.__speed = event.speed
68
69 - def getSpeed (self):
70 return self.__speed
71
72 - def getCursorID (self):
73 return self.__cursorid
74
75 -class EventList:
76 """Keep track of cursors pressed on a Node"""
77 - def __init__(self, 78 node, 79 source, 80 onDown = lambda x: None, 81 onUp = lambda x: None, 82 onMotion = lambda x: None, 83 resetMotion = lambda: None, 84 maxEvents = None, 85 captureEvents = True):
86 87 self.__node = node 88 self.__source = source 89 self.__callback = { 90 'onDown': onDown, 91 'onUp': onUp, 92 'onMotion': onMotion, 93 'resetMotion': resetMotion, 94 } 95 96 self.__maxEvents = maxEvents 97 self.__captureEvents = captureEvents 98 99 self.__capturedCursors = [] 100 self.__node.setEventHandler (avg.CURSORDOWN, self.__source, self.__onDown) 101 self.__node.setEventHandler (avg.CURSORUP, self.__source, self.__onUp) 102 self.__node.setEventHandler (avg.CURSORMOTION, self.__source, self.__onMotion) 103 104 self.__eventCursors = []
105
106 - def handleInitialDown(self, event):
107 self.__onDown(event)
108
109 - def delete(self):
110 for cursorid in self.__capturedCursors: 111 self.__node.releaseEventCapture (cursorid) 112 self.__capturedCursors = [] 113 114 for type_ in avg.CURSORDOWN, avg.CURSORMOTION, avg.CURSORUP: 115 self.__node.setEventHandler(type_, self.__source, None) 116 self.__node = None
117
118 - def __setitem__(self, key, val):
119 raise Exception
120
121 - def __findEventCursor(self, event):
122 found = filter (lambda ec: ec.getCursorID() == event.cursorid, self.__eventCursors) 123 return found[0] if found else None
124
125 - def __resetMotion(self):
126 map(EventCursor.resetMotion, self.__eventCursors) 127 self.__callback['resetMotion']()
128
129 - def __onUp(self, event):
130 def __removeCursor (cursor): 131 def __releaseCapture (cursorid): 132 if cursorid in self.__capturedCursors: 133 try: 134 self.__node.releaseEventCapture (cursorid) 135 self.__capturedCursors.remove (cursorid) 136 except RuntimeError: # XXX 137 print "warning: could not release event capture for cursor %u" % cursorid
138 139 __releaseCapture (cursor.getCursorID()) 140 self.__eventCursors.remove (cursor) 141 self.__resetMotion ()
142 ec = self.__findEventCursor (event) 143 if ec: 144 ec.update (event) 145 __removeCursor (ec) 146 self.__callback['onUp'](ec) 147
148 - def __onDown(self, event):
149 if self.__maxEvents and len(self.__eventCursors) >= self.__maxEvents: 150 return 151 152 if self.__captureEvents: 153 try: 154 self.__node.setEventCapture(event.cursorid) 155 self.__capturedCursors.append(event.cursorid) 156 except RuntimeError: 157 print "warning: could capture events for cursor %u" % event.cursorid 158 159 ec = EventCursor (event) 160 self.__eventCursors.append (ec) 161 self.__resetMotion () 162 self.__callback['onDown'](ec)
163
164 - def __onMotion(self, event):
165 ec = self.__findEventCursor (event) 166 if ec: 167 ec.update (event) 168 self.__callback['onMotion'](ec)
169
170 - def getCursors(self):
171 return self.__eventCursors
172
173 - def __len__(self):
174 return len(self.__eventCursors)
175