Skip to content

Commit 3f9ca61

Browse files
authored
updating the number of columns of home button
1 parent dd9d42b commit 3f9ca61

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

js/activity.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -674,30 +674,57 @@ class Activity {
674674
* @constructor
675675
*/
676676
this._findBlocks = () => {
677+
// Ensure visibility of blocks
677678
if (!this.blocks.visible) {
678679
this._changeBlockVisibility();
679680
}
681+
682+
// Reset active block and hide DOM label
680683
this.blocks.activeBlock = null;
681684
hideDOMLabel();
685+
686+
// Show blocks and set initial container position
682687
this.blocks.showBlocks();
683688
this.blocksContainer.x = 0;
684689
this.blocksContainer.y = 0;
690+
691+
// Calculate initial positions for block placement
685692
let toppos;
686693
if (this.auxToolbar.style.display === "block") {
687694
toppos = 90 + this.toolbarHeight;
688695
} else {
689696
toppos = 90;
690697
}
691698

692-
const columnSpacing = Math.floor(600 * this.turtleBlocksScale);
693-
const leftColumnX = Math.floor((this.canvas.width / 4) * this.turtleBlocksScale);
694-
const rightColumnX = leftColumnX + columnSpacing;
699+
const screenWidth = window.innerWidth;
700+
701+
/**
702+
* Number of columns are dynamic accrding to the screen size
703+
* Minimum column width is set to 400px to ensure readability and usability.
704+
*/
705+
706+
const minColumnWidth = 400;
707+
let numColumns;
708+
709+
// Determine device type and adjust number of columns
710+
if (screenWidth <= 320) {
711+
// For small screen phones
712+
numColumns = 1;
713+
} else {
714+
// anything other than small screen phones
715+
numColumns = Math.floor(screenWidth / minColumnWidth);
716+
}
717+
718+
const columnSpacing = screenWidth / numColumns;
695719
const initialY = Math.floor(toppos * this.turtleBlocksScale);
696-
const verticalSpacing = Math.floor(20 * this.turtleBlocksScale);
697-
let leftColumnY = initialY;
698-
let rightColumnY = initialY;
699-
let placeInLeftColumn = true;
700-
720+
const verticalSpacing = Math.floor(20 * this.turtleBlocksScale);
721+
722+
const columnXPositions = Array.from({ length: numColumns }, (_, i) =>
723+
Math.floor(i * columnSpacing + columnSpacing / 2)
724+
);
725+
const columnYPositions = Array(numColumns).fill(initialY);
726+
727+
// Helper function to move a block and its connected group
701728
const moveBlockAndGroup = (blk, x, y) => {
702729
const myBlock = this.blocks.blockList[blk];
703730
const dx = x - myBlock.container.x;
@@ -715,34 +742,44 @@ class Activity {
715742
}
716743
}
717744
};
718-
745+
746+
// Position blocks in columns
719747
for (const blk in this.blocks.blockList) {
720748
if (!this.blocks.blockList[blk].trash) {
721749
const myBlock = this.blocks.blockList[blk];
750+
751+
// Skip blocks already connected
722752
if (myBlock.connections[0] !== null) {
723753
continue;
724754
}
725-
if (placeInLeftColumn) {
726-
moveBlockAndGroup(blk, leftColumnX, leftColumnY);
727-
leftColumnY += myBlock.height + verticalSpacing;
728-
} else {
729-
moveBlockAndGroup(blk, rightColumnX, rightColumnY);
730-
rightColumnY += myBlock.height + verticalSpacing;
755+
756+
// Determine column and position
757+
let minYIndex = 0;
758+
for (let i = 1; i < numColumns; i++) {
759+
if (columnYPositions[i] < columnYPositions[minYIndex]) {
760+
minYIndex = i;
761+
}
731762
}
732-
placeInLeftColumn = !placeInLeftColumn;
763+
764+
moveBlockAndGroup(blk, columnXPositions[minYIndex], columnYPositions[minYIndex]);
765+
columnYPositions[minYIndex] += myBlock.height + verticalSpacing;
733766
}
734767
}
735768

769+
// Blocks are all home, so reset go-home-button.
736770
this.setHomeContainers(false);
737771
this.boundary.hide();
772+
773+
// Return mice to the center of the screen.
774+
// Reset turtles' positions to center of the screen
738775
for (let turtle = 0; turtle < this.turtles.turtleList.length; turtle++) {
739776
const savedPenState = this.turtles.turtleList[turtle].painter.penState;
740777
this.turtles.turtleList[turtle].painter.penState = false;
741778
this.turtles.turtleList[turtle].painter.doSetXY(0, 0);
742779
this.turtles.turtleList[turtle].painter.doSetHeading(0);
743780
this.turtles.turtleList[turtle].painter.penState = savedPenState;
744781
}
745-
};
782+
};
746783

747784
/**
748785
* Toggles the visibility of the home button container.

0 commit comments

Comments
 (0)