[Editor] Enable drawing with selected brush

This commit is contained in:
2021-02-04 22:16:12 +01:00
parent 90c2cdcd95
commit 18219a065f
6 changed files with 53 additions and 13 deletions

View File

@@ -26,7 +26,7 @@ class TileSet(private val image: Image, val rows: Int, val columns: Int) {
return Tile(this, row, column, tile)
}
fun getTile(row: Int, column: Int) = tiles[row][column]
fun getTile(row: Int, column: Int) = tiles[row.coerceIn(0 until rows)][column.coerceIn(0 until columns)]
fun getTile(id: Int) = tiles[id / rows][id % columns]
fun getTile(id: Int) = tiles[id / columns][id % columns]
}

View File

@@ -1,5 +1,6 @@
package com.bartlomiejpluta.base.editor.render.canvas.tileset
import com.bartlomiejpluta.base.editor.model.tileset.Tile
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
@@ -7,11 +8,10 @@ import com.bartlomiejpluta.base.editor.render.model.Renderable
import javafx.scene.canvas.GraphicsContext
import javafx.scene.input.MouseButton
import javafx.scene.input.MouseEvent
import javafx.scene.paint.Color
class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHandler {
class TileSetCanvas(tileSet: TileSet, selectionCallback: (Array<Array<Tile>>) -> Unit) : Renderable, MapMouseEventHandler {
private val tiles = tileSet.tiles
private var selection = TileSetSelection(tileSet)
private var selection = TileSetSelection(tileSet, selectionCallback)
private var mouseRow = -1
private var mouseColumn = -1
@@ -38,6 +38,7 @@ class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHan
when (event.type) {
MouseEvent.MOUSE_PRESSED -> beginSelection(event)
MouseEvent.MOUSE_DRAGGED -> proceedSelection(event)
MouseEvent.MOUSE_RELEASED -> finishSelection(event)
}
}
@@ -48,6 +49,12 @@ class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHan
}
private fun proceedSelection(event: MapMouseEvent) {
if(event.button == MouseButton.PRIMARY) {
selection.proceed(event.row.toDouble(), event.column.toDouble())
}
}
private fun finishSelection(event: MapMouseEvent) {
if(event.button == MouseButton.PRIMARY) {
selection.finish(event.row.toDouble(), event.column.toDouble())
}

View File

@@ -1,17 +1,24 @@
package com.bartlomiejpluta.base.editor.render.canvas.tileset
import com.bartlomiejpluta.base.editor.model.tileset.Tile
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import com.bartlomiejpluta.base.editor.render.model.Renderable
import javafx.collections.ObservableList
import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color
import org.apache.commons.logging.LogFactory
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tornadofx.observableListOf
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
class TileSetSelection(tileSet: TileSet) : Renderable {
class TileSetSelection(
private val tileSet: TileSet,
private val selectionCallback: (Array<Array<Tile>>) -> Unit
) : Renderable {
private val tileWidth = tileSet.tileWidth.toDouble()
private val tileHeight = tileSet.tileHeight.toDouble()
@@ -24,6 +31,7 @@ class TileSetSelection(tileSet: TileSet) : Renderable {
private var width = tileWidth
private var height = tileHeight
fun begin(row: Double, column: Double) {
startRow = row
offsetRow = 0.0
@@ -40,13 +48,33 @@ class TileSetSelection(tileSet: TileSet) : Renderable {
height = (offsetRow + 1) * tileHeight
}
fun finish(row: Double, column: Double) {
fun proceed(row: Double, column: Double) {
offsetRow = abs(row - startRow)
offsetColumn = abs(column - startColumn)
updateRect(row, column)
}
fun finish(row: Double, column: Double) {
proceed(row, column)
startRow = min(row, startRow)
startColumn = min(column, startColumn)
val firstRow = startRow.toInt()
val firstColumn = startColumn.toInt()
val rows = offsetRow.toInt() + 1
val columns = offsetColumn.toInt() + 1
var brush = Array<Array<Tile>>(rows) { rowIndex ->
Array<Tile>(columns) { columnIndex ->
tileSet.getTile(firstRow + rowIndex, firstColumn + columnIndex)
}
}
selectionCallback(brush)
}
override fun render(gc: GraphicsContext) {
gc.fill = SELECTION_FILL_COLOR
gc.fillRect(x, y, width, height)

View File

@@ -1,6 +1,7 @@
package com.bartlomiejpluta.base.editor.view.component.map
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
import com.bartlomiejpluta.base.editor.model.tileset.Tile
import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
@@ -25,6 +26,8 @@ class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canv
render()
}
fun setBrush(brush: Array<Array<Tile>>) = mapCanvas.setBrush(brush)
fun render() {
mapCanvas.render(graphicsContext2D)
}

View File

@@ -1,5 +1,6 @@
package com.bartlomiejpluta.base.editor.view.component.tileset
import com.bartlomiejpluta.base.editor.model.tileset.Tile
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas
@@ -7,8 +8,8 @@ import javafx.event.EventHandler
import javafx.scene.canvas.Canvas
import javafx.scene.input.MouseEvent
class TileSetPane(private val tileSet: TileSet) : Canvas(), EventHandler<MouseEvent> {
private val tileSetCanvas = TileSetCanvas(tileSet)
class TileSetPane(private val tileSet: TileSet, selectionCallback: (Array<Array<Tile>>) -> Unit) : Canvas(), EventHandler<MouseEvent> {
private val tileSetCanvas = TileSetCanvas(tileSet, selectionCallback)
init {
onMouseMoved = this

View File

@@ -17,7 +17,8 @@ class MapFragment : Fragment() {
val scaleProperty = SimpleDoubleProperty(1.0)
val map: GameMap by param()
private val pane = MapPane(map) { undoRedoService.push(it) }
private val mapPane = MapPane(map) { undoRedoService.push(it) }
private val tileSetPane = TileSetPane(map.tileSet) { mapPane.setBrush(it) }
private val transformation = Scale(1.0, 1.0, 0.0, 0.0).apply {
xProperty().bind(scaleProperty)
@@ -25,7 +26,7 @@ class MapFragment : Fragment() {
}
init {
subscribe<RedrawMapRequestEvent> { pane.render() }
subscribe<RedrawMapRequestEvent> { mapPane.render() }
}
override val root = borderpane {
@@ -44,14 +45,14 @@ class MapFragment : Fragment() {
}
group {
this += pane
this += mapPane
transforms += transformation
}
}
}
right = scrollpane {
this += TileSetPane(map.tileSet)
this += tileSetPane
}
}
}