From 0638896bc3182124f0123018a28575e9a44081cd Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 6 May 2025 22:52:46 +0200 Subject: [PATCH] Rename all functions to reduce duplication --- Actions.lua | 32 +++--- KeybindSaver.lua | 248 +++++++++++++++++++++++------------------------ Main.lua | 2 + 3 files changed, 142 insertions(+), 140 deletions(-) diff --git a/Actions.lua b/Actions.lua index 3c880ec..c26f1f7 100644 --- a/Actions.lua +++ b/Actions.lua @@ -104,7 +104,7 @@ end ---@param setName string ---@return nil -function SaveSet(setName) +function SaveActionbarSet(setName) if not setName or setName == "" then print("Set name cannot be empty") return @@ -140,7 +140,7 @@ end ---@param setName string ---@return nil -function RestoreSet(setName) +function RestoreActionbarSet(setName) if not setName or setName == "" then print("Set name cannot be empty") return @@ -203,7 +203,7 @@ end ---@param setName string ---@return nil -function DeleteSet(setName) +function DeleteActionbarSet(setName) if not setName or setName == "" then print("Set name cannot be empty") return @@ -220,7 +220,7 @@ function DeleteSet(setName) end ---@return nil -function ListSets() +function ListActionbarSets() local sets = {} for setName, foo in pairs(ActionBarSaverDaved.sets) do sets[#sets + 1] = setName @@ -338,7 +338,7 @@ importExportFrameTextBox:SetScript("OnEscapePressed", function(self) local lines = { strsplit("\n", self:GetText()) } for _, line in ipairs(lines) do line = strtrim(line) - if line ~= "" then ImportSet(importingSet, line) end + if line ~= "" then ImportActionbarSet(importingSet, line) end end importingSet = nil end @@ -346,7 +346,7 @@ end) ---@param setName string ---@return nil -function ExportSet(setName) +function ExportActionbarSet(setName) local set = ActionBarSaverDaved.sets[setName] if not set then print(string.format("No set with the name '%s' exists", setName)) @@ -451,7 +451,7 @@ end ---@param setName string ---@param str string ---@return nil -function ImportSet(setName, str) +function ImportActionbarSet(setName, str) if not setName or setName == "" then print("Must provide a valid set name") return @@ -483,7 +483,7 @@ end ---@param setName string ---@return nil -function ImportSetDialogue(setName) +function ImportActionbarSetDialogue(setName) if not setName or setName == "" then print("Must provide a valid set name") return @@ -495,7 +495,7 @@ function ImportSetDialogue(setName) end ---@return nil -function PrintUsage() +function PrintActionbarUsage() print("ABS Slash commands") print("/abs save - Saves your current action bar setup under the given ") print("/abs restore - Restores the saved ") @@ -512,16 +512,16 @@ SlashCmdList["ABS"] = function(argv) local args = { strsplit(" ", argv) } local cmd = args[1] - if cmd == "save" then SaveSet(args[2]) end - if cmd == "restore" then RestoreSet(args[2]) end - if cmd == "delete" then DeleteSet(args[2]) end - if cmd == "list" then ListSets() end + if cmd == "save" then SaveActionbarSet(args[2]) end + if cmd == "restore" then RestoreActionbarSet(args[2]) end + if cmd == "delete" then DeleteActionbarSet(args[2]) end + if cmd == "list" then ListActionbarSets() end if cmd == "alias" then AliasSpell(args[2], args[3]) end if cmd == "unalias" then DeleteSpellAliases(args[2]) end if cmd == "aliases" then ListAliases() end - if cmd == "export" then ExportSet(args[2]) end - if cmd == "import" then ImportSetDialogue(args[2]) end + if cmd == "export" then ExportActionbarSet(args[2]) end + if cmd == "import" then ImportActionbarSetDialogue(args[2]) end - if cmd == "" or not cmd then PrintUsage() end + if cmd == "" or not cmd then PrintActionbarUsage() end end SLASH_ABS1 = "/abs" diff --git a/KeybindSaver.lua b/KeybindSaver.lua index 3e284f3..f54a075 100644 --- a/KeybindSaver.lua +++ b/KeybindSaver.lua @@ -10,6 +10,119 @@ frame:SetScript("OnEvent", function(self, event, addon) KeybindSaverDaved.sets = KeybindSaverDaved.sets or {} end) +---@param setName string +---@return nil +local function SaveKeybindSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + ---@type table + local set = {} + local numBindings = GetNumBindings() + + for i = 1, numBindings do + local command, _, key1, key2 = GetBinding(i) + if key1 or key2 then set[command] = { + key1 = key1, + key2 = key2, + } end + end + + KeybindSaverDaved.sets[setName] = set + print(string.format("Saved keybind set '%s'!", setName)) +end + +---@param setName string +---@return nil +local function RestoreKeybindSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + local set = KeybindSaverDaved.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 + +---@param setName string +---@return nil +local function DeleteKeybindSet(setName) + if not setName or setName == "" then + print("Set name cannot be empty") + return + end + + if not KeybindSaverDaved.sets[setName] then + print(string.format("No set with the name '%s' exists", setName)) + return + end + + KeybindSaverDaved.sets[setName] = nil + print(string.format("Deleted keybind set '%s'", setName)) +end + +---@return nil +local function ListKeybindSets() + local sets = {} + for setName in pairs(KeybindSaverDaved.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 + +---@param setName string +---@param importStr string +---@return nil +local function ImportKeybindSet(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 + + KeybindSaverDaved.sets[setName] = set + print(string.format("Imported keybind set '%s'", setName)) +end + -- Create the import/export frame local importExportFrame = CreateFrame("Frame", "KBSImportExportFrame", UIParent) importExportFrame:SetSize(512, 512) @@ -56,99 +169,14 @@ importExportFrameTextBox:SetScript("OnEscapePressed", function(self) importExportFrame:Hide() if importingSet then local text = self:GetText() - if text and text ~= "" then ImportSet(importingSet, text) end + if text and text ~= "" then ImportKeybindSet(importingSet, text) end importingSet = nil end end) ---@param setName string ---@return nil -local function SaveSet(setName) - if not setName or setName == "" then - print("Set name cannot be empty") - return - end - - ---@type table - local set = {} - local numBindings = GetNumBindings() - - for i = 1, numBindings do - local command, _, key1, key2 = GetBinding(i) - if key1 or key2 then set[command] = { - key1 = key1, - key2 = key2, - } end - end - - KeybindSaverDaved.sets[setName] = set - print(string.format("Saved keybind set '%s'!", setName)) -end - ----@param setName string ----@return nil -local function RestoreSet(setName) - if not setName or setName == "" then - print("Set name cannot be empty") - return - end - - local set = KeybindSaverDaved.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 - ----@param setName string ----@return nil -local function DeleteSet(setName) - if not setName or setName == "" then - print("Set name cannot be empty") - return - end - - if not KeybindSaverDaved.sets[setName] then - print(string.format("No set with the name '%s' exists", setName)) - return - end - - KeybindSaverDaved.sets[setName] = nil - print(string.format("Deleted keybind set '%s'", setName)) -end - ----@return nil -local function ListSets() - local sets = {} - for setName in pairs(KeybindSaverDaved.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 - ----@param setName string ----@return nil -local function ExportSet(setName) +local function ExportKeybindSet(setName) local set = KeybindSaverDaved.sets[setName] if not set then print(string.format("No set with the name '%s' exists", setName)) @@ -168,36 +196,8 @@ local function ExportSet(setName) end ---@param setName string ----@param importStr string ---@return nil -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 - - KeybindSaverDaved.sets[setName] = set - print(string.format("Imported keybind set '%s'", setName)) -end - ----@param setName string ----@return nil -local function ImportSetDialogue(setName) +local function ImportKeybindSetDialogue(setName) if not setName or setName == "" then print("Set name cannot be empty") return @@ -209,7 +209,7 @@ local function ImportSetDialogue(setName) end ---@return nil -local function PrintUsage() +local function PrintKeybindUsage() print("Keybind Saver Slash Commands:") print("/kbs save - Saves your current keybinds under the given ") print("/kbs restore - Restores the saved ") @@ -224,12 +224,12 @@ SlashCmdList["KBS"] = function(argv) local args = { strsplit(" ", argv) } local cmd = args[1] - if cmd == "save" then SaveSet(args[2]) end - if cmd == "restore" then RestoreSet(args[2]) end - if cmd == "delete" then DeleteSet(args[2]) end - if cmd == "list" then ListSets() end - if cmd == "export" then ExportSet(args[2]) end - if cmd == "import" then ImportSetDialogue(args[2]) end + if cmd == "save" then SaveKeybindSet(args[2]) end + if cmd == "restore" then RestoreKeybindSet(args[2]) end + if cmd == "delete" then DeleteKeybindSet(args[2]) end + if cmd == "list" then ListKeybindSets() end + if cmd == "export" then ExportKeybindSet(args[2]) end + if cmd == "import" then ImportKeybindSetDialogue(args[2]) end - if cmd == "" or not cmd then PrintUsage() end + if cmd == "" or not cmd then PrintKeybindUsage() end end diff --git a/Main.lua b/Main.lua index 846f609..cb2161e 100644 --- a/Main.lua +++ b/Main.lua @@ -17,3 +17,5 @@ frame:SetScript("OnEvent", function(self, event, addon) KeybindSaverDaved = KeybindSaverDaved or {} KeybindSaverDaved.sets = KeybindSaverDaved.sets or {} end) + +-- TODO: Fix equipment sets (saving reloading exporting importing...) \ No newline at end of file