-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.lua
293 lines (260 loc) · 8.34 KB
/
Grid.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
local _, SpellBinding = ...
local Grid = SpellBinding:NewModule("Grid", CreateFrame("Frame"))
local currentKey, currentIndex
local function onValueChanged(self, value, isUserInput)
self.currentValue:SetText(value)
if not isUserInput then return end
Grid.db.global[self.setting] = value
Grid:UpdateGrid()
end
local function createSlider(maxValue)
local slider = SpellBinding:CreateSlider(Grid)
slider:SetWidth(96)
slider.min:ClearAllPoints()
slider.min:SetPoint("LEFT", -12, 0)
slider.max:ClearAllPoints()
slider.max:SetPoint("RIGHT", 12, 0)
slider.currentValue:ClearAllPoints()
slider.currentValue:SetPoint("LEFT", slider.label, "RIGHT")
slider.currentValue:SetFontObject("GameFontHighlight")
slider:SetMinMaxValues(1, maxValue)
slider:SetValueStep(1)
slider.min:SetText(1)
slider.max:SetText(maxValue)
slider:SetScript("OnValueChanged", onValueChanged)
return slider
end
local rowsSlider = createSlider(8)
rowsSlider:SetPoint("TOPLEFT", 40, -40)
rowsSlider.setting = "gridRows"
rowsSlider.label:SetText("Rows: ")
local columnsSlider = createSlider(7)
columnsSlider:SetPoint("TOPRIGHT", -40, -40)
columnsSlider.setting = "gridColumns"
columnsSlider.label:SetText("Columns: ")
local overlay = SpellBinding:CreateBindingOverlay(Grid)
overlay.OnAccept = function(self)
Grid.db.global.keys[currentIndex] = currentKey
Grid:UpdateGridBindings()
end
overlay.OnBinding = function(self, keyPressed)
self:SetBindingKeyText(keyPressed)
currentKey = keyPressed
end
overlay:SetScript("OnShow", function(self)
self:SetBindingActionText("Button "..currentIndex)
self:SetBindingKeyText(Grid.db.global.keys[currentIndex])
end)
overlay:SetScript("OnHide", function(self)
currentKey = nil
end)
local function onClick(self, key, action)
SpellBinding:SetPrimaryBinding(action, self.value, key)
end
local selectSetMenu = SpellBinding:CreateDropdown("Menu")
selectSetMenu.initialize = function(self)
local info = UIDropDownMenu_CreateInfo()
info.text = "Select binding set"
info.isTitle = true
info.notCheckable = true
self:AddButton(info)
local activeAction, activeSet = SpellBinding:GetActiveActionForKey(self.key)
for i, set in SpellBinding:IterateActiveSets() do
local info = UIDropDownMenu_CreateInfo()
info.text = SpellBinding:GetSetName(set)
info.value = set
info.func = onClick
info.arg1 = self.key
info.arg2 = self.action
info.notCheckable = true
if activeAction then
info.colorCode = (set == activeSet) and LIGHTYELLOW_FONT_COLOR_CODE
info.tooltipTitle = format("Bind %s to |cffffd200%s|r (%s)",
SpellBinding:GetActionLabel(self.action),
GetBindingText(self.key),
SpellBinding:GetSetName(set))
local text1, text2 = SpellBinding:GetConflictText(self.key, self.action, set, activeAction, activeSet)
info.tooltipText = (text1 or "").."\n"..(text2 or "")
info.tooltipLines = true
end
self:AddButton(info)
end
end
local function dropAction(self, button)
if not self.key then return end
-- button is always nil OnReceiveDrag, should never be nil OnClick
if button == "LeftButton" or not button then
local action = SpellBinding:GetActionStringFromCursor()
if not action then
return
end
selectSetMenu.key = self.key
selectSetMenu.action = action
HideDropDownMenu(1)
selectSetMenu:Toggle(nil, self)
end
ClearCursor()
end
local options = {
{
text = "Set key",
func = function(self, index)
currentIndex = index
overlay:Show()
end,
},
{
text = "Remove key",
func = function(self, index)
Grid.db.global.keys[index] = nil
Grid:UpdateGridBindings()
end,
},
}
local menu = SpellBinding:CreateDropdown("Menu")
menu.initialize = function(self)
local index = UIDROPDOWNMENU_MENU_VALUE
for i, option in ipairs(options) do
local info = UIDropDownMenu_CreateInfo()
info.text = option.text
info.func = option.func
info.arg1 = index
info.notCheckable = true
self:AddButton(info)
end
end
local function onClick(self, button)
if GetCursorInfo() then
dropAction(self, button)
return
end
if not self.key then
currentIndex = self:GetID()
overlay:Show()
return
end
if self:GetID() ~= UIDROPDOWNMENU_MENU_VALUE then
menu:Close()
end
menu:Toggle(self:GetID(), self)
end
local function onEnter(self)
if not self.key then
local cursorInfo = GetCursorInfo()
local cursorAction = SpellBinding:GetActionStringFromCursor()
if not cursorInfo or cursorAction then
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
if not cursorInfo then
GameTooltip:AddLine("Click to set a key for this cell")
elseif cursorAction then
GameTooltip:AddLine("Key must be set on this cell before binding", RED_FONT_COLOR:GetRGB())
end
GameTooltip:Show()
end
return
end
if not self.action then return end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:AddLine(GetBindingText(self.key), HIGHLIGHT_FONT_COLOR:GetRGB())
GameTooltip:AddDoubleLine(SpellBinding:GetActionLabel(self.action), SpellBinding:GetSetName(self.set))
GameTooltip:Show()
end
local buttons = setmetatable({}, {
__index = function(table, index)
local button = CreateFrame("CheckButton", nil, Grid)
button:SetSize(36, 36)
button:SetScript("OnClick", onClick)
button:SetScript("OnEnter", onEnter)
button:SetScript("OnLeave", GameTooltip_Hide)
button:SetScript("OnReceiveDrag", dropAction)
button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
button:SetHighlightTexture([[Interface\Buttons\ButtonHilight-Square]])
local bg = button:CreateTexture(nil, "BACKGROUND")
bg:SetSize(45, 45)
bg:SetPoint("CENTER", 0, -1)
bg:SetTexture([[Interface\Buttons\UI-EmptySlot-Disabled]])
bg:SetTexCoord(0.140625, 0.84375, 0.140625, 0.84375)
button.name = button:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmallOutline")
button.name:SetSize(36, 10)
button.name:SetPoint("BOTTOM", 0, 2)
button.icon = button:CreateTexture()
button.icon:SetSize(36, 36)
button.icon:SetPoint("CENTER", 0, -1)
button.hotKey = button:CreateFontString(nil, nil, "NumberFontNormalSmallGray")
button.hotKey:SetSize(36, 10)
button.hotKey:SetPoint("TOPLEFT", 1, -3)
button.hotKey:SetJustifyH("RIGHT")
table[index] = button
return button
end
})
Grid:SetScript("OnHide", function(self)
selectSetMenu:Close()
menu:Close()
end)
local defaults = {
global = {
keys = {},
-- hiddenButtons = {},
gridRows = 4,
gridColumns = 3,
}
}
function Grid:OnInitialize()
self.UPDATE_BINDINGS = self.UpdateGridBindings
if SpellBinding.db.sv.namespaces and SpellBinding.db.sv.namespaces.Custom then
SpellBinding.db.sv.namespaces.Grid = SpellBinding.db.sv.namespaces.Custom
SpellBinding.db.sv.namespaces.Custom = nil
end
self.db = SpellBinding.db:RegisterNamespace("Grid", defaults)
rowsSlider:SetValue(self.db.global.gridRows)
columnsSlider:SetValue(self.db.global.gridColumns)
self:RegisterEvent("PLAYER_REGEN_DISABLED")
self:RegisterEvent("SPELLS_CHANGED", "UpdateGridBindings")
self:RegisterEvent("UPDATE_MACROS", "UpdateGridBindings")
self:UpdateGrid()
end
function Grid:PLAYER_REGEN_DISABLED()
selectSetMenu:Close()
menu:Close()
end
local XPADDING = 10
local YPADDING = 8
function Grid:UpdateGrid()
local gridRows = self.db.global.gridRows
local gridColumns = self.db.global.gridColumns
local numButtons = gridRows * gridColumns
for i = 1, numButtons do
local button = buttons[i]
button:SetID(i)
button:Show()
if i == 1 then
-- position the grid in the center of the frame
local gridWidth = gridColumns * (button:GetWidth() + XPADDING) - XPADDING
local gridHeight = gridRows * (button:GetHeight() + YPADDING) - YPADDING
button:SetPoint("TOPLEFT", Grid.Inset, "CENTER", -gridWidth / 2, gridHeight / 2)
elseif (i % gridColumns == 1) or (gridColumns == 1) then
button:SetPoint("TOPLEFT", buttons[i - gridColumns], "BOTTOMLEFT", 0, -YPADDING)
else
button:SetPoint("TOPLEFT", buttons[i - 1], "TOPRIGHT", XPADDING, 0)
end
buttons[i] = button
end
for i = numButtons + 1, #buttons do
buttons[i]:Hide()
end
end
function Grid:UpdateGridBindings()
for i = 1, self.db.global.gridRows * self.db.global.gridColumns do
local button = buttons[i]
local key = self.db.global.keys[i]
button.hotKey:SetText(GetBindingText(key, true))
local action, set = SpellBinding:GetActiveActionForKey(key)
local name, texture = SpellBinding:GetActionInfo(action)
button.name:SetText(name)
button.icon:SetTexture(texture)
button.key = key
button.action = SpellBinding:GetActionStringReverse(action) or action
button.set = set
end
end