From 2b1baaca361fe133aee4e7f45ab6b5c02ec3bf46 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 6 May 2025 21:42:06 +0200 Subject: [PATCH] Implement keybind saver --- ActionBarSaverReloaded.toc | 5 +- KeybindSaver.lua | 250 +++++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 KeybindSaver.lua diff --git a/ActionBarSaverReloaded.toc b/ActionBarSaverReloaded.toc index ee84eae..4555c0a 100644 --- a/ActionBarSaverReloaded.toc +++ b/ActionBarSaverReloaded.toc @@ -3,9 +3,10 @@ ## Version: 1.0.7 ## Notes: Manage, save, and restore action bar profiles ## Author: Voodoomoose -## SavedVariables: ActionBarSaverReloaded +## SavedVariables: ActionBarSaverReloaded, KeybindSaverReloaded B64.lua Constants.lua Main.lua -Actions.lua \ No newline at end of file +Actions.lua +KeybindSaver.lua \ No newline at end of file diff --git a/KeybindSaver.lua b/KeybindSaver.lua new file mode 100644 index 0000000..4d1263d --- /dev/null +++ b/KeybindSaver.lua @@ -0,0 +1,250 @@ +local ADDON_NAME, shared = ... + +-- Initialize saved variables +local frame = CreateFrame("Frame") +frame:RegisterEvent("ADDON_LOADED") +frame:SetScript("OnEvent", function(self, event, addon) + if addon ~= ADDON_NAME then + return + end + + KeybindSaverReloaded = KeybindSaverReloaded or {} + KeybindSaverReloaded.sets = KeybindSaverReloaded.sets or {} +end) + +-- Create the import/export frame +local importExportFrame = CreateFrame("Frame", "KBSImportExportFrame", UIParent) +importExportFrame:SetSize(512, 512) +importExportFrame:SetPoint("CENTER") +importExportFrame:SetFrameStrata("HIGH") +importExportFrame:EnableMouse(true) +importExportFrame:SetMovable(true) +importExportFrame:SetResizable(false) +importExportFrame:SetBackdrop({ + bgFile = "Interface/Tooltips/UI-Tooltip-Background", + edgeFile = "Interface/Tooltips/UI-Tooltip-Border", + tile = true, + tileSize = 4, + edgeSize = 4, + insets = { + left = 4, + right = 4, + top = 4, + bottom = 4, + }, +}) +importExportFrame:SetBackdropColor(0, 0, 0, 0.8) +importExportFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) + +importExportFrame:SetMovable(true) +importExportFrame:EnableMouse(true) +importExportFrame:RegisterForDrag("LeftButton") +importExportFrame:SetScript("OnDragStart", function(self) + self:StartMoving() +end) +importExportFrame:SetScript("OnDragStop", function(self) + self:StopMovingOrSizing() +end) +importExportFrame:SetScript("OnShow", function(self) + self:SetScale(1) +end) +importExportFrame:Hide() + +local importingSet = nil +local importExportFrameTextBox = CreateFrame("EditBox", "KBSImportExportFrameTextBox", importExportFrame) +importExportFrameTextBox:SetSize(512, 512) +importExportFrameTextBox:SetPoint("TOPLEFT", importExportFrame, "TOPLEFT", 0, 0) +importExportFrameTextBox:SetFont("Fonts\\FRIZQT__.ttf", 12) +importExportFrameTextBox:SetTextColor(1, 1, 1, 1) +importExportFrameTextBox:SetTextInsets(20, 20, 20, 20) +importExportFrameTextBox:SetMultiLine(true) +importExportFrameTextBox:SetAutoFocus(true) +importExportFrameTextBox:SetMaxLetters(1000000) +importExportFrameTextBox:SetScript("OnEscapePressed", function(self) + importExportFrame:Hide() + if importingSet then + local text = self:GetText() + if text and text ~= "" then + ImportSet(importingSet, text) + end + importingSet = nil + end +end) + + +local function SaveSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + local set = {} + local numBindings = GetNumBindings() + + for i = 1, numBindings do + local command, category, key1, key2 = GetBinding(i) + if key1 or key2 then + set[command] = { + key1 = key1, + key2 = key2, + } + end + end + + KeybindSaverReloaded.sets[setName] = set + print(string.format("Saved keybind set '%s'!", setName)) +end + +local function RestoreSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + local set = KeybindSaverReloaded.sets[setName] + if not set then + print(string.format("No set with the name '%s' exists", setName)) + return + end + + -- Clear all current bindings first + ClearOverrideBindings(frame) + + -- Restore saved bindings + for command, keys in pairs(set) do + if keys.key1 then + SetBinding(keys.key1, command) + end + if keys.key2 then + SetBinding(keys.key2, command) + end + end + + -- Save the changes + SaveBindings(GetCurrentBindingSet()) + print(string.format("Restored keybind set '%s'", setName)) +end + +local function DeleteSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + if not KeybindSaverReloaded.sets[setName] then + print(string.format("No set with the name '%s' exists", setName)) + return + end + + KeybindSaverReloaded.sets[setName] = nil + print(string.format("Deleted keybind set '%s'", setName)) +end + +local function ListSets() + local sets = {} + for setName in pairs(KeybindSaverReloaded.sets) do + table.insert(sets, setName) + end + table.sort(sets) + + if #sets == 0 then + print("No keybind sets found") + else + print("Saved keybind sets:") + print(table.concat(sets, ", ")) + end +end + +local function ExportSet(setName) + local set = KeybindSaverReloaded.sets[setName] + if not set then + print(string.format("No set with the name '%s' exists", setName)) + return + end + + local export = {} + for command, keys in pairs(set) do + if keys.key1 then + table.insert(export, string.format("%s:%s", command, keys.key1)) + end + if keys.key2 then + table.insert(export, string.format("%s:%s", command, keys.key2)) + end + end + + local exportStr = table.concat(export, "\n") + importExportFrame:Show() + importExportFrameTextBox:SetText(exportStr) + importExportFrameTextBox:SetFocus() +end + +local function ImportSet(setName, importStr) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + local set = {} + local lines = { strsplit("\n", importStr) } + + for _, line in ipairs(lines) do + local command, key = strsplit(":", line) + if command and key then + if not set[command] then + set[command] = {} + end + if not set[command].key1 then + set[command].key1 = key + else + set[command].key2 = key + end + end + end + + KeybindSaverReloaded.sets[setName] = set + print(string.format("Imported keybind set '%s'", setName)) +end + +local function ImportSetDialogue(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + importingSet = setName + importExportFrameTextBox:SetText("") + importExportFrame:Show() + importExportFrameTextBox:SetFocus() +end + +local function PrintUsage() + print("Keybind Saver Slash Commands:") + print("/kbs save - Saves your current keybinds under the given ") + print("/kbs restore - Restores the saved ") + print("/kbs delete - Deletes the saved ") + print("/kbs list - Lists all saved sets") + print("/kbs export - Opens a window to export the given ") + print("/kbs import - Opens a window to import a keybind set") +end + +-- Register slash commands +SLASH_KBS1 = "/kbs" +SlashCmdList["KBS"] = function(argv) + local args = { strsplit(" ", argv) } + local cmd = args[1] + + if cmd == "save" then + SaveSet(args[2]) + elseif cmd == "restore" then + RestoreSet(args[2]) + elseif cmd == "delete" then + DeleteSet(args[2]) + elseif cmd == "list" then + ListSets() + elseif cmd == "export" then + ExportSet(args[2]) + elseif cmd == "import" then + ImportSetDialogue(args[2]) + else + PrintUsage() + end +end