diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorFragment.kt index e1273f06..d45a24bf 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorFragment.kt @@ -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() 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() + } + } + } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorView.kt index ca8a6e1a..ad6baefe 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/code/view/editor/CodeEditorView.kt @@ -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) + } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorFragment.kt new file mode 100644 index 00000000..76319a77 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorFragment.kt @@ -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) +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorTab.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorTab.kt new file mode 100644 index 00000000..b21d7cc9 --- /dev/null +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/component/EditorTab.kt @@ -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(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")!! + } +} \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt index 001ac889..b5aa5187 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/main/view/MainView.kt @@ -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(scope).root - graphic = FontIcon("fa-map") - setOnClosed { mainController.openItems.remove(scope) } + + EditorTab(find(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(scope) - content = editor.root - graphic = FontIcon("fa-code") - textProperty().bind(item.fileNode.nameProperty) - setOnClosed { - editor.shutdown() - mainController.openItems.remove(scope) + EditorTab(find(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) + } + } } \ No newline at end of file diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapFragment.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapFragment.kt index a28261c5..674637cc 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapFragment.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapFragment.kt @@ -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() private val editorStateVM = find() private val mapView = find() @@ -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() + } + } + } } diff --git a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt index 3e63817c..2c7686f3 100644 --- a/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt +++ b/editor/src/main/kotlin/com/bartlomiejpluta/base/editor/map/view/editor/MapToolbarView.kt @@ -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)