Skip to content

Commit 8a45d17

Browse files
author
alferio
committed
feat(scatter-over-surface):
Implemented the random-tool finish behavior. Changed: - core/src/main/kotlin/com/github/alfu32/sketch/tools/RandomTools.kt - RandomSurfaceArrayTool now commits immediately after payload pick, restores the initially captured target surface selection, clears its internal payload/surface state, and exits. - RandomOffsetTool now exits after a successful apply while preserving the selected edited geometry. - MeshRegularizeTool now exits after successful apply; since it replaces the source mesh, the replacement faces remain selected. - core/src/main/kotlin/com/github/alfu32/sketch/Main.kt - Wired those tools to return to Select on successful completion.
1 parent ae5a6b8 commit 8a45d17

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

core/src/main/kotlin/com/github/alfu32/sketch/Main.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,13 +915,13 @@ class Main @JvmOverloads constructor(
915915
ToolId.MESH_REGULARIZE,
916916
MeshRegularizeMode.PLANAR,
917917
randomToolSettings
918-
),
918+
) { toolController.setTool(ToolId.SELECT) },
919919
MeshRegularizeTool(
920920
scene,
921921
ToolId.SURFACE_REMESH,
922922
MeshRegularizeMode.SURFACE,
923923
randomToolSettings
924-
),
924+
) { toolController.setTool(ToolId.SELECT) },
925925
RectangleTool(scene),
926926
SurfaceRectangleTool(scene),
927927
QuadTool(scene),
@@ -941,8 +941,12 @@ class Main @JvmOverloads constructor(
941941
ScaleTool(scene),
942942
StretchTool(scene),
943943
StretchScaleTool(scene),
944-
RandomOffsetTool(scene, randomToolSettings),
945-
RandomSurfaceArrayTool(scene, randomToolSettings, ::pickRandomSurfaceArrayPayloadGroup),
944+
RandomOffsetTool(scene, randomToolSettings) { toolController.setTool(ToolId.SELECT) },
945+
RandomSurfaceArrayTool(
946+
scene,
947+
randomToolSettings,
948+
::pickRandomSurfaceArrayPayloadGroup
949+
) { toolController.setTool(ToolId.SELECT) },
946950
RotateStretchTool(scene),
947951
CopyMultipleTool(scene),
948952
PlanarTranslateMultipleTool(scene),

core/src/main/kotlin/com/github/alfu32/sketch/tools/RandomTools.kt

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ enum class MeshRegularizeMode {
3838

3939
class RandomOffsetTool(
4040
private val scene: GroupScene,
41-
private val settings: RandomToolSettings
41+
private val settings: RandomToolSettings,
42+
private val onFinished: (() -> Unit)? = null
4243
) : Tool {
4344
override val id: ToolId = ToolId.RANDOM_OFFSET
4445
override val message: String = "Random Offset: type strength 0-100, then click to offset selected faces/segments."
@@ -80,6 +81,9 @@ class RandomOffsetTool(
8081
}
8182
val result = applyRandomOffset()
8283
status.message = result
84+
if (result.startsWith("Random Offset moved")) {
85+
onFinished?.invoke()
86+
}
8387
return true
8488
}
8589

@@ -309,7 +313,8 @@ class RandomOffsetTool(
309313
class RandomSurfaceArrayTool(
310314
private val scene: GroupScene,
311315
private val settings: RandomToolSettings,
312-
private val pickPayloadGroup: (Int, Int) -> GroupScene.GroupNode? = { _, _ -> null }
316+
private val pickPayloadGroup: (Int, Int) -> GroupScene.GroupNode? = { _, _ -> null },
317+
private val onFinished: (() -> Unit)? = null
313318
) : Tool {
314319
override val id: ToolId = ToolId.RANDOM_SURFACE_ARRAY
315320
override val message: String =
@@ -378,13 +383,13 @@ class RandomSurfaceArrayTool(
378383
val picked = pickPayloadGroup(screenX, screenY)
379384
status.message = if (picked != null) {
380385
capturePayloadGroup(picked)
381-
applyArray()
386+
applyArrayAndFinish()
382387
} else {
383388
"Random Surface Array: click an object instance to scatter, or use Payload after selecting payload geometry."
384389
}
385390
return true
386391
}
387-
status.message = applyArray()
392+
status.message = applyArrayAndFinish()
388393
return true
389394
}
390395

@@ -398,7 +403,7 @@ class RandomSurfaceArrayTool(
398403
if (button != Input.Buttons.LEFT) {
399404
return false
400405
}
401-
status.message = applyArray()
406+
status.message = applyArrayAndFinish()
402407
return true
403408
}
404409

@@ -504,6 +509,24 @@ class RandomSurfaceArrayTool(
504509
return "Random Surface Array copied $copiedGroups object instance(s), $copiedSegments segment(s), and $copiedFaces face(s)."
505510
}
506511

512+
private fun applyArrayAndFinish(): String {
513+
val result = applyArray()
514+
if (result.startsWith("Random Surface Array copied")) {
515+
restoreCapturedSurfaceSelection()
516+
payloadSelection = null
517+
surfaceSelection = null
518+
onFinished?.invoke()
519+
}
520+
return result
521+
}
522+
523+
private fun restoreCapturedSurfaceSelection() {
524+
val group = scene.activeGroup()
525+
val faces = surfaceSelection?.faces?.filter { it in group.faceStore.getTriangles() }.orEmpty()
526+
scene.clearAllSelections()
527+
faces.forEach { group.faceStore.addSelection(it) }
528+
}
529+
507530
private fun surfaceArrayPreview(): SurfaceArrayPreview? {
508531
val group = scene.activeGroup()
509532
val surface = currentSurfaceSelection(group)
@@ -734,7 +757,8 @@ class MeshRegularizeTool(
734757
private val scene: GroupScene,
735758
private val toolId: ToolId = ToolId.MESH_REGULARIZE,
736759
private val mode: MeshRegularizeMode = MeshRegularizeMode.PLANAR,
737-
private val settings: RandomToolSettings
760+
private val settings: RandomToolSettings,
761+
private val onFinished: (() -> Unit)? = null
738762
) : Tool {
739763
override val id: ToolId = toolId
740764
override val message: String = "${toolLabel()}: select faces, configure subdivisions and tolerance, then remesh."
@@ -771,7 +795,11 @@ class MeshRegularizeTool(
771795
if (button != Input.Buttons.LEFT) {
772796
return false
773797
}
774-
status.message = regularize()
798+
val result = regularize()
799+
status.message = result
800+
if (result.startsWith("${toolLabel()} replaced")) {
801+
onFinished?.invoke()
802+
}
775803
return true
776804
}
777805

0 commit comments

Comments
 (0)