[Editor] Remove unnecessary TileSetVM view-model object
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.input
|
||||
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||
import javafx.event.EventType
|
||||
import javafx.scene.input.MouseButton
|
||||
import javafx.scene.input.MouseEvent
|
||||
@@ -8,7 +8,7 @@ import javafx.scene.input.MouseEvent
|
||||
class MapMouseEvent(val row: Int, val column: Int, val type: EventType<out MouseEvent>, val button: MouseButton) {
|
||||
|
||||
companion object {
|
||||
fun of(event: MouseEvent, tileSet: TileSetVM) = MapMouseEvent(
|
||||
fun of(event: MouseEvent, tileSet: TileSet) = MapMouseEvent(
|
||||
(event.y / tileSet.tileHeight).toInt(),
|
||||
(event.x / tileSet.tileWidth).toInt(),
|
||||
event.eventType,
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.bartlomiejpluta.base.editor.render.canvas.layer
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
|
||||
class TileLayerCanvas(private val tileLayer: TileLayer) : Renderable {
|
||||
|
||||
override fun render(gc: GraphicsContext) {
|
||||
for ((row, columns) in tileLayer.layer.withIndex()) {
|
||||
for ((column, tile) in columns.withIndex()) {
|
||||
if (tile != null) {
|
||||
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,14 @@ import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEventHandler
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
import javafx.scene.input.MouseButton
|
||||
import javafx.scene.input.MouseEvent
|
||||
import javafx.scene.paint.Color
|
||||
|
||||
class TileSetCanvas(private val tileSetVM: TileSetVM, brushVM: BrushVM) : Renderable, MapMouseEventHandler {
|
||||
private var selection = TileSetSelection(tileSetVM, brushVM)
|
||||
class TileSetCanvas(private val gameMapVM: GameMapVM, brushVM: BrushVM) : Renderable, MapMouseEventHandler {
|
||||
private var selection = TileSetSelection(gameMapVM, brushVM)
|
||||
|
||||
private var mouseRow = -1
|
||||
private var mouseColumn = -1
|
||||
@@ -24,13 +24,13 @@ class TileSetCanvas(private val tileSetVM: TileSetVM, brushVM: BrushVM) : Render
|
||||
}
|
||||
|
||||
private fun renderTiles(gc: GraphicsContext) {
|
||||
tileSetVM.forEach { row, column, tile ->
|
||||
gameMapVM.tileSet.forEach { row, column, tile ->
|
||||
gc.fill = if ((row + column) % 2 == 0) BACKGROUND_COLOR1 else BACKGROUND_COLOR2
|
||||
gc.fillRect(
|
||||
column * tile.image.width,
|
||||
row * tile.image.height,
|
||||
tileSetVM.tileWidth.toDouble(),
|
||||
tileSetVM.tileHeight.toDouble()
|
||||
gameMapVM.tileSet.tileWidth.toDouble(),
|
||||
gameMapVM.tileSet.tileHeight.toDouble()
|
||||
)
|
||||
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
||||
}
|
||||
|
||||
@@ -5,32 +5,23 @@ 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 com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
import javafx.scene.paint.Color
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.min
|
||||
|
||||
|
||||
class TileSetSelection(
|
||||
private val tileSetVM: TileSetVM,
|
||||
private val brushVM: BrushVM
|
||||
) : Renderable {
|
||||
class TileSetSelection(private val gameMapVM: GameMapVM, private val brushVM: BrushVM) : Renderable {
|
||||
private var startRow = 0.0
|
||||
private var startColumn = 0.0
|
||||
private var offsetRow = 0.0
|
||||
private var offsetColumn = 0.0
|
||||
private var x = 0.0
|
||||
private var y = 0.0
|
||||
private var width = 0.0
|
||||
private var height = 0.0
|
||||
private var width = gameMapVM.tileSet.tileWidth.toDouble()
|
||||
private var height = gameMapVM.tileSet.tileHeight.toDouble()
|
||||
|
||||
init {
|
||||
tileSetVM.itemProperty.addListener { _, _, tileSet ->
|
||||
width = tileSet.tileWidth.toDouble()
|
||||
height = tileSet.tileHeight.toDouble()
|
||||
}
|
||||
}
|
||||
|
||||
fun begin(row: Double, column: Double) {
|
||||
startRow = row
|
||||
@@ -42,10 +33,10 @@ class TileSetSelection(
|
||||
}
|
||||
|
||||
private fun updateRect(row: Double, column: Double) {
|
||||
x = min(column, startColumn) * tileSetVM.tileWidth
|
||||
y = min(row, startRow) * tileSetVM.tileHeight
|
||||
width = (offsetColumn + 1) * tileSetVM.tileWidth
|
||||
height = (offsetRow + 1) * tileSetVM.tileHeight
|
||||
x = min(column, startColumn) * gameMapVM.tileSet.tileWidth
|
||||
y = min(row, startRow) * gameMapVM.tileSet.tileHeight
|
||||
width = (offsetColumn + 1) * gameMapVM.tileSet.tileWidth
|
||||
height = (offsetRow + 1) * gameMapVM.tileSet.tileHeight
|
||||
}
|
||||
|
||||
fun proceed(row: Double, column: Double) {
|
||||
@@ -68,7 +59,7 @@ class TileSetSelection(
|
||||
|
||||
val brushArray = Array(rows) { rowIndex ->
|
||||
Array(columns) { columnIndex ->
|
||||
tileSetVM.getTile(firstRow + rowIndex, firstColumn + columnIndex)
|
||||
gameMapVM.tileSet.getTile(firstRow + rowIndex, firstColumn + columnIndex)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,15 +6,12 @@ import com.bartlomiejpluta.base.editor.render.canvas.map.MapPainter
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.map.MapPaintingTrace
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import javafx.event.EventHandler
|
||||
import javafx.scene.canvas.Canvas
|
||||
import javafx.scene.input.MouseEvent
|
||||
|
||||
|
||||
class MapPane(
|
||||
mapVM: GameMapVM,
|
||||
private val tileSetVM: TileSetVM,
|
||||
class MapPane(private val mapVM: GameMapVM,
|
||||
brushVM: BrushVM,
|
||||
paintingCallback: (MapPaintingTrace) -> Unit
|
||||
) : Canvas(), EventHandler<MouseEvent> {
|
||||
@@ -29,17 +26,17 @@ class MapPane(
|
||||
|
||||
width = mapVM.width.toDouble()
|
||||
height = mapVM.height.toDouble()
|
||||
|
||||
render()
|
||||
}
|
||||
|
||||
|
||||
fun render() {
|
||||
mapCanvas.render(graphicsContext2D)
|
||||
}
|
||||
|
||||
override fun handle(event: MouseEvent?) {
|
||||
if (event != null) {
|
||||
painter.handleMouseInput(MapMouseEvent.of(event, tileSetVM))
|
||||
painter.handleMouseInput(MapMouseEvent.of(event, mapVM.tileSet))
|
||||
}
|
||||
|
||||
mapCanvas.render(graphicsContext2D)
|
||||
|
||||
@@ -3,13 +3,13 @@ package com.bartlomiejpluta.base.editor.view.component.tileset
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.input.MapMouseEvent
|
||||
import com.bartlomiejpluta.base.editor.render.canvas.tileset.TileSetCanvas
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import javafx.event.EventHandler
|
||||
import javafx.scene.canvas.Canvas
|
||||
import javafx.scene.input.MouseEvent
|
||||
|
||||
class TileSetPane(private val tileSetVM: TileSetVM, brushVM: BrushVM) : Canvas(), EventHandler<MouseEvent> {
|
||||
private val tileSetCanvas = TileSetCanvas(tileSetVM, brushVM)
|
||||
class TileSetPane(private val gameMapVM: GameMapVM, brushVM: BrushVM) : Canvas(), EventHandler<MouseEvent> {
|
||||
private val tileSetCanvas = TileSetCanvas(gameMapVM, brushVM)
|
||||
|
||||
init {
|
||||
onMouseMoved = this
|
||||
@@ -17,10 +17,10 @@ class TileSetPane(private val tileSetVM: TileSetVM, brushVM: BrushVM) : Canvas()
|
||||
onMousePressed = this
|
||||
onMouseReleased = this
|
||||
|
||||
widthProperty().bind(tileSetVM.widthProperty)
|
||||
heightProperty().bind(tileSetVM.heightProperty)
|
||||
width = gameMapVM.tileSet.width.toDouble()
|
||||
height = gameMapVM.tileSet.height.toDouble()
|
||||
|
||||
tileSetVM.itemProperty.addListener { _, _, _ -> render() }
|
||||
render()
|
||||
}
|
||||
|
||||
private fun render() {
|
||||
@@ -29,7 +29,7 @@ class TileSetPane(private val tileSetVM: TileSetVM, brushVM: BrushVM) : Canvas()
|
||||
|
||||
override fun handle(event: MouseEvent?) {
|
||||
if (event != null) {
|
||||
tileSetCanvas.handleMouseInput(MapMouseEvent.of(event, tileSetVM))
|
||||
tileSetCanvas.handleMouseInput(MapMouseEvent.of(event, gameMapVM.tileSet))
|
||||
}
|
||||
|
||||
tileSetCanvas.render(graphicsContext2D)
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import javafx.beans.property.SimpleDoubleProperty
|
||||
import javafx.scene.input.MouseButton
|
||||
import javafx.scene.input.MouseEvent
|
||||
@@ -27,9 +26,8 @@ class MapView : View() {
|
||||
|
||||
private val mapVM = find<GameMapVM>()
|
||||
private val brushVM = find<BrushVM>()
|
||||
private val tileSetVM = find<TileSetVM>()
|
||||
|
||||
private val mapPane = MapPane(mapVM, tileSetVM, brushVM) { undoRedoService.push(it) }
|
||||
private val mapPane = MapPane(mapVM, brushVM) { undoRedoService.push(it) }
|
||||
|
||||
init {
|
||||
brushVM.item = mapVM.tileSet.baseBrush
|
||||
|
||||
@@ -3,21 +3,15 @@ package com.bartlomiejpluta.base.editor.view.map
|
||||
import com.bartlomiejpluta.base.editor.view.component.tileset.TileSetPane
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.GameMapVM
|
||||
import com.bartlomiejpluta.base.editor.viewmodel.map.TileSetVM
|
||||
import tornadofx.View
|
||||
import tornadofx.plusAssign
|
||||
import tornadofx.scrollpane
|
||||
|
||||
class TileSetView : View() {
|
||||
private val gameMapVM = find<GameMapVM>()
|
||||
private val tileSetVM = find<TileSetVM>()
|
||||
private val brushVM = find<BrushVM>()
|
||||
|
||||
private val tileSetPane = TileSetPane(tileSetVM, brushVM)
|
||||
|
||||
init {
|
||||
tileSetVM.itemProperty.bind(gameMapVM.tileSetProperty)
|
||||
}
|
||||
private val tileSetPane = TileSetPane(gameMapVM, brushVM)
|
||||
|
||||
override val root = scrollpane {
|
||||
this += tileSetPane
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.bartlomiejpluta.base.editor.viewmodel.map
|
||||
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.Tile
|
||||
import com.bartlomiejpluta.base.editor.model.tileset.TileSet
|
||||
import javafx.collections.ObservableList
|
||||
import tornadofx.*
|
||||
|
||||
class TileSetVM : ItemViewModel<TileSet>() {
|
||||
val rowsProperty = bind(TileSet::rowsProperty)
|
||||
val rows by rowsProperty
|
||||
|
||||
val columnsProperty = bind(TileSet::columnsProperty)
|
||||
val columns by columnsProperty
|
||||
|
||||
val tileWidthProperty = bind(TileSet::tileWidthProperty)
|
||||
val tileWidth by tileWidthProperty
|
||||
|
||||
val tileHeightProperty = bind(TileSet::tileHeightProperty)
|
||||
val tileHeight by tileHeightProperty
|
||||
|
||||
val widthProperty = bind(TileSet::widthProperty)
|
||||
val width by widthProperty
|
||||
|
||||
val heightProperty = bind(TileSet::heightProperty)
|
||||
val height by heightProperty
|
||||
|
||||
val tiles: ObservableList<Tile> = bind(TileSet::tiles)
|
||||
|
||||
fun getTile(row: Int, column: Int) = item.getTile(row, column)
|
||||
|
||||
fun getTile(id: Int) = item.getTile(id)
|
||||
|
||||
fun forEach(consumer: (row: Int, column: Int, tile: Tile) -> Unit) = item.forEach(consumer)
|
||||
}
|
||||
Reference in New Issue
Block a user