Quantcast

Allow users to save custom widgets.

Scott Sibley [09-14-10 - 03:00]
Allow users to save custom widgets.
Filename
Modules/Bars.lua
Modules/Histograms.lua
Modules/Text.lua
Modules/UnitTooltip.lua
StarTip.lua
diff --git a/Modules/Bars.lua b/Modules/Bars.lua
index 3893153..f32a386 100644
--- a/Modules/Bars.lua
+++ b/Modules/Bars.lua
@@ -1,7 +1,7 @@
 local mod = StarTip:NewModule("Bars", "AceTimer-3.0")
 mod.name = "Bars"
 mod.toggled = true
-mod.childGroup = true
+--mod.childGroup = true
 local _G = _G
 local StarTip = _G.StarTip
 local GameTooltip = _G.GameTooltip
@@ -52,7 +52,8 @@ local function copy(tbl)
 end

 local defaultWidgets = {
-	["Health Bar"] = {
+	[1] = {
+		name = "Health Bar",
 		type = "bar",
 		expression = [[
 return UnitHealth(unit)
@@ -73,7 +74,8 @@ end
 		enabled = true,
 		layer = 1
 	},
-	["Mana Bar"] = {
+	[2] = {
+		name = "Mana Bar",
 		type = "bar",
 		expression = [[
 if not UnitExists(unit) then return end
@@ -209,7 +211,7 @@ function createBars()
 			local widget = mod.bars[v]
 			if not widget then
 				local bar = new()
-				widget = mod.bars[v] or WidgetBar:New(mod.core, k, v, v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateBar, bar)
+				widget = mod.bars[v] or WidgetBar:New(mod.core, v.name, v, v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateBar, bar)
 				bar:SetStatusBarTexture(LSM:Fetch("statusbar", v.texture1))
 				bar:ClearAllPoints()
 				local arg1, arg2, arg3, arg4, arg5 = unpack(v.point)
@@ -232,7 +234,7 @@ function createBars()

 				if v.expression2 then
 					bar = new()
-					widget = WidgetBar:New(mod.core, k, copy(v), v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateBar, bar)
+					widget = WidgetBar:New(mod.core, v.name, v, v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateBar, bar)
 					bar:SetStatusBarTexture(LSM:Fetch("statusbar", v.texture2 or v.texutre1 or "Blizzard"))
 					bar:ClearAllPoints()
 					local arg1, arg2, arg3, arg4, arg5 = unpack(v.point)
@@ -268,46 +270,32 @@ function mod:OnInitialize()
 		self.db.profile.bars = {}
 	end

-	for i, v in ipairs(defaultWidgets) do
-		for j, vv in ipairs(self.db.profile.lines) do
-			if v.name == vv.name then
-				for k, val in pairs(v) do
-					if v[k] ~= vv[k] and not vv[k.."Dirty"] then
-						vv[k] = v[k]
-					end
-				end
-				v.tagged = true
-			end
-		end
-	end
-
-	for i, v in ipairs(defaultWidgets) do
-		if not v.tagged and not v.deleted then
-			tinsert(self.db.profile.bars, v)
+	for k in pairs(self.db.profile.bars) do
+		if type(k) == "string" then
+			wipe(self.db.profile.bars)
+			break
 		end
 	end

-	self.db.profile.bars = copy(defaultWidgets)
---[[
 	for k, v in pairs(defaultWidgets) do
-		for kk, vv in pairs(self.db.profile.bars) do
+		for j, vv in ipairs(self.db.profile.bars) do
 			if v.name == vv.name then
 				for k, val in pairs(v) do
 					if v[k] ~= vv[k] and not vv[k.."Dirty"] then
-						vv[k] = copy(v[k])
+						vv[k] = v[k]
 					end
 				end
 				v.tagged = true
 			end
 		end
 	end
-]]
+
 	for k, v in pairs(defaultWidgets) do
 		if not v.tagged and not v.deleted then
 			self.db.profile.bars[k] = copy(v)
 		end
 	end
-
+
 	self.core = LibCore:New(mod, environment, "StarTip.Bars", {["StarTip.Bars"] = {}}, nil, StarTip.db.profile.errorLevel)

 	StarTip:SetOptionsDisabled(options, true)
@@ -406,11 +394,11 @@ end
 function mod:RebuildOpts()
 	local defaults = WidgetBar.defaults

-	for k, db in pairs(self.db.profile.bars) do
-		options.bars.args[k:gsub(" ", "_")] = {
-			name = k,
+	for i, db in ipairs(self.db.profile.bars) do
+		options.bars.args[db.name:gsub(" ", "_")] = {
+			name = db.name,
 			type="group",
-			order = 6,
+			order = i,
 			args={
 				enabled = {
 					name = "Enabled",
diff --git a/Modules/Histograms.lua b/Modules/Histograms.lua
index d9aa040..da10485 100644
--- a/Modules/Histograms.lua
+++ b/Modules/Histograms.lua
@@ -1,7 +1,7 @@
 local mod = StarTip:NewModule("Histograms", "AceTimer-3.0")
 mod.name = "Histograms"
 mod.toggled = true
-mod.childGroup = true
+--mod.childGroup = true
 mod.defaultOff = true
 local _G = _G
 local StarTip = _G.StarTip
@@ -53,7 +53,38 @@ local function copy(tbl)
 end

 local defaultWidgets = {
-	["widget_mem_histogram"] = {
+	[1] = {
+		name = "Health",
+		expression = "return UnitHealth(unit)",
+		min = "return 0",
+		max = "return UnitHealthMax(unit)",
+		enabled = true,
+		width = 10,
+		height = 50,
+		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, -12},
+		color = [[
+return HPColor(UnitHealth(unit), UnitHealthMax(unit))
+]],
+		layer = 1,
+		update = 1000
+	},
+	[2] = {
+		name = "Power",
+		expression = "return UnitMana(unit)",
+		min = "return 0",
+		max = "return UnitManaMax(unit)",
+		enabled = true,
+		width = 10,
+		height = 50,
+		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", -100, -12},
+		color = [[
+return PowerColor("RAGE", unit)
+]],
+		layer = 1,
+		update = 1000
+	},
+	[3] = {
+		name = "Mem",
 		type = "histogram",
 		expression = [[
 mem, percent, memdiff, totalMem, totaldiff = GetMemUsage("StarTip")
@@ -83,11 +114,13 @@ end
 		char = "0",
 		width = 10,
 		height = 50,
-		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, -65},
+		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, -77},
 		layer = 1,
-		update = 1000
+		update = 1000,
+		persistent = true
 	},
-	["widget_cpu_histogram"] = {
+	[4] = {
+		name = "CPU",
 		type = "histogram",
 		expression = [[
 local cpu, percent, cpudiff, totalCPU, totaldiff = GetCPUUsage("StarTip")
@@ -116,38 +149,12 @@ end
 		char = "0",
 		width = 10,
 		height = 50,
-		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", -100, -65},
-		layer = 1,
-		update = 1000
-	},
-	["widget_health_histogram"] = {
-		expression = "return UnitHealth(unit)",
-		min = "return 0",
-		max = "return UnitHealthMax(unit)",
-		enabled = true,
-		width = 10,
-		height = 50,
-		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, -12},
-		color = [[
-return HPColor(UnitHealth(unit), UnitHealthMax(unit))
-]],
+		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", -100, -77},
 		layer = 1,
-		update = 1000
+		update = 1000,
+		persistent = true
 	},
-	["widget_mana_histogram"] = {
-		expression = "return UnitMana(unit)",
-		min = "return 0",
-		max = "return UnitManaMax(unit)",
-		enabled = true,
-		width = 10,
-		height = 50,
-		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", -100, -12},
-		color = [[
-return PowerColor("RAGE", unit)
-]],
-		layer = 1,
-		update = 1000
-	}
+
 }

 local defaults = {
@@ -275,7 +282,8 @@ local function createHistograms()
 			local newWidget
 			if not mod.histograms then mod.histograms = {} end
 			if not widget then
-				widget = WidgetHistogram:New(mod.core, k, v, v.row or 0, v.col or 0, 0, StarTip.db.profile.errorLevel, updateHistogram)
+				widget = WidgetHistogram:New(mod.core, v.name, v, v.row or 0, v.col or 0, 0, StarTip.db.profile.errorLevel, updateHistogram)
+				widget.persistent = v.persistent
 				newWidget = true
 				for i = 0, v.width - 1 do
 					local bar = new()
@@ -321,8 +329,15 @@ function mod:OnInitialize()
 		self.db.profile.histograms = {}
 	end

-	self.db.profile.histograms = copy(defaultWidgets)
-
+	wipe(self.db.profile.histograms)
+
+	for k in pairs(self.db.profile.histograms) do
+		if type(k) == "string" then
+			wipe(self.db.profile.histograms)
+			break
+		end
+	end
+
 	for i, v in ipairs(defaultWidgets) do
 		for j, vv in ipairs(self.db.profile.histograms) do
 			if v.name == vv.name then
@@ -338,7 +353,7 @@ function mod:OnInitialize()

 	for i, v in ipairs(defaultWidgets) do
 		if not v.tagged and not v.deleted then
-			tinsert(self.db.profile.histograms, v)
+			tinsert(self.db.profile.histograms, copy(v))
 		end
 	end

@@ -395,7 +410,9 @@ function mod:SetItem()
 		for i = 1, widget.width or WidgetHistogram.defaults.width do
 			widget.bars[i]:Hide()
 		end
-		widget:Stop()
+		if not widget.persistent then
+			widget:Stop()
+		end
 	end
 end

@@ -404,7 +421,9 @@ function mod:SetSpell()
 		for i = 1, widget.width or WidgetHistogram.defaults.width do
 			widget.bars[i]:Hide()
 		end
-		widget:Stop()
+		if not widget.persistent then
+			widget:Stop()
+		end
 	end
 end

@@ -434,11 +453,11 @@ end
 function mod:RebuildOpts()
 	local defaults = WidgetHistogram.defaults

-	for k, db in pairs(self.db.profile.histograms) do
-		options.histograms.args[k:gsub(" ", "_")] = {
-			name = k,
+	for i, db in ipairs(self.db.profile.histograms) do
+		options.histograms.args[db.name:gsub(" ", "_")] = {
+			name = db.name,
 			type="group",
-			order = 6,
+			order = i,
 			args={
 				enabled = {
 					name = "Enable",
@@ -583,6 +602,14 @@ function mod:RebuildOpts()
 					},
 					order = 7
 				},
+				persistent = {
+					name = "Persistent",
+					desc = "Whether this histogram is persistent or not, meaning it won't stop when the tooltip hides.",
+					type = "toggle",
+					get = function() return db.persistent end,
+					set = function(info, v) db.persistent = v end,
+					order = 8
+				},
 				expression = {
 					name = "Histogram expression",
 					desc = "Enter the histogram's first expression",
@@ -596,7 +623,7 @@ function mod:RebuildOpts()
 						clearHistogram(db)
 						createHistograms()
 					end,
-					order = 8
+					order = 9
 				},
 				min = {
 					name = "Histogram min expression",
diff --git a/Modules/Text.lua b/Modules/Text.lua
index beeac1b..7a48e19 100644
--- a/Modules/Text.lua
+++ b/Modules/Text.lua
@@ -1,7 +1,7 @@
 local mod = StarTip:NewModule("Text", "AceTimer-3.0")
 mod.name = "Text"
 mod.toggled = true
-mod.childGroup = true
+--mod.childGroup = true
 mod.defaultOff = true
 local _G = _G
 local StarTip = _G.StarTip
@@ -52,7 +52,8 @@ local function copy(tbl)
 end

 local defaultWidgets = {
-	["Name"] = {
+	[1] = {
+		name = "Name",
 		enabled = true,
 		value = [[
 if not UnitExists(unit) then return end
@@ -71,7 +72,8 @@ return ClassColor(unit)
 		point = {"BOTTOMLEFT", "GameTooltip", "TOPLEFT", 0, 12},
 		parent = "GameTooltip",
 	},
-	["Health"] = {
+	[2] = {
+		name = "Health",
 		enabled = true,
 		value = [[
 if not UnitExists(unit) then return end
@@ -90,7 +92,8 @@ return HPColor(health, max)
 		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, 1},
 		parent = "GameTooltip"
 	},
-	["Mana"] = {
+	[3] = {
+		name = "Power",
 		enabled = true,
 		value = [[
 if not UnitExists(unit) then return end
@@ -109,6 +112,74 @@ return HPColor(mana, max)
 		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", 0, 1},
 		parent = "GameTooltip"
 	},
+	[4] = {
+		name = "Memory",
+		enabled = false,
+		value = [[
+local mem, percent, memdiff, totalMem, totaldiff = GetMemUsage("StarTip")
+if mem then
+    if totaldiff == 0 then totaldiff = 1 end
+    memperc = (memdiff / totaldiff * 100)
+    local num = floor(memperc + 0.5)
+    if num < 1 then num = 1 end
+    if num > 100 then num = 100 end
+    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
+    return format("Mem: %.2f%%", memperc)
+end
+]],
+		color = [[
+local mem, percent, memdiff, totalMem, totaldiff = GetMemUsage("StarTip")
+if mem then
+    if totaldiff == 0 then totaldiff = 1 end
+    memperc = (memdiff / totaldiff * 100)
+    local num = floor(memperc + 0.5)
+    if num < 1 then num = 1 end
+    if num > 100 then num = 100 end
+    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
+    return r, g, b
+end
+
+]],
+		cols = 20,
+		update = 1000,
+		dontRtrim = true,
+		point = {"TOPLEFT", "GameTooltip", "BOTTOMLEFT", 0, -62},
+		parent = "GameTooltip"
+	},
+	[5] = {
+		name = "CPU",
+		enabled = false,
+		value = [[
+local cpu, percent, cpudiff, totalCPU, totaldiff = GetCPUUsage("StarTip")
+if cpu then
+    if totaldiff == 0 then totaldiff = 100 end
+    cpuperc = cpudiff / totaldiff * 100;
+    local num = floor(cpuperc + 0.5)
+    if num < 1 then num = 1 end
+    if num > 100 then num = 100 end
+    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
+    return format("CPU: %.2f%%", cpuperc)
+end
+]],
+		color = [[
+local cpu, percent, cpudiff, totalCPU, totaldiff = GetCPUUsage("StarTip")
+if cpu then
+    if totaldiff == 0 then totaldiff = 1 end
+    cpuperc = (cpudiff / totaldiff * 100)
+    local num = floor(cpuperc)
+    if num < 1 then num = 1 end
+    if num > 100 then num = 100 end
+    local r, g, b = gradient[num][1], gradient[num][2], gradient[num][3]
+    return r, g, b
+end
+]],
+		cols = 20,
+		align = WidgetText.ALIGN_RIGHT,
+		update = 1000,
+		dontRtrim = true,
+		point = {"TOPRIGHT", "GameTooltip", "BOTTOMRIGHT", 0, -62},
+		parent = "GameTooltip"
+	},
 }

 local defaults = {
@@ -217,7 +288,7 @@ function createTexts()
 			local text = new(v.cols or WidgetText.defaults.cols)
 			local cfg = copy(v)
 			cfg.unit = StarTip.unit
-			local widget = mod.texts[v] or WidgetText:New(mod.core, k, cfg, v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateText)
+			local widget = mod.texts[v] or WidgetText:New(mod.core, v.name, cfg, v.row or 0, v.col or 0, v.layer or 0, StarTip.db.profile.errorLevel, updateText)
 			text:ClearAllPoints()
 			text:SetParent(v.parent)
 			local arg1, arg2, arg3, arg4, arg5 = unpack(v.point)
@@ -238,8 +309,15 @@ function mod:OnInitialize()
 		self.db.profile.texts = {}
 	end

+	for k in pairs(self.db.profile.texts) do
+		if type(k) == "string" then
+			wipe(self.db.profile.texts)
+			break
+		end
+	end
+
 	for i, v in ipairs(defaultWidgets) do
-		for j, vv in ipairs(self.db.profile.lines) do
+		for j, vv in ipairs(self.db.profile.texts) do
 			if v.name == vv.name then
 				for k, val in pairs(v) do
 					if v[k] ~= vv[k] and not vv[k.."Dirty"] then
@@ -253,28 +331,7 @@ function mod:OnInitialize()

 	for i, v in ipairs(defaultWidgets) do
 		if not v.tagged and not v.deleted then
-			tinsert(self.db.profile.texts, v)
-		end
-	end
-
-	self.db.profile.texts = copy(defaultWidgets)
---[[
-	for k, v in pairs(defaultWidgets) do
-		for kk, vv in pairs(self.db.profile.texts) do
-			if v.name == vv.name then
-				for k, val in pairs(v) do
-					if v[k] ~= vv[k] and not vv[k.."Dirty"] then
-						vv[k] = copy(v[k])
-					end
-				end
-				v.tagged = true
-			end
-		end
-	end
-]]
-	for k, v in pairs(defaultWidgets) do
-		if not v.tagged and not v.deleted then
-			self.db.profile.texts[k] = copy(v)
+			tinsert(self.db.profile.texts, copy(v))
 		end
 	end

@@ -359,11 +416,11 @@ end
 function mod:RebuildOpts()
 	local defaults = WidgetText.defaults

-	for k, db in pairs(self.db.profile.texts) do
-		options.texts.args[k:gsub(" ", "_")] = {
-			name = k,
+	for i, db in ipairs(self.db.profile.texts) do
+		options.texts.args[db.name:gsub(" ", "_")] = {
+			name = db.name,
 			type="group",
-			order = 6,
+			order = i,
 			args=WidgetText:GetOptions(StarTip, db)
 		}
 	end
diff --git a/Modules/UnitTooltip.lua b/Modules/UnitTooltip.lua
index e607490..f253294 100644
--- a/Modules/UnitTooltip.lua
+++ b/Modules/UnitTooltip.lua
@@ -350,7 +350,7 @@ local options = {}
 function mod:OnInitialize()
     self.db = StarTip.db:RegisterNamespace(self:GetName(), defaults)

-	for i, v in ipairs(defaultLines) do
+	for k, v in ipairs(defaultLines) do
 		for j, vv in ipairs(self.db.profile.lines) do
 			vv.colorLeft = nil
 			vv.colorRight = nil
@@ -365,7 +365,7 @@ function mod:OnInitialize()
 		end
 	end

-	for i, v in ipairs(defaultLines) do
+	for k, v in ipairs(defaultLines) do
 		if not v.tagged and not v.deleted then
 			tinsert(self.db.profile.lines, v)
 		end
@@ -430,7 +430,6 @@ end

 local fontStringsToDraw = {}
 local function updateFontString(widget, fontString)
-	widget.fontString = fontString
 	fontStringsToDraw[widget] = true
 end

@@ -439,8 +438,8 @@ do
 	local widget, fontString
 	function draw()
 		for widget in pairs(fontStringsToDraw) do
-			fontString = widget.fontString
-			if not fontString then break end
+			if not widget.fontString then break end
+			local fontString = widget.fontString
 			fontString:SetText(widget.buffer)

 			font = LSM:Fetch("font", fontsList[appearance.db.profile.font])
@@ -507,11 +506,7 @@ function mod:CreateLines()
 								v.config.update = 0
 								v.config.scroll = 0
 							end
-							if v.leftObj then
-								v.leftObj.data = mod.leftLines[lineNum]
-							else
-								v.leftObj = WidgetText:New(mod.core, v.name .. "left", v.config, 0, 0, v.layer or 0, StarTip.db.profile.errorLevel, updateFontString, mod.leftLines[lineNum])
-							end
+							v.leftObj = v.leftObj or WidgetText:New(mod.core, v.name .. "left", v.config, 0, 0, v.layer or 0, StarTip.db.profile.errorLevel, updateFontString)
 							v.update = tmp
 						end

@@ -524,38 +519,26 @@ function mod:CreateLines()
 								v.config.update = 0
 								v.config.scroll = 0
 							end
-							if v.rightObj then
-								v.rightObj.data = mod.rightLines[lineNum]
-							else
-								v.rightObj = WidgetText:New(mod.core, v.name .. "right", v.config, 0, 0, v.layer or 0, StarTip.db.profile.errorLevel, updateFontString, mod.rightLines[lineNum])
-							end
+							v.rightObj = v.rightObj or WidgetText:New(mod.core, v.name .. "right", v.config, 0, 0, v.layer or 0, StarTip.db.profile.errorLevel, updateFontString)
 							v.update = tmp
 						end
-						v.leftObj.fontstring = mod.leftLines[lineNum]
+						v.leftObj.fontString = mod.leftLines[lineNum]
 						fontStringsToDraw[v.leftObj] = true
-						v.rightObj.fontstring = mod.rightLines[lineNum]
+						v.rightObj.fontString = mod.rightLines[lineNum]
 						fontStringsToDraw[v.rightObj] = true
                     else
 						GameTooltip:AddLine(' ', mod.db.profile.color.r, mod.db.profile.color.g, mod.db.profile.color.b)

-						if not v.leftObj or v.lineNum ~= lineNum then
-							--if v.leftObj then v.leftObj:Del() end
-							v.config.value = v.left
-							local tmp = v.update
-							if not v.leftUpdating then v.update = 0 end
-							if mod.db.profile.refreshRate == 0 then
-								v.config.update = 0
-								v.config.scroll = 0
-							end
-							if v.leftObj then
-								v.leftObj.data = mod.leftLines[lineNum]
-							else
-								v.leftObj = WidgetText:New(mod.core, v.name, v.config, 0, 0, 0, StarTip.db.profile.errorLevel, updateFontString, mod.leftLines[lineNum])
-							end
-							v.update = tmp
-							v.lineNum = lineNum
+						v.config.value = v.left
+						local tmp = v.update
+						if not v.leftUpdating then v.update = 0 end
+						if mod.db.profile.refreshRate == 0 then
+							v.config.update = 0
+							v.config.scroll = 0
 						end
-						v.leftObj.fontstring = mod.leftLines[lineNum]
+						v.leftObj = v.leftObj or WidgetText:New(mod.core, v.name, v.config, 0, 0, 0, StarTip.db.profile.errorLevel, updateFontString)
+						v.update = tmp
+						v.leftObj.fontString = mod.leftLines[lineNum]
 						fontStringsToDraw[v.leftObj] = true
                     end
 					if v.rightObj then
diff --git a/StarTip.lua b/StarTip.lua
index 9e226ba..c283ba3 100644
--- a/StarTip.lua
+++ b/StarTip.lua
@@ -336,6 +336,17 @@ do
 		pool[t] = true
 		delCount = delCount + 1
 	end
+	function StarTip.copy(src, dst)
+		if type(src) ~= "table" then return nil end
+		if type(dst) ~= "table" then dst = StarTip.new() end
+		for k, v in pairs(src) do
+			if type(v) == "table" then
+				v = StarTip.copy(v)
+			end
+			dst[k] = v
+		end
+		return dst
+	end
 end

 local environment = {}