view SkeletonKey/KeySlot.lua @ 14:82170735e67c

- move co-routine handler to general lib - slightly better handling of pet actions
author Nenue
date Thu, 28 Jul 2016 23:58:53 -0400
parents
children 32d64e42ec9b
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 = {0.5,0.5,0.5,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()

    CURSOR_PETACTION = isPickup and slot
    print('|cFFFF4400PickupPetAction|r', isPickup, CURSOR_PETACTION)
  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 macroName, macroText
    local command, 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 not (CURSOR_SPELLSLOT and CURSOR_BOOKTYPE) then

        ClearCursor()
        ResetCursor()
      end

      local bookType, spellID = GetSpellBookItemInfo(CURSOR_SPELLSLOT, CURSOR_BOOKTYPE)
      actionID = spellID
      pickupID = CURSOR_SPELLSLOT
      pickupBook = CURSOR_BOOKTYPE
      name, _, icon = GetSpellInfo(spellID)
    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
    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 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 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

    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('|cFFFFBB00UpdateSlot|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
    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)
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 == '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

    if actionType == 'petaction' then
      self.pickupSlot = pickupSlot
      self.pickupBook = pickupBook
    else
      self.pickupSlot = nil
      self.pickupBook = nil
    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.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
}