Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drag-drop support. #359

Merged
merged 1 commit into from Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 37 additions & 9 deletions lib/src/display/stage.dart
Expand Up @@ -138,6 +138,28 @@ class Stage extends DisplayObjectContainer {

EventStream<Event> get onMouseLeave => Stage.mouseLeaveEvent.forTarget(this);

/// Dispatched when an external HTML element was dragged entering the stage
/// canvas.
EventStream<MouseEvent> get onDragEnter =>
const EventStreamProvider<MouseEvent>(MouseEvent.DRAG_ENTER)
.forTarget(this);

/// Dispatched when an external HTML element was dragged leaving the stage
/// canvas.
EventStream<MouseEvent> get onDragLeave =>
const EventStreamProvider<MouseEvent>(MouseEvent.DRAG_LEAVE)
.forTarget(this);

/// Dispatched when an external HTML element was dragged over the stage
/// canvas.
EventStream<MouseEvent> get onDragOver =>
const EventStreamProvider<MouseEvent>(MouseEvent.DRAG_OVER)
.forTarget(this);

/// Dispatched when an external HTML element was dropped on stage canvas.
EventStream<MouseEvent> get onDrop =>
const EventStreamProvider<MouseEvent>(MouseEvent.DROP).forTarget(this);

//----------------------------------------------------------------------------

Stage(CanvasElement canvas,
Expand Down Expand Up @@ -183,6 +205,10 @@ class Stage extends DisplayObjectContainer {
canvas.onMouseOut.listen(_onMouseEvent);
canvas.onContextMenu.listen(_onMouseEvent);
canvas.onMouseWheel.listen(_onMouseWheelEvent);
canvas.onDragEnter.listen(_onMouseEvent);
canvas.onDragLeave.listen(_onMouseEvent);
canvas.onDragOver.listen(_onMouseEvent);
canvas.onDrop.listen(_onMouseEvent);
}

final listenToTouchEvents = _inputEventMode == InputEventMode.TouchOnly ||
Expand Down Expand Up @@ -759,23 +785,25 @@ class Stage extends DisplayObjectContainer {
mouseButton.target = target;
mouseButton.clickTime = time;
mouseButton.clickCount++;
}

if (event.type == 'mouseup') {
} else if (event.type == 'mouseup') {
mouseEventType = mouseButton.mouseUpEventType;
mouseButton.buttonDown = false;
isClick = mouseButton.target == target;
isDoubleClick = isClick &&
mouseButton.clickCount.isEven &&
(time < mouseButton.clickTime + 500);
}

if (event.type == 'mousemove') {
} else if (event.type == 'mousemove') {
mouseEventType = MouseEvent.MOUSE_MOVE;
}

if (event.type == 'contextmenu') {
} else if (event.type == 'contextmenu') {
mouseEventType = MouseEvent.CONTEXT_MENU;
} else if (event.type == 'dragenter') {
mouseEventType = MouseEvent.DRAG_ENTER;
} else if (event.type == 'dragleave') {
mouseEventType = MouseEvent.DRAG_LEAVE;
} else if (event.type == 'dragover') {
mouseEventType = MouseEvent.DRAG_OVER;
} else if (event.type == 'drop') {
mouseEventType = MouseEvent.DROP;
}

//-----------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions lib/src/events/mouse_event.dart
Expand Up @@ -34,6 +34,11 @@ class MouseEvent extends InputEvent {
static const String ROLL_OUT = 'rollOut';
static const String ROLL_OVER = 'rollOver';

static const String DRAG_ENTER = 'dragEnter';
static const String DRAG_LEAVE = 'dragLeave';
static const String DRAG_OVER = 'dragOver';
static const String DROP = 'drop';

//---------------------------------------------------------------------------

/// The amount that is expected to scroll horizontally.
Expand Down