From eff24127da1272c9cafee85794106eaccc87c28e Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Wed, 13 May 2026 00:44:02 +0200 Subject: [PATCH] Improve layer clamping logic in terrain brush --- src/core/terrain-brush.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/core/terrain-brush.ts b/src/core/terrain-brush.ts index 814d31a5..b7c7ae21 100644 --- a/src/core/terrain-brush.ts +++ b/src/core/terrain-brush.ts @@ -1,4 +1,7 @@ -import { TERRAIN_LAYER_COUNT } from "../document/terrains"; +import { + MAX_TERRAIN_LAYER_COUNT, + TERRAIN_LAYER_COUNT +} from "../document/terrains"; export type TerrainBrushTool = | "raise" @@ -129,15 +132,20 @@ export function createDefaultTerrainBrushSettings(): TerrainBrushSettings { }; } -export function clampTerrainPaintLayerIndex(layerIndex: number): number { +export function clampTerrainPaintLayerIndex( + layerIndex: number, + layerCount = TERRAIN_LAYER_COUNT +): number { if (!Number.isFinite(layerIndex)) { return 0; } - return Math.min( - TERRAIN_LAYER_COUNT - 1, - Math.max(0, Math.round(layerIndex)) - ); + const normalizedLayerCount = + Number.isInteger(layerCount) && layerCount > 0 + ? Math.min(layerCount, MAX_TERRAIN_LAYER_COUNT) + : TERRAIN_LAYER_COUNT; + + return Math.min(normalizedLayerCount - 1, Math.max(0, Math.round(layerIndex))); } export function getTerrainBrushToolLabel(tool: TerrainBrushTool): string {