1553757 - Refactor updateIndicators
[wowui.git] / libs / AceGUI-3.0 / widgets / AceGUIContainer-Frame.lua
1 --[[-----------------------------------------------------------------------------
2 Frame Container
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "Frame", 24
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 -- Lua APIs
9 local pairs, assert, type = pairs, assert, type
10 local wipe = table.wipe
11
12 -- WoW APIs
13 local PlaySound = PlaySound
14 local CreateFrame, UIParent = CreateFrame, UIParent
15
16 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
17 -- List them here for Mikk's FindGlobals script
18 -- GLOBALS: CLOSE
19
20 --[[-----------------------------------------------------------------------------
21 Scripts
22 -------------------------------------------------------------------------------]]
23 local function Button_OnClick(frame)
24         PlaySound("gsTitleOptionExit")
25         frame.obj:Hide()
26 end
27
28 local function Frame_OnClose(frame)
29         frame.obj:Fire("OnClose")
30 end
31
32 local function Frame_OnMouseDown(frame)
33         AceGUI:ClearFocus()
34 end
35
36 local function Title_OnMouseDown(frame)
37         frame:GetParent():StartMoving()
38         AceGUI:ClearFocus()
39 end
40
41 local function MoverSizer_OnMouseUp(mover)
42         local frame = mover:GetParent()
43         frame:StopMovingOrSizing()
44         local self = frame.obj
45         local status = self.status or self.localstatus
46         status.width = frame:GetWidth()
47         status.height = frame:GetHeight()
48         status.top = frame:GetTop()
49         status.left = frame:GetLeft()
50 end
51
52 local function SizerSE_OnMouseDown(frame)
53         frame:GetParent():StartSizing("BOTTOMRIGHT")
54         AceGUI:ClearFocus()
55 end
56
57 local function SizerS_OnMouseDown(frame)
58         frame:GetParent():StartSizing("BOTTOM")
59         AceGUI:ClearFocus()
60 end
61
62 local function SizerE_OnMouseDown(frame)
63         frame:GetParent():StartSizing("RIGHT")
64         AceGUI:ClearFocus()
65 end
66
67 local function StatusBar_OnEnter(frame)
68         frame.obj:Fire("OnEnterStatusBar")
69 end
70
71 local function StatusBar_OnLeave(frame)
72         frame.obj:Fire("OnLeaveStatusBar")
73 end
74
75 --[[-----------------------------------------------------------------------------
76 Methods
77 -------------------------------------------------------------------------------]]
78 local methods = {
79         ["OnAcquire"] = function(self)
80                 self.frame:SetParent(UIParent)
81                 self.frame:SetFrameStrata("FULLSCREEN_DIALOG")
82                 self:SetTitle()
83                 self:SetStatusText()
84                 self:ApplyStatus()
85                 self:Show()
86         self:EnableResize(true)
87         end,
88
89         ["OnRelease"] = function(self)
90                 self.status = nil
91                 wipe(self.localstatus)
92         end,
93
94         ["OnWidthSet"] = function(self, width)
95                 local content = self.content
96                 local contentwidth = width - 34
97                 if contentwidth < 0 then
98                         contentwidth = 0
99                 end
100                 content:SetWidth(contentwidth)
101                 content.width = contentwidth
102         end,
103
104         ["OnHeightSet"] = function(self, height)
105                 local content = self.content
106                 local contentheight = height - 57
107                 if contentheight < 0 then
108                         contentheight = 0
109                 end
110                 content:SetHeight(contentheight)
111                 content.height = contentheight
112         end,
113
114         ["SetTitle"] = function(self, title)
115                 self.titletext:SetText(title)
116                 self.titlebg:SetWidth((self.titletext:GetWidth() or 0) + 10)
117         end,
118
119         ["SetStatusText"] = function(self, text)
120                 self.statustext:SetText(text)
121         end,
122
123         ["Hide"] = function(self)
124                 self.frame:Hide()
125         end,
126
127         ["Show"] = function(self)
128                 self.frame:Show()
129         end,
130
131         ["EnableResize"] = function(self, state)
132                 local func = state and "Show" or "Hide"
133                 self.sizer_se[func](self.sizer_se)
134                 self.sizer_s[func](self.sizer_s)
135                 self.sizer_e[func](self.sizer_e)
136         end,
137
138         -- called to set an external table to store status in
139         ["SetStatusTable"] = function(self, status)
140                 assert(type(status) == "table")
141                 self.status = status
142                 self:ApplyStatus()
143         end,
144
145         ["ApplyStatus"] = function(self)
146                 local status = self.status or self.localstatus
147                 local frame = self.frame
148                 self:SetWidth(status.width or 700)
149                 self:SetHeight(status.height or 500)
150                 frame:ClearAllPoints()
151                 if status.top and status.left then
152                         frame:SetPoint("TOP", UIParent, "BOTTOM", 0, status.top)
153                         frame:SetPoint("LEFT", UIParent, "LEFT", status.left, 0)
154                 else
155                         frame:SetPoint("CENTER")
156                 end
157         end
158 }
159
160 --[[-----------------------------------------------------------------------------
161 Constructor
162 -------------------------------------------------------------------------------]]
163 local FrameBackdrop = {
164         bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
165         edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
166         tile = true, tileSize = 32, edgeSize = 32,
167         insets = { left = 8, right = 8, top = 8, bottom = 8 }
168 }
169
170 local PaneBackdrop  = {
171         bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
172         edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
173         tile = true, tileSize = 16, edgeSize = 16,
174         insets = { left = 3, right = 3, top = 5, bottom = 3 }
175 }
176
177 local function Constructor()
178         local frame = CreateFrame("Frame", nil, UIParent)
179         frame:Hide()
180
181         frame:EnableMouse(true)
182         frame:SetMovable(true)
183         frame:SetResizable(true)
184         frame:SetFrameStrata("FULLSCREEN_DIALOG")
185         frame:SetBackdrop(FrameBackdrop)
186         frame:SetBackdropColor(0, 0, 0, 1)
187         frame:SetMinResize(400, 200)
188         frame:SetToplevel(true)
189         frame:SetScript("OnHide", Frame_OnClose)
190         frame:SetScript("OnMouseDown", Frame_OnMouseDown)
191
192         local closebutton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
193         closebutton:SetScript("OnClick", Button_OnClick)
194         closebutton:SetPoint("BOTTOMRIGHT", -27, 17)
195         closebutton:SetHeight(20)
196         closebutton:SetWidth(100)
197         closebutton:SetText(CLOSE)
198
199         local statusbg = CreateFrame("Button", nil, frame)
200         statusbg:SetPoint("BOTTOMLEFT", 15, 15)
201         statusbg:SetPoint("BOTTOMRIGHT", -132, 15)
202         statusbg:SetHeight(24)
203         statusbg:SetBackdrop(PaneBackdrop)
204         statusbg:SetBackdropColor(0.1,0.1,0.1)
205         statusbg:SetBackdropBorderColor(0.4,0.4,0.4)
206         statusbg:SetScript("OnEnter", StatusBar_OnEnter)
207         statusbg:SetScript("OnLeave", StatusBar_OnLeave)
208
209         local statustext = statusbg:CreateFontString(nil, "OVERLAY", "GameFontNormal")
210         statustext:SetPoint("TOPLEFT", 7, -2)
211         statustext:SetPoint("BOTTOMRIGHT", -7, 2)
212         statustext:SetHeight(20)
213         statustext:SetJustifyH("LEFT")
214         statustext:SetText("")
215
216         local titlebg = frame:CreateTexture(nil, "OVERLAY")
217         titlebg:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header")
218         titlebg:SetTexCoord(0.31, 0.67, 0, 0.63)
219         titlebg:SetPoint("TOP", 0, 12)
220         titlebg:SetWidth(100)
221         titlebg:SetHeight(40)
222
223         local title = CreateFrame("Frame", nil, frame)
224         title:EnableMouse(true)
225         title:SetScript("OnMouseDown", Title_OnMouseDown)
226         title:SetScript("OnMouseUp", MoverSizer_OnMouseUp)
227         title:SetAllPoints(titlebg)
228
229         local titletext = title:CreateFontString(nil, "OVERLAY", "GameFontNormal")
230         titletext:SetPoint("TOP", titlebg, "TOP", 0, -14)
231
232         local titlebg_l = frame:CreateTexture(nil, "OVERLAY")
233         titlebg_l:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header")
234         titlebg_l:SetTexCoord(0.21, 0.31, 0, 0.63)
235         titlebg_l:SetPoint("RIGHT", titlebg, "LEFT")
236         titlebg_l:SetWidth(30)
237         titlebg_l:SetHeight(40)
238
239         local titlebg_r = frame:CreateTexture(nil, "OVERLAY")
240         titlebg_r:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header")
241         titlebg_r:SetTexCoord(0.67, 0.77, 0, 0.63)
242         titlebg_r:SetPoint("LEFT", titlebg, "RIGHT")
243         titlebg_r:SetWidth(30)
244         titlebg_r:SetHeight(40)
245
246         local sizer_se = CreateFrame("Frame", nil, frame)
247         sizer_se:SetPoint("BOTTOMRIGHT")
248         sizer_se:SetWidth(25)
249         sizer_se:SetHeight(25)
250         sizer_se:EnableMouse()
251         sizer_se:SetScript("OnMouseDown",SizerSE_OnMouseDown)
252         sizer_se:SetScript("OnMouseUp", MoverSizer_OnMouseUp)
253
254         local line1 = sizer_se:CreateTexture(nil, "BACKGROUND")
255         line1:SetWidth(14)
256         line1:SetHeight(14)
257         line1:SetPoint("BOTTOMRIGHT", -8, 8)
258         line1:SetTexture("Interface\\Tooltips\\UI-Tooltip-Border")
259         local x = 0.1 * 14/17
260         line1:SetTexCoord(0.05 - x, 0.5, 0.05, 0.5 + x, 0.05, 0.5 - x, 0.5 + x, 0.5)
261
262         local line2 = sizer_se:CreateTexture(nil, "BACKGROUND")
263         line2:SetWidth(8)
264         line2:SetHeight(8)
265         line2:SetPoint("BOTTOMRIGHT", -8, 8)
266         line2:SetTexture("Interface\\Tooltips\\UI-Tooltip-Border")
267         local x = 0.1 * 8/17
268         line2:SetTexCoord(0.05 - x, 0.5, 0.05, 0.5 + x, 0.05, 0.5 - x, 0.5 + x, 0.5)
269
270         local sizer_s = CreateFrame("Frame", nil, frame)
271         sizer_s:SetPoint("BOTTOMRIGHT", -25, 0)
272         sizer_s:SetPoint("BOTTOMLEFT")
273         sizer_s:SetHeight(25)
274         sizer_s:EnableMouse(true)
275         sizer_s:SetScript("OnMouseDown", SizerS_OnMouseDown)
276         sizer_s:SetScript("OnMouseUp", MoverSizer_OnMouseUp)
277
278         local sizer_e = CreateFrame("Frame", nil, frame)
279         sizer_e:SetPoint("BOTTOMRIGHT", 0, 25)
280         sizer_e:SetPoint("TOPRIGHT")
281         sizer_e:SetWidth(25)
282         sizer_e:EnableMouse(true)
283         sizer_e:SetScript("OnMouseDown", SizerE_OnMouseDown)
284         sizer_e:SetScript("OnMouseUp", MoverSizer_OnMouseUp)
285
286         --Container Support
287         local content = CreateFrame("Frame", nil, frame)
288         content:SetPoint("TOPLEFT", 17, -27)
289         content:SetPoint("BOTTOMRIGHT", -17, 40)
290
291         local widget = {
292                 localstatus = {},
293                 titletext   = titletext,
294                 statustext  = statustext,
295                 titlebg     = titlebg,
296                 sizer_se    = sizer_se,
297                 sizer_s     = sizer_s,
298                 sizer_e     = sizer_e,
299                 content     = content,
300                 frame       = frame,
301                 type        = Type
302         }
303         for method, func in pairs(methods) do
304                 widget[method] = func
305         end
306         closebutton.obj, statusbg.obj = widget, widget
307
308         return AceGUI:RegisterAsContainer(widget)
309 end
310
311 AceGUI:RegisterWidgetType(Type, Constructor, Version)