-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainWindow.lua
executable file
·409 lines (361 loc) · 11.8 KB
/
mainWindow.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
local AddOnName, components = ...
local ONSRaidTools = LibStub("AceAddon-3.0"):GetAddon(AddOnName)
-- TODO: Add recipe module
local VIEWS = {
IMAGE = "image",
SELECT = "select"
}
local moveCrosshair = "Interface/CURSOR/UI-Cursor-Move.crosshair"
local function createWindow()
-- Create the frame
local frame = CreateFrame("Frame", AddOnName .. "mainWindow", UIParent, "ONSImagViewMainWindowTemplate")
return frame
end
function ONSRaidTools:HideImageViewButtons()
if not self.imageView then return end
self.imageView.tabsHolder:Hide()
self.imageView.menuButton:Hide()
end
function ONSRaidTools:ShowImageViewButtons()
if not self.imageView then return end
self.imageView.tabsHolder:Show()
self.imageView.menuButton:Show()
end
local function CheckFrameMouseover(f)
if not f then return end
if f:IsMouseOver() then
return true
end
local children = { f:GetChildren() }
while #children > 0 do
local child = table.remove(children, 1)
if child:IsMouseOver() then
return true
end
local subChildren = { child:GetChildren() }
for i = 1, #subChildren do
table.insert(children, subChildren[i])
end
end
return false
end
function ONSRaidTools:UpdateImageViewMouseOverState()
if not self.imageView then return end
if CheckFrameMouseover(self.imageView) then
self:ShowImageViewButtons()
else
self:HideImageViewButtons()
end
end
function ONSRaidTools:AddListernersToView(view)
if not view then return error("view is nil") end
view:SetScript("OnEnter", function(f)
if IsShiftKeyDown() then
f:EnableMouse(false)
f:SetAlpha(0.1)
return
end
if view.name == VIEWS.IMAGE then
self:UpdateImageViewMouseOverState()
end
end)
view:SetScript("OnLeave", function(f)
local v = self:GetCurrentView()
if v then
v:SetAlpha(1)
end
if view.name == VIEWS.IMAGE then
self:UpdateImageViewMouseOverState()
end
end)
end
function ONSRaidTools:GetCurrentView()
if not self.activeView then return end
local current = self:GetViewByName(self.activeView)
return current
end
function ONSRaidTools:AddMainListeners()
local frame = self.mainWindow
frame:SetScript("OnDragStart", function(f)
f:StartMoving()
f:SetUserPlaced(false)
SetCursor(moveCrosshair)
end)
frame:SetScript("OnDragStop", function(f)
f:StopMovingOrSizing()
SetCursor(nil)
-- Get the position of the frame
local point, _, relativePoint, xOfs, yOfs = f:GetPoint()
-- If we found a point then save that position
if point and relativePoint and xOfs and xOfs then
self.db.global.position = {
point = point,
relativePoint = relativePoint,
x = xOfs,
y = yOfs
}
end
end)
frame:SetScript("OnLeave", function(f)
local view = self:GetCurrentView()
if view then
-- view:EnableMouse(true)
view:SetAlpha(1)
end
end)
end
function ONSRaidTools:SetInitPosition()
local x, y, point, relativePoint
if self.db.global.position then
x = self.db.global.position.x
y = self.db.global.position.y
relativePoint = self.db.global.position.relativePoint
point = self.db.global.position.point
end
local frame = self.mainWindow
frame:SetPoint(point, UIParent, relativePoint, x, y)
end
local function createImageView()
local frame = CreateFrame("Frame", AddOnName .. "ImageView", UIParent, "ONSRaidToolsTemplate")
return frame
end
function ONSRaidTools:GetViewByName(name)
if name == VIEWS.IMAGE then
if not self.imageView then
self:InitImageView()
end
return self.imageView
elseif name == VIEWS.SELECT then
if not self.selectView then
self:InitSelectView()
end
return self.selectView
end
end
function ONSRaidTools:SetView(view)
if not view then return end
if not self.activeView then return end
local current = self:GetCurrentView()
if current then
current:Hide()
end
view:SetParent(self.mainWindow)
view:SetPoint("TOPLEFT", self.mainWindow, "TOPLEFT", 0, 0)
view:SetPoint("BOTTOMRIGHT", self.mainWindow, "BOTTOMRIGHT", 0, 0)
view:Show()
self:UpdateImageViewMouseOverState()
self.activeView = view.name
end
function ONSRaidTools:LoadImageToView(img)
-- img -> path to image
if not img then return end
self.imageView.img:SetTexture(img)
end
function ONSRaidTools:CreateImageViewTabs(tabsTable, overrideIntitial)
if not tabsTable then return end
local tabs = components:CreateTabs(tabsTable)
components:LayoutTabs(tabs, self.imageView.tabsHolder, 5, 5, 4)
local indexToLoad = 1
if overrideIntitial then
indexToLoad = overrideIntitial
end
-- Set the first tab as active
tabsTable[indexToLoad].callback()
components:SetActiveTab(tabs, indexToLoad)
end
function ONSRaidTools:LoadCurrentImageCollection(overrideIntitial)
if not self.db.global.loadedEncounter then return end
if not self.db.global.loadedEncounter.images then return end
local images = self.db.global.loadedEncounter.images
local tabsTable = {}
for i, path in ipairs(images) do
local tab = {
label = "Image " .. i,
callback = function()
self:LoadImageToView(path)
self.active.image = i
if self.WeakAurasLoaded then
WeakAuras.ScanEvents("ONSRAIDTOOLS_IMAGE_CHANGED")
end
end,
onLeave = function()
self:UpdateImageViewMouseOverState()
end
}
table.insert(tabsTable, tab)
end
self:CreateImageViewTabs(tabsTable, overrideIntitial)
end
function ONSRaidTools:SetEncounterNameByIndex(index)
if not index then return end
local bossName = self:GetBossNameByModuleAndIndex(self.activeRaid, index)
if not bossName then return end
self.imageView.menuButton.bossName:SetText(bossName)
end
-- Example: ONSRaidTools:LoadEncounter(1, "dfs1")
function ONSRaidTools:LoadEncounter(encounterIndex, moduleName, overrideIntitial)
-- encounterIndex -> index of encounter in the raid
if not encounterIndex then
error("encounterIndex not found")
return
end
if not moduleName then
error("moduleName not found")
return
end
local module = self.modules[moduleName]
if not module then
error("module not found")
return
end
local images = module.images[encounterIndex]
if not images then
error("images not found")
return
end
local info = module.bosses[encounterIndex]
if not info then
error("info not found")
return
end
if self.WeakAurasLoaded then
WeakAuras.ScanEvents("ONSRAIDTOOLS_IMAGE_CHANGED")
end
self.db.global.loadedEncounter.images = images
self.db.global.loadedEncounter.info = info
self.active = {
encounterIndex = encounterIndex,
moduleName = moduleName,
image = overrideIntitial or 1
}
self:SetEncounterNameByIndex(encounterIndex)
self:LoadCurrentImageCollection(overrideIntitial)
self:SetView(self.imageView)
end
function ONSRaidTools:InitImageView()
self.imageView = createImageView()
self.imageView.menuButton:SetScript("OnClick", function(f)
if not self.selectView then
self:InitSelectView()
end
self:SetView(self.selectView)
end)
self.imageView.menuButton:SetScript("OnLeave", function(f)
self:UpdateImageViewMouseOverState()
end)
self.imageView.tabsHolder:SetScript("OnLeave", function(f)
self:UpdateImageViewMouseOverState()
end)
self.imageView.name = VIEWS.IMAGE
self:AddListernersToView(self.imageView)
end
local function createSelectView()
local frame = CreateFrame("Frame", nil, UIParent, "ONSSelectViewTemplate")
return frame
end
function ONSRaidTools:SetActiveRaidToSelectView()
-- only for hardcoded raid
-- Set the module to the module with the specified name
local module = self.modules[self.activeRaid]
-- Check if the module is set, if not return
if not module then
error("module not found")
return
end
for i, button in ipairs(self.selectView.encounterButtons) do
button:Hide()
end
local buttonIndex = 1
for bossIndex, bossInfo in ipairs(module.bosses) do
if module.images[bossIndex] then
local button = self.selectView.encounterButtons[buttonIndex]
button.icon:SetTexture(bossInfo.icon)
button.label:SetText(bossInfo.name)
button:SetScript("OnClick", function(f)
self:LoadEncounter(bossIndex, self.activeRaid)
self:SetView(self.imageView)
end)
button:Show()
buttonIndex = buttonIndex + 1
end
end
end
function ONSRaidTools:InitSelectView()
self.selectView = createSelectView()
self:AddListernersToView(self.selectView)
self.selectView.titleBar.backButton:SetScript("OnClick", function(f)
self:SetView(self.imageView)
end)
self.selectView.titleBar.settingsButton:SetScript("OnClick", function(f)
self:OpenSettings()
end)
local gap = 5
local row = 0
local max = 14
self.selectView.encounterButtons = {}
for buttonIndex = 1, max do
local button = CreateFrame("Button", nil, self.selectView.encounterButtonHolder,
"ONSRaidToolsEncounterButtonTemplate")
local width = button:GetWidth()
local height = button:GetHeight()
local col = (buttonIndex % 2 == 0 and 2 or 1)
if col == 1 then
row = row + 1
end
local xOffset = (col - 1) * (width + gap)
local yOffset = -((row - 1) * (height + gap))
button:SetPoint("TOPLEFT", self.selectView.encounterButtonHolder, "TOPLEFT", xOffset, yOffset)
button:Hide()
table.insert(self.selectView.encounterButtons, button)
end
self:SetActiveRaidToSelectView()
self.selectView.name = VIEWS.SELECT
end
function ONSRaidTools:setupMainWindow()
self.mainWindow = createWindow()
self:AddMainListeners()
self:SetInitPosition()
ONSRaidTools:GetViewByName(VIEWS.IMAGE)
self.activeView = VIEWS.IMAGE
self:SetView(self.imageView)
self:LoadOptionsValues()
end
function ONSRaidTools:ToggleFrame()
--Create the window if it doesn't exist
if not self.mainWindow then
self:setupMainWindow()
end
--Show or hide the window depending on its current state
if self.mainWindow:IsShown() then
self.mainWindow:Hide()
else
self.mainWindow:Show()
end
end
function ONSRaidTools:MODIFIER_STATE_CHANGED(e, key, state)
if key ~= "LSHIFT" and key ~= "RSHIFT" then return end
if not self.imageView then return end
if state == 1 and (self.imageView:IsMouseOver(1, 0, 1, 0)) then
self.mainWindow.moveInfo:Show()
self.imageView:EnableMouse(false)
self.imageView:SetAlpha(0.1)
else
self.mainWindow.moveInfo:Hide()
self.imageView:EnableMouse(true)
self.imageView:SetAlpha(1)
if (self.imageView:IsMouseOver(1, 0, 1, 0)) then
self.imageView.tabsHolder:Show()
self.imageView.menuButton:Show()
end
end
if not self.selectView then return end
if state == 1 and (self.selectView:IsMouseOver(1, 0, 1, 0)) then
self.mainWindow.moveInfo:Show()
self.selectView:EnableMouse(false)
self.selectView:SetAlpha(0.1)
else
self.mainWindow.moveInfo:Hide()
self.selectView:EnableMouse(true)
self.selectView:SetAlpha(1)
end
end