[Editor] Enable attaching maps to project
This commit is contained in:
@@ -26,7 +26,7 @@ class MainController : Controller() {
|
|||||||
setInScope(vm)
|
setInScope(vm)
|
||||||
val modal = find<ProjectSettingsFragment>().apply { openModal(block = true, resizable = false) }
|
val modal = find<ProjectSettingsFragment>().apply { openModal(block = true, resizable = false) }
|
||||||
|
|
||||||
if(modal.result) {
|
if (modal.result) {
|
||||||
projectContext.project = project
|
projectContext.project = project
|
||||||
projectContext.save()
|
projectContext.save()
|
||||||
}
|
}
|
||||||
@@ -39,9 +39,12 @@ class MainController : Controller() {
|
|||||||
|
|
||||||
find<MapCreationWizard>(scope).apply {
|
find<MapCreationWizard>(scope).apply {
|
||||||
onComplete {
|
onComplete {
|
||||||
openMaps[scope] = vm.item.build()
|
vm.item.build().let { map ->
|
||||||
|
projectContext.attachMap(map)
|
||||||
|
openMaps[scope] = map
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
openModal(block = true)
|
openModal(block = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.editor.map.model.map
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
import com.bartlomiejpluta.base.editor.map.model.layer.Layer
|
||||||
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
import com.bartlomiejpluta.base.editor.tileset.model.TileSet
|
||||||
|
import javafx.beans.property.ReadOnlyStringWrapper
|
||||||
import javafx.beans.property.SimpleDoubleProperty
|
import javafx.beans.property.SimpleDoubleProperty
|
||||||
import javafx.beans.property.SimpleIntegerProperty
|
import javafx.beans.property.SimpleIntegerProperty
|
||||||
import javafx.beans.property.SimpleStringProperty
|
import javafx.beans.property.SimpleStringProperty
|
||||||
@@ -11,6 +12,9 @@ import tornadofx.setValue
|
|||||||
|
|
||||||
|
|
||||||
class GameMap(val tileSet: TileSet) {
|
class GameMap(val tileSet: TileSet) {
|
||||||
|
val uidProperty = SimpleStringProperty()
|
||||||
|
var uid by uidProperty
|
||||||
|
|
||||||
val layers = observableListOf<Layer>()
|
val layers = observableListOf<Layer>()
|
||||||
|
|
||||||
val nameProperty = SimpleStringProperty()
|
val nameProperty = SimpleStringProperty()
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class ProtobufMapDeserializer : MapDeserializer {
|
|||||||
override fun deserialize(input: InputStream): GameMap {
|
override fun deserialize(input: InputStream): GameMap {
|
||||||
val map = GameMap(tileset)
|
val map = GameMap(tileset)
|
||||||
val proto = GameMapProto.GameMap.parseFrom(input)
|
val proto = GameMapProto.GameMap.parseFrom(input)
|
||||||
|
map.uid = proto.uid
|
||||||
map.name = proto.name
|
map.name = proto.name
|
||||||
map.rows = proto.rows
|
map.rows = proto.rows
|
||||||
map.columns = proto.columns
|
map.columns = proto.columns
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class ProtobufMapSerializer : MapSerializer {
|
|||||||
|
|
||||||
override fun serialize(item: GameMap, output: OutputStream) {
|
override fun serialize(item: GameMap, output: OutputStream) {
|
||||||
val protoMap = GameMapProto.GameMap.newBuilder()
|
val protoMap = GameMapProto.GameMap.newBuilder()
|
||||||
|
protoMap.uid = item.uid
|
||||||
protoMap.name = item.name
|
protoMap.name = item.name
|
||||||
protoMap.rows = item.rows
|
protoMap.rows = item.rows
|
||||||
protoMap.columns = item.columns
|
protoMap.columns = item.columns
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package com.bartlomiejpluta.base.editor.project.context
|
package com.bartlomiejpluta.base.editor.project.context
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.map.GameMap
|
||||||
|
import com.bartlomiejpluta.base.editor.map.serial.MapSerializer
|
||||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||||
import com.bartlomiejpluta.base.editor.project.serial.ProjectDeserializer
|
import com.bartlomiejpluta.base.editor.project.serial.ProjectDeserializer
|
||||||
import com.bartlomiejpluta.base.editor.project.serial.ProjectSerializer
|
import com.bartlomiejpluta.base.editor.project.serial.ProjectSerializer
|
||||||
|
import com.bartlomiejpluta.base.editor.util.uid.UID
|
||||||
import javafx.beans.property.ObjectProperty
|
import javafx.beans.property.ObjectProperty
|
||||||
import javafx.beans.property.SimpleObjectProperty
|
import javafx.beans.property.SimpleObjectProperty
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
@@ -14,7 +17,7 @@ import java.io.FileInputStream
|
|||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
class DefaultProjectContext: ProjectContext {
|
class DefaultProjectContext : ProjectContext {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var projectSerializer: ProjectSerializer
|
private lateinit var projectSerializer: ProjectSerializer
|
||||||
@@ -22,6 +25,9 @@ class DefaultProjectContext: ProjectContext {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private lateinit var projectDeserializer: ProjectDeserializer
|
private lateinit var projectDeserializer: ProjectDeserializer
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private lateinit var mapSerializer: MapSerializer
|
||||||
|
|
||||||
override val projectProperty = SimpleObjectProperty<Project?>() as ObjectProperty<Project?>
|
override val projectProperty = SimpleObjectProperty<Project?>() as ObjectProperty<Project?>
|
||||||
override var project by projectProperty
|
override var project by projectProperty
|
||||||
|
|
||||||
@@ -41,4 +47,22 @@ class DefaultProjectContext: ProjectContext {
|
|||||||
.apply { sourceDirectoryProperty.value = file.parentFile }
|
.apply { sourceDirectoryProperty.value = file.parentFile }
|
||||||
.let { project = it }
|
.let { project = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun attachMap(map: GameMap) {
|
||||||
|
project?.let {
|
||||||
|
UID.next(it.maps).let { uid ->
|
||||||
|
map.uid = uid
|
||||||
|
it.maps += uid
|
||||||
|
}
|
||||||
|
|
||||||
|
saveMap(it, map)
|
||||||
|
save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun saveMap(project: Project, map: GameMap) {
|
||||||
|
val dir = File(project.sourceDirectory, "maps")
|
||||||
|
dir.mkdirs()
|
||||||
|
File(dir, "${map.uid}.dat").outputStream().use { mapSerializer.serialize(map, it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.project.context
|
package com.bartlomiejpluta.base.editor.project.context
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.map.model.map.GameMap
|
||||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||||
import javafx.beans.property.ObjectProperty
|
import javafx.beans.property.ObjectProperty
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -10,4 +11,6 @@ interface ProjectContext {
|
|||||||
|
|
||||||
fun save()
|
fun save()
|
||||||
fun open(file: File)
|
fun open(file: File)
|
||||||
|
|
||||||
|
fun attachMap(map: GameMap)
|
||||||
}
|
}
|
||||||
@@ -11,4 +11,6 @@ class Project {
|
|||||||
|
|
||||||
val sourceDirectoryProperty = SimpleObjectProperty<File>()
|
val sourceDirectoryProperty = SimpleObjectProperty<File>()
|
||||||
val sourceDirectory by sourceDirectoryProperty
|
val sourceDirectory by sourceDirectoryProperty
|
||||||
|
|
||||||
|
val maps = observableListOf<String>()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class ProtobufProjectDeserializer : ProjectDeserializer {
|
|||||||
val proto = ProjectProto.Project.parseFrom(input)
|
val proto = ProjectProto.Project.parseFrom(input)
|
||||||
val project = Project()
|
val project = Project()
|
||||||
project.name = proto.name
|
project.name = proto.name
|
||||||
|
project.maps.addAll(proto.mapsList)
|
||||||
|
|
||||||
return project
|
return project
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class ProtobufProjectSerializer : ProjectSerializer {
|
|||||||
override fun serialize(item: Project, output: OutputStream) {
|
override fun serialize(item: Project, output: OutputStream) {
|
||||||
val proto = ProjectProto.Project.newBuilder()
|
val proto = ProjectProto.Project.newBuilder()
|
||||||
proto.name = item.name
|
proto.name = item.name
|
||||||
|
proto.addAllMaps(item.maps)
|
||||||
proto.build().writeTo(output)
|
proto.build().writeTo(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,4 +9,6 @@ class ProjectVM(project: Project) : ItemViewModel<Project>(project) {
|
|||||||
|
|
||||||
val sourceDirectoryProperty = bind(Project::sourceDirectoryProperty)
|
val sourceDirectoryProperty = bind(Project::sourceDirectoryProperty)
|
||||||
val sourceDirectory by sourceDirectoryProperty
|
val sourceDirectory by sourceDirectoryProperty
|
||||||
|
|
||||||
|
val maps = bind(Project::maps)
|
||||||
}
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.editor.resource.uid.manager
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.resource.uid.model.UIDTarget
|
|
||||||
|
|
||||||
interface UIDManager {
|
|
||||||
fun nextUID(target: UIDTarget): String
|
|
||||||
fun loadData(target: UIDTarget, uids: Set<String>)
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.editor.resource.uid.manager
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.resource.uid.model.UIDTarget
|
|
||||||
import org.springframework.stereotype.Component
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
@Component
|
|
||||||
class UUIDBasedUIDManager : UIDManager {
|
|
||||||
private val registry = mutableMapOf<UIDTarget, Set<String>>()
|
|
||||||
|
|
||||||
override fun nextUID(target: UIDTarget): String {
|
|
||||||
val set = registry.putIfAbsent(target, mutableSetOf())!!
|
|
||||||
|
|
||||||
var uid = ""
|
|
||||||
|
|
||||||
do {
|
|
||||||
uid = UUID.randomUUID().toString()
|
|
||||||
} while (uid !in set)
|
|
||||||
|
|
||||||
return uid
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun loadData(target: UIDTarget, uids: Set<String>) {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.editor.resource.uid.model
|
|
||||||
|
|
||||||
enum class UIDTarget {
|
|
||||||
MAP
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.util.uid
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
object UID {
|
||||||
|
fun next(sequence: List<String>): String {
|
||||||
|
var uid: String
|
||||||
|
|
||||||
|
do {
|
||||||
|
uid = UUID.randomUUID().toString()
|
||||||
|
} while(uid in sequence)
|
||||||
|
|
||||||
|
return uid
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,10 +4,11 @@ option java_package = "com.bartlomiejpluta.base.proto";
|
|||||||
option java_outer_classname = "GameMapProto";
|
option java_outer_classname = "GameMapProto";
|
||||||
|
|
||||||
message GameMap {
|
message GameMap {
|
||||||
required string name = 1;
|
required string uid = 1;
|
||||||
required uint32 rows = 2;
|
required string name = 2;
|
||||||
required uint32 columns = 3;
|
required uint32 rows = 3;
|
||||||
repeated Layer layers = 4;
|
required uint32 columns = 4;
|
||||||
|
repeated Layer layers = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Layer {
|
message Layer {
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ option java_outer_classname = "ProjectProto";
|
|||||||
|
|
||||||
message Project {
|
message Project {
|
||||||
required string name = 1;
|
required string name = 1;
|
||||||
|
repeated string maps = 2;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user