From 99a227069603b14276f9322e401b52f190adb3d3 Mon Sep 17 00:00:00 2001 From: Dmitry Shin Date: Wed, 27 Oct 2021 16:26:05 +0400 Subject: [PATCH] Add hotkey setting --- README.md | 3 ++- index.js | 30 ++++++++++++++++++++++++++---- manifest.json | 7 ++++--- module.json | 3 +++ settings_migrator.js | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 settings_migrator.js diff --git a/README.md b/README.md index 9b229a0..4aa103c 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ The teleport scrolls should be in your inventory. A scroll is consumed when tele Toolbox(/8) | Command description --- | --- -**atlas** | Open the Village Atlas GUI (also you can use **Ctrl+Shift+J**). +**atlas** | Open the Village Atlas GUI (also you can use **Ctrl+Shift+J** by default). +**atlas hotkey [hotkey]** | Set new hotkey for opening GUI. ![](https://i.imgur.com/nGOWDyh.png) diff --git a/index.js b/index.js index 1f59e1b..e33b225 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,29 @@ module.exports = function Atlas(mod) { mod.game.initialize("inventory"); mod.game.on("enter_character_lobby", async () => applyCitiesNames()); - mod.command.add("atlas", () => ui.open()); + mod.command.add("atlas", { + "hotkey": arg => { + if (!arg) { + mod.command.message(`Current hotkey: ${mod.settings.hotkey}`); + } else { + if (arg.toLowerCase() !== mod.settings.hotkey.toLowerCase()) { + const hotkey = arg.toLowerCase().split("+").map(w => w[0].toUpperCase() + w.substr(1)).join("+"); + + try { + globalShortcut.register(hotkey, () => ui.open()); + globalShortcut.unregister(mod.settings.hotkey); + + mod.settings.hotkey = hotkey; + } catch (e) { + return mod.command.message(`Invalid hotkey: ${hotkey}`); + } + } + + mod.command.message(`New hotkey: ${mod.settings.hotkey}`); + } + }, + "$none": () => ui.open() + }); mod.hook("S_SPAWN_ME", 3, event => { player = event; }); mod.hook("C_PLAYER_LOCATION", 5, event => { player = event; }); @@ -28,7 +50,7 @@ module.exports = function Atlas(mod) { ui.post("/getTitle", (request, response) => { applyCitiesNames(); - response.json({ "title": `${citiesNames.get(1) || "World"} [Ctrl + Shift + J]` }); + response.json({ "title": `${citiesNames.get(1) || "World"} [${mod.settings.hotkey.replaceAll("+", " + ")}]` }); }); ui.post("/getCities", (request, response) => { @@ -76,10 +98,10 @@ module.exports = function Atlas(mod) { .forEach(res => citiesNames.set(res.attributes.id, res.attributes.string)); } - globalShortcut.register("Ctrl+Shift+J", () => ui.open()); + globalShortcut.register(mod.settings.hotkey, () => ui.open()); this.destructor = () => { - globalShortcut.unregister("Ctrl+Shift+J"); + globalShortcut.unregister(mod.settings.hotkey); mod.command.remove("atlas"); }; }; \ No newline at end of file diff --git a/manifest.json b/manifest.json index 6a55860..e06f2a9 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,10 @@ { "files": { - "README.md": "6a06002bdee61134f3f09ebf92c47ed4cb21d1f188e949e6b1ca7268ec857397", + "README.md": "c3b3d7cb9a39dddb4b016e4f1aea1b8203f8f1613d4346b2aad647eb9aad67b5", "cities.js": "2b64ff57044777c91a03b0d9ea427008b6a72ec73b335d6d1b0ad210e8e9b914", - "index.js": "e9ef40e27f0ccd086c4e4797a4eb2e38109a3dc9075fa5d5ed1ff13a6638186f", - "module.json": "6beb2cb3a7460c14efc5d5504818c7c1a7a36783d4cb58b88725f5c09cba32d7", + "index.js": "3bcae22c8dab328e63108ce584ead9e4704d1d3e14c9220e180fc89e2027aa20", + "module.json": "54ad76757cbcf31a4e848313ecbd904487a4fa75d5408cfce9a57842800b10b9", + "settings_migrator.js": "de482d8179a26a09e19772a1eec702d06ccbe2632db96db05bb9b9ca35cd2502", "ui/css/base.css": "8b4f64c051674f7f26ddd61b2730b53dce30c6dbc3c58edf42742172f8f82b8b", "ui/img/map.png": "0b8e1189ddb563b244630e6802735a0a8d0f8d0759208099f8e59cd3f61aff87", "ui/index.html": "3d45c91e51d32da5f4d20db43e6d2f6a306abba1226ab8ca1f17a38442c54c45", diff --git a/module.json b/module.json index 951b1ee..55a6cc7 100644 --- a/module.json +++ b/module.json @@ -4,6 +4,9 @@ "description": "Alternative Village Atlas based on Teleport Scrolls.", "options": { "niceName": "Atlas", + "settingsVersion": 1, + "settingsFile": "config.json", + "settingsMigrator": "settings_migrator.js", "reloadable": true }, "servers": ["https://raw.githubusercontent.com/hsdn/atlas/master/"], diff --git a/settings_migrator.js b/settings_migrator.js new file mode 100644 index 0000000..38489c4 --- /dev/null +++ b/settings_migrator.js @@ -0,0 +1,35 @@ +/* eslint-disable no-param-reassign */ +"use strict"; + +const DefaultSettings = { + "hotkey": "Ctrl+Shift+J", +}; + +module.exports = function MigrateSettings(from_ver, to_ver, settings) { + if (from_ver === undefined) return { ...DefaultSettings, ...settings }; + else if (from_ver === null) return DefaultSettings; + else { + from_ver = Number(from_ver); + to_ver = Number(to_ver); + + if (from_ver + 1 < to_ver) { + settings = MigrateSettings(from_ver, from_ver + 1, settings); + return MigrateSettings(from_ver + 1, to_ver, settings); + } + + const oldsettings = settings; + + switch (to_ver) { + default: + settings = Object.assign(DefaultSettings, {}); + + for (const option in oldsettings) { + if (settings[option] !== undefined) { + settings[option] = oldsettings[option]; + } + } + } + + return settings; + } +}; \ No newline at end of file