Add LAM configuration

Scott Yeskie [05-23-14 - 00:32]
Add LAM configuration
Filename
GuildCharNames.lua
diff --git a/GuildCharNames.lua b/GuildCharNames.lua
index 1287f71..aa4271a 100644
--- a/GuildCharNames.lua
+++ b/GuildCharNames.lua
@@ -18,6 +18,9 @@ GuildCharInfo.init = function()
     GuildCharInfo.lut = ZO_SavedVars:NewAccountWide('GuildCharacterInfo', 0.9, "cache", emptyDefault)
     GuildCharInfo.createLUT()
     GuildCharInfo.player = GetDisplayName()
+
+    GuildCharInfo.initUI()
+
 	-- register for guild events
 	EVENT_MANAGER:RegisterForEvent("GuildMemberLeveled", EVENT_GUILD_MEMBER_CHARACTER_LEVEL_CHANGED, GuildCharInfo.guildUpdateEvent)
 	EVENT_MANAGER:RegisterForEvent("GuildMemberLoginout", EVENT_GUILD_MEMBER_PLAYER_STATUS_CHANGED, GuildCharInfo.guildUpdateEvent)
@@ -51,7 +54,7 @@ GuildCharInfo.createLUT = function()
 			acctName = GetGuildMemberInfo(guild,i)
 			hasChar, charName, _, _, _, lvl, vr = GetGuildMemberCharacterInfo(guild,i)
 			if hasChar then
-				GuildCharInfo.PutToLUT(acctName, charName, lvl, vr, guild, i)
+				GuildCharInfo.putToLUT(acctName, charName, lvl, vr, guild, i)
 			end
 		end
 	end
@@ -66,7 +69,7 @@ end
 	@post Character added to LUT. If account already exists and charLevel > level in LUT,
 		will replace main character
 ]]--
-GuildCharInfo.PutToLUT = function(acctName, charName, charLevel, charVR, guild, index)
+GuildCharInfo.putToLUT = function(acctName, charName, charLevel, charVR, guild, index)
 	if charLevel == nil then charLevel = 0 end
 	if charVR == nil then charVR = 0 end

@@ -112,7 +115,7 @@ GuildCharInfo.guildUpdateEvent = function(_, guildId, acctName)
 			local hasChar, charName, _, _, _, lvl, vr = GetGuildMemberCharacterInfo(guildId,i)
 			if hasChar then
 				--d("Found update: " .. acctName .. ", " .. charName .. ", " .. lvl .. "+" .. vr)
-				GuildCharInfo.PutToLUT(acctName, charName, lvl, vr, guildId, i)
+				GuildCharInfo.putToLUT(acctName, charName, lvl, vr, guildId, i)
 			end
 			return
 		end
@@ -152,10 +155,15 @@ end
 ]]--

 GuildCharInfo.generateLink = function(acctName)
+    local color, text = GuildCharInfo.cfg.linkColor,  GuildCharInfo.cfg.linkText
+    if color == nil or #color ~= 6 then color = "EEEEEE" end
+    if text == nil then text = "[*]" end
+
 	local data = acctName
 	data = data .. "," .. GuildCharInfo.lut[acctName].guild
     data = data .. "," .. GuildCharInfo.lut[acctName].index
-    local link = "|HEEEEEE:playerDetail:" .. data .. "|h[*]|h"
+    data = data .. ",[]" --Fix for pNames link handling on self
+    local link = "|H" .. color .. ":playerDetail:" .. data .. "|h" .. text .. "|h"
     return link
 end

@@ -169,7 +177,7 @@ GuildCharInfo.getFormattedNameLink = function(channel, acctName, _)
     local ChanInfoArray = ZO_ChatSystem_GetChannelInfo()
     local info = ChanInfoArray[channel]

-    local includeMain, includeAcct = true, false
+    local includeMain, includeAcct = GuildCharInfo.cfg.showMain, GuildCharInfo.cfg.showAcct

 	--Short-circuit if not in LUT
 	if GuildCharInfo.lut[acctName] == nil then
@@ -196,13 +204,13 @@ GuildCharInfo.getFormattedNameLink = function(channel, acctName, _)
 		name = name .. " (" .. main .. ")"
 	end

+    --TODO: Make the account name into a link
 	if includeAcct then
-		name = name .. " " .. acctName
+		name = name .. acctName
 	end

 	--Add info tag link
 	name = name .. " " .. GuildCharInfo.generateLink(acctName)
-
 	return name
 end

@@ -255,4 +263,106 @@ GuildCharInfo.linkHandler = function(_, _, _, _, linkType, varargs)
 	return true
 end

+local function n0(val) if val == nil then return 0 else return val end end
+
+--[[
+    Change a 6-digit hex string into RGB values
+    @param hex - 6-digit hex string in range 000000 to FFFFFF
+        Note: if not length 6, wil return NIL values
+    @return 3 values: red, green, blue
+        All format decimal of range [0,1]
+ ]]
+GuildCharInfo.hex2rgb = function(hex)
+    --Return nil on invalid input
+    if #hex ~= 6 then return nil, nil, nil end
+    --Convert each two digits to a number
+    local r= GuildCharInfo.hex2dec(string.sub(hex, 1, 2))
+    local g= GuildCharInfo.hex2dec(string.sub(hex, 3, 2))
+    local b= GuildCharInfo.hex2dec(string.sub(hex, 5, 2))
+    return n0(r)/255,n0(g)/255,n0(b)/255
+end
+
+--[[
+    Change (r, g, b) into a 6-digit hex format string
+    @param r - red channel intensity. Integer in [0, 255]
+    @param g - green channel intensity. Integer in [0, 255]
+    @param b - blue channel intensity. Integer in [0, 255]
+    @return 6-digit hex string in range 000000 to FFFFFF
+ ]]
+GuildCharInfo.rgb2hex = function(r, g, b)
+    --Clip bounds
+    if r > 1 then r = 1 end
+    if g > 1 then g = 1 end
+    if b > 1 then b = 1 end
+    if r < 0 then r = 0 end
+    if g < 0 then g = 0 end
+    if b < 0 then b = 0 end
+
+    return string.format("%X%X%X",math.floor(r*255),math.floor(g*255),math.floor(b*255))
+end
+
+--Quick hex2dec taking advantage of the fact tonumber recognizes hex input
+GuildCharInfo.hex2dec = function(dd)
+    dd = "0x" .. dd
+    return tonumber(dd)
+end
+
+--[[
+    Init function to create UI configuration menu and managed config variables
+    @post - UI menu created
+    @post - preferences created and saved
+ ]]
+GuildCharInfo.initUI = function()
+    --Defaults
+    local cfgDefault = {}
+    cfgDefault.showMain = true
+    cfgDefault.showAcct = false
+    cfgDefault.showLink = true
+    cfgDefault.linkText = "[*]"
+    cfgDefault.linkColor = "EEEEEE"
+    GuildCharInfo.cfg = ZO_SavedVars:NewAccountWide('GuildCharacterInfo', 0.9, "config", cfgDefault)
+    --
+    --Create menu panel
+    --
+    local LAM = LibStub("LibAddonMenu-1.0")
+    local pre = "GuildCharInfoUI"
+    GuildCharInfo.panelID = LAM:CreateControlPanel("GuildCharNamesUI", "Guild Character Info")
+    --Name formatting options
+    LAM:AddHeader(GuildCharInfo.panelID, pre.."h1", "Name Format")
+    LAM:AddCheckbox(GuildCharInfo.panelID, pre.."h1c1", "Account Name",
+        "Show the account name (@name) in guild chat after the character name",
+        function() return GuildCharInfo.cfg.showAcct end,
+        function(val) GuildCharInfo.cfg.showAcct = val end,
+        false --No warning
+    )
+    LAM:AddCheckbox(GuildCharInfo.panelID, pre.."h1c2", "Main Character Name",
+        "Show the name of the highest character in guild chat after current character name",
+        function() return GuildCharInfo.cfg.showMain end,
+        function(val) GuildCharInfo.cfg.showMain = val end,
+        false --No warning
+    )
+    --Link options
+    LAM:AddHeader(GuildCharInfo.panelID, pre.."h2", "Info Link Format")
+    LAM:AddCheckbox(GuildCharInfo.panelID, pre.."h2c1", "Show Link",
+        "Show the info link after the name",
+        function() return GuildCharInfo.cfg.showLink end,
+        function(val) GuildCharInfo.cfg.showLink = val end,
+        true, "If disabled, there is no way to view the player info card"
+    )
+    LAM:AddColorPicker(GuildCharInfo.panelID, pre.."h2c2", "Link Color",
+        "Change the color of the text link (default [*]) that shows the info card",
+        function() return GuildCharInfo.hex2rgb(GuildCharInfo.cfg.linkColor) end,
+        function(r,g,b) GuildCharInfo.cfg.linkColor = GuildCharInfo.rgb2hex(r,g,b) end,
+        false -- No warning
+    )
+    LAM:AddEditBox(GuildCharInfo.panelID, pre.."h2c3", "Link text",
+        "Text shown after character name which on click shows the info box",
+        false, -- Not multiline
+        function() return GuildCharInfo.cfg.linkText end,
+        function(val) GuildCharInfo.cfg.linkText = val end,
+        true, "If using pChat, do NOT include a '^' in the link text"
+    )
+end
+
+
 EVENT_MANAGER:RegisterForEvent("GuildCharacterInfo", EVENT_PLAYER_ACTIVATED, GuildCharInfo.init)