fd95cb7884ff57c63c473918c212804c3c741e35
[wowui.git] / libs / AceGUI-3.0-SharedMediaWidgets / Libs / AceGUI-3.0 / widgets / AceGUIWidget-Button.lua
1 --[[-----------------------------------------------------------------------------
2 Button Widget
3 Graphical Button.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Button", 22
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
8
9 -- Lua APIs
10 local pairs = pairs
11
12 -- WoW APIs
13 local _G = _G
14 local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent
15
16 local wowMoP
17 do
18         local _, _, _, interface = GetBuildInfo()
19         wowMoP = (interface >= 50000)
20 end
21
22 --[[-----------------------------------------------------------------------------
23 Scripts
24 -------------------------------------------------------------------------------]]
25 local function Button_OnClick(frame, ...)
26         AceGUI:ClearFocus()
27         PlaySound("igMainMenuOption")
28         frame.obj:Fire("OnClick", ...)
29 end
30
31 local function Control_OnEnter(frame)
32         frame.obj:Fire("OnEnter")
33 end
34
35 local function Control_OnLeave(frame)
36         frame.obj:Fire("OnLeave")
37 end
38
39 --[[-----------------------------------------------------------------------------
40 Methods
41 -------------------------------------------------------------------------------]]
42 local methods = {
43         ["OnAcquire"] = function(self)
44                 -- restore default values
45                 self:SetHeight(24)
46                 self:SetWidth(200)
47                 self:SetDisabled(false)
48                 self:SetText()
49         end,
50
51         -- ["OnRelease"] = nil,
52
53         ["SetText"] = function(self, text)
54                 self.text:SetText(text)
55         end,
56
57         ["SetDisabled"] = function(self, disabled)
58                 self.disabled = disabled
59                 if disabled then
60                         self.frame:Disable()
61                 else
62                         self.frame:Enable()
63                 end
64         end
65 }
66
67 --[[-----------------------------------------------------------------------------
68 Constructor
69 -------------------------------------------------------------------------------]]
70 local function Constructor()
71         local name = "AceGUI30Button" .. AceGUI:GetNextWidgetNum(Type)
72         local frame = CreateFrame("Button", name, UIParent, wowMoP and "UIPanelButtonTemplate" or "UIPanelButtonTemplate2")
73         frame:Hide()
74
75         frame:EnableMouse(true)
76         frame:SetScript("OnClick", Button_OnClick)
77         frame:SetScript("OnEnter", Control_OnEnter)
78         frame:SetScript("OnLeave", Control_OnLeave)
79
80         local text = frame:GetFontString()
81         text:ClearAllPoints()
82         text:SetPoint("TOPLEFT", 15, -1)
83         text:SetPoint("BOTTOMRIGHT", -15, 1)
84         text:SetJustifyV("MIDDLE")
85
86         local widget = {
87                 text  = text,
88                 frame = frame,
89                 type  = Type
90         }
91         for method, func in pairs(methods) do
92                 widget[method] = func
93         end
94
95         return AceGUI:RegisterAsWidget(widget)
96 end
97
98 AceGUI:RegisterWidgetType(Type, Constructor, Version)