1 --[[-----------------------------------------------------------------------------
3 Set Keybindings in the Config UI.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Keybinding", 25
6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
13 local IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown = IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown
14 local CreateFrame, UIParent = CreateFrame, UIParent
16 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
17 -- List them here for Mikk's FindGlobals script
20 --[[-----------------------------------------------------------------------------
22 -------------------------------------------------------------------------------]]
24 local function Control_OnEnter(frame)
25 frame.obj:Fire("OnEnter")
28 local function Control_OnLeave(frame)
29 frame.obj:Fire("OnLeave")
32 local function Keybinding_OnClick(frame, button)
33 if button == "LeftButton" or button == "RightButton" then
34 local self = frame.obj
35 if self.waitingForKey then
36 frame:EnableKeyboard(false)
37 frame:EnableMouseWheel(false)
39 frame:UnlockHighlight()
40 self.waitingForKey = nil
42 frame:EnableKeyboard(true)
43 frame:EnableMouseWheel(true)
46 self.waitingForKey = true
53 ["BUTTON1"] = true, ["BUTTON2"] = true,
55 ["LSHIFT"] = true, ["LCTRL"] = true, ["LALT"] = true,
56 ["RSHIFT"] = true, ["RCTRL"] = true, ["RALT"] = true,
58 local function Keybinding_OnKeyDown(frame, key)
59 local self = frame.obj
60 if self.waitingForKey then
61 local keyPressed = key
62 if keyPressed == "ESCAPE" then
65 if ignoreKeys[keyPressed] then return end
66 if IsShiftKeyDown() then
67 keyPressed = "SHIFT-"..keyPressed
69 if IsControlKeyDown() then
70 keyPressed = "CTRL-"..keyPressed
72 if IsAltKeyDown() then
73 keyPressed = "ALT-"..keyPressed
77 frame:EnableKeyboard(false)
78 frame:EnableMouseWheel(false)
80 frame:UnlockHighlight()
81 self.waitingForKey = nil
83 if not self.disabled then
84 self:SetKey(keyPressed)
85 self:Fire("OnKeyChanged", keyPressed)
90 local function Keybinding_OnMouseDown(frame, button)
91 if button == "LeftButton" or button == "RightButton" then
93 elseif button == "MiddleButton" then
95 elseif button == "Button4" then
97 elseif button == "Button5" then
100 Keybinding_OnKeyDown(frame, button)
103 local function Keybinding_OnMouseWheel(frame, direction)
105 if direction >= 0 then
106 button = "MOUSEWHEELUP"
108 button = "MOUSEWHEELDOWN"
110 Keybinding_OnKeyDown(frame, button)
113 --[[-----------------------------------------------------------------------------
115 -------------------------------------------------------------------------------]]
117 ["OnAcquire"] = function(self)
121 self.waitingForKey = nil
123 self:SetDisabled(false)
124 self.button:EnableKeyboard(false)
125 self.button:EnableMouseWheel(false)
128 -- ["OnRelease"] = nil,
130 ["SetDisabled"] = function(self, disabled)
131 self.disabled = disabled
133 self.button:Disable()
134 self.label:SetTextColor(0.5,0.5,0.5)
137 self.label:SetTextColor(1,1,1)
141 ["SetKey"] = function(self, key)
142 if (key or "") == "" then
143 self.button:SetText(NOT_BOUND)
144 self.button:SetNormalFontObject("GameFontNormal")
146 self.button:SetText(key)
147 self.button:SetNormalFontObject("GameFontHighlight")
151 ["GetKey"] = function(self)
152 local key = self.button:GetText()
153 if key == NOT_BOUND then
159 ["SetLabel"] = function(self, label)
160 self.label:SetText(label or "")
161 if (label or "") == "" then
162 self.alignoffset = nil
165 self.alignoffset = 30
171 --[[-----------------------------------------------------------------------------
173 -------------------------------------------------------------------------------]]
175 local ControlBackdrop = {
176 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
177 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
178 tile = true, tileSize = 16, edgeSize = 16,
179 insets = { left = 3, right = 3, top = 3, bottom = 3 }
182 local function keybindingMsgFixWidth(frame)
183 frame:SetWidth(frame.msg:GetWidth() + 10)
184 frame:SetScript("OnUpdate", nil)
187 local function Constructor()
188 local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type)
190 local frame = CreateFrame("Frame", nil, UIParent)
191 local button = CreateFrame("Button", name, frame, "UIPanelButtonTemplate")
193 button:EnableMouse(true)
194 button:EnableMouseWheel(false)
195 button:RegisterForClicks("AnyDown")
196 button:SetScript("OnEnter", Control_OnEnter)
197 button:SetScript("OnLeave", Control_OnLeave)
198 button:SetScript("OnClick", Keybinding_OnClick)
199 button:SetScript("OnKeyDown", Keybinding_OnKeyDown)
200 button:SetScript("OnMouseDown", Keybinding_OnMouseDown)
201 button:SetScript("OnMouseWheel", Keybinding_OnMouseWheel)
202 button:SetPoint("BOTTOMLEFT")
203 button:SetPoint("BOTTOMRIGHT")
205 button:EnableKeyboard(false)
207 local text = button:GetFontString()
208 text:SetPoint("LEFT", 7, 0)
209 text:SetPoint("RIGHT", -7, 0)
211 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
212 label:SetPoint("TOPLEFT")
213 label:SetPoint("TOPRIGHT")
214 label:SetJustifyH("CENTER")
217 local msgframe = CreateFrame("Frame", nil, UIParent)
218 msgframe:SetHeight(30)
219 msgframe:SetBackdrop(ControlBackdrop)
220 msgframe:SetBackdropColor(0,0,0)
221 msgframe:SetFrameStrata("FULLSCREEN_DIALOG")
222 msgframe:SetFrameLevel(1000)
223 msgframe:SetToplevel(true)
225 local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal")
226 msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.")
228 msg:SetPoint("TOPLEFT", 5, -5)
229 msgframe:SetScript("OnUpdate", keybindingMsgFixWidth)
230 msgframe:SetPoint("BOTTOM", button, "TOP")
241 for method, func in pairs(methods) do
242 widget[method] = func
246 return AceGUI:RegisterAsWidget(widget)
249 AceGUI:RegisterWidgetType(Type, Constructor, Version)