[Editor] Create SelectGraphicAssetView and *Fragment
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
package com.bartlomiejpluta.base.editor.asset.model
|
||||
|
||||
interface Asset {
|
||||
val uid: String
|
||||
val source: String
|
||||
var name: String
|
||||
import javafx.beans.binding.Bindings.createObjectBinding
|
||||
import javafx.beans.property.ObjectProperty
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
import java.io.File
|
||||
|
||||
abstract class Asset(directory: ObjectProperty<File>, val uid: String, val source: String, name: String) {
|
||||
val nameProperty = SimpleStringProperty(name)
|
||||
var name by nameProperty
|
||||
|
||||
val fileProperty = createObjectBinding({ File(directory.value, source) }, directory)
|
||||
val file by fileProperty
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.bartlomiejpluta.base.editor.asset.view.select
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import javafx.beans.property.ObjectProperty
|
||||
import javafx.collections.ObservableList
|
||||
import tornadofx.*
|
||||
|
||||
class SelectGraphicAssetFragment : Fragment("Select Asset") {
|
||||
val assets: ObservableList<Asset> by param()
|
||||
val asset: ObjectProperty<Asset> by param()
|
||||
|
||||
private val selectGraphicAssetView = find<SelectGraphicAssetView>(
|
||||
SelectGraphicAssetView::assets to assets,
|
||||
SelectGraphicAssetView::asset to asset
|
||||
)
|
||||
|
||||
private var onCompleteConsumer: ((Asset) -> Unit)? = null
|
||||
|
||||
fun onComplete(onCompleteConsumer: ((Asset) -> Unit)) {
|
||||
this.onCompleteConsumer = onCompleteConsumer
|
||||
}
|
||||
|
||||
override val root = form {
|
||||
fieldset {
|
||||
this += selectGraphicAssetView.root
|
||||
}
|
||||
|
||||
buttonbar {
|
||||
button("Ok") {
|
||||
enableWhen(asset.isNotNull)
|
||||
|
||||
action {
|
||||
onCompleteConsumer?.let { it(asset.value) }
|
||||
close()
|
||||
}
|
||||
}
|
||||
|
||||
button("Cancel") {
|
||||
action { close() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.bartlomiejpluta.base.editor.asset.view.select
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import javafx.beans.binding.Bindings.createObjectBinding
|
||||
import javafx.beans.property.ObjectProperty
|
||||
import javafx.collections.ObservableList
|
||||
import javafx.scene.control.ListView
|
||||
import javafx.scene.image.Image
|
||||
import javafx.scene.image.WritableImage
|
||||
import tornadofx.*
|
||||
|
||||
class SelectGraphicAssetView : View() {
|
||||
val assets: ObservableList<Asset> by param()
|
||||
val asset: ObjectProperty<Asset> by param()
|
||||
|
||||
private var assetsListView: ListView<Asset> by singleAssign()
|
||||
|
||||
private val image = createObjectBinding({
|
||||
asset.value?.file?.inputStream()?.use { Image(it) } ?: PLACEHOLDER_IMAGE
|
||||
}, asset)
|
||||
|
||||
override val root = hbox {
|
||||
|
||||
assetsListView = listview(assets) {
|
||||
cellFormat { text = it.name }
|
||||
bindSelected(asset)
|
||||
}
|
||||
|
||||
scrollpane {
|
||||
prefWidth = 480.0
|
||||
prefHeight = 480.0
|
||||
imageview(image)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val PLACEHOLDER_IMAGE = WritableImage(100, 100)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.map.asset
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
|
||||
class GameMapAsset(override val uid: String, name: String) : Asset {
|
||||
override val source = "$uid.dat"
|
||||
|
||||
val nameProperty = SimpleStringProperty(name)
|
||||
override var name by nameProperty
|
||||
}
|
||||
class GameMapAsset(project: Project, uid: String, name: String) :
|
||||
Asset(project.mapsDirectoryProperty, uid, "$uid.dat", name)
|
||||
@@ -69,7 +69,7 @@ class DefaultProjectContext : ProjectContext {
|
||||
override fun importMap(name: String, map: GameMap) {
|
||||
project?.let {
|
||||
UID.next(it.maps.map(Asset::uid)).let { uid ->
|
||||
val asset = GameMapAsset(uid, name)
|
||||
val asset = GameMapAsset(it, uid, name)
|
||||
map.uid = uid
|
||||
it.maps += asset
|
||||
|
||||
@@ -101,7 +101,7 @@ class DefaultProjectContext : ProjectContext {
|
||||
val source = "$uid.${builder.file.extension}"
|
||||
val targetFile = File(it.tileSetsDirectory, source)
|
||||
builder.file.copyTo(targetFile)
|
||||
it.tileSets += TileSetAsset(uid, source, builder.name, builder.rows, builder.columns)
|
||||
it.tileSets += TileSetAsset(it, uid, source, builder.name, builder.rows, builder.columns)
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
@@ -14,15 +14,20 @@ class ProtobufProjectDeserializer : ProjectDeserializer {
|
||||
val proto = ProjectProto.Project.parseFrom(input)
|
||||
val project = Project()
|
||||
project.name = proto.name
|
||||
project.maps.addAll(proto.mapsList.map(this::deserializeMap))
|
||||
project.tileSets.addAll(proto.tileSetsList.map(this::deserializeTileSet))
|
||||
project.maps.addAll(proto.mapsList.map { deserializeMap(project, it) })
|
||||
project.tileSets.addAll(proto.tileSetsList.map { deserializeTileSet(project, it) })
|
||||
|
||||
return project
|
||||
}
|
||||
|
||||
private fun deserializeMap(map: ProjectProto.GameMapAsset) = GameMapAsset(uid = map.uid, name = map.name)
|
||||
private fun deserializeMap(project: Project, map: ProjectProto.GameMapAsset) = GameMapAsset(
|
||||
project = project,
|
||||
uid = map.uid,
|
||||
name = map.name
|
||||
)
|
||||
|
||||
private fun deserializeTileSet(tileSet: ProjectProto.TileSetAsset) = TileSetAsset(
|
||||
private fun deserializeTileSet(project: Project, tileSet: ProjectProto.TileSetAsset) = TileSetAsset(
|
||||
project = project,
|
||||
uid = tileSet.uid,
|
||||
source = tileSet.source,
|
||||
name = tileSet.name,
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.tileset.asset
|
||||
|
||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import tornadofx.*
|
||||
import com.bartlomiejpluta.base.editor.project.model.Project
|
||||
|
||||
class TileSetAsset(
|
||||
override val uid: String,
|
||||
override val source: String,
|
||||
name: String,
|
||||
val rows: Int,
|
||||
val columns: Int
|
||||
) : Asset {
|
||||
val nameProperty = SimpleStringProperty(name)
|
||||
override var name by nameProperty
|
||||
}
|
||||
class TileSetAsset(project: Project, uid: String, source: String, name: String, val rows: Int, val columns: Int) :
|
||||
Asset(project.tileSetsDirectoryProperty, uid, source, name)
|
||||
|
||||
Reference in New Issue
Block a user