Switch to sending end drag event to be handled by gui

This commit is contained in:
DD 2018-02-23 11:57:19 -05:00
parent 51fd97ee54
commit 8355dd662f
3 changed files with 37 additions and 35 deletions

View file

@ -296,6 +296,12 @@ class Blocks {
case 'endDrag':
if (optRuntime) {
optRuntime.emitBlockDragUpdate(false /* areBlocksOverGui */);
// Drag blocks onto another sprite
if (e.isOutside) {
const newBlocks = adapter(e);
optRuntime.emitBlockEndDrag(newBlocks);
}
}
break;
case 'delete':

View file

@ -387,6 +387,13 @@ class Runtime extends EventEmitter {
return 'BLOCK_DRAG_UPDATE';
}
/**
* Event name for block drag end.
* @const {string}
*/
static get BLOCK_DRAG_END () {
return 'BLOCK_DRAG_END';
}
/**
* Event name for reporting that an extension was added.
@ -1404,6 +1411,14 @@ class Runtime extends EventEmitter {
this.emit(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui);
}
/**
* Emit event to indicate that the block drag has ended with the blocks outside the blocks workspace
* @param {Array.<object>} blocks The set of blocks dragged to the GUI
*/
emitBlockEndDrag (blocks) {
this.emit(Runtime.BLOCK_DRAG_END, blocks);
}
/**
* Emit value for reporter to show in the blocks.
* @param {string} blockId ID for the block.

View file

@ -10,7 +10,6 @@ const sb3 = require('./serialization/sb3');
const StringUtil = require('./util/string-util');
const formatMessage = require('format-message');
const Variable = require('./engine/variable');
const adapter = require('./engine/adapter');
const {loadCostume} = require('./import/load-costume.js');
const {loadSound} = require('./import/load-sound.js');
@ -41,11 +40,6 @@ class VirtualMachine extends EventEmitter {
*/
this.editingTarget = null;
/**
* ID of sprite that mouse is hovered over in sprite selector, if any
*/
this._hoveredSpriteId = null;
/**
* The currently dragging target, for redirecting IO data.
* @type {Target}
@ -83,6 +77,9 @@ class VirtualMachine extends EventEmitter {
this.runtime.on(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui => {
this.emit(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui);
});
this.runtime.on(Runtime.BLOCK_DRAG_END, blocks => {
this.emit(Runtime.BLOCK_DRAG_END, blocks);
});
this.runtime.on(Runtime.EXTENSION_ADDED, blocksInfo => {
this.emit(Runtime.EXTENSION_ADDED, blocksInfo);
});
@ -559,14 +556,6 @@ class VirtualMachine extends EventEmitter {
});
}
/**
* Sets the hovered sprite
* @param {string} targetId ID of a target whose sprite is hovered.
*/
setHoveredSprite (targetId) {
this._hoveredSpriteId = targetId;
}
/**
* Set the audio engine for the VM/runtime
* @param {!AudioEngine} audioEngine The audio engine to attach
@ -610,27 +599,6 @@ class VirtualMachine extends EventEmitter {
blockListener (e) {
if (this.editingTarget) {
this.editingTarget.blocks.blocklyListen(e, this.runtime);
// Validate event
if (typeof e !== 'object') return;
if (typeof e.blockId !== 'string' && typeof e.varId !== 'string') {
return;
}
// Drag blocks onto another sprite
if (e.type === 'endDrag' &&
e.isOutside &&
this._hoveredSpriteId &&
this._hoveredSpriteId !== this.editingTarget.id) {
const dragTarget = this.runtime.getTargetById(this._hoveredSpriteId);
if (dragTarget) {
// TODO tell gui to tell blocks to undo, to cause the blocks to move back into place.
const newBlocks = adapter(e);
for (let i = 0; i < newBlocks.length; i++) {
dragTarget.blocks.createBlock(newBlocks[i]);
}
}
}
}
}
@ -690,6 +658,19 @@ class VirtualMachine extends EventEmitter {
}
}
/**
* Called when blocks are dragged from one sprite to another. Adds the blocks to the
* workspace of the given target.
* @param {!Array<object>} blocks Blocks to add
# @param {!string} targetId Id of target to add blocks to.
*/
shareBlocksToTarget (blocks, targetId) {
const target = this.runtime.getTargetById(targetId);
for (let i = 0; i < blocks.length; i++) {
target.blocks.createBlock(blocks[i]);
}
}
/**
* Repopulate the workspace with the blocks of the current editingTarget. This
* allows us to get around bugs like gui#413.