[Editor] Implement TabPane in MainView | make MapFragment immutable
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.event
|
||||
|
||||
import tornadofx.EventBus.RunOn.BackgroundThread
|
||||
import tornadofx.FXEvent
|
||||
|
||||
object RedrawMapRequestEvent : FXEvent(BackgroundThread)
|
||||
@@ -11,38 +11,26 @@ import javafx.scene.paint.Color
|
||||
import org.apache.commons.logging.LogFactory
|
||||
|
||||
|
||||
class MapCanvas(private val paintingCallback: (MapPaintingTrace) -> Unit) : Renderable {
|
||||
private var map: GameMap? = null
|
||||
private var tileSet: TileSet? = null
|
||||
private var layers: List<Layer>? = null
|
||||
private var rows: Int? = null
|
||||
private var columns: Int? = null
|
||||
private var tileWidth: Double? = null
|
||||
private var tileHeight: Double? = null
|
||||
private var mapWidth: Double? = null
|
||||
private var mapHeight: Double? = null
|
||||
class MapCanvas(private val map: GameMap, private val paintingCallback: (MapPaintingTrace) -> Unit) : Renderable {
|
||||
private var tileSet = map.tileSet
|
||||
private var layers = map.layers
|
||||
private var rows = map.rows
|
||||
private var columns = map.columns
|
||||
private var tileWidth = tileSet.tileWidth.toDouble()
|
||||
private var tileHeight = tileSet.tileHeight.toDouble()
|
||||
private var mapWidth = map.width.toDouble()
|
||||
private var mapHeight = map.height.toDouble()
|
||||
|
||||
private var mouseColumn = -1
|
||||
private var mouseRow = -1
|
||||
|
||||
private var currentTrace: MapPaintingTrace? = null
|
||||
|
||||
fun updateMap(gameMap: GameMap) {
|
||||
map = gameMap
|
||||
tileSet = gameMap.tileSet
|
||||
layers = gameMap.layers
|
||||
rows = gameMap.rows
|
||||
columns = gameMap.columns
|
||||
tileWidth = gameMap.tileSet.tileWidth.toDouble()
|
||||
tileHeight = gameMap.tileSet.tileHeight.toDouble()
|
||||
mapWidth = gameMap.columns * gameMap.tileSet.tileWidth.toDouble()
|
||||
mapHeight = gameMap.rows * gameMap.tileSet.tileHeight.toDouble()
|
||||
}
|
||||
|
||||
override fun render(gc: GraphicsContext) {
|
||||
gc.clearRect(0.0, 0.0, gc.canvas.width, gc.canvas.height);
|
||||
|
||||
layers?.forEach { dispatchLayerRender(gc, it) }
|
||||
layers.forEach { dispatchLayerRender(gc, it) }
|
||||
renderGrid(gc)
|
||||
renderCursor(gc)
|
||||
}
|
||||
@@ -66,22 +54,22 @@ class MapCanvas(private val paintingCallback: (MapPaintingTrace) -> Unit) : Rend
|
||||
private fun renderGrid(gc: GraphicsContext) {
|
||||
gc.lineWidth = 1.5
|
||||
|
||||
for (row in 0 until (rows ?: 0)) {
|
||||
gc.strokeLine(0.0, row * (tileHeight ?: 0.0), (mapWidth ?: 0.0), row * (tileHeight ?: 0.0))
|
||||
for (row in 0..rows - 1) {
|
||||
gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight)
|
||||
}
|
||||
|
||||
for (column in 0 until (columns ?: 0)) {
|
||||
gc.strokeLine(column * (tileWidth ?: 0.0), 0.0, column * (tileWidth ?: 0.0), (mapHeight ?: 0.0))
|
||||
for (column in 0 until columns) {
|
||||
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, mapHeight)
|
||||
}
|
||||
}
|
||||
|
||||
private fun renderCursor(gc: GraphicsContext) {
|
||||
if (mouseColumn >= 0 && mouseRow >= 0) {
|
||||
gc.fill = CURSOR_BRUSH
|
||||
gc.fillRect(mouseColumn * (tileWidth ?: 0.0), mouseRow * (tileHeight ?: 0.0), tileWidth ?: 0.0, tileHeight ?: 0.0)
|
||||
gc.fill = CURSOR_STROKE_BRUSH
|
||||
gc.strokeRect(mouseColumn * (tileWidth ?: 0.0), mouseRow * (tileHeight ?: 0.0), tileWidth ?: 0.0, tileHeight ?: 0.0)
|
||||
gc.fillRect(mouseColumn * tileWidth, mouseRow * tileHeight, tileWidth, tileHeight)
|
||||
|
||||
gc.fill = CURSOR_STROKE_BRUSH
|
||||
gc.strokeRect(mouseColumn * tileWidth, mouseRow * tileHeight, tileWidth, tileHeight)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,11 +77,11 @@ class MapCanvas(private val paintingCallback: (MapPaintingTrace) -> Unit) : Rend
|
||||
mouseRow = event.row
|
||||
mouseColumn = event.column
|
||||
|
||||
val tile = tileSet?.getTile(24, 4)
|
||||
val tile = tileSet.getTile(24, 4)
|
||||
|
||||
when (event.type) {
|
||||
MouseEvent.MOUSE_PRESSED -> map?.let {
|
||||
currentTrace = MapPaintingTrace(it, "Paint trace").apply {
|
||||
MouseEvent.MOUSE_PRESSED -> {
|
||||
currentTrace = MapPaintingTrace(map, "Paint trace").apply {
|
||||
paint(0, mouseRow, mouseColumn, tile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,22 +9,19 @@ import javafx.event.EventHandler
|
||||
import javafx.scene.canvas.Canvas
|
||||
import javafx.scene.input.MouseEvent
|
||||
|
||||
class MapPane(paintingCallback: (MapPaintingTrace) -> Unit) : Canvas(), EventHandler<MouseEvent> {
|
||||
private var tileSet: TileSet? = null
|
||||
private val mapCanvas = MapCanvas(paintingCallback)
|
||||
class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canvas(), EventHandler<MouseEvent> {
|
||||
private var tileSet = map.tileSet
|
||||
private val mapCanvas = MapCanvas(map, paintingCallback)
|
||||
|
||||
init {
|
||||
onMouseMoved = this
|
||||
onMouseDragged = this
|
||||
onMousePressed = this
|
||||
onMouseReleased = this
|
||||
}
|
||||
|
||||
fun updateMap(map: GameMap) {
|
||||
tileSet = map.tileSet
|
||||
width = map.width.toDouble()
|
||||
height = map.height.toDouble()
|
||||
mapCanvas.updateMap(map)
|
||||
render()
|
||||
}
|
||||
|
||||
@@ -33,8 +30,8 @@ class MapPane(paintingCallback: (MapPaintingTrace) -> Unit) : Canvas(), EventHan
|
||||
}
|
||||
|
||||
override fun handle(event: MouseEvent?) {
|
||||
if (event != null && tileSet != null) {
|
||||
mapCanvas.handleMouseInput(MapMouseEvent.of(event, tileSet!!))
|
||||
if (event != null) {
|
||||
mapCanvas.handleMouseInput(MapMouseEvent.of(event, tileSet))
|
||||
}
|
||||
|
||||
mapCanvas.render(graphicsContext2D)
|
||||
|
||||
@@ -1,31 +1,28 @@
|
||||
package com.bartlomiejpluta.base.editor.view.fragment
|
||||
|
||||
import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
|
||||
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
||||
import javafx.beans.property.SimpleDoubleProperty
|
||||
import javafx.scene.transform.Scale
|
||||
import tornadofx.Fragment
|
||||
import tornadofx.group
|
||||
import tornadofx.plusAssign
|
||||
import tornadofx.scrollpane
|
||||
import tornadofx.*
|
||||
|
||||
|
||||
class MapFragment : Fragment() {
|
||||
private val undoRedoService: UndoRedoService by di()
|
||||
private val pane = MapPane { undoRedoService.push(it) }
|
||||
val scaleProperty = SimpleDoubleProperty(1.0)
|
||||
val map: GameMap by param()
|
||||
|
||||
private val pane = MapPane(map) { undoRedoService.push(it) }
|
||||
|
||||
private val transformation = Scale(1.0, 1.0, 0.0, 0.0).apply {
|
||||
xProperty().bind(scaleProperty)
|
||||
yProperty().bind(scaleProperty)
|
||||
}
|
||||
|
||||
fun updateMap(map: GameMap) {
|
||||
pane.updateMap(map)
|
||||
}
|
||||
|
||||
fun redraw() {
|
||||
pane.render()
|
||||
init {
|
||||
subscribe<RedrawMapRequestEvent> { pane.render() }
|
||||
}
|
||||
|
||||
override val root = scrollpane {
|
||||
|
||||
@@ -2,55 +2,69 @@ package com.bartlomiejpluta.base.editor.view.main
|
||||
|
||||
import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
|
||||
import com.bartlomiejpluta.base.editor.controller.map.MapController
|
||||
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||
import com.bartlomiejpluta.base.editor.view.fragment.MapFragment
|
||||
import javafx.beans.property.SimpleDoubleProperty
|
||||
import javafx.scene.control.TabPane
|
||||
import tornadofx.*
|
||||
|
||||
|
||||
class MainView : View() {
|
||||
private val undoRedoService: UndoRedoService by di()
|
||||
private val mapController: MapController by di()
|
||||
private val mapFragment = find<MapFragment>()
|
||||
private val tabPane = TabPane()
|
||||
|
||||
private val mapScale = SimpleDoubleProperty(1.0)
|
||||
|
||||
override val root = borderpane {
|
||||
top = hbox {
|
||||
button("Map 1") {
|
||||
action {
|
||||
mapFragment.updateMap(mapController.getMap(1))
|
||||
val map = mapController.getMap(1)
|
||||
tabPane += find<MapFragment>(Scope(), MapFragment::map to map).apply {
|
||||
title = "Map 1"
|
||||
scaleProperty.bind(mapScale)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
button("Map 2") {
|
||||
action {
|
||||
mapFragment.updateMap(mapController.getMap(2))
|
||||
val map = mapController.getMap(2)
|
||||
tabPane += find<MapFragment>(Scope(), MapFragment::map to map).apply {
|
||||
title = "Map 2"
|
||||
scaleProperty.bind(mapScale)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
button("+") {
|
||||
action {
|
||||
mapFragment.scaleProperty.value += 0.1
|
||||
mapScale.value += 0.1
|
||||
}
|
||||
}
|
||||
|
||||
button("-") {
|
||||
action {
|
||||
mapFragment.scaleProperty.value -= 0.1
|
||||
mapScale.value -= 0.1
|
||||
}
|
||||
}
|
||||
|
||||
button("Undo") {
|
||||
action {
|
||||
undoRedoService.undo()
|
||||
mapFragment.redraw()
|
||||
fire(RedrawMapRequestEvent)
|
||||
}
|
||||
}
|
||||
|
||||
button("Redo") {
|
||||
action {
|
||||
undoRedoService.redo()
|
||||
mapFragment.redraw()
|
||||
fire(RedrawMapRequestEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
center = mapFragment.root
|
||||
center = tabPane
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user