-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWindow.lua
206 lines (187 loc) · 7.51 KB
/
Window.lua
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
---@type string
local addonName = select(1, ...)
---@class AE_Addon
local addon = select(2, ...)
---@type Frame[]
local WindowCollection = {}
local TITLEBAR_HEIGHT = 30
local FOOTER_HEIGHT = 16
-- local SIDEBAR_WIDTH = 150
---@class AE_WindowManager
local Window = {}
addon.Window = Window
---Create a window frame
---@param options AE_WindowOptions
---@return AE_Window
function Window:New(options)
---@class AE_Window : Frame
local window = CreateFrame("Frame", addonName .. "Window123123" .. (options and options.name or #WindowCollection + 1), options.parent or UIParent)
window.config = CreateFromMixins(
{
parent = UIParent,
name = "",
title = "",
border = addon.Constants.sizes.border,
titlebar = true,
windowScale = 100,
windowColor = {r = 0.11372549019, g = 0.14117647058, b = 0.16470588235, a = 1},
point = {"CENTER"},
},
options or {}
)
window:SetFrameStrata("MEDIUM")
window:SetFrameLevel(3000)
window:SetToplevel(true)
window:SetMovable(true)
window:SetPoint(unpack(window.config.point))
window:SetSize(300, 300)
window:EnableMouse(true) -- Disable click-throughs
window:SetParent(window.config.parent)
window:SetClampedToScreen(true)
window:SetClampRectInsets(window:GetWidth() / 2, window:GetWidth() / -2, 0, window:GetHeight() / 2)
window:SetScript("OnSizeChanged", function()
window:SetClampRectInsets(window:GetWidth() / 2, window:GetWidth() / -2, 0, window:GetHeight() / 2)
end)
addon.Utils:SetBackgroundColor(window, window.config.windowColor.r, window.config.windowColor.g, window.config.windowColor.b, window.config.windowColor.a)
---Show or hide the window
---@param state boolean?
function window:Toggle(state)
if state == nil then
state = not window:IsVisible()
end
window:SetShown(state)
end
---Set the title of the window
---@param title string
function window:SetTitle(title)
if not window.config.titlebar then return end
window.titlebar.title:SetText(title)
end
---Set body size and adjust window size
---@param width number
---@param height number
function window:SetBodySize(width, height)
local w = width
local h = height
if window.config.sidebar then
w = w + window.config.sidebar
end
if window.config.titlebar then
h = h + TITLEBAR_HEIGHT
end
window:SetSize(w, h)
end
-- Border
if window.config.border > 0 then
window.border = CreateFrame("Frame", "$parentBorder", window, "BackdropTemplate")
window.border:SetPoint("TOPLEFT", window, "TOPLEFT", -3, 3)
window.border:SetPoint("BOTTOMRIGHT", window, "BOTTOMRIGHT", 3, -3)
window.border:SetBackdrop({edgeFile = "Interface/Tooltips/UI-Tooltip-Border", edgeSize = 16, insets = {left = window.config.border, right = window.config.border, top = window.config.border, bottom = window.config.border}})
window.border:SetBackdropBorderColor(0, 0, 0, .5)
window.border:Show()
end
-- Titlebar
if window.config.titlebar then
window.titlebar = CreateFrame("Frame", "$parentTitleBar", window)
window.titlebar:EnableMouse(true)
window.titlebar:RegisterForDrag("LeftButton")
window.titlebar:SetScript("OnDragStart", function() window:StartMoving() end)
window.titlebar:SetScript("OnDragStop", function() window:StopMovingOrSizing() end)
window.titlebar:SetPoint("TOPLEFT", window, "TOPLEFT")
window.titlebar:SetPoint("TOPRIGHT", window, "TOPRIGHT")
window.titlebar:SetHeight(TITLEBAR_HEIGHT)
addon.Utils:SetBackgroundColor(window.titlebar, 0, 0, 0, 0.5)
window.titlebar.icon = window.titlebar:CreateTexture("$parentIcon", "ARTWORK")
window.titlebar.icon:SetPoint("LEFT", window.titlebar, "LEFT", 6, 0)
window.titlebar.icon:SetSize(20, 20)
window.titlebar.icon:SetTexture(addon.Constants.media.LogoTransparent)
window.titlebar.title = window.titlebar:CreateFontString("$parentText", "OVERLAY")
window.titlebar.title:SetPoint("LEFT", window.titlebar, "LEFT", 20 + addon.Constants.sizes.padding, 0)
window.titlebar.title:SetFontObject("SystemFont_Med3")
window.titlebar.title:SetText(window.config.title or window.config.name)
window.titlebar.CloseButton = CreateFrame("Button", "$parentCloseButton", window.titlebar)
window.titlebar.CloseButton:SetPoint("RIGHT", window.titlebar, "RIGHT", 0, 0)
window.titlebar.CloseButton:SetSize(TITLEBAR_HEIGHT, TITLEBAR_HEIGHT)
window.titlebar.CloseButton:RegisterForClicks("AnyUp")
window.titlebar.CloseButton:SetScript("OnClick", function() window:Hide() end)
window.titlebar.CloseButton.Icon = window.titlebar:CreateTexture("$parentIcon", "ARTWORK")
window.titlebar.CloseButton.Icon:SetPoint("CENTER", window.titlebar.CloseButton, "CENTER")
window.titlebar.CloseButton.Icon:SetSize(10, 10)
window.titlebar.CloseButton.Icon:SetTexture(addon.Constants.media.IconClose)
window.titlebar.CloseButton.Icon:SetVertexColor(0.7, 0.7, 0.7, 1)
window.titlebar.CloseButton:SetScript("OnEnter", function()
window.titlebar.CloseButton.Icon:SetVertexColor(1, 1, 1, 1)
addon.Utils:SetBackgroundColor(window.titlebar.CloseButton, 1, 0, 0, 0.2)
GameTooltip:ClearAllPoints()
GameTooltip:ClearLines()
GameTooltip:SetOwner(window.titlebar.CloseButton, "ANCHOR_TOP")
GameTooltip:SetText("Close the window", 1, 1, 1, 1, true)
GameTooltip:Show()
end)
window.titlebar.CloseButton:SetScript("OnLeave", function()
window.titlebar.CloseButton.Icon:SetVertexColor(0.7, 0.7, 0.7, 1)
addon.Utils:SetBackgroundColor(window.titlebar.CloseButton, 1, 1, 1, 0)
GameTooltip:Hide()
end)
end
local topOffset = 0
local leftOffset = 0
if window.config.titlebar then
topOffset = -TITLEBAR_HEIGHT
end
if window.config.sidebar then
leftOffset = window.config.sidebar
end
-- Body
window.body = CreateFrame("Frame", "$parentBody", window)
window.body:SetPoint("TOPLEFT", window, "TOPLEFT", leftOffset, topOffset)
window.body:SetPoint("TOPRIGHT", window, "TOPRIGHT", 0, topOffset)
window.body:SetPoint("BOTTOMLEFT", window, "BOTTOMLEFT", leftOffset, 0)
window.body:SetPoint("BOTTOMRIGHT", window, "BOTTOMRIGHT", 0, 0)
addon.Utils:SetBackgroundColor(window.body, 0, 0, 0, 0)
-- Sidebar
if window.config.sidebar then
window.sidebar = CreateFrame("Frame", "$parentSidebar", window)
window.sidebar:SetPoint("TOPLEFT", window, "TOPLEFT", 0, topOffset)
window.sidebar:SetPoint("BOTTOMLEFT", window, "BOTTOMLEFT")
window.sidebar:SetWidth(window.config.sidebar)
addon.Utils:SetBackgroundColor(window.sidebar, 0, 0, 0, 0.3)
end
window:Hide()
table.insert(UISpecialFrames, window:GetName())
WindowCollection[window.config.name] = window
return window
end
---Get a window by name
---@param name string
---@return Frame
function Window:GetWindow(name)
return WindowCollection[name]
end
---Scale each window
---@param scale number
function Window:SetWindowScale(scale)
addon.Utils:TableForEach(WindowCollection, function(window)
window:SetScale(scale)
end)
end
---Set background color to each window
---@param color ColorMixin
function Window:SetWindowBackgroundColor(color)
addon.Utils:TableForEach(WindowCollection, function(window)
addon.Utils:SetBackgroundColor(window, color.r, color.g, color.b, color.a)
end)
end
function Window:GetMaxWindowWidth()
return GetScreenWidth() - 100
end
function Window:ToggleWindow(name)
if name == nil or name == "" then name = "Main" end
local window = self:GetWindow(name)
if not window then return end
if window:IsVisible() then
window:Hide()
else
window:Show()
end
end