[Editor] Enable drawing with selected brush
This commit is contained in:
@@ -26,7 +26,7 @@ class TileSet(private val image: Image, val rows: Int, val columns: Int) {
|
|||||||
return Tile(this, row, column, tile)
|
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]
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.render.canvas.tileset
|
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.model.tileset.TileSet
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
|
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.canvas.GraphicsContext
|
||||||
import javafx.scene.input.MouseButton
|
import javafx.scene.input.MouseButton
|
||||||
import javafx.scene.input.MouseEvent
|
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 val tiles = tileSet.tiles
|
||||||
private var selection = TileSetSelection(tileSet)
|
private var selection = TileSetSelection(tileSet, selectionCallback)
|
||||||
|
|
||||||
private var mouseRow = -1
|
private var mouseRow = -1
|
||||||
private var mouseColumn = -1
|
private var mouseColumn = -1
|
||||||
@@ -38,6 +38,7 @@ class TileSetCanvas(private val tileSet: TileSet) : Renderable, MapMouseEventHan
|
|||||||
when (event.type) {
|
when (event.type) {
|
||||||
MouseEvent.MOUSE_PRESSED -> beginSelection(event)
|
MouseEvent.MOUSE_PRESSED -> beginSelection(event)
|
||||||
MouseEvent.MOUSE_DRAGGED -> proceedSelection(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) {
|
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) {
|
if(event.button == MouseButton.PRIMARY) {
|
||||||
selection.finish(event.row.toDouble(), event.column.toDouble())
|
selection.finish(event.row.toDouble(), event.column.toDouble())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
package com.bartlomiejpluta.base.editor.render.canvas.tileset
|
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.model.tileset.TileSet
|
||||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
|
import javafx.collections.ObservableList
|
||||||
import javafx.scene.canvas.GraphicsContext
|
import javafx.scene.canvas.GraphicsContext
|
||||||
import javafx.scene.paint.Color
|
import javafx.scene.paint.Color
|
||||||
import org.apache.commons.logging.LogFactory
|
import org.apache.commons.logging.LogFactory
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import tornadofx.observableListOf
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
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 tileWidth = tileSet.tileWidth.toDouble()
|
||||||
private val tileHeight = tileSet.tileHeight.toDouble()
|
private val tileHeight = tileSet.tileHeight.toDouble()
|
||||||
|
|
||||||
@@ -24,6 +31,7 @@ class TileSetSelection(tileSet: TileSet) : Renderable {
|
|||||||
private var width = tileWidth
|
private var width = tileWidth
|
||||||
private var height = tileHeight
|
private var height = tileHeight
|
||||||
|
|
||||||
|
|
||||||
fun begin(row: Double, column: Double) {
|
fun begin(row: Double, column: Double) {
|
||||||
startRow = row
|
startRow = row
|
||||||
offsetRow = 0.0
|
offsetRow = 0.0
|
||||||
@@ -40,13 +48,33 @@ class TileSetSelection(tileSet: TileSet) : Renderable {
|
|||||||
height = (offsetRow + 1) * tileHeight
|
height = (offsetRow + 1) * tileHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
fun finish(row: Double, column: Double) {
|
fun proceed(row: Double, column: Double) {
|
||||||
offsetRow = abs(row - startRow)
|
offsetRow = abs(row - startRow)
|
||||||
offsetColumn = abs(column - startColumn)
|
offsetColumn = abs(column - startColumn)
|
||||||
|
|
||||||
updateRect(row, column)
|
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) {
|
override fun render(gc: GraphicsContext) {
|
||||||
gc.fill = SELECTION_FILL_COLOR
|
gc.fill = SELECTION_FILL_COLOR
|
||||||
gc.fillRect(x, y, width, height)
|
gc.fillRect(x, y, width, height)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.component.map
|
package com.bartlomiejpluta.base.editor.view.component.map
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
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.map.MapCanvas
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
|
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
|
||||||
@@ -25,6 +26,8 @@ class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canv
|
|||||||
render()
|
render()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setBrush(brush: Array<Array<Tile>>) = mapCanvas.setBrush(brush)
|
||||||
|
|
||||||
fun render() {
|
fun render() {
|
||||||
mapCanvas.render(graphicsContext2D)
|
mapCanvas.render(graphicsContext2D)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.component.tileset
|
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.model.tileset.TileSet
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||||
import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas
|
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.canvas.Canvas
|
||||||
import javafx.scene.input.MouseEvent
|
import javafx.scene.input.MouseEvent
|
||||||
|
|
||||||
class TileSetPane(private val tileSet: TileSet) : Canvas(), EventHandler<MouseEvent> {
|
class TileSetPane(private val tileSet: TileSet, selectionCallback: (Array<Array<Tile>>) -> Unit) : Canvas(), EventHandler<MouseEvent> {
|
||||||
private val tileSetCanvas = TileSetCanvas(tileSet)
|
private val tileSetCanvas = TileSetCanvas(tileSet, selectionCallback)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
onMouseMoved = this
|
onMouseMoved = this
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ class MapFragment : Fragment() {
|
|||||||
val scaleProperty = SimpleDoubleProperty(1.0)
|
val scaleProperty = SimpleDoubleProperty(1.0)
|
||||||
val map: GameMap by param()
|
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 {
|
private val transformation = Scale(1.0, 1.0, 0.0, 0.0).apply {
|
||||||
xProperty().bind(scaleProperty)
|
xProperty().bind(scaleProperty)
|
||||||
@@ -25,7 +26,7 @@ class MapFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
subscribe<RedrawMapRequestEvent> { pane.render() }
|
subscribe<RedrawMapRequestEvent> { mapPane.render() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val root = borderpane {
|
override val root = borderpane {
|
||||||
@@ -44,14 +45,14 @@ class MapFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group {
|
group {
|
||||||
this += pane
|
this += mapPane
|
||||||
transforms += transformation
|
transforms += transformation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
right = scrollpane {
|
right = scrollpane {
|
||||||
this += TileSetPane(map.tileSet)
|
this += tileSetPane
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user