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