Update shadow text to support colour names.

Mere [07-13-13 - 14:19]
Update shadow text to support colour names.

This allow callers to pass the colour name to track, and update the colours when they're changed.

Break out the code to replace an event to track to Utils.lua, as it's needed in a couple of places.

All health, mana, energy and power updates are done using the new event system.
Filename
HealingBar.lua
HealingPanel.lua
RaidManagement.lua
RiftAddon.toc
UI.ShadowedText.lua
Utils.lua
diff --git a/HealingBar.lua b/HealingBar.lua
index 2dc1a83..53857a3 100644
--- a/HealingBar.lua
+++ b/HealingBar.lua
@@ -46,7 +46,6 @@ function MereHealingFrames.HealingBar:new (settings)

     this.updateValueFunction = function(handle, value) MereHealingFrames.HealingBar.UpdateValue(this, value) end
     this.updateMaxValueFunction = function(handle, value) MereHealingFrames.HealingBar.UpdateMax(this, value) end
-
     this.colourChangeFunction = function (handle, r, g, b, a) MereHealingFrames.HealingBar.ChangeColourRGBA(this, r, g, b, a) end
     return this
 end
@@ -87,8 +86,8 @@ function MereHealingFrames.HealingBar:FramesLoadedCallback(frames)

     self.Text:SetVisible(self.showText)
     self.Text:SetText("-/-(-%)")
-    self.Text:SetFontColor(MereHealingFrames.Colours.FetchRGB("HealthBarText"))
-    self.Text:SetShadowColor(MereHealingFrames.Colours.FetchRGB("HealthBarTextShadow"))
+    self.Text:SetTextColourName("HealthBarText")
+    self.Text:SetShadowColourName("HealthBarTextShadow")
     self.Text:SetLayer(MereHealingFrames.Layers.CalculateLayer(self.LayoutOffset, "HealingBarText"))
     self.Text:SetPoint(self.TextAnchor, self.Frame, self.TextAnchor, 0, 3)
     self.Text:ShadowedText(self.borderedText)
@@ -183,22 +182,8 @@ function MereHealingFrames.HealingBar:UpdateBar(value, maxValue)
 end

 function MereHealingFrames.HealingBar:UpdateEvents(currentEvent, maxEvent)
-    self:ReplaceEventHandler("valueChangeEvent", currentEvent, self.updateValueFunction)
-    self:ReplaceEventHandler("maxValueChangeEvent", maxEvent, self.updateMaxValueFunction)
-end
-
-function MereHealingFrames.HealingBar:ReplaceEventHandler(eventHandlerName, newEvent, updateFunction)
-    local eventHandle = self[eventHandlerName]
-
-    if (eventHandle) then
-        Command.Event.Detach(eventHandle, updateFunction, self.healingBarName)
-    end
-
-    self[eventHandlerName] = newEvent
-
-    if newEvent then
-        Command.Event.Attach(newEvent, updateFunction, self.healingBarName)
-    end
+    MereHealingFrames.Utils.ReplaceEventHandler(self, "valueChangeEvent", currentEvent, self.updateValueFunction, self.healingBarName)
+    MereHealingFrames.Utils.ReplaceEventHandler(self, "maxValueChangeEvent", maxEvent, self.updateMaxValueFunction, self.healingBarName)
 end

 function MereHealingFrames.HealingBar:UpdateValue(value)
@@ -316,19 +301,16 @@ function MereHealingFrames.HealingBar:SetVisible(isVisible)
     end
 end

-function MereHealingFrames.HealingBar:ChangeColour(colour)
-    if not self.framesLoaded then return end
-	self.Bar:SetBackgroundColor(colour.r, colour.g, colour.b, colour.a)
-end
-
 function MereHealingFrames.HealingBar:ChangeColourRGBA(r, g, b, a)
     if not self.framesLoaded then return end
     self.Bar:SetBackgroundColor(r, g, b, a)
 end

 function MereHealingFrames.HealingBar:ChangeColourSource(colourName)
-    self:ChangeColour(MereHealingFrames.Colours.FetchColour(colourName))
-    self:ReplaceEventHandler("colourChangeEvent", Event.MereHealingFrames.Colours[colourName], self.colourChangeFunction)
+    MereHealingFrames.Utils.ReplaceEventHandler(self, "colourChangeEvent", Event.MereHealingFrames.Colours[colourName], self.colourChangeFunction, self.healingBarName)
+
+    if not self.framesLoaded then return end
+    self.Bar:SetBackgroundColor(MereHealingFrames.Colours.FetchRGBA(colourName))
 end

 function MereHealingFrames.HealingBar:SetBarMode(barMode)
@@ -340,20 +322,20 @@ function MereHealingFrames.HealingBar:SetBarMode(barMode)

     if barMode == "rogue" then
 		self:ChangeColourSource("BarRogue")
-        self.Text:SetFontColor(MereHealingFrames.Colours.FetchRGB("EnergyBarText"))
-        self.Text:SetShadowColor(MereHealingFrames.Colours.FetchRGB("EnergyBarTextShadow"))
+        self.Text:SetTextColourName("EnergyBarText")
+        self.Text:SetShadowColourName("EnergyBarTextShadow")
 	elseif barMode == "warrior" then
         self:ChangeColourSource("BarWarrior")
-		self.Text:SetFontColor(MereHealingFrames.Colours.FetchRGB("PowerBarText"))
-        self.Text:SetShadowColor(MereHealingFrames.Colours.FetchRGB("PowerBarTextShadow"))
+        self.Text:SetTextColourName("PowerBarText")
+        self.Text:SetShadowColourName("PowerBarTextShadow")
 	elseif barMode == "mana" then
         self:ChangeColourSource("BarMana")
-		self.Text:SetFontColor(MereHealingFrames.Colours.FetchRGB("ManaBarText"))
-        self.Text:SetShadowColor(MereHealingFrames.Colours.FetchRGB("ManaBarTextShadow"))
+		self.Text:SetTextColourName("ManaBarText")
+        self.Text:SetShadowColourName("ManaBarTextShadow")
 	elseif barMode == "health" then
 		self:UpdateHealthBarColour()
-        self.Text:SetFontColor(MereHealingFrames.Colours.FetchRGB("HealthBarText"))
-        self.Text:SetShadowColor(MereHealingFrames.Colours.FetchRGB("HealthBarTextShadow"))
+        self.Text:SetTextColourName("HealthBarText")
+        self.Text:SetShadowColourName("HealthBarTextShadow")
 	else
 		error("Unknown bar mode: " .. barMode)
     end
diff --git a/HealingPanel.lua b/HealingPanel.lua
index 8eb3153..9b282c0 100644
--- a/HealingPanel.lua
+++ b/HealingPanel.lua
@@ -250,6 +250,7 @@ function MereHealingFrames.HealingPanel:Calling()
 		if panelSettings.ShowMana then
 			self.ResourceBar:SetBarMode("mana")
 			self.ResourceBar:UpdateBar(playerInfo.mana, playerInfo.manaMax)
+            self.ResourceBar:UpdateEvents(playerInfo.PlayerEvents.Events.mana, playerInfo.PlayerEvents.Events.manaMax)
 			self.ResourceBar:SetVisible(true)
 			self.ResourceHeight = panelSettings.ManaHeight
 			self.ResourceBar:SetTextlayout(panelSettings.ManaTextFormatter, panelSettings.ManaTextRounding)
@@ -258,11 +259,13 @@ function MereHealingFrames.HealingPanel:Calling()
 		else
 			self.ResourceBar:SetVisible(false)
 			self.ResourceHeight = 0
+            self.ResourceBar:UpdateEvents(nil, nil)
 		end
 	elseif (playerInfo.calling == "rogue") then
 		if panelSettings.ShowEnergy then
 			self.ResourceBar:SetBarMode(playerInfo.calling)
 			self.ResourceBar:UpdateBar(playerInfo.energy, playerInfo.energyMax)
+            self.ResourceBar:UpdateEvents(playerInfo.PlayerEvents.Events.energy, playerInfo.PlayerEvents.Events.energyMax)
 			self.ResourceBar:SetVisible(true)
 			self.ResourceHeight = panelSettings.EnergyHeight
 			self.ResourceBar:SetTextlayout(panelSettings.EnergyTextFormatter, panelSettings.EnergyTextRounding)
@@ -271,11 +274,13 @@ function MereHealingFrames.HealingPanel:Calling()
 		else
 			self.ResourceBar:SetVisible(false)
 			self.ResourceHeight = 0
+            self.ResourceBar:UpdateEvents(nil, nil)
 		end
 	elseif (playerInfo.calling == "warrior") then
 		if panelSettings.ShowEnergy then
 			self.ResourceBar:SetBarMode(playerInfo.calling)
 			self.ResourceBar:UpdateBar(playerInfo.power, 100) -- no unitDetails.powerMax
+            self.ResourceBar:UpdateEvents(playerInfo.PlayerEvents.Events.power, nil)
 			self.ResourceBar:SetVisible(true)
 			self.ResourceHeight = panelSettings.EnergyHeight
 			self.ResourceBar:SetTextlayout(panelSettings.EnergyTextFormatter, panelSettings.EnergyTextRounding)
@@ -284,6 +289,7 @@ function MereHealingFrames.HealingPanel:Calling()
 		else
 			self.ResourceBar:SetVisible(false)
 			self.ResourceHeight = 0
+            self.ResourceBar:UpdateEvents(nil, nil)
 		end
 	else
 		error("Unknown calling: " .. playerInfo.calling)
@@ -300,18 +306,18 @@ function MereHealingFrames.HealingPanel:Name()

 	if (panelSettings.callingColouredText) then
 		if playerInfo.calling == "unknown" then
-			self.NameLabel:SetFontColor(1,1,1,1)
+			self.NameLabel:SetTextColourName("UnknownColour")
 		elseif playerInfo.calling == "warrior" then
-			self.NameLabel:SetFontColor(MereHealingFrames.Colours.FetchRGBA("WarriorColour"))
+            self.NameLabel:SetTextColourName("WarriorColour")
 		elseif playerInfo.calling == "cleric" then
-			self.NameLabel:SetFontColor(MereHealingFrames.Colours.FetchRGBA("ClericColour"))
+            self.NameLabel:SetTextColourName("ClericColour")
 		elseif playerInfo.calling == "mage" then
-			self.NameLabel:SetFontColor(MereHealingFrames.Colours.FetchRGBA("MageColour"))
+            self.NameLabel:SetTextColourName("MageColour")
 		elseif playerInfo.calling == "rogue" then
-			self.NameLabel:SetFontColor(MereHealingFrames.Colours.FetchRGBA("RogueColour"))
+            self.NameLabel:SetTextColourName("RogueColour")
 		end
 	else
-		self.NameLabel:SetFontColor(1,1,1,1)
+        self.NameLabel:SetTextColourName("UnknownColour")
 	end

 	if panelSettings.borderPlayerName then
@@ -548,30 +554,6 @@ function MereHealingFrames.HealingPanel:ResizeBars()
 	self.NameLabel:SetWidth(width - 16 - (self.PanelSettings.borderSize * 2))
 end

-function MereHealingFrames.HealingPanel:UpdateHealth(value)
-	--return self.Health:UpdateValue(value)
-end
-
-function MereHealingFrames.HealingPanel:UpdateHealthMax(value)
-	--return self.Health:UpdateMax(value)
-end
-
-function MereHealingFrames.HealingPanel:UpdateMana(value)
-	return self.ResourceBar:UpdateValue(value)
-end
-
-function MereHealingFrames.HealingPanel:UpdateManaMax(value)
-	return self.ResourceBar:UpdateMax(value)
-end
-
-function MereHealingFrames.HealingPanel:UpdateEnergy(value)
-	return self.ResourceBar:UpdateValue(value)
-end
-
-function MereHealingFrames.HealingPanel:UpdateEnergyMax(value)
-	return self.ResourceBar:UpdateMax(value)
-end
-
 function MereHealingFrames.HealingPanel:UpdateAbsorb(value)
     --self.ResourceBar:UpdateBar(nil, value)
     if (value and self.PanelSettings.absorbShown) then
diff --git a/RaidManagement.lua b/RaidManagement.lua
index de8a657..fb7ed27 100644
--- a/RaidManagement.lua
+++ b/RaidManagement.lua
@@ -142,6 +142,7 @@ function playerInfo:delete()
 end

 local function PanelUpdate(unitId, targetFunction, value)
+    if not targetFunction then return end
 	local panels = MereHealingFrames.UnitIdPanels[unitId] or {}
 	for i, panel in ipairs(panels) do
 		targetFunction(panel, value)
@@ -155,13 +156,13 @@ local function RelayLayouts()
 end

 local eventLookups =
-	{	health 		= MereHealingFrames.HealingPanel.UpdateHealth,
-		healthMax	= MereHealingFrames.HealingPanel.UpdateHealthMax,
-		mana		= MereHealingFrames.HealingPanel.UpdateMana,
-		manaMax 	= MereHealingFrames.HealingPanel.UpdateManaMax,
-		energy 		= MereHealingFrames.HealingPanel.UpdateEnergy,
-		energyMax	= MereHealingFrames.HealingPanel.UpdateEnergyMax,
-		power 		= MereHealingFrames.HealingPanel.UpdateEnergy, -- change to power...
+	{	health 		= false,
+		healthMax	= false,
+		mana		= false,
+		manaMax 	= false,
+		energy 		= false,
+		energyMax	= false,
+		power 		= false, -- change to power...
 		role 		= MereHealingFrames.HealingPanel.SetRole,
 		ready 		= MereHealingFrames.HealingPanel.Ready,
 		aggro 		= MereHealingFrames.HealingPanel.StatusChange,
@@ -173,7 +174,7 @@ local eventLookups =
 		calling		= MereHealingFrames.HealingPanel.Calling,	-- not currently triggered by an event
         absorb      = MereHealingFrames.HealingPanel.UpdateAbsorb,
 	}
-
+
 function playerInfo:UpdateValue(field, value)
 	self[field] = value
     self.PlayerEvents.Functions[field](value)
diff --git a/RiftAddon.toc b/RiftAddon.toc
index 43ba4c6..686dbd5 100644
--- a/RiftAddon.toc
+++ b/RiftAddon.toc
@@ -14,6 +14,7 @@ Environment = "2.3"
 RunOnStartup = {
 	"Debug.lua",
 	"StaticGlobals.lua",
+	"Utils.lua",
 	"PlayerEvents/EventDB.lua",
 	"PlayerEvents/EventManager.lua",
 	"UI.ShadowedText.lua",
diff --git a/UI.ShadowedText.lua b/UI.ShadowedText.lua
index cafe79c..ce05f1f 100644
--- a/UI.ShadowedText.lua
+++ b/UI.ShadowedText.lua
@@ -26,10 +26,16 @@ MereHealingFrames.UI.Widget.ShadowedText = {
 	ShadowB = 0,
     TextWidth = nil,
     TextHeight = nil,
+    TextColourEvent = nil,
+    ShadowColourEvent = nil,
+    TextColourFunc = nil,
+    ShadowColourFunc = nil,
 	}

 local ShadowedText = MereHealingFrames.UI.Widget.ShadowedText

+local ShadowedTextId = 1
+
 function ShadowedText:new(widgetName, parentFrame)
 	local this = {}

@@ -37,6 +43,12 @@ function ShadowedText:new(widgetName, parentFrame)
 	self.__index = self

 	this:CreateUI(widgetName, parentFrame)
+    this.ShadowTextId = "ShadowText"..ShadowedTextId
+    ShadowedTextId = ShadowedTextId + 1
+
+    this.TextColourFunc = function(handler, r, g, b, a) this:SetFontColor(r, g, b) end
+    this.ShadowColourFunc = function(handler, r, g, b, a) this:SetShadowColor(r, g, b) end
+
 	return this
 end

@@ -111,6 +123,17 @@ function ShadowedText:ShadowedText(Shadowed)
 	end
 end

+function ShadowedText:SetTextColourName(colourName)
+    self:SetFontColor(MereHealingFrames.Colours.FetchRGB(colourName))
+    MereHealingFrames.Utils.ReplaceEventHandler(self, "TextColourEvent", Event.MereHealingFrames.Colours[colourName], self.TextColourFunc, self.ShadowTextId)
+
+end
+
+function ShadowedText:SetShadowColourName(colourName)
+    self:SetShadowColor(MereHealingFrames.Colours.FetchRGB(colourName))
+    MereHealingFrames.Utils.ReplaceEventHandler(self, "ShadowColourEvent", Event.MereHealingFrames.Colours[colourName], self.ShadowColourFunc, self.ShadowTextId)
+end
+
 function ShadowedText:SetFontColor(r, g, b)
 	self.TextR = r
 	self.TextG = g
diff --git a/Utils.lua b/Utils.lua
new file mode 100644
index 0000000..786580c
--- /dev/null
+++ b/Utils.lua
@@ -0,0 +1,20 @@
+-- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
+-- To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
+-- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
+
+local MereHealingFrames, privateVars = ...
+MereHealingFrames.Utils = {}
+
+function MereHealingFrames.Utils.ReplaceEventHandler(table, eventHandlerName, newEvent, updateFunction, name)
+    local eventHandle = table[eventHandlerName]
+
+    if (eventHandle) then
+        Command.Event.Detach(eventHandle, updateFunction, name)
+    end
+
+    table[eventHandlerName] = newEvent
+
+    if newEvent then
+        Command.Event.Attach(newEvent, updateFunction, name)
+    end
+end