mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-01 16:51:10 -04:00
Add Move event.
This commit is contained in:
parent
0027d9294a
commit
acd6af1c45
4 changed files with 105 additions and 9 deletions
|
@ -393,6 +393,10 @@ Blockly.Block.prototype.getChildren = function() {
|
|||
* @param {Blockly.Block} newParent New parent block.
|
||||
*/
|
||||
Blockly.Block.prototype.setParent = function(newParent) {
|
||||
var event;
|
||||
if (Blockly.Events.isEnabled() && !this.isShadow()) {
|
||||
event = new Blockly.Events.Move(this);
|
||||
}
|
||||
if (this.parentBlock_) {
|
||||
// Remove this block from the old parent's child list.
|
||||
var children = this.parentBlock_.childBlocks_;
|
||||
|
@ -415,11 +419,7 @@ Blockly.Block.prototype.setParent = function(newParent) {
|
|||
// its connection locations.
|
||||
} else {
|
||||
// Remove this block from the workspace's list of top-most blocks.
|
||||
// Note that during realtime sync we sometimes create child blocks that are
|
||||
// not top level so we check first before removing.
|
||||
if (goog.array.contains(this.workspace.getTopBlocks(false), this)) {
|
||||
this.workspace.removeTopBlock(this);
|
||||
}
|
||||
this.workspace.removeTopBlock(this);
|
||||
}
|
||||
|
||||
this.parentBlock_ = newParent;
|
||||
|
@ -429,6 +429,10 @@ Blockly.Block.prototype.setParent = function(newParent) {
|
|||
} else {
|
||||
this.workspace.addTopBlock(this);
|
||||
}
|
||||
if (event) {
|
||||
event.recordNew();
|
||||
Blockly.Events.fire(event);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -1275,7 +1279,10 @@ Blockly.Block.prototype.getRelativeToSurfaceXY = function() {
|
|||
* @param {number} dy Vertical offset.
|
||||
*/
|
||||
Blockly.Block.prototype.moveBy = function(dx, dy) {
|
||||
var event = new Blockly.Events.Move(this);
|
||||
this.xy_.translate(dx, dy);
|
||||
event.recordNew();
|
||||
Blockly.Events.fire(event);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -217,6 +217,11 @@ Blockly.BlockSvg.terminateDrag_ = function() {
|
|||
// Update the connection locations.
|
||||
var xy = selected.getRelativeToSurfaceXY();
|
||||
var dxy = goog.math.Coordinate.difference(xy, selected.dragStartXY_);
|
||||
var event = new Blockly.Events.Move(selected);
|
||||
event.oldCoordinate = selected.dragStartXY_;
|
||||
event.recordNew();
|
||||
Blockly.Events.fire(event);
|
||||
|
||||
selected.moveConnections_(dxy.x, dxy.y);
|
||||
delete selected.draggedBubbles_;
|
||||
selected.setDragging_(false);
|
||||
|
@ -286,10 +291,13 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() {
|
|||
* @param {number} dy Vertical offset.
|
||||
*/
|
||||
Blockly.BlockSvg.prototype.moveBy = function(dx, dy) {
|
||||
var event = new Blockly.Events.Move(this);
|
||||
var xy = this.getRelativeToSurfaceXY();
|
||||
this.getSvgRoot().setAttribute('transform',
|
||||
'translate(' + (xy.x + dx) + ',' + (xy.y + dy) + ')');
|
||||
this.moveConnections_(dx, dy);
|
||||
event.recordNew();
|
||||
Blockly.Events.fire(event);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,13 +52,19 @@ Blockly.Events.DELETE = 'delete';
|
|||
*/
|
||||
Blockly.Events.CHANGE = 'change';
|
||||
|
||||
/**
|
||||
* Name of event that moves a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.MOVE = 'move';
|
||||
|
||||
/**
|
||||
* Create a custom event and fire it.
|
||||
* @param {Object} detail Custom data for event.
|
||||
* @param {!Blockly.Events.Abstract} detail Custom data for event.
|
||||
*/
|
||||
Blockly.Events.fire = function(detail) {
|
||||
if (!Blockly.Events.isEnabled()) {
|
||||
return; // No events allowed.
|
||||
if (!Blockly.Events.isEnabled() || detail.isNull()) {
|
||||
return;
|
||||
}
|
||||
console.log(detail);
|
||||
var workspace = Blockly.Workspace.getById(detail.workspaceId);
|
||||
|
@ -105,6 +111,14 @@ Blockly.Events.isEnabled = function() {
|
|||
*/
|
||||
Blockly.Events.Abstract = function() {};
|
||||
|
||||
/**
|
||||
* Does this event record any change of state?
|
||||
* @return {boolean} True if something changed.
|
||||
*/
|
||||
Blockly.Events.Abstract.prototype.isNull = function() {
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Class for a block creation event.
|
||||
* @param {!Blockly.Workspace} workspace The workspace.
|
||||
|
@ -158,3 +172,71 @@ Blockly.Events.Change = function(block, element, name, oldValue, newValue) {
|
|||
this.newValue = newValue;
|
||||
};
|
||||
goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);
|
||||
|
||||
/**
|
||||
* Does this event record any change of state?
|
||||
* @return {boolean} True if something changed.
|
||||
*/
|
||||
Blockly.Events.Change.prototype.isNull = function() {
|
||||
return this.oldValue == this.newValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* Class for a block move event. Created before the move.
|
||||
* @param {!Blockly.Block} block The moved block.
|
||||
* @extends {Blockly.Events.Abstract}
|
||||
* @constructor
|
||||
*/
|
||||
Blockly.Events.Move = function(block) {
|
||||
this.type = Blockly.Events.MOVE;
|
||||
this.workspaceId = block.workspace.id;
|
||||
this.blockId = block.id;
|
||||
|
||||
var location = this.currentLocation_();
|
||||
this.oldParentId = location.parentId;
|
||||
this.oldInputName = location.inputName;
|
||||
this.oldCoordinate = location.coordinate;
|
||||
};
|
||||
goog.inherits(Blockly.Events.Move, Blockly.Events.Abstract);
|
||||
|
||||
/**
|
||||
* Record the block's new location. Called after the move.
|
||||
*/
|
||||
Blockly.Events.Move.prototype.recordNew = function() {
|
||||
var location = this.currentLocation_();
|
||||
this.newParentId = location.parentId;
|
||||
this.newInputName = location.inputName;
|
||||
this.newCoordinate = location.coordinate;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the parentId and input if the block is connected,
|
||||
* or the XY location if disconnected.
|
||||
* @return {!Object} Collection of location info.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Events.Move.prototype.currentLocation_ = function() {
|
||||
var block = Blockly.Block.getById(this.blockId);
|
||||
var location = {};
|
||||
var parent = block.getParent();
|
||||
if (parent) {
|
||||
location.parentId = parent.id;
|
||||
var input = parent.getInputWithBlock(block);
|
||||
if (input) {
|
||||
location.inputName = input.name
|
||||
}
|
||||
} else {
|
||||
location.coordinate = block.getRelativeToSurfaceXY();
|
||||
}
|
||||
return location;
|
||||
};
|
||||
|
||||
/**
|
||||
* Does this event record any change of state?
|
||||
* @return {boolean} True if something changed.
|
||||
*/
|
||||
Blockly.Events.Move.prototype.isNull = function() {
|
||||
return this.oldParentId == this.newParentId &&
|
||||
this.oldInputName == this.newInputName &&
|
||||
goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
|
||||
};
|
||||
|
|
|
@ -162,7 +162,6 @@ Blockly.ScrollbarPair.prototype.set = function(x, y) {
|
|||
xyRatio.y = ratio;
|
||||
|
||||
this.workspace_.setMetrics(xyRatio);
|
||||
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue