diff ObjectiveWidgets.lua @ 13:9455693fc290

Init - recall XML display state on reload ObjectiveFrame - quest coloring by relative level - quest coloring by daily/weekly/complete status - remember starting scroll value between reload - limit anchor points to edges for regions affected by style attributes ObjectiveInfo - AutoQuest outline definitions - Pull Quest title and tag data in addition to WatchInfo ObjectiveStyle - ensure consistent style table - hardcode certain attributes for sanity XML - ensure consistent naming conventions for heading and content elements - ensure hardcore anchors are based on edges - expansion of file structure to deal with complexities of dynamic widgets and style caching ObjectiveUI - determine primary style by block handler when restoring original style - moved framescript to 'ObjectiveWidgets' lua
author Nenue
date Sat, 02 Apr 2016 17:46:52 -0400
parents
children ed642234f017
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ObjectiveWidgets.lua	Sat Apr 02 17:46:52 2016 -0400
@@ -0,0 +1,304 @@
+local B = select(2,...).frame
+local mod = B:RegisterModule("ObjectiveTracker", _G.VeneerObjectiveWrapper, 'BuffFrame')
+local print = B.print('WidgetFactory')
+local UIParent = UIParent
+local GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown = GetQuestLogSpecialItemInfo, IsQuestLogSpecialItemInRange, GetQuestLogSpecialItemCooldown
+local CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll = CooldownFrame_SetTimer, SetItemButtonTextureVertexColor, CreateFrame, VeneerObjectiveScroll
+local tremove, tinsert, tContains, pairs, setmetatable = tremove, tinsert, tContains, pairs, setmetatable
+----------------------------------------------------------------------------------------
+--- XML and script code lifted from "QuestKing 2" by Barjack,
+---   found at http://mods.curse.com/addons/wow/questking
+----------------------------------------------------------------------------------------
+local usedButtons = mod.Quest.itemButtons
+local freeButtons = mod.Quest.freeButtons
+mod.SetItemButton = function(block, info)
+  local itemInfo = info.specialItem
+  if not itemInfo then
+    return
+  end
+
+  --- Quest.GetInfo().specialItem :: {link = link, charges = charges, icon = icon, start = start, duration = duration, enable = enable}
+
+
+  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 = mod.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
+    mod.Quest.numButtons = mod.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.questLogIndex = info.questLogIndex
+  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
+mod.FreeItemButtons = function(block)
+
+  if block.itemButton then
+    local itemButton = block.itemButton
+    if itemButton.questID ~= block.info.questID then
+      block.itemButton = nil
+      itemButton.block = mod.Quest.InfoBlock[itemButton.questID]
+    else
+      itemButton.block = nil
+      itemButton:Hide()
+
+      usedButtons[itemButton.questID] = nil
+      freeButtons[#freeButtons + 1] = itemButton
+      mod.Quest.numButtons = mod.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.questLogIndex)
+      if ((not charges) or (charges ~= self.charges)) then
+        mod.UpdateWrapper()
+        return
+      end
+
+      local count = self.HotKey
+      local valid = IsQuestLogSpecialItemInRange(self.questLogIndex)
+      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.questLogIndex)
+  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
+
+-----------------------------------------
+-- Criteria frames
+
+--[[
+      text = description,
+      type = type,
+      finished = completed,
+      quantity = quantity,
+      requiredQuantity = requiredQuantity,
+      characterName = characterName,
+      flags = flags,
+      assetID = assetID,
+      quantityString = quantityString,
+      criteriaID = criteriaID,
+]]
+local newWidgetID = 0
+mod.WidgetRegistry = {}
+local wr = mod.WidgetRegistry
+
+--- Get a usable widget for the given achievement criteria set.
+-- Returns a frame object with dimensioning parameters needed to size the receiving tracker block
+mod.SetWidget = function(obj, info)
+  local print = B.print('ObjectiveWidgets')
+  local widgetType = obj.type
+  local widget
+  if wr[widgetType] and wr[widgetType].used[obj.criteriaID] then
+    widget = wr[widgetType].used[obj.criteriaID]
+    print('|cFF00FF00Updating ('..obj.criteriaID..')', widget)
+  elseif not wr[widgetType] or #wr[widgetType].free == 0 then
+    widget = CreateFrame('Frame', 'VeneerObjective' .. widgetType .. (wr[widgetType] and (wr[widgetType].lastn+1) or (1)), VeneerObjectiveScroll, 'VeneerObjectiveCriteria' .. widgetType)
+
+    print('|cFFFF0088Creating `'..widget:GetName()..'` id', wr[widgetType].lastn)
+  else
+    widget = tremove(wr[widgetType].free)
+    print('|cFFFFFF00Acquiring released widget', widget:GetName())
+  end
+
+  wr[widgetType].used[obj.criteriaID] = widget
+  widget.info = obj
+  widget.parentInfo = info
+  mod.InitializeWidget(widget)
+  return widget
+end
+
+--- WidgetTemplate 'OnLoad'
+mod.RegisterWidget = function(frame)
+  local print = B.print('ObjectiveWidgets')
+  local widgetType = frame.widgetType
+  if not wr[frame.widgetType] then
+    print('|cFFFF4400[[WidgetTemplate]]|r', widgetType)
+    wr[widgetType] = { lastn = 1, free = {}, used = {}, usedIndex = {}, freeIndex = {} }
+  else
+    print('|cFF0088FF+ [[WidgetTemplate]]r', widgetType, wr[widgetType].lastn)
+    wr[widgetType].lastn = wr[widgetType].lastn + 1
+  end
+end
+
+--- WidgetTemplate 'OnShow'
+mod.InitializeWidget = setmetatable({}, {
+  __call = function(t, frame)
+    -- todo: config pull
+    local maxWidth = 250
+
+    frame:SetWidth(maxWidth)
+    mod.UpdateWidget[frame.widgetType](frame)
+    frame:SetScript('OnEvent', mod.UpdateWidget[frame.widgetType])
+    if frame.info.isCurrency then
+      frame:RegisterEvent('CHAT_MSG_CURRENCY')
+      frame:RegisterEvent('CURRENCY_LIST_UPDATE')
+    end
+    frame:RegisterEvent('TRACKED_ACHIEVEMENT_UPDATE')
+    frame:RegisterEvent('TRACKED_ACHIEVEMENT_LIST_CHANGED')
+    frame:RegisterEvent('CRITERIA_UPDATE')
+    frame:RegisterEvent('CRITERIA_COMPLETE')
+    frame:RegisterEvent('CRITERIA_EARNED')
+
+    return t[frame.widgetType](frame)
+  end,
+})
+
+--- WidgetTemplate 'OnEvent'
+mod.UpdateWidget = setmetatable({}, {
+  __call = function(t, frame)
+    if not frame.widgetType then
+      error('Invalid widget template, needs .widgetType')
+      return
+    end
+
+    return t[frame.widgetType](frame)
+  end
+})
+
+--- WidgetTemplate 'OnHide'
+mod.ReleaseWidget = function(frame)
+  local print = B.print('ObjectiveWidgets')
+  local reg = wr[frame.widgetType]
+  if reg and reg.used[frame.info.criteriaID] then
+    reg.used[frame.info.criteriaID] = nil
+    frame.info = nil
+    frame.parentInfo = nil
+    frame:UnregisterAllEvents()
+    tinsert(reg.free, frame)
+    print('|cFFBBBBBBreleased from service', frame:GetName())
+  end
+end
+
+--- RemoveTrackedAchievement post-hook
+mod.CleanWidgets = function()
+  local print = B.print('ObjectiveWidgets')
+  local tracked = {GetTrackedAchievements() }
+  for type, reg in pairs(mod.WidgetRegistry) do
+    print('collecting', type)
+    for criteriaID, frame in pairs(reg.used) do
+      local id = frame.info.cheevID
+
+      if id and not tContains(tracked, id) then
+
+        print('  untracked achievement', id, 'associated with', criteriaID, frame:GetName())
+        frame:Hide()
+      end
+    end
+  end
+end
+
+
+mod.defaults.WidgetVars = {
+  ProgressBar = {
+    bg = {
+      Height = 20,
+    },
+    fg = {
+      Height = 16,
+    },
+    status = {
+      FontObject = _G.VeneerFontNormal
+    }
+  }
+}
+mod.InitializeWidget.ProgressBar = function(self)
+  local c = mod.defaults.WidgetVars.ProgressBar
+  local params = mod.WidgetParams[self.widgetType]
+  self.height = params.height
+  self:SetHeight(c.bg.Height)
+  self.bg:SetHeight(c.bg.Height)
+  self.fg:ClearAllPoints()
+  self.fg:SetPoint('BOTTOMLEFT', self, 'BOTTOMLEFT', 2, 2)
+  self.fg:SetHeight(c.fg.Height)
+  self.status:SetFontObject(c.status.FontObject)
+  self.status:SetText(self.info.quantityString)
+end
+
+mod.UpdateWidget.ProgressBar = function (self)
+  local quantity, requiredQuantity = self.info.quantity, self.info.requiredQuantity
+
+  if self.info.finished then
+    self.fg:SetWidth(self.bg:GetWidth() - 4)
+  elseif quantity == 0 then
+    self.fg:Hide()
+  else
+    self.fg:Show()
+    self.fg:SetWidth((self.bg:GetWidth()-4) * (quantity / requiredQuantity))
+  end
+end
+
+
+mod.InitializeWidget.Hidden = function (self)
+  self.height = 0
+end
+mod.UpdateWidget.Hidden = function (self)
+  self.height=  0
+end
\ No newline at end of file