forked from jsb/RingMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingMenu.lua
252 lines (215 loc) · 7.66 KB
/
RingMenu.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
-- Default Settings
RingMenu_defaultSettings = {
startPageID = 13,
numButtons = 12,
radius = 100.0,
angleOffset = 0.0,
animationSpeedOpen = 4.0,
animationSpeedClose = 3.0,
backdropScale = 1.5,
colorR = 0.0,
colorG = 0.0,
colorB = 0.0,
colorAlpha = 0.5,
autoClose = true,
zoomButtonIcons = false,
}
function RingMenu_ResetDefaultSettings()
RingMenu_settings = {}
for k, v in pairs(RingMenu_defaultSettings) do
RingMenu_settings[k] = v
end
end
function RingMenu_LoadNewDefaultSettings()
-- Only updates fields that are not present in the current settings dictionary.
-- Used for initializing new settings with sensible initial values after a version update.
for k, v in pairs(RingMenu_defaultSettings) do
if RingMenu_settings[k] == nil then
RingMenu_settings[k] = v
end
end
end
-- Settings (saved variables)
RingMenu_settings = {}
-- Runtime variables
RingMenu_currentSize = 0.0
RingMenu_targetSize = 0.0
RingMenu_currentX = -1.0
RingMenu_targetX = -1.0
RingMenu_currentY = -1.0
RingMenu_targetY = -1.0
RingMenu_isOpen = false
-- Slash Commands
SLASH_RINGMENU1 = "/ringmenu";
function SlashCmdList.RINGMENU(message)
RingMenuSettingsFrame:Show()
end
-- Hooked ActionButton functions
local ActionButton_GetPagedID_Old
function RingMenuButton_GetPagedID(button)
if button.isRingMenu then
return RingMenu_settings.startPageID + button:GetID() - 1
else
return ActionButton_GetPagedID_Old(button)
end
end
function RingMenuButton_OnClick()
this:oldScriptOnClick()
if IsShiftKeyDown() or CursorHasSpell() or CursorHasItem() then
-- User is just changing button slots, keep RingMenu open
elseif RingMenu_settings.autoClose then
-- Clicked a button, close RingMenu
RingMenu_Close()
end
end
function RingMenuButton_OnEnter()
-- Only show the tooltip if the ring menu is currently open
-- Prevents flickering tooltips on fadeout animations
if RingMenu_isOpen then
this:oldScriptOnEnter()
end
end
-- RingMenuFrame callbacks
function RingMenuFrame_OnLoad()
RingMenu_ResetDefaultSettings()
RingMenu_Close()
this:RegisterEvent("VARIABLES_LOADED")
end
function RingMenuFrame_OnEvent(event)
if event == "VARIABLES_LOADED" then
RingMenu_LoadNewDefaultSettings()
RingMenuFrame_ConfigureButtons()
RingMenuSettings_SetupSettingsFrame()
-- Hook global button callbacks
ActionButton_GetPagedID_Old = ActionButton_GetPagedID
ActionButton_GetPagedID = RingMenuButton_GetPagedID
end
end
RingMenu_usedButtons = {}
function RingMenuFrame_ConfigureButtons()
-- Hide all used buttons
for _, button in ipairs(RingMenu_usedButtons) do
button:Hide()
button:Disable()
button:SetPoint("CENTER", "UIParent", "BOTTOMLEFT", -1000, -1000)
end
RingMenu_usedButtons = {}
-- Create ring menu buttons
for i = 1, RingMenu_settings.numButtons do
local buttonName = "RingMenuButton" .. i
local button = getglobal(buttonName) -- Try to reuse a button, if available
if not button then -- No reusable button, create a new one
button = CreateFrame("CheckButton", buttonName, RingMenuFrame, "BonusActionButtonTemplate")
-- Hide Hotkey text
local hotkey = getglobal(buttonName .. "HotKey")
hotkey:Hide()
-- Hook individual button callbacks
button.oldScriptOnClick = button:GetScript("OnClick")
button:SetScript("OnClick", RingMenuButton_OnClick)
button.oldScriptOnEnter = button:GetScript("OnEnter")
button:SetScript("OnEnter", RingMenuButton_OnEnter)
end
button:SetID(i)
button:SetPoint("CENTER", RingMenuFrame, "CENTER", 0, 0)
button:SetFrameLevel(2)
button.isRingMenu = true
button.isBonus = true
button.buttonType = "RING_MENU"
local icon = getglobal(buttonName .. "Icon")
if cyCircled_RingMenu and RingMenu_settings.zoomButtonIcons then
icon:SetTexCoord(0.07, 0.93, 0.07, 0.93)
else
icon:SetTexCoord(0.0, 1.0, 0.0, 1.0)
end
table.insert(RingMenu_usedButtons, button)
button:Enable()
button:Show()
this = button
ActionButton_Update()
end
RingMenu_UpdateButtonPositions()
end
function RingMenuFrame_OnUpdate(elapsed)
if RingMenu_currentSize ~= RingMenu_targetSize then
-- Snap to target size if within epsilon
if math.abs(RingMenu_currentSize - RingMenu_targetSize) < 0.001 then
RingMenu_currentSize = RingMenu_targetSize
end
-- Animate
local animationSpeed = 0.0
if RingMenu_isOpen then
animationSpeed = RingMenu_settings.animationSpeedOpen
else
animationSpeed = RingMenu_settings.animationSpeedClose
end
local alpha = math.pow(0.001, elapsed * animationSpeed)
RingMenu_currentSize = RingMenu_Lerp(RingMenu_targetSize, RingMenu_currentSize, alpha)
RingMenu_currentX = RingMenu_Lerp(RingMenu_targetX, RingMenu_currentX, alpha)
RingMenu_currentY = RingMenu_Lerp(RingMenu_targetY, RingMenu_currentY, alpha)
-- Update appearance
RingMenu_UpdateButtonPositions()
end
-- Hide frame when the closing animation has finished
if (not RingMenu_isOpen) and RingMenu_currentSize == RingMenu_targetSize then
RingMenuFrame:Hide()
end
end
-- RingMenu methods
function RingMenu_Lerp(a, b, alpha)
return a * (1 - alpha) + b * alpha
end
function RingMenu_UpdateButtonPositions()
-- Button positions
local radius = RingMenu_settings.radius * RingMenu_currentSize
local angleOffsetRadians = RingMenu_settings.angleOffset / 180.0 * math.pi
for i = 1, RingMenu_settings.numButtons do
local button = getglobal("RingMenuButton" .. i)
local angle = angleOffsetRadians + 2.0 * math.pi * (i - 1) / RingMenu_settings.numButtons
local buttonX = radius * math.sin(angle)
local buttonY = radius * math.cos(angle)
button:SetPoint("CENTER", RingMenuFrame, "CENTER", buttonX, buttonY)
button:SetAlpha(RingMenu_currentSize)
end
-- Background shadow
local backdropAlpha = RingMenu_currentSize * RingMenu_settings.colorAlpha
RingMenuTextureShadow:SetVertexColor(RingMenu_settings.colorR, RingMenu_settings.colorG, RingMenu_settings.colorB, backdropAlpha);
-- Ring size
local size = RingMenu_currentSize * 2 * RingMenu_settings.radius * RingMenu_settings.backdropScale
RingMenuFrame:SetWidth(size)
RingMenuFrame:SetHeight(size)
-- Ring position
RingMenuFrame:SetPoint("CENTER", "UIParent", "BOTTOMLEFT", RingMenu_currentX, RingMenu_currentY)
end
function RingMenu_Toggle()
if RingMenu_isOpen then
RingMenu_Close()
else
RingMenu_Open()
end
end
function RingMenu_GetMousePosition()
local mouseX, mouseY = GetCursorPosition()
local uiScale = RingMenuFrame:GetParent():GetEffectiveScale()
mouseX = mouseX / uiScale
mouseY = mouseY / uiScale
return mouseX, mouseY
end
function RingMenu_Close()
local mouseX, mouseY = RingMenu_GetMousePosition()
RingMenu_targetSize = 0.0
RingMenu_targetX = mouseX
RingMenu_targetY = mouseY
RingMenu_isOpen = false
end
function RingMenu_Open()
local mouseX, mouseY = RingMenu_GetMousePosition()
RingMenu_targetSize = 1.0
RingMenu_targetX = mouseX
RingMenu_targetY = mouseY
if RingMenu_currentSize == 0.0 then
RingMenu_currentX = RingMenu_targetX
RingMenu_currentY = RingMenu_targetY
end
RingMenu_isOpen = true
RingMenuFrame:Show()
end