view Modules/OrderHall.lua @ 81:65ec88b30eb8

- OrderHall plugin converted to a general world state cluster. Includes: - Suramar ancient mana - Artifact power/XP progress - OrderHall bar shrinker
author Nenue
date Mon, 17 Oct 2016 04:57:32 -0400
parents bb6b532c5d2f
children be813a9d99c0
line wrap: on
line source
-- Veneer
-- OrderHall.lua
-- Created: 10/7/2016 10:55 PM
-- %file-revision%
--

VeneerWorldStateCurrencyMixin = {}
VeneerWorldStateProgressMixin = {}
VeneerWorldStateMixin = {
  detectedFrames = {}
}
local print = DEVIAN_WORKSPACE and function(...) print('VnWorldState', ...) end or nop

function VeneerWorldStateMixin:Setup()
  self:Refresh()
  --DEFAULT_CHAT_FRAME:AddMessage('Loaded')
  print('All:Setup()')

  self.modules = self.modules or {self:GetChildren()}
  for i, frame in ipairs(self.modules) do
    if frame.Setup then
      frame:Setup()
    end
  end
end

function VeneerWorldStateMixin:Refresh()
  if OrderHallCommandBar then
    if not self.detectedFrames[OrderHallCommandBar] then
      self.detectedFrames[OrderHallCommandBar] = true
      hooksecurefunc(OrderHallCommandBar,'Show', function()
        self:Refresh()
      end)
    end


    OrderHallCommandBar:ClearAllPoints()
    OrderHallCommandBar:SetPoint('TOP')
    OrderHallCommandBar:SetWidth(600)
    OrderHallCommandBar.Background:SetColorTexture(0,0,0,0.5)
    OrderHallCommandBar.WorldMapButton:Hide()
    OrderHallCommandBar:EnableMouse(false)
  end

end
local initialized
function VeneerWorldStateMixin:OnEvent(event, arg)
  print(event, arg)

  if event == 'PLAYER_LOGIN' and not initialized then
    if IsLoggedIn() then
      initialized = true
      self:Setup()
      self:UnregisterEvent('PLAYER_LOGIN')
    end
  end

  if event == 'PLAYER_ENTERING_WORLD' then
    self:Update()
  elseif event == 'PLAYER_REGEN_ENABLED' then
    self:Show()
  elseif event == 'PLAYER_REGEN_DISABLED' then
    self:Hide()
  end
end

function VeneerWorldStateMixin:Update()
  self.modules = self.modules or {self:GetChildren()}
  print('|cFFFFFF00All:Update()|r')
  self.maxHeight = 0
  for i, frame in ipairs(self.modules) do
    if frame.Update then
      print(frame:GetName())
      frame:Update()
      if frame:IsVisible() then
        self.maxHeight = max(self.maxHeight, frame.maxHeight)
      end
    end
  end
  self:SetHeight(self.maxHeight)
end



function VeneerWorldStateMixin:OnLoad ()
  self:RegisterEvent('PLAYER_LOGIN')
  self:RegisterEvent('ADDON_LOADED')
  self:RegisterEvent('ARTIFACT_UPDATE')
  self:RegisterEvent('ARTIFACT_XP_UPDATE')
  self:RegisterEvent('PLAYER_ENTERING_WORLD')
  self:RegisterEvent('PLAYER_REGEN_ENABLED')
  self:RegisterEvent('PLAYER_REGEN_DISABLED')
end

function VeneerWorldStateCurrencyMixin:OnLoad ()

  self:RegisterEvent('ZONE_CHANGED')
  self:RegisterEvent('CURRENCY_DISPLAY_UPDATE')
  self:RegisterEvent('CHAT_MSG_CURRENCY')

end

function VeneerWorldStateCurrencyMixin:OnEvent (event, arg)
  self:Update()
end


function VeneerWorldStateCurrencyMixin:Update()

  print('currency refresh: zone =', mapID)
  if GetZoneText() == 'Suramar' then
    local name, earned, texture, earnedThisWeek, weeklyMax, totalMax = GetCurrencyInfo(1155)

    self.Icon:SetTexture(texture)
    self.Label:SetFormattedText("%d / %d", earned, totalMax)
    self:Show()
    self:SetWidth(self.Icon:GetWidth() + self.Label:GetStringWidth() + 6)
  else
    self:Hide()
  end

  self.maxHeight = self:GetHeight()
end

function VeneerWorldStateProgressMixin:OnLoad()
  self:RegisterEvent('PLAYER_XP_UPDATE')
  self:RegisterEvent('PLAYER_LEVEL_UP')
  self:RegisterEvent('PLAYER_UPDATE_RESTING')
  self:RegisterEvent('ENABLE_XP_GAIN')
  self:RegisterEvent('DISABLE_XP_GAIN')
  self:RegisterEvent('PLAYER_LOGIN')
  self:RegisterEvent('ARTIFACT_UPDATE')
  self:RegisterEvent('ARTIFACT_XP_UPDATE')

end

function VeneerWorldStateProgressMixin:Setup()
  self:UpdateXPGain()
  if UnitLevel('player') < GetMaxPlayerLevel() then
    self.mode = 'xp'
  else
    self.mode = 'artifact'
  end
  print('setup mode:', self.mode)
  self:Update()
end

function VeneerWorldStateProgressMixin:OnEvent(event)
  if event == 'ENABLE_XP_GAIN' or event == 'DISABLE_XP_GAIN' then
    self:UpdateXPGain()
  elseif event == 'ARTIFACT_XP_UPDATE' or event == 'ARTIFACT_UPDATE' then
    self.mode = 'artifact'
  elseif event == 'PLAYER_XP_UPDATE' or 'PLAYER_LEVEL_UP' then
    self.mode = 'xp'
  end


  self:Update()

end
function VeneerWorldStateProgressMixin:UpdateXPGain()
  if IsXPUserDisabled() then
    self.ProgressBar:SetColorTexture(0.75,0.75,0.75)
    self.mode = nil
  else
    self.ProgressBar:SetColorTexture(1,1,1)
  end
end

local GetEquippedArtifactInfo = _G.C_ArtifactUI.GetEquippedArtifactInfo
local GetCostForPointAtRank = _G.C_ArtifactUI.GetCostForPointAtRank
function VeneerWorldStateProgressMixin:Update()

  if not self.mode then
    if UnitLevel('player') < GetMaxPlayerLevel() then
      self.mode = 'xp'
    else
      self.mode = 'artifact'
    end
  end


  self.progressPercent = 0
  self.progressAmount = 0
  self.progressMax = 1
  self.progressOverflow = 0
  if self.mode == 'xp' then
    local xp = UnitXP('player')
    local xpMax = UnitXPMax('player')
    local bonusXP = GetXPExhaustion()
    if xp then
      self.progressPercent = xp / xpMax
    end
  elseif self.mode == 'artifact' then

    local itemID, altItemID, name, icon, totalXP, pointsSpent = GetEquippedArtifactInfo()
    if not itemID then
      self:Hide()
      return
    end

    local pointsAvailable = 0

    local nextRankCost = GetCostForPointAtRank(pointsSpent) or 0

    self.progressAmount = totalXP
    self.progressMax = nextRankCost
    if totalXP > nextRankCost then
      self.progressPercent = 1
      self.progressOverflow = totalXP - nextRankCost
    else
      self.progressPercent = totalXP / nextRankCost
    end

    self.progressText = name .. ' ('..pointsSpent .. '): '.. totalXP .. ' / ' .. nextRankCost
  else
    self:Hide()
    return
  end

  print(self.progressPercent, floor(self.ProgressBG:GetWidth()* self.progressPercent))

  if self.progressPercent > 0 then
    self.ProgressBar:Show()
    self.ProgressBar:SetPoint('TOPRIGHT', self.ProgressBG, 'TOPLEFT', self.ProgressBG:GetWidth()* self.progressPercent , 0)

    self.Label:SetText(self.progressText)
  else
    self.ProgressBar:Hide()
  end



  self.maxHeight = self:GetHeight()
end