changeset 86:703bbe61d12d

Proper class coloring in one place.
author Farmbuyer of US-Kilrogg <farmbuyer@gmail.com>
date Thu, 21 Jun 2012 21:14:24 +0000
parents 4771ee8eaa81
children 9fea75b0927b
files core.lua gui.lua
diffstat 2 files changed, 48 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/core.lua	Thu Jun 21 19:38:54 2012 +0000
+++ b/core.lua	Thu Jun 21 21:14:24 2012 +0000
@@ -152,7 +152,7 @@
 	..[[|cffffff00to see what can be done by software alone.  You may still]]
 	..[[ need to do a "/reload" afterwards, or even restart the game client.]]
 local unique_collision = "Item '%s' was carrying unique tag <%s>, but that was already in use; tried to generate a new tag and failed!|n|nRemote sender was '%s', previous cache entry was <%s/%s>.|n|nThis may require a live human to figure out; the loot in question has not been stored."
-local remote_chatty = "|cff00ff00%s|r changed %d/%s from %s%s|r to %s%s|r"
+local remote_chatty = "|cff00ff00%s|r changed %d/%s from %s to %s"
 local qualnames = {
 	['gray'] = 0, ['grey'] = 0, ['poor'] = 0, ['trash'] = 0,
 	['white'] = 1, ['common'] = 1,
@@ -316,6 +316,35 @@
 	end
 end
 
+-- Class color support.  Do the expensive string.format calls up front, and
+-- the cheap all-string-all-at-once single-op concatenation as needed.
+do
+	local cc = {}
+	local function extract (color_info)
+		local r = _G.math.floor(255*color_info.r+0.5)
+		local g = _G.math.floor(255*color_info.g+0.5)
+		local b = _G.math.floor(255*color_info.b+0.5)
+		local hex = ("|cff%.2x%.2x%.2x"):format(r,g,b)
+		return { r=r, g=g, b=b, a=1, hex=hex }
+	end
+	local function fill_out_class_colors()
+		for class,color in pairs(CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS) do
+			cc[class] = extract(color)
+		end
+		cc.DEFAULT = extract(_G.NORMAL_FONT_COLOR)
+	end
+	if CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS.RegisterCallback then
+		CUSTOM_CLASS_COLORS:RegisterCallback(fill_out_class_colors)
+	end
+	fill_out_class_colors()
+	addon.class_colors = cc
+	-- What I really want is to have the hooked :Print understand a special
+	-- format specifier like "%Cs" and do the colorizing automatically.
+	function addon:colorize (text, class)
+		return ((class and cc[class]) or cc.DEFAULT).hex .. text .. "|r"
+	end
+end
+
 
 ------ Globals
 local g_loot			= nil
@@ -1812,32 +1841,16 @@
 		end
 	end
 
-	local hexes = _G.setmetatable({}, {__index = function (t, k)
-		local r = _G.math.floor(255*k.r+0.5)
-		local g = _G.math.floor(255*k.g+0.5)
-		local b = _G.math.floor(255*k.b+0.5)
-		local hex = ("|cff%.2x%.2x%.2x"):format(r,g,b)
-		t[k] = hex
-		return hex
-	end})
 	function _notify_about_change (chatframe, source, index, olddisp, from_whom, from_class)
 		local e = g_loot[index]
 		if not e then
 			-- how did this happen?
 			return
 		end
-		local from_color, from_text, to_color, to_text
+		local from_text, to_text
 		if from_whom then
-			from_color = from_class
-				and addon.class_colors[from_class]
-				or _G.NORMAL_FONT_COLOR
-			from_color = hexes[from_color]
-			from_text = from_whom
-			to_color = e.person_class
-				and addon.class_colors[e.person_class]
-				or _G.NORMAL_FONT_COLOR
-			to_color = hexes[to_color]
-			to_text = e.person
+			from_text = addon:colorize (from_whom, from_class)
+			to_text = addon:colorize (e.person, e.person_class)
 		else
 			if olddisp then
 				from_text = addon.disposition_colors[olddisp].text
@@ -1845,19 +1858,22 @@
 				olddisp = "normal"
 				from_text = "normal"
 			end
-			from_color = addon.disposition_colors[olddisp].hex
+			from_text = addon.disposition_colors[olddisp].hex
+				.. from_text .. "|r"
+
 			if e.disposition then
 				to_text = addon.disposition_colors[e.disposition].text
 			else
 				to_text = "normal"
 			end
-			to_color = addon.disposition_colors[e.disposition or "normal"].hex
+			to_text = addon.disposition_colors[e.disposition or "normal"].hex
+				.. to_text .. "|r"
 		end
 
 		addon.dprint ('loot', "notification:", source, index,
-			e.itemlink, from_color, from_text, to_color, to_text)
+			e.itemlink, from_text, to_text)
 		addon:CFPrint (chatframe, remote_chatty, source, index,
-			e.itemlink, from_color, from_text, to_color, to_text)
+			e.itemlink, from_text, to_text)
 	end
 
 	function _notify_about_remote (sender, index, olddisp, from_whom, from_class)
--- a/gui.lua	Thu Jun 21 19:38:54 2012 +0000
+++ b/gui.lua	Thu Jun 21 21:14:24 2012 +0000
@@ -88,27 +88,6 @@
 -- Similarly for the popup tips on the right side of the window.
 local noob_tips = {}
 
--- Class color support
-local class_colors-- = {}
-do
-	local function fill_out_class_colors()
-		class_colors = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
-		-- If we were dependant on lib-st calling this function (via a
-		-- 'color' field in eoi_st_cols[2]), then this would have to be deep
-		-- copied and an "a=1" field added to each.  But as we have to use
-		-- this ourselves via DoCellUpdate, we can just share tables and
-		-- pass an alpha value manually during cell update.
-		--for class,color in pairs(CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS) do
-		--	class_colors[class] = { r = color.r, g = color.g, b = color.b, a = 1 }
-		--end
-	end
-	fill_out_class_colors()
-	if CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS.RegisterCallback then
-		CUSTOM_CLASS_COLORS:RegisterCallback(fill_out_class_colors)
-	end
-	addon.class_colors = class_colors
-end
-
 do
 	local replacement_colors = {
 		["+"]="|cffffffff",   -- white
@@ -448,6 +427,7 @@
 				local st_entry = new()
 				st_entry.kind = 'hist'
 				st_entry.OLwho = player.name
+				st_entry.OLclass = player.person_class
 				st_entry.cols = dotcols
 				st_entry.itemlink = ilink  -- for onenter and onclick
 				tinsert (st, st_entry)
@@ -856,8 +836,8 @@
 		if gone.kind == 'loot' and IsShiftKeyDown() then
 			local okay,err = addon:_delHistoryEntry (gone)
 			if okay then
-				addon:Print("Removed history entry %s from '%s'.",
-					gone.itemlink, gone.person)
+				addon:Print("Removed history entry %s from %s.",
+					gone.itemlink, addon:colorize(gone.person,gone.person_class))
 			else
 				addon:Print(err)
 			end
@@ -1237,7 +1217,7 @@
 		icon:SetTexCoord(unpack(CLASS_ICON_TCOORDS[e.person_class]))
 		icon:Show()
 		cellFrame.text:SetPoint("LEFT", icon, "RIGHT", 1, 0)
-		local color = class_colors[e.person_class]
+		local color = addon.class_colors[e.person_class]
 		cellFrame.text:SetTextColor(color.r,color.g,color.b,1)
 	else
 		if cellFrame.icontexture then
@@ -1527,8 +1507,8 @@
 		local h = _d:GetUserData("DD history entry")
 		local okay,err = addon:_delHistoryEntry (h.cols[2].OLu, h.itemlink)
 		if okay then
-			addon:Print("Removed history entry %s from '%s'.",
-				h.itemlink, h.OLwho)
+			addon:Print("Removed history entry %s from %s.",
+				h.itemlink, addon:colorize(h.OLwho,h.OLclass))
 		else
 			addon:Print(err)
 		end
@@ -1541,8 +1521,8 @@
 		local gone = tremove (addon.history, player_i)
 		assert(gone.name == name)
 		addon:_build_history_names()
-		addon:Print("Removed player '%s' from history (%d total entries).",
-			name, #gone.unique)
+		addon:Print("Removed player %s from history (%d total entries).",
+			addon:colorize(name,gone.person_class), #gone.unique)
 	end,
 }
 local hist_general_dropdown = gen_easymenu_table(