comparison Libs/DF/button.lua @ 39:7944c081e5b4

- framework update. - ToC Update.
author Tercio
date Tue, 19 Jul 2016 13:23:40 -0300
parents 5da06cb420d4
children 7d5934415ad0
comparison
equal deleted inserted replaced
38:219f04e5ba55 39:7944c081e5b4
14 local _math_floor = math.floor --> lua local 14 local _math_floor = math.floor --> lua local
15 local loadstring = loadstring --> lua local 15 local loadstring = loadstring --> lua local
16 16
17 local cleanfunction = function() end 17 local cleanfunction = function() end
18 local APIButtonFunctions = false 18 local APIButtonFunctions = false
19 local ButtonMetaFunctions = {} 19
20 do
21 local metaPrototype = {
22 WidgetType = "button",
23 SetHook = DF.SetHook,
24 RunHooksForWidget = DF.RunHooksForWidget,
25 }
26
27 _G [DF.GlobalWidgetControlNames ["button"]] = _G [DF.GlobalWidgetControlNames ["button"]] or metaPrototype
28 end
29
30 local ButtonMetaFunctions = _G [DF.GlobalWidgetControlNames ["button"]]
20 31
21 ------------------------------------------------------------------------------------------------------------ 32 ------------------------------------------------------------------------------------------------------------
22 --> metatables 33 --> metatables
23 34
24 ButtonMetaFunctions.__call = function (_table, value, ...) 35 ButtonMetaFunctions.__call = function (_table, value, ...)
73 --> locked 84 --> locked
74 local gmember_locked = function (_object) 85 local gmember_locked = function (_object)
75 return _rawget (_object, "is_locked") 86 return _rawget (_object, "is_locked")
76 end 87 end
77 88
78 local get_members_function_index = { 89 ButtonMetaFunctions.GetMembers = ButtonMetaFunctions.GetMembers or {}
79 ["tooltip"] = gmember_tooltip, 90 ButtonMetaFunctions.GetMembers ["tooltip"] = gmember_tooltip
80 ["shown"] = gmember_shown, 91 ButtonMetaFunctions.GetMembers ["shown"] = gmember_shown
81 ["width"] = gmember_width, 92 ButtonMetaFunctions.GetMembers ["width"] = gmember_width
82 ["height"] = gmember_height, 93 ButtonMetaFunctions.GetMembers ["height"] = gmember_height
83 ["text"] = gmember_text, 94 ButtonMetaFunctions.GetMembers ["text"] = gmember_text
84 ["clickfunction"] = gmember_function, 95 ButtonMetaFunctions.GetMembers ["clickfunction"] = gmember_function
85 ["texture"] = gmember_texture, 96 ButtonMetaFunctions.GetMembers ["texture"] = gmember_texture
86 ["locked"] = gmember_locked, 97 ButtonMetaFunctions.GetMembers ["locked"] = gmember_locked
87 ["fontcolor"] = gmember_textcolor, 98 ButtonMetaFunctions.GetMembers ["fontcolor"] = gmember_textcolor
88 ["fontface"] = gmember_textfont, 99 ButtonMetaFunctions.GetMembers ["fontface"] = gmember_textfont
89 ["fontsize"] = gmember_textsize, 100 ButtonMetaFunctions.GetMembers ["fontsize"] = gmember_textsize
90 ["textcolor"] = gmember_textcolor, --alias 101 ButtonMetaFunctions.GetMembers ["textcolor"] = gmember_textcolor --alias
91 ["textfont"] = gmember_textfont, --alias 102 ButtonMetaFunctions.GetMembers ["textfont"] = gmember_textfont --alias
92 ["textsize"] = gmember_textsize --alias 103 ButtonMetaFunctions.GetMembers ["textsize"] = gmember_textsize --alias
93 }
94 104
95 ButtonMetaFunctions.__index = function (_table, _member_requested) 105 ButtonMetaFunctions.__index = function (_table, _member_requested)
96 106
97 local func = get_members_function_index [_member_requested] 107 local func = ButtonMetaFunctions.GetMembers [_member_requested]
98 if (func) then 108 if (func) then
99 return func (_table, _member_requested) 109 return func (_table, _member_requested)
100 end 110 end
101 111
102 local fromMe = _rawget (_table, _member_requested) 112 local fromMe = _rawget (_table, _member_requested)
204 elseif (_value == "right" or _value == ">") then 214 elseif (_value == "right" or _value == ">") then
205 _object.button.text:SetPoint ("right", _object.button, "right", -2, 0) 215 _object.button.text:SetPoint ("right", _object.button, "right", -2, 0)
206 _object.capsule_textalign = "right" 216 _object.capsule_textalign = "right"
207 end 217 end
208 end 218 end
209 219
210 local set_members_function_index = { 220 ButtonMetaFunctions.SetMembers = ButtonMetaFunctions.SetMembers or {}
211 ["tooltip"] = smember_tooltip, 221 ButtonMetaFunctions.SetMembers ["tooltip"] = smember_tooltip
212 ["show"] = smember_show, 222 ButtonMetaFunctions.SetMembers ["show"] = smember_show
213 ["hide"] = smember_hide, 223 ButtonMetaFunctions.SetMembers ["hide"] = smember_hide
214 ["width"] = smember_width, 224 ButtonMetaFunctions.SetMembers ["width"] = smember_width
215 ["height"] = smember_height, 225 ButtonMetaFunctions.SetMembers ["height"] = smember_height
216 ["text"] = smember_text, 226 ButtonMetaFunctions.SetMembers ["text"] = smember_text
217 ["clickfunction"] = smember_function, 227 ButtonMetaFunctions.SetMembers ["clickfunction"] = smember_function
218 ["textcolor"] = smember_textcolor, 228 ButtonMetaFunctions.SetMembers ["textcolor"] = smember_textcolor
219 ["textfont"] = smember_textfont, 229 ButtonMetaFunctions.SetMembers ["textfont"] = smember_textfont
220 ["textsize"] = smember_textsize, 230 ButtonMetaFunctions.SetMembers ["textsize"] = smember_textsize
221 ["texture"] = smember_texture, 231 ButtonMetaFunctions.SetMembers ["texture"] = smember_texture
222 ["locked"] = smember_locked, 232 ButtonMetaFunctions.SetMembers ["locked"] = smember_locked
223 ["textalign"] = smember_textalign, 233 ButtonMetaFunctions.SetMembers ["textalign"] = smember_textalign
224 }
225 234
226 ButtonMetaFunctions.__newindex = function (_table, _key, _value) 235 ButtonMetaFunctions.__newindex = function (_table, _key, _value)
227 local func = set_members_function_index [_key] 236 local func = ButtonMetaFunctions.SetMembers [_key]
228 if (func) then 237 if (func) then
229 return func (_table, _value) 238 return func (_table, _value)
230 else 239 else
231 return _rawset (_table, _key, _value) 240 return _rawset (_table, _key, _value)
232 end 241 end
471 end 480 end
472 function ButtonMetaFunctions:RightClick() 481 function ButtonMetaFunctions:RightClick()
473 return self.funcright() 482 return self.funcright()
474 end 483 end
475 484
476 --> hooks
477 function ButtonMetaFunctions:SetHook (hookType, func)
478 if (func) then
479 _rawset (self, hookType.."Hook", func)
480 else
481 _rawset (self, hookType.."Hook", nil)
482 end
483 end
484
485 --> custom textures 485 --> custom textures
486 function ButtonMetaFunctions:InstallCustomTexture (texture, rect, coords, use_split, side_textures, side_textures2) 486 function ButtonMetaFunctions:InstallCustomTexture (texture, rect, coords, use_split, side_textures, side_textures2)
487 487
488 self.button:SetNormalTexture (nil) 488 self.button:SetNormalTexture (nil)
489 self.button:SetPushedTexture (nil) 489 self.button:SetPushedTexture (nil)
585 ------------------------------------------------------------------------------------------------------------ 585 ------------------------------------------------------------------------------------------------------------
586 --> scripts 586 --> scripts
587 587
588 local OnEnter = function (button) 588 local OnEnter = function (button)
589 589
590 local capsule = button.MyObject
591
590 if (button.textureTopLeft) then 592 if (button.textureTopLeft) then
591 button.textureLeft:SetTexCoord (0, 4/128, 40/128, 56/128) 593 button.textureLeft:SetTexCoord (0, 4/128, 40/128, 56/128)
592 button.textureRight:SetTexCoord (124/128, 1, 40/128, 56/128) 594 button.textureRight:SetTexCoord (124/128, 1, 40/128, 56/128)
593 button.textureTop:SetTexCoord (9/128, 120/128, 33/128, 37/128) 595 button.textureTop:SetTexCoord (9/128, 120/128, 33/128, 37/128)
594 button.textureBottom:SetTexCoord (9/128, 119/128, 60/128, 64/128) 596 button.textureBottom:SetTexCoord (9/128, 119/128, 60/128, 64/128)
596 button.textureTopLeft:SetTexCoord (0, 8/128, 33/128, 40/128) 598 button.textureTopLeft:SetTexCoord (0, 8/128, 33/128, 40/128)
597 button.textureTopRight:SetTexCoord (121/128, 1, 33/128, 40/128) 599 button.textureTopRight:SetTexCoord (121/128, 1, 33/128, 40/128)
598 button.textureBottomLeft:SetTexCoord (0, 8/128, 56/128, 64/128) 600 button.textureBottomLeft:SetTexCoord (0, 8/128, 56/128, 64/128)
599 button.textureBottomRight:SetTexCoord (120/128, 1, 56/128, 64/128) 601 button.textureBottomRight:SetTexCoord (120/128, 1, 56/128, 64/128)
600 end 602 end
601 603
602 if (button.MyObject.OnEnterHook) then 604 local kill = capsule:RunHooksForWidget ("OnEnter", button, capsule)
603 local interrupt = button.MyObject.OnEnterHook (button, button.MyObject) 605 if (kill) then
604 if (interrupt) then 606 return
605 return
606 end
607 end 607 end
608 608
609 button.MyObject.is_mouse_over = true 609 button.MyObject.is_mouse_over = true
610 610
611 if (button.texture) then 611 if (button.texture) then
635 end 635 end
636 end 636 end
637 637
638 local OnLeave = function (button) 638 local OnLeave = function (button)
639 639
640 local capsule = button.MyObject
641
640 if (button.textureLeft and not button.MyObject.is_mouse_down) then 642 if (button.textureLeft and not button.MyObject.is_mouse_down) then
641 button.textureLeft:SetTexCoord (0, 4/128, 9/128, 24/128) 643 button.textureLeft:SetTexCoord (0, 4/128, 9/128, 24/128)
642 button.textureRight:SetTexCoord (124/128, 1, 9/128, 24/128) 644 button.textureRight:SetTexCoord (124/128, 1, 9/128, 24/128)
643 button.textureTop:SetTexCoord (9/128, 120/128, 0, 4/128) 645 button.textureTop:SetTexCoord (9/128, 120/128, 0, 4/128)
644 button.textureBottom:SetTexCoord (9/128, 119/128, 28/128, 32/128) 646 button.textureBottom:SetTexCoord (9/128, 119/128, 28/128, 32/128)
646 button.textureTopLeft:SetTexCoord (0, 8/128, 0, 8/128) 648 button.textureTopLeft:SetTexCoord (0, 8/128, 0, 8/128)
647 button.textureTopRight:SetTexCoord (121/128, 1, 0, 8/128) 649 button.textureTopRight:SetTexCoord (121/128, 1, 0, 8/128)
648 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128) 650 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128)
649 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128) 651 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128)
650 end 652 end
651 653
652 if (button.MyObject.OnLeaveHook) then 654 local kill = capsule:RunHooksForWidget ("OnLeave", button, capsule)
653 local interrupt = button.MyObject.OnLeaveHook (button, button.MyObject) 655 if (kill) then
654 if (interrupt) then 656 return
655 return
656 end
657 end 657 end
658 658
659 button.MyObject.is_mouse_over = false 659 button.MyObject.is_mouse_over = false
660 660
661 if (button.texture and not button.MyObject.is_mouse_down) then 661 if (button.texture and not button.MyObject.is_mouse_down) then
680 end 680 end
681 end 681 end
682 end 682 end
683 683
684 local OnHide = function (button) 684 local OnHide = function (button)
685 if (button.MyObject.OnHideHook) then 685 local capsule = button.MyObject
686 local interrupt = button.MyObject.OnHideHook (button, button.MyObject) 686 local kill = capsule:RunHooksForWidget ("OnHide", button, capsule)
687 if (interrupt) then 687 if (kill) then
688 return 688 return
689 end
690 end 689 end
691 end 690 end
692 691
693 local OnShow = function (button) 692 local OnShow = function (button)
694 if (button.MyObject.OnShowHook) then 693 local capsule = button.MyObject
695 local interrupt = button.MyObject.OnShowHook (button, button.MyObject) 694 local kill = capsule:RunHooksForWidget ("OnShow", button, capsule)
696 if (interrupt) then 695 if (kill) then
697 return 696 return
698 end
699 end 697 end
700 end 698 end
701 699
702 local OnMouseDown = function (button, buttontype) 700 local OnMouseDown = function (button, buttontype)
701 local capsule = button.MyObject
702
703 if (not button:IsEnabled()) then 703 if (not button:IsEnabled()) then
704 return 704 return
705 end 705 end
706 706
707 if (button.textureTopLeft) then 707 if (button.textureTopLeft) then
708 button.textureLeft:SetTexCoord (0, 4/128, 72/128, 88/128) 708 button.textureLeft:SetTexCoord (0, 4/128, 72/128, 88/128)
709 button.textureRight:SetTexCoord (124/128, 1, 72/128, 88/128) 709 button.textureRight:SetTexCoord (124/128, 1, 72/128, 88/128)
710 button.textureTop:SetTexCoord (9/128, 120/128, 65/128, 68/128) 710 button.textureTop:SetTexCoord (9/128, 120/128, 65/128, 68/128)
713 button.textureTopLeft:SetTexCoord (0, 8/128, 65/128, 71/128) 713 button.textureTopLeft:SetTexCoord (0, 8/128, 65/128, 71/128)
714 button.textureTopRight:SetTexCoord (121/128, 1, 65/128, 71/128) 714 button.textureTopRight:SetTexCoord (121/128, 1, 65/128, 71/128)
715 button.textureBottomLeft:SetTexCoord (0, 8/128, 88/128, 96/128) 715 button.textureBottomLeft:SetTexCoord (0, 8/128, 88/128, 96/128)
716 button.textureBottomRight:SetTexCoord (120/128, 1, 88/128, 96/128) 716 button.textureBottomRight:SetTexCoord (120/128, 1, 88/128, 96/128)
717 end 717 end
718 718
719 if (button.MyObject.OnMouseDownHook) then 719 local kill = capsule:RunHooksForWidget ("OnMouseDown", button, capsule)
720 local interrupt = button.MyObject.OnMouseDownHook (button, buttontype, button.MyObject) 720 if (kill) then
721 if (interrupt) then 721 return
722 return
723 end
724 end 722 end
725 723
726 button.MyObject.is_mouse_down = true 724 button.MyObject.is_mouse_down = true
727 725
728 if (button.texture) then 726 if (button.texture) then
801 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128) 799 button.textureBottomLeft:SetTexCoord (0, 8/128, 24/128, 32/128)
802 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128) 800 button.textureBottomRight:SetTexCoord (120/128, 1, 24/128, 32/128)
803 end 801 end
804 end 802 end
805 803
806 if (button.MyObject.OnMouseUpHook) then 804 local capsule = button.MyObject
807 local interrupt = button.MyObject.OnMouseUpHook (button, buttontype, button.MyObject) 805 local kill = capsule:RunHooksForWidget ("OnMouseUp", button, capsule)
808 if (interrupt) then 806 if (kill) then
809 return 807 return
810 end
811 end 808 end
812 809
813 button.MyObject.is_mouse_down = false 810 button.MyObject.is_mouse_down = false
814 811
815 if (button.texture) then 812 if (button.texture) then
955 if (container.dframework) then 952 if (container.dframework) then
956 container = container.widget 953 container = container.widget
957 end 954 end
958 955
959 --> default members: 956 --> default members:
960 --> hooks
961 ButtonObject.OnEnterHook = nil
962 ButtonObject.OnLeaveHook = nil
963 ButtonObject.OnHideHook = nil
964 ButtonObject.OnShowHook = nil
965 ButtonObject.OnMouseDownHook = nil
966 ButtonObject.OnMouseUpHook = nil
967 --> misc
968 ButtonObject.is_locked = true 957 ButtonObject.is_locked = true
969 ButtonObject.container = container 958 ButtonObject.container = container
970 ButtonObject.have_tooltip = nil
971 ButtonObject.options = {OnGrab = false} 959 ButtonObject.options = {OnGrab = false}
972
973 960
974 ButtonObject.button = CreateFrame ("button", name, parent, "DetailsFrameworkButtonTemplate") 961 ButtonObject.button = CreateFrame ("button", name, parent, "DetailsFrameworkButtonTemplate")
975 ButtonObject.widget = ButtonObject.button 962 ButtonObject.widget = ButtonObject.button
976 963
977 --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}}) 964 --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}})
1004 ButtonObject.button:SetDisabledTexture (texture) 991 ButtonObject.button:SetDisabledTexture (texture)
1005 ButtonObject.button:SetHighlightTexture (texture, "ADD") 992 ButtonObject.button:SetHighlightTexture (texture, "ADD")
1006 993
1007 ButtonObject.button.text:SetText (text) 994 ButtonObject.button.text:SetText (text)
1008 ButtonObject.button.text:SetPoint ("center", ButtonObject.button, "center") 995 ButtonObject.button.text:SetPoint ("center", ButtonObject.button, "center")
1009 996
1010 local text_width = ButtonObject.button.text:GetStringWidth() 997 local text_width = ButtonObject.button.text:GetStringWidth()
1011 if (text_width > w-15 and ButtonObject.button.text:GetText() ~= "") then 998 if (text_width > w-15 and ButtonObject.button.text:GetText() ~= "") then
1012 if (not short_method) then 999 if (short_method == false) then --> if is false, do not use auto resize
1000 --do nothing
1001 elseif (not short_method) then --> if the value is omitted, use the default resize
1013 local new_width = text_width+15 1002 local new_width = text_width+15
1014 ButtonObject.button:SetWidth (new_width) 1003 ButtonObject.button:SetWidth (new_width)
1004
1015 elseif (short_method == 1) then 1005 elseif (short_method == 1) then
1016 local loop = true 1006 local loop = true
1017 local textsize = 11 1007 local textsize = 11
1018 while (loop) do 1008 while (loop) do
1019 if (text_width+15 < w or textsize < 8) then 1009 if (text_width+15 < w or textsize < 8) then
1049 DF:SetFontFace (ButtonObject.button.text, font) 1039 DF:SetFontFace (ButtonObject.button.text, font)
1050 end 1040 end
1051 end 1041 end
1052 1042
1053 --> hooks 1043 --> hooks
1044 ButtonObject.HookList = {
1045 OnEnter = {},
1046 OnLeave = {},
1047 OnHide = {},
1048 OnShow = {},
1049 OnMouseDown = {},
1050 OnMouseUp = {},
1051 }
1052
1054 ButtonObject.button:SetScript ("OnEnter", OnEnter) 1053 ButtonObject.button:SetScript ("OnEnter", OnEnter)
1055 ButtonObject.button:SetScript ("OnLeave", OnLeave) 1054 ButtonObject.button:SetScript ("OnLeave", OnLeave)
1056 ButtonObject.button:SetScript ("OnHide", OnHide) 1055 ButtonObject.button:SetScript ("OnHide", OnHide)
1057 ButtonObject.button:SetScript ("OnShow", OnShow) 1056 ButtonObject.button:SetScript ("OnShow", OnShow)
1058 ButtonObject.button:SetScript ("OnMouseDown", OnMouseDown) 1057 ButtonObject.button:SetScript ("OnMouseDown", OnMouseDown)
1117 background:SetTexCoord (0.337890625, 0.390625, 0.625, 0.658203125) 1116 background:SetTexCoord (0.337890625, 0.390625, 0.625, 0.658203125)
1118 background:SetDrawLayer ("background", 1) 1117 background:SetDrawLayer ("background", 1)
1119 1118
1120 --textura da cor 1119 --textura da cor
1121 local img = DF:NewImage (button, nil, color_button_width, color_button_height, nil, nil, "color_texture", "$parentTex") 1120 local img = DF:NewImage (button, nil, color_button_width, color_button_height, nil, nil, "color_texture", "$parentTex")
1122 img:SetTexture (1, 1, 1) 1121 img:SetColorTexture (1, 1, 1)
1123 img:SetPoint ("topleft", button.widget, "topleft", 1, -2) 1122 img:SetPoint ("topleft", button.widget, "topleft", 1, -2)
1124 img:SetPoint ("bottomright", button.widget, "bottomright", -1, 1) 1123 img:SetPoint ("bottomright", button.widget, "bottomright", -1, 1)
1125 img:SetDrawLayer ("background", 2) 1124 img:SetDrawLayer ("background", 2)
1126 1125
1127 return button 1126 return button