Skip to content

Commit

Permalink
Added Duplicate keyboard shortcut to the InfoModal
Browse files Browse the repository at this point in the history
Altered duplicateElement logic not overwrite existing elements
  • Loading branch information
RianM committed Oct 26, 2022
1 parent fa0c91c commit 2cdc228
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/components/layout/Layout.ts
Expand Up @@ -137,6 +137,9 @@ export class Layout {
this.elements = [];
}

// Duplicate element in the selected cell
// It will either be placed in the hoveredCell, if it is not a wall and not already occupied
// Or it will be placed in the first empty cell
duplicateElement(
selectedCell: [number, number],
hoveredCell: [number, number] | undefined,
Expand All @@ -150,20 +153,24 @@ export class Layout {

const square = this.layout[selectedCellX][selectedCellY] as SquareType;

if (hoveredCell) {
if (hoveredCell && this.isEmptySquare(hoveredCell[0], hoveredCell[1])) {
this.setElement(hoveredCell[0], hoveredCell[1], square.clone());
} else {
// If hoveredCell is undefined, duplicate to first Empty Square
for (let i = 0; i < this.height * 2 - 1; i++) {
for (let j = 0; j < this.width * 2 - 1; j++) {
if (this.layout[i][j] == SquareType.Empty) {
this.setElement(i, j, square);
if (this.isEmptySquare(i, j)) {
this.setElement(i, j, square.clone());
return;
}
}
}
}
}

isEmptySquare(x: number, y: number): boolean {
const square = this.layout[x][y] as SquareType;
return square && square === SquareType.Empty;
}
}

export function encodeLayoutString(layout: Layout) {
Expand Down
4 changes: 4 additions & 0 deletions src/components/modals/infoModal/InfoModal.tsx
Expand Up @@ -68,6 +68,10 @@ const InfoModal = ({ className }: Props) => {
<kbd>Q</kbd> <kbd>E</kbd>
</div>
<span>rotate selected item left/right</span>
<div>
<kbd>CTRL+D</kbd>
</div>
<span>duplicate selected item</span>
<div>
<kbd>?</kbd>
</div>
Expand Down

0 comments on commit 2cdc228

Please sign in to comment.