view SkeletonUnit/UnitFrame.lua @ 1:cd7d06bcd98d

KeyBinds: set hotkey text for blizzard action buttons UnitFrame: prototype templates for the majority of units
author Nenue
date Tue, 21 Jun 2016 04:47:52 -0400
parents 69e828f4238a
children 07293831dd7b
line wrap: on
line source
--------------------------------------------
-- KrakTool
-- Nick
-- @project - r e v i s i o n @ @project-hash@
-- @file - r e v i s i o n @ @file-hash@
-- Created: 6/16/2016 3:46 AM
--------------------------------------------

local KT = LibKT.register(SkeletonUnits)
local PLAYER_NAMEPLATE
local PLAYER_WIDTH = 220
local BUFF_SIZE = 24
local sk = SkeletonUnits
local player = KTPlayerFrame
local prototypes = {
  player = {},
  target = {},
  pet = {},
  focus = {}
}
local units = {}
local buttons = {}
local params = {
  player = {width = 220, height = 30},
  pet = {height = 25}
}
sk.handler = sk -- so sk.event can work


local UpdateUnitAnchor = function(self)
  self.nameplate = C_NamePlate.GetNamePlateForUnit(self.unit)
  if self.nameplate and self.nameplate.namePlateUnitToken then
    self:ClearAllPoints()
    self:SetPoint('TOP', self.nameplate, 'BOTTOM', 0, 0)
    print('snapping', self.unit, 'to', self.nameplate.namePlateUnitToken)
  else
    self:ClearAllPoints()
    self:SetPoint(unpack(self.anchorPoint))
  end
end

local SetupButton = function(self, unit, index)
  if not buttons[unit][index] then
    buttons[unit][index] = CreateFrame('Frame', 'KT'..unit..'Buff'..index, self, 'KTAuraButton')
    buttons[unit][index]:SetSize(BUFF_SIZE, BUFF_SIZE)
    buttons[unit][index].cooldown:SetHideCountdownNumbers(true)

  end
  return buttons[unit][index]
end

sk.init = function()
  for unit, handler in pairs(prototypes) do
    if not _G['KT'..unit..'Frame'] then
      CreateFrame('Frame', 'KT'.. unit .. 'Frame', sk)
    end
    local frame = _G['KT'..unit..'Frame']
    frame.unit = unit
    frame.anchorPoint = {frame:GetPoint(1)}
    frame.handler = handler
    if handler.init then
      handler.init(frame, unit)
    end
    buttons[unit] = {}
    units[unit] = frame
  end

  sk:RegisterEvent('PLAYER_TARGET_CHANGED')
  sk:RegisterUnitEvent("UNIT_AURA")
end
sk.variables = function()
  for unit, frame in pairs(units) do
    if frame.handler.variables then
      frame.handler.variables(frame, unit)
    end
    local width = params.player.width
    local height = params.player.height
    if params[unit] then
      if params[unit].width then
      width = params[unit].width
      end
      if params[unit].height then
        height = params[unit].height
      end
    end

    frame:SetWidth(width)
    frame:SetHeight(height)
  end
end

sk.event = function(self, event, ...)
  if self.handler[event] then
    self.handler[event](self, event, ...)
  end
end

sk.PLAYER_TARGET_CHANGED = function()
  prototypes.player.refresh(units.target)
end

prototypes.player.init = function(self)
  self:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", self.unit)
  self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit)
  self:RegisterEvent("NAME_PLATE_UNIT_ADDED")
  self:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
  self:RegisterEvent("UNIT_TARGET")
  self:SetScript('OnEvent', sk.event)
end

--- Runs once
prototypes.player.variables = function(self)
  self.handler.refresh(self)
end

--- Runs when event handler decides so
prototypes.player.refresh = function(self)
  if not UnitExists(self.unit) then
    self:Hide()
    return
  end

  local class, classFile = UnitClass(self.unit)
  if classFile then
    self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b)
  end

  self.handler.UNIT_HEALTH_FREQUENT(self)
  self.handler.UNIT_POWER_FREQUENT(self)
end

sk.UNIT_AURA = function(self, event, unit)
  if not units[unit] then return end

  local buffOffset = 0
  local buttons = buttons[unit]
  for i = 1, 16 do

    local aura, _, texture, count, dispelType, duration, expires, caster = UnitAura(unit, i, 'HARMFUL')
    if aura then
      local button = SetupButton(units[unit], unit, i)

      button.icon:SetTexture(texture)
      button.cooldown:SetCooldown(expires - duration, duration)
      button.cooldown:Show()
      button.count:SetText(count > 0 and count or nil)
      button:SetPoint('BOTTOMLEFT', units[unit], 'TOPLEFT', buffOffset* BUFF_SIZE, 2)
      button:Show()
      buffOffset = buffOffset + 1
    else
      if buttons[i] then
        buttons[i]:Hide()
      end
    end
  end
end

prototypes.player.NAME_PLATE_UNIT_ADDED = function(self, event, nameplate)
    UpdateUnitAnchor(self)
end
prototypes.player.NAME_PLATE_UNIT_REMOVED = function(self, event, nameplate)
    UpdateUnitAnchor(self)
end

prototypes.player.UNIT_HEALTH_FREQUENT = function(self, ...)
  if UnitHealthMax(self.unit) > 0 then
    self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit))
    self.healthtext:SetText(UnitHealth(self.unit))
  else
    self.healthbar:SetWidth(PLAYER_WIDTH)
    self.healthtext:SetText(nil)
  end
  return true
end

prototypes.player.UNIT_POWER_FREQUENT = function(self)
  if UnitPowerMax(self.unit) > 0 then
    self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit))
    self.powertext:SetText(UnitPower(self.unit))
  else
    self.powerbar:Hide()
    self.powertext:SetText(nil)
  end
  return true
end


prototypes.player.UNIT_TARGET = function(self, ...)
  if not UnitExists(self.unit) then
    self:Hide()
  else
    self:Show()
    self.handler.refresh(self)
    UpdateUnitAnchor(self)
  end
end

prototypes.pet = prototypes.player
prototypes.target = prototypes.player