yellowfive@57
|
1 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
2 Keybinding Widget
|
yellowfive@57
|
3 Set Keybindings in the Config UI.
|
yellowfive@57
|
4 -------------------------------------------------------------------------------]]
|
yellowfive@106
|
5 local Type, Version = "Keybinding", 25
|
yellowfive@57
|
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
|
yellowfive@57
|
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
|
yellowfive@57
|
8
|
yellowfive@57
|
9 -- Lua APIs
|
yellowfive@57
|
10 local pairs = pairs
|
yellowfive@57
|
11
|
yellowfive@57
|
12 -- WoW APIs
|
yellowfive@57
|
13 local IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown = IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown
|
yellowfive@57
|
14 local CreateFrame, UIParent = CreateFrame, UIParent
|
yellowfive@57
|
15
|
yellowfive@57
|
16 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
yellowfive@57
|
17 -- List them here for Mikk's FindGlobals script
|
yellowfive@57
|
18 -- GLOBALS: NOT_BOUND
|
yellowfive@57
|
19
|
yellowfive@57
|
20 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
21 Scripts
|
yellowfive@57
|
22 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
23
|
yellowfive@57
|
24 local function Control_OnEnter(frame)
|
yellowfive@57
|
25 frame.obj:Fire("OnEnter")
|
yellowfive@57
|
26 end
|
yellowfive@57
|
27
|
yellowfive@57
|
28 local function Control_OnLeave(frame)
|
yellowfive@57
|
29 frame.obj:Fire("OnLeave")
|
yellowfive@57
|
30 end
|
yellowfive@57
|
31
|
yellowfive@57
|
32 local function Keybinding_OnClick(frame, button)
|
yellowfive@57
|
33 if button == "LeftButton" or button == "RightButton" then
|
yellowfive@57
|
34 local self = frame.obj
|
yellowfive@57
|
35 if self.waitingForKey then
|
yellowfive@57
|
36 frame:EnableKeyboard(false)
|
yellowfive@106
|
37 frame:EnableMouseWheel(false)
|
yellowfive@57
|
38 self.msgframe:Hide()
|
yellowfive@57
|
39 frame:UnlockHighlight()
|
yellowfive@57
|
40 self.waitingForKey = nil
|
yellowfive@57
|
41 else
|
yellowfive@57
|
42 frame:EnableKeyboard(true)
|
yellowfive@106
|
43 frame:EnableMouseWheel(true)
|
yellowfive@57
|
44 self.msgframe:Show()
|
yellowfive@57
|
45 frame:LockHighlight()
|
yellowfive@57
|
46 self.waitingForKey = true
|
yellowfive@57
|
47 end
|
yellowfive@57
|
48 end
|
yellowfive@57
|
49 AceGUI:ClearFocus()
|
yellowfive@57
|
50 end
|
yellowfive@57
|
51
|
yellowfive@57
|
52 local ignoreKeys = {
|
yellowfive@57
|
53 ["BUTTON1"] = true, ["BUTTON2"] = true,
|
yellowfive@57
|
54 ["UNKNOWN"] = true,
|
yellowfive@57
|
55 ["LSHIFT"] = true, ["LCTRL"] = true, ["LALT"] = true,
|
yellowfive@57
|
56 ["RSHIFT"] = true, ["RCTRL"] = true, ["RALT"] = true,
|
yellowfive@57
|
57 }
|
yellowfive@57
|
58 local function Keybinding_OnKeyDown(frame, key)
|
yellowfive@57
|
59 local self = frame.obj
|
yellowfive@57
|
60 if self.waitingForKey then
|
yellowfive@57
|
61 local keyPressed = key
|
yellowfive@57
|
62 if keyPressed == "ESCAPE" then
|
yellowfive@57
|
63 keyPressed = ""
|
yellowfive@57
|
64 else
|
yellowfive@57
|
65 if ignoreKeys[keyPressed] then return end
|
yellowfive@57
|
66 if IsShiftKeyDown() then
|
yellowfive@57
|
67 keyPressed = "SHIFT-"..keyPressed
|
yellowfive@57
|
68 end
|
yellowfive@57
|
69 if IsControlKeyDown() then
|
yellowfive@57
|
70 keyPressed = "CTRL-"..keyPressed
|
yellowfive@57
|
71 end
|
yellowfive@57
|
72 if IsAltKeyDown() then
|
yellowfive@57
|
73 keyPressed = "ALT-"..keyPressed
|
yellowfive@57
|
74 end
|
yellowfive@57
|
75 end
|
yellowfive@57
|
76
|
yellowfive@57
|
77 frame:EnableKeyboard(false)
|
yellowfive@106
|
78 frame:EnableMouseWheel(false)
|
yellowfive@57
|
79 self.msgframe:Hide()
|
yellowfive@57
|
80 frame:UnlockHighlight()
|
yellowfive@57
|
81 self.waitingForKey = nil
|
yellowfive@57
|
82
|
yellowfive@57
|
83 if not self.disabled then
|
yellowfive@57
|
84 self:SetKey(keyPressed)
|
yellowfive@57
|
85 self:Fire("OnKeyChanged", keyPressed)
|
yellowfive@57
|
86 end
|
yellowfive@57
|
87 end
|
yellowfive@57
|
88 end
|
yellowfive@57
|
89
|
yellowfive@57
|
90 local function Keybinding_OnMouseDown(frame, button)
|
yellowfive@57
|
91 if button == "LeftButton" or button == "RightButton" then
|
yellowfive@57
|
92 return
|
yellowfive@57
|
93 elseif button == "MiddleButton" then
|
yellowfive@57
|
94 button = "BUTTON3"
|
yellowfive@57
|
95 elseif button == "Button4" then
|
yellowfive@57
|
96 button = "BUTTON4"
|
yellowfive@57
|
97 elseif button == "Button5" then
|
yellowfive@57
|
98 button = "BUTTON5"
|
yellowfive@57
|
99 end
|
yellowfive@57
|
100 Keybinding_OnKeyDown(frame, button)
|
yellowfive@57
|
101 end
|
yellowfive@57
|
102
|
yellowfive@106
|
103 local function Keybinding_OnMouseWheel(frame, direction)
|
yellowfive@106
|
104 local button
|
yellowfive@106
|
105 if direction >= 0 then
|
yellowfive@106
|
106 button = "MOUSEWHEELUP"
|
yellowfive@106
|
107 else
|
yellowfive@106
|
108 button = "MOUSEWHEELDOWN"
|
yellowfive@106
|
109 end
|
yellowfive@106
|
110 Keybinding_OnKeyDown(frame, button)
|
yellowfive@106
|
111 end
|
yellowfive@106
|
112
|
yellowfive@57
|
113 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
114 Methods
|
yellowfive@57
|
115 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
116 local methods = {
|
yellowfive@57
|
117 ["OnAcquire"] = function(self)
|
yellowfive@57
|
118 self:SetWidth(200)
|
yellowfive@57
|
119 self:SetLabel("")
|
yellowfive@57
|
120 self:SetKey("")
|
yellowfive@57
|
121 self.waitingForKey = nil
|
yellowfive@57
|
122 self.msgframe:Hide()
|
yellowfive@57
|
123 self:SetDisabled(false)
|
yellowfive@57
|
124 self.button:EnableKeyboard(false)
|
yellowfive@106
|
125 self.button:EnableMouseWheel(false)
|
yellowfive@57
|
126 end,
|
yellowfive@57
|
127
|
yellowfive@57
|
128 -- ["OnRelease"] = nil,
|
yellowfive@57
|
129
|
yellowfive@57
|
130 ["SetDisabled"] = function(self, disabled)
|
yellowfive@57
|
131 self.disabled = disabled
|
yellowfive@57
|
132 if disabled then
|
yellowfive@57
|
133 self.button:Disable()
|
yellowfive@57
|
134 self.label:SetTextColor(0.5,0.5,0.5)
|
yellowfive@57
|
135 else
|
yellowfive@57
|
136 self.button:Enable()
|
yellowfive@57
|
137 self.label:SetTextColor(1,1,1)
|
yellowfive@57
|
138 end
|
yellowfive@57
|
139 end,
|
yellowfive@57
|
140
|
yellowfive@57
|
141 ["SetKey"] = function(self, key)
|
yellowfive@57
|
142 if (key or "") == "" then
|
yellowfive@57
|
143 self.button:SetText(NOT_BOUND)
|
yellowfive@57
|
144 self.button:SetNormalFontObject("GameFontNormal")
|
yellowfive@57
|
145 else
|
yellowfive@57
|
146 self.button:SetText(key)
|
yellowfive@57
|
147 self.button:SetNormalFontObject("GameFontHighlight")
|
yellowfive@57
|
148 end
|
yellowfive@57
|
149 end,
|
yellowfive@57
|
150
|
yellowfive@57
|
151 ["GetKey"] = function(self)
|
yellowfive@57
|
152 local key = self.button:GetText()
|
yellowfive@57
|
153 if key == NOT_BOUND then
|
yellowfive@57
|
154 key = nil
|
yellowfive@57
|
155 end
|
yellowfive@57
|
156 return key
|
yellowfive@57
|
157 end,
|
yellowfive@57
|
158
|
yellowfive@57
|
159 ["SetLabel"] = function(self, label)
|
yellowfive@57
|
160 self.label:SetText(label or "")
|
yellowfive@57
|
161 if (label or "") == "" then
|
yellowfive@57
|
162 self.alignoffset = nil
|
yellowfive@57
|
163 self:SetHeight(24)
|
yellowfive@57
|
164 else
|
yellowfive@57
|
165 self.alignoffset = 30
|
yellowfive@57
|
166 self:SetHeight(44)
|
yellowfive@57
|
167 end
|
yellowfive@57
|
168 end,
|
yellowfive@57
|
169 }
|
yellowfive@57
|
170
|
yellowfive@57
|
171 --[[-----------------------------------------------------------------------------
|
yellowfive@57
|
172 Constructor
|
yellowfive@57
|
173 -------------------------------------------------------------------------------]]
|
yellowfive@57
|
174
|
yellowfive@57
|
175 local ControlBackdrop = {
|
yellowfive@57
|
176 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
|
yellowfive@57
|
177 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
|
yellowfive@57
|
178 tile = true, tileSize = 16, edgeSize = 16,
|
yellowfive@57
|
179 insets = { left = 3, right = 3, top = 3, bottom = 3 }
|
yellowfive@57
|
180 }
|
yellowfive@57
|
181
|
yellowfive@57
|
182 local function keybindingMsgFixWidth(frame)
|
yellowfive@57
|
183 frame:SetWidth(frame.msg:GetWidth() + 10)
|
yellowfive@57
|
184 frame:SetScript("OnUpdate", nil)
|
yellowfive@57
|
185 end
|
yellowfive@57
|
186
|
yellowfive@57
|
187 local function Constructor()
|
yellowfive@57
|
188 local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type)
|
yellowfive@57
|
189
|
yellowfive@57
|
190 local frame = CreateFrame("Frame", nil, UIParent)
|
yellowfive@106
|
191 local button = CreateFrame("Button", name, frame, "UIPanelButtonTemplate")
|
yellowfive@57
|
192
|
yellowfive@57
|
193 button:EnableMouse(true)
|
yellowfive@106
|
194 button:EnableMouseWheel(false)
|
yellowfive@57
|
195 button:RegisterForClicks("AnyDown")
|
yellowfive@57
|
196 button:SetScript("OnEnter", Control_OnEnter)
|
yellowfive@57
|
197 button:SetScript("OnLeave", Control_OnLeave)
|
yellowfive@57
|
198 button:SetScript("OnClick", Keybinding_OnClick)
|
yellowfive@57
|
199 button:SetScript("OnKeyDown", Keybinding_OnKeyDown)
|
yellowfive@57
|
200 button:SetScript("OnMouseDown", Keybinding_OnMouseDown)
|
yellowfive@106
|
201 button:SetScript("OnMouseWheel", Keybinding_OnMouseWheel)
|
yellowfive@57
|
202 button:SetPoint("BOTTOMLEFT")
|
yellowfive@57
|
203 button:SetPoint("BOTTOMRIGHT")
|
yellowfive@57
|
204 button:SetHeight(24)
|
yellowfive@57
|
205 button:EnableKeyboard(false)
|
yellowfive@57
|
206
|
yellowfive@57
|
207 local text = button:GetFontString()
|
yellowfive@57
|
208 text:SetPoint("LEFT", 7, 0)
|
yellowfive@57
|
209 text:SetPoint("RIGHT", -7, 0)
|
yellowfive@57
|
210
|
yellowfive@57
|
211 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
|
yellowfive@57
|
212 label:SetPoint("TOPLEFT")
|
yellowfive@57
|
213 label:SetPoint("TOPRIGHT")
|
yellowfive@57
|
214 label:SetJustifyH("CENTER")
|
yellowfive@57
|
215 label:SetHeight(18)
|
yellowfive@57
|
216
|
yellowfive@57
|
217 local msgframe = CreateFrame("Frame", nil, UIParent)
|
yellowfive@57
|
218 msgframe:SetHeight(30)
|
yellowfive@57
|
219 msgframe:SetBackdrop(ControlBackdrop)
|
yellowfive@57
|
220 msgframe:SetBackdropColor(0,0,0)
|
yellowfive@57
|
221 msgframe:SetFrameStrata("FULLSCREEN_DIALOG")
|
yellowfive@57
|
222 msgframe:SetFrameLevel(1000)
|
yellowfive@57
|
223 msgframe:SetToplevel(true)
|
yellowfive@57
|
224
|
yellowfive@57
|
225 local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal")
|
yellowfive@57
|
226 msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.")
|
yellowfive@57
|
227 msgframe.msg = msg
|
yellowfive@57
|
228 msg:SetPoint("TOPLEFT", 5, -5)
|
yellowfive@57
|
229 msgframe:SetScript("OnUpdate", keybindingMsgFixWidth)
|
yellowfive@57
|
230 msgframe:SetPoint("BOTTOM", button, "TOP")
|
yellowfive@57
|
231 msgframe:Hide()
|
yellowfive@57
|
232
|
yellowfive@57
|
233 local widget = {
|
yellowfive@57
|
234 button = button,
|
yellowfive@57
|
235 label = label,
|
yellowfive@57
|
236 msgframe = msgframe,
|
yellowfive@57
|
237 frame = frame,
|
yellowfive@57
|
238 alignoffset = 30,
|
yellowfive@57
|
239 type = Type
|
yellowfive@57
|
240 }
|
yellowfive@57
|
241 for method, func in pairs(methods) do
|
yellowfive@57
|
242 widget[method] = func
|
yellowfive@57
|
243 end
|
yellowfive@57
|
244 button.obj = widget
|
yellowfive@57
|
245
|
yellowfive@57
|
246 return AceGUI:RegisterAsWidget(widget)
|
yellowfive@57
|
247 end
|
yellowfive@57
|
248
|
yellowfive@57
|
249 AceGUI:RegisterWidgetType(Type, Constructor, Version)
|