view Dock.lua @ 32:c6a2c2df4790

v2 work
author Nenue
date Sat, 26 Dec 2015 21:51:57 -0500
parents
children e6650821a2c0
line wrap: on
line source
--- ${PACKAGE_NAME}
-- @file-author@
-- @project-revision@ @project-hash@
-- @file-revision@ @file-hash@
-- Created: 12/26/2015 12:32 PM
-- Dock management functions

local D = LibStub("AceAddon-3.0"):GetAddon("Devian")
local _G = _G
local db = DevianDB


local function Dock_MouseDown(self, button, up)
  local parent = self.dockedTo and D.console[self.dockedTo] or self
  local docked = parent.docked
  if docked == nil then
    docked = {}
  end
  local worklist = {}
  for _, i in pairs(docked) do
    table.insert(worklist, D.console[i])
  end

  if not up then
    if button ~= 'RightButton' then
      if parent.titlebar:IsMouseOver() or not parent.isDocked then
        parent:ToFront()
      else
        parent.out:Hide()
      end

      if parent.out.grip:IsMouseOver() then
        parent:StartSizing()
      else
        parent:StartMoving()
      end
    else
      parent:MinMax()
    end

  else
    if button ~= 'RightButton' then
      if parent.titlebar:IsMouseOver() then
        parent.out:Show()
      end
      parent:StopMovingOrSizing()
    end
  end

  local fixparent = true
  for _, frame in pairs(worklist) do
    if not up then
      if button ~= 'RightButton' then
        if frame.titlebar:IsMouseOver() then
          frame:ToFront()
          fixparent = nil
        else
          frame.out:Hide()
        end
        if parent.out.grip:IsMouseOver() then
          frame:StartSizing()
        else
          frame:StartMoving()
        end
      end
    else
      if button ~= 'RightButton' then
        frame:StopMovingOrSizing()
        frame:SetPoint('TOPLEFT', parent, 'TOPLEFT')
        frame:SetPoint('BOTTOMRIGHT', parent, 'BOTTOMRIGHT')
        frame.x = nil
        frame.y = nil
        frame.width = nil
        frame.height = nil
        frame:Save()

      end
    end
  end

  if fixparent then
    parent.out:Show()
  end
end
local function Dock_MouseUp (self, button)
  Dock_MouseDown(self, button, true)
end

--- Adjusts frame element alignments
local function PrepareForDock (frame, draw_x)
  frame.left:SetPoint('TOPLEFT', frame.out, 'TOPLEFT', -2, 0)
  frame.left:SetPoint('BOTTOMRIGHT', frame.out, 'BOTTOMLEFT', 0, 0)
  frame.topleft:SetPoint('BOTTOMRIGHT', frame.out, 'TOPLEFT', 0, 0)
  frame.top: SetPoint('TOPLEFT', frame.out, 'TOPLEFT', 0, 2)
  frame.top:SetPoint('BOTTOMRIGHT', frame.out, 'TOPRIGHT', 0, 0)
  frame.topright:SetPoint('BOTTOMLEFT', frame.out, 'TOPRIGHT', 0, 0)
  frame.right:SetPoint('TOPLEFT', frame.out, 'TOPRIGHT', 0, 0)
  frame.header:SetWidth(frame.header:GetStringWidth() * 1.2)
  frame.header:SetPoint('TOPLEFT', frame, 'TOPLEFT', draw_x+4, 0)
  frame.titlebar:SetPoint('TOPLEFT', frame.header, 'TOPLEFT', -4, 0)
  frame.titlebar:SetPoint('TOPRIGHT', frame.header, 'TOPRIGHT', 0, 0)

  return (draw_x + frame.header:GetStringWidth() * 1.2 + 4)
end


--- Docks frames together
-- @param target frame on which to dock
-- @param ... frame objects to be docked
--
function D:DockFrame(...)
  local target = D.console[select(1,...)]
  if target.dockedTo then
    local t = D.c[target.dockedTo]
    print('channel',target.index,target.signature, 'is docked to',t.index, t.signature..'. using that instead')
    target = D.console[target.DockedTo]
  end

  target.docked = {}
  local draw_x = PrepareForDock(target, 4)
  for i = 2, select('#', ...) do
    local frame = D.console[select(i, ...)]
    frame.dockedTo = target.index
    width, draw_x = MorphToDock (frame, draw_x)
    table.insert(target.docked, frame.index)
    frame:ClearAllPoints()
    frame:SetPoint('TOPLEFT', target, 'TOPLEFT', 0, 0)
    frame:SetSize(target:GetWidth(), target:GetHeight())
    frame.x = nil
    frame.y = nil
    frame.width = nil
    frame.height = nil
    frame.oldscript = frame:GetScript('OnMousedown')
    frame:SetScript('OnMouseDown', nil)
    frame:SetScript('OnMouseUp', nil)
    frame:Save()
  end
end

--- Spaces each frame evenly across the screen.
function D:DistributeFrames() --
--print('frame grid:', max, num_side)
local max = self.num_channels
local num_side = math.ceil(math.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 = math.floor((i-1) / num_side)

  --print('move:', frame.signature, 'dx=', dx, 'dy=', dy)
  --print('move:', frame.signature, '                                       x=', dx * w, 'y=', -(dy * h), 'h=', h, 'w=', w)
  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 i, 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