[Editor] Enable saving Game Maps
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.bartlomiejpluta.base.editor.map.controller
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.model.map.GameMap
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import org.springframework.stereotype.Component
|
||||
import tornadofx.Controller
|
||||
|
||||
@Component
|
||||
class MapController : Controller() {
|
||||
private val projectContext: ProjectContext by di()
|
||||
|
||||
fun saveMap(map: GameMap) {
|
||||
projectContext.saveMap(map)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.base.editor.map.view.editor
|
||||
import com.bartlomiejpluta.base.editor.command.context.UndoableScope
|
||||
import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
|
||||
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
|
||||
import com.bartlomiejpluta.base.editor.map.controller.MapController
|
||||
import com.bartlomiejpluta.base.editor.map.model.brush.BrushMode
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.BrushVM
|
||||
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
|
||||
@@ -13,6 +14,7 @@ import tornadofx.*
|
||||
|
||||
class MapToolbarView : View() {
|
||||
private val undoRedoService: UndoRedoService by di()
|
||||
private val mapController: MapController by di()
|
||||
|
||||
override val scope = super.scope as UndoableScope
|
||||
|
||||
@@ -27,6 +29,13 @@ class MapToolbarView : View() {
|
||||
}
|
||||
|
||||
override val root = toolbar {
|
||||
button(graphic = FontIcon("fa-floppy-o")) {
|
||||
shortcut("Ctrl+S")
|
||||
action {
|
||||
mapController.saveMap(mapVM.item)
|
||||
}
|
||||
}
|
||||
|
||||
button(graphic = FontIcon("fa-undo")) {
|
||||
shortcut("Ctrl+Z")
|
||||
action {
|
||||
|
||||
@@ -80,12 +80,21 @@ class DefaultProjectContext : ProjectContext {
|
||||
}
|
||||
|
||||
override fun loadMap(uid: String) = project?.let {
|
||||
val asset = it.maps.first { map -> map.uid == uid }
|
||||
val asset = it.maps.firstOrNull { map -> map.uid == uid }
|
||||
?: throw IllegalStateException("The map with uid [$uid] does not exist ")
|
||||
|
||||
File(it.mapsDirectory, asset.source).inputStream().use { fis -> mapDeserializer.deserialize(fis) }
|
||||
} ?: throw IllegalStateException("There is no open project in the context")
|
||||
|
||||
override fun saveMap(map: GameMap) {
|
||||
project?.let {
|
||||
val asset = it.maps.firstOrNull { asset -> asset.uid == map.uid }
|
||||
?: throw IllegalStateException("The map with uid [${map.uid}] does not exist ")
|
||||
|
||||
File(it.mapsDirectory, asset.source).outputStream().use { fos -> mapSerializer.serialize(map, fos) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun importTileSet(builder: TileSetAssetBuilder) {
|
||||
project?.let {
|
||||
UID.next(it.tileSets.map(Asset::uid)).let { uid ->
|
||||
@@ -101,7 +110,7 @@ class DefaultProjectContext : ProjectContext {
|
||||
|
||||
override fun loadTileSet(uid: String) = tileSetCache.getOrPut(uid) {
|
||||
project?.let {
|
||||
val asset = it.tileSets.first { tileSet -> tileSet.uid == uid }
|
||||
val asset = it.tileSets.firstOrNull { tileSet -> tileSet.uid == uid }
|
||||
?: throw IllegalStateException("The Tile Set with uid [$uid] does not exist ")
|
||||
|
||||
val image = File(it.tileSetsDirectory, asset.source).inputStream().use { fis -> Image(fis) }
|
||||
|
||||
@@ -16,6 +16,7 @@ interface ProjectContext {
|
||||
|
||||
fun importMap(name: String, map: GameMap)
|
||||
fun loadMap(uid: String): GameMap
|
||||
fun saveMap(map: GameMap)
|
||||
|
||||
fun importTileSet(builder: TileSetAssetBuilder)
|
||||
fun loadTileSet(uid: String): TileSet
|
||||
|
||||
Reference in New Issue
Block a user