Experimental implementation of true global vars

Talen-Shei [08-09-15 - 22:05]
Experimental implementation of true global vars
Filename
KeyBindSwapper.lua
diff --git a/KeyBindSwapper.lua b/KeyBindSwapper.lua
index 5f957f3..fddb92f 100644
--- a/KeyBindSwapper.lua
+++ b/KeyBindSwapper.lua
@@ -16,8 +16,12 @@ KBS.Defaults.Character.DelTarget = "Main"
 KBS.Defaults.Character.saveType = "Global"
 KBS.Defaults.Character.BindingTarget = "Main"
 KBS.Defaults.Character.SyncMode = "Manual"
+KBS.Defaults.Account = {}
+KBS.Defaults.Account.SingleTarget = false
+KBS.Defaults.Account.BindingTarget = "Main"
+KBS.Defaults.Account.AutoSync = false

-KBS.CODE_VERSION = 0.51
+KBS.CODE_VERSION = 0.6
 KBS.SAVED_VAR_VERSION = 0.4
 KBS.UsableOnly = true
 KBS.SettingsFile = "KeyBindSwapperSettings"
@@ -72,68 +76,57 @@ end

 --[[
 	-- Useage:		setKeyBinds(profile)
-	-- @param:		{profile} Name of table in KBS.Global.KeyTables, if invalid we will use the saved target table
+	-- @param:		{profile} Name of table in KBS.Global.KeyTables, if invalid throw error
 	-- Used By:		Import Profile button, /setkeys
 ]]
 function setKeyBinds(profile)
-	local isSafe = true
+	local isSafe = false
 	if profile then
-		isSafe = false
 		for i=1, #KBS.Global.AvailKeyTables do
 			if KBS.Global.AvailKeyTables[i] == profile then
 				isSafe = true
-				-- d("we checked if it was safe")
 			end
 		end
-		if isSafe == true then
-			for layer = 1, #KBS.Global.KeyTables[profile].BindTable do
-				for cat = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category do
-					for action = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions do
-						if KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].isRebindable == true and KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].isHidden == false then
-							for index = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys do
-								keycode = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyCode
-								keymod1 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod1
-								keymod2 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod2
-								keymod3 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod3
-								keymod4 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod4
-								if IsProtectedFunction("BindKeyToAction") then
-									CallSecureProtected("BindKeyToAction", layer,cat,action,index,keycode,keymod1,keymod2,keymod3,keymod4)
-								else
-									BindKeyToAction(layer,cat,action,index,keycode,keymod1,keymod2,keymod3,keymod4)
-								end
-							end
-						end
-					end
-				end
+	elseif KBS.Account.SingleTarget then
+		for i=1, #KBS.Global.AvailKeyTables do
+			if KBS.Global.AvailKeyTables[i] == KBS.Account.BindingTarget then
+				profile = KBS.Account.BindingTarget
+				d("Loading Profile: "..profile)
+				isSafe = true
 			end
 		end
 	else
-		-- d("We didn't check if it was safe")
-		for layer = 1, #KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable do
-			for cat = 1, #KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category do
-				for action = 1, #KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions do
-					if  KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].isRebindable == true and KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].isHidden == false then
-						for index = 1, #KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys do
-							keycode = KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyCode
-							keymod1 = KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod1
-							keymod2 = KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod2
-							keymod3 = KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod3
-							keymod4 = KBS.Global.KeyTables[KBS.Character.BindingTarget].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod4
+		for i=1, #KBS.Global.AvailKeyTables do
+			if KBS.Global.AvailKeyTables[i] == KBS.Character.BindingTarget then
+				profile = KBS.Character.BindingTarget
+				d("Loading Profile: "..profile)
+				isSafe = true
+			end
+		end
+	end
+	if isSafe == true then
+		if KBS.Account.SingleTarget then
+		for layer = 1, #KBS.Global.KeyTables[profile].BindTable do
+			for cat = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category do
+				for action = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions do
+					if  KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].isRebindable == true and KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].isHidden == false then
+						for index = 1, #KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys do
+							keycode = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyCode
+							keymod1 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod1
+							keymod2 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod2
+							keymod3 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod3
+							keymod4 = KBS.Global.KeyTables[profile].BindTable[layer].Category[cat].Actions[action].Keys[index].KeyMod4
 							if keycode == 0 and keymod1 == 0 and keymod2 == 0 and keymod3 == 0 and keymod4 == 0 then
 								if IsProtectedFunction("UnbindKeyFromAction") then
 									CallSecureProtected("UnbindKeyFromAction", layer,cat,action,index)
-									-- d(layer..cat..action..index)
 								else
 									BindKeyToAction(layer,cat,action,index,keycode,keymod1,keymod2,keymod3,keymod4)
-									-- d(layer..cat..action..index)
 								end
 							else
 								if IsProtectedFunction("BindKeyToAction") then
 									CallSecureProtected("BindKeyToAction", layer,cat,action,index,keycode,keymod1,keymod2,keymod3,keymod4)
-									-- d(layer..cat..action..index)
 								else
 									BindKeyToAction(layer,cat,action,index,keycode,keymod1,keymod2,keymod3,keymod4)
-									-- d(layer..cat..action..index)
 								end
 							end
 						end
@@ -141,7 +134,8 @@ function setKeyBinds(profile)
 				end
 			end
 		end
-	end
+	else
+		d("Error: Profile '"..profile.."' Not found!")
 end

 --[[
@@ -150,28 +144,58 @@ end
 	-- @param:		{arg2} Name of table to create or overwrite
 	-- Used By:		Save Profile Button, /savekeys
 ]]
-function saveBinds(arg1, arg2)
-	if arg2 == nil then
-		if arg1 == nil then
-			arg1 = KBS.Character.saveType
-		end
-		if arg1 == "Global" then
-			getKeyBinds(KBS.Global.KeyTables.Main)
-		else
-			getKeyBinds(KBS.Character.KeyTables.Main)
+function saveBinds(profile)
+	local isSafe = false
+	local accountbound = false
+	if profile then
+		for i=1, #KBS.Global.AvailKeyTables do
+			if KBS.Global.AvailKeyTables[i] == profile then
+				isSafe = true
+			end
 		end
-	else
-		if arg1 == nil then
-			arg1 = KBS.Character.saveType
+	elseif KBS.Account.SingleTarget == true then
+		for i=1, #KBS.Global.AvailKeyTables do
+			if KBS.Global.AvailKeyTables[i] == KBS.Account.BindingTarget then
+				isSafe = true
+				profile = KBS.Account.BindingTarget
+				accountbound = true
+			end
 		end
-		if arg1 == "Global" then
-			KBS.Global.KeyTables[arg2] = {}
-			getKeyBinds(KBS.Global.KeyTables[arg2])
-		else
-			KBS.Character.KeyTables[arg2] = {}
-			getKeyBinds(KBS.Character.KeyTables[arg2])
+	elseif accountbound == false then
+		for i=1, #KBS.Global.AvailKeyTables do
+			if KBS.Global.AvailKeyTables[i] == KBS.Character.BindingTarget then
+				isSafe = true
+				profile = KBS.Account.BindingTarget
+				accountbound = true
+			end
 		end
 	end
+	if isSafe = true then
+		getKeyBinds(profile)
+	else
+		d("Error!")
+end
+	-- if arg2 == nil then
+	-- 	if arg1 == nil then
+	-- 		arg1 = KBS.Character.saveType
+	-- 	end
+	-- 	if arg1 == "Global" then
+	-- 		getKeyBinds(KBS.Global.KeyTables.Main)
+	-- 	else
+	-- 		getKeyBinds(KBS.Character.KeyTables.Main)
+	-- 	end
+	-- else
+	-- 	if arg1 == nil then
+	-- 		arg1 = KBS.Character.saveType
+	-- 	end
+	-- 	if arg1 == "Global" then
+	-- 		KBS.Global.KeyTables[arg2] = {}
+	-- 		getKeyBinds(KBS.Global.KeyTables[arg2])
+	-- 	else
+	-- 		KBS.Character.KeyTables[arg2] = {}
+	-- 		getKeyBinds(KBS.Character.KeyTables[arg2])
+	-- 	end
+	-- end
 end


@@ -237,13 +261,25 @@ local function CreateSettingsMenu()
 			type = "description",
 			text = "A Tool to save and import keybind profiles."..colorYellow.."For the time being please do not use Per-Character Save Type.",
 		},
+		-- [3] = {
+		-- 	type = "dropdown",
+		-- 	name = "Save Type",
+		-- 	choices = {"Global", "Account"},
+		-- 	default = "Global",
+		-- 	getFunc = function() return KBS.Character.saveType end,
+		-- 	setFunc = function(bValue) KBS.Character.saveType = bValue end,
+		-- },
 		[3] = {
-			type = "dropdown",
-			name = "Save Type",
-			choices = {"Global", "Per Character"},
-			default = "Global",
-			getFunc = function() return KBS.Character.saveType end,
-			setFunc = function(bValue) KBS.Character.saveType = bValue end,
+			type = "checkbox",
+			name = "Single Target Mode",
+			tooltip = "This sets an entire account to sync to one profile.",
+			getFunc = function() return KBS.Account.SingleTarget end,
+			setFunc = function(value) KBS.Account.SingleTarget = value end,
+			width = "full",	--or "half" (optional)
+			-- disabled = function() return db.someBooleanSetting end,	--or boolean (optional)
+			-- warning = "Will need to reload the UI.",	--(optional)
+			default = KBS.Defaults.Account.SingleTarget,	--(optional)
+			reference = "MyAddonCheckbox"	--(optional) unique global reference to control
 		},
 		[4] = {
 			type = "dropdown",
@@ -252,8 +288,19 @@ local function CreateSettingsMenu()
 			sort = "name-up",
 			reference = "KBSProfiles",
 			default = "Main",
-			getFunc = function() return KBS.Character.BindingTarget end,
-			setFunc = function(bValue) KBS.Character.BindingTarget = bValue end,
+			getFunc = function()
+			if KBS.Account.SingleTarget == true then
+				return KBS.Account.BindingTarget
+			else
+				return KBS.Character.BindingTarget
+			end,
+			setFunc = function(bValue)
+			if KBS.Account.SingleTarget == true then
+				KBS.Account.BindingTarget = bValue
+			else
+				KBS.Character.BindingTarget = bValue
+			end,
+			 -- KBS.Character.BindingTarget = bValue end,
 		},
 		[5] = {
 			type = "dropdown",
@@ -352,15 +399,20 @@ end

 function InitializePlugin ()
     if type(KBSMultiAccount) ~= "table" then
-    	d("We're populating")
+    	-- d("We're populating")
         KBSMultiAccount = KBS.Defaults.Global
         KBS.Global = KBSMultiAccount
     else
         KBS.Global = KBSMultiAccount
     end
+	KBS.Account = ZO_SavedVars:NewAccountWide(KBS.SettingsFile, KBS.SAVED_VAR_VERSION, nil, KBS.Defaults.Account, KBS.SVProfile)
 	KBS.Character = ZO_SavedVars:New(KBS.SettingsFile, KBS.SAVED_VAR_VERSION, nil, KBS.Defaults.Character, KBS.SVProfile)
 	EVENT_MANAGER:UnregisterForEvent("KeyBindSwapper", EVENT_ADD_ON_LOADED)
-	if KBS.Character.SyncMode == "Automatic" then
+	if KBS.Account.SingleTarget == true then
+		if KBS.Account.AutoSync == true then
+			setKeyBinds(KBS.Account.BindingTarget)
+		end
+	elseif KBS.Character.SyncMode == "Automatic" then
 		setKeyBinds()
 	end
 end