$dispatchMouseCallback method

void $dispatchMouseCallback(
  1. MouseInputType type,
  2. GDisplayObject object,
  3. MouseInputData input
)

(Internal usage) Dispatches a mouse event to the appropriate callback functions.

If mouseEnabled is false, no mouse events will be dispatched. The type argument specifies the type of mouse event. The object argument specifies the object that the event originated from. The input argument specifies the mouse input data for the event.

The following types of mouse events are dispatched:

  • MouseInputType.zoomPan: dispatched when the user uses pinch zoom or pan gestures.
  • MouseInputType.wheel: dispatched when the user scrolls the mouse wheel.
  • MouseInputType.down: dispatched when the user presses the left mouse button.
  • MouseInputType.move: dispatched when the user moves the mouse cursor.
  • MouseInputType.up: dispatched when the user releases the left mouse button.
  • MouseInputType.over: dispatched when the mouse cursor enters the bounds of the object.
  • MouseInputType.out: dispatched when the mouse cursor leaves the bounds of the object.

Depending on the type of mouse event, the appropriate callback function will be invoked. The mouseInput argument is a cloned copy of the input argument, with its target and currentTarget fields set to this and object, respectively. If the mouse event is a down event, the mouseDownObj field of this object will be set to object. If the mouse event is an up event, the mouseDownObj field will be set to null, and the appropriate onMouseClick or onMouseDoubleClick callback function will be invoked if necessary.

If the mouse event is a over event, the mouseOverObj field of this object will be set to object. If the useCursor field of this object is true and the cursor is showing, the cursor will be set to the click cursor.

If the mouse event is an out event, the mouseOverObj field will be set to null. If the useCursor field of this object is true and the cursor is showing, the cursor will be set to the basic cursor.

The parent of this object will also receive the mouse event, recursively, until there are no more parents.

Implementation

void $dispatchMouseCallback(
  MouseInputType type,
  GDisplayObject object,
  MouseInputData input,
) {
  if (mouseEnabled) {
    var mouseInput = input.clone(this, object, type);
    switch (type) {
      case MouseInputType.zoomPan:
        $onZoomPan?.dispatch(mouseInput);
        break;
      case MouseInputType.wheel:
        $onMouseWheel?.dispatch(mouseInput);
        break;
      case MouseInputType.down:
        $mouseDownObj = object;
        $onMouseDown?.dispatch(mouseInput);
        break;
//        case MouseInputType.rightDown:
//          $rightMouseDownObj = object;
//          $onRightMouseDown?.dispatch(mouseInput);
//          break;
      case MouseInputType.move:
        $onMouseMove?.dispatch(mouseInput);
        break;
      case MouseInputType.up:
        if ($mouseDownObj == object &&
            ($onMouseClick != null || $onMouseDoubleClick != null)) {
          var mouseClickInput = input.clone(this, object, MouseInputType.up);
          $onMouseClick?.dispatch(mouseClickInput);

          if ($lastClickTime > 0 &&
              input.time - $lastClickTime < MouseInputData.doubleClickTime) {
            $onMouseDoubleClick?.dispatch(mouseClickInput);
            $lastClickTime = -1;
          } else {
            $lastClickTime = input.time;
          }
        }
        $mouseDownObj = null;
        $onMouseUp?.dispatch(mouseInput);
        break;
      case MouseInputType.over:
        $mouseOverObj = object;
        if (useCursor && GMouse.isShowing()) {
          GMouse.setClickCursor();
        }
        $onMouseOver?.dispatch(mouseInput);
        break;
      case MouseInputType.out:
        $mouseOverObj = null;

        /// TODO: check if other object on top receives the event
        /// todo. before this one, and Cursor loses the stage.
        if (useCursor && GMouse.isShowing()) {
          GMouse.basic();
        }
        $onMouseOut?.dispatch(mouseInput);
        break;
      default:
        break;
    }
  }
  $parent?.$dispatchMouseCallback(type, object, input);
}