view Dock.lua @ 99:7d94df3804a7

- Console drag buttons for resizing - While dragging a console frame, other frames are ghosted out - Dropdown menu includes Toggle, Pin, and MinMax
author Nenue
date Thu, 27 Oct 2016 06:11:04 -0400
parents 33bc8baba858
children 790dca545f1d
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 CreateFrame, tinsert, random = CreateFrame, tinsert, math.random
local ipairs, pairs = ipairs, pairs
local db
local print = DEVIAN_WORKSPACE and function(...) print('DvnDock', ...) end or nop
local DOCK_BUTTON_PADDING = 6

DevianDockHandler = {
  usedButtons = {},
  buttons = {},
}
DevianDockTabMixin = {}

function DevianDockTabMixin: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 DevianDockTabMixin:OnShow()
  self:Update()
end
function DevianDockTabMixin:OnEnter()
end
function DevianDockTabMixin: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 DevianDockTabMixin: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


do
  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, 'DevianDockTabTemplate')
      button.color = {r = random(), g = random(), b = 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
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