mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 15:02:52 -05:00
Switch to sending end drag event to be handled by gui
This commit is contained in:
parent
51fd97ee54
commit
8355dd662f
3 changed files with 37 additions and 35 deletions
|
@ -296,6 +296,12 @@ class Blocks {
|
||||||
case 'endDrag':
|
case 'endDrag':
|
||||||
if (optRuntime) {
|
if (optRuntime) {
|
||||||
optRuntime.emitBlockDragUpdate(false /* areBlocksOverGui */);
|
optRuntime.emitBlockDragUpdate(false /* areBlocksOverGui */);
|
||||||
|
|
||||||
|
// Drag blocks onto another sprite
|
||||||
|
if (e.isOutside) {
|
||||||
|
const newBlocks = adapter(e);
|
||||||
|
optRuntime.emitBlockEndDrag(newBlocks);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'delete':
|
case 'delete':
|
||||||
|
|
|
@ -387,6 +387,13 @@ class Runtime extends EventEmitter {
|
||||||
return 'BLOCK_DRAG_UPDATE';
|
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.
|
* Event name for reporting that an extension was added.
|
||||||
|
@ -1404,6 +1411,14 @@ class Runtime extends EventEmitter {
|
||||||
this.emit(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui);
|
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.
|
* Emit value for reporter to show in the blocks.
|
||||||
* @param {string} blockId ID for the block.
|
* @param {string} blockId ID for the block.
|
||||||
|
|
|
@ -10,7 +10,6 @@ const sb3 = require('./serialization/sb3');
|
||||||
const StringUtil = require('./util/string-util');
|
const StringUtil = require('./util/string-util');
|
||||||
const formatMessage = require('format-message');
|
const formatMessage = require('format-message');
|
||||||
const Variable = require('./engine/variable');
|
const Variable = require('./engine/variable');
|
||||||
const adapter = require('./engine/adapter');
|
|
||||||
|
|
||||||
const {loadCostume} = require('./import/load-costume.js');
|
const {loadCostume} = require('./import/load-costume.js');
|
||||||
const {loadSound} = require('./import/load-sound.js');
|
const {loadSound} = require('./import/load-sound.js');
|
||||||
|
@ -41,11 +40,6 @@ class VirtualMachine extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
this.editingTarget = null;
|
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.
|
* The currently dragging target, for redirecting IO data.
|
||||||
* @type {Target}
|
* @type {Target}
|
||||||
|
@ -83,6 +77,9 @@ class VirtualMachine extends EventEmitter {
|
||||||
this.runtime.on(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui => {
|
this.runtime.on(Runtime.BLOCK_DRAG_UPDATE, areBlocksOverGui => {
|
||||||
this.emit(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.runtime.on(Runtime.EXTENSION_ADDED, blocksInfo => {
|
||||||
this.emit(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
|
* Set the audio engine for the VM/runtime
|
||||||
* @param {!AudioEngine} audioEngine The audio engine to attach
|
* @param {!AudioEngine} audioEngine The audio engine to attach
|
||||||
|
@ -610,27 +599,6 @@ class VirtualMachine extends EventEmitter {
|
||||||
blockListener (e) {
|
blockListener (e) {
|
||||||
if (this.editingTarget) {
|
if (this.editingTarget) {
|
||||||
this.editingTarget.blocks.blocklyListen(e, this.runtime);
|
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
|
* Repopulate the workspace with the blocks of the current editingTarget. This
|
||||||
* allows us to get around bugs like gui#413.
|
* allows us to get around bugs like gui#413.
|
||||||
|
|
Loading…
Reference in a new issue