[Editor] Create Asset object base for attachable project items
Implement GameMapAsset which contains additional metainfo related to attached GameMap.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.asset.model
|
||||
|
||||
interface Asset {
|
||||
val uid: String
|
||||
val source: String
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.bartlomiejpluta.base.editor.map.asset
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
|
||||
// TODO(Add tileSetUID field)
|
||||
data class GameMapAsset(
|
||||
override val uid: String,
|
||||
val name: String,
|
||||
val rows: Int,
|
||||
val columns: Int,
|
||||
) : Asset {
|
||||
override val source = "$uid.dat"
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.project.context
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
|
||||
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
|
||||
@@ -7,10 +9,12 @@ import com.bartlomiejpluta.base.editor.project.serial.ProjectDeserializer
|
||||
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.ReadOnlyObjectWrapper
|
||||
import javafx.beans.property.SimpleObjectProperty
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
import tornadofx.getValue
|
||||
import tornadofx.select
|
||||
import tornadofx.setValue
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
@@ -31,6 +35,23 @@ class DefaultProjectContext : ProjectContext {
|
||||
override val projectProperty = SimpleObjectProperty<Project?>() as ObjectProperty<Project?>
|
||||
override var project by projectProperty
|
||||
|
||||
private val mapsDirectoryProperty = SimpleObjectProperty<File?>()
|
||||
private val mapsDirectory by mapsDirectoryProperty
|
||||
|
||||
init {
|
||||
projectProperty.addListener { _, _, newProject ->
|
||||
when(newProject) {
|
||||
null -> {
|
||||
mapsDirectoryProperty.value = null
|
||||
}
|
||||
|
||||
else -> {
|
||||
mapsDirectoryProperty.value = File(newProject.sourceDirectory, MAPS_DIR).apply(File::mkdirs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun save() {
|
||||
project?.let {
|
||||
it.sourceDirectory.mkdirs()
|
||||
@@ -50,19 +71,18 @@ class DefaultProjectContext : ProjectContext {
|
||||
|
||||
override fun attachMap(map: GameMap) {
|
||||
project?.let {
|
||||
UID.next(it.maps).let { uid ->
|
||||
UID.next(it.maps.map(Asset::uid)).let { uid ->
|
||||
val asset = GameMapAsset(uid, map.name, map.rows, map.columns)
|
||||
map.uid = uid
|
||||
it.maps += uid
|
||||
}
|
||||
it.maps += asset
|
||||
|
||||
saveMap(it, map)
|
||||
save()
|
||||
save()
|
||||
File(mapsDirectory, asset.source).outputStream().use { fos -> mapSerializer.serialize(map, fos) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) }
|
||||
companion object {
|
||||
const val MAPS_DIR = "maps"
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.bartlomiejpluta.base.editor.project.model
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
|
||||
import javafx.beans.property.SimpleObjectProperty
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import tornadofx.*
|
||||
import tornadofx.getValue
|
||||
import tornadofx.observableListOf
|
||||
import tornadofx.setValue
|
||||
import java.io.File
|
||||
|
||||
class Project {
|
||||
@@ -12,5 +15,5 @@ class Project {
|
||||
val sourceDirectoryProperty = SimpleObjectProperty<File>()
|
||||
val sourceDirectory by sourceDirectoryProperty
|
||||
|
||||
val maps = observableListOf<String>()
|
||||
val maps = observableListOf<GameMapAsset>()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.bartlomiejpluta.base.editor.project.serial
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
|
||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
import com.bartlomiejpluta.base.proto.ProjectProto
|
||||
import org.springframework.stereotype.Component
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
|
||||
@Component
|
||||
@@ -12,8 +14,15 @@ class ProtobufProjectDeserializer : ProjectDeserializer {
|
||||
val proto = ProjectProto.Project.parseFrom(input)
|
||||
val project = Project()
|
||||
project.name = proto.name
|
||||
project.maps.addAll(proto.mapsList)
|
||||
project.maps.addAll(proto.mapsList.map(this::deserializeMap))
|
||||
|
||||
return project
|
||||
}
|
||||
|
||||
private fun deserializeMap(map: ProjectProto.GameMapAsset) = GameMapAsset(
|
||||
uid = map.uid,
|
||||
name = map.name,
|
||||
rows = map.rows,
|
||||
columns = map.columns
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.project.serial
|
||||
|
||||
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
|
||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
import com.bartlomiejpluta.base.proto.ProjectProto
|
||||
import org.springframework.stereotype.Component
|
||||
@@ -11,7 +12,16 @@ class ProtobufProjectSerializer : ProjectSerializer {
|
||||
override fun serialize(item: Project, output: OutputStream) {
|
||||
val proto = ProjectProto.Project.newBuilder()
|
||||
proto.name = item.name
|
||||
proto.addAllMaps(item.maps)
|
||||
proto.addAllMaps(item.maps.map(this::serializeMap))
|
||||
proto.build().writeTo(output)
|
||||
}
|
||||
|
||||
private fun serializeMap(map: GameMapAsset) = ProjectProto.GameMapAsset.newBuilder()
|
||||
.setUid(map.uid)
|
||||
.setSource(map.source)
|
||||
.setName(map.name)
|
||||
.setRows(map.rows)
|
||||
.setColumns(map.columns)
|
||||
.build()
|
||||
|
||||
}
|
||||
@@ -5,5 +5,14 @@ option java_outer_classname = "ProjectProto";
|
||||
|
||||
message Project {
|
||||
required string name = 1;
|
||||
repeated string maps = 2;
|
||||
repeated GameMapAsset maps = 2;
|
||||
}
|
||||
|
||||
// TODO(Add tileSetUID field)
|
||||
message GameMapAsset {
|
||||
required string uid = 1;
|
||||
required string source = 2;
|
||||
required string name = 3;
|
||||
required uint32 rows = 4;
|
||||
required uint32 columns = 5;
|
||||
}
|
||||
Reference in New Issue
Block a user