Pygame 'drag' event Handler
A class, to implement "click and hold" dragging operations in pygame. Hooks into the event dispatch mechanism provided by the PyGameApp component.
Subclass this class to implement your required dragging functionality.
A set of circles that can be dragged around the pygame window:
class Circle(object): def __init__(self, x, y, radius): self.x, self.y, self.radius = x, y, radius def draw(self, surface): pygame.draw.circle(surface, (255,128,128), (self.x, self.y), self.radius) class CircleDragHandler(DragHandler): def __init__(self, event, app, theCircle): self.circle = theCircle super(CircleDragHandler,self).__init__(event, app) def detect(self, pos, button): if (pos - self.circle.x)**2 + (pos - self.circle.y)**2 < (self.circle.radius**2): return (self.circle.x, self.circle.y) return False def drag(self,newx,newy): self.circle.x = newx self.circle.y = newy def release(self,newx, newy): self.drag(newx, newy) class DraggableCirclesApp(PyGameApp): def initialiseComponent(self): self.circles =  for i in range(100,200,20): circle = Circle(i, 2*i, 20) self.circles.append(circle) handler = lambda event, circle=circle : CircleDragHandler.handle(event, self, circle) self.addHandler(MOUSEBUTTONDOWN, handler) def mainLoop(self): self.screen.fill( (255,255,255) ) for circle in self.circles: circle.draw(self.screen) return 1 DraggableCirclesApp((800,600)).run()
Subclass DragHandler to use it, and (re)implement the __init__(...), detect(...), drag(...) and release(...) methods.
Bind the handler(...) static method to the event (usually MOUSEBUTTONDOWN), providing the arguments for the initializer.
The DragHandler will instantiate upon the event and the detect(...) method will be called to determine whether a drag operation should begin.
The 'event' and 'app' attributes are set to the event that triggered this and the PyGameApp component concerned respectively.
Implement detect(...) so that it returns False to abort the drag operation, or (x,y) coordinates for the start of the drag operation. These co-ordinates don't have to be the same as the ones supplied - they are your opportunity to specify the origin for the drag.
During the drag, the DragHandler object will bind to the MOUSEMOTION and MOUSEBUTTONUP pygame events.
Whilst dragging, your drag(...) method will be called whenever the mouse moves and release(...) will be called when the mouse button(s) are finally released.
drag(...) and release(...) are passed updated x,y coordinates. These are the origin coordinates (returned by detect(...) method) plus motion since the drag began.
Got a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording!
Please leave you feedback here in reply to the documentation thread in the Kamaelia blog.
-- Automatic documentation generator, 05 Jun 2009 at 03:01:38 UTC/GMT
This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)
(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved.