Mercurial > wow > ouroloot
comparison LibFarmbuyer.lua @ 1:822b6ca3ef89
Import of 2.15, moving to wowace svn.
| author | Farmbuyer of US-Kilrogg <farmbuyer@gmail.com> |
|---|---|
| date | Sat, 16 Apr 2011 06:03:29 +0000 |
| parents | |
| children | df3e27edbd60 |
comparison
equal
deleted
inserted
replaced
| 0:0f14a1e5364d | 1:822b6ca3ef89 |
|---|---|
| 1 --[[ | |
| 2 Not really meant for public use. Stuff that I keep using everywhere and | |
| 3 got tired of reimplementing, or even copy-and-pasting. The notes here are | |
| 4 reminders to myself. | |
| 5 | |
| 6 Library contents: | |
| 7 - author_debug | |
| 8 Evaluates to true if I'm hacking on something. | |
| 9 | |
| 10 - tableprint(t) | |
| 11 A single print() call to the contents of T, including nils; strings are | |
| 12 cleaned up with respect to embedded '|'/control chars. If there is a | |
| 13 global LIBFARMPRINT function, it is called with the cleaned-up T instead | |
| 14 of directly calling print. | |
| 15 | |
| 16 - safeprint(...) | |
| 17 Same as tableprint() on the argument list. | |
| 18 | |
| 19 - safeiprint(...) | |
| 20 Same as safeprint() but with index numbers inserted. Ex: | |
| 21 safeiprint(a,b,c) --> <1>,a,<2>,b,<3>,c | |
| 22 | |
| 23 - CHAT(n) | |
| 24 Returns a function suitable for assigning to LIBFARMPRINT, directing all | |
| 25 output to builtin chat frame N. | |
| 26 | |
| 27 - t = StaticPopup(t) | |
| 28 Fills out "typical" settings inside T, especially if T contains any kind | |
| 29 of editbox: | |
| 30 + cannot accept an empty editbox | |
| 31 + pressing Enter runs OnAccept | |
| 32 + editbox grabs keyboard focus | |
| 33 + OnAccept runs with editbox's text in dialog.usertext | |
| 34 Returns T. | |
| 35 | |
| 36 - nullfunc() | |
| 37 Empty placeholder function. Will be less useful if WoW ever moves to Lua 5.2. | |
| 38 This is a fascinating and educational place to insert print calls... | |
| 39 | |
| 40 - tabledump(t)/dumptable(t) | |
| 41 If author_debug, this runs the builtin /dump command on T. Otherwise nothing. | |
| 42 | |
| 43 - DoOnceNextUpdate(f) | |
| 44 Runs F on the next frame refresh cycle. Multiple calls in one cycle will | |
| 45 stack LIFO. Calls *while* processing the stack are safe, and will be stacked | |
| 46 up for the next cycle. | |
| 47 ]] | |
| 48 | |
| 49 local MAJOR, MINOR = "LibFarmbuyer", 7 | |
| 50 assert(LibStub,MAJOR.." requires LibStub") | |
| 51 local lib = LibStub:NewLibrary(MAJOR, MINOR) | |
| 52 if not lib then return end | |
| 53 | |
| 54 _G[MAJOR] = lib | |
| 55 | |
| 56 ---------------------------------------------------------------------- | |
| 57 local new, del, copy, clear | |
| 58 | |
| 59 | |
| 60 ---------------------------------------------------------------------- | |
| 61 --[[ | |
| 62 safeprint | |
| 63 ]] | |
| 64 local function undocontrol(c) | |
| 65 return ("\\%.3d"):format(c:byte()) | |
| 66 end | |
| 67 function lib.CHAT(n) | |
| 68 local cf = _G["ChatFrame"..n] | |
| 69 return function(t) | |
| 70 local msg = table.concat(t,' ', i, tonumber(t.n) or #t) | |
| 71 cf:AddMessage(msg) | |
| 72 end | |
| 73 end | |
| 74 function lib.safeprint(...) | |
| 75 local args = { n=select('#',...), ... } | |
| 76 lib.tableprint(args) | |
| 77 end | |
| 78 function lib.safeiprint(...) | |
| 79 local args = { n=select('#',...), ... } | |
| 80 local last = args.n | |
| 81 while last > 0 do | |
| 82 table.insert (args, last, "<"..last..">") | |
| 83 last = last - 1 | |
| 84 end | |
| 85 args.n = 2 * args.n | |
| 86 lib.tableprint(args) | |
| 87 end | |
| 88 function lib.tableprint(t) | |
| 89 for i = 1, (tonumber(t.n) or #t) do | |
| 90 t[i] = tostring(t[i]):gsub('\124','\124\124') | |
| 91 :gsub('(%c)', undocontrol) | |
| 92 end | |
| 93 if type(_G.LIBFARMPRINT) == 'function' then | |
| 94 return _G.LIBFARMPRINT(t) | |
| 95 else | |
| 96 return print(unpack(t)) | |
| 97 end | |
| 98 end | |
| 99 | |
| 100 -- See below for global versions. | |
| 101 | |
| 102 | |
| 103 ---------------------------------------------------------------------- | |
| 104 local StaticPopupDialogs = _G.StaticPopupDialogs | |
| 105 | |
| 106 local function EditBoxOnTextChanged_notempty (editbox) -- this is also called when first shown | |
| 107 if editbox:GetText() ~= "" then | |
| 108 editbox:GetParent().button1:Enable() | |
| 109 else | |
| 110 editbox:GetParent().button1:Disable() | |
| 111 end | |
| 112 end | |
| 113 local function EditBoxOnEnterPressed_accept (editbox) | |
| 114 local dialog = editbox:GetParent() | |
| 115 StaticPopupDialogs[dialog.which].OnAccept (dialog, dialog.data, dialog.data2) | |
| 116 dialog:Hide() | |
| 117 end | |
| 118 local function OnShow_witheditbox (dialog, data) | |
| 119 local info = StaticPopupDialogs[dialog.which] | |
| 120 dialog[info.hasWideEditBox and "wideEditBox" or "editBox"]:SetFocus() | |
| 121 if info.farm_OnShow then | |
| 122 return info.farm_OnShow (dialog, data) | |
| 123 end | |
| 124 end | |
| 125 local function OnAccept_witheditbox (dialog, data, data2) | |
| 126 local info = StaticPopupDialogs[dialog.which] | |
| 127 dialog.usertext = dialog[info.hasWideEditBox and "wideEditBox" or "editBox"]:GetText():trim() | |
| 128 if info.farm_OnAccept then | |
| 129 return info.farm_OnAccept (dialog, data, data2) | |
| 130 end | |
| 131 end | |
| 132 | |
| 133 --[[ | |
| 134 StaticPopup | |
| 135 ]] | |
| 136 function lib.StaticPopup (t) | |
| 137 if t.hasEditBox then | |
| 138 t.EditBoxOnTextChanged = EditBoxOnTextChanged_notempty | |
| 139 t.EditBoxOnEnterPressed = EditBoxOnEnterPressed_accept | |
| 140 if t.OnShow then | |
| 141 t.farm_OnShow = t.OnShow | |
| 142 end | |
| 143 t.OnShow = OnShow_witheditbox | |
| 144 if t.OnAccept then | |
| 145 t.farm_OnAccept = t.OnAccept | |
| 146 end | |
| 147 t.OnAccept = OnAccept_witheditbox | |
| 148 -- this calls OnCancel with "clicked", unless noCancelOnEscape is set | |
| 149 t.EditBoxOnEscapePressed = StaticPopup_EscapePressed | |
| 150 end | |
| 151 | |
| 152 t.timeout = 0 | |
| 153 t.whileDead = true | |
| 154 t.hideOnEscape = true | |
| 155 t.enterClicksFirstButton = true | |
| 156 | |
| 157 return t | |
| 158 end | |
| 159 | |
| 160 | |
| 161 ---------------------------------------------------------------------- | |
| 162 --[[ | |
| 163 This is ugly, but at least it all gets GC'd almost immediately. | |
| 164 ]] | |
| 165 function lib.nullfunc() end | |
| 166 | |
| 167 if ({ | |
| 168 ["Bandwagon"] = true, ["Kilvin"] = true, ["Waterfaucet"] = true, | |
| 169 ["Farmbuyer"] = true, ["Oxdeadbeef"] = true, ["Pointystick"] = true, | |
| 170 ["Angryhobbit"] = true, ["Malrubius"] = true, ["Hemogoblin"] = true, | |
| 171 })[UnitName("player")] then | |
| 172 lib.author_debug = true | |
| 173 _G.safeprint = lib.safeprint | |
| 174 _G.safeiprint = lib.safeiprint | |
| 175 function lib.tabledump(t) | |
| 176 _G.UIParentLoadAddOn("Blizzard_DebugTools") | |
| 177 _G.LibF_DEBUG = t | |
| 178 _G.SlashCmdList.DUMP("LibF_DEBUG") | |
| 179 end | |
| 180 else | |
| 181 lib.tabledump = lib.nullfunc | |
| 182 end | |
| 183 lib.dumptable = lib.tabledump | |
| 184 | |
| 185 | |
| 186 ---------------------------------------------------------------------- | |
| 187 --[[ | |
| 188 DoOnceNextUpdate | |
| 189 ]] | |
| 190 do | |
| 191 local frame = CreateFrame("Frame", "LibFarmbuyerDONUFrame") | |
| 192 frame:Hide() | |
| 193 frame:SetScript("OnUpdate", function() | |
| 194 frame:Hide() | |
| 195 local q = frame.nexttime | |
| 196 local tmp | |
| 197 frame.nexttime = nil | |
| 198 while q do | |
| 199 tmp = q | |
| 200 q.f(frame) | |
| 201 q = q.n | |
| 202 del(tmp) | |
| 203 end | |
| 204 end) | |
| 205 | |
| 206 function lib.DoOnceNextUpdate (func) | |
| 207 local nextt = new() | |
| 208 nextt.f = func | |
| 209 nextt.n = frame.nexttime | |
| 210 frame.nexttime = nextt | |
| 211 frame:Show() | |
| 212 end | |
| 213 end | |
| 214 | |
| 215 | |
| 216 ---------------------------------------------------------------------- | |
| 217 -- Recycling functions yoinked from AceConfigDialog and tweaked | |
| 218 do | |
| 219 local pool = setmetatable({},{__mode="k"}) | |
| 220 function clear() | |
| 221 wipe(pool) | |
| 222 end | |
| 223 function new(...) -- slightly modified variant, takes optional initializers | |
| 224 local t = next(pool) | |
| 225 if t then | |
| 226 pool[t] = nil | |
| 227 for i = 1, select('#',...) do | |
| 228 t[i] = select(i,...) | |
| 229 end | |
| 230 return t | |
| 231 else | |
| 232 return {...} | |
| 233 end | |
| 234 end | |
| 235 function copy(t) | |
| 236 local c = new() | |
| 237 for k, v in pairs(t) do | |
| 238 c[k] = v | |
| 239 end | |
| 240 return c | |
| 241 end | |
| 242 function del(t) | |
| 243 wipe(t) | |
| 244 pool[t] = true | |
| 245 end | |
| 246 end | |
| 247 | |
| 248 -- vim: noet |
