view SkeletonKey/ActionTypes.lua @ 17:500f9b2bd9ac

- for RegisterAction, use a function table instead of that if/then rats nest - consequently pet bar actions work now - unlocalize bindings data - activate keyslot input manually instead of on mouse over - activate checkbox to keep input mode active - dynamic buttons update in real time for petaction/talent/profession spells
author Nenue
date Sat, 30 Jul 2016 00:08:00 -0400
parents cdd387d39137
children 67db6b712bf3
line wrap: on
line source
-- SkeletonKey
-- ActionTypes.lua
-- Created: 7/29/2016 9:14 PM
-- %file-revision%
--
local kb, print, wrap = LibStub('LibKraken').register(KeyBinder)
local cprint = DEVIAN_WORKSPACE and function(...) _G.print('Cfg', ...) end or function() end

local SUMMON_RANDOM_FAVORITE_MOUNT_SPELL = 150544
local CLICK_KEYBINDER_MACRO = "CLICK KeyBinderMacro:"
local CLICK_KEYBINDER_KEY = "CLICK KeyBinderKey:"

local PETACTION_SCRIPT = {
  [PET_ACTION_MOVE_TO] = {SLASH_PET_MOVE_TO1, 'pet_move_to'},
  [PET_ACTION_ATTACK] = {SLASH_PET_ATTACK1, 'pet_attack'},
  [PET_ACTION_FOLLOW] = {SLASH_PET_FOLLOW1, 'pet_follow'},
  [PET_ACTION_WAIT] = {SLASH_PET_STAY1, 'pet_stay'},
  [PET_MODE_AGGRESSIVE] = {SLASH_PET_AGGRESSIVE1, 'pet_aggressive'},
  [PET_MODE_DEFENSIVE] = {SLASH_PET_DEFENSIVE1, 'pet_defensive'},
  [PET_MODE_PASSIVE] = {SLASH_PET_PASSIVE1, 'pet_passive'},
  [PET_MODE_ASSIST] = {SLASH_PET_ASSIST1, 'pet_assist'},
}

--- Caps Lock
local ACTION_HANDLERS = {}
ACTION_HANDLERS['mount'] = function(id, name)
  if id == SUMMON_RANDOM_FAVORITE_MOUNT_SPELL then
    return 'mount_random', "/script C_MountJournal.SummonByID(0)", CLICK_KEYBINDER_MACRO
  else
    return 'mount_'..id, "/script C_MountJournal.SummonByID("..id..")", CLICK_KEYBINDER_MACRO
  end
end
ACTION_HANDLERS['macro'] = function(id, name)
  return CLICK_KEYBINDER_MACRO, 'macro_' .. tostring(name), id
end
ACTION_HANDLERS['equipset'] = function(id, name)
    return CLICK_KEYBINDER_MACRO, 'equipset_'..tostring(name), "/script UseEquipmentSet("..tostring(id)..")"
end
ACTION_HANDLERS['spell'] = function(id, name)
  local attributeName = name
  if kb.ProfessionCache[id] then
    attributeName = "profession_".. kb.ProfessionCache[id].profOffset .. '_' .. kb.ProfessionCache[id].spellNum
  end
  return CLICK_KEYBINDER_KEY, attributeName, name
end
ACTION_HANDLERS['petaction'] = function(_, name)
  -- ID doesn't exist for basic commands, even though they can be picked up
  local attributeName, attributeValue = "petaction_" .. tostring(name), "/cast "..tostring(name)
  if PETACTION_SCRIPT[name] then
    attributeValue, attributeName = unpack(PETACTION_SCRIPT[name])
  end

  return CLICK_KEYBINDER_MACRO, attributeName, attributeValue
end

ACTION_HANDLERS['battlepet'] = function(id, name)
  return CLICK_KEYBINDER_MACRO, 'battlepet_' .. tostring(name), SLASH_SUMMON_BATTLE_PET1 .. " " .. tostring(name)
end
ACTION_HANDLERS['item'] = function(id, name)
  return CLICK_KEYBINDER_KEY, 'item_' .. tostring(name), id
end


--- Resolves the SecureActionButton attribute names used for the given action
kb.RegisterAction = function(actionType, id, name)

  assert(ACTION_HANDLERS[actionType], 'Missing actionType handler for `'..tostring(actionType)..'`')
  local target, attributeName, attributeValue  = ACTION_HANDLERS[actionType](id, name)

  local command = target .. attributeName
  local baseName, iterative = attributeName, 1
  while (kb.macros[attributeName] and kb.macros[attributeName][1] ~= attributeValue) do
    print(' * cannot use|cFF00FF00', attributeName, '|r"'.. tostring(kb.macros[attributeName][1]) .. '"')
    attributeName = baseName .. '_' .. iterative
    iterative = iterative + 1
  end
  if macroName ~= baseName then
    print(' * Creating|cFF00FF00', macroName)
  else
    print(' * Re-using|cFF00FF00', macroName)
  end
  kb.macros[attributeName] = {attributeValue, command}


  print('RegisterAction', actionType, id, '->', attributeName, attributeValue, target .. attributeName)
  return attributeName, attributeValue, command
end




kb.inactiveTalentBindings = {}
kb.ApplyTalentBinding = function(talentInfo, cache)
  for i = 5, #talentInfo do
    local command = CLICK_KEYBINDER_KEY.. talentInfo[2]
    SetBinding(talentInfo[i], command)
    cprint(' **', talentInfo[i], '->', command)
    tinsert(cache, talentInfo[i])
  end
end
kb.CacheTalentBinding = function(talentInfo, cache)

  local spellID = talentInfo[4]
  kb.inactiveTalentBindings[spellID] = kb.inactiveTalentBindings[spellID] or {}
  kb.inactiveTalentBindings[spellID] = {select(5,unpack(talentInfo)) }
  --cprint(spellID, unpack(kb.inactiveTalentBindings[spellID]))
end

do
  local bindings = kb.bindings
  local key, macro = KeyBinderKey, KeyBinderMacro
  kb.LoadBinding = function(command, name, icon, actionType, actionID, macroName, macroText )

    if actionType == 'spell' then
      key:SetAttribute("*type-"..name, actionType)
      key:SetAttribute("*"..actionType.."-"..name, name)
    elseif actionType == 'item' then
      key:SetAttribute("*type-"..name, actionType)
      key:SetAttribute("*"..actionType.."-"..name, name)
    elseif actionType == 'macro' then
      macro:SetAttribute("*macro-"..macroName, actionID)
    else
      macro:SetAttribute("*macrotext-"..macroName, macroText)
    end

    cprint('Loading binding', actionType, actionID)
    bindings[actionType] = bindings[actionType] or {}
    bindings[actionType][actionID] = bindings[actionType][actionID] or {}
    bindings[command] = bindings[actionType][actionID]
    return bindings[actionType], actionID
  end

  kb.ApplyBindings = function (profile)
    cprint('binding profile', profile)
    for slot, data in pairs(profile.buttons) do
      kb.LoadBinding(unpack(data))
    end

    for key, command in pairs(profile.bindings) do

      cprint(' *', key, '->', command)

      --_G.print('HotKey','loading', key, command)
      SetBinding(key, command)
      if bindings[command] and not tContains(bindings[command], key) then
        tinsert(bindings[command], key)
      end
    end

    for spellName, talentInfo in pairs(profile.talents) do
      local dummy = GetSpellInfo(spellName)
      local func = kb.CacheTalentBinding
      local dest = kb.inactiveTalentBindings
      if dummy then
        cprint('|cFFBBFF00Active:|r', dummy)
        local macroName, spellName, actionType, actionID = unpack(talentInfo)
        bindings[actionType] = bindings[actionType] or {}
        bindings[actionType][actionID] = {}
        func = kb.ApplyTalentBinding
        dest = kb.bindings[actionType][actionID]
      else

        cprint('|cFFFF4400Inactive:|r', talentInfo[2])
      end
      func(talentInfo, dest)
    end

  end

  kb.ApplyAllBindings =function ()
    table.wipe(kb.inactiveTalentBindings)


    -- reflect action key settings
    if GetCVarBool("ActionButtonUseKeyDown") then
      KeyBinderMacro:RegisterForClicks("AnyDown")
      KeyBinderKey:RegisterForClicks("AnyDown")
    else
      KeyBinderMacro:RegisterForClicks("AnyUp")
      KeyBinderKey:RegisterForClicks("AnyUp")
    end

    for i, profile in ipairs(kb.orderedProfiles) do
      kb.ApplyBindings(profile)
    end
    -- do this after to ensure that profession binds are properly overridden
    kb.UpdateProfessionInfo()


    SaveBindings(GetCurrentBindingSet())
  end
end