-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameBuilder.lua
More file actions
364 lines (304 loc) · 9.48 KB
/
FrameBuilder.lua
File metadata and controls
364 lines (304 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
RaidCalendar = RaidCalendar or {}
---@class RaidCalendar
local m = RaidCalendar
if m.FrameBuilder then return end
local M = {}
---@alias FrameStyle
---| "TOOLTIP"
---| "NONE"
---@class FrameBuilder
---@field name fun( self: FrameBuilder, name: string ): FrameBuilder
---@field type fun( self: FrameBuilder, type: FrameType ): FrameBuilder
---@field title fun( self: FrameBuilder, title: string ): FrameBuilder
---@field parent fun( self: FrameBuilder, parent: Frame ): FrameBuilder
---@field point fun( self: FrameBuilder, point: FramePoint, relative_region: string|Region|nil, relative_point: FramePoint, offset_x: number?, offset_y: number?)
---@field width fun( self: FrameBuilder, width: number ): FrameBuilder
---@field height fun( self: FrameBuilder, height: number ): FrameBuilder
---@field frame_level fun( self: FrameBuilder, frame_level: number ): FrameBuilder
---@field frame_style fun( self: FrameBuilder, frame_style: FrameStyle ): FrameBuilder
---@field strata fun( self: FrameBuilder, strata: FrameStrata ): FrameBuilder
---@field movable fun( self: FrameBuilder ): FrameBuilder
---@field backdrop fun( self: FrameBuilder, backdrop: Backdrop ): FrameBuilder
---@field backdrop_color fun( self: FrameBuilder, r: number, g: number, b: number, a: number ): FrameBuilder
---@field border_color fun( self: FrameBuilder, r: number, g: number, b: number, a: number ): FrameBuilder
---@field esc fun( self: FrameBuilder ): FrameBuilder
---@field close_button fun( self: FrameBuilder ): FrameBuilder
---@field on_close fun( self: FrameBuilder, callback: function ): FrameBuilder
---@field on_drag_stop fun( self: FrameBuilder, callback: function ): FrameBuilder
---@field on_hide fun( self: FrameBuilder, on_hide: function ): FrameBuilder
---@field hidden fun( self: FrameBuilder ): FrameBuilder
---@field build fun( self: FrameBuilder ): Frame
---@class FrameBuilderFactory
---@field new fun(): FrameBuilder
---@return FrameBuilder
function M.new()
local options = {
backdrop = {}
}
local is_dragging
local function create_frame()
---@param parent Frame
---@param title string
---@return TitlebarFrame
local function create_titlebar( parent, title )
---@class TitlebarFrame: Frame
local frame = CreateFrame( "Frame", nil, parent )
frame:SetPoint( "TopLeft", parent, "TopLeft", 5, -5 )
frame:SetPoint( "BottomRight", parent, "TopRight", -5, -24 )
frame:SetBackdrop( { bgFile = "Interface/Buttons/WHITE8x8" } )
frame:SetBackdropColor( 0, 0, 0, 1 )
if options.frame_level then
frame:SetFrameLevel( options.frame_level )
end
frame.bottom_border = frame:CreateTexture( nil, "ARTWORK" )
frame.bottom_border:SetTexture( .6, .6, .6, 1 )
frame.bottom_border:SetPoint( "TopLeft", frame, "BottomLeft", -1, 1 )
frame.bottom_border:SetPoint( "BottomRight", frame, "BottomRight", 1, 0 )
if options.close_button then
frame.btn_close = m.GuiElements.tiny_button( parent, "X", "Close Window" )
frame.btn_close:SetPoint( "TopRight", parent, "TopRight", -4, -4 )
frame.btn_close:SetScript( "OnClick", function()
if options.on_close then
options.on_close( frame )
else
parent:Hide()
end
end )
if options.frame_level then
frame.btn_close:SetFrameLevel( options.frame_level + 1 )
end
end
frame.title = frame:CreateFontString( nil, "ARTWORK", "GameFontNormal" )
frame.title:SetPoint( "TopLeft", frame, "TopLeft", 6, -3 )
frame.title:SetTextColor( 1, 1, 1 )
frame.title:SetJustifyH( "Left" )
frame.title:SetText( title )
frame.title:SetFontObject(m.GuiElements.font_highlight )
return frame
end
local function create_main_frame()
local type = options.type or "Frame"
local parent = options.parent or UIParent
---@class BuilderFrame: Frame
local frame = CreateFrame( type, options.name, parent )
frame:SetWidth( options.width or 280 )
frame:SetHeight( options.height or 100 )
frame:EnableMouse( true )
if options.frame_style == "TOOLTIP" then
frame:SetBackdrop( {
bgFile = options.backdrop.bgFile or "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = options.backdrop.edgeFile or "Interface/Tooltips/UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = options.backdrop.edgeSize or 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
} )
elseif options.frame_style == "NONE" then
if options.backdrop then
frame:SetBackdrop( {
bgFile = options.backdrop.bgFile,
edgeFile = options.backdrop.edgeFile,
tile = options.backdrop.tile or false,
tileSize = options.backdrop.tileSize,
edgeSize = options.backdrop.edgeSize,
insets = options.backdrop.insets
} )
end
else
frame:SetBackdrop( {
bgFile = options.backdrop.bgFile or "Interface/Buttons/WHITE8x8",
edgeFile = options.backdrop.edgeFile or "Interface/Buttons/WHITE8x8",
tile = options.backdrop.tile or true,
tileSize = options.backdrop.tileSize or 0,
edgeSize = options.backdrop.edgeSize or 0.8,
insets = options.backdrop.insets or { left = 0, right = 0, top = 0, bottom = 0 }
} )
end
if options.points then
for _, p in pairs( options.points ) do
frame:SetPoint( p.point, p.relative_region or UIParent, p.relative_point, p.x, p.y )
end
else
frame:SetPoint( "TopLeft", UIParent, "Center", -(options.width or 200) / 2, (options.height or 200) / 2 )
end
if options.backdrop_color then
local c = options.backdrop_color
frame:SetBackdropColor( c.r, c.g, c.b, c.a or 1 )
else
frame:SetBackdropColor( 0, 0, 0, 0.7 )
end
if options.border_color then
local c = options.border_color
frame:SetBackdropBorderColor( c.r, c.g, c.b, options.frame_style == "Classic" and 1 or c.a )
end
if options.title then
frame.titlebar = create_titlebar( frame, options.title )
end
if options.hidden then
frame:Hide()
end
if options.frame_level then
frame:SetFrameLevel( options.frame_level )
end
if options.strata then
frame:SetFrameStrata( options.strata )
else
frame:SetFrameStrata( "DIALOG" )
end
frame:SetScript( "OnHide", function()
if is_dragging then
is_dragging = false
frame:StopMovingOrSizing()
end
if options.on_hide then options.on_hide() end
end )
if options.movable then
frame:SetMovable( true )
frame:RegisterForDrag( "LeftButton" )
frame:SetScript( "OnDragStart", function()
if not frame:IsMovable() then return end
is_dragging = true
this:StartMoving()
end )
frame:SetScript( "OnDragStop", function()
if not frame:IsMovable() then return end
is_dragging = false
frame:StopMovingOrSizing()
if options.on_drag_stop then
options.on_drag_stop( frame )
end
end )
else
frame:SetMovable( false )
end
if options.esc then
table.insert( UISpecialFrames, frame:GetName() )
end
if options.scale then
frame:SetScale( options.scale )
end
return frame
end
local frame = create_main_frame()
return frame
end
local function name( self, v )
options.name = v
return self
end
local function type( self, v )
options.type = v
return self
end
local function title( self, v )
options.title = v
return self
end
local function parent( self, v )
options.parent = v
return self
end
local function point( self, _point, relative_region, relative_point, offset_x, offset_y )
options.points = options.points or {}
table.insert( options.points, {
point = _point,
relative_region = relative_region,
relative_point = relative_point,
x = offset_x,
y = offset_y
} )
return self
end
local function width( self, v )
options.width = v
return self
end
local function height( self, v )
options.height = v
return self
end
local function frame_style( self, v )
options.frame_style = v
return self
end
local function frame_level( self, v )
options.frame_level = v
return self
end
local function strata( self, v )
options.strata = v
return self
end
local function movable( self )
options.movable = true
return self
end
local function backdrop( self, _backdrop )
options.backdrop = _backdrop
return self
end
local function backdrop_color( self, r, g, b, a )
options.backdrop_color = { r = r, g = g, b = b, a = a }
return self
end
local function border_color( self, r, g, b, a )
options.border_color = { r = r, g = g, b = b, a = a }
return self
end
local function esc( self )
options.esc = true
return self
end
local function close_button( self )
options.close_button = true
return self
end
local function on_close( self, callback )
options.on_close = callback
if not options.close_button then
options.close_button = true
end
return self
end
local function on_drag_stop( self, callback )
options.on_drag_stop = callback
return self
end
local function on_hide( self, f )
options.on_hide = f
return self
end
local function hidden( self )
options.hidden = true
return self
end
local function build()
return create_frame()
end
---@type FrameBuilder
return {
name = name,
type = type,
title = title,
parent = parent,
point = point,
width = width,
height = height,
frame_level = frame_level,
frame_style = frame_style,
strata = strata,
movable = movable,
backdrop = backdrop,
backdrop_color = backdrop_color,
border_color = border_color,
esc = esc,
close_button = close_button,
on_close = on_close,
on_drag_stop = on_drag_stop,
on_hide = on_hide,
hidden = hidden,
build = build
}
end
m.FrameBuilder = M
return M