def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        '''Translate this event into a number of events:

        First up, generate enter/leave events (replicating the behaviour of
        `on_mouse_move`).

        If already dragging (self.drag_element or self.mouse_drag_element
        are set) then continue that behaviour.
        
        If the mouse button was pressed while over an element we attempt to
        pass an `on_mouse_drag` event to that element. If that event is
        handled and the handler returns EVENT_HANDLED then we set
        self.mouse_drag_element and the handler returns.
        
        We then check a drag threshold to determine whether an on_drag
        should be generated. If the mouse has moved far enough (currently
        4 pixels) then we attempt to pass an `on_drag` event to the
        element under the pointer. If that event is handled and the
        handler returns EVENT_HANDLED we set self.drag_element and them
        attempt to pass on_drag_enter and on_drag_leave events on the
        element *under* the element being dragged.
        '''
        element = self.determineHit(x, y)
        self.generateEnterLeave(x, y, element)

        if self.drag_element is not None:
            # continue to feed on_drag events
            element, handled = self.dispatch_event(self.drag_element,
                'on_drag', x, y, dx, dy, buttons, modifiers)
            self.drag_element = element

            # tell the element we've dragged over
            over = self.determineHit(x, y, exclude=element)
            if (over is not self.drag_over_element and 
                    self.drag_over_element is not None):
                self.dispatch_event(self.drag_over_element, 'on_drag_leave',
                    x, y, element)
            if over is not None and over.isEnabled():
                self.dispatch_event(over, 'on_drag_enter', x, y, element)
            self.drag_over_element = over
            return handled

        elif self.mouse_drag_element is not None:
            # continue to feed on_mouse_drag events
            element, handled = self.dispatch_event(self.mouse_drag_element,
                'on_mouse_drag', x, y, dx, dy, buttons, modifiers)
            self.mouse_drag_element = element
            return handled

        # we didn't actually click on an element
        if self.mouse_press_element is None:
            return EVENT_UNHANDLED

        # try an on_mouse_drag event if the element is enabled
        if self.mouse_press_element.isEnabled():
            element, handled = self.dispatch_event(self.mouse_press_element,
                'on_mouse_drag', x, y, dx, dy, buttons, modifiers)
            if handled == EVENT_HANDLED:
                self.mouse_drag_element = element
                return EVENT_HANDLED

        # check drag threshold
        cdx, cdy = self.cumulative_drag
        cdx += abs(dx); cdy += abs(dy)
        self.cumulative_drag = (cdx, cdy)
        if cdx + cdy < 4:
            # less than 4 pixels, don't drag just yet
            return EVENT_UNHANDLED

        # if the mouse_press_element is disabled we need to find the first
        # enabled parent to try to pass the event to
        element = self.mouse_press_element
        if not element.isEnabled():
            while not element.isEnabled():
                element = element.parent
                if element is None:
                    return EVENT_UNHANDLED

        # now try an on_drag event
        element, handled = self.dispatch_event(element, 'on_drag',
            x, y, dx, dy, buttons, modifiers)
        if handled == EVENT_UNHANDLED:
            return EVENT_UNHANDLED

        # the event may have been handled by a parent
        self.drag_element = element

        # tell the element we've dragged the active element over
        element = self.determineHit(x, y, exclude=element)
        if element is not self.drag_over_element:
            if self.drag_over_element is not None:
                self.dispatch_event(self.drag_over_element,
                    'on_drag_leave', x, y, self.drag_element)
        if element is not None and element.isEnabled():
            self.dispatch_event(element, 'on_drag_enter', x, y,
                self.drag_element)
        self.drag_over_element = element
        return EVENT_HANDLED
dataset/ETHPy150Open ardekantur/pyglet/contrib/wydget/wydget/event.py/GUIEventDispatcher.on_mouse_drag/original