[Editor] Refactor Project Structure related code

This commit is contained in:
2021-02-23 20:39:06 +01:00
parent 953ca10676
commit ed88836ac8
4 changed files with 118 additions and 104 deletions

View File

@@ -0,0 +1,28 @@
package com.bartlomiejpluta.base.editor.main.component
import com.bartlomiejpluta.base.editor.asset.model.Asset
import com.bartlomiejpluta.base.editor.main.model.StructureCategory
import javafx.scene.control.TreeCell
import javafx.util.StringConverter
class StructureItemStringConverter(
private val cell: TreeCell<Any>,
private val onUpdate: (item: Asset, name: String) -> Asset
) : StringConverter<Any>() {
override fun toString(item: Any?): String = when (item) {
is StructureCategory -> item.name
is Asset -> item.name
else -> ""
}
// Disclaimer:
// Because of the fact that we want to support undo/redo mechanism
// the actual update must be done from the execute() method of the Command object
// so that the Command object has access to the actual as well as the new value of layer name.
// That's why we are running the submission logic in the converter.
override fun fromString(string: String?): Any = when (val item = cell.item) {
is Asset -> string?.let { onUpdate(item, it) } ?: ""
is StructureCategory -> item.name
else -> ""
}
}

View File

@@ -0,0 +1,68 @@
package com.bartlomiejpluta.base.editor.main.component
import com.bartlomiejpluta.base.editor.asset.model.Asset
import com.bartlomiejpluta.base.editor.image.asset.ImageAsset
import com.bartlomiejpluta.base.editor.main.model.StructureCategory
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAsset
import javafx.scene.control.ContextMenu
import javafx.scene.control.MenuItem
import javafx.scene.control.cell.TextFieldTreeCell
import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.action
class StructureItemTreeCell(renameAsset: (asset: Asset, name: String) -> Asset, deleteAsset: (asset: Asset) -> Unit) :
TextFieldTreeCell<Any>() {
private val assetMenu = ContextMenu()
init {
converter = StructureItemStringConverter(this, renameAsset)
MenuItem("Rename").apply {
action {
treeView.isEditable = true
startEdit()
treeView.isEditable = false
}
assetMenu.items.add(this)
}
MenuItem("Delete").apply {
action {
deleteAsset(item as Asset)
}
assetMenu.items.add(this)
}
}
override fun updateItem(item: Any?, empty: Boolean) {
super.updateItem(item, empty)
if (empty || item == null) {
text = null
graphic = null
return
}
contextMenu = when (item) {
is Asset -> if (isEditing) null else assetMenu
is StructureCategory -> item.menu
else -> null
}
text = when (item) {
is StructureCategory -> item.name
is Asset -> item.name
else -> null
}
graphic = when (item) {
is StructureCategory -> FontIcon("fa-folder")
is GameMapAsset -> FontIcon("fa-map")
is TileSetAsset -> FontIcon("fa-th")
is ImageAsset -> FontIcon("fa-image")
else -> null
}
}
}

View File

@@ -0,0 +1,20 @@
package com.bartlomiejpluta.base.editor.main.model
import javafx.collections.ObservableList
import javafx.scene.Node
import javafx.scene.control.ContextMenu
import javafx.scene.control.MenuItem
import tornadofx.*
class StructureCategory(name: String = "", var items: ObservableList<out Any> = observableListOf()) {
val nameProperty = name.toProperty()
val name by nameProperty
val menu = ContextMenu()
fun menuitem(text: String, graphic: Node? = null, action: () -> Unit) {
MenuItem(text, graphic).apply {
action { action() }
menu.items.add(this)
}
}
}

View File

@@ -1,22 +1,13 @@
package com.bartlomiejpluta.base.editor.main.view package com.bartlomiejpluta.base.editor.main.view
import com.bartlomiejpluta.base.editor.asset.model.Asset import com.bartlomiejpluta.base.editor.asset.model.Asset
import com.bartlomiejpluta.base.editor.image.asset.ImageAsset import com.bartlomiejpluta.base.editor.main.component.StructureItemTreeCell
import com.bartlomiejpluta.base.editor.main.controller.MainController import com.bartlomiejpluta.base.editor.main.controller.MainController
import com.bartlomiejpluta.base.editor.main.model.StructureCategory
import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset
import com.bartlomiejpluta.base.editor.project.context.ProjectContext import com.bartlomiejpluta.base.editor.project.context.ProjectContext
import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAsset
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
import javafx.beans.property.SimpleStringProperty
import javafx.collections.ObservableList
import javafx.scene.Node
import javafx.scene.control.ContextMenu
import javafx.scene.control.MenuItem
import javafx.scene.control.TreeCell
import javafx.scene.control.TreeItem import javafx.scene.control.TreeItem
import javafx.scene.control.cell.TextFieldTreeCell
import javafx.util.StringConverter
import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.* import tornadofx.*
@@ -92,97 +83,4 @@ class ProjectStructureView : View() {
projectContext.deleteAsset(asset) projectContext.deleteAsset(asset)
projectContext.save() projectContext.save()
} }
private class StructureCategory(name: String = "", var items: ObservableList<out Any> = observableListOf()) {
val nameProperty = SimpleStringProperty(name)
val name by nameProperty
val menu = ContextMenu()
fun menuitem(text: String, graphic: Node? = null, action: () -> Unit) {
MenuItem(text, graphic).apply {
action { action() }
menu.items.add(this)
}
}
}
private class StructureItemConverter(
private val cell: TreeCell<Any>,
private val onUpdate: (item: Asset, name: String) -> Asset
) : StringConverter<Any>() {
override fun toString(item: Any?): String = when (item) {
is StructureCategory -> item.name
is Asset -> item.name
else -> ""
}
// Disclaimer:
// Because of the fact that we want to support undo/redo mechanism
// the actual update must be done from the execute() method of the Command object
// so that the Command object has access to the actual as well as the new value of layer name.
// That's why we are running the submission logic in the converter.
override fun fromString(string: String?): Any = when (val item = cell.item) {
is Asset -> string?.let { onUpdate(item, it) } ?: ""
is StructureCategory -> item.name
else -> ""
}
}
private class StructureItemTreeCell(
renameAsset: (asset: Asset, name: String) -> Asset,
deleteAsset: (asset: Asset) -> Unit
) : TextFieldTreeCell<Any>() {
private val assetMenu = ContextMenu()
init {
converter = StructureItemConverter(this, renameAsset)
MenuItem("Rename").apply {
action {
treeView.isEditable = true
startEdit()
treeView.isEditable = false
}
assetMenu.items.add(this)
}
MenuItem("Delete").apply {
action {
deleteAsset(item as Asset)
}
assetMenu.items.add(this)
}
}
override fun updateItem(item: Any?, empty: Boolean) {
super.updateItem(item, empty)
if (empty || item == null) {
text = null
graphic = null
return
}
contextMenu = when (item) {
is Asset -> if (isEditing) null else assetMenu
is StructureCategory -> item.menu
else -> null
}
text = when (item) {
is StructureCategory -> item.name
is Asset -> item.name
else -> null
}
graphic = when (item) {
is StructureCategory -> FontIcon("fa-folder")
is GameMapAsset -> FontIcon("fa-map")
is TileSetAsset -> FontIcon("fa-th")
is ImageAsset -> FontIcon("fa-image")
else -> null
}
}
}
} }