[Editor] Enable creating empty MapFragment
This commit is contained in:
@@ -7,11 +7,24 @@ import tornadofx.Controller
|
||||
class MapController : Controller() {
|
||||
private val tileSetController: TileSetController by inject()
|
||||
|
||||
val map = GameMap(tileSetController.tileset, 20, 20)
|
||||
private val map1 = GameMap(tileSetController.tileset, 20, 20)
|
||||
.createTileLayer(0)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
|
||||
private val map2 = GameMap(tileSetController.tileset, 50, 50)
|
||||
.createTileLayer(3)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
.createTileLayer(3, 5)
|
||||
|
||||
fun getMap(id: Int) = when(id) {
|
||||
1 -> map1
|
||||
else -> map2
|
||||
}
|
||||
}
|
||||
@@ -39,4 +39,8 @@ class GameMap(val tileSet: TileSet, val rows: Int, val columns: Int) {
|
||||
fun setTile(layer: Int, row: Int, column: Int, tileRow: Int, tileColumn: Int) = apply {
|
||||
(layers[layer] as TileLayer).setTile(row, column, tileSet.getTile(tileRow, tileColumn))
|
||||
}
|
||||
|
||||
companion object {
|
||||
val EMPTY_8x8 = GameMap(TileSet.EMPTY, 8, 8)
|
||||
}
|
||||
}
|
||||
@@ -28,4 +28,8 @@ class TileSet(private val image: Image, val rows: Int, val columns: Int) {
|
||||
fun getTile(row: Int, column: Int) = tiles[row][column]
|
||||
|
||||
fun getTile(id: Int) = tiles[id / rows][id % columns]
|
||||
|
||||
companion object {
|
||||
val EMPTY = TileSet(Image("/textures/tileset.png"), 1, 1)
|
||||
}
|
||||
}
|
||||
@@ -6,24 +6,14 @@ import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||
import javafx.scene.canvas.GraphicsContext
|
||||
|
||||
class MapCanvas(gameMap: GameMap) : Renderable {
|
||||
private var layers: List<Layer>
|
||||
private var rows: Int
|
||||
private var columns: Int
|
||||
private var tileWidth: Double
|
||||
private var tileHeight: Double
|
||||
private var mapWidth: Double
|
||||
private var mapHeight: Double
|
||||
|
||||
init {
|
||||
layers = gameMap.layers
|
||||
rows = gameMap.rows
|
||||
columns = gameMap.columns
|
||||
tileWidth = gameMap.tileSet.tileWidth.toDouble()
|
||||
tileHeight = gameMap.tileSet.tileHeight.toDouble()
|
||||
mapWidth = columns * tileWidth
|
||||
mapHeight = rows * tileHeight
|
||||
}
|
||||
class MapCanvas : Renderable {
|
||||
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
|
||||
|
||||
fun updateMap(gameMap: GameMap) {
|
||||
layers = gameMap.layers
|
||||
@@ -31,12 +21,12 @@ class MapCanvas(gameMap: GameMap) : Renderable {
|
||||
columns = gameMap.columns
|
||||
tileWidth = gameMap.tileSet.tileWidth.toDouble()
|
||||
tileHeight = gameMap.tileSet.tileHeight.toDouble()
|
||||
mapWidth = columns * tileWidth
|
||||
mapHeight = rows * tileHeight
|
||||
mapWidth = gameMap.columns * gameMap.tileSet.tileWidth.toDouble()
|
||||
mapHeight = gameMap.rows * gameMap.tileSet.tileHeight.toDouble()
|
||||
}
|
||||
|
||||
override fun render(gc: GraphicsContext) {
|
||||
layers.forEach { dispatchLayerRender(gc, it) }
|
||||
layers?.forEach { dispatchLayerRender(gc, it) }
|
||||
renderGrid(gc)
|
||||
}
|
||||
|
||||
@@ -59,12 +49,12 @@ class MapCanvas(gameMap: GameMap) : Renderable {
|
||||
private fun renderGrid(gc: GraphicsContext) {
|
||||
gc.lineWidth = 1.5
|
||||
|
||||
for (row in 0 until rows) {
|
||||
gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight)
|
||||
for (row in 0 until (rows ?: 0)) {
|
||||
gc.strokeLine(0.0, row * (tileHeight ?: 0.0), (mapWidth ?: 0.0), row * (tileHeight ?: 0.0))
|
||||
}
|
||||
|
||||
for (column in 0 until columns) {
|
||||
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, mapHeight)
|
||||
for (column in 0 until (columns ?: 0)) {
|
||||
gc.strokeLine(column * (tileWidth ?: 0.0), 0.0, column * (tileWidth ?: 0.0), (mapHeight ?: 0.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,17 +5,17 @@ import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas
|
||||
import com.bartlomiejpluta.base.editor.view.render.renderer.Renderer
|
||||
import javafx.scene.canvas.Canvas
|
||||
|
||||
class MapPane(map: GameMap) : Canvas() {
|
||||
private val mapCanvas = MapCanvas(map)
|
||||
class MapPane : Canvas() {
|
||||
private val mapCanvas = MapCanvas()
|
||||
private val renderer = Renderer(graphicsContext2D, mapCanvas)
|
||||
|
||||
init {
|
||||
width = map.width.toDouble()
|
||||
height = map.height.toDouble()
|
||||
renderer.start()
|
||||
}
|
||||
|
||||
fun updateMap(map: GameMap) {
|
||||
width = map.width.toDouble()
|
||||
height = map.height.toDouble()
|
||||
mapCanvas.updateMap(map)
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,15 @@ import tornadofx.plusAssign
|
||||
import tornadofx.scrollpane
|
||||
|
||||
class MapFragment : Fragment() {
|
||||
val map: GameMap by param()
|
||||
val pane = MapPane(map)
|
||||
private val pane = MapPane()
|
||||
|
||||
fun updateMap(map: GameMap) {
|
||||
pane.updateMap(map)
|
||||
}
|
||||
|
||||
override val root = scrollpane {
|
||||
prefWidth = 300.0
|
||||
prefHeight = 300.0
|
||||
prefWidth = 640.0
|
||||
prefHeight = 480.0
|
||||
|
||||
group {
|
||||
group {
|
||||
|
||||
@@ -2,13 +2,26 @@ package com.bartlomiejpluta.base.editor.view.main
|
||||
|
||||
import com.bartlomiejpluta.base.editor.controller.map.MapController
|
||||
import com.bartlomiejpluta.base.editor.view.fragment.MapFragment
|
||||
import tornadofx.View
|
||||
import tornadofx.borderpane
|
||||
import tornadofx.*
|
||||
|
||||
class MainView : View() {
|
||||
private val mapController: MapController by inject()
|
||||
private val mapFragment = find<MapFragment>()
|
||||
|
||||
override val root = borderpane {
|
||||
center = find<MapFragment>(mapOf(MapFragment::map to mapController.map)).root
|
||||
top = hbox {
|
||||
button("Map 1") {
|
||||
action {
|
||||
mapFragment.updateMap(mapController.getMap(1))
|
||||
}
|
||||
}
|
||||
|
||||
button("Map 2") {
|
||||
action {
|
||||
mapFragment.updateMap(mapController.getMap(2))
|
||||
}
|
||||
}
|
||||
}
|
||||
center = mapFragment.root
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user