diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemStringConverter.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemStringConverter.kt new file mode 100644 index 00000000..0f061cb7 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemStringConverter.kt @@ -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, + private val onUpdate: (item: Asset, name: String) -> Asset +) : StringConverter() { + 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 -> "" + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemTreeCell.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemTreeCell.kt new file mode 100644 index 00000000..491cce33 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/StructureItemTreeCell.kt @@ -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() { + 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 + } + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/model/StructureCategory.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/model/StructureCategory.kt new file mode 100644 index 00000000..c0060fe9 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/model/StructureCategory.kt @@ -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 = 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) + } + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/ProjectStructureView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/ProjectStructureView.kt index d2685246..b428f256 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/ProjectStructureView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/ProjectStructureView.kt @@ -1,22 +1,13 @@ package com.bartlomiejpluta.base.editor.main.view 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.model.StructureCategory import com.bartlomiejpluta.base.editor.map.asset.GameMapAsset import com.bartlomiejpluta.base.editor.project.context.ProjectContext -import com.bartlomiejpluta.base.editor.tileset.asset.TileSetAsset 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.cell.TextFieldTreeCell -import javafx.util.StringConverter -import org.kordamp.ikonli.javafx.FontIcon import tornadofx.* @@ -92,97 +83,4 @@ class ProjectStructureView : View() { projectContext.deleteAsset(asset) projectContext.save() } - - private class StructureCategory(name: String = "", var items: ObservableList = 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, - private val onUpdate: (item: Asset, name: String) -> Asset - ) : StringConverter() { - 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() { - 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 - } - } - } }