[Editor] Fix invalid keyboard shortcuts handling

This commit is contained in:
2021-03-09 11:46:54 +01:00
parent edf5a50f05
commit 95c2f021a9
7 changed files with 133 additions and 38 deletions

View File

@@ -1,8 +1,12 @@
package com.bartlomiejpluta.base.editor.code.view.editor package com.bartlomiejpluta.base.editor.code.view.editor
import tornadofx.Fragment import com.bartlomiejpluta.base.editor.main.component.EditorFragment
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.REDO_SHORTCUT
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.SAVE_SHORTCUT
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.UNDO_SHORTCUT
import javafx.scene.input.KeyEvent
class CodeEditorFragment : Fragment() { class CodeEditorFragment : EditorFragment() {
private val editorView = find<CodeEditorView>() private val editorView = find<CodeEditorView>()
fun shutdown() { fun shutdown() {
@@ -10,4 +14,23 @@ class CodeEditorFragment : Fragment() {
} }
override val root = editorView.root override val root = editorView.root
override fun handleShortcut(event: KeyEvent) {
when {
SAVE_SHORTCUT.match(event) -> {
editorView.save()
event.consume()
}
UNDO_SHORTCUT.match(event) -> {
editorView.undo()
event.consume()
}
REDO_SHORTCUT.match(event) -> {
editorView.redo()
event.consume()
}
}
}
} }

View File

@@ -43,36 +43,30 @@ class CodeEditorView : View() {
override val root = borderpane { override val root = borderpane {
top = toolbar { top = toolbar {
button(graphic = FontIcon("fa-floppy-o")) { button(graphic = FontIcon("fa-floppy-o")) {
shortcut("Ctrl+S")
enableWhen(codeVM.dirty.and(editable)) enableWhen(codeVM.dirty.and(editable))
action { save() }
action {
codeVM.item?.let {
codeVM.commit(codeVM.codeProperty)
projectContext.saveScript(it)
}
}
} }
button(graphic = FontIcon("fa-undo")) { button(graphic = FontIcon("fa-undo")) {
shortcut("Ctrl+Z")
enableWhen(editable) enableWhen(editable)
action { undo() }
action {
editor.undo()
}
} }
button(graphic = FontIcon("fa-repeat")) { button(graphic = FontIcon("fa-repeat")) {
shortcut("Ctrl+Shift+Z")
enableWhen(editable) enableWhen(editable)
action { redo() }
action {
editor.redo()
}
} }
} }
center = editor center = editor
} }
fun redo() = editor.redo()
fun undo() = editor.undo()
fun save() = codeVM.item?.let {
codeVM.commit(codeVM.codeProperty)
projectContext.saveScript(it)
}
} }

View File

@@ -0,0 +1,9 @@
package com.bartlomiejpluta.base.editor.main.component
import javafx.scene.Node
import javafx.scene.input.KeyEvent
import tornadofx.Fragment
abstract class EditorFragment(title: String? = null, icon: Node? = null) : Fragment(title, icon) {
abstract fun handleShortcut(event: KeyEvent)
}

View File

@@ -0,0 +1,22 @@
package com.bartlomiejpluta.base.editor.main.component
import javafx.scene.Node
import javafx.scene.control.Tab
import javafx.scene.input.KeyCombination.keyCombination
import javafx.scene.input.KeyEvent
class EditorTab<T : EditorFragment>(val fragment: T, graphic: Node) : Tab() {
init {
this.content = fragment.root
this.graphic = graphic
}
fun handleShortcut(event: KeyEvent) = fragment.handleShortcut(event)
companion object {
val SAVE_SHORTCUT = keyCombination("Ctrl+S")!!
val UNDO_SHORTCUT = keyCombination("Ctrl+Z")!!
val REDO_SHORTCUT = keyCombination("Ctrl+Y")!!
}
}

View File

@@ -9,6 +9,7 @@ import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
import com.bartlomiejpluta.base.editor.event.AppendBuildLogsEvent import com.bartlomiejpluta.base.editor.event.AppendBuildLogsEvent
import com.bartlomiejpluta.base.editor.event.AppendProcessLogsEvent 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.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.model.map.GameMap
import com.bartlomiejpluta.base.editor.map.view.editor.MapFragment import com.bartlomiejpluta.base.editor.map.view.editor.MapFragment
@@ -20,6 +21,7 @@ import javafx.beans.binding.Bindings.createStringBinding
import javafx.collections.MapChangeListener import javafx.collections.MapChangeListener
import javafx.event.Event import javafx.event.Event
import javafx.scene.control.Tab import javafx.scene.control.Tab
import javafx.scene.input.KeyEvent
import org.kordamp.ikonli.javafx.FontIcon import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.* import tornadofx.*
@@ -106,6 +108,8 @@ class MainView : View("BASE Game Editor") {
} }
override val root = borderpane { override val root = borderpane {
addEventHandler(KeyEvent.KEY_PRESSED, this@MainView::handleShortcut)
top = mainMenuView.root top = mainMenuView.root
center = tabPane center = tabPane
@@ -137,30 +141,42 @@ class MainView : View("BASE Game Editor") {
} }
private fun createTab(scope: Scope, item: Any) = when (item) { private fun createTab(scope: Scope, item: Any) = when (item) {
is GameMap -> Tab().apply { is GameMap -> {
val vm = GameMapVM(item) val vm = GameMapVM(item)
setInScope(vm, scope) setInScope(vm, scope)
projectContext.project?.maps?.first { it.uid == item.uid }
?.let { textProperty().bindBidirectional(it.nameProperty) } EditorTab(find<MapFragment>(scope), FontIcon("fa-map")).apply {
content = find<MapFragment>(scope).root projectContext.project
graphic = FontIcon("fa-map") ?.maps
setOnClosed { mainController.openItems.remove(scope) } ?.first { it.uid == item.uid }
?.let { textProperty().bindBidirectional(it.nameProperty) }
setOnClosed { mainController.openItems.remove(scope) }
}
} }
is Code -> Tab().apply { is Code -> {
val vm = CodeVM(item) val vm = CodeVM(item)
setInScope(vm, scope) setInScope(vm, scope)
val editor = find<CodeEditorFragment>(scope)
content = editor.root
graphic = FontIcon("fa-code")
textProperty().bind(item.fileNode.nameProperty)
setOnClosed { EditorTab(find<CodeEditorFragment>(scope), FontIcon("fa-code")).apply {
editor.shutdown() textProperty().bind(item.fileNode.nameProperty)
mainController.openItems.remove(scope)
setOnClosed {
fragment.shutdown()
mainController.openItems.remove(scope)
}
} }
} }
else -> throw IllegalStateException("Unsupported tab item") else -> throw IllegalStateException("Unsupported tab item")
} }
private fun handleShortcut(event: KeyEvent) {
val currentTab = tabPane.selectionModel.selectedItem
if (currentTab is EditorTab<*>) {
currentTab.handleShortcut(event)
}
}
} }

View File

@@ -1,16 +1,29 @@
package com.bartlomiejpluta.base.editor.map.view.editor package com.bartlomiejpluta.base.editor.map.view.editor
import com.bartlomiejpluta.base.editor.command.context.UndoableScope import com.bartlomiejpluta.base.editor.command.context.UndoableScope
import com.bartlomiejpluta.base.editor.command.service.UndoRedoService
import com.bartlomiejpluta.base.editor.event.RedrawMapRequestEvent
import com.bartlomiejpluta.base.editor.main.component.EditorFragment
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.REDO_SHORTCUT
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.SAVE_SHORTCUT
import com.bartlomiejpluta.base.editor.main.component.EditorTab.Companion.UNDO_SHORTCUT
import com.bartlomiejpluta.base.editor.map.controller.MapController
import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer import com.bartlomiejpluta.base.editor.map.model.layer.TileLayer
import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM import com.bartlomiejpluta.base.editor.map.viewmodel.EditorStateVM
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
import com.bartlomiejpluta.base.editor.tileset.view.editor.TileSetView import com.bartlomiejpluta.base.editor.tileset.view.editor.TileSetView
import javafx.beans.binding.Bindings import javafx.beans.binding.Bindings
import javafx.scene.input.KeyEvent
import tornadofx.* import tornadofx.*
class MapFragment : Fragment() { class MapFragment : EditorFragment() {
override val scope = super.scope as UndoableScope override val scope = super.scope as UndoableScope
private val mapController: MapController by di()
private val undoRedoService: UndoRedoService by di()
private val mapVM = find<GameMapVM>()
private val editorStateVM = find<EditorStateVM>() private val editorStateVM = find<EditorStateVM>()
private val mapView = find<MapView>() private val mapView = find<MapView>()
@@ -57,4 +70,25 @@ class MapFragment : Fragment() {
bottom = statusBarView.root bottom = statusBarView.root
} }
override fun handleShortcut(event: KeyEvent) {
when {
SAVE_SHORTCUT.match(event) -> {
mapController.saveMap(mapVM.item)
event.consume()
}
UNDO_SHORTCUT.match(event) -> {
undoRedoService.undo(scope)
fire(RedrawMapRequestEvent)
event.consume()
}
REDO_SHORTCUT.match(event) -> {
undoRedoService.redo(scope)
fire(RedrawMapRequestEvent)
event.consume()
}
}
}
} }

View File

@@ -43,14 +43,12 @@ class MapToolbarView : View() {
override val root = toolbar { override val root = toolbar {
button(graphic = FontIcon("fa-floppy-o")) { button(graphic = FontIcon("fa-floppy-o")) {
shortcut("Ctrl+S")
action { action {
mapController.saveMap(mapVM.item) mapController.saveMap(mapVM.item)
} }
} }
button(graphic = FontIcon("fa-undo")) { button(graphic = FontIcon("fa-undo")) {
shortcut("Ctrl+Z")
action { action {
undoRedoService.undo(scope) undoRedoService.undo(scope)
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)
@@ -58,7 +56,6 @@ class MapToolbarView : View() {
} }
button(graphic = FontIcon("fa-repeat")) { button(graphic = FontIcon("fa-repeat")) {
shortcut("Ctrl+Shift+Z")
action { action {
undoRedoService.redo(scope) undoRedoService.redo(scope)
fire(RedrawMapRequestEvent) fire(RedrawMapRequestEvent)