comparison Libs/DF/button.lua @ 11:2f09fe4be15c

Added an Options Panel.
author Tercio
date Mon, 20 Apr 2015 16:34:18 -0300
parents
children 215f0dd37a6c
comparison
equal deleted inserted replaced
10:f1e32be6773e 11:2f09fe4be15c
1
2 local DF = _G ["DetailsFramework"]
3
4 local _
5 local _rawset = rawset --> lua local
6 local _rawget = rawget --> lua local
7 local _setmetatable = setmetatable --> lua local
8 local _unpack = unpack --> lua local
9 local _type = type --> lua local
10 local _math_floor = math.floor --> lua local
11 local loadstring = loadstring --> lua local
12
13 local cleanfunction = function() end
14 local APIButtonFunctions = false
15 local ButtonMetaFunctions = {}
16
17 ------------------------------------------------------------------------------------------------------------
18 --> metatables
19
20 ButtonMetaFunctions.__call = function (_table, value, ...)
21 return self.func (_table.param1, _table.param2, value, ...)
22 end
23
24 ------------------------------------------------------------------------------------------------------------
25 --> members
26
27 --> tooltip
28 local gmember_tooltip = function (_object)
29 return _object:GetTooltip()
30 end
31 --> shown
32 local gmember_shown = function (_object)
33 return _object:IsShown()
34 end
35 --> frame width
36 local gmember_width = function (_object)
37 return _object.button:GetWidth()
38 end
39 --> frame height
40 local gmember_height = function (_object)
41 return _object.button:GetHeight()
42 end
43 --> text
44 local gmember_text = function (_object)
45 return _object.button.text:GetText()
46 end
47 --> function
48 local gmember_function = function (_object)
49 return _rawget (_object, "func")
50 end
51 --> text color
52 local gmember_textcolor = function (_object)
53 return _object.button.text:GetTextColor()
54 end
55 --> text font
56 local gmember_textfont = function (_object)
57 local fontface = _object.button.text:GetFont()
58 return fontface
59 end
60 --> text size
61 local gmember_textsize = function (_object)
62 local _, fontsize = _object.button.text:GetFont()
63 return fontsize
64 end
65 --> texture
66 local gmember_texture = function (_object)
67 return {_object.button:GetNormalTexture(), _object.button:GetHighlightTexture(), _object.button:GetPushedTexture(), _object.button:GetDisabledTexture()}
68 end
69 --> locked
70 local gmember_locked = function (_object)
71 return _rawget (_object, "is_locked")
72 end
73
74 local get_members_function_index = {
75 ["tooltip"] = gmember_tooltip,
76 ["shown"] = gmember_shown,
77 ["width"] = gmember_width,
78 ["height"] = gmember_height,
79 ["text"] = gmember_text,
80 ["clickfunction"] = gmember_function,
81 ["texture"] = gmember_texture,
82 ["locked"] = gmember_locked,
83 ["fontcolor"] = gmember_textcolor,
84 ["fontface"] = gmember_textfont,
85 ["fontsize"] = gmember_textsize,
86 ["textcolor"] = gmember_textcolor, --alias
87 ["textfont"] = gmember_textfont, --alias
88 ["textsize"] = gmember_textsize --alias
89 }
90
91 ButtonMetaFunctions.__index = function (_table, _member_requested)
92
93 local func = get_members_function_index [_member_requested]
94 if (func) then
95 return func (_table, _member_requested)
96 end
97
98 local fromMe = _rawget (_table, _member_requested)
99 if (fromMe) then
100 return fromMe
101 end
102
103 return ButtonMetaFunctions [_member_requested]
104 end
105
106 -------------------------------------------------------------------------------------------------------------------------------------------------------------------
107
108 --> tooltip
109 local smember_tooltip = function (_object, _value)
110 return _object:SetTooltip (_value)
111 end
112 --> show
113 local smember_show = function (_object, _value)
114 if (_value) then
115 return _object:Show()
116 else
117 return _object:Hide()
118 end
119 end
120 --> hide
121 local smember_hide = function (_object, _value)
122 if (not _value) then
123 return _object:Show()
124 else
125 return _object:Hide()
126 end
127 end
128 --> frame width
129 local smember_width = function (_object, _value)
130 return _object.button:SetWidth (_value)
131 end
132 --> frame height
133 local smember_height = function (_object, _value)
134 return _object.button:SetHeight (_value)
135 end
136 --> text
137 local smember_text = function (_object, _value)
138 return _object.button.text:SetText (_value)
139 end
140 --> function
141 local smember_function = function (_object, _value)
142 return _rawset (_object, "func", _value)
143 end
144 --> text color
145 local smember_textcolor = function (_object, _value)
146 local _value1, _value2, _value3, _value4 = DF:ParseColors (_value)
147 return _object.button.text:SetTextColor (_value1, _value2, _value3, _value4)
148 end
149 --> text font
150 local smember_textfont = function (_object, _value)
151 return DF:SetFontFace (_object.button.text, _value)
152 end
153 --> text size
154 local smember_textsize = function (_object, _value)
155 return DF:SetFontSize (_object.button.text, _value)
156 end
157 --> texture
158 local smember_texture = function (_object, _value)
159 if (_type (_value) == "table") then
160 local _value1, _value2, _value3, _value4 = unpack (_value)
161 if (_value1) then
162 _object.button:SetNormalTexture (_value1)
163 end
164 if (_value2) then
165 _object.button:SetHighlightTexture (_value2, "ADD")
166 end
167 if (_value3) then
168 _object.button:SetPushedTexture (_value3)
169 end
170 if (_value4) then
171 _object.button:SetDisabledTexture (_value4)
172 end
173 else
174 _object.button:SetNormalTexture (_value)
175 _object.button:SetHighlightTexture (_value, "ADD")
176 _object.button:SetPushedTexture (_value)
177 _object.button:SetDisabledTexture (_value)
178 end
179 return
180 end
181 --> locked
182 local smember_locked = function (_object, _value)
183 if (_value) then
184 _object.button:SetMovable (false)
185 return _rawset (_object, "is_locked", true)
186 else
187 _object.button:SetMovable (true)
188 _rawset (_object, "is_locked", false)
189 return
190 end
191 end
192 --> text align
193 local smember_textalign = function (_object, _value)
194 if (_value == "left" or _value == "<") then
195 _object.button.text:SetPoint ("left", _object.button, "left", 2, 0)
196 _object.capsule_textalign = "left"
197 elseif (_value == "center" or _value == "|") then
198 _object.button.text:SetPoint ("center", _object.button, "center", 0, 0)
199 _object.capsule_textalign = "center"
200 elseif (_value == "right" or _value == ">") then
201 _object.button.text:SetPoint ("right", _object.button, "right", -2, 0)
202 _object.capsule_textalign = "right"
203 end
204 end
205
206 local set_members_function_index = {
207 ["tooltip"] = smember_tooltip,
208 ["show"] = smember_show,
209 ["hide"] = smember_hide,
210 ["width"] = smember_width,
211 ["height"] = smember_height,
212 ["text"] = smember_text,
213 ["clickfunction"] = smember_function,
214 ["textcolor"] = smember_textcolor,
215 ["textfont"] = smember_textfont,
216 ["textsize"] = smember_textsize,
217 ["texture"] = smember_texture,
218 ["locked"] = smember_locked,
219 ["textalign"] = smember_textalign,
220 }
221
222 ButtonMetaFunctions.__newindex = function (_table, _key, _value)
223 local func = set_members_function_index [_key]
224 if (func) then
225 return func (_table, _value)
226 else
227 return _rawset (_table, _key, _value)
228 end
229 end
230
231 ------------------------------------------------------------------------------------------------------------
232 --> methods
233
234 --> show & hide
235 function ButtonMetaFunctions:IsShown()
236 return self.button:IsShown()
237 end
238 function ButtonMetaFunctions:Show()
239 return self.button:Show()
240 end
241 function ButtonMetaFunctions:Hide()
242 return self.button:Hide()
243 end
244
245 -- setpoint
246 function ButtonMetaFunctions:SetPoint (v1, v2, v3, v4, v5)
247 v1, v2, v3, v4, v5 = DF:CheckPoints (v1, v2, v3, v4, v5, self)
248 if (not v1) then
249 error ("SetPoint: Invalid parameter.")
250 return
251 end
252 return self.widget:SetPoint (v1, v2, v3, v4, v5)
253 end
254
255 -- sizes
256 function ButtonMetaFunctions:SetSize (w, h)
257 if (w) then
258 self.button:SetWidth (w)
259 end
260 if (h) then
261 return self.button:SetHeight (h)
262 end
263 end
264
265 -- tooltip
266 function ButtonMetaFunctions:SetTooltip (tooltip)
267 if (tooltip) then
268 return _rawset (self, "have_tooltip", tooltip)
269 else
270 return _rawset (self, "have_tooltip", nil)
271 end
272 end
273 function ButtonMetaFunctions:GetTooltip()
274 return _rawget (self, "have_tooltip")
275 end
276
277 -- functions
278 function ButtonMetaFunctions:SetClickFunction (func, param1, param2, clicktype)
279 if (not clicktype or string.find (string.lower (clicktype), "left")) then
280 if (func) then
281 _rawset (self, "func", func)
282 else
283 _rawset (self, "func", cleanfunction)
284 end
285
286 if (param1 ~= nil) then
287 _rawset (self, "param1", param1)
288 end
289 if (param2 ~= nil) then
290 _rawset (self, "param2", param2)
291 end
292
293 elseif (clicktype or string.find (string.lower (clicktype), "right")) then
294 if (func) then
295 _rawset (self, "funcright", func)
296 else
297 _rawset (self, "funcright", cleanfunction)
298 end
299 end
300 end
301
302 -- text
303 function ButtonMetaFunctions:SetText (text)
304 if (text) then
305 self.button.text:SetText (text)
306 else
307 self.button.text:SetText (nil)
308 end
309 end
310
311 -- textcolor
312 function ButtonMetaFunctions:SetTextColor (color, arg2, arg3, arg4)
313 if (arg2) then
314 return self.button.text:SetTextColor (color, arg2, arg3, arg4 or 1)
315 end
316 local _value1, _value2, _value3, _value4 = DF:ParseColors (color)
317 return self.button.text:SetTextColor (_value1, _value2, _value3, _value4)
318 end
319
320 -- textsize
321 function ButtonMetaFunctions:SetTextSize (size)
322 return DF:SetFontSize (self.button.text, _value)
323 end
324
325 -- textfont
326 function ButtonMetaFunctions:SetTextFont (font)
327 return DF:SetFontFace (_object.button.text, _value)
328 end
329
330 -- textures
331 function ButtonMetaFunctions:SetTexture (normal, highlight, pressed, disabled)
332 if (normal) then
333 self.button:SetNormalTexture (normal)
334 elseif (_type (normal) ~= "boolean") then
335 self.button:SetNormalTexture (nil)
336 end
337
338 if (_type (highlight) == "boolean") then
339 if (highlight and normal and _type (normal) ~= "boolean") then
340 self.button:SetHighlightTexture (normal, "ADD")
341 end
342 elseif (highlight == nil) then
343 self.button:SetHighlightTexture (nil)
344 else
345 self.button:SetHighlightTexture (highlight, "ADD")
346 end
347
348 if (_type (pressed) == "boolean") then
349 if (pressed and normal and _type (normal) ~= "boolean") then
350 self.button:SetPushedTexture (normal)
351 end
352 elseif (pressed == nil) then
353 self.button:SetPushedTexture (nil)
354 else
355 self.button:SetPushedTexture (pressed, "ADD")
356 end
357
358 if (_type (disabled) == "boolean") then
359 if (disabled and normal and _type (normal) ~= "boolean") then
360 self.button:SetDisabledTexture (normal)
361 end
362 elseif (disabled == nil) then
363 self.button:SetDisabledTexture (nil)
364 else
365 self.button:SetDisabledTexture (disabled, "ADD")
366 end
367
368 end
369
370 -- frame levels
371 function ButtonMetaFunctions:GetFrameLevel()
372 return self.button:GetFrameLevel()
373 end
374 function ButtonMetaFunctions:SetFrameLevel (level, frame)
375 if (not frame) then
376 return self.button:SetFrameLevel (level)
377 else
378 local framelevel = frame:GetFrameLevel (frame) + level
379 return self.button:SetFrameLevel (framelevel)
380 end
381 end
382
383 -- icon
384 function ButtonMetaFunctions:SetIcon (texture, width, height, layout, texcoord, overlay, textdistance, leftpadding)
385 if (not self.icon) then
386 self.icon = self:CreateTexture (nil, "artwork")
387 self.icon:SetSize (self.height*0.8, self.height*0.8)
388 self.icon:SetPoint ("left", self.widget, "left", 4 + (leftpadding or 0), 0)
389 self.icon.leftpadding = leftpadding or 0
390 self.widget.text:ClearAllPoints()
391 self.widget.text:SetPoint ("left", self.icon, "right", textdistance or 2, 0)
392 end
393
394 self.icon:SetTexture (texture)
395 self.icon:SetSize (width or self.height*0.8, height or self.height*0.8)
396 self.icon:SetDrawLayer (layout or "artwork")
397 if (texcoord) then
398 self.icon:SetTexCoord (unpack (texcoord))
399 else
400 self.icon:SetTexCoord (0, 1, 0, 1)
401 end
402 if (overlay) then
403 if (type (overlay) == "string") then
404 local r, g, b, a = DF:ParseColors (overlay)
405 self.icon:SetVertexColor (r, g, b, a)
406 else
407 self.icon:SetVertexColor (unpack (overlay))
408 end
409 else
410 self.icon:SetVertexColor (1, 1, 1, 1)
411 end
412
413 local w = self.button:GetWidth()
414 local iconw = self.icon:GetWidth()
415 local text_width = self.button.text:GetStringWidth()
416 if (text_width > w-15-iconw) then
417 if (not short_method) then
418 local new_width = text_width+15+iconw
419 self.button:SetWidth (new_width)
420 elseif (short_method == 1) then
421 local loop = true
422 local textsize = 11
423 while (loop) do
424 if (text_width+15+iconw < w or textsize < 8) then
425 loop = false
426 break
427 else
428 DF:SetFontSize (self.button.text, textsize)
429 text_width = self.button.text:GetStringWidth()
430 textsize = textsize - 1
431 end
432 end
433 end
434 end
435
436 end
437
438 -- frame stratas
439 function ButtonMetaFunctions:SetFrameStrata()
440 return self.button:GetFrameStrata()
441 end
442 function ButtonMetaFunctions:SetFrameStrata (strata)
443 if (_type (strata) == "table") then
444 self.button:SetFrameStrata (strata:GetFrameStrata())
445 else
446 self.button:SetFrameStrata (strata)
447 end
448 end
449
450 -- enabled
451 function ButtonMetaFunctions:IsEnabled()
452 return self.button:IsEnabled()
453 end
454 function ButtonMetaFunctions:Enable()
455 return self.button:Enable()
456 end
457 function ButtonMetaFunctions:Disable()
458 return self.button:Disable()
459 end
460
461 -- exec
462 function ButtonMetaFunctions:Exec()
463 return self.func (self.param1, self.param2)
464 end
465 function ButtonMetaFunctions:Click()
466 return self.func (self.param1, self.param2)
467 end
468 function ButtonMetaFunctions:RightClick()
469 return self.funcright()
470 end
471
472 --> hooks
473 function ButtonMetaFunctions:SetHook (hookType, func)
474 if (func) then
475 _rawset (self, hookType.."Hook", func)
476 else
477 _rawset (self, hookType.."Hook", nil)
478 end
479 end
480
481 --> custom textures
482 function ButtonMetaFunctions:InstallCustomTexture (texture, rect, coords, use_split, side_textures, side_textures2)
483
484 self.button:SetNormalTexture (nil)
485 self.button:SetPushedTexture (nil)
486 self.button:SetDisabledTexture (nil)
487 self.button:SetHighlightTexture (nil)
488
489 local button = self.button
490
491 if (use_split) then
492
493 --> 4 corners
494 button.textureTopLeft = button:CreateTexture (nil, "artwork"); button.textureTopLeft:SetSize (8, 8); button.textureTopLeft:SetPoint ("topleft", button)
495 button.textureTopRight = button:CreateTexture (nil, "artwork"); button.textureTopRight:SetSize (8, 8); button.textureTopRight:SetPoint ("topright", button)
496 button.textureBottomLeft = button:CreateTexture (nil, "artwork"); button.textureBottomLeft:SetSize (8, 8); button.textureBottomLeft:SetPoint ("bottomleft", button)
497 button.textureBottomRight = button:CreateTexture (nil, "artwork"); button.textureBottomRight:SetSize (8, 8); button.textureBottomRight:SetPoint ("bottomright", button)
498
499 button.textureLeft = button:CreateTexture (nil, "artwork"); button.textureLeft:SetWidth (4); button.textureLeft:SetPoint ("topleft", button.textureTopLeft, "bottomleft"); button.textureLeft:SetPoint ("bottomleft", button.textureBottomLeft, "topleft")
500 button.textureRight = button:CreateTexture (nil, "artwork"); button.textureRight:SetWidth (4); button.textureRight:SetPoint ("topright", button.textureTopRight, "bottomright"); button.textureRight:SetPoint ("bottomright", button.textureBottomRight, "topright")
501 button.textureTop = button:CreateTexture (nil, "artwork"); button.textureTop:SetHeight (4); button.textureTop:SetPoint ("topleft", button.textureTopLeft, "topright"); button.textureTop:SetPoint ("topright", button.textureTopRight, "topleft");
502 button.textureBottom = button:CreateTexture (nil, "artwork"); button.textureBottom:SetHeight (4); button.textureBottom:SetPoint ("bottomleft", button.textureBottomLeft, "bottomright"); button.textureBottom:SetPoint ("bottomright", button.textureBottomRight, "bottomleft");
503
504 button.textureLeft:SetTexCoord (0, 4/128, 9/128, 24/128)
505 button.textureRight:SetTexCoord (124/128, 1, 9/128, 24/128)
506 button.textureTop:SetTexCoord (9/128, 120/128, 0, 4/128)
507 button.textureBottom:SetTexCoord (9/128, 119/128, 28/128, 32/128)
508
509 button.textureTopLeft:SetTexCoord (0, 8/128, 0, 8/128)
510 button.textureTopRight:SetTexCoord (121/128, 1, 0, 8/128)
511 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128)
512 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128)
513
514 button.textureTopLeft:SetTexture ([[Interface\AddOns\Details\images\default_button]])
515 button.textureTopRight:SetTexture ([[Interface\AddOns\Details\images\default_button]])
516 button.textureBottomLeft:SetTexture ([[Interface\AddOns\Details\images\default_button]])
517 button.textureBottomRight:SetTexture ([[Interface\AddOns\Details\images\default_button]])
518 button.textureLeft:SetTexture ([[Interface\AddOns\Details\images\default_button]])
519 button.textureRight:SetTexture ([[Interface\AddOns\Details\images\default_button]])
520 button.textureTop:SetTexture ([[Interface\AddOns\Details\images\default_button]])
521 button.textureBottom:SetTexture ([[Interface\AddOns\Details\images\default_button]])
522
523 else
524 texture = texture or "Interface\\AddOns\\Details\\images\\default_button"
525 self.button.texture = self.button:CreateTexture (nil, "artwork")
526
527 if (not rect) then
528 self.button.texture:SetAllPoints (self.button)
529 else
530 self.button.texture:SetPoint ("topleft", self.button, "topleft", rect.x1, rect.y1)
531 self.button.texture:SetPoint ("bottomright", self.button, "bottomright", rect.x2, rect.y2)
532 end
533
534 if (coords) then
535 self.button.texture.coords = coords
536 self.button.texture:SetTexCoord (_unpack (coords.Normal))
537 else
538 self.button.texture:SetTexCoord (0, 1, 0, 0.24609375)
539 end
540
541 self.button.texture:SetTexture (texture)
542 end
543
544 if (side_textures) then
545 local left = self.button:CreateTexture (nil, "overlay")
546 left:SetTexture ([[Interface\TALENTFRAME\talent-main]])
547 left:SetTexCoord (0.13671875, 0.25, 0.486328125, 0.576171875)
548 left:SetPoint ("left", self.button, 0, 0)
549 left:SetWidth (10)
550 left:SetHeight (self.button:GetHeight()+2)
551 self.button.left_border = left
552
553 local right = self.button:CreateTexture (nil, "overlay")
554 right:SetTexture ([[Interface\TALENTFRAME\talent-main]])
555 right:SetTexCoord (0.01953125, 0.13671875, 0.486328125, 0.576171875)
556 right:SetPoint ("right", self.button, 0, 0)
557 right:SetWidth (10)
558 right:SetHeight (self.button:GetHeight()+2)
559 self.button.right_border = right
560
561 elseif (side_textures2) then
562
563 local left = self.button:CreateTexture (nil, "overlay")
564 left:SetTexture ([[Interface\AddOns\Details\images\icons]])
565 left:SetTexCoord (94/512, 123/512, 42/512, 87/512)
566 left:SetPoint ("left", self.button, 0, 0)
567 left:SetWidth (10)
568 left:SetHeight (self.button:GetHeight()+2)
569 self.button.left_border = left
570
571 local right = self.button:CreateTexture (nil, "overlay")
572 right:SetTexture ([[Interface\AddOns\Details\images\icons]])
573 right:SetTexCoord (65/512, 94/512, 42/512, 87/512)
574 right:SetPoint ("right", self.button, 0, 0)
575 right:SetWidth (10)
576 right:SetHeight (self.button:GetHeight()+2)
577 self.button.right_border = right
578 end
579 end
580
581 ------------------------------------------------------------------------------------------------------------
582 --> scripts
583
584 local OnEnter = function (button)
585
586 if (button.textureTopLeft) then
587 button.textureLeft:SetTexCoord (0, 4/128, 40/128, 56/128)
588 button.textureRight:SetTexCoord (124/128, 1, 40/128, 56/128)
589 button.textureTop:SetTexCoord (9/128, 120/128, 33/128, 37/128)
590 button.textureBottom:SetTexCoord (9/128, 119/128, 60/128, 64/128)
591
592 button.textureTopLeft:SetTexCoord (0, 8/128, 33/128, 40/128)
593 button.textureTopRight:SetTexCoord (121/128, 1, 33/128, 40/128)
594 button.textureBottomLeft:SetTexCoord (0, 8/128, 56/128, 64/128)
595 button.textureBottomRight:SetTexCoord (120/128, 1, 56/128, 64/128)
596 end
597
598 if (button.MyObject.OnEnterHook) then
599 local interrupt = button.MyObject.OnEnterHook (button, button.MyObject)
600 if (interrupt) then
601 return
602 end
603 end
604
605 button.MyObject.is_mouse_over = true
606
607 if (button.texture) then
608 if (button.texture.coords) then
609 button.texture:SetTexCoord (_unpack (button.texture.coords.Highlight))
610 else
611 button.texture:SetTexCoord (0, 1, 0.24609375, 0.49609375)
612 end
613 end
614
615 if (button.MyObject.have_tooltip) then
616 GameCooltip2:Preset (2)
617 if (type (button.MyObject.have_tooltip) == "function") then
618 GameCooltip2:AddLine (button.MyObject.have_tooltip() or "")
619 else
620 GameCooltip2:AddLine (button.MyObject.have_tooltip)
621 end
622 GameCooltip2:ShowCooltip (button, "tooltip")
623 end
624
625 local parent = button:GetParent().MyObject
626 if (parent and parent.type == "panel") then
627 if (parent.GradientEnabled) then
628 parent:RunGradient()
629 end
630 end
631 end
632
633 local OnLeave = function (button)
634
635 if (button.textureLeft and not button.MyObject.is_mouse_down) then
636 button.textureLeft:SetTexCoord (0, 4/128, 9/128, 24/128)
637 button.textureRight:SetTexCoord (124/128, 1, 9/128, 24/128)
638 button.textureTop:SetTexCoord (9/128, 120/128, 0, 4/128)
639 button.textureBottom:SetTexCoord (9/128, 119/128, 28/128, 32/128)
640
641 button.textureTopLeft:SetTexCoord (0, 8/128, 0, 8/128)
642 button.textureTopRight:SetTexCoord (121/128, 1, 0, 8/128)
643 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128)
644 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128)
645 end
646
647 if (button.MyObject.OnLeaveHook) then
648 local interrupt = button.MyObject.OnLeaveHook (button, button.MyObject)
649 if (interrupt) then
650 return
651 end
652 end
653
654 button.MyObject.is_mouse_over = false
655
656 if (button.texture and not button.MyObject.is_mouse_down) then
657 if (button.texture.coords) then
658 button.texture:SetTexCoord (_unpack (button.texture.coords.Normal))
659 else
660 button.texture:SetTexCoord (0, 1, 0, 0.24609375)
661 end
662 end
663
664 if (button.MyObject.have_tooltip) then
665 if (GameCooltip2:GetText (1) == button.MyObject.have_tooltip or type (button.MyObject.have_tooltip) == "function") then
666 GameCooltip2:Hide()
667 end
668 end
669
670 local parent = button:GetParent().MyObject
671 if (parent and parent.type == "panel") then
672 if (parent.GradientEnabled) then
673 parent:RunGradient (false)
674 end
675 end
676 end
677
678 local OnHide = function (button)
679 if (button.MyObject.OnHideHook) then
680 local interrupt = button.MyObject.OnHideHook (button, button.MyObject)
681 if (interrupt) then
682 return
683 end
684 end
685 end
686
687 local OnShow = function (button)
688 if (button.MyObject.OnShowHook) then
689 local interrupt = button.MyObject.OnShowHook (button, button.MyObject)
690 if (interrupt) then
691 return
692 end
693 end
694 end
695
696 local OnMouseDown = function (button, buttontype)
697 if (not button:IsEnabled()) then
698 return
699 end
700
701 if (button.textureTopLeft) then
702 button.textureLeft:SetTexCoord (0, 4/128, 72/128, 88/128)
703 button.textureRight:SetTexCoord (124/128, 1, 72/128, 88/128)
704 button.textureTop:SetTexCoord (9/128, 120/128, 65/128, 68/128)
705 button.textureBottom:SetTexCoord (9/128, 119/128, 92/128, 96/128)
706
707 button.textureTopLeft:SetTexCoord (0, 8/128, 65/128, 71/128)
708 button.textureTopRight:SetTexCoord (121/128, 1, 65/128, 71/128)
709 button.textureBottomLeft:SetTexCoord (0, 8/128, 88/128, 96/128)
710 button.textureBottomRight:SetTexCoord (120/128, 1, 88/128, 96/128)
711 end
712
713 if (button.MyObject.OnMouseDownHook) then
714 local interrupt = button.MyObject.OnMouseDownHook (button, buttontype, button.MyObject)
715 if (interrupt) then
716 return
717 end
718 end
719
720 button.MyObject.is_mouse_down = true
721
722 if (button.texture) then
723 if (button.texture.coords) then
724 button.texture:SetTexCoord (_unpack (button.texture.coords.Pushed))
725 else
726 button.texture:SetTexCoord (0, 1, 0.5078125, 0.75)
727 end
728 end
729
730 if (button.MyObject.capsule_textalign) then
731 if (button.MyObject.icon) then
732 button.MyObject.icon:SetPoint ("left", button, "left", 5 + (button.MyObject.icon.leftpadding or 0), -1)
733 elseif (button.MyObject.capsule_textalign == "left") then
734 button.text:SetPoint ("left", button, "left", 3, -1)
735 elseif (button.MyObject.capsule_textalign == "center") then
736 button.text:SetPoint ("center", button, "center", 1, -1)
737 elseif (button.MyObject.capsule_textalign == "right") then
738 button.text:SetPoint ("right", button, "right", -1, -1)
739 end
740 else
741 if (button.MyObject.icon) then
742 button.MyObject.icon:SetPoint ("left", button, "left", 7 + (button.MyObject.icon.leftpadding or 0), -2)
743 else
744 button.text:SetPoint ("center", button,"center", 1, -1)
745 end
746 end
747
748 button.mouse_down = GetTime()
749 local x, y = GetCursorPosition()
750 button.x = _math_floor (x)
751 button.y = _math_floor (y)
752
753 if (not button.MyObject.container.isLocked and button.MyObject.container:IsMovable()) then
754 if (not button.isLocked and button:IsMovable()) then
755 button.MyObject.container.isMoving = true
756 button.MyObject.container:StartMoving()
757 end
758 end
759
760 if (button.MyObject.options.OnGrab) then
761 if (_type (button.MyObject.options.OnGrab) == "string" and button.MyObject.options.OnGrab == "PassClick") then
762 if (buttontype == "LeftButton") then
763 button.MyObject.func (button.MyObject.param1, button.MyObject.param2)
764 else
765 button.MyObject.funcright (button.MyObject.param1, button.MyObject.param2)
766 end
767 end
768 end
769 end
770
771 local OnMouseUp = function (button, buttontype)
772 if (not button:IsEnabled()) then
773 return
774 end
775
776 if (button.textureLeft) then
777 if (button.MyObject.is_mouse_over) then
778 button.textureLeft:SetTexCoord (0, 4/128, 40/128, 56/128)
779 button.textureRight:SetTexCoord (124/128, 1, 40/128, 56/128)
780 button.textureTop:SetTexCoord (9/128, 120/128, 33/128, 37/128)
781 button.textureBottom:SetTexCoord (9/128, 119/128, 60/128, 64/128)
782
783 button.textureTopLeft:SetTexCoord (0, 8/128, 33/128, 40/128)
784 button.textureTopRight:SetTexCoord (121/128, 1, 33/128, 40/128)
785 button.textureBottomLeft:SetTexCoord (0, 8/128, 56/128, 64/128)
786 button.textureBottomRight:SetTexCoord (120/128, 1, 56/128, 64/128)
787 else
788 button.textureLeft:SetTexCoord (0, 4/128, 9/128, 24/128)
789 button.textureRight:SetTexCoord (124/128, 1, 9/128, 24/128)
790 button.textureTop:SetTexCoord (9/128, 120/128, 0, 4/128)
791 button.textureBottom:SetTexCoord (9/128, 119/128, 28/128, 32/128)
792
793 button.textureTopLeft:SetTexCoord (0, 8/128, 0, 8/128)
794 button.textureTopRight:SetTexCoord (121/128, 1, 0, 8/128)
795 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128)
796 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128)
797 end
798 end
799
800 if (button.MyObject.OnMouseUpHook) then
801 local interrupt = button.MyObject.OnMouseUpHook (button, buttontype, button.MyObject)
802 if (interrupt) then
803 return
804 end
805 end
806
807 button.MyObject.is_mouse_down = false
808
809 if (button.texture) then
810 if (button.texture.coords) then
811 if (button.MyObject.is_mouse_over) then
812 button.texture:SetTexCoord (_unpack (button.texture.coords.Highlight))
813 else
814 button.texture:SetTexCoord (_unpack (coords.Normal))
815 end
816 else
817 if (button.MyObject.is_mouse_over) then
818 button.texture:SetTexCoord (0, 1, 0.24609375, 0.49609375)
819 else
820 button.texture:SetTexCoord (0, 1, 0, 0.24609375)
821 end
822 end
823 end
824
825 if (button.MyObject.capsule_textalign) then
826 if (button.MyObject.icon) then
827 button.MyObject.icon:SetPoint ("left", button, "left", 4 + (button.MyObject.icon.leftpadding or 0), 0)
828 elseif (button.MyObject.capsule_textalign == "left") then
829 button.text:SetPoint ("left", button, "left", 2, 0)
830 elseif (button.MyObject.capsule_textalign == "center") then
831 button.text:SetPoint ("center", button, "center", 0, 0)
832 elseif (button.MyObject.capsule_textalign == "right") then
833 button.text:SetPoint ("right", button, "right", -2, 0)
834 end
835 else
836 if (button.MyObject.icon) then
837 button.MyObject.icon:SetPoint ("left", button, "left", 4 + (button.MyObject.icon.leftpadding or 0), 0)
838 else
839 button.text:SetPoint ("center", button,"center", 0, 0)
840 end
841 end
842
843 if (button.MyObject.container.isMoving) then
844 button.MyObject.container:StopMovingOrSizing()
845 button.MyObject.container.isMoving = false
846 end
847
848 local x, y = GetCursorPosition()
849 x = _math_floor (x)
850 y = _math_floor (y)
851 if ((button.mouse_down+0.4 > GetTime() and (x == button.x and y == button.y)) or (x == button.x and y == button.y)) then
852 if (buttontype == "LeftButton") then
853 button.MyObject.func (button, buttontype, button.MyObject.param1, button.MyObject.param2)
854 else
855 button.MyObject.funcright (button, buttontype, button.MyObject.param1, button.MyObject.param2)
856 end
857 end
858 end
859
860 ------------------------------------------------------------------------------------------------------------
861 --> object constructor
862
863 function DF:CreateButton (parent, func, w, h, text, param1, param2, texture, member, name, short_method)
864 return DF:NewButton (parent, parent, name, member, w, h, func, param1, param2, texture, text, short_method)
865 end
866
867 function DF:NewButton (parent, container, name, member, w, h, func, param1, param2, texture, text, short_method)
868
869 if (not name) then
870 name = "DetailsFrameworkButtonNumber" .. DF.ButtonCounter
871 DF.ButtonCounter = DF.ButtonCounter + 1
872
873 elseif (not parent) then
874 return nil
875 end
876 if (not container) then
877 container = parent
878 end
879
880 if (name:find ("$parent")) then
881 name = name:gsub ("$parent", parent:GetName())
882 end
883
884
885 local ButtonObject = {type = "button", dframework = true}
886
887 if (member) then
888 parent [member] = ButtonObject
889 end
890
891 if (parent.dframework) then
892 parent = parent.widget
893 end
894 if (container.dframework) then
895 container = container.widget
896 end
897
898 --> default members:
899 --> hooks
900 ButtonObject.OnEnterHook = nil
901 ButtonObject.OnLeaveHook = nil
902 ButtonObject.OnHideHook = nil
903 ButtonObject.OnShowHook = nil
904 ButtonObject.OnMouseDownHook = nil
905 ButtonObject.OnMouseUpHook = nil
906 --> misc
907 ButtonObject.is_locked = true
908 ButtonObject.container = container
909 ButtonObject.have_tooltip = nil
910 ButtonObject.options = {OnGrab = false}
911
912
913 ButtonObject.button = CreateFrame ("button", name, parent, "DetailsFrameworkButtonTemplate")
914 ButtonObject.widget = ButtonObject.button
915
916 ButtonObject.button:SetBackdrop ({bgFile = DF.folder .. "background", tileSize = 64, edgeFile = DF.folder .. "border_2", edgeSize = 10, insets = {left = 1, right = 1, top = 1, bottom = 1}})
917 ButtonObject.button:SetBackdropColor (0, 0, 0, 0.4)
918 ButtonObject.button:SetBackdropBorderColor (1, 1, 1, 1)
919
920 if (not APIButtonFunctions) then
921 APIButtonFunctions = true
922 local idx = getmetatable (ButtonObject.button).__index
923 for funcName, funcAddress in pairs (idx) do
924 if (not ButtonMetaFunctions [funcName]) then
925 ButtonMetaFunctions [funcName] = function (object, ...)
926 local x = loadstring ( "return _G."..object.button:GetName()..":"..funcName.."(...)")
927 return x (...)
928 end
929 end
930 end
931 end
932
933 ButtonObject.button:SetWidth (w or 100)
934 ButtonObject.button:SetHeight (h or 20)
935 ButtonObject.button.MyObject = ButtonObject
936
937 ButtonObject.text_overlay = _G [name .. "_Text"]
938 ButtonObject.disabled_overlay = _G [name .. "_TextureDisabled"]
939
940 ButtonObject.button:SetNormalTexture (texture)
941 ButtonObject.button:SetPushedTexture (texture)
942 ButtonObject.button:SetDisabledTexture (texture)
943 ButtonObject.button:SetHighlightTexture (texture, "ADD")
944
945 ButtonObject.button.text:SetText (text)
946 ButtonObject.button.text:SetPoint ("center", ButtonObject.button, "center")
947
948 local text_width = ButtonObject.button.text:GetStringWidth()
949 if (text_width > w-15 and ButtonObject.button.text:GetText() ~= "") then
950 if (not short_method) then
951 local new_width = text_width+15
952 ButtonObject.button:SetWidth (new_width)
953 elseif (short_method == 1) then
954 local loop = true
955 local textsize = 11
956 while (loop) do
957 if (text_width+15 < w or textsize < 8) then
958 loop = false
959 break
960 else
961 DF:SetFontSize (ButtonObject.button.text, textsize)
962 text_width = ButtonObject.button.text:GetStringWidth()
963 textsize = textsize - 1
964 end
965 end
966 end
967 end
968
969 ButtonObject.func = func or cleanfunction
970 ButtonObject.funcright = cleanfunction
971 ButtonObject.param1 = param1
972 ButtonObject.param2 = param2
973
974 ButtonObject.short_method = short_method
975
976 --> hooks
977 ButtonObject.button:SetScript ("OnEnter", OnEnter)
978 ButtonObject.button:SetScript ("OnLeave", OnLeave)
979 ButtonObject.button:SetScript ("OnHide", OnHide)
980 ButtonObject.button:SetScript ("OnShow", OnShow)
981 ButtonObject.button:SetScript ("OnMouseDown", OnMouseDown)
982 ButtonObject.button:SetScript ("OnMouseUp", OnMouseUp)
983
984 _setmetatable (ButtonObject, ButtonMetaFunctions)
985
986 return ButtonObject
987
988 end
989
990 local pickcolor_callback = function (self, r, g, b, a, button)
991 button.MyObject.color_texture:SetVertexColor (r, g, b, a)
992 button.MyObject:color_callback (r, g, b, a)
993 end
994 local pickcolor = function (alpha, param2, self)
995 local r, g, b, a = self.MyObject.color_texture:GetVertexColor()
996 DF:ColorPick (self, r, g, b, a, pickcolor_callback)
997 end
998
999 local color_button_height = 16
1000 local color_button_width = 16
1001
1002 local set_colorpick_color = function (button, r, g, b, a)
1003 button.color_texture:SetVertexColor (r, g, b, a)
1004 end
1005
1006 local colorpick_cancel = function (self)
1007 ColorPickerFrame:Hide()
1008 end
1009
1010 function DF:CreateColorPickButton (parent, name, member, callback, alpha)
1011 return DF:NewColorPickButton (parent, name, member, callback, alpha)
1012 end
1013
1014 function DF:NewColorPickButton (parent, name, member, callback, alpha)
1015
1016 --button
1017 local button = DF:NewButton (parent, _, name, member, color_button_width, color_button_height, pickcolor, alpha, "param2")
1018 button:InstallCustomTexture()
1019 button.color_callback = callback
1020 button.Cancel = colorpick_cancel
1021 button.SetColor = set_colorpick_color
1022
1023 button:SetBackdrop ({edgeFile = [[Interface\Tooltips\UI-Tooltip-Border]], edgeSize = 6,
1024 bgFile = [[Interface\DialogFrame\UI-DialogBox-Background]], insets = {left = 0, right = 0, top = 0, bottom = 0}})
1025
1026 --textura do fundo
1027 local background = DF:NewImage (button, nil, color_button_width, color_button_height, nil, nil, nil, "$parentBck")
1028 --background:SetTexture ([[Interface\AddOns\Details\images\icons]])
1029 background:SetPoint ("topleft", button.widget, "topleft", 1, -2)
1030 background:SetPoint ("bottomright", button.widget, "bottomright", -1, 1)
1031 background:SetTexCoord (0.337890625, 0.390625, 0.625, 0.658203125)
1032 background:SetDrawLayer ("background", 1)
1033
1034 --textura da cor
1035 local img = DF:NewImage (button, nil, color_button_width, color_button_height, nil, nil, "color_texture", "$parentTex")
1036 img:SetTexture (1, 1, 1)
1037 img:SetPoint ("topleft", button.widget, "topleft", 1, -2)
1038 img:SetPoint ("bottomright", button.widget, "bottomright", -1, 1)
1039 img:SetDrawLayer ("background", 2)
1040
1041 return button
1042
1043 end