From 6b9d75897cb7515315590a8fc82da26dfddd70f9 Mon Sep 17 00:00:00 2001 From: FinemechanicPub <93194456+FinemechanicPub@users.noreply.github.com> Date: Wed, 21 Aug 2024 18:11:16 +0300 Subject: [PATCH 01/17] Extract usePoints composable --- frontend/src/components/PieceItem.vue | 21 ++++--------------- frontend/src/composables/points.js | 30 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 frontend/src/composables/points.js diff --git a/frontend/src/components/PieceItem.vue b/frontend/src/components/PieceItem.vue index 84b5656..2d79ceb 100644 --- a/frontend/src/components/PieceItem.vue +++ b/frontend/src/components/PieceItem.vue @@ -2,6 +2,7 @@ import { computed, ref } from 'vue' import divmod from '@/utils/divmod' + import { usePoints } from '@/composables/points' const props = defineProps({ piece: Object, @@ -15,12 +16,7 @@ const rotation = computed(() => props.piece.rotations[props.piece.base_version]) const points = computed(() => rotation.value.points) const colorString = computed(() => `#${props.piece.color.toString(16)}`) - const maxX = computed(() => Math.max(...points.value.map((point) => point[1]))) - 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 grid = computed(make_grid) - const width = computed(() => maxX.value - minX.value + 1) + const {left, width, diameter, grid } = usePoints(points) const canRotate = computed(() => props.piece.rotations.length > 1) const flipIndex = computed(() => 1 + props.piece.rotations.findLastIndex((item) => item.flipped === 0)) const canFlip = computed(() => flipIndex.value < props.piece.rotations.length) @@ -33,15 +29,6 @@ const touchShiftX = computed(() => Math.round(touchCurrent.value.length ? touchCurrent.value[0] - touchStart.value[0] : 0) + "px") const touchShiftY = computed(() => Math.round(touchCurrent.value.length ? touchCurrent.value[1] - touchStart.value[1] - scrollShift.value : 0) + "px") - - function make_grid(){ - const grid = Array(maxY.value + 1).fill().map(()=>Array(maxX.value - minX.value + 1).fill(false)) - for (const [y, x] of points.value){ - grid[y][x - minX.value] = true - } - return grid - } - function on_mouse_down(index){ console.log(`mouse down in cell #${index}`) mouse_index.value = index @@ -57,7 +44,7 @@ const offsetY = Math.round(evt.clientY - pieceRect.top) % cellSize - halfSize const piece_data = { dy: -dy, - dx: -dx - minX.value, + dx: -dx - left.value, pieceId: props.piece.id, rotationId: rotation.value.id, offsetX: offsetX, @@ -106,7 +93,7 @@ const [dy, dx] = divmod(mouse_index.value, width.value) const piece_data = { dy: -dy, - dx: -dx - minX.value, + dx: -dx - left.value, pieceId: props.piece.id, rotationId: rotation.value.id, touchXY: touchCurrent.value diff --git a/frontend/src/composables/points.js b/frontend/src/composables/points.js new file mode 100644 index 0000000..82b9a5c --- /dev/null +++ b/frontend/src/composables/points.js @@ -0,0 +1,30 @@ +import { ref, toValue, watchEffect } from 'vue' + +export function usePoints(points){ + const grid = ref([]) + const left = ref(0) + const width = ref(0) + const diameter = ref(0) + + const process = () => { + const _points = toValue(points) + const maxX = Math.max(..._points.map((point) => point[1])) + const minX = Math.min(..._points.map((point) => point[1])) + const maxY = Math.max(..._points.map((point) => point[0])) + const _width = maxX - minX + 1 + const _height = maxY + 1 + left.value = minX + width.value = _width + diameter.value = Math.max(_width, _height) + + const _grid = Array(maxY + 1).fill().map(()=>Array(_width).fill(false)) + for (const [y, x] of _points){ + _grid[y][x - minX] = true + } + grid.value = _grid + } + + watchEffect(process) + + return {left, width, diameter, grid } +} From b78739176d9ba59d8ae904b770e3980acd92c2db Mon Sep 17 00:00:00 2001 From: FinemechanicPub <93194456+FinemechanicPub@users.noreply.github.com> Date: Wed, 21 Aug 2024 18:55:49 +0300 Subject: [PATCH 02/17] Extract useTransform composable --- frontend/src/components/PieceItem.vue | 40 ++++----------------------- frontend/src/composables/transform.js | 39 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 34 deletions(-) create mode 100644 frontend/src/composables/transform.js diff --git a/frontend/src/components/PieceItem.vue b/frontend/src/components/PieceItem.vue index 2d79ceb..56e9e71 100644 --- a/frontend/src/components/PieceItem.vue +++ b/frontend/src/components/PieceItem.vue @@ -3,6 +3,7 @@ import divmod from '@/utils/divmod' import { usePoints } from '@/composables/points' + import { useTransform } from '@/composables/transform' const props = defineProps({ piece: Object, @@ -17,9 +18,7 @@ const points = computed(() => rotation.value.points) const colorString = computed(() => `#${props.piece.color.toString(16)}`) const {left, width, diameter, grid } = usePoints(points) - const canRotate = computed(() => props.piece.rotations.length > 1) - const flipIndex = computed(() => 1 + props.piece.rotations.findLastIndex((item) => item.flipped === 0)) - const canFlip = computed(() => flipIndex.value < props.piece.rotations.length) + const { flip, rotate } = useTransform(props.piece.rotations) const mouse_index = ref(null) @@ -102,33 +101,6 @@ console.log("touch end") emit("pieceTouch", piece_data) } - - - function rotate(index, turns) { - const piece = props.piece - const length = piece.rotations.length - if (canFlip.value){ - const half_length = length / 2 - if (index < half_length){ - return (half_length + index + turns) % half_length - } else { - return half_length + (index - turns) % half_length - } - } else { // no flips for this piece - return (length + index + turns) % length - } - } - - function flip(index, horizontal){ - const piece = props.piece - const cycleLength = piece.rotations.length > 2 ? Math.floor(piece.rotations.length / 2) : 0 - const new_index = (index + cycleLength) % piece.rotations.length - if (horizontal && flipIndex.value > 2){ - return rotate(new_index, 2) - } - return new_index - } -