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