1 --[[-----------------------------------------------------------------------------
3 Set Keybindings in the Config UI.
4 -------------------------------------------------------------------------------]]
5 local Type, Version = "Keybinding", 24
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
22 local _, _, _, interface = GetBuildInfo()
23 wowMoP = (interface >= 50000)
26 --[[-----------------------------------------------------------------------------
28 -------------------------------------------------------------------------------]]
30 local function Control_OnEnter(frame)
31 frame.obj:Fire("OnEnter")
34 local function Control_OnLeave(frame)
35 frame.obj:Fire("OnLeave")
38 local function Keybinding_OnClick(frame, button)
39 if button == "LeftButton" or button == "RightButton" then
40 local self = frame.obj
41 if self.waitingForKey then
42 frame:EnableKeyboard(false)
44 frame:UnlockHighlight()
45 self.waitingForKey = nil
47 frame:EnableKeyboard(true)
50 self.waitingForKey = true
57 ["BUTTON1"] = true, ["BUTTON2"] = true,
59 ["LSHIFT"] = true, ["LCTRL"] = true, ["LALT"] = true,
60 ["RSHIFT"] = true, ["RCTRL"] = true, ["RALT"] = true,
62 local function Keybinding_OnKeyDown(frame, key)
63 local self = frame.obj
64 if self.waitingForKey then
65 local keyPressed = key
66 if keyPressed == "ESCAPE" then
69 if ignoreKeys[keyPressed] then return end
70 if IsShiftKeyDown() then
71 keyPressed = "SHIFT-"..keyPressed
73 if IsControlKeyDown() then
74 keyPressed = "CTRL-"..keyPressed
76 if IsAltKeyDown() then
77 keyPressed = "ALT-"..keyPressed
81 frame:EnableKeyboard(false)
83 frame:UnlockHighlight()
84 self.waitingForKey = nil
86 if not self.disabled then
87 self:SetKey(keyPressed)
88 self:Fire("OnKeyChanged", keyPressed)
93 local function Keybinding_OnMouseDown(frame, button)
94 if button == "LeftButton" or button == "RightButton" then
96 elseif button == "MiddleButton" then
98 elseif button == "Button4" then
100 elseif button == "Button5" then
103 Keybinding_OnKeyDown(frame, button)
106 --[[-----------------------------------------------------------------------------
108 -------------------------------------------------------------------------------]]
110 ["OnAcquire"] = function(self)
114 self.waitingForKey = nil
116 self:SetDisabled(false)
117 self.button:EnableKeyboard(false)
120 -- ["OnRelease"] = nil,
122 ["SetDisabled"] = function(self, disabled)
123 self.disabled = disabled
125 self.button:Disable()
126 self.label:SetTextColor(0.5,0.5,0.5)
129 self.label:SetTextColor(1,1,1)
133 ["SetKey"] = function(self, key)
134 if (key or "") == "" then
135 self.button:SetText(NOT_BOUND)
136 self.button:SetNormalFontObject("GameFontNormal")
138 self.button:SetText(key)
139 self.button:SetNormalFontObject("GameFontHighlight")
143 ["GetKey"] = function(self)
144 local key = self.button:GetText()
145 if key == NOT_BOUND then
151 ["SetLabel"] = function(self, label)
152 self.label:SetText(label or "")
153 if (label or "") == "" then
154 self.alignoffset = nil
157 self.alignoffset = 30
163 --[[-----------------------------------------------------------------------------
165 -------------------------------------------------------------------------------]]
167 local ControlBackdrop = {
168 bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
169 edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
170 tile = true, tileSize = 16, edgeSize = 16,
171 insets = { left = 3, right = 3, top = 3, bottom = 3 }
174 local function keybindingMsgFixWidth(frame)
175 frame:SetWidth(frame.msg:GetWidth() + 10)
176 frame:SetScript("OnUpdate", nil)
179 local function Constructor()
180 local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type)
182 local frame = CreateFrame("Frame", nil, UIParent)
183 local button = CreateFrame("Button", name, frame, wowMoP and "UIPanelButtonTemplate" or "UIPanelButtonTemplate2")
185 button:EnableMouse(true)
186 button:RegisterForClicks("AnyDown")
187 button:SetScript("OnEnter", Control_OnEnter)
188 button:SetScript("OnLeave", Control_OnLeave)
189 button:SetScript("OnClick", Keybinding_OnClick)
190 button:SetScript("OnKeyDown", Keybinding_OnKeyDown)
191 button:SetScript("OnMouseDown", Keybinding_OnMouseDown)
192 button:SetPoint("BOTTOMLEFT")
193 button:SetPoint("BOTTOMRIGHT")
195 button:EnableKeyboard(false)
197 local text = button:GetFontString()
198 text:SetPoint("LEFT", 7, 0)
199 text:SetPoint("RIGHT", -7, 0)
201 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
202 label:SetPoint("TOPLEFT")
203 label:SetPoint("TOPRIGHT")
204 label:SetJustifyH("CENTER")
207 local msgframe = CreateFrame("Frame", nil, UIParent)
208 msgframe:SetHeight(30)
209 msgframe:SetBackdrop(ControlBackdrop)
210 msgframe:SetBackdropColor(0,0,0)
211 msgframe:SetFrameStrata("FULLSCREEN_DIALOG")
212 msgframe:SetFrameLevel(1000)
213 msgframe:SetToplevel(true)
215 local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal")
216 msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.")
218 msg:SetPoint("TOPLEFT", 5, -5)
219 msgframe:SetScript("OnUpdate", keybindingMsgFixWidth)
220 msgframe:SetPoint("BOTTOM", button, "TOP")
231 for method, func in pairs(methods) do
232 widget[method] = func
236 return AceGUI:RegisterAsWidget(widget)
239 AceGUI:RegisterWidgetType(Type, Constructor, Version)