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

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children 0515882856f1
comparison
equal deleted inserted replaced
56:75431c084aa0 57:01b63b8ed811
1 --[[-----------------------------------------------------------------------------
2 Text Button Widget
3 Based on the AceGUI button, but a custom look that just shows text.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "AmrUiTextButton", 1
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 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
10
11 -- Lua APIs
12 local pairs = pairs
13
14 -- WoW APIs
15 local _G = _G
16 local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent
17
18
19 --[[-----------------------------------------------------------------------------
20 Scripts
21 -------------------------------------------------------------------------------]]
22 local function buttonOnClick(frame, ...)
23 AceGUI:ClearFocus()
24 PlaySound("igMainMenuOption")
25 frame.obj:Fire("OnClick", ...)
26 end
27
28 local function buttonOnEnter(frame)
29 frame.obj.bg:Hide()
30 frame.obj.hover:Show()
31 frame.obj:Fire("OnEnter")
32 end
33
34 local function buttonOnLeave(frame)
35 frame.obj.bg:Show()
36 frame.obj.hover:Hide()
37 frame.obj:Fire("OnLeave")
38 end
39
40 --[[-----------------------------------------------------------------------------
41 Methods
42 -------------------------------------------------------------------------------]]
43 local methods = {
44 ["OnAcquire"] = function(self)
45 -- restore default values
46 self:SetHeight(24)
47 self:SetWidth(200)
48 self:SetBackgroundColor(Amr.Colors.Black, 0)
49 self:SetHoverBackgroundColor(Amr.Colors.Black, 0)
50 self:SetDisabled(false)
51
52 self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text))
53 self:SetHoverFont(Amr.CreateFont("Regular", 16, Amr.Colors.TextHover))
54 self:SetText("")
55 self:SetWordWrap(true)
56 self:SetJustifyH("CENTER")
57 self:SetJustifyV("MIDDLE")
58 self:SetTextPadding()
59
60 self:SetSubtextFont(Amr.CreateFont("Regular", 16, Amr.Colors.Text))
61 self:SetSubtext()
62 self:SetSubtextWordWrap(true)
63 self:SetSubtextJustifyH("CENTER")
64 self:SetSubtextJustifyV("MIDDLE")
65 self:SetSubtextPadding()
66
67 self.frame:ClearAllPoints()
68 self.bg:Show()
69 self.hover:Hide()
70 end,
71
72 ["OnWidthSet"] = function(self, width)
73 self.frame:GetFontString():SetWidth(width)
74 end,
75
76 ["OnHeightSet"] = function(self, height)
77 self.frame:GetFontString():SetHeight(height)
78 end,
79
80 ["SetBackgroundColor"] = function(self, color, alpha)
81 self.bg:SetTexture(color.R, color.G, color.B, alpha)
82 end,
83
84 ["SetBackgroundImage"] = function(self, image)
85 self.bg:SetTexture(image)
86 self.bg:SetDesaturated(false)
87 end,
88
89 ["SetHoverBackgroundColor"] = function(self, color, alpha)
90 self.hover:SetTexture(color.R, color.G, color.B, alpha)
91 end,
92
93 ["SetHoverBackgroundImage"] = function(self, image)
94 self.hover:SetTexture(image)
95 end,
96
97 ["SetText"] = function(self, text)
98 self.frame:SetText(text)
99 end,
100
101 ["SetWordWrap"] = function(self, enable)
102 self.frame:GetFontString():SetWordWrap(enable)
103 end,
104
105 ["SetJustifyH"] = function(self, val)
106 self.frame:GetFontString():SetJustifyH(val)
107 end,
108
109 ["SetJustifyV"] = function(self, val)
110 self.frame:GetFontString():SetJustifyV(val)
111 end,
112
113 ["SetTextPadding"] = function(self, top, right, bottom, left)
114 local f = self.frame:GetFontString()
115 f:ClearAllPoints()
116
117 if not top and not right and not bottom and not left then
118 f:SetPoint("CENTER")
119 end
120
121 if top then f:SetPoint("TOP", self.frame, "TOP", 0, -top) end
122 if right then f:SetPoint("RIGHT", self.frame, "RIGHT", -right, 0) end
123 if bottom then f:SetPoint("BOTTOM", self.frame, "BOTTOM", 0, bottom) end
124 if left then f:SetPoint("LEFT", self.frame, "LEFT", left, 0) end
125 end,
126
127 ["SetFont"] = function(self, font)
128 self.frame:SetNormalFontObject(font)
129 end,
130
131 ["SetHoverFont"] = function(self, font)
132 self.frame:SetHighlightFontObject(font)
133 end,
134
135 ["SetSubtext"] = function(self, text)
136 self.subtxt:SetText(text)
137 if text then
138 self.subtxt:Show()
139 else
140 self.subtxt:Hide()
141 end
142 end,
143
144 ["SetSubtextWordWrap"] = function(self, enable)
145 self.subtxt:SetWordWrap(enable)
146 end,
147
148 ["SetSubtextJustifyH"] = function(self, val)
149 self.subtxt:SetJustifyH(val)
150 end,
151
152 ["SetSubtextJustifyV"] = function(self, val)
153 self.subtxt:SetJustifyV(val)
154 end,
155
156 ["SetSubtextPadding"] = function(self, top, right, bottom, left)
157 local f = self.subtxt
158 f:ClearAllPoints()
159 if top then f:SetPoint("TOP", self.frame, "TOP", 0, -top) end
160 if right then f:SetPoint("RIGHT", self.frame, "RIGHT", -right, 0) end
161 if bottom then f:SetPoint("BOTTOM", self.frame, "BOTTOM", 0, bottom) end
162 if left then f:SetPoint("LEFT", self.frame, "LEFT", left, 0) end
163 end,
164
165 ["SetSubtextFont"] = function(self, font)
166 self.subtxt:SetFontObject(font)
167 end,
168
169 ["SetDisabled"] = function(self, disabled)
170 self.disabled = disabled
171 if disabled then
172 self.frame:Disable()
173 else
174 self.frame:Enable()
175 end
176 end,
177
178 ["SetVisible"] = function(self, visible)
179 if visible then
180 self.frame:Show()
181 else
182 self.frame:Hide()
183 end
184 end
185 }
186
187 --[[-----------------------------------------------------------------------------
188 Constructor
189 -------------------------------------------------------------------------------]]
190 local function Constructor()
191 local name = "AmrUiTextButton" .. AceGUI:GetNextWidgetNum(Type)
192 local frame = CreateFrame("Button", name, UIParent)
193 frame:Hide()
194
195 local txt = frame:CreateFontString()
196 frame:SetFontString(txt)
197
198 local subtxt = frame:CreateFontString()
199 subtxt:Hide()
200
201 frame:EnableMouse(true)
202 frame:SetScript("OnEnter", buttonOnEnter)
203 frame:SetScript("OnLeave", buttonOnLeave)
204 frame:SetScript("OnClick", buttonOnClick)
205
206 local bg = frame:CreateTexture()
207 bg:SetAllPoints()
208
209 local hover = frame:CreateTexture()
210 hover:SetAllPoints()
211
212 local widget = {
213 bg = bg,
214 subtxt = subtxt,
215 hover = hover,
216 frame = frame,
217 type = Type
218 }
219 for method, func in pairs(methods) do
220 widget[method] = func
221 end
222
223 return AceGUI:RegisterAsWidget(widget)
224 end
225
226 AceGUI:RegisterWidgetType(Type, Constructor, Version)