From 29e9a0ec6a1d944ef6edcb7b470f45bc98464724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 4 Feb 2021 16:44:44 +0100 Subject: [PATCH] [Editor] Implement TabPane in MainView | make MapFragment immutable --- .../editor/event/RedrawMapRequestEvent.kt | 6 ++ .../editor/render/canvas/map/MapCanvas.kt | 56 ++++++++----------- .../base/editor/view/component/map/MapPane.kt | 13 ++--- .../base/editor/view/fragment/MapFragment.kt | 19 +++---- .../base/editor/view/main/MainView.kt | 30 +++++++--- 5 files changed, 63 insertions(+), 61 deletions(-) create mode 100755 editor/src/main/kotlin/com/bartlomiejpluta/base/editor/event/RedrawMapRequestEvent.kt diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/event/RedrawMapRequestEvent.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/event/RedrawMapRequestEvent.kt new file mode 100755 index 00000000..bc8c5179 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/event/RedrawMapRequestEvent.kt @@ -0,0 +1,6 @@ +package com.bartlomiejpluta.base.editor.event + +import tornadofx.EventBus.RunOn.BackgroundThread +import tornadofx.FXEvent + +object RedrawMapRequestEvent : FXEvent(BackgroundThread) \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt index a05220d2..e7c18433 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/render/canvas/map/MapCanvas.kt @@ -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? = 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) { + 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 { + when (event.type) { + MouseEvent.MOUSE_PRESSED -> { + currentTrace = MapPaintingTrace(map, "Paint trace").apply { paint(0, mouseRow, mouseColumn, tile) } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt index 8617ab97..8b3f6952 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/component/map/MapPane.kt @@ -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 { - private var tileSet: TileSet? = null - private val mapCanvas = MapCanvas(paintingCallback) +class MapPane(map: GameMap, paintingCallback: (MapPaintingTrace) -> Unit) : Canvas(), EventHandler { + 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) diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/fragment/MapFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/fragment/MapFragment.kt index 9c261c91..1adc6eaa 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/fragment/MapFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/fragment/MapFragment.kt @@ -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 { pane.render() } } override val root = scrollpane { diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/main/MainView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/main/MainView.kt index e3ec6a4b..f85d809e 100755 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/main/MainView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/view/main/MainView.kt @@ -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() + 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(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(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 } } \ No newline at end of file