Skip to content

Commit cd27b60

Browse files
committed
Only create new blocks in flyout at certain angle #179 (#183)
* Only create new blocks in flyout at certain angle * Updating direction check to be generic to position * Update comment about direction check * Add comment about atan2 direction * Improve documentation, naming for flyout angles. * Shortening dragTowardWorkspaceOrthogonalAngleRange_
1 parent c9276f9 commit cd27b60

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

core/flyout.js

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ Blockly.Flyout.prototype.height_ = 0;
140140
*/
141141
Blockly.Flyout.prototype.verticalOffset_ = 0;
142142

143+
/**
144+
* Range of a drag angle from a fixed flyout considered "dragging toward workspace."
145+
* Drags that are within the bounds of this many degrees from the orthogonal
146+
* line to the flyout edge are considered to be "drags toward the workspace."
147+
* Example:
148+
* Flyout Edge Workspace
149+
* [block] / <-within this angle, drags "toward workspace" |
150+
* [block] ---- orthogonal to flyout boundary ---- |
151+
* [block] \ |
152+
* The angle is given in degrees from the orthogonal.
153+
* @type {number}
154+
* @private
155+
*/
156+
Blockly.Flyout.prototype.dragAngleRange_ = 70;
157+
143158
/**
144159
* Creates the flyout's DOM. Only needs to be called once.
145160
* @return {!Element} The flyout's SVG group.
@@ -301,7 +316,7 @@ Blockly.Flyout.prototype.setMetrics_ = function(xyRatio) {
301316

302317
Blockly.Flyout.prototype.setVerticalOffset = function(verticalOffset) {
303318
this.verticalOffset_ = verticalOffset;
304-
}
319+
};
305320

306321
/**
307322
* Move the toolbox to the edge of the workspace.
@@ -671,6 +686,8 @@ Blockly.Flyout.prototype.blockMouseDown_ = function(block) {
671686
// Left-click (or middle click)
672687
Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED);
673688
// Record the current mouse position.
689+
flyout.startDragMouseY_ = e.clientY;
690+
flyout.startDragMouseX_ = e.clientX;
674691
Blockly.Flyout.startDownEvent_ = e;
675692
Blockly.Flyout.startBlock_ = block;
676693
Blockly.Flyout.startFlyout_ = flyout;
@@ -751,12 +768,63 @@ Blockly.Flyout.prototype.onMouseMoveBlock_ = function(e) {
751768
}
752769
var dx = e.clientX - Blockly.Flyout.startDownEvent_.clientX;
753770
var dy = e.clientY - Blockly.Flyout.startDownEvent_.clientY;
754-
// Still dragging within the sticky DRAG_RADIUS.
755-
if (Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) {
771+
772+
// Only create a block if the user is dragging toward the workspace,
773+
// Otherwise the drag might be the start of a scroll in the flyout.
774+
// Don't create a block within the sticky drag radius.
775+
if (this.isDragTowardWorkspace_(dx, dy) &&
776+
Math.sqrt(dx * dx + dy * dy) > Blockly.DRAG_RADIUS) {
756777
// Create the block.
757778
Blockly.Flyout.startFlyout_.createBlockFunc_(Blockly.Flyout.startBlock_)(
758779
Blockly.Flyout.startDownEvent_);
780+
} else {
781+
// Do a scroll
782+
Blockly.Flyout.startFlyout_.onMouseMove_(e);
783+
}
784+
e.stopPropagation();
785+
};
786+
787+
/**
788+
* Determine if a drag delta is toward the workspace, based on the position
789+
* and orientation of the flyout. This is used in onMouseMoveBlock_ to determine
790+
* if a new block should be created or if the flyout should scroll.
791+
* @param {number} dx X delta of the drag
792+
* @param {number} dy Y delta of the drag
793+
* @return {boolean} true if the drag is toward the workspace.
794+
* @private
795+
*/
796+
Blockly.Flyout.prototype.isDragTowardWorkspace_ = function(dx, dy) {
797+
// Direction goes from -180 to 180, with 0 toward the right and 90 on top.
798+
var dragDirection = Math.atan2(dy, dx) / Math.PI * 180;
799+
800+
var draggingTowardWorkspace = false;
801+
var range = Blockly.Flyout.startFlyout_.dragAngleRange_;
802+
if (Blockly.Flyout.startFlyout_.horizontalLayout_) {
803+
if (Blockly.Flyout.startFlyout_.toolboxPosition_ == Blockly.TOOLBOX_AT_TOP) {
804+
// Horizontal at top
805+
if (dragDirection < 90 + range && dragDirection > 90 - range ) {
806+
draggingTowardWorkspace = true;
807+
}
808+
} else {
809+
// Horizontal at bottom
810+
if (dragDirection > -90 - range && dragDirection < -90 + range) {
811+
draggingTowardWorkspace = true;
812+
}
813+
}
814+
} else {
815+
if (Blockly.Flyout.startFlyout_.toolboxPosition_ == Blockly.TOOLBOX_AT_LEFT) {
816+
// Vertical at left
817+
if (dragDirection < range && dragDirection > -range) {
818+
draggingTowardWorkspace = true;
819+
}
820+
} else {
821+
// Vertical at right
822+
if (dragDirection < -180 + range || dragDirection > 180 - range) {
823+
draggingTowardWorkspace = true;
824+
}
825+
}
759826
}
827+
return draggingTowardWorkspace;
760828
};
761829

762830
/**

0 commit comments

Comments
 (0)