view HotKey.lua @ 92:c1c9dd6063a8

Added tag v8.0.1b for changeset 5005aecc2dc8
author Nenue
date Tue, 17 Jul 2018 20:48:19 -0400
parents c48913c5924c
children
line wrap: on
line source
-- KrakTool
-- HotKey.lua
-- Created: 7/22/2016 10:28 PM
-- %file-revision%
-- Module for fixing actionbar hotkey text

SkeletonHotKeyMixin = {}
local _G, wipe, tContains, tinsert = _G, table.wipe, tContains, tinsert
local hotkeyText = {}
local blizHotKey = {}
local bindings

local _, kb = ...
local print = (DEVIAN_PNAME == 'SkeletonKey') and function(...) _G.print('HotKey', ...) end or nop
local cprint = (DEVIAN_PNAME == 'SkeletonKey') and function(...) _G.print('Cfg', ...) end or nop

-- 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 = {}
local hotkey = SkeletonHotKeyMixin

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


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

function hotkey:unwrapEvent (event)
  if not self[event] then
    self:UnregisterEvent(event)
  end
  hotkeyEvents[event] = nil
  self[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

function hotkey:OnEvent (event, ...)
  print('|cFFFF8888'..self:GetName()..':OnEvent()|r', event, ...)
  if hotkeyEvents[event] then
    for i, func  in ipairs(hotkeyEvents[event]) do

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

function hotkey:Setup ()
  print('variables')
  bindings = kb.GetBindings()
  self:player()
  for event, triggers in pairs(hotkeyEvents) do
    print('setup', event, triggers)
    self:RegisterEvent(event)
  end

end

function hotkey:OnLoad()
  hooksecurefunc("ActionBarButtonEventsFrame_RegisterFrame", hotkey.RegisterFrame)

end


function hotkey:Update()
  print('--|cFF00FF00ui|r')
  self:player()
  hotkey:pet()
  hotkey:binding()
  kb.UpdateHotKeys = function() self:Update() end
end

function hotkey:world()
  print('--|cFF00FF00world|r')
  -- needs to be delayed so it isn't fired 50 times at login
  if not hotkeyEvents["UPDATE_BINDINGS"] then
    hotkeyEvents["UPDATE_BINDINGS"] = {"binding"}
    self.UPDATE_BINDINGS = self.UpdateFromEvent
    self:RegisterEvent("UPDATE_BINDINGS")
  end

  self:player()
  self: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
function hotkey:UpdateHotKey(frame, actionType, actionID, hasAction)


  if hasAction then
    local indexKey = kb.FormatActionID(actionType, actionID)



    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('  actionKey:', indexKey)

    local binds = kb.bindings[indexKey]
    if binds and (not frame.HotKey:IsVisible()) then
      kprint(frame:GetName(), '|cFF88FF00'..indexKey..'|r',  hasAction, actionName)
      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

      if actionType == 'macro' then
        local name, rank, spellID = GetMacroSpell(actionID)
        if spellID and bindings['spell_'..spellID] then
          bindingsText = kb.BindingString(unpack(bindings['spell_'..spellID]))
        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

local foundSlots = {}
function hotkey:actionbar()
  print('--|cFF00FF00actionbar|r')
  wipe(actionFrames)
  wipe(foundSlots)


  bindings = kb.GetBindings()
  for k,v in pairs(bindings) do
    print(k, #v)
  end


  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 {}
        if not tContains(actionFrames[indexKey], frame) then
          tinsert(actionFrames[indexKey], frame)
          actionIndex[slot] = indexKey
        end
        local actionName = ''
        if actionType == 'macro' then
          actionName = GetMacroInfo(actionID)
          elseif actionType == 'spell' then
          actionName = GetSpellInfo(actionID)
        end

        if bindings[indexKey] then
          print(indexKey, actionName)
        for k,v in pairs(bindings[indexKey]) do
          print(k,v)
        end
        end


       --tinsert(foundSlots, '|cFFFFFF00#'..index .. '|r ' .. tostring(indexKey).. '('..(bindings[indexKey] and table.concat(bindings[indexKey],' ') or '-')..')')
      end


      self:UpdateHotKey(frame, actionType, actionID, hasAction)
    end
  end

  --print('found slots: ', table.concat(foundSlots, ', '))
end

function hotkey:actionslot (event, slot)
  local actionType, actionID = GetActionInfo(slot)
  print(actionSlots[slot], event, actionType, actionID, HasAction(slot))
  local frame = actionSlots[slot]
  if frame then
    self:UpdateHotKey(frame, actionType, actionID, HasAction(slot))
  end

end

function hotkey:player()
  print('player')
  self:actionbar()
end



function hotkey:pet(event, arg1)
  print('--|cFF00FF00pet|r')
  if event == 'UNIT_PET' and arg1 == 'player' then
    if PetHasActionBar() and UnitIsVisible("pet") then
      self:wrapEvent('PET_UI_CLOSE', 'pet')
      self:wrapEvent('PET_BAR_UPDATE', 'pet')
    else
      self:unwrapEvent('PET_UI_CLOSE')
      self: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
function hotkey:binding ()
  local changeNum = #kb.ChangedBindings
  cprint('--|cFF00FF00binding|r')
  if changeNum >= changeIndex then


    for i = changeIndex, changeNum do
      cprint(changeIndex,'of', changeNum)
      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
          cprint('|cFFFF0088'..actionKey..'|r', frame)
          if frame then
            self:UpdateHotKey(frame , actionType, actionID, HasAction(frame.action))
          end
        end
      else
        cprint('no frames picked up, rebuild')
        self:actionbar()
      end

      changeIndex = i + 1
    end

  end
end