flickerstreak@88: --[[ flickerstreak@88: Name: LibKeyBound-1.0 flickerstreak@88: Revision: $Rev: 76300 $ flickerstreak@88: Author(s): Gello, Maul, Toadkiller, Tuller flickerstreak@88: Website: http://www.wowace.com/wiki/LibKeyBound-1.0 flickerstreak@88: Documentation: http://www.wowace.com/wiki/LibKeyBound-1.0 flickerstreak@88: SVN: http://svn.wowace.com/wowace/trunk/LibKeyBound-1.0 flickerstreak@88: Description: An intuitive keybindings system: mouseover frame, click keys or buttons. flickerstreak@88: Dependencies: CallbackHandler-1.0 flickerstreak@88: --]] flickerstreak@88: flickerstreak@88: local MAJOR = "LibKeyBound-1.0" flickerstreak@88: local MINOR = "$Revision: 76300 $" flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: LibKeyBound-1.0 flickerstreak@88: ClickBinder by Gello and TrinityBinder by Maul -> keyBound by Tuller -> LibKeyBound library by Toadkiller flickerstreak@88: flickerstreak@88: Functions needed to implement flickerstreak@88: button:GetHotkey() - returns the current hotkey assigned to the given button flickerstreak@88: flickerstreak@88: Functions to implement if using a custom keybindings system: flickerstreak@88: button:SetKey(key) - binds the given key to the given button flickerstreak@88: button:FreeKey(key) - unbinds the given key from all other buttons flickerstreak@88: button:ClearBindings() - removes all keys bound to the given button flickerstreak@88: button:GetBindings() - returns a string listing all bindings of the given button flickerstreak@88: button:GetActionName() - what we're binding to, used for printing flickerstreak@88: --]] flickerstreak@88: flickerstreak@88: local LibKeyBound, oldminor = LibStub:NewLibrary(MAJOR, MINOR) flickerstreak@88: flickerstreak@88: if not LibKeyBound then return end -- no upgrade needed flickerstreak@88: flickerstreak@88: local _G = _G flickerstreak@88: flickerstreak@88: -- CallbackHandler flickerstreak@88: LibKeyBound.events = LibKeyBound.events or _G.LibStub("CallbackHandler-1.0"):New(LibKeyBound) flickerstreak@88: flickerstreak@88: local L = LibKeyBoundLocale10 flickerstreak@88: LibKeyBound.L = L flickerstreak@88: -- ToDo delete global LibKeyBoundLocale10 at some point flickerstreak@88: LibKeyBound.Binder = LibKeyBound.Binder or {} flickerstreak@88: flickerstreak@88: -- #NODOC flickerstreak@88: function LibKeyBound:Initialize() flickerstreak@88: do flickerstreak@88: local f = CreateFrame("Frame", "KeyboundDialog", UIParent) flickerstreak@88: f:SetFrameStrata("DIALOG") flickerstreak@88: f:SetToplevel(true); f:EnableMouse(true) flickerstreak@88: f:SetWidth(360); f:SetHeight(140) flickerstreak@88: f:SetBackdrop{ flickerstreak@88: bgFile="Interface\\DialogFrame\\UI-DialogBox-Background" , flickerstreak@88: edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border", flickerstreak@88: tile = true, flickerstreak@88: insets = {left = 11, right = 12, top = 12, bottom = 11}, flickerstreak@88: tileSize = 32, flickerstreak@88: edgeSize = 32, flickerstreak@88: } flickerstreak@88: f:SetPoint("TOP", 0, -24) flickerstreak@88: f:Hide() flickerstreak@88: flickerstreak@88: local tr = f:CreateTitleRegion() flickerstreak@88: tr:SetAllPoints(f) flickerstreak@88: f:SetClampedToScreen(true) flickerstreak@88: flickerstreak@88: local text = f:CreateFontString("ARTWORK") flickerstreak@88: text:SetFontObject("GameFontHighlight") flickerstreak@88: text:SetPoint("TOP", 0, -16) flickerstreak@88: text:SetWidth(252); text:SetHeight(0) flickerstreak@88: text:SetText(format(L.BindingsHelp, GetBindingText("ESCAPE", "KEY_"))) flickerstreak@88: flickerstreak@88: -- Per character bindings checkbox flickerstreak@88: local perChar = CreateFrame("CheckButton", "KeyboundDialogCheck", f, "OptionsCheckButtonTemplate") flickerstreak@88: getglobal(perChar:GetName() .. "Text"):SetText(CHARACTER_SPECIFIC_KEYBINDINGS) flickerstreak@88: flickerstreak@88: perChar:SetScript("OnShow", function(self) flickerstreak@88: self:SetChecked(GetCurrentBindingSet() == 2) flickerstreak@88: end) flickerstreak@88: flickerstreak@88: local current flickerstreak@88: perChar:SetScript("OnClick", function(self) flickerstreak@88: current = (perChar:GetChecked() and 2) or 1 flickerstreak@88: LoadBindings(current) flickerstreak@88: end) flickerstreak@88: flickerstreak@88: -- local added flickerstreak@88: f:SetScript("OnShow", function(self) flickerstreak@88: -- if (not added) then flickerstreak@88: -- UISpecialFrames[#UISpecialFrames + 1] = self:GetName() flickerstreak@88: -- added = true flickerstreak@88: -- end flickerstreak@88: -- PlaySound("igCharacterInfoOpen") flickerstreak@88: end) flickerstreak@88: f:SetScript("OnHide", function(self) flickerstreak@88: -- PlaySound("igCharacterInfoClose") flickerstreak@88: end) flickerstreak@88: flickerstreak@88: -- Okay bindings checkbox flickerstreak@88: local okayBindings = CreateFrame("CheckButton", "KeyboundDialogOkay", f, "OptionsButtonTemplate") flickerstreak@88: getglobal(okayBindings:GetName() .. "Text"):SetText(OKAY) flickerstreak@88: okayBindings:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -12, 14) flickerstreak@88: flickerstreak@88: okayBindings:SetScript("OnClick", function(self) flickerstreak@88: current = (perChar:GetChecked() and 2) or 1 flickerstreak@88: if InCombatLockdown() then flickerstreak@88: self:RegisterEvent("PLAYER_REGEN_ENABLED") flickerstreak@88: else flickerstreak@88: -- DEFAULT_CHAT_FRAME:AddMessage("okayBindings " .. tostring(current)) flickerstreak@88: SaveBindings(current) flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: end flickerstreak@88: end) flickerstreak@88: flickerstreak@88: okayBindings:SetScript("OnHide", function(self) flickerstreak@88: current = (perChar:GetChecked() and 2) or 1 flickerstreak@88: if InCombatLockdown() then flickerstreak@88: self:RegisterEvent("PLAYER_REGEN_ENABLED") flickerstreak@88: else flickerstreak@88: SaveBindings(current) flickerstreak@88: end flickerstreak@88: end) flickerstreak@88: flickerstreak@88: okayBindings:SetScript("OnEvent", function(self, event) flickerstreak@88: -- DEFAULT_CHAT_FRAME:AddMessage("okayBindings delayed " .. tostring(current)) flickerstreak@88: SaveBindings(current) flickerstreak@88: self:UnregisterEvent(event) flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: end) flickerstreak@88: flickerstreak@88: -- Cancel bindings checkbox flickerstreak@88: local cancelBindings = CreateFrame("CheckButton", "KeyboundDialogCancel", f, "OptionsButtonTemplate") flickerstreak@88: getglobal(cancelBindings:GetName() .. "Text"):SetText(CANCEL) flickerstreak@88: cancelBindings:SetPoint("BOTTOMLEFT", f, "BOTTOMLEFT", 12, 14) flickerstreak@88: flickerstreak@88: cancelBindings:SetScript("OnClick", function(self) flickerstreak@88: if InCombatLockdown() then flickerstreak@88: self:RegisterEvent("PLAYER_REGEN_ENABLED") flickerstreak@88: else flickerstreak@88: -- DEFAULT_CHAT_FRAME:AddMessage("cancelBindings ") flickerstreak@88: LoadBindings(GetCurrentBindingSet()) flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: end flickerstreak@88: end) flickerstreak@88: flickerstreak@88: cancelBindings:SetScript("OnEvent", function(self, event) flickerstreak@88: -- DEFAULT_CHAT_FRAME:AddMessage("cancelBindings delayed ") flickerstreak@88: LoadBindings(GetCurrentBindingSet()) flickerstreak@88: self:UnregisterEvent(event) flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: end) flickerstreak@88: flickerstreak@88: perChar:SetPoint("BOTTOMLEFT", cancelBindings, "TOPLEFT", 0, 4) flickerstreak@88: flickerstreak@88: self.dialog = f flickerstreak@88: end flickerstreak@88: flickerstreak@88: SlashCmdList["LibKeyBoundSlashCOMMAND"] = function() self:Toggle() end flickerstreak@88: SLASH_LibKeyBoundSlashCOMMAND1 = "/libkeybound" flickerstreak@88: SLASH_LibKeyBoundSlashCOMMAND2 = "/kb" flickerstreak@88: SLASH_LibKeyBoundSlashCOMMAND3 = "/lkb" flickerstreak@88: flickerstreak@88: LibKeyBound.initialized = true flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: -- Default color to indicate bindable frames in your mod. flickerstreak@88: LibKeyBound.colorKeyBoundMode = LibKeyBound.colorKeyBoundMode or { 0, 1, 1, 0.5 } flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: LibKeyBound:SetColorKeyBoundMode([r][, g][, b][, a]) flickerstreak@88: --]] flickerstreak@88: --[[ flickerstreak@88: Arguments: flickerstreak@88: number - red, default 0 flickerstreak@88: number - green, default 0 flickerstreak@88: number - blue, default 0 flickerstreak@88: number - alpha, default 1 flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: if (MyMod.keyBoundMode) then flickerstreak@88: overlayFrame:SetBackdropColor(LibKeyBound:GetColorKeyBoundMode()) flickerstreak@88: end flickerstreak@88: ... flickerstreak@88: local r, g, b, a = LibKeyBound:GetColorKeyBoundMode() flickerstreak@88: flickerstreak@88: Notes: flickerstreak@88: * Returns the color to use on your participating buttons during KeyBound Mode flickerstreak@88: * Values are unpacked and ready to use as color arguments flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:SetColorKeyBoundMode(r, g, b, a) flickerstreak@88: r, g, b, a = r or 0, g or 0, b or 0, a or 1 flickerstreak@88: LibKeyBound.colorKeyBoundMode[1] = r flickerstreak@88: LibKeyBound.colorKeyBoundMode[2] = g flickerstreak@88: LibKeyBound.colorKeyBoundMode[3] = b flickerstreak@88: LibKeyBound.colorKeyBoundMode[4] = a flickerstreak@88: LibKeyBound.events:Fire("LIBKEYBOUND_MODE_COLOR_CHANGED") flickerstreak@88: end flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Returns: flickerstreak@88: * number - red flickerstreak@88: * number - green flickerstreak@88: * number - blue flickerstreak@88: * number - alpha flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: if (MyMod.keyBoundMode) then flickerstreak@88: overlayFrame:SetBackdropColor(LibKeyBound:GetColorKeyBoundMode()) flickerstreak@88: end flickerstreak@88: ... flickerstreak@88: local r, g, b, a = LibKeyBound:GetColorKeyBoundMode() flickerstreak@88: flickerstreak@88: Notes: flickerstreak@88: * Returns the color to use on your participating buttons during KeyBound Mode flickerstreak@88: * Values are unpacked and ready to use as color arguments flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:GetColorKeyBoundMode() flickerstreak@88: return unpack(LibKeyBound.colorKeyBoundMode) flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: function LibKeyBound:PLAYER_REGEN_ENABLED() flickerstreak@88: if self.enabled then flickerstreak@88: UIErrorsFrame:AddMessage(L.CombatBindingsEnabled, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME) flickerstreak@88: self.dialog:Hide() flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound:PLAYER_REGEN_DISABLED() flickerstreak@88: if self.enabled then flickerstreak@88: self:Set(nil) flickerstreak@88: UIErrorsFrame:AddMessage(L.CombatBindingsDisabled, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME) flickerstreak@88: self.dialog:Show() flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Notes: flickerstreak@88: * Switches KeyBound Mode between on and off flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local LibKeyBound = LibStub("LibKeyBound-1.0") flickerstreak@88: LibKeyBound:Toggle() flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:Toggle() flickerstreak@88: if (LibKeyBound:IsShown()) then flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: else flickerstreak@88: LibKeyBound:Activate() flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Notes: flickerstreak@88: * Switches KeyBound Mode to on flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local LibKeyBound = LibStub("LibKeyBound-1.0") flickerstreak@88: LibKeyBound:Activate() flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:Activate() flickerstreak@88: if not self:IsShown() then flickerstreak@88: if InCombatLockdown() then flickerstreak@88: UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME) flickerstreak@88: else flickerstreak@88: self.enabled = true flickerstreak@88: if not self.frame then flickerstreak@88: self.frame = LibKeyBound.Binder:Create() flickerstreak@88: end flickerstreak@88: self:Set(nil) flickerstreak@88: self.dialog:Show() flickerstreak@88: self.events:Fire("LIBKEYBOUND_ENABLED") flickerstreak@88: end flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Notes: flickerstreak@88: * Switches KeyBound Mode to off flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local LibKeyBound = LibStub("LibKeyBound-1.0") flickerstreak@88: LibKeyBound:Deactivate() flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:Deactivate() flickerstreak@88: if self:IsShown() then flickerstreak@88: self.enabled = nil flickerstreak@88: self:Set(nil) flickerstreak@88: self.dialog:Hide() flickerstreak@88: flickerstreak@88: self.events:Fire("LIBKEYBOUND_DISABLED") flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Returns: flickerstreak@88: boolean - true if KeyBound Mode is currently on flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local LibKeyBound = LibStub("LibKeyBound-1.0") flickerstreak@88: local isKeyBoundMode = LibKeyBound:IsShown() flickerstreak@88: if (isKeyBoundMode) then flickerstreak@88: -- Do something flickerstreak@88: else flickerstreak@88: -- Do another thing flickerstreak@88: end flickerstreak@88: flickerstreak@88: Notes: flickerstreak@88: * Is KeyBound Mode currently on flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:IsShown() flickerstreak@88: return self.enabled flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Arguments: flickerstreak@88: table - the button frame flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local button = this flickerstreak@88: LibKeyBound:Set(button) flickerstreak@88: flickerstreak@88: Notes: flickerstreak@88: * Sets up button for keybinding flickerstreak@88: * Call this in your OnEnter script for the button flickerstreak@88: * Current bindings are shown in the tooltip flickerstreak@88: * Primary binding is shown in green in the button text flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:Set(button) flickerstreak@88: local bindFrame = self.frame flickerstreak@88: flickerstreak@88: if button and self:IsShown() and not InCombatLockdown() then flickerstreak@88: bindFrame.button = button flickerstreak@88: bindFrame:SetAllPoints(button) flickerstreak@88: flickerstreak@88: bindFrame.text:SetFontObject("GameFontNormalLarge") flickerstreak@88: bindFrame.text:SetText(button:GetHotkey()) flickerstreak@88: if bindFrame.text:GetStringWidth() > bindFrame:GetWidth() then flickerstreak@88: bindFrame.text:SetFontObject("GameFontNormal") flickerstreak@88: end flickerstreak@88: bindFrame:Show() flickerstreak@88: bindFrame:OnEnter() flickerstreak@88: elseif bindFrame then flickerstreak@88: bindFrame.button = nil flickerstreak@88: bindFrame:ClearAllPoints() flickerstreak@88: bindFrame:Hide() flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ flickerstreak@88: Arguments: flickerstreak@88: string - the keyString to shorten flickerstreak@88: flickerstreak@88: Returns: flickerstreak@88: string - the shortened displayString flickerstreak@88: flickerstreak@88: Example: flickerstreak@88: local key1 = GetBindingKey(button:GetName()) flickerstreak@88: local displayKey = LibKeyBound:ToShortKey(key1) flickerstreak@88: return displayKey flickerstreak@88: flickerstreak@88: Notes: flickerstreak@88: * Shortens the key text (returned from GetBindingKey etc.) flickerstreak@88: * Result is suitable for display on a button flickerstreak@88: * Can be used for your button:GetHotkey() return value flickerstreak@88: --]] flickerstreak@88: function LibKeyBound:ToShortKey(key) flickerstreak@88: if key then flickerstreak@88: key = key:upper() flickerstreak@88: key = key:gsub(" ", "") flickerstreak@88: key = key:gsub("ALT%-", L["Alt"]) flickerstreak@88: key = key:gsub("CTRL%-", L["Ctrl"]) flickerstreak@88: key = key:gsub("SHIFT%-", L["Shift"]) flickerstreak@88: key = key:gsub("NUMPAD", L["NumPad"]) flickerstreak@88: flickerstreak@88: key = key:gsub("PLUS", "%+") flickerstreak@88: key = key:gsub("MINUS", "%-") flickerstreak@88: key = key:gsub("MULTIPLY", "%*") flickerstreak@88: key = key:gsub("DIVIDE", "%/") flickerstreak@88: flickerstreak@88: key = key:gsub("BACKSPACE", L["Backspace"]) flickerstreak@88: key = key:gsub("BUTTON3", L["Button3"]) flickerstreak@88: key = key:gsub("BUTTON4", L["Button4"]) flickerstreak@88: key = key:gsub("BUTTON5", L["Button5"]) flickerstreak@88: key = key:gsub("CAPSLOCK", L["Capslock"]) flickerstreak@88: key = key:gsub("CLEAR", L["Clear"]) flickerstreak@88: key = key:gsub("DELETE", L["Delete"]) flickerstreak@88: key = key:gsub("END", L["End"]) flickerstreak@88: key = key:gsub("HOME", L["Home"]) flickerstreak@88: key = key:gsub("INSERT", L["Insert"]) flickerstreak@88: key = key:gsub("MOUSEWHEELDOWN", L["Mouse Wheel Down"]) flickerstreak@88: key = key:gsub("MOUSEWHEELUP", L["Mouse Wheel Up"]) flickerstreak@88: key = key:gsub("NUMLOCK", L["Num Lock"]) flickerstreak@88: key = key:gsub("PAGEDOWN", L["Page Down"]) flickerstreak@88: key = key:gsub("PAGEUP", L["Page Up"]) flickerstreak@88: key = key:gsub("SCROLLLOCK", L["Scroll Lock"]) flickerstreak@88: key = key:gsub("SPACEBAR", L["Spacebar"]) flickerstreak@88: key = key:gsub("TAB", L["Tab"]) flickerstreak@88: flickerstreak@88: key = key:gsub("DOWNARROW", L["Down Arrow"]) flickerstreak@88: key = key:gsub("LEFTARROW", L["Left Arrow"]) flickerstreak@88: key = key:gsub("RIGHTARROW", L["Right Arrow"]) flickerstreak@88: key = key:gsub("UPARROW", L["Up Arrow"]) flickerstreak@88: flickerstreak@88: return key flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ Binder Widget ]]-- flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:Create() flickerstreak@88: local binder = CreateFrame("Button") flickerstreak@88: binder:RegisterForClicks("anyUp") flickerstreak@88: binder:SetFrameStrata("DIALOG") flickerstreak@88: binder:EnableKeyboard(true) flickerstreak@88: binder:EnableMouseWheel(true) flickerstreak@88: flickerstreak@88: for k,v in pairs(self) do flickerstreak@88: binder[k] = v flickerstreak@88: end flickerstreak@88: flickerstreak@88: local bg = binder:CreateTexture() flickerstreak@88: bg:SetTexture(0, 0, 0, 0.5) flickerstreak@88: bg:SetAllPoints(binder) flickerstreak@88: flickerstreak@88: local text = binder:CreateFontString("OVERLAY") flickerstreak@88: text:SetFontObject("GameFontNormalLarge") flickerstreak@88: text:SetTextColor(0, 1, 0) flickerstreak@88: text:SetAllPoints(binder) flickerstreak@88: binder.text = text flickerstreak@88: flickerstreak@88: binder:SetScript("OnClick", self.OnKeyDown) flickerstreak@88: binder:SetScript("OnKeyDown", self.OnKeyDown) flickerstreak@88: binder:SetScript("OnMouseWheel", self.OnMouseWheel) flickerstreak@88: binder:SetScript("OnEnter", self.OnEnter) flickerstreak@88: binder:SetScript("OnLeave", self.OnLeave) flickerstreak@88: binder:SetScript("OnHide", self.OnHide) flickerstreak@88: binder:Hide() flickerstreak@88: flickerstreak@88: return binder flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:OnHide() flickerstreak@88: LibKeyBound:Set(nil) flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:OnKeyDown(key) flickerstreak@88: local button = self.button flickerstreak@88: if not button then return end flickerstreak@88: flickerstreak@88: if (key == "UNKNOWN" or key == "LSHIFT" or key == "RSHIFT" or flickerstreak@88: key == "LCTRL" or key == "RCTRL" or key == "LALT" or key == "RALT" or flickerstreak@88: key == "LeftButton" or key == "RightButton") then flickerstreak@88: return flickerstreak@88: end flickerstreak@88: flickerstreak@88: local screenshotKey = GetBindingKey("SCREENSHOT") flickerstreak@88: if screenshotKey and key == screenshotKey then flickerstreak@88: Screenshot() flickerstreak@88: return flickerstreak@88: end flickerstreak@88: flickerstreak@88: local openChatKey = GetBindingKey("OPENCHAT") flickerstreak@88: if openChatKey and key == openChatKey then flickerstreak@88: ChatFrameEditBox:Show() flickerstreak@88: return flickerstreak@88: end flickerstreak@88: flickerstreak@88: if key == "MiddleButton" then flickerstreak@88: key = "BUTTON3" flickerstreak@88: elseif key == "Button4" then flickerstreak@88: key = "BUTTON4" flickerstreak@88: elseif key == "Button5" then flickerstreak@88: key = "BUTTON5" flickerstreak@88: end flickerstreak@88: flickerstreak@88: if key == "ESCAPE" then flickerstreak@88: self:ClearBindings(button) flickerstreak@88: LibKeyBound:Set(button) flickerstreak@88: return flickerstreak@88: end flickerstreak@88: flickerstreak@88: if IsShiftKeyDown() then flickerstreak@88: key = "SHIFT-" .. key flickerstreak@88: end flickerstreak@88: if IsControlKeyDown() then flickerstreak@88: key = "CTRL-" .. key flickerstreak@88: end flickerstreak@88: if IsAltKeyDown() then flickerstreak@88: key = "ALT-" .. key flickerstreak@88: end flickerstreak@88: flickerstreak@88: if MouseIsOver(button) then flickerstreak@88: self:SetKey(button, key) flickerstreak@88: LibKeyBound:Set(button) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:OnMouseWheel(arg1) flickerstreak@88: if arg1 > 0 then flickerstreak@88: self:OnKeyDown("MOUSEWHEELUP") flickerstreak@88: else flickerstreak@88: self:OnKeyDown("MOUSEWHEELDOWN") flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:OnEnter() flickerstreak@88: local button = self.button flickerstreak@88: if button and not InCombatLockdown() then flickerstreak@88: if self:GetRight() >= (GetScreenWidth() / 2) then flickerstreak@88: GameTooltip:SetOwner(self, "ANCHOR_LEFT") flickerstreak@88: else flickerstreak@88: GameTooltip:SetOwner(self, "ANCHOR_RIGHT") flickerstreak@88: end flickerstreak@88: flickerstreak@88: if button.GetActionName then flickerstreak@88: GameTooltip:SetText(button:GetActionName(), 1, 1, 1) flickerstreak@88: else flickerstreak@88: GameTooltip:SetText(button:GetName(), 1, 1, 1) flickerstreak@88: end flickerstreak@88: flickerstreak@88: local bindings = self:GetBindings(button) flickerstreak@88: if bindings then flickerstreak@88: GameTooltip:AddLine(bindings, 0, 1, 0) flickerstreak@88: GameTooltip:AddLine(L.ClearTip) flickerstreak@88: else flickerstreak@88: GameTooltip:AddLine(L.NoKeysBoundTip, 0, 1, 0) flickerstreak@88: end flickerstreak@88: GameTooltip:Show() flickerstreak@88: else flickerstreak@88: GameTooltip:Hide() flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:OnLeave() flickerstreak@88: LibKeyBound:Set(nil) flickerstreak@88: GameTooltip:Hide() flickerstreak@88: end flickerstreak@88: flickerstreak@88: flickerstreak@88: --[[ Update Functions ]]-- flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:ToBinding(button) flickerstreak@88: return format("CLICK %s:LeftButton", button:GetName()) flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:FreeKey(button, key) flickerstreak@88: local msg flickerstreak@88: if button.FreeKey then flickerstreak@88: local action = button:FreeKey(key) flickerstreak@88: if button:FreeKey(key) then flickerstreak@88: msg = format(L.UnboundKey, GetBindingText(key, "KEY_"), action) flickerstreak@88: end flickerstreak@88: else flickerstreak@88: local action = GetBindingAction(key) flickerstreak@88: if action and action ~= "" and action ~= self:ToBinding(button) then flickerstreak@88: msg = format(L.UnboundKey, GetBindingText(key, "KEY_"), action) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: if msg then flickerstreak@88: UIErrorsFrame:AddMessage(msg, 1, 0.82, 0, 1, UIERRORS_HOLD_TIME) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:SetKey(button, key) flickerstreak@88: if InCombatLockdown() then flickerstreak@88: UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME) flickerstreak@88: else flickerstreak@88: self:FreeKey(button, key) flickerstreak@88: flickerstreak@88: if button.SetKey then flickerstreak@88: button:SetKey(key) flickerstreak@88: else flickerstreak@88: SetBindingClick(key, button:GetName(), "LeftButton") flickerstreak@88: end flickerstreak@88: flickerstreak@88: local msg flickerstreak@88: if button.GetActionName then flickerstreak@88: msg = format(L.BoundKey, GetBindingText(key, "KEY_"), button:GetActionName()) flickerstreak@88: else flickerstreak@88: msg = format(L.BoundKey, GetBindingText(key, "KEY_"), button:GetName()) flickerstreak@88: end flickerstreak@88: UIErrorsFrame:AddMessage(msg, 1, 1, 1, 1, UIERRORS_HOLD_TIME) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:ClearBindings(button) flickerstreak@88: if InCombatLockdown() then flickerstreak@88: UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME) flickerstreak@88: else flickerstreak@88: if button.ClearBindings then flickerstreak@88: button:ClearBindings() flickerstreak@88: else flickerstreak@88: local binding = self:ToBinding(button) flickerstreak@88: while (GetBindingKey(binding)) do flickerstreak@88: SetBinding(GetBindingKey(binding), nil) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: local msg flickerstreak@88: if button.GetActionName then flickerstreak@88: msg = format(L.ClearedBindings, button:GetActionName()) flickerstreak@88: else flickerstreak@88: msg = format(L.ClearedBindings, button:GetName()) flickerstreak@88: end flickerstreak@88: UIErrorsFrame:AddMessage(msg, 1, 1, 1, 1, UIERRORS_HOLD_TIME) flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: function LibKeyBound.Binder:GetBindings(button) flickerstreak@88: if button.GetBindings then flickerstreak@88: return button:GetBindings() flickerstreak@88: end flickerstreak@88: flickerstreak@88: local keys flickerstreak@88: local binding = self:ToBinding(button) flickerstreak@88: for i = 1, select("#", GetBindingKey(binding)) do flickerstreak@88: local hotKey = select(i, GetBindingKey(binding)) flickerstreak@88: if keys then flickerstreak@88: keys = keys .. ", " .. GetBindingText(hotKey, "KEY_") flickerstreak@88: else flickerstreak@88: keys = GetBindingText(hotKey, "KEY_") flickerstreak@88: end flickerstreak@88: end flickerstreak@88: flickerstreak@88: return keys flickerstreak@88: end flickerstreak@88: flickerstreak@88: LibKeyBound.EventButton = LibKeyBound.EventButton or CreateFrame("Frame") flickerstreak@88: do flickerstreak@88: local EventButton = LibKeyBound.EventButton flickerstreak@88: EventButton:UnregisterAllEvents() flickerstreak@88: EventButton:SetScript("OnEvent", function(self, event, addon) flickerstreak@88: if (event == "PLAYER_REGEN_DISABLED") then flickerstreak@88: LibKeyBound:PLAYER_REGEN_DISABLED() flickerstreak@88: elseif (event == "PLAYER_REGEN_ENABLED") then flickerstreak@88: LibKeyBound:PLAYER_REGEN_ENABLED() flickerstreak@88: elseif (event == "PLAYER_LOGIN" and not LibKeyBound.initialized) then flickerstreak@88: LibKeyBound:Initialize() flickerstreak@88: EventButton:UnregisterEvent("PLAYER_LOGIN") flickerstreak@88: end flickerstreak@88: end) flickerstreak@88: flickerstreak@88: if IsLoggedIn() and not LibKeyBound.initialized then flickerstreak@88: LibKeyBound:Initialize() flickerstreak@88: elseif not LibKeyBound.initialized then flickerstreak@88: EventButton:RegisterEvent("PLAYER_LOGIN") flickerstreak@88: end flickerstreak@88: EventButton:RegisterEvent("PLAYER_REGEN_ENABLED") flickerstreak@88: EventButton:RegisterEvent("PLAYER_REGEN_DISABLED") flickerstreak@88: end