From ee1571a1c40ebcd864d2b3cf7e77c8934c8e0d2a Mon Sep 17 00:00:00 2001 From: luboslenco Date: Fri, 10 Jan 2020 16:30:02 +0100 Subject: [PATCH] Import and export keymaps --- Sources/arm/io/ImportKeymap.hx | 21 ++++++++++++++++++ Sources/arm/sys/Path.hx | 5 +++++ Sources/arm/ui/BoxPreferences.hx | 38 +++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 Sources/arm/io/ImportKeymap.hx diff --git a/Sources/arm/io/ImportKeymap.hx b/Sources/arm/io/ImportKeymap.hx new file mode 100644 index 00000000..b5d1148c --- /dev/null +++ b/Sources/arm/io/ImportKeymap.hx @@ -0,0 +1,21 @@ +package arm.io; + +import arm.sys.Path; +import arm.sys.File; + +class ImportKeymap { + + public static function run(path: String) { + if (!Path.isJson(path)) { + Log.error(Strings.error1); + return; + } + + var filename = path.substr(path.lastIndexOf(Path.sep) + 1); + var dstPath = Path.data() + Path.sep + "keymap_presets" + Path.sep + filename; + File.copy(path, dstPath); // Copy to preset folder + arm.ui.BoxPreferences.fetchKeymaps(); // Refresh file list + arm.ui.BoxPreferences.presetHandle.position = arm.ui.BoxPreferences.getPresetIndex(); + Log.info("Keymap '" + filename + "' imported."); + } +} diff --git a/Sources/arm/sys/Path.hx b/Sources/arm/sys/Path.hx index 7908bcdd..37df436d 100644 --- a/Sources/arm/sys/Path.hx +++ b/Sources/arm/sys/Path.hx @@ -91,6 +91,11 @@ class Path { // p.endsWith(".zip"); } + public static function isJson(path: String): Bool { + var p = path.toLowerCase(); + return p.endsWith(".json"); + } + public static function isBaseTex(p: String): Bool { return p.endsWith("_albedo") || p.endsWith("_alb") || diff --git a/Sources/arm/ui/BoxPreferences.hx b/Sources/arm/ui/BoxPreferences.hx index b4cabb7a..f7139799 100644 --- a/Sources/arm/ui/BoxPreferences.hx +++ b/Sources/arm/ui/BoxPreferences.hx @@ -8,6 +8,7 @@ import iron.data.Data; import arm.node.MaterialParser; import arm.data.LayerSlot; import arm.io.ImportPlugin; +import arm.io.ImportKeymap; import arm.sys.Path; import arm.sys.File; using StringTools; @@ -17,6 +18,7 @@ class BoxPreferences { public static var htab = Id.handle(); public static var filesPlugin: Array = null; public static var filesKeymap: Array = null; + public static var presetHandle: Handle; @:access(zui.Zui) public static function show() { @@ -200,19 +202,32 @@ class BoxPreferences { if (ui.tab(htab, "Keymap")) { if (filesKeymap == null) { - filesKeymap = File.readDirectory(Path.data() + Path.sep + "keymap_presets"); - for (i in 0...filesKeymap.length) { - filesKeymap[i] = filesKeymap[i].substr(0, filesKeymap[i].length - 5); // Strip .json - } + fetchKeymaps(); } - var presetHandle = Id.handle({position: filesKeymap.indexOf(Config.raw.keymap.substr(0, Config.raw.keymap.length - 5))}); // Strip .json - ui.combo(presetHandle, filesKeymap, "Preset", true); + ui.row([1 / 2, 1 / 4, 1 / 4]); + + presetHandle = Id.handle({position: getPresetIndex()}); + ui.combo(presetHandle, filesKeymap, "Preset"); if (presetHandle.changed) { Config.raw.keymap = filesKeymap[presetHandle.position] + ".json"; Config.applyConfig(); Config.loadKeymap(); } + + if (ui.button("Import")) { + UIFiles.show("json", false, function(path: String) { + ImportKeymap.run(path); + }); + } + if (ui.button("Export")) { + UIFiles.show("json", true, function(dest: String) { + if (!UIFiles.filename.endsWith(".json")) UIFiles.filename += ".json"; + var path = Path.data() + Path.sep + "keymap_presets" + Path.sep + Config.raw.keymap; + File.copy(path, dest + Path.sep + UIFiles.filename); + }); + } + ui.separator(8, false); var i = 0; @@ -321,6 +336,17 @@ plugin.drawUI = function(ui) { }, 500, 310); } + public static function fetchKeymaps() { + filesKeymap = File.readDirectory(Path.data() + Path.sep + "keymap_presets"); + for (i in 0...filesKeymap.length) { + filesKeymap[i] = filesKeymap[i].substr(0, filesKeymap[i].length - 5); // Strip .json + } + } + + public static function getPresetIndex(): Int { + return filesKeymap.indexOf(Config.raw.keymap.substr(0, Config.raw.keymap.length - 5)); // Strip .json + } + static function setScale() { var scale = Config.raw.window_scale; UITrait.inst.ui.setScale(scale);