view SkeletonKey/SkeletonKey.lua @ 52:81a7c71c4483

- fixed safety override - fixed pet journal drag/drop - fixed confirmation cancel button
author Nenue
date Fri, 19 Aug 2016 10:46:36 -0400
parents 1aba8a6fd4a9
children a545933ddf3d
line wrap: on
line source
--------------------------------------------
-- SkeletonKey
-- Krakyn-Mal'Ganis
-- @project-revision@ @project-hash@
-- @file-revision@ @file-hash@
-- Created: 6/16/2016 3:47 AM
--------------------------------------------
-- kb
--   .StoreBinding(button, key)                     bind current keystroke to command
--   .GetSlot(index)                         return display slot
--   .SetSlot(button, command, name, icon)   assign display slot
--   .ReleaseSlot(button)                       clear button command
--   .UpdateSlot(button)                       update button contents
--   .SelectProfile(name)                         set profile character
--   .ApplyBindings(bindings)                   walk table with SetBinding()

local _
local kb, print = LibStub("LibKraken").register(KeyBinder)
kb.L = setmetatable({}, {
  __call = function(t, k, ...) return format(t[k] or k, ...) end
})
local L = kb.L

--- Caps Lock literals
local CLASS_ICON_TEXTURE = "Interface\\GLUES\\CHARACTERCREATE\\UI-CHARACTERCREATE-CLASSES"
L.BINDING_ASSIGNED = '|cFF00FF00%s|r assigned to |cFFFFFF00%s|r (%s).'
L.BINDING_REMOVED = '|cFFFFFF00%s|r (|cFF00FFFF%s|r) unbound.'
L.BINDING_FAILED_PROTECTED = '|cFFFF4400Cannot use |r|cFF00FF00%s|r|cFFFF4400 (currently |cFFFFFF00%s|r|cFFFF4400). Uncheck "Safety" to ignore this restraint.|r'


local BINDING_TYPE_SPECIALIZATION = 3
local BINDING_TYPE_CHARACTER = 2
local BINDING_TYPE_GLOBAL = 1
kb.configTitle = {
  [BINDING_TYPE_GLOBAL] = L('Global Binds'),
  [BINDING_TYPE_CHARACTER] = L('Character: %%s'),
  [BINDING_TYPE_SPECIALIZATION] = L('Specialization: %%s')
}
kb.configDescription = {
  [BINDING_TYPE_GLOBAL] = L('The bindings are applied globally.'),
  [BINDING_TYPE_CHARACTER] = L('Applied when you log onto this character.'),
  [BINDING_TYPE_SPECIALIZATION] = L('Applied when you select this specialization.'),
}

kb.ChangedBindings = {}
kb.SystemBindings = {}
kb.ActionTypes = {}
kb.TalentBindings = {}
kb.PetCache = {
  spell = {},
  spellslot = {},
  action = {},
  special = {},
  subtext = {}
}
kb.TalentCache = {}
kb.ProfessionCache = {}
kb.pendingCalls = {}
kb.pendingAttributes = {}

kb.configHeaders = {}
kb.loadedProfiles = {}
kb.orderedProfiles = {}
kb.buttons = {}
kb.macros = {}
kb.bindings = {}
kb.petFrames = {} -- pet data is slightly delayed, their buttons are indexed here so they can be refreshed
kb.talentFrames = {}
kb.professionFrames = {}

-- these are sent to plugin


local db
local _G = _G
local UnitName, SelectedRealmName, InCombatLockdown, UnitClass = UnitName, SelectedRealmName, InCombatLockdown, UnitClass
local tostring, select, tinsert, pairs = tostring, select, tinsert, pairs
local concat, wipe = table.concat, table.wipe
local classHeader, className, classID = '', '', 0

--- Control handles
local saveButton, restoreButton, clearButton

--- Returns conflicting assignment and binding profiles for use in displaying confirmations
kb.IsCommandBound = function(self, command)
  local isAssigned, assignedBy = false, db.bindMode
  local isBound, boundBy = false, db.bindMode
  command = command or self.command
  for i = 1, #kb.orderedProfiles do
    local tier = kb.orderedProfiles[i]
    if i ~= db.bindMode then

      if tier.commands[command] then
        isAssigned = true
        assignedBy = i
      end
      if tier.bound[command] then
        isBound = true
        boundBy = i
      end


      --print(' *', configHeaders[i], tier.commands[command], tier.bound[command])

      if isAssigned and isBound then
        break
      end
    end

  end

  print('|cFFFFFF00IsCommandBound:|r', command,'|r [profile:', db.bindMode .. ']', isAssigned, isBound, assignedBy, boundBy)
  return isAssigned, isBound, assignedBy, boundBy
end

local talentSpellHardCodes = {
  [109248] = 'Binding Shot',
}

--- Returns a value for use with Texture:SetDesaturated()
kb.BindingIsLocked = function(key)
  local success = false
  for i = 1, db.bindMode-1 do
    local tier = kb.orderedProfiles[i]
    if tier.bindings[key] then
      success = true
      break
    end
  end
  return success
end

--- Translates GetBindingKey() results into a printable string.
kb.BindingString = function(...)
  local stack = {}
  for i = 1, select('#', ...) do
    local key = select(i, ...)
    stack[i] = key:gsub('SHIFT', 's'):gsub('ALT', 'a'):gsub('CTRL', 'c'):gsub('SPACE', 'Sp'):gsub('BUTTON', 'M '):gsub('NUMPAD', '# ')
  end

  if #stack >= 1 then
    return concat(stack, ',')
  else
    return nil
  end
end





kb.Command = function(args, editor)
  if args:match("import") then
    kb.ImportCommmit(args)
    return
  elseif args:match("scan") then
    kb.ImportScan(args)
    kb.ui()
    return
  elseif args:match("load") then
    kb:ApplyAllBindings()
    return
  end

  if db.showUI then
    db.showUI = false
    kb:print('|cFFFFFF00KeyBinds|r trace, |cFFFF0000OFF|r.')
    kb:Hide()
  else
    db.showUI = true
    kb:print('|cFFFFFF00KeyBinds|r trace, |cFF00FF00ON|r.')
  end
  kb.ui(true)
end

kb.InitProfile = function(profile, prototype)
  if not profile then
    profile = {}
  end
  if prototype then
    print('appplying prototype', prototype)
    for k,v in pairs(prototype) do
      if not profile[k] then
        profile[k] = v
      end
    end
  end

  profile.bound = profile.bound or {}
  profile.buttons = profile.buttons or {}
  profile.commands = profile.commands or {}
  profile.bindings = profile.bindings or {}
  profile.macros = profile.macros or {}
  profile.talents = profile.talents or {}
  return profile
end

kb.ResetProfile = function(profile, prototype)
  if profile == kb.currentProfile then
    for i, button in pairs(kb.buttons) do
      kb.ReleaseSlot(button)
    end
  end
  wipe(profile)
  kb.InitProfile(profile, prototype)
end



--- Handles constructing spec profiles as they are selected


--- Obtains profile data or creates the necessary tables
kb.SelectProfileSet = function(name)

  local defaultMode
  --- General info
  classHeader, className, classID = UnitClass('player')
  print('|cFF00FF00profile:|r', name)
  print('|cFF00FF00class:|r', UnitClass('player'))

  defaultMode = BINDING_TYPE_GLOBAL
  if db[name] then
    defaultMode = BINDING_TYPE_CHARACTER
    if db[name][kb.specInfo.id] then
      defaultMode = BINDING_TYPE_SPECIALIZATION
    end
  end

  db[name] = kb.InitProfile(db[name],
    {
      classHeader = classHeader,
      className = className,
      classID = classID
    })
  db[name][kb.specInfo.id] = kb.InitProfile(db[name][kb.specInfo.id],
    {
      specID = kb.specInfo.id,
      specName = kb.specInfo.name
    })

  kb.loadedProfiles[BINDING_TYPE_GLOBAL] = db
  kb.loadedProfiles[BINDING_TYPE_CHARACTER] = db[name]
  kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] = db[name][kb.specInfo.id]
  kb.orderedProfiles = {db, db[name], db[name][kb.specInfo.id]}

  if (not db.bindMode) or (not kb.configTitle[db.bindMode]) then
    print('fixing bad bindMode value, was', db.bindMode)
    db.bindMode = defaultMode
  end


  print(BINDING_TYPE_GLOBAL)
  kb.configHeaders[BINDING_TYPE_GLOBAL] = kb.configTitle[BINDING_TYPE_GLOBAL]
  kb.configHeaders[BINDING_TYPE_CHARACTER] = kb.configTitle[BINDING_TYPE_CHARACTER]:format(UnitName('player', true))
  kb.configHeaders[BINDING_TYPE_SPECIALIZATION] = kb.configTitle[BINDING_TYPE_SPECIALIZATION]:format(kb.specInfo.name)


  setmetatable(kb.loadedProfiles[BINDING_TYPE_GLOBAL], {__tostring =function() return kb.configHeaders[BINDING_TYPE_GLOBAL] end})
  setmetatable(kb.loadedProfiles[BINDING_TYPE_CHARACTER], {__tostring =function() return kb.configHeaders[BINDING_TYPE_CHARACTER] end})
  setmetatable(kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION], {__tostring =function() return kb.configHeaders[BINDING_TYPE_SPECIALIZATION] end})

  print('|cFF00FF00bindMode:|r', db.bindMode)
  kb.currentProfile = kb.loadedProfiles[db.bindMode]
  kb.currentHeader = kb.configHeaders[db.bindMode]
end

local scrollCache = {}
kb.SelectTab = function(self)
  scrollCache[db.bindMode] = kb.scrollOffset
  db.bindMode = self:GetID()
  kb.currentProfile = kb.loadedProfiles[self:GetID()]
  kb.currentHeader = kb.configHeaders[db.bindMode]
  kb.scrollOffset = scrollCache[db.bindMode] or 0
  kb.ui(true)
end

kb.RevertBindings = function()
  -- todo: reversion code
end

kb.ConfirmBindings = function()
  kb.ApplyAllBindings()
  kb.ui()
end

--- post ADDON_LOADED
kb.variables = function()
  _G.SkeletonKeyDB = kb.InitProfile(_G.SkeletonKeyDB, {})
  kb.db = _G.SkeletonKeyDB
  kb.playerName = UnitName('player')
  kb.playerRealm = SelectedRealmName()
  kb.profileName = kb.playerRealm .. '_' .. kb.playerName
  db = kb.db

  kb.UpdateSpecInfo()
  kb.UpdateTalentInfo()
  kb.SelectProfileSet(kb.profileName)
  -- todo: redo import checking

  kb.UpdateSystemBinds()
  kb.ApplyAllBindings()

  if not InCombatLockdown() then
    kb.CreateHooks()
  else
    kb:print('Some functionality will not load until breaking combat.')
    tinsert(kb.pendingCalls, kb.CreateHooks)
  end


  kb.ui(true)
end


kb.wrap = function(module)
  kb.modules = kb.modules or {}
  tinsert(kb.modules, module)
end

-- Volatiles Access
kb.FormatActionID = function(actionType, actionID) return tostring(actionType) .. '_' .. tostring(actionID) end
kb.GetBindings = function() return kb.bindings end
kb.GetButtons = function() return kb.buttons end
kb.GetCharacterProfile = function () return kb.loadedProfiles[BINDING_TYPE_CHARACTER] end
kb.GetGlobalProfile = function () return kb.loadedProfiles[BINDING_TYPE_GLOBAL] end
kb.GetSpecProfile = function () return kb.loadedProfiles[BINDING_TYPE_SPECIALIZATION] end


SLASH_SKB1 = "/skb"
SLASH_SKB2 = "/skeletonkey"
SlashCmdList.SKB = kb.Command