[Editor] Improve brushes

This commit is contained in:
2021-02-04 19:14:06 +01:00
parent e5f1ef1260
commit acc0c1bd55
4 changed files with 33 additions and 82 deletions

View File

@@ -10,7 +10,7 @@ class MapBrush(
private val map: GameMap,
private val brush: Array<Array<Tile>>,
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
}
}
}

View File

@@ -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<Array<Tile>>) {
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)
}
}

View File

@@ -0,0 +1,5 @@
package com.bartlomiejpluta.base.editor.render.canvas.map
interface MapMouseEventHandler {
fun handleMouseInput(event: MapMouseEvent)
}

View File

@@ -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<MouseEvent> {
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)