From 1f02518cd82780a43ac66d75e6da604beee8ed68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Mon, 8 Mar 2021 20:01:06 +0100 Subject: [PATCH] Remove unnecessary PassageAbility (other than ALLOW and BLOCK) --- .../game/map/layer/object/PassageAbility.java | 6 +- .../base/editor/map/canvas/MapPainter.kt | 2 +- .../editor/map/canvas/ObjectPaintingCursor.kt | 21 +++- .../editor/map/canvas/ObjectPaintingTrace.kt | 100 ++++++++++++------ .../editor/map/canvas/PassageAbilitySymbol.kt | 68 ------------ .../map/model/enumeration/PassageAbility.kt | 6 +- .../map/serial/ProtobufMapDeserializer.kt | 4 - .../map/serial/ProtobufMapSerializer.kt | 4 - .../editor/map/view/editor/MapToolbarView.kt | 2 +- .../map/serial/ProtobufMapDeserializer.java | 4 - proto/src/main/proto/map.proto | 4 - 11 files changed, 86 insertions(+), 135 deletions(-) diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/map/layer/object/PassageAbility.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/map/layer/object/PassageAbility.java index 1d2b8e94..98fecb39 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/map/layer/object/PassageAbility.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/map/layer/object/PassageAbility.java @@ -2,9 +2,5 @@ package com.bartlomiejpluta.base.api.game.map.layer.object; public enum PassageAbility { BLOCK, - ALLOW, - UP_ONLY, - DOWN_ONLY, - LEFT_ONLY, - RIGHT_ONLY + ALLOW } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapPainter.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapPainter.kt index 5f13126d..662e622f 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapPainter.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/MapPainter.kt @@ -28,7 +28,7 @@ class MapPainter( editorStateVM.selectedLayerProperty.addListener { _, _, layer -> cursor = when (layer) { is TileLayer -> TilePaintingCursor(tileWidth, tileHeight, editorStateVM, brushVM) - is ObjectLayer -> ObjectPaintingCursor(tileWidth, tileHeight, editorStateVM) + is ObjectLayer -> ObjectPaintingCursor(tileWidth, tileHeight, editorStateVM, brushVM) else -> null } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingCursor.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingCursor.kt index 7a57f113..af164b58 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingCursor.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingCursor.kt @@ -1,5 +1,7 @@ package com.bartlomiejpluta.base.editor.map.canvas +import com.bartlomiejpluta.base.editor.map.model.brush.BrushMode +import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM import javafx.scene.canvas.GraphicsContext import javafx.scene.paint.Color @@ -7,19 +9,30 @@ import javafx.scene.paint.Color class ObjectPaintingCursor( private val tileWidth: Double, private val tileHeight: Double, - private val editorStateVM: EditorStateVM + private val editorStateVM: EditorStateVM, + private val brushVM: BrushVM ) : PaintingCursor { override fun render(gc: GraphicsContext) { + brushVM.forEach { row, column, centerRow, centerColumn, tile -> + renderTile(gc, row, column, centerRow, centerColumn) + } + } + + private fun renderTile(gc: GraphicsContext, row: Int, column: Int, centerRow: Int, centerColumn: Int) { val alpha = gc.globalAlpha val stroke = gc.stroke val width = gc.lineWidth gc.globalAlpha = 1.0 - gc.stroke = Color.WHITE + gc.stroke = when (brushVM.mode!!) { + BrushMode.PAINTING_MODE -> Color.RED + BrushMode.ERASING_MODE -> Color.WHITE + } gc.lineWidth = 3.0 - val x = editorStateVM.cursorColumn * tileWidth - val y = editorStateVM.cursorRow * tileHeight + + val x = tileWidth * (editorStateVM.cursorColumn - centerColumn + column) + val y = tileHeight * (editorStateVM.cursorRow - centerRow + row) gc.strokeLine(x + tileWidth / 2, y + (1 - SIZE) * tileHeight, x + tileWidth / 2, y + SIZE * tileHeight) gc.strokeLine(x + (1 - SIZE) * tileWidth, y + tileHeight / 2, x + SIZE * tileWidth, y + tileHeight / 2) diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingTrace.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingTrace.kt index 5265db5d..8251edfb 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingTrace.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/ObjectPaintingTrace.kt @@ -10,56 +10,86 @@ import com.bartlomiejpluta.base.editor.render.input.MapMouseEvent import javafx.scene.input.MouseButton class ObjectPaintingTrace(val map: GameMapVM, override val commandName: String) : PaintingTrace { - private var layerIndex = 0 - private var row = 0 - private var column = 0 - private lateinit var layer: ObjectLayer - private lateinit var formerPassageAbility: PassageAbility - private lateinit var passageAbility: PassageAbility - - - override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { - // Do nothing - } - - override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { - // Do nothing - } - - override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { - this.layerIndex = editorStateVM.selectedLayerIndex - this.row = editorStateVM.cursorRow - this.column = editorStateVM.cursorColumn + private val trace = mutableListOf() + private fun paint(layerIndex: Int, row: Int, column: Int, passageAbility: PassageAbility) { if (row >= map.rows || column >= map.columns || row < 0 || column < 0 || layerIndex < 0) { return } - this.layer = (map.layers[layerIndex] as ObjectLayer) + val passageMap = (map.layers[layerIndex] as ObjectLayer).passageMap + val formerPassageAbility = passageMap[row][column] - formerPassageAbility = layer.passageMap[row][column] - - passageAbility = when (mouseEvent.button) { - MouseButton.PRIMARY -> when (brushVM.mode!!) { - BrushMode.PAINTING_MODE -> PassageAbility.values()[(formerPassageAbility.ordinal + 1) % PassageAbility.values().size] - BrushMode.ERASING_MODE -> PassageAbility.ALLOW - } - - MouseButton.SECONDARY -> PassageAbility.ALLOW - - else -> throw IllegalStateException("Unsupported mouse button") + if (trace.isEmpty()) { + trace += Element(layerIndex, row, column, formerPassageAbility, passageAbility) + passageMap[row][column] = passageAbility + return } - layer.passageMap[row][column] = passageAbility + val tileAlreadyPainted = + trace.find { it.layerIndex == layerIndex && it.row == row && it.column == column } != null + + if (!tileAlreadyPainted) { + trace += Element(layerIndex, row, column, formerPassageAbility, passageAbility) + passageMap[row][column] = passageAbility + } + } + + override fun beginTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { + brushVM.forEach { row, column, centerRow, centerColumn, _ -> + paint( + editorStateVM.selectedLayerIndex, + editorStateVM.cursorRow - centerRow + row, + editorStateVM.cursorColumn - centerColumn + column, + when { + brushVM.mode == BrushMode.ERASING_MODE -> PassageAbility.ALLOW + mouseEvent.button == MouseButton.PRIMARY -> PassageAbility.BLOCK + else -> PassageAbility.ALLOW + } + ) + } + } + + override fun proceedTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { + brushVM.forEach { row, column, centerRow, centerColumn, _ -> + paint( + editorStateVM.selectedLayerIndex, + editorStateVM.cursorRow - centerRow + row, + editorStateVM.cursorColumn - centerColumn + column, + when { + brushVM.mode == BrushMode.ERASING_MODE -> PassageAbility.ALLOW + mouseEvent.button == MouseButton.PRIMARY -> PassageAbility.BLOCK + else -> PassageAbility.ALLOW + } + ) + } + } + + override fun commitTrace(editorStateVM: EditorStateVM, brushVM: BrushVM, mouseEvent: MapMouseEvent) { + } override fun undo() { - layer.passageMap[row][column] = formerPassageAbility + trace.forEach { + (map.layers[it.layerIndex] as ObjectLayer).passageMap[it.row][it.column] = it.formerPassageAbility + } } override fun redo() { - layer.passageMap[row][column] = passageAbility + trace.forEach { + (map.layers[it.layerIndex] as ObjectLayer).passageMap[it.row][it.column] = it.passageAbility + } } override val supportedButtons = arrayOf(MouseButton.PRIMARY, MouseButton.SECONDARY) + + companion object { + private data class Element( + val layerIndex: Int, + val row: Int, + val column: Int, + val formerPassageAbility: PassageAbility, + val passageAbility: PassageAbility + ) + } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/PassageAbilitySymbol.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/PassageAbilitySymbol.kt index 72ec285b..5a1a07c1 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/PassageAbilitySymbol.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/canvas/PassageAbilitySymbol.kt @@ -14,10 +14,6 @@ object PassageAbilitySymbol { when (passageAbility) { PassageAbility.ALLOW -> allow(gc, x, y, w, h) PassageAbility.BLOCK -> block(gc, x, y, w, h) - PassageAbility.UP_ONLY -> up(gc, x, y, w, h) - PassageAbility.DOWN_ONLY -> down(gc, x, y, w, h) - PassageAbility.LEFT_ONLY -> left(gc, x, y, w, h) - PassageAbility.RIGHT_ONLY -> right(gc, x, y, w, h) } gc.fill = fill @@ -35,68 +31,4 @@ object PassageAbilitySymbol { gc.globalAlpha = 0.4 gc.fillRect(x, y, w, h) } - - fun down(gc: GraphicsContext, x: Double, y: Double, w: Double, h: Double) { - gc.fill = Color.GREEN - gc.globalAlpha = 0.1 - gc.fillRect(x, y, w, h) - gc.globalAlpha = 1.0 - - gc.fill = Color.WHITE - gc.beginPath() - gc.moveTo(x + (1 - SIZE) * w, y + (1 - SIZE) * h) - gc.lineTo(x + w * SIZE, y + (1 - SIZE) * h) - gc.lineTo(x + w / 2, y + h * SIZE) - gc.closePath() - - gc.fill() - } - - fun up(gc: GraphicsContext, x: Double, y: Double, w: Double, h: Double) { - gc.fill = Color.GREEN - gc.globalAlpha = 0.1 - gc.fillRect(x, y, w, h) - gc.globalAlpha = 1.0 - - gc.fill = Color.WHITE - gc.beginPath() - gc.moveTo(x + (1 - SIZE) * w, y + h * SIZE) - gc.lineTo(x + w * SIZE, y + h * SIZE) - gc.lineTo(x + w / 2, y + (1 - SIZE) * h) - gc.closePath() - - gc.fill() - } - - fun left(gc: GraphicsContext, x: Double, y: Double, w: Double, h: Double) { - gc.fill = Color.GREEN - gc.globalAlpha = 0.1 - gc.fillRect(x, y, w, h) - gc.globalAlpha = 1.0 - - gc.fill = Color.WHITE - gc.beginPath() - gc.moveTo(x + (1 - SIZE) * w, y + h / 2) - gc.lineTo(x + SIZE * w, y + (1 - SIZE) * h) - gc.lineTo(x + w * SIZE, y + h * SIZE) - gc.closePath() - - gc.fill() - } - - fun right(gc: GraphicsContext, x: Double, y: Double, w: Double, h: Double) { - gc.fill = Color.GREEN - gc.globalAlpha = 0.1 - gc.fillRect(x, y, w, h) - gc.globalAlpha = 1.0 - - gc.fill = Color.WHITE - gc.beginPath() - gc.moveTo(x + SIZE * w, y + h / 2) - gc.lineTo(x + (1 - SIZE) * w, y + (1 - SIZE) * h) - gc.lineTo(x + w * (1 - SIZE), y + h * SIZE) - gc.closePath() - - gc.fill() - } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/enumeration/PassageAbility.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/enumeration/PassageAbility.kt index 62da4a14..5dc8c451 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/enumeration/PassageAbility.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/model/enumeration/PassageAbility.kt @@ -2,9 +2,5 @@ package com.bartlomiejpluta.base.editor.map.model.enumeration enum class PassageAbility { ALLOW, - BLOCK, - UP_ONLY, - DOWN_ONLY, - LEFT_ONLY, - RIGHT_ONLY + BLOCK } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapDeserializer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapDeserializer.kt index f5ba5016..26c36f0f 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapDeserializer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapDeserializer.kt @@ -65,10 +65,6 @@ class ProtobufMapDeserializer : MapDeserializer { passageMap[index / columns][index % columns] = when (passage!!) { GameMapProto.PassageAbility.ALLOW -> PassageAbility.ALLOW GameMapProto.PassageAbility.BLOCK -> PassageAbility.BLOCK - GameMapProto.PassageAbility.UP_ONLY -> PassageAbility.UP_ONLY - GameMapProto.PassageAbility.DOWN_ONLY -> PassageAbility.DOWN_ONLY - GameMapProto.PassageAbility.LEFT_ONLY -> PassageAbility.LEFT_ONLY - GameMapProto.PassageAbility.RIGHT_ONLY -> PassageAbility.RIGHT_ONLY } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapSerializer.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapSerializer.kt index de694f11..56387f3c 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapSerializer.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/serial/ProtobufMapSerializer.kt @@ -37,10 +37,6 @@ class ProtobufMapSerializer : MapSerializer { when (passage) { PassageAbility.ALLOW -> GameMapProto.PassageAbility.ALLOW PassageAbility.BLOCK -> GameMapProto.PassageAbility.BLOCK - PassageAbility.UP_ONLY -> GameMapProto.PassageAbility.UP_ONLY - PassageAbility.DOWN_ONLY -> GameMapProto.PassageAbility.DOWN_ONLY - PassageAbility.LEFT_ONLY -> GameMapProto.PassageAbility.LEFT_ONLY - PassageAbility.RIGHT_ONLY -> GameMapProto.PassageAbility.RIGHT_ONLY } ) } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt index dadc4b6e..3e63817c 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt @@ -110,7 +110,7 @@ class MapToolbarView : View() { isSnapToTicks = true minorTickCount = 0 - enableWhen(isTileLayerSelected) + enableWhen(isTileLayerSelected.or(isObjectLayerSelected)) valueProperty().addListener { _, _, newValue -> brushVM.item = brushVM.withRange(newValue.toInt()) diff --git a/engine/src/main/java/com/bartlomiejpluta/base/engine/world/map/serial/ProtobufMapDeserializer.java b/engine/src/main/java/com/bartlomiejpluta/base/engine/world/map/serial/ProtobufMapDeserializer.java index c4b11ac5..c3cee42f 100644 --- a/engine/src/main/java/com/bartlomiejpluta/base/engine/world/map/serial/ProtobufMapDeserializer.java +++ b/engine/src/main/java/com/bartlomiejpluta/base/engine/world/map/serial/ProtobufMapDeserializer.java @@ -73,10 +73,6 @@ public class ProtobufMapDeserializer extends MapDeserializer { layer.setPassageAbility(i / columns, i % columns, switch (passageMap.get(i)) { case ALLOW -> PassageAbility.ALLOW; case BLOCK -> PassageAbility.BLOCK; - case RIGHT_ONLY -> PassageAbility.RIGHT_ONLY; - case LEFT_ONLY -> PassageAbility.LEFT_ONLY; - case DOWN_ONLY -> PassageAbility.DOWN_ONLY; - case UP_ONLY -> PassageAbility.UP_ONLY; }); } } diff --git a/proto/src/main/proto/map.proto b/proto/src/main/proto/map.proto index db268792..8c7bc64d 100644 --- a/proto/src/main/proto/map.proto +++ b/proto/src/main/proto/map.proto @@ -34,10 +34,6 @@ message ObjectLayer { enum PassageAbility { ALLOW = 0; BLOCK = 1; - UP_ONLY = 2; - DOWN_ONLY = 3; - LEFT_ONLY = 4; - RIGHT_ONLY = 5; } message ColorLayer {