comparison libKT/libKT-1.0.lua @ 2:f7a0898d293c

adding library files, toc files, and a missing XML to versioning
author Nenue
date Tue, 21 Jun 2016 08:10:36 -0400
parents
children
comparison
equal deleted inserted replaced
1:cd7d06bcd98d 2:f7a0898d293c
1 --[[ Implements
2 -- KT.register(frame) to hook the following (all optional):
3 -- frame:init() run immediately after KT sets itself up
4 -- frame:profile("Name-TruncatedRealm") called the first time SavedVars data becomes available
5 -- frame:variables() called by PLAYER_ENTERING_WORLD
6 -- frame:event(event, ...) replaces the event callback
7 -- frame:ui() called by /ui when activating
8 ]]--
9
10 --GLOBALS: LibKT, KrakTool, KTErrorFrame, LibKTError, SlashCmdList, SLASH_RL1, SLASH_UI1
11 local CreateFrame, debugstack, tostring, select = CreateFrame, debugstack, tostring, select
12 local print, max, unpack, tinsert = print, max, unpack, tinsert
13 local ipairs, xpcall = ipairs, xpcall
14 local UI_TOGGLE = false
15
16 KrakTool = CreateFrame('Frame', 'KrakTool', UIParent)
17 LibKT = select(2, ...)
18 local KT = LibKT
19 KT.handler = KrakTool
20 KT.frames = {}
21
22 SLASH_RL1 = "/rl"
23 SlashCmdList.RL = function ()
24 ReloadUI()
25 end
26
27 SLASH_UI1 = "/ui"
28 SlashCmdList.UI = function ()
29 if UI_TOGGLE then
30 UI_TOGGLE = false
31 else
32 UI_TOGGLE = true
33 end
34 for i, frame in pairs(KT.frames) do
35 if UI_TOGGLE then
36 if frame.close then
37 frame.close()
38 else
39 frame:Hide()
40 end
41 else
42 if frame.ui then
43 frame.ui()
44 end
45 frame:Show()
46 end
47 end
48 end
49
50 LibKTError = function(msg)
51 local dstack = debugstack()
52 :gsub("Interface\\AddOns\\",'~\\')
53 :gsub("<(.-)>", function(a) return '|cFF00FFFF<'.. a ..'>|r' end)
54 KTErrorFrame.errmsg:SetText(msg)
55 KTErrorFrame.debugstack:SetText(dstack)
56 KTErrorFrame:SetHeight(KTErrorFrame.debugstack:GetStringHeight() + KTErrorFrame.errmsg:GetStringHeight() + 12)
57 KTErrorFrame:Show()
58 end
59
60 local initStack = {}
61 local eventStub = function(self, event, ...)
62 local isHandled
63 local nodebug
64
65 if self.event then
66 nodebug = self.event(self, event, ...)
67 end
68
69 if self[event] then
70 nodebug = nodebug or self[event](self, event, ...)
71 self.missed = 0
72 self.handled = self.handled + 1
73 isHandled = true
74 else
75 self.firstEvent = false
76 self.unhandled = self.unhandled + 1
77 self.missed = self.missed + 1
78 end
79
80 if nodebug then
81 return
82 end
83
84 print(self:GetName(), event, ...)
85
86 -- debug outputs
87 if self.status then
88 self.status:SetText(event .. '\n|cFF00FF00' .. self.handled .. '|r |cFFFF8800' .. self.missed .. '|r |cFFFF4400' .. self.unhandled .. '|r')
89 if isHandled then
90 self.status:SetTextColor(0,1,0)
91 if self.log then
92 local logtext = event
93 for i = 1, select('#',...) do
94 logtext = logtext .. '\n' .. i .. ':' .. tostring(select(i,...))
95 end
96 self.log:SetText('|cFFFFFF00last|r\n' .. logtext)
97 local newWidth = self.log:GetStringWidth()
98
99 if self.logfirst then
100 if not self.firstEvent then
101 self.firstEvent = event
102 self.logfirst:SetText('|cFF00FF88first|r\n' .. logtext)
103 end
104
105 newWidth = newWidth + self.logfirst:GetStringWidth()
106 end
107 if self.logdiff then
108 if not event ~= self.firstEvent then
109 self.firstEvent = event
110 self.logdiff:SetText('|cFF0088FFdiff|r\n' .. logtext)
111 end
112 newWidth = newWidth + self.logdiff:GetStringWidth()
113 end
114 --self:SetWidth(newWidth)
115 end
116 else
117 self.status:SetTextColor(1,0,0)
118 end
119 end
120 end
121
122 KT.register = function(frame, name, noGUI)
123 if not name then
124 name = frame:GetName()
125 end
126
127 KT.frames[name] = frame
128 frame:SetScript('OnEvent', eventStub)
129 frame.unhandled = 0
130 frame.missed = 0
131 frame.handled = 0
132 frame.firstEvent = false
133 tinsert(initStack, frame)
134
135 if noGUI then
136 return
137 end
138
139 frame.UIPanelAnchor = {'TOPLEFT', frame, 'TOPLEFT', 12, -12 }
140 frame.UIPanelGrowth = {'TOPLEFT', 'TOPRIGHT', 14, 0}
141 frame.button = KT.button
142 frame.uibutton = KT.uibutton
143 frame.tab = KT.tab
144 frame.print = KT.print
145
146 return KT
147 end
148
149 KT.handler:RegisterEvent('VARIABLES_LOADED')
150 KT.handler:SetScript('OnEvent', function(self, event, ...)
151 print('KrakTool', event, ...)
152 if not LibKTDB then
153 LibKTDB = {}
154 end
155 KT.db = LibKTDB
156
157 KT.db.runcount = KT.db.runcount or 1
158 KT.db.runcount = KT.db.runcount + 1
159 print(KT.db.runcount)
160
161 for i, frame in ipairs(initStack) do
162 print('|cFF00FF00', i, '|r', frame:GetName())
163 if frame.init then
164 xpcall(frame.init, LibKTError)
165 end
166 end
167
168 self:RegisterEvent('PLAYER_ENTERING_WORLD')
169 self:SetScript('OnEvent', function()
170 for i, frame in ipairs(initStack) do
171 print('|cFF00FFFF', i, '|r', frame:GetName())
172 if frame.variables then
173 xpcall(frame.variables, LibKTError)
174 end
175 end
176 end)
177 end)
178
179 KT.print = function(module, ...)
180 print('|cFF00FFFF'..module:GetName()..'|r:', ...)
181 end
182
183 --- Button generators
184
185 local GetButtonTemplate = function(name, parent, template, onClick)
186 if _G[name] then
187 return _G[name]
188 end
189
190 local button = CreateFrame('Button', name, parent, template)
191 button:SetScript('OnMouseUp', onClick)
192 return button
193 end
194
195 local SetButtonAnchor = function(self, collector, anchor, growth)
196 if self:GetID() == 0 then
197 self:SetID(#collector)
198 print('registered TabButton #', self:GetID())
199 end
200
201 if self:GetID() == 1 then
202 self:SetPoint(unpack(anchor))
203 else
204 growth[2] = collector[self:GetID()-1]
205 self:SetPoint(unpack(growth))
206 end
207 end
208
209 KT.tab = function(self, name, tooltip, texture, coords)
210 local button = GetButtonTemplate(name, self, 'KTTabButton', self.SelectTab)
211 button.icon:SetTexture(texture)
212 button.tooltip = tooltip
213 button:SetSize(unpack(self.tabSize))
214 if coords then
215 button.icon:SetTexCoord(unpack(coords))
216 end
217 SetButtonAnchor(button, self.tabButtons, self.tabAnchor, self.tabGrowth)
218 return button
219 end
220
221 KT.button = function(self, name, text, tooltip, onClick)
222 local button = GetButtonTemplate(name, self, 'KTButton', onClick)
223
224 button.tooltip = tooltip
225 button:SetText(text)
226 button:SetWidth(max(button:GetWidth(), button:GetFontString():GetStringWidth() + 12))
227
228 SetButtonAnchor(button, self.controls, self.controlsAnchor, self.controlsGrowth)
229 return button
230 end
231
232 KT.uibutton = function(self, name, text, tooltip, onClick, texture, coords)
233 local button = GetButtonTemplate(name, self, 'KTUIPanelButton', onClick)
234
235 button.tooltip = tooltip
236 button:SetText(text)
237 button.icon:SetTexture(texture)
238 button:SetWidth(button:GetFontString():GetStringWidth() + button.icon:GetWidth()/1.5)
239 if coords then
240 button.icon:SetTexCoord(unpack(coords))
241 end
242 SetButtonAnchor(button, self.UIPanels, self.UIPanelAnchor, self.UIPanelGrowth)
243 return button
244 end