Implement keybind saver
This commit is contained in:
@@ -3,9 +3,10 @@
|
|||||||
## Version: 1.0.7
|
## Version: 1.0.7
|
||||||
## Notes: Manage, save, and restore action bar profiles
|
## Notes: Manage, save, and restore action bar profiles
|
||||||
## Author: Voodoomoose
|
## Author: Voodoomoose
|
||||||
## SavedVariables: ActionBarSaverReloaded
|
## SavedVariables: ActionBarSaverReloaded, KeybindSaverReloaded
|
||||||
|
|
||||||
B64.lua
|
B64.lua
|
||||||
Constants.lua
|
Constants.lua
|
||||||
Main.lua
|
Main.lua
|
||||||
Actions.lua
|
Actions.lua
|
||||||
|
KeybindSaver.lua
|
250
KeybindSaver.lua
Normal file
250
KeybindSaver.lua
Normal file
@@ -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 <set> - Saves your current keybinds under the given <set>")
|
||||||
|
print("/kbs restore <set> - Restores the saved <set>")
|
||||||
|
print("/kbs delete <set> - Deletes the saved <set>")
|
||||||
|
print("/kbs list - Lists all saved sets")
|
||||||
|
print("/kbs export <set> - Opens a window to export the given <set>")
|
||||||
|
print("/kbs import <set> - 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
|
Reference in New Issue
Block a user