[Editor] Make MainController holds open items as ViewModels instead of Models themselves
This commit is contained in:
@@ -5,7 +5,6 @@ import com.bartlomiejpluta.base.editor.animation.viewmodel.AnimationAssetDataVM
|
|||||||
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
import com.bartlomiejpluta.base.editor.asset.model.Asset
|
||||||
import com.bartlomiejpluta.base.editor.audio.view.importing.ImportSoundFragment
|
import com.bartlomiejpluta.base.editor.audio.view.importing.ImportSoundFragment
|
||||||
import com.bartlomiejpluta.base.editor.audio.viewmodel.SoundAssetDataVM
|
import com.bartlomiejpluta.base.editor.audio.viewmodel.SoundAssetDataVM
|
||||||
import com.bartlomiejpluta.base.editor.code.model.Code
|
|
||||||
import com.bartlomiejpluta.base.editor.code.model.CodeScope
|
import com.bartlomiejpluta.base.editor.code.model.CodeScope
|
||||||
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
||||||
import com.bartlomiejpluta.base.editor.command.context.UndoableScope
|
import com.bartlomiejpluta.base.editor.command.context.UndoableScope
|
||||||
@@ -42,7 +41,7 @@ import kotlin.collections.set
|
|||||||
class MainController : Controller() {
|
class MainController : Controller() {
|
||||||
private val projectContext: ProjectContext by di()
|
private val projectContext: ProjectContext by di()
|
||||||
|
|
||||||
val openItems = observableMapOf<Scope, Any>()
|
val openItems = observableMapOf<Scope, ItemViewModel<*>>()
|
||||||
|
|
||||||
fun createEmptyProject() {
|
fun createEmptyProject() {
|
||||||
val vm = ProjectVM(Project())
|
val vm = ProjectVM(Project())
|
||||||
@@ -72,7 +71,7 @@ class MainController : Controller() {
|
|||||||
handler = vm.handler
|
handler = vm.handler
|
||||||
}
|
}
|
||||||
projectContext.importMap(vm.name, map)
|
projectContext.importMap(vm.name, map)
|
||||||
openItems[scope] = map
|
openItems[scope] = GameMapVM(map)
|
||||||
}
|
}
|
||||||
|
|
||||||
openModal(block = true, resizable = false)
|
openModal(block = true, resizable = false)
|
||||||
@@ -90,13 +89,13 @@ class MainController : Controller() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openMap(uid: String) {
|
fun openMap(uid: String) {
|
||||||
openItem<GameMap, UndoableScope>({ it.uid == uid }) {
|
openItem<GameMap, GameMapVM, UndoableScope>({ it.uid == uid }) {
|
||||||
val map = projectContext.loadMap(uid)
|
val map = projectContext.loadMap(uid)
|
||||||
val vm = GameMapVM(map)
|
val vm = GameMapVM(map)
|
||||||
val scope = UndoableScope()
|
val scope = UndoableScope()
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
scope to map
|
scope to vm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +106,7 @@ class MainController : Controller() {
|
|||||||
execute: ((String) -> Unit)? = null,
|
execute: ((String) -> Unit)? = null,
|
||||||
saveable: Boolean = true
|
saveable: Boolean = true
|
||||||
) {
|
) {
|
||||||
val findScript = { script: Code -> script.fileNode.absolutePath == fsNode.absolutePath }
|
val findScript = { script: CodeVM -> script.fileNode.absolutePath == fsNode.absolutePath }
|
||||||
val updateExistingScope = { scope: CodeScope -> scope.setCaretPosition(line, column) }
|
val updateExistingScope = { scope: CodeScope -> scope.setCaretPosition(line, column) }
|
||||||
|
|
||||||
openItem(findScript, updateExistingScope) {
|
openItem(findScript, updateExistingScope) {
|
||||||
@@ -116,36 +115,33 @@ class MainController : Controller() {
|
|||||||
val scope = CodeScope(line, column)
|
val scope = CodeScope(line, column)
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
scope to code
|
scope to vm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openQuery(query: Query) {
|
fun openQuery(query: Query) {
|
||||||
val findQuery = { q: Query -> q.name == query.name }
|
val findQuery = { q: QueryVM -> q.name == query.name }
|
||||||
val updateQuery = { scope: Scope ->
|
val updateModel = { vm: QueryVM -> vm.item = query }
|
||||||
openItems -= scope
|
|
||||||
// openItems[scope] = query
|
|
||||||
// find<QueryVM>(scope).item = query
|
|
||||||
}
|
|
||||||
|
|
||||||
openItem(findQuery, updateQuery) {
|
openItem<Query, QueryVM, Scope>(findQuery, updateViewModel = updateModel) {
|
||||||
val vm = QueryVM(query)
|
val vm = QueryVM(query)
|
||||||
val scope = Scope()
|
val scope = Scope()
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
scope to query
|
scope to vm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T, S : Scope> openItem(
|
private inline fun <reified T, reified VM : ItemViewModel<T>, S : Scope> openItem(
|
||||||
findItem: (item: T) -> Boolean,
|
findItem: (item: VM) -> Boolean,
|
||||||
updateExistingScope: (S) -> Unit = {},
|
updateExistingScope: (S) -> Unit = {},
|
||||||
createItem: () -> Pair<Scope, T>
|
updateViewModel: (VM) -> Unit = {},
|
||||||
|
createItem: () -> Pair<Scope, VM>
|
||||||
) {
|
) {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val pair = openItems.entries
|
val pair = openItems.entries
|
||||||
.filter { (_, item) -> item is T }
|
.filter { (_, item) -> item is VM }
|
||||||
.map { (scope, item) -> (scope as S) to (item as T) }
|
.map { (scope, item) -> (scope as S) to (item as VM) }
|
||||||
.firstOrNull { (_, item) -> findItem(item) }
|
.firstOrNull { (_, item) -> findItem(item) }
|
||||||
|
|
||||||
if (pair == null) {
|
if (pair == null) {
|
||||||
@@ -155,6 +151,7 @@ class MainController : Controller() {
|
|||||||
} else {
|
} else {
|
||||||
val scope = pair.first
|
val scope = pair.first
|
||||||
updateExistingScope(scope)
|
updateExistingScope(scope)
|
||||||
|
updateViewModel(pair.second)
|
||||||
fire(SelectMainViewTabEvent(scope))
|
fire(SelectMainViewTabEvent(scope))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,7 +254,7 @@ class MainController : Controller() {
|
|||||||
|
|
||||||
fun closeAsset(asset: Asset) {
|
fun closeAsset(asset: Asset) {
|
||||||
when (asset) {
|
when (asset) {
|
||||||
is GameMapAsset -> openItems.entries.firstOrNull { (_, item) -> item is GameMap && item.uid == asset.uid }?.key?.let {
|
is GameMapAsset -> openItems.entries.firstOrNull { (_, item) -> item is GameMapVM && item.uid == asset.uid }?.key?.let {
|
||||||
openItems.remove(it)
|
openItems.remove(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,12 +263,12 @@ class MainController : Controller() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun closeScript(fsNode: FileNode) {
|
fun closeScript(fsNode: FileNode) {
|
||||||
openItems.entries.firstOrNull { (_, item) -> item is Code && item.fileNode.absolutePath == fsNode.absolutePath }?.key?.let {
|
openItems.entries.firstOrNull { (_, item) -> item is CodeVM && item.fileNode.absolutePath == fsNode.absolutePath }?.key?.let {
|
||||||
openItems.remove(it)
|
openItems.remove(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
fsNode.allChildren.forEach { child ->
|
fsNode.allChildren.forEach { child ->
|
||||||
openItems.entries.firstOrNull { (_, item) -> item is Code && item.fileNode.absolutePath == child.absolutePath }?.key?.let {
|
openItems.entries.firstOrNull { (_, item) -> item is CodeVM && item.fileNode.absolutePath == child.absolutePath }?.key?.let {
|
||||||
openItems.remove(it)
|
openItems.remove(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package com.bartlomiejpluta.base.editor.main.view
|
package com.bartlomiejpluta.base.editor.main.view
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.asset.view.list.AssetsListView
|
import com.bartlomiejpluta.base.editor.asset.view.list.AssetsListView
|
||||||
import com.bartlomiejpluta.base.editor.code.model.Code
|
|
||||||
import com.bartlomiejpluta.base.editor.code.view.build.BuildLogsView
|
import com.bartlomiejpluta.base.editor.code.view.build.BuildLogsView
|
||||||
import com.bartlomiejpluta.base.editor.code.view.editor.CodeEditorFragment
|
import com.bartlomiejpluta.base.editor.code.view.editor.CodeEditorFragment
|
||||||
import com.bartlomiejpluta.base.editor.code.view.list.ScriptFilesView
|
import com.bartlomiejpluta.base.editor.code.view.list.ScriptFilesView
|
||||||
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
||||||
import com.bartlomiejpluta.base.editor.database.model.Query
|
|
||||||
import com.bartlomiejpluta.base.editor.database.view.list.TablesListView
|
import com.bartlomiejpluta.base.editor.database.view.list.TablesListView
|
||||||
import com.bartlomiejpluta.base.editor.database.view.query.QueryResultFragment
|
import com.bartlomiejpluta.base.editor.database.view.query.QueryResultFragment
|
||||||
import com.bartlomiejpluta.base.editor.database.viewmodel.QueryVM
|
import com.bartlomiejpluta.base.editor.database.viewmodel.QueryVM
|
||||||
@@ -15,7 +13,6 @@ import com.bartlomiejpluta.base.editor.event.AppendProcessLogsEvent
|
|||||||
import com.bartlomiejpluta.base.editor.event.SelectMainViewTabEvent
|
import com.bartlomiejpluta.base.editor.event.SelectMainViewTabEvent
|
||||||
import com.bartlomiejpluta.base.editor.main.component.EditorTab
|
import com.bartlomiejpluta.base.editor.main.component.EditorTab
|
||||||
import com.bartlomiejpluta.base.editor.main.controller.MainController
|
import com.bartlomiejpluta.base.editor.main.controller.MainController
|
||||||
import com.bartlomiejpluta.base.editor.map.model.map.GameMap
|
|
||||||
import com.bartlomiejpluta.base.editor.map.view.editor.MapFragment
|
import com.bartlomiejpluta.base.editor.map.view.editor.MapFragment
|
||||||
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
|
||||||
import com.bartlomiejpluta.base.editor.process.view.ProcessLogsView
|
import com.bartlomiejpluta.base.editor.process.view.ProcessLogsView
|
||||||
@@ -149,27 +146,25 @@ class MainView : View("BASE Game Editor") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createTab(scope: Scope, item: Any) = when (item) {
|
private fun createTab(scope: Scope, vm: Any) = when (vm) {
|
||||||
is GameMap -> {
|
is GameMapVM -> {
|
||||||
val vm = GameMapVM(item)
|
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
EditorTab(find<MapFragment>(scope), FontIcon("fa-map")).apply {
|
EditorTab(find<MapFragment>(scope), FontIcon("fa-map")).apply {
|
||||||
projectContext.project
|
projectContext.project
|
||||||
?.maps
|
?.maps
|
||||||
?.first { it.uid == item.uid }
|
?.first { it.uid == vm.uid }
|
||||||
?.let { textProperty().bindBidirectional(it.nameProperty) }
|
?.let { textProperty().bindBidirectional(it.nameProperty) }
|
||||||
|
|
||||||
setOnClosed { mainController.openItems.remove(scope) }
|
setOnClosed { mainController.openItems.remove(scope) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is Code -> {
|
is CodeVM -> {
|
||||||
val vm = CodeVM(item)
|
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
EditorTab(find<CodeEditorFragment>(scope), FontIcon("fa-code")).apply {
|
EditorTab(find<CodeEditorFragment>(scope), FontIcon("fa-code")).apply {
|
||||||
textProperty().bind(item.fileNode.nameProperty)
|
textProperty().bind(vm.fileNode.nameProperty)
|
||||||
|
|
||||||
setOnClosed {
|
setOnClosed {
|
||||||
fragment.shutdown()
|
fragment.shutdown()
|
||||||
@@ -178,12 +173,11 @@ class MainView : View("BASE Game Editor") {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is Query -> {
|
is QueryVM -> {
|
||||||
val vm = QueryVM(item)
|
|
||||||
setInScope(vm, scope)
|
setInScope(vm, scope)
|
||||||
|
|
||||||
EditorTab(find<QueryResultFragment>(scope), FontIcon("fa-table")).apply {
|
EditorTab(find<QueryResultFragment>(scope), FontIcon("fa-table")).apply {
|
||||||
textProperty().bind(item.nameProperty)
|
textProperty().bind(vm.nameProperty)
|
||||||
|
|
||||||
setOnClosed { mainController.openItems.remove(scope) }
|
setOnClosed { mainController.openItems.remove(scope) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ import tornadofx.setValue
|
|||||||
class GameMapVM(map: GameMap) : ItemViewModel<GameMap>(map) {
|
class GameMapVM(map: GameMap) : ItemViewModel<GameMap>(map) {
|
||||||
val layers: SimpleListProperty<Layer> = bind(GameMap::layers)
|
val layers: SimpleListProperty<Layer> = bind(GameMap::layers)
|
||||||
|
|
||||||
|
val uidProperty = bind(GameMap::uidProperty)
|
||||||
|
val uid by uidProperty
|
||||||
|
|
||||||
val tileSetProperty = bind(GameMap::tileSet)
|
val tileSetProperty = bind(GameMap::tileSet)
|
||||||
val tileSet by tileSetProperty
|
val tileSet by tileSetProperty
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user