view Dock.lua @ 58:0a9a6740ea5d v2.1

- Discarded use of blizzard functions never meant for the wild (http://forums.wowace.com/showthread.php?t=20397) - Fixed dock buttons not highlighting properly - Fixed dock buttons not dropping after selection change - Mouse input is disabled during player movement
author Nenue
date Tue, 12 Jan 2016 04:50:37 -0500
parents c3166f700438
children 516ceb31703d
line wrap: on
line source
--- Devian - Dock.lua
-- @file-author@
-- @project-revision@ @project-hash@
-- @file-revision@ @file-hash@
-- Created: 12/26/2015 12:32 PM
-- Docking and arrangement calls

local D = LibStub("AceAddon-3.0"):GetAddon("Devian")
local ceil, floor, sqrt, pairs, GetScreenWidth, GetScreenHeight =  math.ceil, math.floor, math.sqrt, pairs, GetScreenWidth, GetScreenHeight
local UIFrameFadeIn, UIFrameFadeOut = UIFrameFadeIn, UIFrameFadeOut
local db

--- Spaces each frame evenly across the screen.
function D:DistributeFrames() --
  local max = self.num_channels
  local num_side = ceil(sqrt(max))
  local w = GetScreenWidth() / num_side
  local h = GetScreenHeight() / num_side
  for i, frame in pairs(D.console) do
    local dx = (i-1) % num_side
    local dy = floor((i-1) / num_side)
    frame.width = w
    frame.height = h
    frame.x = dx * w
    frame.y = -(dy * h)
    frame:Save()
  end
end

--- Place all frames stacked beneath the primary frame.
function D:StackFrames()
  local last
  for _, frame in pairs(self.console) do
    if last then
      frame.x = last.x
      frame.y = last.y - 20
    else
      frame.x = (GetScreenWidth()-frame:GetWidth())/2
      frame.y = 0
    end
    frame:Save()
    last = frame
  end
end


--- Space everything and set the dock size
function D:UpdateDock()
  local pad_offset = 12
  local draw_offset = pad_offset
  for i = 1, #self.dock.buttons do
    local d = self.dock.buttons[i]
    d:SetPoint('TOPLEFT',  DevianDock, 'TOPLEFT', draw_offset, 0)
    draw_offset=  draw_offset + d:GetWidth() + pad_offset
  end
  self.dock:SetWidth(draw_offset)
end

--- Updates region visibility as needed
local getFadeInArgs = function(sign, region)
  --print('Dvn', region)
  local db = D.db
  local alph = region:GetAlpha()
  local a = (db[sign..'_alpha_on'] - alph)
  local b = (db[sign..'_alpha_on']-db[sign..'_alpha_off'])
  local dur = (a / b) * db[sign..'_fade_in']
  return dur, alph, db[sign..'_alpha_on']
end

local getFadeOutArgs = function(sign, region)
  local db = D.db
  local alph = region:GetAlpha()
  local a = (alph  - db[sign..'_alpha_off'])
  local b = (db[sign..'_alpha_on']-db[sign..'_alpha_off'])
  local dur = (a / b) * db[sign..'_fade_out']
  return dur, alph, db[sign..'_alpha_off']
end

local function queueFade (self, duration, from, to)
  self:SetAlpha(to)
end

function D.UpdateBeacon(beacon)
  local db = D.db
  local isActive = (beacon.raised or beacon.selected or beacon.newMessage)
  if isActive then
    --print(beacon:GetName(), 'active, fade in')
    queueFade(beacon, getFadeInArgs('dock_button', beacon))
  end

  if beacon.showName or isActive then
    --print(beacon:GetName(), 'show name, fade in name')
    queueFade(beacon.caption, getFadeInArgs('dock_button', beacon.caption))
  end

  if not isActive then
    queueFade(beacon, getFadeOutArgs('dock_button', beacon))
  end

  if (not beacon.showName) and (not isActive) then
    --print(beacon:GetName(), 'no name no active, fade out')
    queueFade(beacon.caption,getFadeOutArgs('dock_button', beacon.caption))
  end
end

local function FrameFade(frame)
  if not D.fader then
    D.fader = CreateFrame('Frame', 'DevianFaderFrame', UIParent):CreateAnimationGroup('fader'):CreateAnimation('Alpha', 'FadeIn')
  end
end

--- Dock interaction framescript
function D.DockHighlight(beacon)
  db = D.db
  local self = D.dock
  local mouseOverDock = false
  if self:IsMouseOver() then
    mouseOverDock = true
  end

  if beacon then
    --print(beacon:GetName(), ' highlighter got beacon')
    mouseOverDock = true
    if not beacon.raised then
      --print(beacon:GetName(), '  beacon not raised')
      beacon.raised = true
      D.UpdateBeacon(beacon)

      -- IsMouseOver can still return false during its frame's OnEnter
      beacon.inc = 0
      beacon:SetScript('OnUpdate', function(self)
        if not self:IsMouseOver() then
          self.inc = self.inc + 1
          --print(self:GetName(),self.inc)
          if self.inc > 10 then
          --print(self:GetName(), 'lost mouseover, terminating OnUpdate')
          self.raised = nil
          D.UpdateBeacon(self)
          self:SetScript('OnUpdate', nil)
            end
        end
      end)
    end
  elseif beacon.raised and beacon.index ~= db.current_channel then
    beacon.raised = nil
    D.UpdateBeacon(beacon)
    --beacon:SetScript('OnUpdate', nil)
  end

  if mouseOverDock then
    -- Raise it up
    if not self.raised then

      self.raised = true
      queueFade(self, getFadeInArgs('dock', self))
    end
  elseif  self.raised then
    -- Drop it down
    self.raised = nil
    queueFade(self, getFadeOutArgs('dock', self))
    for k, v in pairs(self.buttons) do
      v.raised = nil
      D.UpdateBeacon(v)
    end
  end

end