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