From acc0c1bd556496fe18764e17432c78afa332024f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 4 Feb 2021 19:14:06 +0100 Subject: [PATCH] [Editor] Improve brushes --- .../base/editor/render/canvas/map/MapBrush.kt | 26 +++---- .../editor/render/canvas/map/MapCanvas.kt | 68 ++++--------------- .../render/canvas/map/MapMouseEventHandler.kt | 5 ++ .../base/editor/view/component/map/MapPane.kt | 16 +---- 4 files changed, 33 insertions(+), 82 deletions(-) create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapMouseEventHandler.kt diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapBrush.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapBrush.kt index 6939ce53..5a50befd 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapBrush.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapBrush.kt @@ -10,7 +10,7 @@ class MapBrush( private val map: GameMap, private val brush: Array>, private val paintingCallback: (MapPaintingTrace) -> Unit -) : Renderable { +) : Renderable, MapMouseEventHandler { private val tileWidth = map.tileSet.tileWidth.toDouble() private val tileHeight = map.tileSet.tileHeight.toDouble() private val centerRow: Int @@ -52,7 +52,7 @@ class MapBrush( ) } - fun handleMouseInput(event: MapMouseEvent) { + override fun handleMouseInput(event: MapMouseEvent) { mouseRow = event.row mouseColumn = event.column @@ -63,10 +63,13 @@ class MapBrush( } } - private fun commitTrace() { - currentTrace?.let { - paintingCallback(it) - currentTrace = null + private fun beginTrace() { + currentTrace = MapPaintingTrace(map, "Paint trace").apply { + for ((row, columns) in brush.withIndex()) { + for ((column, tile) in columns.withIndex()) { + paint(0, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) + } + } } } @@ -80,13 +83,10 @@ class MapBrush( } } - private fun beginTrace() { - currentTrace = MapPaintingTrace(map, "Paint trace").apply { - for ((row, columns) in brush.withIndex()) { - for ((column, tile) in columns.withIndex()) { - paint(0, mouseRow - centerRow + row, mouseColumn - centerColumn + column, tile) - } - } + private fun commitTrace() { + currentTrace?.let { + paintingCallback(it) + currentTrace = null } } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt index 002ef564..c5c878c9 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt @@ -1,14 +1,15 @@ package com.bartlomiejpluta.base.editor.render.canvas.map -import com.bartlomiejpluta.base.editor.model.map.map.GameMap import com.bartlomiejpluta.base.editor.model.map.layer.Layer import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer +import com.bartlomiejpluta.base.editor.model.map.map.GameMap +import com.bartlomiejpluta.base.editor.model.tileset.Tile import com.bartlomiejpluta.base.editor.render.model.Renderable import javafx.scene.canvas.GraphicsContext -import org.apache.commons.logging.LogFactory -class MapCanvas(val map: GameMap, var brush: MapBrush) : Renderable { +class MapCanvas(val map: GameMap, private val paintingCallback: (MapPaintingTrace) -> Unit) : Renderable, + MapMouseEventHandler { var tileSet = map.tileSet private var layers = map.layers private var rows = map.rows @@ -18,28 +19,19 @@ class MapCanvas(val map: GameMap, var brush: MapBrush) : Renderable { private var mapWidth = map.width.toDouble() private var mapHeight = map.height.toDouble() - var mouseColumn = -1 - private set - - var mouseRow = -1 - private set - - // private val brush = MapBrush( -// this, arrayOf( -// arrayOf(tileSet.getTile(140, 4), tileSet.getTile(140, 4), tileSet.getTile(140, 4)), -// arrayOf(tileSet.getTile(140, 4), tileSet.getTile(140, 4), tileSet.getTile(140, 4)), -// arrayOf(tileSet.getTile(140, 4), tileSet.getTile(140, 4), tileSet.getTile(140, 4)) -// ) -// ) - + private var brush = MapBrush(map, arrayOf(arrayOf(tileSet.getTile(0, 0))), paintingCallback) + fun setBrush(brush: Array>) { + this.brush = MapBrush(map, brush, paintingCallback) + } override fun render(gc: GraphicsContext) { gc.clearRect(0.0, 0.0, gc.canvas.width, gc.canvas.height); layers.forEach { dispatchLayerRender(gc, it) } renderGrid(gc) - renderCursor(gc) + + brush.render(gc) } private fun dispatchLayerRender(gc: GraphicsContext, layer: Layer) { @@ -61,7 +53,7 @@ class MapCanvas(val map: GameMap, var brush: MapBrush) : Renderable { private fun renderGrid(gc: GraphicsContext) { gc.lineWidth = 1.5 - for (row in 0..rows - 1) { + for (row in 0 until rows) { gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight) } @@ -70,41 +62,7 @@ class MapCanvas(val map: GameMap, var brush: MapBrush) : Renderable { } } - private fun renderCursor(gc: GraphicsContext) { -// if (mouseColumn >= 4 && mouseRow >= 4) { -// gc.globalAlpha.let { alpha -> -// gc.globalAlpha = 0.4 -// gc.drawImage(tile.image, mouseColumn * tileWidth, mouseRow * tileHeight) -// gc.globalAlpha = alpha -// } - - brush.render(gc) -// } - } - - fun handleMouseInput(event: MapMouseEvent) { - mouseRow = event.row - mouseColumn = event.column - -// when (event.type) { -// MouseEvent.MOUSE_PRESSED -> { -// currentTrace = MapPaintingTrace(map, "Paint trace").apply { -// paint(0, mouseRow, mouseColumn, tile) -// } -// } -// -// MouseEvent.MOUSE_DRAGGED -> currentTrace?.apply { -// paint(0, mouseRow, mouseColumn, tile) -// } -// -// MouseEvent.MOUSE_RELEASED -> currentTrace?.let { -// paintingCallback(it) -// currentTrace = null -// } -// } - } - - companion object { - private val log = LogFactory.getLog(MapCanvas::class.java) + override fun handleMouseInput(event: MapMouseEvent) { + brush.handleMouseInput(event) } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapMouseEventHandler.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapMouseEventHandler.kt new file mode 100755 index 00000000..66df5a7b --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapMouseEventHandler.kt @@ -0,0 +1,5 @@ +package com.bartlomiejpluta.base.editor.render.canvas.map + +interface MapMouseEventHandler { + fun handleMouseInput(event: MapMouseEvent) +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt index 780425c3..ebf78fc7 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt @@ -1,7 +1,6 @@ package com.bartlomiejpluta.base.editor.view.component.map import com.bartlomiejpluta.base.editor.model.map.map.GameMap -import com.bartlomiejpluta.base.editor.render.canvas.map.MapBrush import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas import com.bartlomiejpluta.base.editor.render.canvas.map.MapMouseEvent import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace @@ -10,17 +9,9 @@ import javafx.scene.canvas.Canvas import javafx.scene.input.MouseEvent - class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canvas(), EventHandler { private var tileSet = map.tileSet - private var brush: MapBrush - private val mapCanvas: MapCanvas - - private val brushDefinition = arrayOf( - arrayOf(tileSet.getTile(140, 4), tileSet.getTile(140, 5), tileSet.getTile(140, 6)), - arrayOf(tileSet.getTile(141, 4), tileSet.getTile(141, 5), tileSet.getTile(141, 6)), - arrayOf(tileSet.getTile(142, 4), tileSet.getTile(142, 5), tileSet.getTile(142, 6)) - ) + private val mapCanvas = MapCanvas(map, paintingCallback) init { onMouseMoved = this @@ -28,9 +19,6 @@ class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canv onMousePressed = this onMouseReleased = this - brush = MapBrush(map, brushDefinition, paintingCallback) - mapCanvas = MapCanvas(map, brush) - tileSet = map.tileSet width = map.width.toDouble() height = map.height.toDouble() @@ -43,7 +31,7 @@ class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canv override fun handle(event: MouseEvent?) { if (event != null) { - brush.handleMouseInput(MapMouseEvent.of(event, tileSet)) + mapCanvas.handleMouseInput(MapMouseEvent.of(event, tileSet)) } mapCanvas.render(graphicsContext2D)