Move SetLocaleFonts call, would try to use preferences which wouldn't be set up yet...
[QuestHelper.git] / recycle.lua
blobbd806380cb01786f19f4742b0525da7ee7b00383
1 QuestHelper.used_tables = 0
2 QuestHelper.free_tables = {}
4 QuestHelper.used_textures = 0
5 QuestHelper.free_textures = {}
7 QuestHelper.used_text = 0
8 QuestHelper.free_text = {}
10 QuestHelper.used_frames = 0
11 QuestHelper.free_frames = {}
13 function QuestHelper:CreateTable()
14 self.used_tables = self.used_tables + 1
15 return table.remove(self.free_tables) or {}
16 end
18 function QuestHelper:ReleaseTable(tbl)
19 assert(type(tbl) == "table")
20 for i,t in ipairs(self.free_tables) do assert(t ~= tbl) end
22 for key in pairs(tbl) do
23 tbl[key] = nil
24 end
26 self.used_tables = self.used_tables - 1
27 table.insert(self.free_tables, tbl)
28 end
30 function QuestHelper:CreateFrame(parent)
31 self.used_frames = self.used_frames + 1
32 local frame = table.remove(self.free_frames)
34 if frame then
35 frame:SetParent(parent)
36 else
37 frame = CreateFrame("Button", string.format("QuestHelperFrame%d",self.used_frames), parent)
38 end
40 frame:SetFrameLevel((parent or UIParent):GetFrameLevel()+1)
41 frame:SetFrameStrata("MEDIUM")
42 frame:Show()
44 return frame
45 end
47 local frameScripts =
49 "OnChar",
50 "OnClick",
51 "OnDoubleClick",
52 "OnDragStart",
53 "OnDragStop",
54 "OnEnter",
55 "OnEvent",
56 "OnHide",
57 "OnKeyDown",
58 "OnKeyUp",
59 "OnLeave",
60 "OnLoad",
61 "OnMouseDown",
62 "OnMouseUp",
63 "OnMouseWheel",
64 "OnReceiveDrag",
65 "OnShow",
66 "OnSizeChanged",
67 "OnUpdate",
68 "PostClick",
69 "PreClick"
72 function QuestHelper:ReleaseFrame(frame)
73 assert(type(frame) == "table")
74 for i,t in ipairs(self.free_frames) do assert(t ~= frame) end
76 for key in pairs(frame) do
77 -- Remove all keys except 0, which seems to hold some special data.
78 if key ~= 0 then
79 frame[key] = nil
80 end
81 end
83 for i, script in ipairs(frameScripts) do
84 frame:SetScript(script, nil)
85 end
87 frame:Hide()
88 frame:SetParent(nil)
89 frame:ClearAllPoints()
90 frame:SetMovable(false)
91 frame:RegisterForDrag()
92 frame:RegisterForClicks()
93 frame:SetScale(1)
94 frame:SetAlpha(1)
96 self.used_frames = self.used_frames - 1
97 table.insert(self.free_frames, frame)
98 end
100 function QuestHelper:CreateText(parent, text_str, text_size, text_font, r, g, b, a)
101 self.used_text = self.used_text + 1
102 local text = table.remove(self.free_text)
104 if text then
105 text:SetParent(parent)
106 else
107 text = parent:CreateFontString()
110 text:SetFont(text_font or QuestHelper.font.sans or ChatFontNormal:GetFont(), text_size or 12)
111 text:SetDrawLayer("OVERLAY")
112 text:SetJustifyH("CENTER")
113 text:SetJustifyV("MIDDLE")
114 text:SetTextColor(r or 1, g or 1, b or 1, a or 1)
115 text:SetText(text_str or "")
116 text:SetShadowColor(0, 0, 0, 0.3)
117 text:SetShadowOffset(1, -1)
118 text:Show()
120 return text
123 function QuestHelper:ReleaseText(text)
124 assert(type(text) == "table")
125 for i,t in ipairs(self.free_text) do assert(t ~= text) end
127 for key in pairs(text) do
128 -- Remove all keys except 0, which seems to hold some special data.
129 if key ~= 0 then
130 text[key] = nil
134 text:Hide()
135 text:SetParent(nil)
136 text:ClearAllPoints()
137 self.used_text = self.used_text - 1
138 table.insert(self.free_text, text)
141 function QuestHelper:CreateTexture(parent, r, g, b, a)
142 self.used_textures = self.used_textures + 1
143 local tex = table.remove(self.free_textures)
145 if tex then
146 tex:SetParent(parent)
147 else
148 tex = parent:CreateTexture()
151 if not tex:SetTexture(r, g, b, a) and
152 not tex:SetTexture("Interface\\Icons\\Temp.blp") then
153 tex:SetTexture(1, 0, 1, 0.5)
156 tex:ClearAllPoints()
157 tex:SetTexCoord(0, 1, 0, 1)
158 tex:SetVertexColor(1, 1, 1, 1)
159 tex:SetDrawLayer("ARTWORK")
160 tex:SetBlendMode("BLEND")
161 tex:SetWidth(12)
162 tex:SetHeight(12)
163 tex:Show()
165 return tex
168 function QuestHelper:CreateIconTexture(parent, id)
169 local icon = self:CreateTexture(parent, "Interface\\AddOns\\QuestHelper\\Art\\Icons.tga")
171 local w, h = 1/8, 1/8
172 local x, y = ((id-1)%8)*w, math.floor((id-1)/8)*h
174 icon:SetTexCoord(x, x+w, y, y+h)
176 return icon
179 function QuestHelper:CreateDotTexture(parent)
180 local icon = self:CreateIconTexture(parent, 13)
181 icon:SetWidth(5)
182 icon:SetHeight(5)
183 icon:SetVertexColor(0, 0, 0, 0.35)
184 return icon
187 function QuestHelper:CreateGlowTexture(parent)
188 local tex = self:CreateTexture(parent, "Interface\\Addons\\QuestHelper\\Art\\Glow.tga")
190 local angle = math.random()*6.28318530717958647692528676655900576839433879875021164
191 local x, y = math.cos(angle)*0.707106781186547524400844362104849039284835937688474036588339869,
192 math.sin(angle)*0.707106781186547524400844362104849039284835937688474036588339869
194 -- Randomly rotate the texture, so they don't all look the same.
195 tex:SetTexCoord(x+0.5, y+0.5, y+0.5, 0.5-x, 0.5-y, x+0.5, 0.5-x, 0.5-y)
196 tex:ClearAllPoints()
198 return tex
201 function QuestHelper:ReleaseTexture(tex)
202 assert(type(tex) == "table")
203 for i,t in ipairs(self.free_textures) do assert(t ~= tex) end
205 for key in pairs(tex) do
206 -- Remove all keys except 0, which seems to hold some special data.
207 if key ~= 0 then
208 tex[key] = nil
212 tex:Hide()
213 tex:SetParent(nil)
214 tex:ClearAllPoints()
215 self.used_textures = self.used_textures - 1
216 table.insert(self.free_textures, tex)