Skip to content

Commit

Permalink
Refactor to use css var for cell width
Browse files Browse the repository at this point in the history
  • Loading branch information
FinemechanicPub committed Aug 20, 2024
1 parent 822fad3 commit eb0625a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 20 deletions.
7 changes: 1 addition & 6 deletions frontend/src/components/BoardGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
const props = defineProps({
width: Number,
height: Number,
cellSize: Number,
installed_pieces: Array
})
const emit = defineEmits(['install', 'remove'])
const cell_width = `${props.cellSize}px`
const board = ref(null)
const grid = ref(Array(props.height).fill().map(()=>Array(props.width).fill(0xffffff)))
Expand Down Expand Up @@ -48,10 +46,7 @@
return
}
const piece_data = JSON.parse(raw_data)
const cellRect = evt.target.getBoundingClientRect()
const amendX = Math.floor((evt.clientX - cellRect.left - piece_data.offsetX) / props.cellSize)
const amendY = Math.floor((evt.clientY - cellRect.top - piece_data.offsetY) / props.cellSize)
const corrected_index = index + piece_data.dx + amendX + (piece_data.dy + amendY)*props.width
const corrected_index = index + piece_data.dx + piece_data.dy*props.width
emit('install', piece_data.pieceId, piece_data.rotationId, corrected_index)
}
Expand Down
13 changes: 4 additions & 9 deletions frontend/src/components/PieceItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
const props = defineProps({
piece: Object,
cellSize: Number
})
const emit = defineEmits(['pieceTouch', 'changeVersion'])
const cells = ref([])
const cell_width = `${props.cellSize - 2}px`
const hovering = ref(false)
const rotation = computed(() => props.piece.rotations[props.piece.base_version])
Expand All @@ -22,7 +19,6 @@
const minX = computed(() => Math.min(...points.value.map((point) => point[1])))
const maxY = computed(() => Math.max(...points.value.map((point) => point[0])))
const diameter = computed(() => (1 + Math.max(maxX.value - minX.value, maxY.value)))
const box_width = computed(() => `${diameter.value*(props.cellSize)}px`)
const grid = computed(make_grid)
const width = computed(() => maxX.value - minX.value + 1)
const canRotate = computed(() => props.piece.rotations.length > 1)
Expand Down Expand Up @@ -55,9 +51,10 @@
console.log('dragging piece')
const [dy, dx] = divmod(mouse_index.value, width.value)
const pieceRect = evt.target.getBoundingClientRect()
const halfSize = props.cellSize / 2
const offsetX = Math.round(evt.clientX - pieceRect.left) % props.cellSize - halfSize
const offsetY = Math.round(evt.clientY - pieceRect.top) % props.cellSize - halfSize
const cellSize = Math.max(pieceRect.height, pieceRect.width) / diameter.value
const halfSize = cellSize / 2
const offsetX = Math.round(evt.clientX - pieceRect.left) % cellSize - halfSize
const offsetY = Math.round(evt.clientY - pieceRect.top) % cellSize - halfSize
const piece_data = {
dy: -dy,
dx: -dx - minX.value,
Expand Down Expand Up @@ -156,9 +153,7 @@
}
.piece-cell {
aspect-ratio: 1/ 1;
/* width: v-bind(cell_width); */
width: calc(var(--cell-width) - 2px);
/* display: flex; */
margin: 1px;
}
.colored {
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/components/PiecePalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const props = defineProps({
availablePieces: Array,
cellSize: Number
})
const emit = defineEmits(["pieceTouch", "changeVersion"])
Expand All @@ -28,7 +27,6 @@
@changeVersion="(versionIndex) => emit('changeVersion', versionIndex, index)"
@piece-touch="onPieceTouch"
:piece="piece"
:cell-size="cellSize"
/>
</div>
</div>
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/views/GameView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
const hintbox = ref(null)
const cellSize = 30
const loading = ref(false)
const error = ref(null)
const game = ref(null)
Expand Down Expand Up @@ -239,9 +238,9 @@
<p>Доска заполнена!</p>
</div>
</div>
<BoardGrid id="board" ref="board" @install="handleInstall" @remove="handleRemove" :width="game.width" :height="game.height" :cell-size="cellSize" :installed_pieces="installedPieces" />
<BoardGrid id="board" ref="board" @install="handleInstall" @remove="handleRemove" :width="game.width" :height="game.height" :installed_pieces="installedPieces" />
<HintBox id="hintbox" ref="hintbox" @hint="hint => handleInstall(...hint)" :gameId="game.id" :installedPices="installedPieces" />
<PiecePalette id="palette" @changeVersion="onChangeVersion" @piece-touch="onPieceTouch" :availablePieces="availablePieces" :cell-size="cellSize"/>
<PiecePalette id="palette" @changeVersion="onChangeVersion" @piece-touch="onPieceTouch" :availablePieces="availablePieces"/>
</div>
<GameTour v-if="game && hintbox" :hasHint="hintbox.hasHint" :hasPieces="availablePieces.length > 0"/>
</template>

0 comments on commit eb0625a

Please sign in to comment.