view Dock.lua @ 98:33bc8baba858

start of a lot of v3 groundwork based on better knowledge of the addon interface: - use of mixin as a lexical center for generated frames - removal of unfinished segments
author Nenue
date Wed, 26 Oct 2016 10:17:43 -0400
parents f6fae1a4c66c
children 7d94df3804a7
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 = ...
local ceil, floor, sqrt, pairs, GetScreenWidth, GetScreenHeight =  math.ceil, math.floor, math.sqrt, pairs, GetScreenWidth, GetScreenHeight
local db
local print = DEVIAN_WORKSPACE and function(...) print('DvnDock', ...) end or nop
local DOCK_BUTTON_PADDING = 6

DevianDockHandler = {
  usedButtons = {},
  buttons = {},
}
DevianDockButtonMixin = {}

--- 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

local numBeacons = 0
function DevianDockHandler:GetDockButton(console)
  self.usedButtons = self.usedButtons or {}
  local index = console:GetID()
  local button = self.usedButtons[index]
  if not button then
    numBeacons = numBeacons + 1
    button = CreateFrame('Button', 'DevianDockBeacon'.. numBeacons, UIParent, 'DevianBeacon')
    button.color = {r = math.random(), g = math.random(), b = math.random()}
    button.Stripe:SetColorTexture(button.color.r, button.color.g, button.color.b,1)
    button.console = console
    self.usedButtons[index] = button
    tinsert(self.buttons, button)
    --oldprint('create dock', index, console.signature)
  end
  button.index = console.index
  button.caption.name:SetText(console.signature)
  button:SetShown(true)
  return button
end

function DevianDockButtonMixin:OnMouseDown(button)
  --print("click", self:GetName(), button, self.console.index)
  if button == "LeftButton" then
    if IsShiftKeyDown() then
      self.console:Toggle()
    else
      if self.console.isFront or (not self.console.enabled) then

        self.console:Toggle()
        if self.console.enabled then
          if self.console.minimized then
            self.console:MinMax()
          end
          self.console:ToFront()
        end
      else
        self.console:ToFront()
      end
    end
  elseif button == "RightButton" then
    self.console:MinMax()
  end
end
function DevianDockButtonMixin:OnShow()
  self:Update()
end
function DevianDockButtonMixin:OnEnter()
end
function DevianDockButtonMixin:Update()
  local db = D.db
  local isActive = (self.raised or self.selected or self.newMessage)

  if (self.showName or isActive) then
    self.caption:SetAlpha(1)
  else
    self.caption:SetAlpha(0.5)
  end

  if self.selected then
    self.Background:SetColorTexture(0.4,0.4,0.4,1)
  else
    self.Background:SetColorTexture(0,0,0,.5)
  end

  if (not self.showName) and (not isActive) then
    --print(self:GetName(), 'no name no active, fade out')
    self:SetAlpha(0.5)
  else
    self:SetAlpha(1)
  end

  self:SetWidth(self.caption.name:GetStringWidth() + DOCK_BUTTON_PADDING)
end

function DevianDockButtonMixin:Select()
  self.caption.pulse:Stop()
  self:Update()
end

--- 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

function DevianDockHandler:OnMouseWheel(delta)
  if delta >= 1 then
    self.dockScale = (self.dockScale or 1) - 0.1
  else
    self.dockScale = (self.dockScale or 1) + 0.1
  end
  self:Update()
end

--- Space everything and set the dock size
function DevianDockHandler:Update()
  local pad_offset = 12
  local drawWidth = 0
  local lastButton
  local numButtons = 0
  for i, d in ipairs(self.buttons) do
    if d and d:IsShown() then
      d:SetScale(D.dockScale or 1)
      if lastButton then
        d:SetPoint('TOPLEFT', lastButton, 'TOPRIGHT', pad_offset, 0)
      else
        d:SetPoint('TOPLEFT',  DevianDock, 'TOPLEFT', pad_offset, 0)
      end

      drawWidth =  drawWidth + d:GetWidth() + pad_offset
      lastButton = d
      numButtons = numButtons + 1
      print(numButtons)
    end
  end
  self.numButtons = numButtons
  self:SetWidth(drawWidth)

  D.db.dockPoint = D.db.dockPoint or 'TOPLEFT'
  self:SetPoint(D.db.dockPoint , UIParent, D.db.dockPoint , 0, 0)
end


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