tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Keybinding Widget tercio@0: Set Keybindings in the Config UI. tercio@0: -------------------------------------------------------------------------------]] Tercio@11: local Type, Version = "Keybinding", 25 tercio@0: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) tercio@0: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end tercio@0: tercio@0: -- Lua APIs tercio@0: local pairs = pairs tercio@0: tercio@0: -- WoW APIs tercio@0: local IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown = IsShiftKeyDown, IsControlKeyDown, IsAltKeyDown tercio@0: local CreateFrame, UIParent = CreateFrame, UIParent tercio@0: tercio@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded tercio@0: -- List them here for Mikk's FindGlobals script tercio@0: -- GLOBALS: NOT_BOUND tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Scripts tercio@0: -------------------------------------------------------------------------------]] tercio@0: tercio@0: local function Control_OnEnter(frame) tercio@0: frame.obj:Fire("OnEnter") tercio@0: end tercio@0: tercio@0: local function Control_OnLeave(frame) tercio@0: frame.obj:Fire("OnLeave") tercio@0: end tercio@0: tercio@0: local function Keybinding_OnClick(frame, button) tercio@0: if button == "LeftButton" or button == "RightButton" then tercio@0: local self = frame.obj tercio@0: if self.waitingForKey then tercio@0: frame:EnableKeyboard(false) Tercio@11: frame:EnableMouseWheel(false) tercio@0: self.msgframe:Hide() tercio@0: frame:UnlockHighlight() tercio@0: self.waitingForKey = nil tercio@0: else tercio@0: frame:EnableKeyboard(true) Tercio@11: frame:EnableMouseWheel(true) tercio@0: self.msgframe:Show() tercio@0: frame:LockHighlight() tercio@0: self.waitingForKey = true tercio@0: end tercio@0: end tercio@0: AceGUI:ClearFocus() tercio@0: end tercio@0: tercio@0: local ignoreKeys = { tercio@0: ["BUTTON1"] = true, ["BUTTON2"] = true, tercio@0: ["UNKNOWN"] = true, tercio@0: ["LSHIFT"] = true, ["LCTRL"] = true, ["LALT"] = true, tercio@0: ["RSHIFT"] = true, ["RCTRL"] = true, ["RALT"] = true, tercio@0: } tercio@0: local function Keybinding_OnKeyDown(frame, key) tercio@0: local self = frame.obj tercio@0: if self.waitingForKey then tercio@0: local keyPressed = key tercio@0: if keyPressed == "ESCAPE" then tercio@0: keyPressed = "" tercio@0: else tercio@0: if ignoreKeys[keyPressed] then return end tercio@0: if IsShiftKeyDown() then tercio@0: keyPressed = "SHIFT-"..keyPressed tercio@0: end tercio@0: if IsControlKeyDown() then tercio@0: keyPressed = "CTRL-"..keyPressed tercio@0: end tercio@0: if IsAltKeyDown() then tercio@0: keyPressed = "ALT-"..keyPressed tercio@0: end tercio@0: end tercio@0: tercio@0: frame:EnableKeyboard(false) Tercio@11: frame:EnableMouseWheel(false) tercio@0: self.msgframe:Hide() tercio@0: frame:UnlockHighlight() tercio@0: self.waitingForKey = nil tercio@0: tercio@0: if not self.disabled then tercio@0: self:SetKey(keyPressed) tercio@0: self:Fire("OnKeyChanged", keyPressed) tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: local function Keybinding_OnMouseDown(frame, button) tercio@0: if button == "LeftButton" or button == "RightButton" then tercio@0: return tercio@0: elseif button == "MiddleButton" then tercio@0: button = "BUTTON3" tercio@0: elseif button == "Button4" then tercio@0: button = "BUTTON4" tercio@0: elseif button == "Button5" then tercio@0: button = "BUTTON5" tercio@0: end tercio@0: Keybinding_OnKeyDown(frame, button) tercio@0: end tercio@0: Tercio@11: local function Keybinding_OnMouseWheel(frame, direction) Tercio@11: local button Tercio@11: if direction >= 0 then Tercio@11: button = "MOUSEWHEELUP" Tercio@11: else Tercio@11: button = "MOUSEWHEELDOWN" Tercio@11: end Tercio@11: Keybinding_OnKeyDown(frame, button) Tercio@11: end Tercio@11: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Methods tercio@0: -------------------------------------------------------------------------------]] tercio@0: local methods = { tercio@0: ["OnAcquire"] = function(self) tercio@0: self:SetWidth(200) tercio@0: self:SetLabel("") tercio@0: self:SetKey("") tercio@0: self.waitingForKey = nil tercio@0: self.msgframe:Hide() tercio@0: self:SetDisabled(false) tercio@0: self.button:EnableKeyboard(false) Tercio@11: self.button:EnableMouseWheel(false) tercio@0: end, tercio@0: tercio@0: -- ["OnRelease"] = nil, tercio@0: tercio@0: ["SetDisabled"] = function(self, disabled) tercio@0: self.disabled = disabled tercio@0: if disabled then tercio@0: self.button:Disable() tercio@0: self.label:SetTextColor(0.5,0.5,0.5) tercio@0: else tercio@0: self.button:Enable() tercio@0: self.label:SetTextColor(1,1,1) tercio@0: end tercio@0: end, tercio@0: tercio@0: ["SetKey"] = function(self, key) tercio@0: if (key or "") == "" then tercio@0: self.button:SetText(NOT_BOUND) tercio@0: self.button:SetNormalFontObject("GameFontNormal") tercio@0: else tercio@0: self.button:SetText(key) tercio@0: self.button:SetNormalFontObject("GameFontHighlight") tercio@0: end tercio@0: end, tercio@0: tercio@0: ["GetKey"] = function(self) tercio@0: local key = self.button:GetText() tercio@0: if key == NOT_BOUND then tercio@0: key = nil tercio@0: end tercio@0: return key tercio@0: end, tercio@0: tercio@0: ["SetLabel"] = function(self, label) tercio@0: self.label:SetText(label or "") tercio@0: if (label or "") == "" then tercio@0: self.alignoffset = nil tercio@0: self:SetHeight(24) tercio@0: else tercio@0: self.alignoffset = 30 tercio@0: self:SetHeight(44) tercio@0: end tercio@0: end, tercio@0: } tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Constructor tercio@0: -------------------------------------------------------------------------------]] tercio@0: tercio@0: local ControlBackdrop = { tercio@0: bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tercio@0: edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tercio@0: tile = true, tileSize = 16, edgeSize = 16, tercio@0: insets = { left = 3, right = 3, top = 3, bottom = 3 } tercio@0: } tercio@0: tercio@0: local function keybindingMsgFixWidth(frame) tercio@0: frame:SetWidth(frame.msg:GetWidth() + 10) tercio@0: frame:SetScript("OnUpdate", nil) tercio@0: end tercio@0: tercio@0: local function Constructor() tercio@0: local name = "AceGUI30KeybindingButton" .. AceGUI:GetNextWidgetNum(Type) tercio@0: tercio@0: local frame = CreateFrame("Frame", nil, UIParent) Tercio@11: local button = CreateFrame("Button", name, frame, "UIPanelButtonTemplate") tercio@0: tercio@0: button:EnableMouse(true) Tercio@11: button:EnableMouseWheel(false) tercio@0: button:RegisterForClicks("AnyDown") tercio@0: button:SetScript("OnEnter", Control_OnEnter) tercio@0: button:SetScript("OnLeave", Control_OnLeave) tercio@0: button:SetScript("OnClick", Keybinding_OnClick) tercio@0: button:SetScript("OnKeyDown", Keybinding_OnKeyDown) tercio@0: button:SetScript("OnMouseDown", Keybinding_OnMouseDown) Tercio@11: button:SetScript("OnMouseWheel", Keybinding_OnMouseWheel) tercio@0: button:SetPoint("BOTTOMLEFT") tercio@0: button:SetPoint("BOTTOMRIGHT") tercio@0: button:SetHeight(24) tercio@0: button:EnableKeyboard(false) tercio@0: tercio@0: local text = button:GetFontString() tercio@0: text:SetPoint("LEFT", 7, 0) tercio@0: text:SetPoint("RIGHT", -7, 0) tercio@0: tercio@0: local label = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") tercio@0: label:SetPoint("TOPLEFT") tercio@0: label:SetPoint("TOPRIGHT") tercio@0: label:SetJustifyH("CENTER") tercio@0: label:SetHeight(18) tercio@0: tercio@0: local msgframe = CreateFrame("Frame", nil, UIParent) tercio@0: msgframe:SetHeight(30) tercio@0: msgframe:SetBackdrop(ControlBackdrop) tercio@0: msgframe:SetBackdropColor(0,0,0) tercio@0: msgframe:SetFrameStrata("FULLSCREEN_DIALOG") tercio@0: msgframe:SetFrameLevel(1000) tercio@0: msgframe:SetToplevel(true) tercio@0: tercio@0: local msg = msgframe:CreateFontString(nil, "OVERLAY", "GameFontNormal") tercio@0: msg:SetText("Press a key to bind, ESC to clear the binding or click the button again to cancel.") tercio@0: msgframe.msg = msg tercio@0: msg:SetPoint("TOPLEFT", 5, -5) tercio@0: msgframe:SetScript("OnUpdate", keybindingMsgFixWidth) tercio@0: msgframe:SetPoint("BOTTOM", button, "TOP") tercio@0: msgframe:Hide() tercio@0: tercio@0: local widget = { tercio@0: button = button, tercio@0: label = label, tercio@0: msgframe = msgframe, tercio@0: frame = frame, tercio@0: alignoffset = 30, tercio@0: type = Type tercio@0: } tercio@0: for method, func in pairs(methods) do tercio@0: widget[method] = func tercio@0: end tercio@0: button.obj = widget tercio@0: tercio@0: return AceGUI:RegisterAsWidget(widget) tercio@0: end tercio@0: tercio@0: AceGUI:RegisterWidgetType(Type, Constructor, Version)