diff WorldQuests.lua @ 69:31de7e9e7849

stop hiding pins when the timeleft returns 0; use C_TQ.IsActive
author Nenue
date Wed, 05 Apr 2017 11:36:06 -0400
parents 96183f981acb
children d6c0bed32c51
line wrap: on
line diff
--- a/WorldQuests.lua	Sun Apr 02 13:51:24 2017 -0400
+++ b/WorldQuests.lua	Wed Apr 05 11:36:06 2017 -0400
@@ -13,7 +13,7 @@
 local pairs, ipairs, tinsert, tremove, wipe = pairs, ipairs, tinsert, tremove, table.wipe
 local GetTaskInfo, GetTasksTable, HaveQuestData = GetTaskInfo, GetTasksTable, HaveQuestData
 local GetTime = GetTime
-local SpellCanTargetQuest = SpellCanTargetQuest
+local SpellCanTargetQuest, IsQuestIDValidSpellTarget = SpellCanTargetQuest, IsQuestIDValidSpellTarget
 local tonumber, abs = tonumber, math.abs
 local GetQuestLogRewardInfo = GetQuestLogRewardInfo
 local GetCurrentMapAreaID, GetMapInfo, GetMapNameByID = GetCurrentMapAreaID, GetMapInfo, GetMapNameByID
@@ -25,6 +25,8 @@
 local wprint = DEVIAN_WORKSPACE and function(...) _G.print('WP', ...) end or function() end
 local mprint = DEVIAN_WORKSPACE and function(...) _G.print('Canvas', ...) end or function() end
 
+local ToggleButton = {}
+local callbacks = {}
 local PinBaseIndex = 1200
 local BROKEN_ISLES_ID, DALARAN_ID, AZSUNA_ID, VALSHARAH_ID, HIGHMOUNTAIN_ID, STORMHEIM_ID, SURAMAR_ID, EOA_ID = 1007, 1014, 1015,1018, 1024, 1017, 1033, 1096
 local WORLD_QUEST_MAPS = { [DALARAN_ID] = 'Dalaran70',  [AZSUNA_ID] = 'Azsuna',  [VALSHARAH_ID] = "Val'sharah",
@@ -39,12 +41,12 @@
   0.25, 0.7, 1
 }
 
-local ToggleButton = {}
+local currentScale = WorldMapDetailFrame:GetScale()
+local canTargetQuests
 local numShown = 0
 local numLoaded = 0
-local isDataLoaded
-local numPins = 0
-local NumPinFrames = 1
+local isDataLoaded = true
+local numOverlays = 1
 local scaleConstant = 1
 Module.TasksByID = {}
 
@@ -69,42 +71,28 @@
 
 end
 
-local currentScale = WorldMapDetailFrame:GetScale()
-function Module:RefreshIfChanged()
-  local scaleCheck = WorldMapDetailFrame:GetScale()
-  if scaleCheck ~= currentScale then
-    print('|cFF00FF88scale changed from', currentScale, 'to', scaleCheck)
-    self:Refresh()
-    currentScale = scaleCheck
-  end
-end
 
 function Module:Setup()
   print('|cFFFF4400'..self:GetName()..':Setup()')
-
   for mapID, mapName in pairs(WORLD_QUEST_MAPS) do
     db.QuestsByZone[mapID] = {}
   end
-
-  hooksecurefunc("ClickWorldMapActionButton", function () self:OnClickWorldMapActionButton() end)
-  hooksecurefunc("WorldMapScrollFrame_ReanchorQuestPOIs", function()
-    print("WorldMapScrollFrame_ReanchorQuestPOIs")
-    self:RefreshIfChanged()
-  end)
-  hooksecurefunc("WorldMap_UpdateQuestBonusObjectives", function ()
-    self:OnUpdateQuestBonusObjectives()
-  end)
-  hooksecurefunc("WorldMapFrame_UpdateMap", function()
-    print("WorldMapFrame_UpdateMap")
-    self:RefreshIfChanged()
-  end)
-  WorldMapFrame.UIElementsFrame.BountyBoard:SetSelectedBountyChangedCallback(function()
-    self:OnSelectedBountyChanged()
-  end);
-  WorldMapFrame.UIElementsFrame.ActionButton:SetOnCastChangedCallback(function()
-    print("CastChangedCallback")
-    self:Refresh(true)
-  end);
+  for target, arg in pairs(callbacks) do
+    print(type(target))
+    if type(target) == 'table' then
+      local callerName = target:GetName()
+      for name, method  in pairs(arg) do
+        print(callerName, arg)
+        hooksecurefunc(target, name, function(...)
+          self:OnSecureHook(callerName .. '.' .. arg, method, ...)
+        end)
+      end
+    else
+      hooksecurefunc(target, function(...)
+        self:OnSecureHook(target, arg, ...)
+      end)
+    end
+  end
 
   self.Status = CreateFrame('Frame', nil, self)
   self.Status:SetPoint('TOPLEFT', WorldMapPOIFrame, 'TOPLEFT', 0, 0)
@@ -127,8 +115,6 @@
       translateTo = numShown * status:GetWidth()
       status.t:SetWidth(translateTo)
     end
-
-
   end)
 
   self:SetAllPoints(WorldMapPOIFrame)
@@ -137,6 +123,29 @@
   end
   self:Show()
 end
+callbacks.ClickWorldMapActionButton = function(WorldQuests)
+  WorldQuests:GetUsedPOIs()
+end
+callbacks.WorldMap_UpdateQuestBonusObjectives = function(WorldQuests)
+  WorldQuests:GetUsedPOIs()
+end
+callbacks.WorldMapFrame_UpdateMap = function(WorldQuests)
+  WorldQuests:RefreshIfChanged()
+end
+callbacks.WorldMapScrollFrame_ReanchorQuestPOIs = function (WorldQuests)
+  WorldQuests:RefreshIfChanged()
+end
+callbacks[WorldMapFrame.UIElementsFrame.BountyBoard] = {
+  SetSelectedBountyIndex = function(WorldQuests)
+    WorldQuests:UpdateBountyInfo()
+  end
+}
+callbacks[WorldMapFrame.UIElementsFrame.ActionButton] = {
+  UpdateCastingState = function(WorldQuests)
+    WorldQuests:Refresh(true)
+  end
+}
+
 
 local GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestFlaggedCompleted = GetQuestBountyInfoForMapID, GetQuestLogTitle, GetQuestLogIndexByID, IsQuestFlaggedCompleted
 function Module:UpdateBountyInfo()
@@ -181,10 +190,6 @@
   db.BountyUpdate = nil
 end
 
-function Module:OnSelectedBountyChanged()
-  self:UpdateBountyInfo()
-end
-
 function Module:OnConfigUpdate()
   print('|cFFFFFF00OnConfigUpdate()|r')
   if db.Config.FadeWhileGrouped then
@@ -217,25 +222,23 @@
   end
 end
 
-function Module:UpdateBlizzButtons()
-
-  local func = SpellCanTargetQuest() and InternalShowButton or InternalHideButton
+db.UsedPOIs = {}
+function Module:GetUsedPOIs()
+  db.canTargetQuests = SpellCanTargetQuest()
+  local func = db.canTargetQuests and InternalShowButton or InternalHideButton
+  wipe(db.UsedPOIs)
   for i = 1, NUM_WORLDMAP_TASK_POIS do
     local button = _G['WorldMapFrameTaskPOI'..i]
     if button and button.worldQuest then
       func(button, i)
+      db.UsedPOIs[button.questID] = button
     end
   end
 end
 
-function Module:OnUpdateQuestBonusObjectives()
-  rprint('|cFFFF4400WorldMap_UpdateQuestBonusObjectives')
-  self:UpdateBlizzButtons()
-end
-
-function Module:OnClickWorldMapActionButton()
-  rprint('|cFFFF4400ClickWorldMapActionButton')
-  self:UpdateBlizzButtons()
+function Module:OnSecureHook(callbackName, func, ...)
+  rprint('|cFFFF4400'..callbackName..'|r', ...)
+  func(self, ...)
 end
 
 local defaults = {}
@@ -255,6 +258,7 @@
   self:RegisterEvent('SUPER_TRACKED_QUEST_CHANGED')
   self:RegisterEvent('SKILL_LINES_CHANGED')
   self:RegisterEvent('CURRENT_SPELL_CAST_CHANGED')
+  self:RegisterEvent('ARTIFACT_UPDATE')
 end
 
 function Module:OnMapInfo(isBrokenIsle, isZoomedOut, mapAreaID, isNewMap, isMapOpen)
@@ -278,15 +282,11 @@
 function Module:OnEvent (event, ...)
 
   print('|cFFFFFF00'..self:GetName()..':OnEvent() '..event..'|r', GetTime(), ...)
-  if (event == 'QUEST_LOG_UPDATE') or (event == 'SUPER_TRACKED_QUEST_CHANGED') then
-    local questID, added = ...
-    if questID and added then
-      local questPOI = self:AcquirePin(questID)
-      questPOI:GetQuestInfo()
-      questPOI.isStale = true
+  if (event == 'QUEST_LOG_UPDATE') then
+    if self:IsVisible() then
+      self:Refresh()
+    else
       self.isStale = true
-    else
-      self:Refresh()
     end
     print('WorldMapFrame', WorldMapFrame:IsVisible(), 'hasUpdates:', self.isStale)
   elseif event == 'WORLD_QUEST_COMPLETED_BY_SPELL' then
@@ -303,9 +303,10 @@
       print('push artifact knowledge update', self.akLevel, 'to', ak)
       self.akLevel = ak
       for index, pin in pairs( db.QuestsByID) do
-        pin.dataLoaded = nil
+        if pin.rewardType == REWARD_ARTIFACT_POWER then
+          pin.dataLoaded = nil
+        end
       end
-      self:Refresh()
     end
   elseif event == 'SUPER_TRACKED_QUEST_CHANGED' then
     if superTrackedQuestID and db.QuestsByID[superTrackedQuestID] then
@@ -339,7 +340,7 @@
       --print('|cFF00FF00Re-using', pin:GetName())
     else
       totalPins = totalPins + 1
-      local name = 'WorldPlanQuestMarker' .. NumPinFrames
+      local name = 'WorldPlanQuestMarker' .. numOverlays
       --print('|cFF00FF00Creating', name)
       pin = CreateFrame('Frame', name, WorldMapPOIFrame, 'WorldPlanQuestPin')
 
@@ -348,7 +349,7 @@
         return self:GetTypeInfo(typeID)
       end
       pin:SetID(totalPins)
-      NumPinFrames = NumPinFrames + 1
+      numOverlays = numOverlays + 1
       --pin.iconBorder:SetVertexColor(0,0,0,1)
     end
     pin.questID = questID
@@ -381,7 +382,6 @@
   pin.isActive = TQ_IsActive(questID)
   pin:CheckFilterRules()
   rprint(pin:GetID(), pin.filtered, pin.used)
-  pin:SetShown(pin.used)
 
   return pin
 end
@@ -520,7 +520,14 @@
     local scaleFactor = SCALE_FACTORS[(pin.dataLoaded and not pin.filtered) and scaleConstant or 1]
     print(pin.title, pin.dataLoaded  and not pin.filtered, scaleFactor)
     pin:SetAnchor(nil, pin.x, pin.y, self.hostWidth, self.hostHeight, scaleFactor)
+
+    if pin.isNew then
+      pin:OnShow()
+    end
+
+
     pin = tremove(db.UpdatedPins)
+
   end
 end
 
@@ -607,6 +614,7 @@
     return self:MarkAllPins()
   end
   if not db.Config.EnablePins then
+    numShown = 0
     return
   end
 
@@ -618,13 +626,10 @@
     pin.used = nil
   end
 
-  if SpellCanTargetQuest() then
-    self:UpdateBlizzButtons()
-  else
-    self:UpdateAnchors(nil)
-  end
+  canTargetQuests = SpellCanTargetQuest()
+  self:UpdateAnchors()
 
-  --local showQuestPOI = db.Config.EnablePins
+  -- calculate quests shown
   numShown = 0
   numLoaded = 0
   for questID, pin in pairs(db.QuestsByID) do
@@ -644,6 +649,7 @@
       if oV == true then
         rprint('|cFFFF4400 -|r', questID, pin.title)
       end
+      pin.hideReason = "Not used in map area " .. (db.currentMapID)
       pin:HideFrames()
     end
   end
@@ -659,6 +665,18 @@
   self.isZoomDirty = nil
 end
 
+function Module:RefreshIfChanged()
+  local scaleCheck = WorldMapDetailFrame:GetScale()
+  if scaleCheck ~= currentScale then
+    print('|cFF00FF88scale changed from', currentScale, 'to', scaleCheck)
+    self:Refresh()
+    currentScale = scaleCheck
+  elseif self.isStale then
+    print('|cFF00FF88isStale flag was set')
+    self:Refresh()
+  end
+end
+
 -- update visibility states of all pins
 function Module:MarkAllPins(pins)
   print('  |cFFFFFF00'..self:GetName()..':MarkAllPins()|r', pins)
@@ -676,10 +694,8 @@
     return
   end
 
-
   --print('  |- ', pin.questID, pin.title)
   rprint('|cFF00FF00update|r', pin.questID, pin.title)
-
   if x and y then
     local scaleFactor = SCALE_FACTORS[(pin.dataLoaded and not pin.filtered) and scaleConstant or 1]
     pin:SetFrameLevel(PinBaseIndex+pin:GetID())
@@ -688,10 +704,10 @@
     --tinsert(self.UsedPositions, pin)
   end
 
-
   if self:IsVisible() and (pin.isStale) then
     pin:Refresh()
   end
+
   if mapID then
     if not db.QuestsByZone[mapID] then
       db.QuestsByZone[mapID] = {}
@@ -700,15 +716,16 @@
   end
 end
 
-function Module:UpdateMap(taskInfo, mapID)
+-- Updates quest markers in taskInfo while associating them with the given map
+function Module:UpdateQuestsForMap(taskInfo, mapID)
   rprint('Map', GetMapNameByID(mapID), GetMapNameByID(db.currentMapID))
   for index, info in pairs(taskInfo) do
     self:UpdateQuestButton(info, mapID)
   end
 end
 
+-- Used to refresh the visible quest markers
 function Module:UpdateAnchors ()
-
   wipe(self.UsedPositions)
   print('  |cFF00FF00'..self:GetName()..':UpdateAnchors()')
   self.hostWidth, self.hostHeight = WorldMapPOIFrame:GetSize()
@@ -720,13 +737,11 @@
   if isMicroDungeon then
     return
   end
-  -- starts as true
+
   isDataLoaded = true
-
-  numPins = 0
   local taskInfo = TQ_GetQuestsForPlayerByMapID(db.currentMapID)
   if taskInfo then
-    self:UpdateMap(taskInfo, db.currentMapID)
+    self:UpdateQuestsForMap(taskInfo, db.currentMapID)
   end
   local numZones = MC_GetNumZones(db.currentMapID)
   if numZones then
@@ -734,7 +749,7 @@
       local mapAreaID = MC_GetZoneInfo(db.currentMapID, i)
       local taskInfo = TQ_GetQuestsForPlayerByMapID(mapAreaID, db.currentMapID)
       if taskInfo then
-        self:UpdateMap(taskInfo, mapAreaID)
+        self:UpdateQuestsForMap(taskInfo, mapAreaID)
       end
     end
   end