From 4e1c2596af99031c77afea835db96fdd8f729e47 Mon Sep 17 00:00:00 2001 From: QADRAX Date: Mon, 21 Apr 2025 17:47:22 +0200 Subject: [PATCH 1/4] chore: add tile hovering --- package.json | 2 +- .../ManualCamera/ManualCamera.useHandlers.ts | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 528b8b4..7ceffdb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-super-tilemap", "license": "MIT", - "version": "1.1.0", + "version": "1.1.1", "private": false, "description": "React implementation of a low-level 2D tilemap board optimized for high-performance rendering in web browsers", "author": "Carlos Cuadra", diff --git a/src/components/Camera/ManualCamera/ManualCamera.useHandlers.ts b/src/components/Camera/ManualCamera/ManualCamera.useHandlers.ts index 8d4f96f..f82364b 100644 --- a/src/components/Camera/ManualCamera/ManualCamera.useHandlers.ts +++ b/src/components/Camera/ManualCamera/ManualCamera.useHandlers.ts @@ -1,3 +1,4 @@ +import { useRef } from 'react'; import { Position } from '../../../types/Position'; import { floorTilePosition, getTilePosition, isTilePositionValid } from '../../../utils/positions'; import { useTilemapContext } from '../../Tilemap/TilemapContext/useTilemapContext'; @@ -16,6 +17,8 @@ export function useHandlers(): Partial { const { cameraPosition, canvasSize } = state; const { tileSize, mapDimensions } = computed; + const lastHoveredTileRef = useRef(null); + const getTilePositionByMousePosition = (mousePosition: Position) => { if (cameraPosition && canvasSize) { const position = getTilePosition(mousePosition, cameraPosition, tileSize, canvasSize); @@ -24,6 +27,32 @@ export function useHandlers(): Partial { return null; }; + const handleMouseMove = (position: Position) => { + const tilePosition = getTilePositionByMousePosition(position); + if (!tilePosition) return; + + const result = floorTilePosition(tilePosition); + const last = lastHoveredTileRef.current; + + const isValid = isTilePositionValid(result, mapDimensions); + + if (!isValid) { + if (last !== null) { + contextProps.onTileHoverOut?.(last); + lastHoveredTileRef.current = null; + } + return; + } + + if (!last || last.x !== result.x || last.y !== result.y) { + if (last) { + contextProps.onTileHoverOut?.(last); + } + contextProps.onTileHover?.(result); + lastHoveredTileRef.current = result; + } + }; + const handleClick = (position: Position) => { const tilePosition = getTilePositionByMousePosition(position); if (tilePosition) { @@ -65,5 +94,6 @@ export function useHandlers(): Partial { handleClick, handleDoubleClick, handleContextMenu, + handleMouseMove, }; } From 80c06dba08ce51860288f134d7d924d3348a6bc4 Mon Sep 17 00:00:00 2001 From: QADRAX Date: Mon, 21 Apr 2025 17:53:31 +0200 Subject: [PATCH 2/4] chore: update build pipeline --- .github/workflows/Build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Build.yaml b/.github/workflows/Build.yaml index 5360dd2..fefb398 100644 --- a/.github/workflows/Build.yaml +++ b/.github/workflows/Build.yaml @@ -28,7 +28,7 @@ jobs: cache: 'npm' - name: Restore node modules cache - uses: actions/cache@v2 + uses: actions/cache@v3 env: cache-name: cache-node-modules with: From bec9036939cd6efc0ddefacbff9db7f158069b84 Mon Sep 17 00:00:00 2001 From: QADRAX Date: Mon, 21 Apr 2025 17:54:56 +0200 Subject: [PATCH 3/4] chore: remove cache --- .github/workflows/Build.yaml | 8 -------- .github/workflows/Publish.yaml | 8 -------- 2 files changed, 16 deletions(-) diff --git a/.github/workflows/Build.yaml b/.github/workflows/Build.yaml index fefb398..61a610f 100644 --- a/.github/workflows/Build.yaml +++ b/.github/workflows/Build.yaml @@ -27,14 +27,6 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' - - name: Restore node modules cache - uses: actions/cache@v3 - env: - cache-name: cache-node-modules - with: - path: node_modules - key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }} - - name: Install Dependencies run: yarn install diff --git a/.github/workflows/Publish.yaml b/.github/workflows/Publish.yaml index 8abce32..fbdbfa3 100644 --- a/.github/workflows/Publish.yaml +++ b/.github/workflows/Publish.yaml @@ -23,14 +23,6 @@ jobs: cache: 'npm' registry-url: 'https://registry.npmjs.org' - - name: Restore node modules cache - uses: actions/cache@v2 - env: - cache-name: cache-node-modules - with: - path: node_modules - key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }} - - name: Install Dependencies run: yarn install From 97ffc306b04fee849e9f4ce5a5f54d16b7f795d2 Mon Sep 17 00:00:00 2001 From: QADRAX Date: Mon, 21 Apr 2025 17:56:29 +0200 Subject: [PATCH 4/4] chore: update build --- .github/workflows/Build.yaml | 1 - .github/workflows/Publish.yaml | 2 -- 2 files changed, 3 deletions(-) diff --git a/.github/workflows/Build.yaml b/.github/workflows/Build.yaml index 61a610f..df29726 100644 --- a/.github/workflows/Build.yaml +++ b/.github/workflows/Build.yaml @@ -25,7 +25,6 @@ jobs: uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - cache: 'npm' - name: Install Dependencies run: yarn install diff --git a/.github/workflows/Publish.yaml b/.github/workflows/Publish.yaml index fbdbfa3..9bf3bba 100644 --- a/.github/workflows/Publish.yaml +++ b/.github/workflows/Publish.yaml @@ -20,8 +20,6 @@ jobs: uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - cache: 'npm' - registry-url: 'https://registry.npmjs.org' - name: Install Dependencies run: yarn install