view ObjectiveTracker/Widgets.lua @ 40:03ed70f846de

- move block accessors into a new file - define a tMove function for reconciling the free/used tables as needed - when retrieving an old block frame, confirm ID still matches; resolves multiple watch items on one block - stop any animations when a block is freed; resolves stuck flare graphics
author Nenue
date Sun, 24 Apr 2016 14:15:25 -0400
parents 92534dc793f2
children 9480bd904f4c
line wrap: on
line source
local B = select(2,...).frame
local T = B:RegisterModule("ObjectiveTracker", _G.VeneerObjectiveWrapper, 'BuffFrame')
local print = B.print('WidgetFactory')
local _G, UIParent = _G, UIParent
local GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown = GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown
local CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll = CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll
local tremove, tContains, pairs, ipairs, setmetatable, floor = tremove, tContains, pairs, ipairs, setmetatable, floor
local SetItemButtonTexture, SetItemButtonCount = SetItemButtonTexture, SetItemButtonCount
local ToggleWorldMap, GetTrackedAchievements, GetTasksTable = ToggleWorldMap, GetTrackedAchievements, GetTasksTable
-- GLOBALS: Veneer_QuestObjectiveItem_UpdateCooldown, Veneer_QuestObjectiveItem_OnUpdate
--- frame refs
local Wrapper = _G.VeneerObjectiveWrapper
local Scroller = Wrapper.scrollArea
local CloseButton = Wrapper.CloseButton
local QuestMapButton = Wrapper.QuestMapButton
local Scroll = _G.VeneerObjectiveScroll
local usedButtons = T.Quest.itemButtons
local freeButtons = T.Quest.freeButtons

T.buttons = {
  CloseButton = {
    closedSwatch = {
      [[Interface\Buttons\UI-Panel-QuestHideButton]],
      [[Interface\Buttons\UI-Panel-QuestHideButton]],
      0, 0.5, 0.5, 1,
      0.5, 1, 0.5, 1,
    },
    openSwatch = {
      [[Interface\Buttons\UI-Panel-QuestHideButton]],
      [[Interface\Buttons\UI-Panel-QuestHideButton]],
      0.5, 1, 0.5, 1,
      0, 0.5, 0.5, 1,
    },
    parent = 'VeneerObjectiveWrapper'
  },
  QuestMapButton = {
    closedSwatch = {
      [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]],
      [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]],
      0, 1, 0.5, 1,
      0, 1, 0, 0.5,
    },
    openSwatch = {
      [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]],
      [[Interface\QUESTFRAME\UI-QUESTMAP_BUTTON]],
      0, 1, 0, 0.5,
      0, 1, 0.5, 1,
    }
  }
}

local Scroller_OnShow = function()
  Wrapper.watchMoneyReasons = 0;
  --T:Update()
  --T:OnInitialize()
  for i, region in ipairs(Wrapper.headerComplex) do
    region:Show()
  end
end

local Scroller_OnHide = function()
  local self = Wrapper
  Wrapper:UnregisterAllEvents()
  Wrapper:SetScript('OnEvent', nil)
  for i, region in ipairs(Wrapper.headerComplex) do
    region:Hide()
  end
end

local Scroller_OnMouseWheel = function(self, delta)
  local r = Scroll:GetHeight() - Scroller:GetHeight()
  local s = B.Conf.ObjectiveScroll - delta * floor(r/5+.5)
  local from = self:GetVerticalScroll()
  print('|cFF00FF00OnMouseWheel', 'scroll =', s)
  if s >= r then
    s = r
  elseif s < 1 then
    s = 0
  end
  self:SetVerticalScroll(s)
  B.Conf.ObjectiveScroll = s
  print('|cFF00FF00OnMouseWheel', 'from = ', from, 'scroll =', s, ' range =', r, 'current =', self:GetVerticalScroll())

  T.UpdateActionButtons('SCROLLING')
end

local UpdatePanelButton = function (self, state)
  state = state or true
  local swatch = (state == true) and self.openSwatch or self.closedSwatch
  print((state ~= true) and 'closedSwatch' or 'openSwatch')
  self:SetNormalTexture(swatch[1])
  self:SetPushedTexture(swatch[2])
  if #swatch >= 6 then
    self:GetNormalTexture():SetTexCoord(swatch[3], swatch[4], swatch[5], swatch[6])
  end
  if #swatch == 10 then
    self:GetPushedTexture():SetTexCoord(swatch[7], swatch[8], swatch[9], swatch[10])
  end

end

local OnClick = {}
OnClick.CloseButton = function(self)
  T:Enable()
  if T.Conf.enabled then
    T:Update()
  end

  UpdatePanelButton(self, T.Conf.enabled)
end

OnClick.QuestMapButton = function()
  ToggleWorldMap()
end


T.InitializeWidgets = function()
  local panelButtons = T.buttons
  --- tracker scroll
  Scroller:SetScript('OnMouseWheel', Scroller_OnMouseWheel)
  Scroller:SetScript('OnShow', Scroller_OnShow)
  Scroller:SetScript('OnHide', Scroller_OnHide)
  for name, swatch in pairs(panelButtons) do
    local source = swatch and swatch or panelButtons.CloseButton
    local button = Wrapper[name]
    button.parent = swatch.parent
    button.openSwatch = source.openSwatch
    button.closedSwatch = source.closedSwatch
    if OnClick[name] then
      button:SetScript('OnClick', OnClick[name])
    end
    UpdatePanelButton(button, T.Conf.enabled)
  end
end

----------------------------------------------------------------------------------------
--- modified version of the itemButton initializer used by Barjack's 'QuestKing 2' addon,
--- url: http://mods.curse.com/addons/wow/questking
----------------------------------------------------------------------------------------
T.SetItemButton = function(block, info)
  local itemInfo = info.specialItem
  if not itemInfo then
    return
  end

  local itemButton
  if not info.itemButton then
    if #freeButtons >= 1 then
      print('    |cFF00FFFFfound a free button')
      itemButton = freeButtons[#freeButtons]
      freeButtons[#freeButtons] = nil
      if itemButton.block then
        itemButton.block.itemButton = nil
        itemButton.block = nil
      end
    else
      local buttonIndex = T.Quest.numButtons + #freeButtons + 1
      itemButton = CreateFrame('Button', 'VeneerQuestItemButton' .. buttonIndex, UIParent, 'VeneerItemButtonTemplate')
      itemButton.buttonIndex = buttonIndex
      itemButton:SetSize(36, 36)
      itemButton:GetNormalTexture():SetSize(36 * (5/3), 36 * (5/3))
      print('    |cFFFF4400starting new button', itemButton:GetName())
    end
    T.Quest.numButtons = T.Quest.numButtons + 1
  else
    itemButton = info.itemButton
    print('    |cFF00FF00found assigned button', itemButton:GetName())

  end
  -- set values

  info.itemButton = itemButton
  usedButtons[info.questID] = itemButton
  print('      |cFF8800FFassigning|r', itemButton:GetName(), 'to quest|cFF00FF00', info.questID, '|rat|cFFFFFF00', block:GetName(),'|r')

  for k,v in pairs(usedButtons) do
    print('|cFFFF44DD'..k..'|r', v:GetName())
  end

  itemButton:SetAttribute("type", "item")
  itemButton:SetAttribute("item", itemInfo.link)

  itemButton.questID = info.questID
  itemButton.logIndex = info.logIndex
  itemButton.charges = itemInfo.charges
  itemButton.rangeTimer = -1
  itemButton.block = block

  SetItemButtonTexture(itemButton, itemInfo.icon)
  SetItemButtonCount(itemButton, itemInfo.charges)
  Veneer_QuestObjectiveItem_UpdateCooldown(itemButton);

  return itemButton
end
--- Clear an itemButton from the given block
T.FreeItemButtons = function(block)

  if block.itemButton then
    local itemButton = block.itemButton
    if itemButton.questID ~= block.info.questID then
      block.itemButton = nil
      itemButton.block = T.Quest.InfoBlock[itemButton.questID]
    else
      itemButton.block = nil
      itemButton:Hide()

      usedButtons[itemButton.questID] = nil
      freeButtons[#freeButtons + 1] = itemButton
      T.Quest.numButtons = T.Quest.numButtons - 1
      print('|cFFFF0088released', itemButton:GetName(),'and', block:GetName())
    end
  end
end

function Veneer_QuestObjectiveItem_OnUpdate (self, elapsed)
  -- Handle range indicator
  local rangeTimer = self.rangeTimer
  if (rangeTimer) then
    rangeTimer = rangeTimer - elapsed
    if (rangeTimer <= 0) then
      local link, item, charges, showItemWhenComplete = GetQuestLogSpecialItemInfo(self.logIndex)
      if ((not charges) or (charges ~= self.charges)) then
        T:Update()
        return
      end

      local count = self.HotKey
      local valid = IsQuestLogSpecialItemInRange(self.logIndex)
      if (valid == 0) then
        count:Show()
        count:SetVertexColor(1.0, 0.1, 0.1)
      elseif (valid == 1) then
        count:Show()
        count:SetVertexColor(0.6, 0.6, 0.6)
      else
        count:Hide()
      end
      rangeTimer = TOOLTIP_UPDATE_TIME
    end

    self.rangeTimer = rangeTimer
  end
end

function Veneer_QuestObjectiveItem_UpdateCooldown (itemButton)
  local start, duration, enable = GetQuestLogSpecialItemCooldown(itemButton.logIndex)
  if (start) then
    CooldownFrame_SetTimer(itemButton.Cooldown, start, duration, enable)
    if (duration > 0 and enable == 0) then
      SetItemButtonTextureVertexColor(itemButton, 0.4, 0.4, 0.4)
    else
      SetItemButtonTextureVertexColor(itemButton, 1, 1, 1)
    end
  end
end