view SkeletonKey/KeySlot.lua @ 15:32d64e42ec9b

- resolve pet bar actions for binding slots - detect type of petaction (can be spell, stance, or 'PETACTION') - keep track of displayed pet ability slots and update them alongside pet cache refreshes
author Nenue
date Fri, 29 Jul 2016 03:27:15 -0400
parents 82170735e67c
children cdd387d39137
line wrap: on
line source
-- SkeletonKey
-- KeySlot.lua
-- Created: 7/28/2016 11:26 PM
-- %file-revision%
-- All the internal slot logic is kept here

local kb, print = LibStub('LibKraken').register(KeyBinder, 'Slot')
local CURSOR_SPELLSLOT, CURSOR_BOOKTYPE, CURSOR_PETACTION
local SUMMON_RANDOM_FAVORITE_MOUNT_SPELL = 150544
local BORDER_UNASSIGNED = {0.2,0.2,0.2,1}
local BORDER_ASSIGNED = {1,1,1,1}
local BORDER_DYNAMIC = {1,1,0,1}
local BORDER_PENDING = {1,0.5,0,1 }

local BUTTON_HEADERS = {
  ['spell'] = SPELLS,
  ['macro'] = MACRO,
  ['petaction'] = PET,
  ['mount'] = MOUNT,
  ['battlepet'] = BATTLEPET,


  [5] = PROFESSIONS_FIRST_AID,
  [7] = PROFESSIONS_COOKING,
  [9] = PROFESSIONS_FISHING,
  [10] = PROFESSIONS_ARCHAEOLOGY,

}

-- This is needed to identify a spells that aren't reflected by GetCursorInfo()
hooksecurefunc("PickupSpellBookItem", function(slot, bookType)
  print('|cFFFF4400PickupSpellBookItem(..', tostring(slot),', '..tostring(bookType)..')')
  CURSOR_SPELLSLOT = slot
  CURSOR_BOOKTYPE = bookType
end)

do
-- Pet actions
  local isPickup
  hooksecurefunc("PickupPetAction", function(slot, ...)
    isPickup = GetCursorInfo()
    print(slot, ...)


    if kb.PetCache.action[slot] then
      if isPickup then
        local key = kb.PetCache.action[slot][1]
        local spellName = _G[key] or key
        if spellName and kb.PetCache.spellslot[spellName] then
          print('picked up', spellName, kb.PetCache.spellslot[spellName][1])
          CURSOR_SPELLSLOT = kb.PetCache.spellslot[spellName][1]
          CURSOR_BOOKTYPE = BOOKTYPE_PET
        end

      else
        print('Dropped pet action =', GetPetActionInfo(slot))
      end
    print('|cFFFF4400PickupPetAction|r', isPickup, CURSOR_PETACTION)
    end

    local name, subtext, texture, isToken = GetPetActionInfo(slot)
    if name then
        kb.PetCache.action[slot] = {name, subtext, texture, isToken}
    end

  end)
end


kb.DropToSlot = function(self)
  print(self:GetName(),'|cFF0088FFreceived|r')
  local actionType, actionID, subType, subData = GetCursorInfo()
  print('GetCursorInfo', GetCursorInfo())
  if actionType then

    if actionType == 'flyout' then
      ClearCursor()
      ResetCursor()
      return
    end


    local name, icon, _
    local pickupID, pickupBook

    if actionType == 'spell' then
      actionID = subData
      name, _, icon = GetSpellInfo(actionID)

    elseif actionType == 'macro' then
      name, icon = GetMacroInfo(actionID)
    elseif actionType == 'petaction' then
      if CURSOR_SPELLSLOT and CURSOR_BOOKTYPE then

        local spellType, spellID = GetSpellBookItemInfo(CURSOR_SPELLSLOT, CURSOR_BOOKTYPE)
        local spellName, spellText = GetSpellBookItemName(CURSOR_SPELLSLOT, CURSOR_BOOKTYPE)
        if spellType == 'PETACTION' then
          actionID = spellText
        else
          name, _, icon = GetSpellInfo(spellID)
          actionID = spellID
        end

        pickupID = CURSOR_SPELLSLOT
        pickupBook = CURSOR_BOOKTYPE
      else

      end

    elseif actionType == 'mount' then
      if subType == 0 then
        name, _, icon = GetSpellInfo(SUMMON_RANDOM_FAVORITE_MOUNT_SPELL)
        actionID = 0
      else
        name, _, icon = C_MountJournal.GetMountInfoByID(actionID)
      end
    elseif actionType == 'item' then
      name = GetItemInfo(actionID)
      icon = GetItemIcon(actionID)
      actionID = name
    elseif actionType == 'battlepet' then

      local speciesID, customName, level, xp, maxXp, displayID, isFavorite, petName, petIcon, petType, creatureID = C_PetJournal.GetPetInfoByPetID(detail);
      name = customName or petName
      icon = petIcon

    end
    local macroName, macroText, command = kb.RegisterAction(actionType, actionID, name)


    local isAssigned, isBound, assignedBy, boundBy = kb.IsCommandBound(self, command)
    if isAssigned then
      local popup = StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"]
      popup.slot = self
      popup.text = "Currently assigned in |cFFFFFF00"..tostring(kb.configHeaders[assignedBy]).."|r. Are you sure?"
      popup.oldProfile = assignedBy
      popup.args = {command, name, icon, actionType, actionID, macroName, macroText, pickupID, pickupBook }
      kb:SetScript('OnMouseWheel', nil) -- disable scrolling
      StaticPopup_Show('SKELETONKEY_CONFIRM_ASSIGN_SLOT')
    else
      kb.SetSlot(self, command, name, icon, actionType, actionID, macroName, macroText, pickupID, pickupBook)
      kb.UpdateSlot(self)
      self.active = nil
      ClearCursor()
      ResetCursor()
    end
  end
end


do
  local PickupAction = {
    spell = _G.PickupSpell,
    petaction = _G.PickupSpellBookItem,
    macro = _G.PickupMacro,
    item = _G.PickupItem,
    mount = _G.C_MountJournal.Pickup
  }
  local GetPickupValue = {
    spell = function(self) return select(7, GetSpellInfo(self.actionID)) end,
    petaction = function(self) return self.pickupSlot, self.pickupBook end,
  }
  kb.PickupSlot = function(self)
    if not (self.command and self.isAvailable) then
      return
    end
    print(self.actionType)
    if self.actionType == 'spell' then
      -- It can't be picked up if SpellInfo(name) returns void
      local dummy = GetSpellInfo(self.actionName)
      if not dummy then
        return
      end
    end
    if PickupAction[self.actionType] then
      if GetPickupValue[self.actionType] then
        PickupAction[self.actionType](GetPickupValue[self.actionType](self))
      else
        PickupAction[self.actionType](self.actionID)
      end
      kb.ReleaseSlot(self)
      kb.UpdateSlot(self)
    end
  end
end



--- Updates profile assignment and button contents
kb.UpdateSlot = function(self, force)
  local slot = self:GetID()

  if force then
    if kb.currentProfile.buttons[slot] then
      kb.SetSlot(self, unpack(kb.currentProfile.buttons[slot]))
    else
      kb.ReleaseSlot(self)
    end
  end

  if self.command then
    print('['..slot..'] =', self.command, GetBindingKey(self.command))

    if not self.isAvailable then
      self.border:SetColorTexture(1,0,0,1)
      self.ignoreTexture:Show()
    else
      self.ignoreTexture:Hide()

      if self.pending then
        self.border:SetColorTexture(unpack(BORDER_PENDING))
      elseif self.isDynamic then
        self.border:SetColorTexture(unpack(BORDER_DYNAMIC))
      else
        self.border:SetColorTexture(unpack(BORDER_ASSIGNED))
      end
    end


    if self.actionType == 'macro' then
      self.macro:Show()
    else
      self.macro:Hide()
      if self.actionType == 'spell' then
        local dummy = GetSpellInfo(self.actionName)
        if not dummy then
          self.icon:SetDesaturated(true)
        else
          self.icon:SetDesaturated(false)
        end

      end
    end

    if self.isDynamic then
      print('|cFF00BBFFUpdateSlot|r:', self.isDynamic, self.isAvailable, self.actionID)
    end

    if self.isDynamic == 'profession'  then
      local profText = (self.spellNum == 1) and TRADE_SKILLS or (BUTTON_HEADERS[self.profIndex] or GetProfessionInfo(self.profIndex))
      if self.isAvailable then
        print(self.profIndex, 'spnum', type(self.spellNum), (self.spellNum == 1))

        self.statusText = '|cFFFFFF00'..profText..'|r'
        self.bindingText = kb.BindingString(GetBindingKey(self.command))
      else
        self.statusText = '|cFFFF4400'..profText..'|r'
        self.actionName = '(need to train profession #'..self.profNum..')'
        self.bindingText ='?'
      end
    elseif self.isDynamic == 'talent' then

      self.statusText = '|cFF00FFFF'.. TALENT .. '|r'
      if self.isAvailable then
        self.bindingText = kb.BindingString(GetBindingKey(self.command))
      else
        if kb.inactiveTalentBindings[self.actionID] then
          print(self.actionID, #kb.inactiveTalentBindings[self.actionID])
          self.bindingText= kb.BindingString(unpack(kb.inactiveTalentBindings[self.actionID]))
        end

      end
    elseif self.isDynamic == 'petaction' and self.command:match("special") then
      self.statusText = '|cFF00FF00Pet Special|r'
      self.bindingText = kb.BindingString(GetBindingKey(self.command))
    else
      self.statusText = '|cFF00FF00'.. (BUTTON_HEADERS[self.actionType] and BUTTON_HEADERS[self.actionType] or self.actionType) .. '|r'
      self.bindingText = kb.BindingString(GetBindingKey(self.command))
    end

    local locked, layer = kb.IsCommandBound(self)
    if locked then
      self.icon:SetAlpha(0.5)
    else
      self.icon:SetAlpha(1)
    end

    if self.actionType == 'spell' then
      self.icon:SetTexture(GetSpellTexture(self.actionID))
    end
  end

  if not self.isAvailable then
    self.bind:SetTextColor(0.7,0.7,0.7,1)
  else
    self.bind:SetTextColor(1,1,1,1)
  end

  self.header:SetText(self.statusText)
  self.bind:SetText(self.bindingText)
  self.details:SetText(self.actionName)
end

--- Resets button command
kb.ReleaseSlot = function(self)
  local slot = self:GetID()


  if kb.currentProfile.buttons[slot] then
    kb.currentProfile.buttons[slot] = nil
  end
  if self.command then
    kb.currentProfile.commands[self.command] = nil
  end
  if self.actionType == 'spell' and IsTalentSpell(self.actionName) then
    if kb.currentProfile.talents[self.actionID] then
      kb.currentProfile.talents[self.actionID] = nil
    end
  end
  local droppedKeys = {}

  -- doing removal in second loop to avoid possible iterator shenanigans
  for k,v in pairs(kb.currentProfile.bindings) do
    if v == self.command then
      tinsert(droppedKeys, k)
    end
  end
  if #droppedKeys >=1 then
    for i, k in ipairs(droppedKeys) do
      kb.currentProfile.bindings[k] = nil
    end
  end

  self.isAvailable = nil
  self.isDynamic = nil
  self.bindingText = nil
  self.statusText = nil
  self.command = nil
  self.actionType = nil
  self.actionID = nil
  self.actionName = nil
  self.pickupSlot = nil
  self.pickupBook = nil
  self.macroName = nil
  self.profile = nil
  self.icon:SetTexture(nil)
  self.border:SetColorTexture(unpack(BORDER_UNASSIGNED))
  self:EnableKeyboard(false)
  self:SetScript('OnKeyDown', nil)
  self.ignoreTexture:Hide()
end

kb.SetSlot = function(self, command, name, icon, actionType, actionID, macroName, macroText, pickupSlot, pickupBook)
  local slot = self:GetID()
  local isDynamic, isAvailable

  print('|cFFFFFF00SetSlot|r:', self:GetID())
  if command then

    if actionType == 'spell' then
      local professionNum, spellNum = command:match("profession_(%d)_(%d)")

      if (professionNum and spellNum) then
        isDynamic = 'profession'
        local cacheInfo = kb.ProfessionCache[professionNum..'_'..spellNum]
        if cacheInfo then
          isAvailable = true
          name = cacheInfo.spellName
          icon = cacheInfo.icon
          actionID = cacheInfo.spellID
          self.profIndex = cacheInfo.profIndex
          self.spellOffset = cacheInfo.spellOffset
        end
        print(' Special slot: |cFF00FFFFProfession|r', professionNum, spellNum, isDynamic, isAvailable)

        self.professionNum = tonumber(professionNum)
        self.spellNum = tonumber(spellNum)

      else
        if kb.TalentCache[actionID] then
          isDynamic = 'talent'
          print(' Special slot: |cFFBBFF00talent|r', name, isAvailable)
        end

        isAvailable = GetSpellInfo(name)
      end
    elseif actionType == 'petaction' then
      isDynamic = 'petaction'
      if kb.PetCache.spellslot and kb.PetCache.spellslot[name] then
        isAvailable = true
        kb.RemoveCacheButton(kb.petFrames, self)
      else
        print('|cFFFF4400OnCacheUpdate, re-do #', slot)
        tinsert(kb.petFrames, self)
      end
    elseif actionType == 'macro' then
      if not actionID then
        actionID = GetMacroIndexByName(name)
      end
      isAvailable = true
    else
      --- Journal selections
      -- todo: consider using the deep end of blizzard action bar instead
      if not actionID then
        actionID = command:match("^KeyBinderMacro:(.+)")
      end
      isAvailable = true
    end

    if isAvailable then
      local oldCommand = command
      macroName, macroText, command = kb.RegisterAction(actionType, actionID, name)
      if oldCommand ~= command then
        print('|cFFFF4400fixing command string', actionType, actionID, name)
        kb.currentProfile.bound[oldCommand] = nil
        kb.currentProfile.bound[command] = slot
        for k,v in pairs(kb.currentProfile.bindings) do
          if v == oldCommand then
            kb.currentProfile.bindings[k] = command
          end
        end
      end
      kb.LoadBinding(command, name, icon, actionType, actionID, macroName, macroText)
    end


    actionID = actionID or 0
    self:EnableKeyboard(true)
    print(' |cFF00FF00kb.currentProfile.buttons['..slot..'] |cFF00FFFF=|r |cFF00FFFF"'.. command.. '"|r |cFF00FF00"'.. name.. '"|r |cFFFFFF00icon:'.. icon .. '|r |cFFFF8800"'.. actionType, '"|r |cFFFF0088id:'.. actionID ..'|r |cFF00FF00"'.. macroName .. '"|r')
    kb.currentProfile.buttons[slot] = {command, name, icon, actionType, actionID, macroName, macroText, pickupSlot, pickupBook}

    -- Clean up conflicting entries for loaded button
    local previous = kb.currentProfile.commands[command]
    if previous ~= slot and kb.buttons[previous] then
      kb.ReleaseSlot(kb.buttons[previous])
    end
    kb.currentProfile.commands[command] = slot
  end

  self.isAvailable = isAvailable
  self.isDynamic = isDynamic

  self.pickupSlot = pickupSlot
  self.pickupBook = pickupBook
  self.macroText = macroText
  self.macroName = macroName
  self.actionType = actionType
  self.actionID = actionID
  self.actionName = name
  self.command = command
  self.icon:SetTexture(icon)
  self.profile = kb.db.bindMode
  self:RegisterForDrag('LeftButton')
end



--- Add to blizzard interfaces
StaticPopupDialogs["SKELETONKEY_CONFIRM_ASSIGN_SLOT"] = {
  text = "Confirm moving an assigned command.",
  button1 = OKAY,
  button2 = CANCEL,
  timeout = 0,
  whileDead = 1,
  showAlert = 1,
  OnAccept = kb.AcceptAssignment,
  OnCancel = function() kb:SetScript('OnMouseWheel', KeyBinder_OnMouseWheel) end
}