From 0618ca0b87e04edd54f3ab48177d9dc574f03256 Mon Sep 17 00:00:00 2001 From: jtuu Date: Sun, 15 Sep 2019 20:59:51 +0300 Subject: [PATCH] Added a checkbox for enabling inline arguments transformation mode to the script editor toolbar. --- src/quest_editor/gui/AsmEditorToolBar.ts | 27 +++++++++++++++++-- .../scripting/AssemblyAnalyser.ts | 11 +++++++- src/quest_editor/scripting/assembly.ts | 4 +++ src/quest_editor/scripting/assembly_worker.ts | 21 +++++++++++++-- .../scripting/assembly_worker_messages.ts | 10 +++++-- src/quest_editor/stores/AsmEditorStore.ts | 20 ++++++++++++++ 6 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/quest_editor/gui/AsmEditorToolBar.ts b/src/quest_editor/gui/AsmEditorToolBar.ts index b9113392..e7a28a1b 100644 --- a/src/quest_editor/gui/AsmEditorToolBar.ts +++ b/src/quest_editor/gui/AsmEditorToolBar.ts @@ -1,12 +1,35 @@ import { ToolBar } from "../../core/gui/ToolBar"; -import { Button } from "../../core/gui/Button"; +import { CheckBox } from "../../core/gui/CheckBox"; +import { asm_editor_store } from "../stores/AsmEditorStore"; export class AsmEditorToolBar extends ToolBar { constructor() { + const inline_args_mode_checkbox = new CheckBox(true, { + label: "Inline args mode", + tooltip: asm_editor_store.has_issues.map((has_issues) => { + let text = "Transform arg_push* opcodes to be inline with the opcode the arguments are given to."; + + if (has_issues) { + text += "\nThis mode cannot be toggled because there are issues in the script."; + } + + return text; + }), + }); super({ - children: [], + children: [ + inline_args_mode_checkbox + ], }); + + this.disposables( + asm_editor_store.inline_args_mode.bind_to(inline_args_mode_checkbox.checked), + + inline_args_mode_checkbox.enabled.bind_to(asm_editor_store.has_issues.map(b => !b)), + ); + + this.finalize_construction(AsmEditorToolBar.prototype); } } diff --git a/src/quest_editor/scripting/AssemblyAnalyser.ts b/src/quest_editor/scripting/AssemblyAnalyser.ts index 3a0127f2..4c055e9d 100644 --- a/src/quest_editor/scripting/AssemblyAnalyser.ts +++ b/src/quest_editor/scripting/AssemblyAnalyser.ts @@ -7,8 +7,9 @@ import { NewAssemblyInput, OutputMessageType, SignatureHelpInput, + AssemblySettingsChangeInput, } from "./assembly_worker_messages"; -import { AssemblyError, AssemblyWarning } from "./assembly"; +import { AssemblyError, AssemblyWarning, AssemblySettings } from "./assembly"; import { disassemble } from "./disassembly"; import { QuestModel } from "../model/QuestModel"; import { Kind, OPCODES } from "./opcodes"; @@ -130,6 +131,14 @@ export class AssemblyAnalyser implements Disposable { }); } + update_settings(changed_settings: Partial): void { + const message: AssemblySettingsChangeInput = { + type: InputMessageType.SettingsChange, + settings: changed_settings + }; + this.worker.postMessage(message); + } + dispose(): void { this.worker.terminate(); } diff --git a/src/quest_editor/scripting/assembly.ts b/src/quest_editor/scripting/assembly.ts index 27bea91e..aabdd4d2 100644 --- a/src/quest_editor/scripting/assembly.ts +++ b/src/quest_editor/scripting/assembly.ts @@ -35,6 +35,10 @@ export type AssemblyWarning = { export type AssemblyError = AssemblyWarning; +export type AssemblySettings = { + manual_stack: boolean +}; + export function assemble( assembly: string[], manual_stack: boolean = false, diff --git a/src/quest_editor/scripting/assembly_worker.ts b/src/quest_editor/scripting/assembly_worker.ts index 57881415..f428dcb0 100644 --- a/src/quest_editor/scripting/assembly_worker.ts +++ b/src/quest_editor/scripting/assembly_worker.ts @@ -6,8 +6,9 @@ import { OutputMessageType, SignatureHelpInput, SignatureHelpOutput, + AssemblySettingsChangeInput, } from "./assembly_worker_messages"; -import { assemble } from "./assembly"; +import { assemble, AssemblySettings } from "./assembly"; import Logger from "js-logger"; import { SegmentType } from "./instructions"; import { Opcode, OPCODES_BY_MNEMONIC } from "./opcodes"; @@ -24,6 +25,10 @@ let lines: string[] = []; const messages: AssemblyWorkerInput[] = []; let timeout: any; +const assembly_settings: AssemblySettings = { + manual_stack: false +}; + ctx.onmessage = (e: MessageEvent) => { messages.push(e.data); @@ -52,6 +57,9 @@ function process_messages(): void { case InputMessageType.SignatureHelp: signature_help(message); break; + case InputMessageType.SettingsChange: + settings_change(message); + break; } } } @@ -130,8 +138,17 @@ function signature_help(message: SignatureHelpInput): void { ctx.postMessage(response); } +/** + * Apply changes to settings. + */ +function settings_change(message: AssemblySettingsChangeInput): void { + if (message.settings.hasOwnProperty("manual_stack")) { + assembly_settings.manual_stack = Boolean(message.settings.manual_stack); + } +} + function assemble_and_send(): void { - const assembler_result = assemble(lines); + const assembler_result = assemble(lines, assembly_settings.manual_stack); const map_designations = new Map(); for (const segment of assembler_result.object_code) { diff --git a/src/quest_editor/scripting/assembly_worker_messages.ts b/src/quest_editor/scripting/assembly_worker_messages.ts index c1718b09..fb82b52e 100644 --- a/src/quest_editor/scripting/assembly_worker_messages.ts +++ b/src/quest_editor/scripting/assembly_worker_messages.ts @@ -1,4 +1,4 @@ -import { AssemblyError, AssemblyWarning } from "./assembly"; +import { AssemblyError, AssemblyWarning, AssemblySettings } from "./assembly"; import { Segment } from "./instructions"; import { Opcode } from "./opcodes"; @@ -6,9 +6,10 @@ export enum InputMessageType { NewAssembly, AssemblyChange, SignatureHelp, + SettingsChange, } -export type AssemblyWorkerInput = NewAssemblyInput | AssemblyChangeInput | SignatureHelpInput; +export type AssemblyWorkerInput = NewAssemblyInput | AssemblyChangeInput | SignatureHelpInput | AssemblySettingsChangeInput; export type NewAssemblyInput = { readonly type: InputMessageType.NewAssembly; @@ -33,6 +34,11 @@ export type SignatureHelpInput = { readonly col: number; }; +export type AssemblySettingsChangeInput = { + readonly type: InputMessageType.SettingsChange; + readonly settings: Partial; +}; + export enum OutputMessageType { NewObjectCode, SignatureHelp, diff --git a/src/quest_editor/stores/AsmEditorStore.ts b/src/quest_editor/stores/AsmEditorStore.ts index 98836211..0c5e3cc3 100644 --- a/src/quest_editor/stores/AsmEditorStore.ts +++ b/src/quest_editor/stores/AsmEditorStore.ts @@ -69,6 +69,9 @@ export class AsmEditorStore implements Disposable { () => this._did_redo.emit({ value: "asm undo" }), ); + readonly inline_args_mode: WritableProperty = property(true); + readonly has_issues: WritableProperty = property(false); + private readonly disposer = new Disposer(); private readonly model_disposer = this.disposer.add(new Disposer()); private readonly _model: WritableProperty = property(undefined); @@ -88,6 +91,17 @@ export class AsmEditorStore implements Disposable { assembly_analyser.issues.observe(({ value }) => this.update_model_markers(value), { call_now: true, }), + + this.inline_args_mode.observe(() => { + // don't allow changing inline args mode if there are issues + if (!this.has_issues.val) { + this.update_assembly_settings(); + } + }), + + assembly_analyser.issues.observe(({ value }) => { + this.has_issues.val = Boolean(value.warnings.length) || Boolean(value.errors.length); + }) ); } @@ -188,6 +202,12 @@ export class AsmEditorStore implements Disposable { ), ); } + + private update_assembly_settings(): void { + assembly_analyser.update_settings({ + manual_stack: !this.inline_args_mode.val + }); + } } export const asm_editor_store = new AsmEditorStore();