view SkeletonKey/HotKey.lua @ 63:2409fe9b81e1

- check macro spells when considering whether a binding should be treated as a talent - clean up talent binding data when releasing/unbinding slots with a related macro/spell
author Nenue
date Thu, 08 Sep 2016 16:52:55 -0400
parents 04c23ceaf9e0
children
line wrap: on
line source
-- KrakTool
-- HotKey.lua
-- Created: 7/22/2016 10:28 PM
-- %file-revision%
-- Module for fixing actionbar hotkey text

local _G, wipe, tContains, tinsert = _G, table.wipe, tContains, tinsert

local hotkey = CreateFrame('Frame', 'SkeletonHotKeys', UIParent)
local kb, print, wrap = LibStub("LibKraken").register(KeyBinder, hotkey)
local hotkeyText = {}
local blizHotKey = {}
local bindings

-- frames obtained via post-load hooks, created by addons like Dominos or BarTender4
local loadedFrames = {}
-- frames divided by update categories
local categoryFrames = {}
-- frames indexed by action slot ID (just the action bar, for... reasons)
local actionSlots = {}
local actionFrames = {}
local actionIndex = {}


--- Used to determine which groups of action buttons need updating
local hotkeyEvents = {
  ["UPDATE_BONUS_ACTIONBAR"] = {"bonus"},
  ["UPDATE_VEHICLE_ACTIONBAR"] = {"vehicle"},
  ["UPDATE_OVERRIDE_ACTIONBAR"] = {"override"},
  ["ACTIONBAR_PAGE_CHANGED"] = {"actionbar"},
  ["ACTIONBAR_SLOT_CHANGED"] = {"actionslot"},
  ["PLAYER_ENTERING_WORLD"] = {"world","all"},
  ["PET_UI_UPDATE"] = {"pet"},
  ["PLAYER_SPECIALIZATION_CHANGED"] = {"player"},
  ["PLAYER_TALENTS_UPDATED"] = {"player"},
}


hotkey.wrapEvent = function(event, ...)
  hotkey:RegisterEvent(event)
  hotkeyEvents[event] = {...}
  hotkey[event] = hotkey.UpdateFromEvent
end

hotkey.unwrapEvent = function(event)
  if not kb[event] then
    kb:UnregisterEvent(event)
  end
  hotkeyEvents[event] = nil
  hotkey[event] = nil
end


hotkey.RegisterFrame = function(frame)
  --wrap(function()
  print('ActionBarButtonEventsFrame_RegisterFrame(', frame:GetName(), frame.action, frame:IsVisible(), frame:IsShown())
  --end)
  blizHotKey[frame] = frame.HotKey
  loadedFrames[frame] = true
end

hotkey.UpdateFromEvent = function(self, event, ...)
  if hotkeyEvents[event] then
    --print('call batch', event, ...)
    for i, func  in ipairs(hotkeyEvents[event]) do

      if hotkey[func] then
        print('->|cFF88FF00', func)
        hotkey[func](self, event, ...)
      end
    end
  end
  return true
end

hotkey.variables = function()
  print('variables')
  bindings = kb.GetBindings()
  for event, manifest in pairs(hotkeyEvents) do
    print('-', event, table.concat(manifest, ', '))
    hotkey:RegisterEvent(event)
    hotkey[event] = hotkey.UpdateFromEvent
  end
  hotkey.wrapEvent('UNIT_PET', 'pet')
  hotkey.player()
end


hotkey.init = function()

  hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)

end

hotkey.ui = function()
  hotkey.player()
  hotkey.pet()
end


hotkey.world = function()
  -- needs to be delayed so it isn't fired 50 times at login
  hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
  hotkey.UPDATE_BINDINGS = hotkey.UpdateFromEvent
  hotkey:RegisterEvent("UPDATE_BINDINGS")

  hotkey.player()
  hotkey.pet()
end

-- requires all these arguments since non-actionbar buttons don't have all of said methods
local kprint = (DEVIAN_WORKSPACE and function(...) _G.print('HotKeyUpdate', ...) end) or function() end
hotkey.UpdateHotKey = function(frame, actionType, actionID, hasAction)
  bindings = kb.GetBindings()

  if hasAction then
    local indexKey = kb.FormatActionID(actionType, actionID)
    kprint(frame:GetName(), '|cFF88FF00'..indexKey..'|r',  hasAction)


    local actionName
    if actionType == 'spell' then
      actionName = GetSpellInfo(actionID)
    elseif actionType == 'macro' then
      actionName = GetMacroInfo(actionID)
    elseif actionType == 'summonmount' then
      actionName = C_MountJournal.GetMountInfoByID(actionID)
    elseif actionType == 'item' then
      actionName = GetItemInfo(actionID)
    end
    kprint(' ', actionName)

    local binds = bindings[indexKey]
    kprint(binds)
    if binds and (not frame.HotKey:IsVisible()) then
      local bindingsText = kb.BindingString(unpack(binds))

      if not hotkeyText[frame] then
        kprint('-new hotkey element')
        hotkeyText[frame] = frame:CreateFontString(frame:GetName()..'SkeletonKey', 'OVERLAY')
        hotkeyText[frame]:SetFont(frame.HotKey:GetFont())
        hotkeyText[frame]:SetTextColor(frame.HotKey:GetTextColor())
        hotkeyText[frame]:SetPoint('TOPRIGHT', frame.HotKey, 'TOPRIGHT')

        hooksecurefunc(frame.HotKey, 'SetVertexColor', function(self, r,g,b,a)
          hotkeyText[frame]:SetTextColor(r,g,b,a)
        end)
      end

      hotkeyText[frame]:SetText(bindingsText)
      hotkeyText[frame]:Show()
      kprint('   |cFF00FFFF', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')

      return
    end
  end

  if hotkeyText[frame] then
    local oldText = hotkeyText[frame]:GetText()
    if oldText then
    hotkeyText[frame]:SetText(nil)
    print('|cFFFF4400' .. frame:GetName() .. '|r', 'removed text', oldText)
    end

  end
end

hotkey.actionbar = function()
  -- reset frames list
  print('actionbar')
  wipe(actionFrames)
  if ActionBarButtonEventsFrame.frames then
    for index, frame in ipairs(ActionBarButtonEventsFrame.frames) do
      local slot = frame.action
      local actionType, actionID = GetActionInfo(slot)
      local hasAction = HasAction(slot)

      actionSlots[slot] = frame
      if hasAction then
        local indexKey = kb.FormatActionID(actionType, actionID)

        actionFrames[indexKey] = actionFrames[indexKey] or {}
        local result = ''
        if not tContains(actionFrames[indexKey], frame) then
          tinsert(actionFrames[indexKey], frame)
          actionIndex[slot] = indexKey
          result = 'added'
        end
        print('#'..index, frame:GetName(), indexKey, result)
      end


      hotkey.UpdateHotKey(frame, actionType, actionID, hasAction)
    end
  end
end

hotkey.actionslot = function(self, event, slot)
  print(actionSlots[slot], event, slot, GetActionInfo(slot))
  local atype, aid = GetActionInfo(slot)
  local indexKey = kb.FormatActionID(atype, aid)
  local frame = actionSlots[slot]
  actionFrames[indexKey] = actionFrames[indexKey] or {}
  if not tContains(actionFrames[indexKey]) then
    tinsert(actionFrames[indexKey], frame)
    actionIndex[slot] = indexKey
  end
  if frame then
    hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
  end

end

hotkey.player = function()
  print('player')
  hotkey.actionbar()
end



hotkey.pet = function(self, event, arg1)
  if event == 'UNIT_PET' and arg1 == 'player' then
    if PetHasActionBar() and UnitIsVisible("pet") then
      hotkey.wrapEvent('PET_UI_CLOSE', 'pet')
      hotkey.wrapEvent('PET_BAR_UPDATE', 'pet')
    else
      hotkey.unwrapEvent('PET_UI_CLOSE')
      hotkey.unwrapEvent('PET_BAR_UPDATE')
      return
    end
  end

  for i=1, NUM_PET_ACTION_SLOTS, 1 do
    local button = _G['PetActionButton'.. i]
    --print(button:GetName())
    for k, v in pairs(button) do
      --print(' ', k, type(v))
    end
  end
end


--- used to pick up changes from user interaction
local changeIndex = 1
hotkey.binding = function()
  print('|cFFFF0088BindingsUpdate')
  local changeNum = #kb.ChangedBindings
  if changeNum >= changeIndex then


    for i = changeIndex, changeNum do
      local actionType, actionID, name = unpack(kb.ChangedBindings[i])
      local actionKey = kb.FormatActionID(actionType, actionID)
      local frames = actionFrames[actionKey]
      if frames then
        for i, frame in ipairs(frames) do
          print('|cFFFF0088'..actionKey..'|r', frame)
          if frame then
            hotkey.UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
          end
        end
      else
        print('no frames picked up, rebuild')
        hotkey.actionbar()
      end

      changeIndex = i + 1
    end

  end
end