From 8943dc045cb16a6cfa54637759a87c9ab23f543e Mon Sep 17 00:00:00 2001 From: tobspr Date: Mon, 17 Jan 2022 09:34:34 +0100 Subject: [PATCH] Add support for paste signals --- electron/index.js | 4 +++ mod_examples/combined.js | 49 +++++++++++++------------------- mod_examples/pasting.js | 25 ++++++++++++++++ src/js/core/input_distributor.js | 9 ++++++ src/js/core/input_receiver.js | 3 ++ 5 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 mod_examples/pasting.js diff --git a/electron/index.js b/electron/index.js index 5f63789b..bc7cf393 100644 --- a/electron/index.js +++ b/electron/index.js @@ -8,6 +8,10 @@ const steam = require("./steam"); const asyncLock = require("async-lock"); const windowStateKeeper = require("electron-window-state"); +// Disable hardware key handling, i.e. being able to pause/resume the game music +// with hardware keys +app.commandLine.appendSwitch("disable-features", "HardwareMediaKeyHandling"); + const isDev = app.commandLine.hasSwitch("dev"); const isLocal = app.commandLine.hasSwitch("local"); const safeMode = app.commandLine.hasSwitch("safe-mode"); diff --git a/mod_examples/combined.js b/mod_examples/combined.js index b74d13a1..57d2f218 100644 --- a/mod_examples/combined.js +++ b/mod_examples/combined.js @@ -106,17 +106,6 @@ class Mod extends shapez.Mod { // Add an atlas this.modInterface.registerAtlas(RESOURCES["demoAtlas.png"], RESOURCES["demoAtlas.json"]); - this.modInterface.registerTranslations("en", { - ingame: { - interactiveTutorial: { - title: "Hello", - hints: { - "1_1_extractor": "World!", - }, - }, - }, - }); - // Register a new component this.modInterface.registerComponent(DemoModComponent); @@ -128,29 +117,29 @@ class Mod extends shapez.Mod { drawHooks: ["staticAfter"], }); - // Register the new building - this.modInterface.registerNewBuilding({ - metaClass: MetaDemoModBuilding, - buildingIconBase64: RESOURCES["demoBuilding.png"], + // // Register the new building + // this.modInterface.registerNewBuilding({ + // metaClass: MetaDemoModBuilding, + // buildingIconBase64: RESOURCES["demoBuilding.png"], - variantsAndRotations: [ - { - description: "A test building", - name: "A test name", + // variantsAndRotations: [ + // { + // description: "A test building", + // name: "A test name", - regularImageBase64: RESOURCES["demoBuilding.png"], - blueprintImageBase64: RESOURCES["demoBuildingBlueprint.png"], - tutorialImageBase64: RESOURCES["demoBuildingBlueprint.png"], - }, - ], - }); + // regularImageBase64: RESOURCES["demoBuilding.png"], + // blueprintImageBase64: RESOURCES["demoBuildingBlueprint.png"], + // tutorialImageBase64: RESOURCES["demoBuildingBlueprint.png"], + // }, + // ], + // }); // Add it to the regular toolbar - this.modInterface.addNewBuildingToToolbar({ - toolbar: "regular", - location: "primary", - metaClass: MetaDemoModBuilding, - }); + // this.modInterface.addNewBuildingToToolbar({ + // toolbar: "regular", + // location: "primary", + // metaClass: MetaDemoModBuilding, + // }); // Register keybinding this.modInterface.registerIngameKeybinding({ diff --git a/mod_examples/pasting.js b/mod_examples/pasting.js new file mode 100644 index 00000000..5da71fb7 --- /dev/null +++ b/mod_examples/pasting.js @@ -0,0 +1,25 @@ +/** + * This is the minimal structure of a mod + */ + +const METADATA = { + website: "https://tobspr.io", + author: "tobspr", + name: "Mod Example: Pasting", + version: "1", + id: "pasting", + description: "Shows how to properly receive paste events ingame", +}; + +class Mod extends shapez.Mod { + init() { + this.signals.gameInitialized.add(root => { + root.gameState.inputReciever.paste.add(event => { + event.preventDefault(); + + const data = event.clipboardData.getData("text"); + this.dialogs.showInfo("Pasted", "You pasted: '" + data + "'"); + }); + }); + } +} diff --git a/src/js/core/input_distributor.js b/src/js/core/input_distributor.js index debdba6c..be5440a9 100644 --- a/src/js/core/input_distributor.js +++ b/src/js/core/input_distributor.js @@ -149,6 +149,8 @@ export class InputDistributor { window.addEventListener("mouseup", this.handleKeyMouseUp.bind(this)); window.addEventListener("blur", this.handleBlur.bind(this)); + + document.addEventListener("paste", this.handlePaste.bind(this)); } forwardToReceiver(eventId, payload = null) { @@ -186,6 +188,13 @@ export class InputDistributor { this.keysDown.clear(); } + /** + * + */ + handlePaste(ev) { + this.forwardToReceiver("paste", ev); + } + /** * @param {KeyboardEvent | MouseEvent} event */ diff --git a/src/js/core/input_receiver.js b/src/js/core/input_receiver.js index ae54f24d..164ab84b 100644 --- a/src/js/core/input_receiver.js +++ b/src/js/core/input_receiver.js @@ -12,12 +12,15 @@ export class InputReceiver { // Dispatched on destroy this.destroyed = new Signal(); + + this.paste = new Signal(); } cleanup() { this.backButton.removeAll(); this.keydown.removeAll(); this.keyup.removeAll(); + this.paste.removeAll(); this.destroyed.dispatch(); }