annotate SkeletonKey/KeyBinds.lua @ 0:69e828f4238a

Initial Commit
author Nenue
date Mon, 20 Jun 2016 06:35:11 -0400
parents
children cd7d06bcd98d
rev   line source
Nenue@0 1 --------------------------------------------
Nenue@0 2 -- KrakTool
Nenue@0 3 -- Nick
Nenue@0 4 -- @project-revision@ @project-hash@
Nenue@0 5 -- @file-revision@ @file-hash@
Nenue@0 6 -- Created: 6/16/2016 3:47 AM
Nenue@0 7 --------------------------------------------
Nenue@0 8 -- kb
Nenue@0 9 -- .bind(button, key) bind current keystroke to command
Nenue@0 10 -- .assign(button, command, name, icon) set button command
Nenue@0 11 -- .release(button) clear button command
Nenue@0 12 -- .refresh(button) update button contents
Nenue@0 13 -- .ui() invoke interface
Nenue@0 14 -- .profile(name) set profile character
Nenue@0 15 -- .loadbinds(bindings) walk table with SetBinding()
Nenue@0 16
Nenue@0 17 local kb = KeyBinder
Nenue@0 18 local KT = select(2,...)
Nenue@0 19 KT.register(KeyBinder)
Nenue@0 20 local MIN_BIND_SLOTS = 32
Nenue@0 21 local BINDS_PER_ROW = 8
Nenue@0 22 local KEY_BUTTON_SIZE = 40
Nenue@0 23 local TAB_OFFSET = 12
Nenue@0 24 local TAB_HEIGHT = 40
Nenue@0 25 local TAB_SPACING = 2
Nenue@0 26 local BUTTON_SPACING = 4
Nenue@0 27 local BUTTON_PADDING = 12
Nenue@0 28 local HEADER_OFFSET
Nenue@0 29 local FOOTER_OFFSET
Nenue@0 30 local SUMMON_RANDOM_FAVORITE_MOUNT_SPELL = 150544;
Nenue@0 31 local BINDING_TYPE_SPECIALIZATION = 3
Nenue@0 32 local BINDING_TYPE_CHARACTER = 2
Nenue@0 33 local BINDING_TYPE_GLOBAL = 1
Nenue@0 34 local BINDING_ASSIGNED = '|cFF00FF00%s|r assigned to |cFFFFFF00%s|r (%s).'
Nenue@0 35 local BINDING_FAILED_PROTECTED = '|cFF00FF00%s|r used by |cFFFFFF00%s|r!'
Nenue@0 36 local BINDING_MODE = {
Nenue@0 37 [BINDING_TYPE_SPECIALIZATION] = 'Specialization: %s',
Nenue@0 38 [BINDING_TYPE_CHARACTER] = 'Character: %s',
Nenue@0 39 [BINDING_TYPE_GLOBAL] = 'Global Binds'
Nenue@0 40 }
Nenue@0 41 local BINDING_SCHEME_COLOR = {
Nenue@0 42 [BINDING_TYPE_SPECIALIZATION] = {0,0,0,0.5},
Nenue@0 43 [BINDING_TYPE_CHARACTER] = {0,0.25,0,0.5},
Nenue@0 44 [BINDING_TYPE_GLOBAL] = {0,.125,.5,.5}
Nenue@0 45 }
Nenue@0 46 local BINDING_SCHEME_VERTEX = {
Nenue@0 47
Nenue@0 48 [BINDING_TYPE_SPECIALIZATION] = {1,1,1,1},
Nenue@0 49 [BINDING_TYPE_CHARACTER] = {0,1,0,1},
Nenue@0 50 [BINDING_TYPE_GLOBAL] = {0,.5,1,1}
Nenue@0 51 }
Nenue@0 52
Nenue@0 53 local BINDING_SCHEME_TEXT = {
Nenue@0 54 [BINDING_TYPE_SPECIALIZATION] = {1, 1, 0},
Nenue@0 55 [BINDING_TYPE_CHARACTER] = {0, 1, 0},
Nenue@0 56 [BINDING_TYPE_GLOBAL] = {0, 1, 1}
Nenue@0 57 }
Nenue@0 58 local ACTION_SCRIPT = {
Nenue@0 59 ['mount'] = "/script C_MountJournal.SummonByID(%d)",
Nenue@0 60 ['equipset'] = "/script UseEquipmentSet(%d)",
Nenue@0 61 }
Nenue@0 62
Nenue@0 63 local COMMAND_SPELL = "^SPELL (%S.+)"
Nenue@0 64 local COMMAND_MACRO = "^MACRO (%S.+)"
Nenue@0 65 local COMMAND_ITEM = "^ITEM (%S.+)"
Nenue@0 66 local COMMAND_MOUNT = "^CLICK KeyBinderMacro:mount(%d+)"
Nenue@0 67 local COMMAND_EQUIPSET = "^CLICK KeyBinderMacro:equipset(%d+)"
Nenue@0 68
Nenue@0 69 local PICKUP_TYPES = {
Nenue@0 70 [COMMAND_SPELL] = PickupSpell,
Nenue@0 71 [COMMAND_MACRO] = PickupMacro,
Nenue@0 72 [COMMAND_ITEM] = PickupItem,
Nenue@0 73 [COMMAND_MOUNT] = C_MountJournal.Pickup,
Nenue@0 74 [COMMAND_EQUIPSET] = PickupEquipmentSet
Nenue@0 75 }
Nenue@0 76 local PICKUP_VALUES = {
Nenue@0 77 [COMMAND_SPELL] = function(name) return select(7, GetSpellInfo(name)) end
Nenue@0 78 }
Nenue@0 79 local CLASS_ICON_TEXTURE = "Interface\\GLUES\\CHARACTERCREATE\\UI-CHARACTERCREATE-CLASSES"
Nenue@0 80 local BORDER_UNASSIGNED = {0.2,0.2,0.2,1 }
Nenue@0 81 local BORDER_ASSIGNED = {0.5,0.5,0.5,1 }
Nenue@0 82 local BORDER_PENDING = {1,0.5,0,1 }
Nenue@0 83
Nenue@0 84
Nenue@0 85
Nenue@0 86 local bindMode = 3 -- not to be confused with db.bindMode which is boolean
Nenue@0 87 local bindHeader = ''
Nenue@0 88 local specHeader, specTexture, characterHeader = 'SPEC_NAME', 'Interface\\ICONS\\INV_Misc_QuestionMark', 'PLAYER_NAME'
Nenue@0 89 local bindsCommitted = true
Nenue@0 90
Nenue@0 91 local profile
Nenue@0 92 local character
Nenue@0 93 local specialization
Nenue@0 94 local global
Nenue@0 95 local priority = {}
Nenue@0 96 local numButtons = BINDS_PER_ROW * 4
Nenue@0 97 local buttons = {}
Nenue@0 98 local reverts = {}
Nenue@0 99 local KeyButton = {} -- collection of KeyButton template handlers
Nenue@0 100 local Action = {} -- collection of special action buttons for special binds
Nenue@0 101 local protected = {
Nenue@0 102 ['OPENCHATSLASH'] = true,
Nenue@0 103 ['OPENCHAT'] = true,
Nenue@0 104 }
Nenue@0 105 local saveButton, restoreButton, clearButton
Nenue@0 106
Nenue@0 107 --- Returns a value for use with Texture:SetDesaturated()
Nenue@0 108 local CommandIsLocked = function(self)
Nenue@0 109 print('command check: 1-'..(bindMode-1))
Nenue@0 110 local desaturated, layer = false, 3
Nenue@0 111 for i = 1, bindMode-1 do
Nenue@0 112 local tier = priority[i]
Nenue@0 113 local existing = tier.commands[self.command]
Nenue@0 114 print(' ', i, tier.commands[self.command])
Nenue@0 115 if existing then
Nenue@0 116 if self:GetID() ~= existing then
Nenue@0 117 -- sanitize bad data
Nenue@0 118 tier.commands[self.command] = nil
Nenue@0 119 else
Nenue@0 120 layer = i
Nenue@0 121 desaturated = true
Nenue@0 122 break
Nenue@0 123 end
Nenue@0 124 end
Nenue@0 125 end
Nenue@0 126 return desaturated, layer
Nenue@0 127 end
Nenue@0 128
Nenue@0 129 --- Returns a value for use with Texture:SetDesaturated()
Nenue@0 130 local BindingIsLocked = function(key)
Nenue@0 131 local success = false
Nenue@0 132 for i = 1, bindMode-1 do
Nenue@0 133 local tier = priority[i]
Nenue@0 134 if tier.bindings[key] then
Nenue@0 135 success = true
Nenue@0 136 break
Nenue@0 137 end
Nenue@0 138 end
Nenue@0 139 return success
Nenue@0 140 end
Nenue@0 141
Nenue@0 142 --- Translates GetBindingKey() results into a printable string.
Nenue@0 143 local BindingString = function(...)
Nenue@0 144 local stack = {}
Nenue@0 145 for i = 1, select('#', ...) do
Nenue@0 146 local key = select(i, ...)
Nenue@0 147 stack[i] = key:gsub('SHIFT', 's'):gsub('ALT', 'a'):gsub('CTRL', 'c'):gsub('SPACE', 'Sp')
Nenue@0 148 end
Nenue@0 149
Nenue@0 150 if #stack >= 1 then
Nenue@0 151 return table.concat(stack, ',')
Nenue@0 152 else
Nenue@0 153 return nil
Nenue@0 154 end
Nenue@0 155 end
Nenue@0 156
Nenue@0 157 --- This keeps our KeyDown handler from getting stuck with game controls
Nenue@0 158 KeyButton.OnUpdate = function(self)
Nenue@0 159 if not self.command then
Nenue@0 160 return
Nenue@0 161 end
Nenue@0 162
Nenue@0 163 if self:IsMouseOver() then
Nenue@0 164 if not self.active then
Nenue@0 165 -- only set this handler when the button is activated/mouseOver
Nenue@0 166 self.active = true
Nenue@0 167 self:SetScript('OnKeyDown', kb.bind)
Nenue@0 168
Nenue@0 169 local bindText = self.command
Nenue@0 170 if self.bind:GetText() then
Nenue@0 171 bindText = bindText .. ': |cFF00FF00' .. self.bind:GetText()
Nenue@0 172 end
Nenue@0 173
Nenue@0 174 kb.bindlist:SetText(bindText)
Nenue@0 175 GameTooltip:SetOwner(self)
Nenue@0 176 GameTooltip:SetAnchorType('ANCHOR_BOTTOMRIGHT')
Nenue@0 177 GameTooltip:SetText(self.actionName)
Nenue@0 178 GameTooltip:Show()
Nenue@0 179 end
Nenue@0 180 else
Nenue@0 181 if self.active then
Nenue@0 182 GameTooltip:Hide()
Nenue@0 183 self.active = nil
Nenue@0 184 self:SetScript('OnKeyDown', nil)
Nenue@0 185 end
Nenue@0 186 end
Nenue@0 187 end
Nenue@0 188
Nenue@0 189 --- Cursor pickup handler
Nenue@0 190 -- Walks through PICKUP_TYPES and runs the function if match(command, key) turns up a result.
Nenue@0 191 -- Passes the result through PICKUP_VALUES[pattern]() if defined.
Nenue@0 192
Nenue@0 193 kb.pickup = function(self)
Nenue@0 194 for pattern, pickup in pairs(PICKUP_TYPES) do
Nenue@0 195 local value = self.command:match(pattern)
Nenue@0 196 if value then
Nenue@0 197 if PICKUP_VALUES[pattern] then
Nenue@0 198 value = PICKUP_VALUES[pattern](value)
Nenue@0 199 end
Nenue@0 200
Nenue@0 201 pickup(value)
Nenue@0 202 kb.release(self)
Nenue@0 203 break
Nenue@0 204 end
Nenue@0 205 end
Nenue@0 206 end
Nenue@0 207
Nenue@0 208 --- Setup an action button base on template info
Nenue@0 209 kb.action = function(type, id)
Nenue@0 210
Nenue@0 211 local macroName = type .. id
Nenue@0 212 macroName = macroName:gsub(' ', '')
Nenue@0 213
Nenue@0 214 local attribute = '*macrotext-'..macroName
Nenue@0 215 local value = ACTION_SCRIPT[type]:format(id)
Nenue@0 216 local command = 'CLICK KeyBinderMacro:'.. macroName
Nenue@0 217 profile.macros[attribute] = {value, command}
Nenue@0 218
Nenue@0 219 KeyBinderMacro:SetAttribute(attribute, value)
Nenue@0 220 return command
Nenue@0 221 end
Nenue@0 222
Nenue@0 223 KeyButton.OnDragStart = function(self)
Nenue@0 224 if not self.command then
Nenue@0 225 return
Nenue@0 226 end
Nenue@0 227 kb.pickup(self)
Nenue@0 228 end
Nenue@0 229
Nenue@0 230 KeyButton.OnReceiveDrag = function(self, ...)
Nenue@0 231 print(self:GetName(),'|cFF0088FFreceived|r', ...)
Nenue@0 232 local type, value, subType, subData = GetCursorInfo()
Nenue@0 233 print('GetCursorInfo', type, value, subType, subData)
Nenue@0 234 if type then
Nenue@0 235 if type == 'spell' then
Nenue@0 236 value = subData
Nenue@0 237 end
Nenue@0 238
Nenue@0 239 local command, name, icon, _
Nenue@0 240 if type == 'spell' then
Nenue@0 241 name, _, icon = GetSpellInfo(value)
Nenue@0 242 command = 'SPELL ' .. name
Nenue@0 243 name = ''
Nenue@0 244 elseif type == 'macro' then
Nenue@0 245 name, icon = GetMacroInfo(value)
Nenue@0 246 command = 'MACRO ' .. name
Nenue@0 247 elseif type == 'mount' then
Nenue@0 248 if subType == 0 then
Nenue@0 249 name, _, icon = GetSpellInfo(SUMMON_RANDOM_FAVORITE_MOUNT_SPELL)
Nenue@0 250 value= 0
Nenue@0 251 else
Nenue@0 252 name, _, icon = C_MountJournal.GetMountInfoByID(value)
Nenue@0 253 end
Nenue@0 254 command = kb.action(type, value)
Nenue@0 255 elseif type == 'item' then
Nenue@0 256 name = GetItemInfo(value)
Nenue@0 257 icon = GetItemIcon(value)
Nenue@0 258 command = 'ITEM ' .. name
Nenue@0 259 end
Nenue@0 260 kb.assign(self, command, name, icon)
Nenue@0 261 kb.refresh(self)
Nenue@0 262 ClearCursor()
Nenue@0 263 end
Nenue@0 264 end
Nenue@0 265
Nenue@0 266 KeyButton.OnMouseDown = function(self, click)
Nenue@0 267 print(self:GetName(), 'OnMouseDown', click)
Nenue@0 268 if click == 'LeftButton' then
Nenue@0 269 KeyButton.OnReceiveDrag(self)
Nenue@0 270 elseif click == 'RightButton' then
Nenue@0 271 kb.release(self)
Nenue@0 272 else
Nenue@0 273 kb.bind(self)
Nenue@0 274 end
Nenue@0 275 end
Nenue@0 276
Nenue@0 277
Nenue@0 278 --- Updates the current KeyBinding for the button's command
Nenue@0 279 kb.bind = function(self, key)
Nenue@0 280
Nenue@0 281 print('|cFFFFFF00bind|cFFFFFF00', self:GetID(), '|cFF00FFFF', key)
Nenue@0 282 if not self.command then
Nenue@0 283 return
Nenue@0 284 end
Nenue@0 285
Nenue@0 286 if key:match('[RL]SHIFT') or key:match('[RL]ALT') or key:match('[RL]CTRL') then
Nenue@0 287 return
Nenue@0 288 end
Nenue@0 289
Nenue@0 290 if protected[GetBindingAction(key)] then
Nenue@0 291 return
Nenue@0 292 kb.bindlist:SetText(BINDING_FAILED_PROTECTED:format(key, GetBindingAction(key)))
Nenue@0 293 end
Nenue@0 294
Nenue@0 295 if key == 'ESCAPE' then
Nenue@0 296 local key1, key2 = GetBindingKey(self.command)
Nenue@0 297 if key1 then
Nenue@0 298 SetBinding(key1, nil)
Nenue@0 299 print('Unbound', key1)
Nenue@0 300 end
Nenue@0 301 if key2 then
Nenue@0 302 SetBinding(key2, nil)
Nenue@0 303 print('Unbound', key2)
Nenue@0 304 end
Nenue@0 305 self.active = false
Nenue@0 306 return
Nenue@0 307 end
Nenue@0 308
Nenue@0 309 local modifier = ''
Nenue@0 310 if IsAltKeyDown() then
Nenue@0 311 modifier = 'ALT-'
Nenue@0 312 end
Nenue@0 313 if IsControlKeyDown() then
Nenue@0 314 modifier = modifier.. 'CTRL-'
Nenue@0 315 end
Nenue@0 316 if IsShiftKeyDown() then
Nenue@0 317 modifier = modifier..'SHIFT-'
Nenue@0 318 end
Nenue@0 319
Nenue@0 320 if self.command then
Nenue@0 321 self.binding = modifier..key
Nenue@0 322 self.pending = true
Nenue@0 323 self.border:SetColorTexture(1,.5,0, 1)
Nenue@0 324
Nenue@0 325 local old = GetBindingAction(self.binding)
Nenue@0 326 local binding1, binding2, new1, new2
Nenue@0 327 if old and old ~= self.command then
Nenue@0 328 print('Discarding keybind for', old)
Nenue@0 329 local binding1, binding2 = GetBindingKey(old)
Nenue@0 330 -- need to preserve argument order
Nenue@0 331 end
Nenue@0 332 tinsert(reverts, {old, binding1, binding2, new1, new2})
Nenue@0 333
Nenue@0 334
Nenue@0 335 bindsCommitted = false
Nenue@0 336 SetBinding(self.binding, self.command)
Nenue@0 337 for level, profile in ipairs(priority) do
Nenue@0 338 profile.bindings[self.binding] = (level == bindMode) and self.command or nil
Nenue@0 339 end
Nenue@0 340 print(BINDING_ASSIGNED:format(self.binding, self.command, BINDING_MODE[bindMode]:format(bindHeader)))
Nenue@0 341
Nenue@0 342 kb.refresh(self)
Nenue@0 343 end
Nenue@0 344 end
Nenue@0 345
Nenue@0 346 --- Resets button command
Nenue@0 347 kb.release = function(self)
Nenue@0 348 local index = self:GetID()
Nenue@0 349 self.command = nil
Nenue@0 350 self.actionName = nil
Nenue@0 351 self.macro:SetText(nil)
Nenue@0 352 self.profile = nil
Nenue@0 353 self.bind:SetText(nil)
Nenue@0 354 self.icon:SetTexture(nil)
Nenue@0 355 self.border:SetColorTexture(unpack(BORDER_UNASSIGNED))
Nenue@0 356 self:EnableKeyboard(false)
Nenue@0 357 self:SetScript('OnKeyDown', nil)
Nenue@0 358
Nenue@0 359
Nenue@0 360 if profile.buttons[index] then
Nenue@0 361 profile.buttons[index] = nil
Nenue@0 362 end
Nenue@0 363 end
Nenue@0 364
Nenue@0 365 -- Sets button command
Nenue@0 366
Nenue@0 367 kb.assign = function(self, command, name, icon)
Nenue@0 368 local index = self:GetID()
Nenue@0 369 print('|cFF00FFFFassign|cFF0088FF', index, '|cFFFFFF00'.. (command or 'none'), '|cFF00FF00'.. (name or ''), '|cFF00FFFF' .. (icon or ''))
Nenue@0 370
Nenue@0 371
Nenue@0 372 if command then
Nenue@0 373 if command:match(COMMAND_SPELL) then
Nenue@0 374 name = command:match(COMMAND_SPELL)
Nenue@0 375 end
Nenue@0 376
Nenue@0 377
Nenue@0 378 self:EnableKeyboard(true)
Nenue@0 379 print('profile.buttons['..index..'] |cFF00FFFF=|r ', command, name, icon)
Nenue@0 380 profile.buttons[index] = {command, name, icon}
Nenue@0 381
Nenue@0 382 --- Clean up any residual buttons
Nenue@0 383 local previous = profile.commands[command]
Nenue@0 384 if previous ~= index and buttons[previous] then
Nenue@0 385 kb.release(buttons[previous])
Nenue@0 386 end
Nenue@0 387
Nenue@0 388 profile.commands[command] = index
Nenue@0 389 end
Nenue@0 390
Nenue@0 391 self.profile = bindMode
Nenue@0 392 self.actionName = name
Nenue@0 393 self.command = command
Nenue@0 394 self.icon:SetTexture(icon)
Nenue@0 395 self:RegisterForDrag('LeftButton')
Nenue@0 396 end
Nenue@0 397
Nenue@0 398 --- Retrieves button at index; creates said button and instates any stored parameters
Nenue@0 399 kb.keyslot = function(index)
Nenue@0 400 if not buttons[index] then
Nenue@0 401 local button = CreateFrame('CheckButton', 'KeyBinderSlot'..index, kb, 'KeyButton')
Nenue@0 402 button:SetScript('OnMouseDown', KeyButton.OnMouseDown)
Nenue@0 403 button:SetScript('OnMouseUp', KeyButton.OnMouseUp)
Nenue@0 404 button:SetScript('OnUpdate', KeyButton.OnUpdate)
Nenue@0 405 button:SetScript('OnDragStart', KeyButton.OnDragStart)
Nenue@0 406 button:SetScript('OnReceiveDrag', KeyButton.OnReceiveDrag)
Nenue@0 407 button:SetID(index)
Nenue@0 408
Nenue@0 409 if profile.buttons[index] and type(profile.buttons[index] ) == 'table' then
Nenue@0 410 kb.assign(button, unpack(profile.buttons[index] ))
Nenue@0 411 else
Nenue@0 412 kb.release(button)
Nenue@0 413 end
Nenue@0 414
Nenue@0 415 local x, y = BUTTON_PADDING, - (BUTTON_PADDING + HEADER_OFFSET)
Nenue@0 416 if index ~= 1 then
Nenue@0 417 local col = mod(index, BINDS_PER_ROW)
Nenue@0 418 if col == 0 then
Nenue@0 419 col = BINDS_PER_ROW - 1
Nenue@0 420 else
Nenue@0 421 col = col - 1
Nenue@0 422 end
Nenue@0 423 x = col * (KEY_BUTTON_SIZE + BUTTON_SPACING) + BUTTON_PADDING
Nenue@0 424 y = (ceil(index/ BINDS_PER_ROW)-1) * - (KEY_BUTTON_SIZE + BUTTON_SPACING) - BUTTON_PADDING - HEADER_OFFSET
Nenue@0 425 end
Nenue@0 426 button:SetSize(KEY_BUTTON_SIZE, KEY_BUTTON_SIZE)
Nenue@0 427 button:SetPoint('TOPLEFT', kb, 'TOPLEFT', x, y)
Nenue@0 428 button:Show()
Nenue@0 429 buttons[index] = button
Nenue@0 430 end
Nenue@0 431 return buttons[index]
Nenue@0 432 end
Nenue@0 433
Nenue@0 434 --- Updates profile assignment and button contents
Nenue@0 435 kb.refresh = function(self)
Nenue@0 436 if self.profile ~= bindMode then
Nenue@0 437 if profile.buttons[self:GetID()] then
Nenue@0 438 kb.assign(self, unpack(profile.buttons[self:GetID()]))
Nenue@0 439 else
Nenue@0 440 kb.release(self)
Nenue@0 441 end
Nenue@0 442 end
Nenue@0 443
Nenue@0 444 if self.command then
Nenue@0 445 if self.pending then
Nenue@0 446 self.border:SetColorTexture(unpack(BORDER_PENDING))
Nenue@0 447 else
Nenue@0 448 self.border:SetColorTexture(unpack(BORDER_ASSIGNED))
Nenue@0 449 end
Nenue@0 450 --self.macro:SetText(self.actionName)
Nenue@0 451 self.bind:SetText(BindingString(GetBindingKey(self.command)))
Nenue@0 452 local locked, layer = CommandIsLocked(self)
Nenue@0 453 self.icon:SetDesaturated(locked)
Nenue@0 454 self.icon:SetVertexColor(unpack(BINDING_SCHEME_VERTEX[layer]))
Nenue@0 455 else
Nenue@0 456 self.border:SetColorTexture(unpack(BORDER_UNASSIGNED))
Nenue@0 457 --self.macro:SetText(nil)
Nenue@0 458 self.bind:SetText(nil)
Nenue@0 459 end
Nenue@0 460 end
Nenue@0 461
Nenue@0 462 local SetupUI = function()
Nenue@0 463
Nenue@0 464
Nenue@0 465 kb.tabAnchor = {'TOPLEFT', kb, 'TOPRIGHT', 2, -TAB_OFFSET}
Nenue@0 466 kb.tabGrowth = {'TOPLEFT', nil,'BOTTOMLEFT', 0, -TAB_SPACING}
Nenue@0 467 kb.tabSize = {TAB_HEIGHT, TAB_HEIGHT }
Nenue@0 468 kb.UIPanelAnchor = {'TOPLEFT', kb, 'TOPLEFT', BUTTON_PADDING + 12, -BUTTON_PADDING}
Nenue@0 469 kb.UIPanelGrowth = {'TOPLEFT', nil, 'TOPRIGHT', 14, 0 }
Nenue@0 470 kb.controlsAnchor = {'BOTTOMLEFT', kb, BUTTON_PADDING, BUTTON_PADDING }
Nenue@0 471 kb.controlsGrowth = {'BOTTOMLEFT', nil, 'BOTTOMRIGHT', BUTTON_SPACING, 0}
Nenue@0 472
Nenue@0 473 --tab() frame, name, tooltip, texture, coords
Nenue@0 474 kb:tab('KeyBinderGlobalTab', BINDING_MODE[1], "Interface\\ICONS\\item_azereansphere", {0.15,.85,.15,.85})
Nenue@0 475 kb:tab('KeyBinderCharacterTab', characterHeader, nil)
Nenue@0 476 kb:tab('KeyBinderSpecTab', specHeader, specTexture)
Nenue@0 477 SetPortraitTexture(KeyBinderCharacterTab.icon, 'player')
Nenue@0 478 KeyBinderCharacterTab.icon:SetTexCoord(0.15,.85,.15,.85)
Nenue@0 479
Nenue@0 480 saveButton = kb:button('KeyBinderSaveButton', 'Save', 'Commit all changes.', nil, kb.save)
Nenue@0 481 restoreButton = kb:button('KeyBinderRestoreButton', 'Discard', 'Revert all changes.', nil, kb.restore)
Nenue@0 482 clearButton = kb:button('KeyBinderClearButton', 'Clear Page', 'Release all buttons.', nil, kb.ResetProfile)
Nenue@0 483
Nenue@0 484 kb:uibutton(
Nenue@0 485 'KeyBinderSpellBookButton', 'SpellBook', nil,
Nenue@0 486 function() ToggleSpellBook(BOOKTYPE_SPELL) end,
Nenue@0 487 "Interface\\Spellbook\\Spellbook-Icon")
Nenue@0 488 kb:uibutton(
Nenue@0 489 'KeyBinderTalentFrameButton', 'Talents', nil,
Nenue@0 490 function() ToggleTalentFrame() end,
Nenue@0 491 "Interface\\TargetingFrame\\UI-Classes-Circles",
Nenue@0 492 CLASS_ICON_TCOORDS[strupper(select(2,UnitClass("player")))])
Nenue@0 493
Nenue@0 494 kb:uibutton(
Nenue@0 495 'KeyBinderMacroFrameButton', 'Macros', nil,
Nenue@0 496 function() if MacroFrame then HideUIPanel(MacroFrame) else ShowMacroFrame() end end,
Nenue@0 497 "Interface\\MacroFrame\\MacroFrame-Icon")
Nenue@0 498
Nenue@0 499 kb:uibutton(
Nenue@0 500 'KeyBinderInventoryButton', 'Bags', nil,
Nenue@0 501 function() OpenAllBags() end,
Nenue@0 502 "Interface\\BUTTONS\\Button-Backpack-Up")
Nenue@0 503
Nenue@0 504 kb.info:SetPoint('TOPLEFT', kb.UIPanels[1], 'BOTTOMLEFT', 0, -BUTTON_SPACING)
Nenue@0 505 HEADER_OFFSET = kb.UIPanels[1]:GetHeight() + BUTTON_PADDING
Nenue@0 506 FOOTER_OFFSET = saveButton:GetHeight() + BUTTON_PADDING
Nenue@0 507 end
Nenue@0 508
Nenue@0 509 --- Invokes the KeyBinder frame (from the /kb function or some other source)
Nenue@0 510 kb.ui = function()
Nenue@0 511 if not KT.db.bindMode then
Nenue@0 512 return
Nenue@0 513 end
Nenue@0 514
Nenue@0 515 if not kb:IsVisible() then
Nenue@0 516 kb:Show()
Nenue@0 517 KT.db.bindMode = true
Nenue@0 518 end
Nenue@0 519
Nenue@0 520 if not kb.loaded then
Nenue@0 521 SetupUI()
Nenue@0 522 kb.loaded = true
Nenue@0 523 end
Nenue@0 524
Nenue@0 525 for i = 1, numButtons do
Nenue@0 526 kb.refresh(kb.keyslot(i))
Nenue@0 527 end
Nenue@0 528
Nenue@0 529 if bindMode == BINDING_TYPE_SPECIALIZATION then
Nenue@0 530 bindHeader = select(2,GetSpecializationInfo(GetSpecialization()))
Nenue@0 531 elseif bindMode == BINDING_TYPE_CHARACTER then
Nenue@0 532 bindHeader = UnitName('player')
Nenue@0 533 else
Nenue@0 534 bindHeader = ''
Nenue@0 535 end
Nenue@0 536
Nenue@0 537 if bindsCommitted then
Nenue@0 538 KeyBinderSaveButton:Disable()
Nenue@0 539 KeyBinderRestoreButton:Disable()
Nenue@0 540 else
Nenue@0 541 KeyBinderSaveButton:Enable()
Nenue@0 542 KeyBinderRestoreButton:Enable()
Nenue@0 543 end
Nenue@0 544
Nenue@0 545 --- panel attributes
Nenue@0 546 local numRows = numButtons/BINDS_PER_ROW
Nenue@0 547 kb:SetHeight( numRows * (KEY_BUTTON_SIZE) + (numRows - 1) * BUTTON_SPACING + HEADER_OFFSET + FOOTER_OFFSET + BUTTON_PADDING * 2)
Nenue@0 548 kb:SetWidth((BINDS_PER_ROW - 1) * BUTTON_SPACING + BINDS_PER_ROW * KEY_BUTTON_SIZE + BUTTON_PADDING * 2)
Nenue@0 549 kb.bg:SetColorTexture(unpack(BINDING_SCHEME_COLOR[bindMode]))
Nenue@0 550
Nenue@0 551
Nenue@0 552 for i, tab in ipairs(kb.tabButtons) do
Nenue@0 553
Nenue@0 554 local n = tab:GetNormalTexture()
Nenue@0 555 local tabTexture = "Interface\\Buttons\\UI-Quickslot2"
Nenue@0 556 local left, top, right, bottom = -12, 12, 13, -13
Nenue@0 557 if i == bindMode then
Nenue@0 558 tabTexture = "Interface\\Buttons\\CheckButtonGlow"
Nenue@0 559 left, top, right, bottom = -14, 14, 15, -15
Nenue@0 560 end
Nenue@0 561 n:SetTexture(tabTexture)
Nenue@0 562 n:SetPoint('TOPLEFT', tab, 'TOPLEFT', left, top)
Nenue@0 563 n:SetPoint('BOTTOMRIGHT', tab, 'BOTTOMRIGHT', right, bottom)
Nenue@0 564 end
Nenue@0 565 end
Nenue@0 566
Nenue@0 567 kb.loadbinds = function (bindings)
Nenue@0 568 for key, command in pairs(bindings) do
Nenue@0 569 -- store for reversion
Nenue@0 570 local oldAction = GetBindingAction(key)
Nenue@0 571 if oldAction ~= command then
Nenue@0 572 local bind1, bind2 = GetBindingKey(oldAction)
Nenue@0 573 if bind1 and not reverts[bind1] then
Nenue@0 574 reverts[bind1] = oldAction
Nenue@0 575 end
Nenue@0 576 if bind2 and not reverts[bind2] then
Nenue@0 577 reverts[bind2] = oldAction
Nenue@0 578 end
Nenue@0 579 end
Nenue@0 580 SetBindings(key, command)
Nenue@0 581 end
Nenue@0 582 SaveBindings()
Nenue@0 583 end
Nenue@0 584
Nenue@0 585 local ACTION_BARS = {
Nenue@0 586 'MultiBarBottomLeftButton',
Nenue@0 587 'MultiBarBottomRighttButton',
Nenue@0 588 'MultiBarLeftButton',
Nenue@0 589 'MultiBarRightButton',
Nenue@0 590 'ActionButton'
Nenue@0 591 }
Nenue@0 592 kb.HotKeyText = function ()
Nenue@0 593 for _, prefix in ipairs(ACTION_BARS) do
Nenue@0 594 for i = 1,12 do
Nenue@0 595 local button = _G[prefix .. i]
Nenue@0 596 if button and button.action then
Nenue@0 597 local type, id, subType, subID = GetActionInfo(button.action)
Nenue@0 598 if type == 'spell' then
Nenue@0 599 local name = GetSpellInfo(id)
Nenue@0 600 local bind, bind2 = GetBindingKey('SPELL '..name)
Nenue@0 601 if bind or bind2 then
Nenue@0 602 --print('SPELL '..name, GetBindingKey('SPELL '..name))
Nenue@0 603 button.HotKey:SetText(BindingString(bind))
Nenue@0 604 button.HotKey:Show()
Nenue@0 605 end
Nenue@0 606 end
Nenue@0 607 end
Nenue@0 608 end
Nenue@0 609 end
Nenue@0 610 end
Nenue@0 611
Nenue@0 612 kb.InitProfile = function(profile)
Nenue@0 613 profile.buttons = profile.buttons or {}
Nenue@0 614 profile.commands = profile.commands or {}
Nenue@0 615 profile.bindings = profile.bindings or {}
Nenue@0 616 profile.macros = profile.macros or {}
Nenue@0 617 return profile
Nenue@0 618 end
Nenue@0 619 kb.ResetProfile = function()
Nenue@0 620
Nenue@0 621 for i, button in pairs(buttons) do
Nenue@0 622 kb.release(button)
Nenue@0 623 end
Nenue@0 624
Nenue@0 625 profile.commands = {}
Nenue@0 626 profile.bindings = {}
Nenue@0 627 profile.macros = {}
Nenue@0 628 end
Nenue@0 629
Nenue@0 630 --- Gives us the profile structure to work with while instating data
Nenue@0 631 kb.profile = function(name)
Nenue@0 632 KT.db = KT.db or {}
Nenue@0 633 global = kb.InitProfile(KT.db)
Nenue@0 634 profile = global
Nenue@0 635 local subtitle
Nenue@0 636 if name then
Nenue@0 637 KT.db[name] = KT.db[name] or {}
Nenue@0 638 KT.db[name] = kb.InitProfile(KT.db[name])
Nenue@0 639 character = KT.db[name]
Nenue@0 640 local spec = GetSpecialization()
Nenue@0 641 if spec then
Nenue@0 642 KT.db[name][spec] = KT.db[name][spec] or {}
Nenue@0 643 profile = kb.InitProfile(KT.db[name][spec])
Nenue@0 644 bindMode = BINDING_TYPE_SPECIALIZATION
Nenue@0 645 subtitle = select(2,GetSpecializationInfo(spec))
Nenue@0 646 specialization = KT.db[name][spec]
Nenue@0 647 else
Nenue@0 648 profile = kb.InitProfile(KT.db[name])
Nenue@0 649 bindMode = BINDING_TYPE_CHARACTER
Nenue@0 650 subtitle = name
Nenue@0 651 specialization = character
Nenue@0 652 end
Nenue@0 653 end
Nenue@0 654 priority = {global, character, specialization }
Nenue@0 655
Nenue@0 656
Nenue@0 657
Nenue@0 658 if not KT.db.bindsPage then
Nenue@0 659 KT.db.bindsPage = bindMode
Nenue@0 660 end
Nenue@0 661 bindMode = KT.db.bindsPage
Nenue@0 662
Nenue@0 663
Nenue@0 664 if not BINDING_MODE[bindMode] then
Nenue@0 665 bindMode = 3
Nenue@0 666 KT.db.bindsPage = 3
Nenue@0 667 print('overriding', bindMode)
Nenue@0 668 end
Nenue@0 669
Nenue@0 670 profile = priority[bindMode]
Nenue@0 671
Nenue@0 672
Nenue@0 673 local _
Nenue@0 674 _, specHeader, _, specTexture = GetSpecializationInfo(GetSpecialization())
Nenue@0 675 print(GetSpecializationInfo(GetSpecialization()))
Nenue@0 676 specHeader = BINDING_MODE[2]:format(specHeader)
Nenue@0 677 characterHeader = BINDING_MODE[2]:format(UnitName('player'))
Nenue@0 678
Nenue@0 679 print('Using binding profile |cFF00FF88'..BINDING_MODE[bindMode]:format(subtitle)..'|r')
Nenue@0 680 end
Nenue@0 681
Nenue@0 682 kb.SelectTab = function(self)
Nenue@0 683 bindMode = self:GetID()
Nenue@0 684 profile = priority[self:GetID()]
Nenue@0 685 KT.db.bindsPage = self:GetID()
Nenue@0 686 kb.ui()
Nenue@0 687 end
Nenue@0 688 kb.save = function()
Nenue@0 689 SaveBindings(GetCurrentBindingSet())
Nenue@0 690 bindsCommitted = true
Nenue@0 691 for i, button in ipairs(buttons) do
Nenue@0 692 button.pending = false
Nenue@0 693 end
Nenue@0 694
Nenue@0 695 kb.ui()
Nenue@0 696 print('Bindings saved.')
Nenue@0 697 end
Nenue@0 698 kb.restore = function()
Nenue@0 699 for i, button in pairs(buttons) do
Nenue@0 700 button.pending = false
Nenue@0 701 end
Nenue@0 702 bindsCommitted = true
Nenue@0 703 LoadBindings(GetCurrentBindingSet())
Nenue@0 704 print('All changes discarded.')
Nenue@0 705 end
Nenue@0 706
Nenue@0 707 --- Tells all the hud buttons what to do
Nenue@0 708 kb.init = function()
Nenue@0 709 KeyBinderMacro:SetAttribute('*type*', 'macro')
Nenue@0 710 end
Nenue@0 711
Nenue@0 712 --- Get started
Nenue@0 713 kb.variables = function()
Nenue@0 714 kb.profile(GetUnitName('player', true))
Nenue@0 715 for i = 1, 3 do
Nenue@0 716 for attribute, data in pairs(priority[i].macros) do
Nenue@0 717 KeyBinderMacro:SetAttribute(attribute, data[1])
Nenue@0 718 end
Nenue@0 719 end
Nenue@0 720
Nenue@0 721 kb.UPDATE_BINDINGS()
Nenue@0 722 kb:RegisterEvent('UPDATE_BINDINGS')
Nenue@0 723 kb:RegisterEvent('UPDATE_MACROS')
Nenue@0 724 kb:RegisterEvent('PLAYER_SPECIALIZATION_CHANGED')
Nenue@0 725 kb:RegisterEvent('PLAYER_EQUIPMENT_CHANGED')
Nenue@0 726 kb:RegisterEvent('PLAYER_REGEN_DISABLED')
Nenue@0 727 kb:RegisterEvent('PLAYER_REGEN_ENABLED')
Nenue@0 728 kb:RegisterEvent('ACTIONBAR_SLOT_CHANGED')
Nenue@0 729 end
Nenue@0 730
Nenue@0 731 kb.close = function()
Nenue@0 732 KT.db.bindMode = false
Nenue@0 733 kb:Hide()
Nenue@0 734 end
Nenue@0 735
Nenue@0 736 kb.PLAYER_REGEN_DISABLED = function()
Nenue@0 737 if KT.db.bindMode then
Nenue@0 738
Nenue@0 739 kb:Hide()
Nenue@0 740 end
Nenue@0 741 end
Nenue@0 742
Nenue@0 743 kb.PLAYER_REGEN_ENABLED = function()
Nenue@0 744 if KT.db.bindMode then
Nenue@0 745 kb.ui()
Nenue@0 746 end
Nenue@0 747 end
Nenue@0 748 --- Refresh buttons if macros are updated
Nenue@0 749 kb.UPDATE_BINDINGS = function()
Nenue@0 750 if KT.db.bindMode then
Nenue@0 751 kb.ui()
Nenue@0 752 end
Nenue@0 753 kb.HotKeyText()
Nenue@0 754 end
Nenue@0 755
Nenue@0 756 kb.ACTIONBAR_SLOT_CHANGED = function()
Nenue@0 757 kb.HotKeyText()
Nenue@0 758 end
Nenue@0 759
Nenue@0 760
Nenue@0 761
Nenue@0 762 kb.UPDATE_MACROS = kb.UPDATE_BINDINGS
Nenue@0 763
Nenue@0 764
Nenue@0 765 SLASH_KB1 = "/kb"
Nenue@0 766 SlashCmdList.KB = function()
Nenue@0 767 if KT.db.bindMode then
Nenue@0 768 KT.db.bindMode = false
Nenue@0 769 print('|cFFFFFF00KeyBinds|r trace, |cFFFF0000OFF|r.')
Nenue@0 770 kb:Hide()
Nenue@0 771 else
Nenue@0 772 KT.db.bindMode = true
Nenue@0 773 print('|cFFFFFF00KeyBinds|r trace, |cFF00FF00ON|r.')
Nenue@0 774 kb.ui()
Nenue@0 775 end
Nenue@0 776 end