comparison ui/AmrUiCheckBox.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children 57c6cac5143c
comparison
equal deleted inserted replaced
56:75431c084aa0 57:01b63b8ed811
1 --[[-----------------------------------------------------------------------------
2 CheckBox Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "AmrUiCheckBox", 1
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
7
8 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
9
10 -- Lua APIs
11 local pairs = pairs
12
13 -- WoW APIs
14 local _G = _G
15 local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent
16
17
18 --[[-----------------------------------------------------------------------------
19 Scripts
20 -------------------------------------------------------------------------------]]
21 local function buttonOnClick(frame, ...)
22 AceGUI:ClearFocus()
23 PlaySound("igMainMenuOption")
24 frame.obj:Fire("OnClick", ...)
25 end
26
27 --[[-----------------------------------------------------------------------------
28 Methods
29 -------------------------------------------------------------------------------]]
30 local methods = {
31 ["OnAcquire"] = function(self)
32 -- restore default values
33 self:SetDisabled(false)
34 self:SetFont(Amr.CreateFont("Regular", 14, Amr.Colors.Text))
35 self:SetText()
36 self:SetChecked(false)
37 self.frame:ClearAllPoints()
38 end,
39
40 ["SetText"] = function(self, text)
41 self.label:SetText(text)
42 self.frame:SetWidth(16 + 6 + self.label:GetStringWidth())
43 end,
44
45 ["SetFont"] = function(self, font)
46 self.label:SetFontObject(font)
47 end,
48
49 ["SetChecked"] = function(self, checked)
50 -- not sure if WoW expects boolean type or not, too lazy to find out so just cast it
51 self.frame:SetChecked(not not checked)
52 end,
53
54 ["GetChecked"] = function(self)
55 return self.frame:GetChecked()
56 end,
57
58 ["GetWidth"] = function(self)
59 return self.frame:GetWidth()
60 end,
61
62 ["SetDisabled"] = function(self, disabled)
63 self.disabled = disabled
64 if disabled then
65 self.frame:Disable()
66 else
67 self.frame:Enable()
68 end
69 end,
70
71 ["SetVisible"] = function(self, visible)
72 if visible then
73 self.frame:Show()
74 else
75 self.frame:Hide()
76 end
77 end
78 }
79
80 --[[-----------------------------------------------------------------------------
81 Constructor
82 -------------------------------------------------------------------------------]]
83 local function Constructor()
84 local name = "AmrUiCheckBox" .. AceGUI:GetNextWidgetNum(Type)
85 local frame = CreateFrame("CheckButton", name, UIParent)
86 frame:SetHeight(16)
87 frame:SetPushedTextOffset(0, 0)
88 frame:Hide()
89
90 frame:EnableMouse(true)
91 frame:SetScript("OnClick", buttonOnClick)
92
93 -- unchecked texture
94 local texNormal = frame:CreateTexture(nil, "BACKGROUND")
95 texNormal:SetWidth(16)
96 texNormal:SetHeight(16)
97 texNormal:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\check-off")
98 texNormal:SetPoint("LEFT", frame, "LEFT")
99 frame:SetNormalTexture(texNormal)
100
101 -- checked texture
102 local texCheck = frame:CreateTexture(nil, "BORDER")
103 texCheck:SetTexture("Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\check-on")
104 texCheck:SetPoint("LEFT", frame, "LEFT")
105 frame:SetCheckedTexture(texCheck)
106
107 -- label
108 local lbl = frame:CreateFontString(nil, "ARTWORK")
109 lbl:SetJustifyV("MIDDLE")
110 lbl:SetPoint("LEFT", texNormal, "RIGHT", 8, 0)
111 frame:SetFontString(lbl)
112
113 local widget = {
114 texNormal = texNormal,
115 label = lbl,
116 frame = frame,
117 type = Type
118 }
119 for method, func in pairs(methods) do
120 widget[method] = func
121 end
122
123 return AceGUI:RegisterAsWidget(widget)
124 end
125
126 AceGUI:RegisterWidgetType(Type, Constructor, Version)