view SkeletonKey/HotKey.lua @ 54:a545933ddf3d

- pet ability binding macro now contains a /cast line for all known abilities with the same subtext, fixing an issue where swapping pets during combat breaks the key binding
author Nenue
date Sun, 21 Aug 2016 03:13:32 -0400
parents 1aba8a6fd4a9
children 9eebce04e69b
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 kb, print, wrap = LibStub("LibKraken").register(KeyBinder, 'HotKey')
local 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 = {}

kb.wrap(hotkey)

--- 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"},
}


hotkey.wrapEvent = function(event, ...)
  kb: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(' ', func)
        hotkey[func](self, event, ...)
      end
    end
  end
  return true
end

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


hotkey.init = function()
  hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)
end

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

hotkey.world = function()
  hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
  hotkey.UPDATE_BINDINGS = hotkey.UpdateFromEvent
  kb:RegisterEvent("UPDATE_BINDINGS")

  hotkey.player()
  hotkey.pet()

end

-- requires all these arguments since non-actionbar buttons don't have all of said methods
hotkey.UpdateHotKey = function(frame, actionType, actionID, hasAction)
  bindings = kb.GetBindings()

  local indexKey = kb.FormatActionID(actionType, actionID)
  print('|cFF00FFFFUpdate|r', frame, indexKey, hasAction)


  if bindings[indexKey] then
    --print('|cFFFFFF00'..frame:GetName(), actionType, actionID, hasAction)
    local binds = bindings[indexKey]
    if binds  then
      if hasAction then
        local bindingsText = kb.BindingString(unpack(binds))

        if not hotkeyText[frame] then
          print('-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')
        end

        hotkeyText[frame]:SetText(bindingsText)
        hotkeyText[frame]:Show()
        print('|cFF00FFFFUpdate text for', frame:GetName(), '|cFFFFFF00'..tostring(bindingsText)..'|r')

        return
      end
    end
  end

  if hotkeyText[frame] then
    hotkeyText[frame]:SetText(nil)
    --print('|cFFFF4400cleared text from', frame:GetName())
  end
end

hotkey.actionbar = function()
  wipe(actionFrames)
  -- reset frames list

  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)
      local indexKey = kb.FormatActionID(actionType, actionID)
      actionSlots[slot] = frame
      actionFrames[indexKey] = actionFrames[indexKey] or {}
      if not tContains(actionFrames[indexKey]) then
        tinsert(actionFrames[indexKey], frame)
        actionIndex[slot] = indexKey
      end

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

hotkey.actionslot = function(self, event, slot)
  print(actionSlots[slot], event, slot, GetActionInfo(slot))
  --print(GetActionButtonForID(slot))
  local atype, aid = GetActionInfo(slot)
  local indexKey = kb.FormatActionID(atype, aid)
  local frame = actionSlots[slot]
  if frame then
    hotkey.UpdateHotKey(frame, atype, aid, HasAction(slot))
  end

end

hotkey.player = function()
  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