Add automatic bind set functionality

Amber Yust [04-16-14 - 02:23]
Add automatic bind set functionality
Filename
Binder.lua
diff --git a/Binder.lua b/Binder.lua
index 7c2d0ed..8776d49 100644
--- a/Binder.lua
+++ b/Binder.lua
@@ -22,6 +22,12 @@ local function print(...)
     d(strjoin("", ...))
 end

+Binder.defaults = {
+    ["bindings"] = {},
+    ["auto"] = false,
+    ["autoSets"] = {},
+}
+
 function Binder.BuildActionTables()
     local actionHierarchy = {}
     local actionNames = {}
@@ -104,17 +110,21 @@ function Binder.RestoreBindingsFromTable()
     end
 end

-function Binder.SaveBindings(bindSetName)
+function Binder.SaveBindings(bindSetName, isSilent)
     if bindSetName == nil or bindSetName == "" then
         print("Usage: /binder save <set name>")
         return
     end
     Binder.BuildBindingsTable()
     Binder.savedVariables.bindings[bindSetName] = Binder.bindings
-    print("Saved ", Binder.bindCount, " bindings to bind set '", bindSetName, "'.")
+    if not isSilent then
+        print("Saved ", Binder.bindCount, " bindings to bind set '", bindSetName, "'.")
+    end
+    local character = GetUnitName("player")
+    Binder.savedVariables.autoSets[character] = bindSetName
 end

-function Binder.LoadBindings(bindSetName)
+function Binder.LoadBindings(bindSetName, isSilent)
     if bindSetName == nil or bindSetName == "" then
         print("Usage: /binder load <set name>")
         return
@@ -125,7 +135,11 @@ function Binder.LoadBindings(bindSetName)
     end
     Binder.bindings = Binder.savedVariables.bindings[bindSetName]
     Binder.RestoreBindingsFromTable()
-    print("Loaded ", Binder.bindCount, " bindings from bind set '", bindSetName, "'.")
+    if not isSilent then
+        print("Loaded ", Binder.bindCount, " bindings from bind set '", bindSetName, "'.")
+    end
+    local character = GetUnitName("player")
+    Binder.savedVariables.autoSets[character] = bindSetName
 end

 function Binder.ListBindings()
@@ -153,12 +167,83 @@ function Binder.DeleteBindings(bindSetName)
     print("Deleted bind set '", bindSetName, "'.")
 end

+function Binder.SetAuto(newValue)
+    if newValue == "on" then
+        Binder.savedVariables.auto = true
+        print("Enabled automatic bind set updates.")
+        Binder.LoadAutomaticBindings()
+    elseif newValue == "off" then
+        Binder.savedVariables.auto = false
+        print("Disabled automatic bind set updates.")
+    else
+        if Binder.savedVariables.auto then
+            print("Automatic bind set updates are on (disable with /binder auto off).")
+        else
+            print("Automatic bind set updates are off (enable with /binder auto on).")
+        end
+
+        local character = GetUnitName("player")
+        local setName = Binder.savedVariables.autoSets[character]
+        if setName ~= nil then
+            print("Currently active set: ", setName)
+        end
+    end
+end
+
+function Binder.SaveAutomaticBindings(isSilent)
+    -- No-op if automatic mode is disabled.
+    if not Binder.savedVariables.auto then return end
+
+    local character = GetUnitName("player")
+    local setName = Binder.savedVariables.autoSets[character]
+
+    if setName == nil then
+        setName = character .. "-auto"
+        Binder.savedVariables.autoSets[character] = setName
+    end
+
+    Binder.SaveBindings(setName, isSilent)
+end
+
+function Binder.LoadAutomaticBindings(isSilent)
+    -- No-op if automatic mode is disabled.
+    if not Binder.savedVariables.auto then return end
+
+    local character = GetUnitName("player")
+    local setName = Binder.savedVariables.autoSets[character]
+
+    if setName ~= nil then
+        Binder.LoadBindings(setName, isSilent)
+    else
+        -- If there isn't a set to load, but automatic mode is on,
+        -- create a new set.
+        Binder.SaveAutomaticBindings(isSilent)
+    end
+end
+
+function Binder.OnKeybindingSetOrCleared()
+    -- Silently update active bind set
+    Binder.SaveAutomaticBindings(true)
+end
+
+function Binder.OnKeybindingsLoaded()
+    -- Silently load active bind set
+    Binder.LoadAutomaticBindings(true)
+end
+
+function Binder.RegisterEvents()
+    EVENT_MANAGER:RegisterForEvent("Binder", EVENT_KEYBINDINGS_LOADED, Binder.OnKeybindingsLoaded)
+    EVENT_MANAGER:RegisterForEvent("Binder", EVENT_KEYBINDING_SET, Binder.OnKeybindingSetOrCleared)
+    EVENT_MANAGER:RegisterForEvent("Binder", EVENT_KEYBINDING_CLEARED, Binder.OnKeybindingSetOrCleared)
+end
+
 Binder.commands = {
     ["build"] = Binder.BuildBindingsTable,
     ["save"] = Binder.SaveBindings,
     ["load"] = Binder.LoadBindings,
     ["list"] = Binder.ListBindings,
     ["delete"] = Binder.DeleteBindings,
+    ["auto"] = Binder.SetAuto,
 }

 function Binder.SlashCommandHelp()
@@ -189,7 +274,13 @@ end

 function Binder.OnAddOnLoaded(event, addonName)
     if addonName ~= "Binder" then return end
-    Binder.savedVariables = ZO_SavedVars:NewAccountWide("Binder_SavedVariables", 1, "default", {["bindings"]={}})
+
+    Binder.savedVariables = ZO_SavedVars:NewAccountWide("Binder_SavedVariables", 1, "default", Binder.defaults)
+
+    -- Silently load the active bind set, if automatic mode is on.
+    Binder.LoadAutomaticBindings(true)
+
+    Binder.RegisterEvents()
 end

 EVENT_MANAGER:RegisterForEvent("Binder", EVENT_ADD_ON_LOADED, Binder.OnAddOnLoaded)