From f73db32eaa48ccf53abeab0f44e6908758adcfcd Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Sat, 31 Aug 2019 20:30:40 +0200 Subject: [PATCH] Fixed entity translation bug. Fixed bug that caused default browser key bindings to be triggered instead of applications bindings. --- src/core/gui/Menu.ts | 6 +++++ src/core/stores/GuiStore.ts | 24 +++++++++++-------- .../actions/TranslateEntityAction.ts | 16 ++++++------- src/quest_editor/gui/QuestEditorToolBar.ts | 8 +++---- src/quest_editor/gui/QuestEditorView.ts | 2 +- src/quest_editor/model/QuestEntityModel.ts | 11 +++++---- 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/core/gui/Menu.ts b/src/core/gui/Menu.ts index 2d78ced5..b907894a 100644 --- a/src/core/gui/Menu.ts +++ b/src/core/gui/Menu.ts @@ -51,6 +51,8 @@ export class Menu extends Widget { disposable_listener(document, "mousedown", (e: Event) => this.document_mousedown(e), { capture: true, }), + + disposable_listener(document, "keydown", () => this.document_keydown()), ); } @@ -80,4 +82,8 @@ export class Menu extends Widget { this.visible.val = false; } } + + private document_keydown(): void { + this.visible.val = false; + } } diff --git a/src/core/stores/GuiStore.ts b/src/core/stores/GuiStore.ts index ef02feb5..201b4af7 100644 --- a/src/core/stores/GuiStore.ts +++ b/src/core/stores/GuiStore.ts @@ -21,34 +21,34 @@ class GuiStore implements Disposable { private readonly hash_disposer = this.tool.observe(({ value: tool }) => { window.location.hash = `#/${gui_tool_to_string(tool)}`; }); - private readonly global_keyup_handlers = new Map void>(); + private readonly global_keydown_handlers = new Map void>(); constructor() { const tool = window.location.hash.slice(2); this.tool.val = string_to_gui_tool(tool) || GuiTool.Viewer; - window.addEventListener("keyup", this.dispatch_global_keyup); + window.addEventListener("keydown", this.dispatch_global_keydown); } dispose(): void { this.hash_disposer.dispose(); - this.global_keyup_handlers.clear(); + this.global_keydown_handlers.clear(); - window.removeEventListener("keyup", this.dispatch_global_keyup); + window.removeEventListener("keydown", this.dispatch_global_keydown); } - on_global_keyup(tool: GuiTool, binding: string, handler: () => void): Disposable { + on_global_keydown(tool: GuiTool, binding: string, handler: () => void): Disposable { const key = this.handler_key(tool, binding); - this.global_keyup_handlers.set(key, handler); + this.global_keydown_handlers.set(key, handler); return { dispose: () => { - this.global_keyup_handlers.delete(key); + this.global_keydown_handlers.delete(key); }, }; } - private dispatch_global_keyup = (e: KeyboardEvent) => { + private dispatch_global_keydown = (e: KeyboardEvent) => { const binding_parts: string[] = []; if (e.ctrlKey) binding_parts.push("Ctrl"); if (e.shiftKey) binding_parts.push("Shift"); @@ -57,8 +57,12 @@ class GuiStore implements Disposable { const binding = binding_parts.join("-"); - const handler = this.global_keyup_handlers.get(this.handler_key(this.tool.val, binding)); - if (handler) handler(); + const handler = this.global_keydown_handlers.get(this.handler_key(this.tool.val, binding)); + + if (handler) { + e.preventDefault(); + handler(); + } }; private handler_key(tool: GuiTool, binding: string): string { diff --git a/src/quest_editor/actions/TranslateEntityAction.ts b/src/quest_editor/actions/TranslateEntityAction.ts index 78f2ccf4..b8fc72a3 100644 --- a/src/quest_editor/actions/TranslateEntityAction.ts +++ b/src/quest_editor/actions/TranslateEntityAction.ts @@ -22,28 +22,28 @@ export class TranslateEntityAction implements Action { undo(): void { quest_editor_store.set_selected_entity(this.entity); + if (this.old_section) { + this.entity.set_section(this.old_section); + } + if (this.world) { this.entity.set_world_position(this.old_position); } else { this.entity.set_position(this.old_position); } - - if (this.old_section) { - this.entity.set_section(this.old_section); - } } redo(): void { quest_editor_store.set_selected_entity(this.entity); + if (this.new_section) { + this.entity.set_section(this.new_section); + } + if (this.world) { this.entity.set_world_position(this.new_position); } else { this.entity.set_position(this.new_position); } - - if (this.new_section) { - this.entity.set_section(this.new_section); - } } } diff --git a/src/quest_editor/gui/QuestEditorToolBar.ts b/src/quest_editor/gui/QuestEditorToolBar.ts index 261e3a86..c1810d75 100644 --- a/src/quest_editor/gui/QuestEditorToolBar.ts +++ b/src/quest_editor/gui/QuestEditorToolBar.ts @@ -106,24 +106,24 @@ export class QuestEditorToolBar extends ToolBar { quest_editor_store.set_current_area(area), ), - gui_store.on_global_keyup(GuiTool.QuestEditor, "Ctrl-O", () => + gui_store.on_global_keydown(GuiTool.QuestEditor, "Ctrl-O", () => open_file_button.click(), ), - gui_store.on_global_keyup( + gui_store.on_global_keydown( GuiTool.QuestEditor, "Ctrl-Shift-S", quest_editor_store.save_as, ), - gui_store.on_global_keyup(GuiTool.QuestEditor, "Ctrl-Z", () => { + gui_store.on_global_keydown(GuiTool.QuestEditor, "Ctrl-Z", () => { // Let Monaco handle its own key bindings. if (undo_manager.current.val !== asm_editor_store.undo) { undo_manager.undo(); } }), - gui_store.on_global_keyup(GuiTool.QuestEditor, "Ctrl-Shift-Z", () => { + gui_store.on_global_keydown(GuiTool.QuestEditor, "Ctrl-Shift-Z", () => { // Let Monaco handle its own key bindings. if (undo_manager.current.val !== asm_editor_store.undo) { undo_manager.redo(); diff --git a/src/quest_editor/gui/QuestEditorView.ts b/src/quest_editor/gui/QuestEditorView.ts index de3613ac..496dc251 100644 --- a/src/quest_editor/gui/QuestEditorView.ts +++ b/src/quest_editor/gui/QuestEditorView.ts @@ -109,7 +109,7 @@ export class QuestEditorView extends ResizableWidget { this.layout = this.init_golden_layout(); this.disposables( - gui_store.on_global_keyup( + gui_store.on_global_keydown( GuiTool.QuestEditor, "Ctrl-Alt-D", () => (quest_editor_store.debug.val = !quest_editor_store.debug.val), diff --git a/src/quest_editor/model/QuestEntityModel.ts b/src/quest_editor/model/QuestEntityModel.ts index cb175b26..06706e98 100644 --- a/src/quest_editor/model/QuestEntityModel.ts +++ b/src/quest_editor/model/QuestEntityModel.ts @@ -1,7 +1,7 @@ import { EntityType } from "../../core/data_formats/parsing/quest/entities"; import { Vec3 } from "../../core/data_formats/vector"; import { Property } from "../../core/observable/property/Property"; -import { property } from "../../core/observable"; +import { map, property } from "../../core/observable"; import { WritableProperty } from "../../core/observable/property/WritableProperty"; import { SectionModel } from "./SectionModel"; @@ -82,12 +82,13 @@ export abstract class QuestEntityModel { this.position = this._position; this._rotation = property(rotation); this.rotation = this._rotation; - this.world_position = this.position.map(this.position_to_world_position); + this.world_position = map(this.position_to_world_position, this.section, this.position); } - private position_to_world_position = (position: Vec3): Vec3 => { - const section = this.section.val; - + private position_to_world_position = ( + section: SectionModel | undefined, + position: Vec3, + ): Vec3 => { if (section) { let { x: rel_x, y: rel_y, z: rel_z } = position;