Quantcast

Gold, Repair, XP : Gold : Fix gold display when user selects . (period) as thousands separator.

urnati [07-22-24 - 19:58]
Gold, Repair, XP : Gold : Fix gold display when user selects . (period) as thousands separator.
- Added TitanUtils_NumToString for use in all 3.
- Added TitanUtils_CashToString for use in Gold and Repair for common look of gold/silver/copper.
Filename
Titan/TitanGlobal.lua
Titan/TitanHistory.lua
Titan/TitanUtils.lua
TitanGold/TitanGold.lua
TitanRegen/TitanRegen.lua
TitanRepair/TitanRepair.lua
TitanXP/TitanXP.lua
diff --git a/Titan/TitanGlobal.lua b/Titan/TitanGlobal.lua
index c91023d..2588389 100644
--- a/Titan/TitanGlobal.lua
+++ b/Titan/TitanGlobal.lua
@@ -166,6 +166,9 @@ Titan_Global.literals = {
 Titan_Global.colors = {
 	alliance = "00adf0", -- PLAYER_FACTION_COLOR_ALLIANCE
 	blue = "0000ff", -- PURE_BLUE_COLOR
+	coin_gold = "ffd100",
+	coin_silver = "e6e6e6",
+	coin_copper = "c8602c",
 	gold = "f2e699", -- GOLD_FONT_COLOR
 	gray = "808080", -- GRAY_FONT_COLOR
 	green = "19ff19", -- GREEN_FONT_COLOR
diff --git a/Titan/TitanHistory.lua b/Titan/TitanHistory.lua
index e1ad67c..54b8ac3 100644
--- a/Titan/TitanHistory.lua
+++ b/Titan/TitanHistory.lua
@@ -15,6 +15,15 @@ Green - 'header' - Titan or plugin
 Highlight - notes. tips. and details
 --]]
 Titan_Global.recent_changes = ""
+.. TitanUtils_GetGoldText("8.0.16 : 2024/07/22\n")
+.. TitanUtils_GetGreenText("Gold, Repair, XP : \n")
+.. TitanUtils_GetHighlightText(""
+.. "- Gold : Fix gold display when user selects . (period) as thousands separator.\n"
+.. "- Added TitanUtils_NumToString for use in all 3.\n"
+.. "- Added TitanUtils_CashToString for use in Gold and Repair for common look of gold/silver/copper.\n"
+)
+.. "\n\n"
+Titan_Global.recent_changes = ""
 .. TitanUtils_GetGoldText("8.0.15 : 2024/07/14\n")
 .. TitanUtils_GetGreenText("Location : \n")
 .. TitanUtils_GetHighlightText(""
diff --git a/Titan/TitanUtils.lua b/Titan/TitanUtils.lua
index b245ea1..409fe64 100644
--- a/Titan/TitanUtils.lua
+++ b/Titan/TitanUtils.lua
@@ -847,6 +847,205 @@ function TitanUtils_ToString(text)
 	return TitanUtils_Ternary(text, text, "");
 end

+---API Add separators into the value given.  This does not break coin into its parts.
+--- This routines handles negative and fractional numbers.
+--- Assumes amount decimal separator is a period per tostring().
+---@param amount number
+---@param thousands_separator string
+---@param decimal_separator string
+---@return string formatted
+function TitanUtils_NumToString(amount, thousands_separator, decimal_separator)
+	-- Jul 2024 Moved to Utils for use by plugins
+	--[=[ Jul 2024
+	Handle the general cases of converting any number to a string with separators for plugins.
+	Titan usage is , / . or . / , although this will handle other schemes.
+	NOTE: Currently only positive, whole numbers are passed in from Titan (no fractional or negative).
+	NOTE: If ampount is 100 trillion or more then return the string as is to avoid very messy strings.
+		This is the behavior of Lua tostring.
+	NOTE: Do not use separator directly in gsub - it could be a pattern special char, resulting in unexpected behavior!
+	--]=]
+
+	local formatted = ""
+
+	if type(amount) == "number" then
+		-- Break number into segments - minus, integer, and fractional
+		local i, j, minus, int, fraction = 0, 0, "", "", ""
+		if amount > 99999999999999 then -- 1 trillion - 1
+			int = tostring(amount)
+			-- leave as is and, if gold, congratulate the player!!!
+			-- Result will be have an exponent (1.23+e16)
+		else
+			i, j, minus, int, fraction = tostring(amount):find('([-]?)(%d+)([.]?%d*)')
+
+			-- Reverse the int-string and append a separator to all blocks of 3 digits
+			int = int:reverse():gsub("(%d%d%d)", "%1|")
+
+			-- Reverse the int-string back and remove an extraneous separator
+			int = int:reverse():gsub("^|", "")
+
+			-- Now use the given decimal separator.
+			-- tostring outputs a period as the separator so it needs to be escaped.
+			int = int:gsub("%.", decimal_separator)
+
+			-- Now use the given thousands separator
+			int = int:gsub("|", thousands_separator)
+
+			-- Add optional minus part back
+			formatted = minus .. int .. fraction
+		end
+	else
+		formatted = "0" -- 'silent' error
+	end
+	return formatted
+end
+
+---API Take the total cash and make it into a nice, colorful string of g s c (gold silver copper)
+---@param value number
+---@param thousands_separator string
+---@param decimal_separator string
+---@param only_gold boolean
+---@param show_labels boolean
+---@param show_icons boolean
+---@param add_color boolean
+---@return string outstr Formatted cash for output
+---@return integer gold part of value
+---@return integer silver part of value
+---@return integer copper part of value
+function TitanUtils_CashToString(value, thousands_separator, decimal_separator,
+	only_gold, show_labels, show_icons,
+	add_color)
+	local show_zero = true
+	local show_neg = true
+
+	local neg1 = ""
+	local neg2 = ""
+	local agold = 10000;
+	local asilver = 100;
+	local outstr = "";
+	local gold = 0;
+	local gold_str = ""
+	local gc = ""
+	local silver = 0;
+	local silver_str = ""
+	local sc = ""
+	local copper = 0;
+	local copper_str = ""
+	local cc = ""
+	local amount = (value or 0)
+	local font_size = TitanPanelGetVar("FontSize")
+	local icon_pre = "|TInterface\\MoneyFrame\\"
+	local icon_post = ":" .. font_size .. ":" .. font_size .. ":2:0|t"
+	local g_icon = icon_pre .. "UI-GoldIcon" .. icon_post
+	local s_icon = icon_pre .. "UI-SilverIcon" .. icon_post
+	local c_icon = icon_pre .. "UI-CopperIcon" .. icon_post
+	-- build the coin label strings based on the user selections
+	local c_lab = (show_labels and L["TITAN_GOLD_COPPER"]) or (show_icons and c_icon) or ""
+	local s_lab = (show_labels and L["TITAN_GOLD_SILVER"]) or (show_icons and s_icon) or ""
+	local g_lab = (show_labels and L["TITAN_GOLD_GOLD"]) or (show_icons and g_icon) or ""
+
+	-- show the money in highlight or coin color based on user selection
+	if add_color then
+		gc = Titan_Global.colors.coin_gold
+		sc = Titan_Global.colors.coin_silver
+		cc = Titan_Global.colors.coin_copper
+	else
+		gc = Titan_Global.colors.white
+		sc = Titan_Global.colors.white
+		cc = Titan_Global.colors.white
+	end
+
+	if show_neg then
+		if amount < 0 then
+			neg1 = TitanUtils_GetHexText("(", Titan_Global.colors.orange) -- "|cFFFF6600" .. "(" .. FONT_COLOR_CODE_CLOSE
+			neg2 = TitanUtils_GetHexText(")", Titan_Global.colors.orange) --"|cFFFF6600" .. ")" .. FONT_COLOR_CODE_CLOSE
+		else
+			-- no padding
+		end
+	end
+	if amount < 0 then
+		amount = amount * -1
+	end
+
+	-- amount INCLUDES silver and copper (last 4 digits)
+	if amount == 0 then
+		if show_zero then
+		   copper_str = TitanUtils_GetHexText("0".. c_lab, cc) --cc .. (amount or "?") .. c_lab .. "" .. FONT_COLOR_CODE_CLOSE
+		end
+	 elseif amount > 999999999999999999 then -- 999,999,999,999,999,999 (1 quadrillion - 1)
+		-- we are really in trouble :)
+		-- gold should be accurate but in exponent format
+		gold = (math.floor(amount / agold) or 0)
+		gold_str = TitanUtils_GetHexText(tostring(gold)..g_lab .. " ", gc)
+		-- silver and copper will be off
+		silver_str = ""
+		copper_str = ""
+	 elseif amount > 99999999999999999 then -- 99,999,999,999,999,999 (100 trillion - 1)
+		-- we are in some trouble :)
+		-- gold should be accurate so format
+		gold = (math.floor(amount / agold) or 0)
+		local gnum = TitanUtils_NumToString(gold, thousands_separator, decimal_separator)
+		gold_str = TitanUtils_GetHexText(gnum..g_lab .. " ", gc)
+		-- silver and copper will be off
+		silver_str = ""
+		copper_str = ""
+
+	  elseif amount > 0 then
+		-- figure out the gold - silver - copper components for return and string
+		gold = (math.floor(amount / agold) or 0)
+		amount = amount - (gold * agold) -- now only silver + copper
+		silver = (math.floor(amount / asilver) or 0)
+		copper = amount - (silver * asilver)
+
+		-- now make the coin strings
+		if gold > 0 then
+			local gnum = TitanUtils_NumToString(gold, thousands_separator, decimal_separator)
+			gold_str = TitanUtils_GetHexText(gnum..g_lab .. " ", gc) --gc .. (gnum) .. g_lab .. " " .. FONT_COLOR_CODE_CLOSE
+		else
+			gold_str = ""
+		end
+		if (silver > 0) then
+			local snum = (string.format("%02d", silver) or "?")
+			silver_str = TitanUtils_GetHexText(snum..s_lab .. " ", sc) --sc .. (silver or "?") .. s_lab .. " " .. FONT_COLOR_CODE_CLOSE
+		else
+			silver_str = ""
+		end
+		if (copper > 0) then
+			local cnum = (string.format("%02d", copper) or "?")
+			copper_str = TitanUtils_GetHexText(cnum..c_lab, cc) --cc .. (copper or "?") .. c_lab .. "" .. FONT_COLOR_CODE_CLOSE
+		else
+			copper_str = ""
+		end
+	end
+
+	if only_gold then
+		silver_str = ""
+		copper_str = ""
+		-- special case for those who want only gold when amount is less than 1 gold
+		if gold == 0 then
+			if show_zero then
+				gold_str = TitanUtils_GetHexText("0"..g_lab, gc) --gc .. "0" .. g_lab .. " " .. FONT_COLOR_CODE_CLOSE
+			end
+		end
+	end
+
+	-- build the return string
+	outstr = outstr
+		.. neg1
+		.. gold_str
+		.. silver_str
+		.. copper_str
+		.. neg2
+	--[[
+print("_CashToString:"
+..(gold or "?").."g "
+..(silver or "?").."s "
+..(copper or "?").."c "
+..(outstr or "?")
+);
+--]]
+	return outstr, gold, silver, copper
+end
+
 --====== Right click menu routines - Retail dropdown menu

 ---local Add menu button at the given level.
diff --git a/TitanGold/TitanGold.lua b/TitanGold/TitanGold.lua
index 053dac6..8132cc5 100644
--- a/TitanGold/TitanGold.lua
+++ b/TitanGold/TitanGold.lua
@@ -59,37 +59,17 @@ end
 ---@param amount number
 ---@return string
 local function comma_value(amount)
---[===[
-	local formatted = tostring(amount)
-	local k
-	local sep = (TitanGetVar(TITAN_GOLD_ID, "UseSeperatorComma") and "UseComma" or "UsePeriod")
-	while true do
-		if sep == "UseComma" then formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') end
-		if sep == "UsePeriod" then formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1.%2') end
-		if (k == 0) then
-			break
-		end
-	end
-	return formatted
---]===]
-	-- Jul 2024 Use same code as XP
-	local formatted = ""
-
-	if type(amount) == "number" then
-		local sep = (TitanGetVar(TITAN_GOLD_ID, "UseSeperatorComma") and "," or ".")
-		local i, j, minus, int, fraction = tostring(amount):find('([-]?)(%d+)([.]?%d*)')
-
-		-- reverse the int-string and append a comma to all blocks of 3 digits
-		int = int:reverse():gsub("(%d%d%d)", "%1"..sep)
-
-		-- reverse the int-string back remove an optional comma and put the
-		-- optional minus and fractional part back
-		formatted = minus .. int:reverse():gsub("^"..sep, "") .. fraction
+	local sep = ""
+	local dec = ""
+	if (TitanGetVar(TITAN_GOLD_ID, "UseSeperatorComma")) then
+		sep = ","
+		dec = "."
 	else
-		formatted = "0" -- 'silent' error
+		sep = "."
+		dec = ","
 	end
-	return formatted

+	return TitanUtils_NumToString(amount, sep, dec)
 end

 ---local Take the total cash and make it into a nice, colorful string of g s c (gold silver copper)
@@ -97,11 +77,11 @@ end
 ---@param show_zero boolean
 ---@param show_neg boolean
 ---@return string outstr Formatted cash for output
----@return number cash Value as passed in
 ---@return integer gold part of value
 ---@return integer silver part of value
 ---@return integer copper part of value
 local function NiceCash(value, show_zero, show_neg)
+	--[[
 	local neg1 = ""
 	local neg2 = ""
 	local agold = 10000;
@@ -195,15 +175,30 @@ local function NiceCash(value, show_zero, show_neg)
 		.. silver_str
 		.. copper_str
 		.. neg2
-	--[[
-SC.Print("Acc cash:"
+print("Acc cash:"
 ..(gold or "?").."g "
 ..(silver or "?").."s "
 ..(copper or "?").."c "
 ..(outstr or "?")
 );
 --]]
-	return outstr, cash, gold, silver, copper
+	local sep = ""
+	local dec = ""
+	if (TitanGetVar(TITAN_GOLD_ID, "UseSeperatorComma")) then
+		sep = ","
+		dec = "."
+	else
+		sep = "."
+		dec = ","
+	end
+
+	local outstr, gold, silver, copper =
+		TitanUtils_CashToString(value, sep, dec,
+			TitanGetVar(TITAN_GOLD_ID, "ShowGoldOnly"),
+			TitanGetVar(TITAN_GOLD_ID, "ShowCoinLabels"),
+			TitanGetVar(TITAN_GOLD_ID, "ShowCoinIcons"),
+			TitanGetVar(TITAN_GOLD_ID, "ShowColoredText"))
+	return outstr, gold, silver, copper
 end

 ---local Create Show menu - list of characters in same faction
diff --git a/TitanRegen/TitanRegen.lua b/TitanRegen/TitanRegen.lua
index 848cd9c..f5dcb85 100644
--- a/TitanRegen/TitanRegen.lua
+++ b/TitanRegen/TitanRegen.lua
@@ -29,30 +29,28 @@ local TITAN_RegenMPCombatTrack = 0;
 local L = LibStub("AceLocale-3.0"):GetLocale(TITAN_ID, true)
 -- ******************************** Functions *******************************

---[[
--- **************************************************************************
--- NAME : TitanRegenTemp_GetColoredTextRGB(text, r, g, b)
--- DESC : Define colors for colored text and display
--- VARS : text = text to display, r = red value, g = green value, b = blue value
--- **************************************************************************
---]]
-local function TitanRegenTemp_GetColoredTextRGB(text, r, g, b)
+---Color a text tring based on the RBG passed
+---@param text string To color
+---@param r number Red (0.0 - 1.0)
+---@param g any Green (0.0 - 1.0)
+---@param b any Blue (0.0 - 1.0)
+---@return string Color_string
+local function SetTextColorRBG(text, r, g, b)
+	local str = ""
      if (text and r and g and b) then
           local redColorCode = format("%02x", r * 255);
           local greenColorCode = format("%02x", g * 255);
           local blueColorCode = format("%02x", b * 255);
-          local colorCode = "|cff"..redColorCode..greenColorCode..blueColorCode;
-          return colorCode..text..FONT_COLOR_CODE_CLOSE;
+          local colorCode = "|cff"..redColorCode..greenColorCode..blueColorCode.."|r"
+          str = colorCode..text..FONT_COLOR_CODE_CLOSE
      end
+
+	 return str
 end

---[[
--- **************************************************************************
--- NAME : TitanPanelRegenButton_OnLoad()
--- DESC : Registers the plugin upon it loading
--- **************************************************************************
---]]
-function TitanPanelRegenButton_OnLoad(self)
+---local Build the plugin .registry and register events
+---@param self Button plugin frame
+function OnLoad(self)
 	local notes = ""
 		.."Adds a regen monitor to Titan Panel to show HP/MANA regen - Classic versions only.\n"
 	self.registry = {
@@ -60,7 +58,7 @@ function TitanPanelRegenButton_OnLoad(self)
 		category = "Built-ins",
 		version = TITAN_VERSION,
 		menuText = L["TITAN_REGEN_MENU_TEXT"],
-		buttonTextFunction = "TitanPanelRegenButton_GetButtonText",
+		buttonTextFunction = "GetButtonText",
 		tooltipTitle = L["TITAN_REGEN_MENU_TOOLTIP_TITLE"],
 		tooltipTextFunction = "TitanPanelRegenButton_GetTooltipText",
 		icon = "Interface\\AddOns\\TitanRegen\\TitanRegen",
@@ -84,12 +82,10 @@ function TitanPanelRegenButton_OnLoad(self)
 	}
 end

---[[
--- **************************************************************************
--- NAME : TitanPanelXPButton_OnEvent
--- DESC : Parse events registered to addon and act on them
--- **************************************************************************
---]]
+---local Handle events the clock plugin is interested in.
+---@param self Button plugin frame
+---@param event string Event
+---@param ... any Event parameters
 function TitanPanelRegenButton_OnEvent(self, event, a1, a2, ...)
 	if ( event == "PLAYER_ENTERING_WORLD") then
 	end
@@ -153,19 +149,17 @@ function TitanPanelRegenButton_OnEvent(self, event, a1, a2, ...)
 	end
 end

---[[
--- **************************************************************************
--- NAME : TitanPanelRegenButton_GetButtonText(id)
--- DESC : Calculate regeneration logic for button text
--- VARS : id = button ID
--- **************************************************************************
---]]
-function TitanPanelRegenButton_GetButtonText(id)
+---Generate button text
+---@param id string
+---@return string label_hit_points
+---@return string text_hit_points
+---@return string label_mana
+---@return string text_mana
+function GetButtonText(id)
 	local labelTextHP = "";
 	local valueTextHP = "";
 	local labelTextMP = "";
 	local valueTextMP = "";
-	local OutputStr = "";

 	if UnitHealth("player") == UnitHealthMax("player") then
 		TITAN_RegenHP = 0;
@@ -202,7 +196,7 @@ function TitanPanelRegenButton_GetButtonText(id)
 			valueTextMP = format(TITAN_REGEN_MP_FORMAT, TITAN_RegenMP);
 		end
 		if (TitanGetVar(TITAN_REGEN_ID, "ShowColoredText")) then
-			valueTextMP = TitanRegenTemp_GetColoredTextRGB(valueTextMP, 0.0, 0.0, 1.0);
+			valueTextMP = SetTextColorRBG(valueTextMP, 0.0, 0.0, 1.0);
 		else
 			valueTextMP = TitanUtils_GetHighlightText(valueTextMP);
 		end
@@ -222,12 +216,8 @@ print("Regen text"
 	return labelTextHP, valueTextHP, labelTextMP, valueTextMP;
 end

---[[
--- **************************************************************************
--- NAME : TitanPanelRegenButton_GetTooltipText()
--- DESC : Display tooltip text
--- **************************************************************************
---]]
+---Generate tooltip text
+---@return string Tool_tip Formatted text
 function TitanPanelRegenButton_GetTooltipText()
 	local minHP = TITAN_RegenMinHPRate;
 	local minMP = TITAN_RegenMinMPRate;
@@ -264,12 +254,7 @@ function TitanPanelRegenButton_GetTooltipText()
 	return txt
 end

---[[
--- **************************************************************************
--- NAME : TitanPanelRightClickMenu_PrepareTitanRegenMenu()
--- DESC : Display rightclick menu options
--- **************************************************************************
---]]
+---Generate right click menu options
 function TitanPanelRightClickMenu_PrepareRegenMenu()
 	local id = TITAN_REGEN_ID;
 	local info;
@@ -325,7 +310,7 @@ local function Create_Frames()
 	local window = CreateFrame("Button", TITAN_BUTTON, f, "TitanPanelComboTemplate")
 	window:SetFrameStrata("FULLSCREEN")
 	-- Using SetScript("OnLoad",   does not work
-	TitanPanelRegenButton_OnLoad(window);
+	OnLoad(window);
 --	TitanPanelButton_OnLoad(window); -- Titan XML template calls this...

 	window:SetScript("OnShow", function(self)
diff --git a/TitanRepair/TitanRepair.lua b/TitanRepair/TitanRepair.lua
index 3a25137..bb69d62 100644
--- a/TitanRepair/TitanRepair.lua
+++ b/TitanRepair/TitanRepair.lua
@@ -564,20 +564,11 @@ local function GetGSC(money)
 	return g, s, c, neg;
 end

---[[ local
--- **************************************************************************
--- NAME : GetGSC(money)
--- DESC : Scan all bags and equipment and set the 'scan in progress'
--- VARS :
--- money : int : money in copper
--- OUT :
--- gsc : string : formatted and colored of given money
--- **************************************************************************
---]]
 ---local Get a formated string from the given money (in copper)
 ---@param money number Money in copper
 ---@return string readable_money
 local function GetTextGSC(money)
+--[===[
 	local GSC_GOLD = "ffd100";
 	local GSC_SILVER = "e6e6e6";
 	local GSC_COPPER = "c8602c";
@@ -611,6 +602,30 @@ local function GetTextGSC(money)
 	end
 	if (neg) then gsc = "(" .. gsc .. ")"; end
 	return gsc;
+--]===]
+
+	local sep = ""
+	local dec = ""
+--	if (TitanGetVar(TITAN_REPAIR_ID, "UseSeperatorComma")) then
+		sep = ","
+		dec = "."
+--	else
+--		sep = "."
+--		dec = ","
+--	end
+
+	-- Not all parameters are Repair options so default.
+	local outstr, gold, silver, copper =
+		TitanUtils_CashToString(money,
+			",", -- thousand seprator
+			".", -- decimal seprator
+			TitanGetVar(TITAN_REPAIR_ID, "ShowCostGoldOnly"),
+			false, -- coin labels
+			false, -- coin icons
+			true -- color G / S / C
+		)
+	return outstr
+
 end

 ---local Repair items per user settings to use Guild or own gold.
diff --git a/TitanXP/TitanXP.lua b/TitanXP/TitanXP.lua
index 4212893..ad94257 100644
--- a/TitanXP/TitanXP.lua
+++ b/TitanXP/TitanXP.lua
@@ -83,22 +83,17 @@ end
 ---@param amount number
 ---@return string
 local function comma_value(amount)
-	local formatted = ""
-
-	if type(amount) == "number" then
-		local sep = (TitanGetVar(TITAN_XP_ID, "UseSeperatorComma") and "," or ".")
-		local i, j, minus, int, fraction = tostring(amount):find('([-]?)(%d+)([.]?%d*)')
-
-		-- reverse the int-string and append a comma to all blocks of 3 digits
-		int = int:reverse():gsub("(%d%d%d)", "%1"..sep)
-
-		-- reverse the int-string back remove an optional comma and put the
-		-- optional minus and fractional part back
-		formatted = minus .. int:reverse():gsub("^"..sep, "") .. fraction
+	local sep = ""
+	local dec = ""
+	if (TitanGetVar(TITAN_XP_ID, "UseSeperatorComma")) then
+		sep = ","
+		dec = "."
 	else
-		formatted = "0" -- 'silent' error
+		sep = "."
+		dec = ","
 	end
-	return formatted
+
+	return TitanUtils_NumToString(amount, sep, dec)
 end

 ---local Reset session and accumulated variables