comparison HotCorners.lua @ 0:fc346da3afd9

First commit Hot Corners standalone.
author tercio
date Fri, 08 Aug 2014 12:35:17 -0300
parents
children 018fc9f0c471
comparison
equal deleted inserted replaced
-1:000000000000 0:fc346da3afd9
1
2 LibHotCorners = LibStub ("AceAddon-3.0"):NewAddon ("HotCorners", "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
3 local LibHotCorners = LibHotCorners
4
5 local LBD = LibStub ("LibDataBroker-1.1")
6
7 local debug = false
8 local tinsert = tinsert
9
10 local default_db = {
11 profile = {
12 is_enabled = true,
13 topleft_enabled = true,
14 quickfunc = false,
15 clicks = {},
16 disabled = {}
17 },
18 }
19
20 function LibHotCorners:OnEnable()
21
22 end
23
24 function LibHotCorners:OnDisable()
25
26 end
27
28 local refresh_topleft = function()
29 LibHotCorners ["topleft"].is_enabled = LibHotCorners.db.profile.topleft_enabled
30 end
31
32 local OptionsTable = {
33 name = "HotCorners",
34 type = "group",
35 args = {
36 Enabled = {
37 type = "toggle",
38 name = "Enabled",
39 desc = "Enable or Disable this addon.",
40 order = 1,
41 get = function() return LibHotCorners.db.profile.is_enabled end,
42 set = function (self, val) LibHotCorners.db.profile.is_enabled = not LibHotCorners.db.profile.is_enabled; --[[ do something]] end,
43 },
44 TopLeftEnabled = {
45 type = "toggle",
46 name = "Top Left",
47 desc = "Enable or Disable the Top Left bar.",
48 order = 2,
49 get = function() return LibHotCorners.db.profile.topleft_enabled end,
50 set = function (self, val) LibHotCorners.db.profile.topleft_enabled = not LibHotCorners.db.profile.topleft_enabled; refresh_topleft() end,
51 },
52 }
53 }
54
55 function LibHotCorners:OnInitialize()
56
57 --declarar primeiro o db usando a global que é declarada no toc.
58 self.db = LibStub ("AceDB-3.0"):New ("HotCornersDB", default_db, true)
59
60 --declara agora as opções da tab raiz
61 LibStub("AceConfig-3.0"):RegisterOptionsTable ("HotCorners", OptionsTable)
62 LibHotCorners.OptionsFrame1 = LibStub ("AceConfigDialog-3.0"):AddToBlizOptions ("HotCorners", "HotCorners")
63 --sub tab
64 LibStub ("AceConfig-3.0"):RegisterOptionsTable ("HotCorners-Profiles", LibStub ("AceDBOptions-3.0"):GetOptionsTable (self.db))
65 LibHotCorners.OptionsFrame2 = LibStub ("AceConfigDialog-3.0"):AddToBlizOptions ("HotCorners-Profiles", "Profiles", "HotCorners")
66
67 LibHotCorners ["topleft"].is_enabled = self.db.topleft_enabled
68 refresh_topleft()
69
70 SLASH_HOTCORNER1, SLASH_HOTCORNER2 = "/hotcorners", "/hotcorner"
71 function SlashCmdList.HOTCORNER (msg, editbox)
72 HotCornersOpenOptions (self);
73 end
74
75 for name, dataobj in LBD:DataObjectIterator() do
76 if (dataobj.type and dataobj.icon and dataobj.OnClick) then
77 LibHotCorners:RegisterHotCornerButton (name, "TopLeft", nil, name .. "HotCornerLauncher", dataobj.icon, dataobj.OnTooltipShow, dataobj.OnClick, nil, nil, dataobj.OnEnter, dataobj.OnLeave)
78 end
79 end
80 for k, v in pairs (LBD.attributestorage) do
81 --print (k, v)
82 --print ("----------------")
83 --vardump (v)
84 end
85
86 end
87
88 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
89 --> main function
90
91 LibHotCorners.embeds = LibHotCorners.embeds or {}
92 local embed_functions = {
93 "RegisterHotCornerButton",
94 "HideHotCornerButton",
95 "QuickHotCornerEnable"
96 }
97
98 function LibHotCorners:Embed (target)
99 for k, v in pairs (embed_functions) do
100 target[v] = self[v]
101 end
102 self.embeds [target] = true
103 return target
104 end
105
106 local CallbackHandler = LibStub:GetLibrary ("CallbackHandler-1.0")
107 LibHotCorners.callbacks = LibHotCorners.callbacks or CallbackHandler:New (LibHotCorners)
108
109 LibHotCorners.topleft = LibHotCorners.topleft or {widgets = {}, quickclick = false, is_enabled = false, map = {}}
110 LibHotCorners.bottomleft = {}
111 LibHotCorners.topright = {}
112 LibHotCorners.bottomright = {}
113
114 local function test (corner)
115 assert (corner == "topleft" or corner == "bottomleft" or corner == "topright" or corner == "bottomright", "LibHotCorners:RegisterAddon expects a corner on #1 argument.")
116 end
117
118 function LibHotCorners:RegisterHotCornerButton (name, corner, savedtable, fname, icon, tooltip, clickfunc, menus, quickfunc, onenter, onleave, is_install)
119 corner = string.lower (corner)
120 test (corner)
121
122 if (is_install) then
123 --> overwrite if already exists a widget
124 for i, widget in ipairs (LibHotCorners [corner]) do
125 if (widget.name == name) then
126 table.remove (LibHotCorners [corner], i)
127 table.insert (LibHotCorners [corner], i, {name = name, fname = fname, savedtable = savedtable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave, is_install = true})
128 return LibHotCorners [corner].map [name]
129 end
130 end
131
132 table.insert (LibHotCorners [corner], {name = name, fname = fname, savedtable = savedtable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave, is_install = true})
133 LibHotCorners [corner].map [name] = #LibHotCorners [corner]
134 return LibHotCorners [corner].map [name]
135 else
136 tinsert (LibHotCorners [corner], {name = name, fname = fname, savedtable = savedtable, icon = icon, tooltip = tooltip, click = clickfunc, menus = menus, quickfunc = quickfunc, onenter = onenter, onleave = onleave})
137 LibHotCorners [corner].map [name] = #LibHotCorners [corner]
138 return LibHotCorners [corner].map [name]
139 end
140 end
141
142 function LibHotCorners:QuickHotCornerEnable (name, corner, value)
143
144 corner = string.lower (corner)
145 test (corner)
146
147 local corner_table = LibHotCorners [corner]
148 local addon_table = corner_table [corner_table.map [name]]
149
150 addon_table.savedtable [corner .. "_quickclick"] = value
151
152 if (value and addon_table.quickfunc) then
153 corner_table.quickfunc = addon_table.quickfunc
154 else
155 local got = false
156 for index, button_table in ipairs (corner_table) do
157 if (button_table.savedtable.quickclick) then
158 corner_table.quickfunc = button_table.quickfunc
159 got = true
160 break
161 end
162 end
163
164 if (not got) then
165 corner_table.quickfunc = nil
166 end
167 end
168 end
169
170 function LibHotCorners:HideHotCornerButton (name, corner, value)
171
172 corner = string.lower (corner)
173 test (corner)
174
175 local corner_table = LibHotCorners [corner]
176 local addon_table = corner_table [corner_table.map [name]]
177
178 addon_table.savedtable.hide = value
179
180 --print (LibHotCorners, corner)
181 LibHotCorners [corner].is_enabled = false
182
183 for index, button_table in ipairs (corner_table) do
184 if (not button_table.savedtable.hide) then
185 LibHotCorners [corner].is_enabled = true
186 break
187 end
188 end
189
190 return true
191 end
192
193 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
194 --> data broker stuff
195 function LibHotCorners:DataBrokerCallback (event, name, dataobj)
196 if (not name or not dataobj or not dataobj.type) then
197 return
198 end
199 if (dataobj.icon and dataobj.OnClick) then
200 LibHotCorners:RegisterHotCornerButton (name, "TopLeft", nil, name .. "HotCornerLauncher", dataobj.icon, dataobj.OnTooltipShow, dataobj.OnClick, nil, nil, dataobj.OnEnter, dataobj.OnLeave)
201 end
202 end
203 LBD.RegisterCallback (LibHotCorners, "DataBrokerCallback")
204
205 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
206 --> scripts
207
208 --> background (window mode fix)
209 function HotCornersBackgroundOnEnter (self)
210 if (LibHotCornersTopLeft and LibHotCornersTopLeft:IsShown()) then
211 if (LibHotCornersTopLeft:GetWidth() > 2) then
212 HotCornersOnLeave (LibHotCornersTopLeft)
213 end
214 end
215 self:EnableMouse (false)
216 end
217
218 --> set size
219 local function set_size (self)
220 if (self.position == "topleft" or self.position == "topright") then
221 self:SetSize (40, GetScreenHeight())
222 else
223 self:SetSize (GetScreenWidth(), 40)
224 end
225 end
226
227 --> show tooltip
228 local show_tooltip = function (self)
229 if (self.table.tooltip) then
230 if (type (self.table.tooltip) == "function") then
231 GameTooltip:SetOwner (self, "ANCHOR_RIGHT")
232 self.table.tooltip (GameTooltip)
233 GameTooltip:Show()
234 elseif (type (self.table.tooltip) == "string") then
235 GameTooltip:SetOwner (self, "ANCHOR_RIGHT")
236 GameTooltip:AddLine (self.table.tooltip)
237 GameTooltip:Show()
238 end
239 elseif (self.table.onenter) then
240 self.table.onenter (self)
241 end
242 end
243
244 --> corner frame on enter
245 local more_clicked = function (t1, t2)
246 return t1[1] > t2[1]
247 end
248
249 function HotCornersOnEnter (self)
250
251 if (not LibHotCorners.db.profile.is_enabled) then
252 return
253 end
254
255 if (not LibHotCorners.db.profile [self.position .. "_enabled"]) then
256 return
257 end
258
259 set_size (self)
260
261 HotCornersBackgroundFrame:EnableMouse (true)
262
263 local i = 1
264
265 local sort = {}
266 local clicks = LibHotCorners.db.profile.clicks
267 for index, button_table in ipairs (LibHotCorners [self.position]) do
268 tinsert (sort, {clicks [button_table.name] or 0, button_table})
269 end
270 table.sort (sort, more_clicked)
271
272 local last_button
273
274 local disabled = LibHotCorners.db.profile.disabled
275
276 for index, button_table in ipairs (sort) do
277 button_table = button_table [2]
278 if (not button_table.widget) then
279 LibHotCorners:CreateAddonWidget (self, button_table, index, self.position)
280 end
281
282 button_table.widget:ClearAllPoints()
283
284 if (not disabled [button_table.name]) then
285 if (self.position == "topleft" or self.position == "topright") then
286 local y = i * 35 * -1
287 button_table.widget:SetPoint ("topleft", self, "topleft", 4, y)
288 button_table.widget.y = y
289 else
290 local x = i * 35
291 button_table.widget:SetPoint ("topleft", self, "topleft", x, -4)
292 button_table.widget.x = x
293 end
294
295 button_table.widget:Show()
296 last_button = button_table.widget
297
298 i = i + 1
299 else
300 button_table.widget:Hide()
301 end
302 end
303
304 local OptionsButton = LibHotCorners [self.position].optionsbutton
305 local y = i * 35 * -1
306 OptionsButton:SetPoint ("top", self, "top", 0, y)
307 OptionsButton:Show()
308
309 end
310
311 --> corner frame on leave
312 function HotCornersOnLeave (self)
313 self:SetSize (1, 1)
314 for index, button_table in ipairs (LibHotCorners [self.position]) do
315 button_table.widget:Hide()
316 end
317 local OptionsButton = LibHotCorners [self.position].optionsbutton
318 OptionsButton:Hide()
319 end
320
321 --> quick corner on click
322 function HotCornersOnQuickClick (self, button)
323 local parent_position = self:GetParent().position
324 if (LibHotCorners [parent_position].quickfunc) then
325 LibHotCorners [parent_position].quickfunc (self, button)
326 end
327 end
328
329 --> options button onenter
330 function HotCornersOptionsButtonOnEnter (self)
331 set_size (self:GetParent())
332 for index, button_table in ipairs (LibHotCorners [self:GetParent().position]) do
333 if (not LibHotCorners.db.profile.disabled [button_table.name]) then
334 button_table.widget:Show()
335 end
336 end
337 self:Show()
338 end
339
340 function HotCornersOpenOptions (self)
341 InterfaceOptionsFrame_OpenToCategory ("HotCorners")
342 InterfaceOptionsFrame_OpenToCategory ("HotCorners")
343 end
344
345 function HotCornersSetEnabled (state)
346 LibHotCorners.db.profile.is_enabled = state
347 end
348
349 --> options button onleave
350 function HotCornersOptionsButtonOnLeave (self)
351 self:GetParent():GetScript("OnLeave")(self:GetParent())
352 end
353
354 --> button onenter
355 function HotCornersButtonOnEnter (self)
356 set_size (self:GetParent())
357 for index, button_table in ipairs (LibHotCorners [self:GetParent().position]) do
358 if (not LibHotCorners.db.profile.disabled [button_table.name]) then
359 button_table.widget:Show()
360 end
361 end
362 show_tooltip (self)
363 local OptionsButton = LibHotCorners [self:GetParent().position].optionsbutton
364 OptionsButton:Show()
365 end
366
367 --> button onleave
368 function HotCornersButtonOnLeave (self)
369 GameTooltip:Hide()
370 if (self.table.onleave) then
371 self.table.onleave (self)
372 end
373 self:GetParent():GetScript("OnLeave")(self:GetParent())
374 local OptionsButton = LibHotCorners [self:GetParent().position].optionsbutton
375 OptionsButton:Hide()
376 end
377
378 --> button onmousedown
379 function HotCornersButtonOnMouseDown (self, button)
380 if (self:GetParent().position == "topleft" or self:GetParent().position == "topright") then
381 self:SetPoint ("topleft", self:GetParent(), "topleft", 5, self.y - 1)
382 else
383 self:SetPoint ("topleft", self:GetParent(), "topleft", self.x+1, -6)
384 end
385 end
386
387 --> button onmouseup
388 function HotCornersButtonOnMouseUp (self, button)
389 if (self:GetParent().position == "topleft" or self:GetParent().position == "topright") then
390 self:SetPoint ("topleft", self:GetParent(), "topleft", 4, self.y)
391 else
392 self:SetPoint ("topleft", self:GetParent(), "topleft", self.x, -4)
393 end
394 if (self.table.click) then
395 local clicks = LibHotCorners.db.profile.clicks
396 clicks [self.table.name] = clicks [self.table.name] or 0
397 clicks [self.table.name] = clicks [self.table.name] + 1
398 self.table.click (self, button)
399 end
400 end
401
402 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
403 --> create top left corner
404
405 local TopLeftCorner = CreateFrame ("Frame", "LibHotCornersTopLeft", nil, "HotCornersFrameCornerTemplate")
406 TopLeftCorner:SetPoint ("topleft", UIParent, "topleft", 0, 0)
407 TopLeftCorner.position = "topleft"
408
409 --fast corner button
410 local QuickClickButton = CreateFrame ("button", "LibHotCornersTopLeftFastButton", TopLeftCorner, "HotCornersQuickCornerButtonTemplate")
411
412 --options button
413 local OptionsButton = CreateFrame ("button", "LibHotCornersTopLeftOptionsButton", TopLeftCorner, "HotCornersOptionsButtonTemplate")
414
415 if (debug) then
416 QuickClickButton:SetSize (20, 20)
417 QuickClickButton:SetBackdrop ({bgFile = [[Interface\DialogFrame\UI-DialogBox-Gold-Background]], tile = true, tileSize = 40})
418 QuickClickButton:SetBackdropColor (1, 0, 0, 1)
419 end
420
421 LibHotCorners.topleft.quickbutton = QuickClickButton
422 LibHotCorners.topleft.optionsbutton = OptionsButton
423
424 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
425 --> buttons
426
427 function LibHotCorners:CreateAddonWidget (frame, button_table, index, side)
428
429 --> create the button
430 local button = CreateFrame ("button", "LibHotCorners" .. side .. button_table.fname, frame, "HotCornersButtonTemplate")
431
432 --> write some attributes
433 button.index = index
434 button.table = button_table
435 button.parent = frame
436 button_table.widget = button
437
438 --> set the icon
439 button:SetNormalTexture (button_table.icon)
440 button:SetHighlightTexture (button_table.icon)
441
442 if (string.lower (button_table.icon):find ([[\icons\]])) then
443 button:GetNormalTexture():SetTexCoord (0.078125, 0.9375, 0.078125, 0.9375)
444 button:GetHighlightTexture():SetTexCoord (0.078125, 0.9375, 0.078125, 0.9375)
445 end
446
447 return button
448 end