vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 ------------------------------------------------------
2 -- GFWUtils.lua
3 -- Useful utility / debug functions
4 ------------------------------------------------------
5  
6 GFWUTILS_THIS_VERSION = 8;
7  
8 ------------------------------------------------------
9  
10 -- Shortcuts for common text color codes
11 function GFWUtils_temp_HiliteText(text)
12 if (text == nil) then return nil; end
13 return HIGHLIGHT_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
14 end
15 function GFWUtils_temp_RedText(text)
16 if (text == nil) then return nil; end
17 return RED_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
18 end
19 function GFWUtils_temp_GrayText(text)
20 if (text == nil) then return nil; end
21 return GRAY_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
22 end
23 function GFWUtils_temp_LightYellowText(text)
24 if (text == nil) then return nil; end
25 return LIGHTYELLOW_FONT_COLOR_CODE..text..FONT_COLOR_CODE_CLOSE;
26 end
27  
28 -- Prints a message to the chat frame
29 function GFWUtils_temp_Print(message, r, g, b)
30 DEFAULT_CHAT_FRAME:AddMessage(message, (r or GFW_FONT_COLOR.r), (g or GFW_FONT_COLOR.g), (b or GFW_FONT_COLOR.b));
31 end
32  
33 -- Prints a message to the chat frame once per interval (seconds) or once per session if interval is nil
34 function GFWUtils_temp_PrintOnce(message, interval, r, g, b)
35 if (GFWUtils.PrintOnceCache == nil) then
36 GFWUtils.PrintOnceCache = {};
37 end
38 if (interval == nil and GFWUtils.PrintOnceCache[message]) then
39 return;
40 end
41 if (interval and GFWUtils.PrintOnceCache[message] and GetTime() - GFWUtils.PrintOnceCache[message] < interval) then
42 return;
43 end
44 DEFAULT_CHAT_FRAME:AddMessage(message, (r or GFW_FONT_COLOR.r), (g or GFW_FONT_COLOR.g), (b or GFW_FONT_COLOR.b));
45 GFWUtils.PrintOnceCache[message] = GetTime();
46 end
47  
48 -- Prints a message to the chat frame only if Debug is set
49 function GFWUtils_temp_DebugLog(message)
50 if (GFWUtils.Debug) then
51 DEFAULT_CHAT_FRAME:AddMessage(message, GFW_DEBUG_COLOR.r, GFW_DEBUG_COLOR.g, GFW_DEBUG_COLOR.b);
52 end
53 end
54  
55 -- Prints a message in yellow to the floating messages frame
56 function GFWUtils_temp_Note(message)
57 UIErrorsFrame:AddMessage(message, 1.0, 1.0, 0.0, 1.0, UIERRORS_HOLD_TIME);
58 end
59  
60 -- Converts string.format to a string.find pattern: "%s hits %s for %d." to "(.+) hits (.+) for (%d+)"
61 -- based on Recap by Gello
62 function GFWUtils_temp_FormatToPattern(formatString)
63  
64 local patternString = formatString;
65  
66 patternString = string.gsub(patternString, "%%%d+%$([diouXxfgbcsq])", "%%%1"); -- reordering specifiers (e.g. %2$s) stripped
67 patternString = string.gsub(patternString, "([%$%(%)%.%[%]%*%+%-%?%^])", "%%%1"); -- convert regex special characters
68  
69 patternString = string.gsub(patternString, "%%c", "(.)"); -- %c to (.)
70 patternString = string.gsub(patternString, "%%s", "(.+)"); -- %s to (.+)
71 patternString = string.gsub(patternString, "%%[du]", "(%%d+)"); -- %d to (%d+)
72 patternString = string.gsub(patternString, "%%([gf])", "(%%d+%%.*%%d*)"); -- %g or %f to (%d+%.*%d*)
73  
74 return patternString;
75  
76 end
77  
78 -- Splits a string into a table of strings separated by 'separator'.
79 -- e.g. Split(aString, ", ") is the reverse of table.concat(aTable, ", ")
80 function GFWUtils_temp_Split(aString, separator)
81 if (aString == nil) then return nil; end
82  
83 local t = {};
84 local function helper(segment)
85 table.insert(t, segment);
86 end
87 helper((string.gsub(aString, "(.-)"..separator, helper)));
88 return t;
89 end
90  
91 -- Capitalizes the first letter of each word in aString.
92 function GFWUtils_temp_TitleCase(aString)
93 if (aString == nil) then return nil; end
94 local function capWords(first, rest)
95 return string.upper(first)..string.lower(rest);
96 end
97 return string.gsub(aString, "(%w)([%w_']*)", capWords);
98 end
99  
100 -- Splits a cash amount into gold, siver, and copper
101 function GFWUtils_temp_GSC(money)
102 if (money == nil) then money = 0; end
103 local g = math.floor(money / 10000);
104 local s = math.floor((money - (g*10000)) / 100);
105 local c = math.floor(money - (g*10000) - (s*100));
106 return g,s,c;
107 end
108  
109 -- Formats money text by color for gold, silver, copper
110 -- discards copper for amounts greater than 1g unless second arg evaluates true
111 -- based on Auctioneer
112 function GFWUtils_temp_TextGSC(money, noRound)
113 local GSC_GOLD="ffd100";
114 local GSC_SILVER="e6e6e6";
115 local GSC_COPPER="c8602c";
116 local GSC_START="|cff%s%d%s|r";
117 local GSC_PART=" |cff%s%02d%s|r";
118 local GSC_NONE="|cffa0a0a0none|r";
119  
120 local g, s, c = GFWUtils.GSC(money);
121  
122 local gsc = "";
123 if (g > 0) then
124 gsc = format(GSC_START, GSC_GOLD, g, "g");
125 if (s > 0) then
126 gsc = gsc..format(GSC_PART, GSC_SILVER, s, "s");
127 if (noRound and c > 0) then
128 gsc = gsc..format(GSC_PART, GSC_COPPER, c, "c");
129 end
130 end
131 elseif (s > 0) then
132 gsc = format(GSC_START, GSC_SILVER, s, "s");
133 if (c > 0) then
134 gsc = gsc..format(GSC_PART, GSC_COPPER, c, "c");
135 end
136 elseif (c > 0) then
137 gsc = gsc..format(GSC_START, GSC_COPPER, c, "c");
138 else
139 gsc = GSC_NONE;
140 end
141  
142 return gsc;
143 end
144  
145 function GFWUtils_temp_ItemLink(linkInfo)
146 local sName, sLink, iQuality, iLevel, sType, sSubType, iCount = GetItemInfo(linkInfo);
147 if (sName) then
148 local _, _, _, color = GetItemQualityColor(iQuality);
149 local linkFormat = "%s|H%s|h[%s]|h|r";
150 return string.format(linkFormat, color, sLink, sName);
151 else
152 return nil;
153 end
154 end
155  
156 ------------------------------------------------------
157 -- load only if not already loaded
158 ------------------------------------------------------
159  
160 if (GFWUtils == nil) then
161 GFWUtils = {};
162 end
163 local G = GFWUtils;
164 if (G.Version == nil or (tonumber(G.Version) ~= nil and G.Version < GFWUTILS_THIS_VERSION)) then
165  
166 -- Constants
167 GFW_FONT_COLOR = {r=0.25, g=1.0, b=1.0};
168 GFW_DEBUG_COLOR = {r=1.0, g=0.75, b=0.25};
169 G.Debug = false;
170  
171 -- Functions
172 G.Hilite = GFWUtils_temp_HiliteText;
173 G.Red = GFWUtils_temp_RedText;
174 G.Gray = GFWUtils_temp_GrayText;
175 G.LtY = GFWUtils_temp_LightYellowText;
176 G.Print = GFWUtils_temp_Print;
177 G.PrintOnce = GFWUtils_temp_PrintOnce;
178 G.DebugLog = GFWUtils_temp_DebugLog;
179 G.Note = GFWUtils_temp_Note;
180 G.FormatToPattern = GFWUtils_temp_FormatToPattern;
181 G.Split = GFWUtils_temp_Split;
182 G.TitleCase = GFWUtils_temp_TitleCase;
183 G.GSC = GFWUtils_temp_GSC;
184 G.TextGSC = GFWUtils_temp_TextGSC;
185 G.ItemLink = GFWUtils_temp_ItemLink;
186  
187 -- Set version number
188 G.Version = GFWUTILS_THIS_VERSION;
189 end
190