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