Skip to content

Commit 3d3f276

Browse files
Nikhita-14vanshika2720
authored andcommitted
Bounce blocks away from illegal connections on drag release (sugarlabs#5324)
* Bounce blocks away from illegal connections on drag release * Add directional bounce for different block types
1 parent a073767 commit 3d3f276

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

js/blocks.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,9 @@ class Blocks {
17261726
start = this.blockList[b].connections.length - 1;
17271727
}
17281728

1729+
const ILLEGAL_BOUNCE_DIST = 400; // squared distance (20px)
1730+
let bounced = false;
1731+
17291732
for (let i = start; i < this.blockList[b].connections.length; i++) {
17301733
/**
17311734
* When converting from Python projects to JS format,
@@ -1781,11 +1784,42 @@ class Blocks {
17811784
min = dist;
17821785
}
17831786
} else {
1784-
/**
1785-
* TODO: bounce away from illegal connection?
1786-
* only if the distance was small
1787-
* console.debug('cannot not connect these two block types');
1788-
*/
1787+
// Bounce away from illegal connection if the distance was small.
1788+
if (!myBlock.isDragging) {
1789+
const x2 =
1790+
this.blockList[b].container.x + this.blockList[b].docks[i][0];
1791+
const y2 =
1792+
this.blockList[b].container.y + this.blockList[b].docks[i][1];
1793+
1794+
const dx = x2 - x1;
1795+
const dy = y2 - y1;
1796+
const dist = dx * dx + dy * dy;
1797+
1798+
if (!bounced && dist < ILLEGAL_BOUNCE_DIST) {
1799+
console.debug("cannot connect these two block types");
1800+
1801+
const distance = Math.sqrt(dist) || 0.0001;
1802+
const bounceFactor = 60;
1803+
1804+
// Snap back first
1805+
if (myBlock.lastGoodX !== undefined) {
1806+
myBlock.container.x = myBlock.lastGoodX;
1807+
myBlock.container.y = myBlock.lastGoodY;
1808+
}
1809+
1810+
// Directional push away from illegal dock based on block type
1811+
if (myBlock.isArgBlock()) {
1812+
// Arg blocks bounce to the right
1813+
myBlock.container.x += bounceFactor;
1814+
} else {
1815+
// Flow blocks bounce below and to the right
1816+
myBlock.container.x += bounceFactor * 0.7;
1817+
myBlock.container.y += bounceFactor;
1818+
}
1819+
1820+
bounced = true;
1821+
}
1822+
}
17891823
}
17901824
}
17911825
}

0 commit comments

Comments
 (0)