view Veneer.lua @ 73:95ed343c3a42

- VeneerBuffTemplate and members
author Nenue
date Tue, 23 Aug 2016 16:15:09 -0400
parents d80db9a9b7e4
children cd6e78091b04
line wrap: on
line source
-- Veneer
-- Base framework for making things draggable.

local vn, print = LibStub("LibKraken").register(VeneerController)


local defaults = {
  enableAll = true,
  enableModule = {
    BuffFrame = true,
  },
  BuffFrame = {
    width = 48,
    height = 48,
  }
}
local configMode
local veneers = {}

do
  local anonID = 0
  local AnonymousName = function()
    anonID = anonID + 1
    return 'VN' .. anonID
  end
end

local anchor_coefficients = {
  ['TOP'] = function(x, y) return x, y end,
  ['BOTTOM'] = function(x, y) return x,y end,
  ['LEFT'] = function(x, y) return x,y end,
  ['RIGHT'] = function(x,y) return x,y end,
}

local VeneerButton_OnDragStart = function(self)
  self.startingLeft = self:GetLeft()
  self.startingBottom = self:GetBottom()
  self.anchors = self.anchors or {}
  table.wipe(self.anchors)

  local frame = self:GetParent()
  local n = frame:GetNumPoints()
  for i = 1, n do
    local anchor, parent, relative, x, y = frame:GetPoint(i)
    self.anchors[i] = {
      anchor = anchor,
      parent = parent,
      relative = relative,
      x = x,
      y = y
    }
  end

  print(self:GetName(), 'start moving', self.startingLeft, self.startingBottom)
  self:StartMoving()
end

local VeneerButton_OnDragStop =  function(self)
  self:StopMovingOrSizing()
  if self.OnDragStop then
    self.OnDragStop(self)
  else
    local frame = self:GetParent()
    local dx = self:GetLeft() - self.startingLeft
    local dy = self:GetBottom() - self.startingBottom

    frame:ClearAllPoints()
    for i, point in ipairs(self.anchors) do
      frame:SetPoint(point.anchor, point.parent, point.relative, point.x + dx, point.y + dy)
      print('adjusting anchor', point.anchor, point.parent, point.relative, point.x + dx, point.y + dy)
    end
  end
end

local Veneer_FixMovers = function()
  for frame, veneer in pairs(veneers) do
    if veneer:IsMoving() then
      VeneerButton_OnDragStop(veneer)
    end
  end
end

local VeneerButton_Update = function(self)
  if configMode then
    self:SetScript('OnDragStart', VeneerButton_OnDragStart)
    self:SetScript('OnDragStop', VeneerButton_OnDragStop)
    self:SetMovable(true)
    self:EnableMouse(true)
    self:RegisterForDrag('LeftButton')

    self.bg:SetColorTexture(0,1,0,0.5)
    for i, region in ipairs(self.configLayers) do
      region:Show()
    end
    self:Show()
  else

    self:SetScript('OnDragStart', self.StartMoving)
    self:SetScript('OnDragStop', self.StopMovingOrSizing)
    self:SetMovable(false)
    self:EnableMouse(false)

    self.bg:SetColorTexture(0,1,0,0)
    for i, region in ipairs(self.configLayers) do
      region:Hide()
    end
    if self.isHidden then
      self:Hide()
    end

  end
end

local ToggleVeneerConfig = function()
  if configMode then
    configMode = false
    vn:print('Config mode off.')
  else
    configMode = true
    vn:print('Config mode on.')
  end

  for frame, veneer in pairs(veneers) do
    VeneerButton_Update(veneer)
  end
end

local VeneerButton_OnShow = function(self)
  VeneerButton_Update(self)
end

vn.GetVeneer = function(frame, template)
  if not frame then
    print('|cFFFF4400Unable to acquire frame...|r')
    return
  end


  if veneers[frame] then
    return veneers[frame]
  end

  local name = (frame:GetName() or AnonymousName())..'Veneer'
  local veneer = CreateFrame('Frame', name, frame, template or 'VeneerTemplate')
  print('+veneer', name)

  veneer:SetAllPoints(frame)
  veneer:SetParent(frame)
  veneer.label:SetText(name)
  veneer.bg:SetColorTexture(1,1,1,0)
  veneer:Hide()
  veneer:EnableMouse(false)

  veneer:SetScript('OnShow', VeneerButton_OnShow)

  -- find current X/Y
  veneer.currentLeft = frame:GetLeft()
  veneer.currentTop = frame:GetTop()


  veneers[frame] = veneer
  return veneers[frame]
end

vn.init = function()
  if (not VeneerData) or (not VeneerData.version) then
    VeneerData = defaults
  end
  vn.db = VeneerData
end


vn.wrap = function(module)
  vn.modules = vn.modules or {}
  tinsert(vn.modules, module)
end

SLASH_VENEER1 = "/veneer"
SLASH_VENEER2 = "/vn"

SlashCmdList.VENEER = function()
  ToggleVeneerConfig()
end