[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
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>()
fun shutdown() {
@@ -10,4 +14,23 @@ class CodeEditorFragment : Fragment() {
}
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 {
top = toolbar {
button(graphic = FontIcon("fa-floppy-o")) {
shortcut("Ctrl+S")
enableWhen(codeVM.dirty.and(editable))
action {
codeVM.item?.let {
codeVM.commit(codeVM.codeProperty)
projectContext.saveScript(it)
}
}
action { save() }
}
button(graphic = FontIcon("fa-undo")) {
shortcut("Ctrl+Z")
enableWhen(editable)
action {
editor.undo()
}
action { undo() }
}
button(graphic = FontIcon("fa-repeat")) {
shortcut("Ctrl+Shift+Z")
enableWhen(editable)
action {
editor.redo()
}
action { redo() }
}
}
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.AppendProcessLogsEvent
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.map.model.map.GameMap
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.event.Event
import javafx.scene.control.Tab
import javafx.scene.input.KeyEvent
import org.kordamp.ikonli.javafx.FontIcon
import tornadofx.*
@@ -106,6 +108,8 @@ class MainView : View("BASE Game Editor") {
}
override val root = borderpane {
addEventHandler(KeyEvent.KEY_PRESSED, this@MainView::handleShortcut)
top = mainMenuView.root
center = tabPane
@@ -137,30 +141,42 @@ class MainView : View("BASE Game Editor") {
}
private fun createTab(scope: Scope, item: Any) = when (item) {
is GameMap -> Tab().apply {
is GameMap -> {
val vm = GameMapVM(item)
setInScope(vm, scope)
projectContext.project?.maps?.first { it.uid == item.uid }
?.let { textProperty().bindBidirectional(it.nameProperty) }
content = find<MapFragment>(scope).root
graphic = FontIcon("fa-map")
setOnClosed { mainController.openItems.remove(scope) }
EditorTab(find<MapFragment>(scope), FontIcon("fa-map")).apply {
projectContext.project
?.maps
?.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)
setInScope(vm, scope)
val editor = find<CodeEditorFragment>(scope)
content = editor.root
graphic = FontIcon("fa-code")
textProperty().bind(item.fileNode.nameProperty)
setOnClosed {
editor.shutdown()
mainController.openItems.remove(scope)
EditorTab(find<CodeEditorFragment>(scope), FontIcon("fa-code")).apply {
textProperty().bind(item.fileNode.nameProperty)
setOnClosed {
fragment.shutdown()
mainController.openItems.remove(scope)
}
}
}
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
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.viewmodel.EditorStateVM
import com.bartlomiejpluta.base.editor.map.viewmodel.GameMapVM
import com.bartlomiejpluta.base.editor.tileset.view.editor.TileSetView
import javafx.beans.binding.Bindings
import javafx.scene.input.KeyEvent
import tornadofx.*
class MapFragment : Fragment() {
class MapFragment : EditorFragment() {
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 mapView = find<MapView>()
@@ -57,4 +70,25 @@ class MapFragment : Fragment() {
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 {
button(graphic = FontIcon("fa-floppy-o")) {
shortcut("Ctrl+S")
action {
mapController.saveMap(mapVM.item)
}
}
button(graphic = FontIcon("fa-undo")) {
shortcut("Ctrl+Z")
action {
undoRedoService.undo(scope)
fire(RedrawMapRequestEvent)
@@ -58,7 +56,6 @@ class MapToolbarView : View() {
}
button(graphic = FontIcon("fa-repeat")) {
shortcut("Ctrl+Shift+Z")
action {
undoRedoService.redo(scope)
fire(RedrawMapRequestEvent)