April 2024 - This site, and Kamaelia are being updated. There is significant work needed, and PRs are welcome.

Kamaelia.UI.MH.DragHandler

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.

Example Usage

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[0] - self.circle.x)**2 + (pos[1] - 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()

How does it work?

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.


Feedback

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