annotate lib/LibKeyBound-1.0/LibKeyBound-1.0.lua @ 88:fc83b3f5b322

Added keybindings using LibKeyBound-1.0, with modifications for Override bindings instead of standard bindings.
author Flick <flickerstreak@gmail.com>
date Sun, 31 Aug 2008 06:02:18 +0000
parents
children
rev   line source
flickerstreak@88 1 --[[
flickerstreak@88 2 Name: LibKeyBound-1.0
flickerstreak@88 3 Revision: $Rev: 76300 $
flickerstreak@88 4 Author(s): Gello, Maul, Toadkiller, Tuller
flickerstreak@88 5 Website: http://www.wowace.com/wiki/LibKeyBound-1.0
flickerstreak@88 6 Documentation: http://www.wowace.com/wiki/LibKeyBound-1.0
flickerstreak@88 7 SVN: http://svn.wowace.com/wowace/trunk/LibKeyBound-1.0
flickerstreak@88 8 Description: An intuitive keybindings system: mouseover frame, click keys or buttons.
flickerstreak@88 9 Dependencies: CallbackHandler-1.0
flickerstreak@88 10 --]]
flickerstreak@88 11
flickerstreak@88 12 local MAJOR = "LibKeyBound-1.0"
flickerstreak@88 13 local MINOR = "$Revision: 76300 $"
flickerstreak@88 14
flickerstreak@88 15 --[[
flickerstreak@88 16 LibKeyBound-1.0
flickerstreak@88 17 ClickBinder by Gello and TrinityBinder by Maul -> keyBound by Tuller -> LibKeyBound library by Toadkiller
flickerstreak@88 18
flickerstreak@88 19 Functions needed to implement
flickerstreak@88 20 button:GetHotkey() - returns the current hotkey assigned to the given button
flickerstreak@88 21
flickerstreak@88 22 Functions to implement if using a custom keybindings system:
flickerstreak@88 23 button:SetKey(key) - binds the given key to the given button
flickerstreak@88 24 button:FreeKey(key) - unbinds the given key from all other buttons
flickerstreak@88 25 button:ClearBindings() - removes all keys bound to the given button
flickerstreak@88 26 button:GetBindings() - returns a string listing all bindings of the given button
flickerstreak@88 27 button:GetActionName() - what we're binding to, used for printing
flickerstreak@88 28 --]]
flickerstreak@88 29
flickerstreak@88 30 local LibKeyBound, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
flickerstreak@88 31
flickerstreak@88 32 if not LibKeyBound then return end -- no upgrade needed
flickerstreak@88 33
flickerstreak@88 34 local _G = _G
flickerstreak@88 35
flickerstreak@88 36 -- CallbackHandler
flickerstreak@88 37 LibKeyBound.events = LibKeyBound.events or _G.LibStub("CallbackHandler-1.0"):New(LibKeyBound)
flickerstreak@88 38
flickerstreak@88 39 local L = LibKeyBoundLocale10
flickerstreak@88 40 LibKeyBound.L = L
flickerstreak@88 41 -- ToDo delete global LibKeyBoundLocale10 at some point
flickerstreak@88 42 LibKeyBound.Binder = LibKeyBound.Binder or {}
flickerstreak@88 43
flickerstreak@88 44 -- #NODOC
flickerstreak@88 45 function LibKeyBound:Initialize()
flickerstreak@88 46 do
flickerstreak@88 47 local f = CreateFrame("Frame", "KeyboundDialog", UIParent)
flickerstreak@88 48 f:SetFrameStrata("DIALOG")
flickerstreak@88 49 f:SetToplevel(true); f:EnableMouse(true)
flickerstreak@88 50 f:SetWidth(360); f:SetHeight(140)
flickerstreak@88 51 f:SetBackdrop{
flickerstreak@88 52 bgFile="Interface\\DialogFrame\\UI-DialogBox-Background" ,
flickerstreak@88 53 edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border",
flickerstreak@88 54 tile = true,
flickerstreak@88 55 insets = {left = 11, right = 12, top = 12, bottom = 11},
flickerstreak@88 56 tileSize = 32,
flickerstreak@88 57 edgeSize = 32,
flickerstreak@88 58 }
flickerstreak@88 59 f:SetPoint("TOP", 0, -24)
flickerstreak@88 60 f:Hide()
flickerstreak@88 61
flickerstreak@88 62 local tr = f:CreateTitleRegion()
flickerstreak@88 63 tr:SetAllPoints(f)
flickerstreak@88 64 f:SetClampedToScreen(true)
flickerstreak@88 65
flickerstreak@88 66 local text = f:CreateFontString("ARTWORK")
flickerstreak@88 67 text:SetFontObject("GameFontHighlight")
flickerstreak@88 68 text:SetPoint("TOP", 0, -16)
flickerstreak@88 69 text:SetWidth(252); text:SetHeight(0)
flickerstreak@88 70 text:SetText(format(L.BindingsHelp, GetBindingText("ESCAPE", "KEY_")))
flickerstreak@88 71
flickerstreak@88 72 -- Per character bindings checkbox
flickerstreak@88 73 local perChar = CreateFrame("CheckButton", "KeyboundDialogCheck", f, "OptionsCheckButtonTemplate")
flickerstreak@88 74 getglobal(perChar:GetName() .. "Text"):SetText(CHARACTER_SPECIFIC_KEYBINDINGS)
flickerstreak@88 75
flickerstreak@88 76 perChar:SetScript("OnShow", function(self)
flickerstreak@88 77 self:SetChecked(GetCurrentBindingSet() == 2)
flickerstreak@88 78 end)
flickerstreak@88 79
flickerstreak@88 80 local current
flickerstreak@88 81 perChar:SetScript("OnClick", function(self)
flickerstreak@88 82 current = (perChar:GetChecked() and 2) or 1
flickerstreak@88 83 LoadBindings(current)
flickerstreak@88 84 end)
flickerstreak@88 85
flickerstreak@88 86 -- local added
flickerstreak@88 87 f:SetScript("OnShow", function(self)
flickerstreak@88 88 -- if (not added) then
flickerstreak@88 89 -- UISpecialFrames[#UISpecialFrames + 1] = self:GetName()
flickerstreak@88 90 -- added = true
flickerstreak@88 91 -- end
flickerstreak@88 92 -- PlaySound("igCharacterInfoOpen")
flickerstreak@88 93 end)
flickerstreak@88 94 f:SetScript("OnHide", function(self)
flickerstreak@88 95 -- PlaySound("igCharacterInfoClose")
flickerstreak@88 96 end)
flickerstreak@88 97
flickerstreak@88 98 -- Okay bindings checkbox
flickerstreak@88 99 local okayBindings = CreateFrame("CheckButton", "KeyboundDialogOkay", f, "OptionsButtonTemplate")
flickerstreak@88 100 getglobal(okayBindings:GetName() .. "Text"):SetText(OKAY)
flickerstreak@88 101 okayBindings:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", -12, 14)
flickerstreak@88 102
flickerstreak@88 103 okayBindings:SetScript("OnClick", function(self)
flickerstreak@88 104 current = (perChar:GetChecked() and 2) or 1
flickerstreak@88 105 if InCombatLockdown() then
flickerstreak@88 106 self:RegisterEvent("PLAYER_REGEN_ENABLED")
flickerstreak@88 107 else
flickerstreak@88 108 -- DEFAULT_CHAT_FRAME:AddMessage("okayBindings " .. tostring(current))
flickerstreak@88 109 SaveBindings(current)
flickerstreak@88 110 LibKeyBound:Deactivate()
flickerstreak@88 111 end
flickerstreak@88 112 end)
flickerstreak@88 113
flickerstreak@88 114 okayBindings:SetScript("OnHide", function(self)
flickerstreak@88 115 current = (perChar:GetChecked() and 2) or 1
flickerstreak@88 116 if InCombatLockdown() then
flickerstreak@88 117 self:RegisterEvent("PLAYER_REGEN_ENABLED")
flickerstreak@88 118 else
flickerstreak@88 119 SaveBindings(current)
flickerstreak@88 120 end
flickerstreak@88 121 end)
flickerstreak@88 122
flickerstreak@88 123 okayBindings:SetScript("OnEvent", function(self, event)
flickerstreak@88 124 -- DEFAULT_CHAT_FRAME:AddMessage("okayBindings delayed " .. tostring(current))
flickerstreak@88 125 SaveBindings(current)
flickerstreak@88 126 self:UnregisterEvent(event)
flickerstreak@88 127 LibKeyBound:Deactivate()
flickerstreak@88 128 end)
flickerstreak@88 129
flickerstreak@88 130 -- Cancel bindings checkbox
flickerstreak@88 131 local cancelBindings = CreateFrame("CheckButton", "KeyboundDialogCancel", f, "OptionsButtonTemplate")
flickerstreak@88 132 getglobal(cancelBindings:GetName() .. "Text"):SetText(CANCEL)
flickerstreak@88 133 cancelBindings:SetPoint("BOTTOMLEFT", f, "BOTTOMLEFT", 12, 14)
flickerstreak@88 134
flickerstreak@88 135 cancelBindings:SetScript("OnClick", function(self)
flickerstreak@88 136 if InCombatLockdown() then
flickerstreak@88 137 self:RegisterEvent("PLAYER_REGEN_ENABLED")
flickerstreak@88 138 else
flickerstreak@88 139 -- DEFAULT_CHAT_FRAME:AddMessage("cancelBindings ")
flickerstreak@88 140 LoadBindings(GetCurrentBindingSet())
flickerstreak@88 141 LibKeyBound:Deactivate()
flickerstreak@88 142 end
flickerstreak@88 143 end)
flickerstreak@88 144
flickerstreak@88 145 cancelBindings:SetScript("OnEvent", function(self, event)
flickerstreak@88 146 -- DEFAULT_CHAT_FRAME:AddMessage("cancelBindings delayed ")
flickerstreak@88 147 LoadBindings(GetCurrentBindingSet())
flickerstreak@88 148 self:UnregisterEvent(event)
flickerstreak@88 149 LibKeyBound:Deactivate()
flickerstreak@88 150 end)
flickerstreak@88 151
flickerstreak@88 152 perChar:SetPoint("BOTTOMLEFT", cancelBindings, "TOPLEFT", 0, 4)
flickerstreak@88 153
flickerstreak@88 154 self.dialog = f
flickerstreak@88 155 end
flickerstreak@88 156
flickerstreak@88 157 SlashCmdList["LibKeyBoundSlashCOMMAND"] = function() self:Toggle() end
flickerstreak@88 158 SLASH_LibKeyBoundSlashCOMMAND1 = "/libkeybound"
flickerstreak@88 159 SLASH_LibKeyBoundSlashCOMMAND2 = "/kb"
flickerstreak@88 160 SLASH_LibKeyBoundSlashCOMMAND3 = "/lkb"
flickerstreak@88 161
flickerstreak@88 162 LibKeyBound.initialized = true
flickerstreak@88 163 end
flickerstreak@88 164
flickerstreak@88 165
flickerstreak@88 166 -- Default color to indicate bindable frames in your mod.
flickerstreak@88 167 LibKeyBound.colorKeyBoundMode = LibKeyBound.colorKeyBoundMode or { 0, 1, 1, 0.5 }
flickerstreak@88 168
flickerstreak@88 169 --[[
flickerstreak@88 170 LibKeyBound:SetColorKeyBoundMode([r][, g][, b][, a])
flickerstreak@88 171 --]]
flickerstreak@88 172 --[[
flickerstreak@88 173 Arguments:
flickerstreak@88 174 number - red, default 0
flickerstreak@88 175 number - green, default 0
flickerstreak@88 176 number - blue, default 0
flickerstreak@88 177 number - alpha, default 1
flickerstreak@88 178
flickerstreak@88 179 Example:
flickerstreak@88 180 if (MyMod.keyBoundMode) then
flickerstreak@88 181 overlayFrame:SetBackdropColor(LibKeyBound:GetColorKeyBoundMode())
flickerstreak@88 182 end
flickerstreak@88 183 ...
flickerstreak@88 184 local r, g, b, a = LibKeyBound:GetColorKeyBoundMode()
flickerstreak@88 185
flickerstreak@88 186 Notes:
flickerstreak@88 187 * Returns the color to use on your participating buttons during KeyBound Mode
flickerstreak@88 188 * Values are unpacked and ready to use as color arguments
flickerstreak@88 189 --]]
flickerstreak@88 190 function LibKeyBound:SetColorKeyBoundMode(r, g, b, a)
flickerstreak@88 191 r, g, b, a = r or 0, g or 0, b or 0, a or 1
flickerstreak@88 192 LibKeyBound.colorKeyBoundMode[1] = r
flickerstreak@88 193 LibKeyBound.colorKeyBoundMode[2] = g
flickerstreak@88 194 LibKeyBound.colorKeyBoundMode[3] = b
flickerstreak@88 195 LibKeyBound.colorKeyBoundMode[4] = a
flickerstreak@88 196 LibKeyBound.events:Fire("LIBKEYBOUND_MODE_COLOR_CHANGED")
flickerstreak@88 197 end
flickerstreak@88 198
flickerstreak@88 199 --[[
flickerstreak@88 200 Returns:
flickerstreak@88 201 * number - red
flickerstreak@88 202 * number - green
flickerstreak@88 203 * number - blue
flickerstreak@88 204 * number - alpha
flickerstreak@88 205
flickerstreak@88 206 Example:
flickerstreak@88 207 if (MyMod.keyBoundMode) then
flickerstreak@88 208 overlayFrame:SetBackdropColor(LibKeyBound:GetColorKeyBoundMode())
flickerstreak@88 209 end
flickerstreak@88 210 ...
flickerstreak@88 211 local r, g, b, a = LibKeyBound:GetColorKeyBoundMode()
flickerstreak@88 212
flickerstreak@88 213 Notes:
flickerstreak@88 214 * Returns the color to use on your participating buttons during KeyBound Mode
flickerstreak@88 215 * Values are unpacked and ready to use as color arguments
flickerstreak@88 216 --]]
flickerstreak@88 217 function LibKeyBound:GetColorKeyBoundMode()
flickerstreak@88 218 return unpack(LibKeyBound.colorKeyBoundMode)
flickerstreak@88 219 end
flickerstreak@88 220
flickerstreak@88 221
flickerstreak@88 222 function LibKeyBound:PLAYER_REGEN_ENABLED()
flickerstreak@88 223 if self.enabled then
flickerstreak@88 224 UIErrorsFrame:AddMessage(L.CombatBindingsEnabled, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 225 self.dialog:Hide()
flickerstreak@88 226 end
flickerstreak@88 227 end
flickerstreak@88 228
flickerstreak@88 229 function LibKeyBound:PLAYER_REGEN_DISABLED()
flickerstreak@88 230 if self.enabled then
flickerstreak@88 231 self:Set(nil)
flickerstreak@88 232 UIErrorsFrame:AddMessage(L.CombatBindingsDisabled, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 233 self.dialog:Show()
flickerstreak@88 234 end
flickerstreak@88 235 end
flickerstreak@88 236
flickerstreak@88 237
flickerstreak@88 238 --[[
flickerstreak@88 239 Notes:
flickerstreak@88 240 * Switches KeyBound Mode between on and off
flickerstreak@88 241
flickerstreak@88 242 Example:
flickerstreak@88 243 local LibKeyBound = LibStub("LibKeyBound-1.0")
flickerstreak@88 244 LibKeyBound:Toggle()
flickerstreak@88 245 --]]
flickerstreak@88 246 function LibKeyBound:Toggle()
flickerstreak@88 247 if (LibKeyBound:IsShown()) then
flickerstreak@88 248 LibKeyBound:Deactivate()
flickerstreak@88 249 else
flickerstreak@88 250 LibKeyBound:Activate()
flickerstreak@88 251 end
flickerstreak@88 252 end
flickerstreak@88 253
flickerstreak@88 254
flickerstreak@88 255 --[[
flickerstreak@88 256 Notes:
flickerstreak@88 257 * Switches KeyBound Mode to on
flickerstreak@88 258
flickerstreak@88 259 Example:
flickerstreak@88 260 local LibKeyBound = LibStub("LibKeyBound-1.0")
flickerstreak@88 261 LibKeyBound:Activate()
flickerstreak@88 262 --]]
flickerstreak@88 263 function LibKeyBound:Activate()
flickerstreak@88 264 if not self:IsShown() then
flickerstreak@88 265 if InCombatLockdown() then
flickerstreak@88 266 UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 267 else
flickerstreak@88 268 self.enabled = true
flickerstreak@88 269 if not self.frame then
flickerstreak@88 270 self.frame = LibKeyBound.Binder:Create()
flickerstreak@88 271 end
flickerstreak@88 272 self:Set(nil)
flickerstreak@88 273 self.dialog:Show()
flickerstreak@88 274 self.events:Fire("LIBKEYBOUND_ENABLED")
flickerstreak@88 275 end
flickerstreak@88 276 end
flickerstreak@88 277 end
flickerstreak@88 278
flickerstreak@88 279
flickerstreak@88 280 --[[
flickerstreak@88 281 Notes:
flickerstreak@88 282 * Switches KeyBound Mode to off
flickerstreak@88 283
flickerstreak@88 284 Example:
flickerstreak@88 285 local LibKeyBound = LibStub("LibKeyBound-1.0")
flickerstreak@88 286 LibKeyBound:Deactivate()
flickerstreak@88 287 --]]
flickerstreak@88 288 function LibKeyBound:Deactivate()
flickerstreak@88 289 if self:IsShown() then
flickerstreak@88 290 self.enabled = nil
flickerstreak@88 291 self:Set(nil)
flickerstreak@88 292 self.dialog:Hide()
flickerstreak@88 293
flickerstreak@88 294 self.events:Fire("LIBKEYBOUND_DISABLED")
flickerstreak@88 295 end
flickerstreak@88 296 end
flickerstreak@88 297
flickerstreak@88 298
flickerstreak@88 299 --[[
flickerstreak@88 300 Returns:
flickerstreak@88 301 boolean - true if KeyBound Mode is currently on
flickerstreak@88 302
flickerstreak@88 303 Example:
flickerstreak@88 304 local LibKeyBound = LibStub("LibKeyBound-1.0")
flickerstreak@88 305 local isKeyBoundMode = LibKeyBound:IsShown()
flickerstreak@88 306 if (isKeyBoundMode) then
flickerstreak@88 307 -- Do something
flickerstreak@88 308 else
flickerstreak@88 309 -- Do another thing
flickerstreak@88 310 end
flickerstreak@88 311
flickerstreak@88 312 Notes:
flickerstreak@88 313 * Is KeyBound Mode currently on
flickerstreak@88 314 --]]
flickerstreak@88 315 function LibKeyBound:IsShown()
flickerstreak@88 316 return self.enabled
flickerstreak@88 317 end
flickerstreak@88 318
flickerstreak@88 319
flickerstreak@88 320 --[[
flickerstreak@88 321 Arguments:
flickerstreak@88 322 table - the button frame
flickerstreak@88 323
flickerstreak@88 324 Example:
flickerstreak@88 325 local button = this
flickerstreak@88 326 LibKeyBound:Set(button)
flickerstreak@88 327
flickerstreak@88 328 Notes:
flickerstreak@88 329 * Sets up button for keybinding
flickerstreak@88 330 * Call this in your OnEnter script for the button
flickerstreak@88 331 * Current bindings are shown in the tooltip
flickerstreak@88 332 * Primary binding is shown in green in the button text
flickerstreak@88 333 --]]
flickerstreak@88 334 function LibKeyBound:Set(button)
flickerstreak@88 335 local bindFrame = self.frame
flickerstreak@88 336
flickerstreak@88 337 if button and self:IsShown() and not InCombatLockdown() then
flickerstreak@88 338 bindFrame.button = button
flickerstreak@88 339 bindFrame:SetAllPoints(button)
flickerstreak@88 340
flickerstreak@88 341 bindFrame.text:SetFontObject("GameFontNormalLarge")
flickerstreak@88 342 bindFrame.text:SetText(button:GetHotkey())
flickerstreak@88 343 if bindFrame.text:GetStringWidth() > bindFrame:GetWidth() then
flickerstreak@88 344 bindFrame.text:SetFontObject("GameFontNormal")
flickerstreak@88 345 end
flickerstreak@88 346 bindFrame:Show()
flickerstreak@88 347 bindFrame:OnEnter()
flickerstreak@88 348 elseif bindFrame then
flickerstreak@88 349 bindFrame.button = nil
flickerstreak@88 350 bindFrame:ClearAllPoints()
flickerstreak@88 351 bindFrame:Hide()
flickerstreak@88 352 end
flickerstreak@88 353 end
flickerstreak@88 354
flickerstreak@88 355
flickerstreak@88 356 --[[
flickerstreak@88 357 Arguments:
flickerstreak@88 358 string - the keyString to shorten
flickerstreak@88 359
flickerstreak@88 360 Returns:
flickerstreak@88 361 string - the shortened displayString
flickerstreak@88 362
flickerstreak@88 363 Example:
flickerstreak@88 364 local key1 = GetBindingKey(button:GetName())
flickerstreak@88 365 local displayKey = LibKeyBound:ToShortKey(key1)
flickerstreak@88 366 return displayKey
flickerstreak@88 367
flickerstreak@88 368 Notes:
flickerstreak@88 369 * Shortens the key text (returned from GetBindingKey etc.)
flickerstreak@88 370 * Result is suitable for display on a button
flickerstreak@88 371 * Can be used for your button:GetHotkey() return value
flickerstreak@88 372 --]]
flickerstreak@88 373 function LibKeyBound:ToShortKey(key)
flickerstreak@88 374 if key then
flickerstreak@88 375 key = key:upper()
flickerstreak@88 376 key = key:gsub(" ", "")
flickerstreak@88 377 key = key:gsub("ALT%-", L["Alt"])
flickerstreak@88 378 key = key:gsub("CTRL%-", L["Ctrl"])
flickerstreak@88 379 key = key:gsub("SHIFT%-", L["Shift"])
flickerstreak@88 380 key = key:gsub("NUMPAD", L["NumPad"])
flickerstreak@88 381
flickerstreak@88 382 key = key:gsub("PLUS", "%+")
flickerstreak@88 383 key = key:gsub("MINUS", "%-")
flickerstreak@88 384 key = key:gsub("MULTIPLY", "%*")
flickerstreak@88 385 key = key:gsub("DIVIDE", "%/")
flickerstreak@88 386
flickerstreak@88 387 key = key:gsub("BACKSPACE", L["Backspace"])
flickerstreak@88 388 key = key:gsub("BUTTON3", L["Button3"])
flickerstreak@88 389 key = key:gsub("BUTTON4", L["Button4"])
flickerstreak@88 390 key = key:gsub("BUTTON5", L["Button5"])
flickerstreak@88 391 key = key:gsub("CAPSLOCK", L["Capslock"])
flickerstreak@88 392 key = key:gsub("CLEAR", L["Clear"])
flickerstreak@88 393 key = key:gsub("DELETE", L["Delete"])
flickerstreak@88 394 key = key:gsub("END", L["End"])
flickerstreak@88 395 key = key:gsub("HOME", L["Home"])
flickerstreak@88 396 key = key:gsub("INSERT", L["Insert"])
flickerstreak@88 397 key = key:gsub("MOUSEWHEELDOWN", L["Mouse Wheel Down"])
flickerstreak@88 398 key = key:gsub("MOUSEWHEELUP", L["Mouse Wheel Up"])
flickerstreak@88 399 key = key:gsub("NUMLOCK", L["Num Lock"])
flickerstreak@88 400 key = key:gsub("PAGEDOWN", L["Page Down"])
flickerstreak@88 401 key = key:gsub("PAGEUP", L["Page Up"])
flickerstreak@88 402 key = key:gsub("SCROLLLOCK", L["Scroll Lock"])
flickerstreak@88 403 key = key:gsub("SPACEBAR", L["Spacebar"])
flickerstreak@88 404 key = key:gsub("TAB", L["Tab"])
flickerstreak@88 405
flickerstreak@88 406 key = key:gsub("DOWNARROW", L["Down Arrow"])
flickerstreak@88 407 key = key:gsub("LEFTARROW", L["Left Arrow"])
flickerstreak@88 408 key = key:gsub("RIGHTARROW", L["Right Arrow"])
flickerstreak@88 409 key = key:gsub("UPARROW", L["Up Arrow"])
flickerstreak@88 410
flickerstreak@88 411 return key
flickerstreak@88 412 end
flickerstreak@88 413 end
flickerstreak@88 414
flickerstreak@88 415
flickerstreak@88 416 --[[ Binder Widget ]]--
flickerstreak@88 417
flickerstreak@88 418 function LibKeyBound.Binder:Create()
flickerstreak@88 419 local binder = CreateFrame("Button")
flickerstreak@88 420 binder:RegisterForClicks("anyUp")
flickerstreak@88 421 binder:SetFrameStrata("DIALOG")
flickerstreak@88 422 binder:EnableKeyboard(true)
flickerstreak@88 423 binder:EnableMouseWheel(true)
flickerstreak@88 424
flickerstreak@88 425 for k,v in pairs(self) do
flickerstreak@88 426 binder[k] = v
flickerstreak@88 427 end
flickerstreak@88 428
flickerstreak@88 429 local bg = binder:CreateTexture()
flickerstreak@88 430 bg:SetTexture(0, 0, 0, 0.5)
flickerstreak@88 431 bg:SetAllPoints(binder)
flickerstreak@88 432
flickerstreak@88 433 local text = binder:CreateFontString("OVERLAY")
flickerstreak@88 434 text:SetFontObject("GameFontNormalLarge")
flickerstreak@88 435 text:SetTextColor(0, 1, 0)
flickerstreak@88 436 text:SetAllPoints(binder)
flickerstreak@88 437 binder.text = text
flickerstreak@88 438
flickerstreak@88 439 binder:SetScript("OnClick", self.OnKeyDown)
flickerstreak@88 440 binder:SetScript("OnKeyDown", self.OnKeyDown)
flickerstreak@88 441 binder:SetScript("OnMouseWheel", self.OnMouseWheel)
flickerstreak@88 442 binder:SetScript("OnEnter", self.OnEnter)
flickerstreak@88 443 binder:SetScript("OnLeave", self.OnLeave)
flickerstreak@88 444 binder:SetScript("OnHide", self.OnHide)
flickerstreak@88 445 binder:Hide()
flickerstreak@88 446
flickerstreak@88 447 return binder
flickerstreak@88 448 end
flickerstreak@88 449
flickerstreak@88 450 function LibKeyBound.Binder:OnHide()
flickerstreak@88 451 LibKeyBound:Set(nil)
flickerstreak@88 452 end
flickerstreak@88 453
flickerstreak@88 454 function LibKeyBound.Binder:OnKeyDown(key)
flickerstreak@88 455 local button = self.button
flickerstreak@88 456 if not button then return end
flickerstreak@88 457
flickerstreak@88 458 if (key == "UNKNOWN" or key == "LSHIFT" or key == "RSHIFT" or
flickerstreak@88 459 key == "LCTRL" or key == "RCTRL" or key == "LALT" or key == "RALT" or
flickerstreak@88 460 key == "LeftButton" or key == "RightButton") then
flickerstreak@88 461 return
flickerstreak@88 462 end
flickerstreak@88 463
flickerstreak@88 464 local screenshotKey = GetBindingKey("SCREENSHOT")
flickerstreak@88 465 if screenshotKey and key == screenshotKey then
flickerstreak@88 466 Screenshot()
flickerstreak@88 467 return
flickerstreak@88 468 end
flickerstreak@88 469
flickerstreak@88 470 local openChatKey = GetBindingKey("OPENCHAT")
flickerstreak@88 471 if openChatKey and key == openChatKey then
flickerstreak@88 472 ChatFrameEditBox:Show()
flickerstreak@88 473 return
flickerstreak@88 474 end
flickerstreak@88 475
flickerstreak@88 476 if key == "MiddleButton" then
flickerstreak@88 477 key = "BUTTON3"
flickerstreak@88 478 elseif key == "Button4" then
flickerstreak@88 479 key = "BUTTON4"
flickerstreak@88 480 elseif key == "Button5" then
flickerstreak@88 481 key = "BUTTON5"
flickerstreak@88 482 end
flickerstreak@88 483
flickerstreak@88 484 if key == "ESCAPE" then
flickerstreak@88 485 self:ClearBindings(button)
flickerstreak@88 486 LibKeyBound:Set(button)
flickerstreak@88 487 return
flickerstreak@88 488 end
flickerstreak@88 489
flickerstreak@88 490 if IsShiftKeyDown() then
flickerstreak@88 491 key = "SHIFT-" .. key
flickerstreak@88 492 end
flickerstreak@88 493 if IsControlKeyDown() then
flickerstreak@88 494 key = "CTRL-" .. key
flickerstreak@88 495 end
flickerstreak@88 496 if IsAltKeyDown() then
flickerstreak@88 497 key = "ALT-" .. key
flickerstreak@88 498 end
flickerstreak@88 499
flickerstreak@88 500 if MouseIsOver(button) then
flickerstreak@88 501 self:SetKey(button, key)
flickerstreak@88 502 LibKeyBound:Set(button)
flickerstreak@88 503 end
flickerstreak@88 504 end
flickerstreak@88 505
flickerstreak@88 506 function LibKeyBound.Binder:OnMouseWheel(arg1)
flickerstreak@88 507 if arg1 > 0 then
flickerstreak@88 508 self:OnKeyDown("MOUSEWHEELUP")
flickerstreak@88 509 else
flickerstreak@88 510 self:OnKeyDown("MOUSEWHEELDOWN")
flickerstreak@88 511 end
flickerstreak@88 512 end
flickerstreak@88 513
flickerstreak@88 514 function LibKeyBound.Binder:OnEnter()
flickerstreak@88 515 local button = self.button
flickerstreak@88 516 if button and not InCombatLockdown() then
flickerstreak@88 517 if self:GetRight() >= (GetScreenWidth() / 2) then
flickerstreak@88 518 GameTooltip:SetOwner(self, "ANCHOR_LEFT")
flickerstreak@88 519 else
flickerstreak@88 520 GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
flickerstreak@88 521 end
flickerstreak@88 522
flickerstreak@88 523 if button.GetActionName then
flickerstreak@88 524 GameTooltip:SetText(button:GetActionName(), 1, 1, 1)
flickerstreak@88 525 else
flickerstreak@88 526 GameTooltip:SetText(button:GetName(), 1, 1, 1)
flickerstreak@88 527 end
flickerstreak@88 528
flickerstreak@88 529 local bindings = self:GetBindings(button)
flickerstreak@88 530 if bindings then
flickerstreak@88 531 GameTooltip:AddLine(bindings, 0, 1, 0)
flickerstreak@88 532 GameTooltip:AddLine(L.ClearTip)
flickerstreak@88 533 else
flickerstreak@88 534 GameTooltip:AddLine(L.NoKeysBoundTip, 0, 1, 0)
flickerstreak@88 535 end
flickerstreak@88 536 GameTooltip:Show()
flickerstreak@88 537 else
flickerstreak@88 538 GameTooltip:Hide()
flickerstreak@88 539 end
flickerstreak@88 540 end
flickerstreak@88 541
flickerstreak@88 542 function LibKeyBound.Binder:OnLeave()
flickerstreak@88 543 LibKeyBound:Set(nil)
flickerstreak@88 544 GameTooltip:Hide()
flickerstreak@88 545 end
flickerstreak@88 546
flickerstreak@88 547
flickerstreak@88 548 --[[ Update Functions ]]--
flickerstreak@88 549
flickerstreak@88 550 function LibKeyBound.Binder:ToBinding(button)
flickerstreak@88 551 return format("CLICK %s:LeftButton", button:GetName())
flickerstreak@88 552 end
flickerstreak@88 553
flickerstreak@88 554 function LibKeyBound.Binder:FreeKey(button, key)
flickerstreak@88 555 local msg
flickerstreak@88 556 if button.FreeKey then
flickerstreak@88 557 local action = button:FreeKey(key)
flickerstreak@88 558 if button:FreeKey(key) then
flickerstreak@88 559 msg = format(L.UnboundKey, GetBindingText(key, "KEY_"), action)
flickerstreak@88 560 end
flickerstreak@88 561 else
flickerstreak@88 562 local action = GetBindingAction(key)
flickerstreak@88 563 if action and action ~= "" and action ~= self:ToBinding(button) then
flickerstreak@88 564 msg = format(L.UnboundKey, GetBindingText(key, "KEY_"), action)
flickerstreak@88 565 end
flickerstreak@88 566 end
flickerstreak@88 567
flickerstreak@88 568 if msg then
flickerstreak@88 569 UIErrorsFrame:AddMessage(msg, 1, 0.82, 0, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 570 end
flickerstreak@88 571 end
flickerstreak@88 572
flickerstreak@88 573 function LibKeyBound.Binder:SetKey(button, key)
flickerstreak@88 574 if InCombatLockdown() then
flickerstreak@88 575 UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 576 else
flickerstreak@88 577 self:FreeKey(button, key)
flickerstreak@88 578
flickerstreak@88 579 if button.SetKey then
flickerstreak@88 580 button:SetKey(key)
flickerstreak@88 581 else
flickerstreak@88 582 SetBindingClick(key, button:GetName(), "LeftButton")
flickerstreak@88 583 end
flickerstreak@88 584
flickerstreak@88 585 local msg
flickerstreak@88 586 if button.GetActionName then
flickerstreak@88 587 msg = format(L.BoundKey, GetBindingText(key, "KEY_"), button:GetActionName())
flickerstreak@88 588 else
flickerstreak@88 589 msg = format(L.BoundKey, GetBindingText(key, "KEY_"), button:GetName())
flickerstreak@88 590 end
flickerstreak@88 591 UIErrorsFrame:AddMessage(msg, 1, 1, 1, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 592 end
flickerstreak@88 593 end
flickerstreak@88 594
flickerstreak@88 595 function LibKeyBound.Binder:ClearBindings(button)
flickerstreak@88 596 if InCombatLockdown() then
flickerstreak@88 597 UIErrorsFrame:AddMessage(L.CannotBindInCombat, 1, 0.3, 0.3, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 598 else
flickerstreak@88 599 if button.ClearBindings then
flickerstreak@88 600 button:ClearBindings()
flickerstreak@88 601 else
flickerstreak@88 602 local binding = self:ToBinding(button)
flickerstreak@88 603 while (GetBindingKey(binding)) do
flickerstreak@88 604 SetBinding(GetBindingKey(binding), nil)
flickerstreak@88 605 end
flickerstreak@88 606 end
flickerstreak@88 607
flickerstreak@88 608 local msg
flickerstreak@88 609 if button.GetActionName then
flickerstreak@88 610 msg = format(L.ClearedBindings, button:GetActionName())
flickerstreak@88 611 else
flickerstreak@88 612 msg = format(L.ClearedBindings, button:GetName())
flickerstreak@88 613 end
flickerstreak@88 614 UIErrorsFrame:AddMessage(msg, 1, 1, 1, 1, UIERRORS_HOLD_TIME)
flickerstreak@88 615 end
flickerstreak@88 616 end
flickerstreak@88 617
flickerstreak@88 618 function LibKeyBound.Binder:GetBindings(button)
flickerstreak@88 619 if button.GetBindings then
flickerstreak@88 620 return button:GetBindings()
flickerstreak@88 621 end
flickerstreak@88 622
flickerstreak@88 623 local keys
flickerstreak@88 624 local binding = self:ToBinding(button)
flickerstreak@88 625 for i = 1, select("#", GetBindingKey(binding)) do
flickerstreak@88 626 local hotKey = select(i, GetBindingKey(binding))
flickerstreak@88 627 if keys then
flickerstreak@88 628 keys = keys .. ", " .. GetBindingText(hotKey, "KEY_")
flickerstreak@88 629 else
flickerstreak@88 630 keys = GetBindingText(hotKey, "KEY_")
flickerstreak@88 631 end
flickerstreak@88 632 end
flickerstreak@88 633
flickerstreak@88 634 return keys
flickerstreak@88 635 end
flickerstreak@88 636
flickerstreak@88 637 LibKeyBound.EventButton = LibKeyBound.EventButton or CreateFrame("Frame")
flickerstreak@88 638 do
flickerstreak@88 639 local EventButton = LibKeyBound.EventButton
flickerstreak@88 640 EventButton:UnregisterAllEvents()
flickerstreak@88 641 EventButton:SetScript("OnEvent", function(self, event, addon)
flickerstreak@88 642 if (event == "PLAYER_REGEN_DISABLED") then
flickerstreak@88 643 LibKeyBound:PLAYER_REGEN_DISABLED()
flickerstreak@88 644 elseif (event == "PLAYER_REGEN_ENABLED") then
flickerstreak@88 645 LibKeyBound:PLAYER_REGEN_ENABLED()
flickerstreak@88 646 elseif (event == "PLAYER_LOGIN" and not LibKeyBound.initialized) then
flickerstreak@88 647 LibKeyBound:Initialize()
flickerstreak@88 648 EventButton:UnregisterEvent("PLAYER_LOGIN")
flickerstreak@88 649 end
flickerstreak@88 650 end)
flickerstreak@88 651
flickerstreak@88 652 if IsLoggedIn() and not LibKeyBound.initialized then
flickerstreak@88 653 LibKeyBound:Initialize()
flickerstreak@88 654 elseif not LibKeyBound.initialized then
flickerstreak@88 655 EventButton:RegisterEvent("PLAYER_LOGIN")
flickerstreak@88 656 end
flickerstreak@88 657 EventButton:RegisterEvent("PLAYER_REGEN_ENABLED")
flickerstreak@88 658 EventButton:RegisterEvent("PLAYER_REGEN_DISABLED")
flickerstreak@88 659 end