view modules/ReAction_ConfigUI/ReAction_ConfigUI.lua @ 30:0d95ce7a9ec2

- added Ace3 externs - converted ReAction_ConfigUI to use blizzard interface addons panel via AceConfigDialog-3.0 - partially converted FuBar module to LibRock, deprecated it (going to remove it entirely later) - cleaned up a couple other tidbits
author Flick <flickerstreak@gmail.com>
date Wed, 02 Apr 2008 23:31:13 +0000
parents 21bcaf8215ff
children c54c481ad0ed
line wrap: on
line source
--[[
  ReAction Configuration UI module

  This modules creates and manages ReAction configuration
  elements, including:

   - Interface Options panel
   - bar dragging and resizing control overlays
   - contextual menus

  Individual modules are responsible for populating these
  configuration elements via ReAction:RegisterOptions(). The
  valid values of 'context' are:
  
   - 'global'  : added to the Global Settings tab
   - 'module'  : added to the Module Settings tab
   - 'bar'     : added to the Bar Settings tab
   - 'barMenu' : shown on the bar contextual menu

--]]

-- local imports
local ReAction = ReAction
local L = ReAction.L
local _G = _G
local InCombatLockdown = InCombatLockdown

local Dewdrop = AceLibrary("Dewdrop-2.0")

-- module declaration
local moduleID = "ConfigUI"
local module = ReAction:NewModule( moduleID,
  "AceEvent-3.0"
)

module.configOptions = {
  type = "group",
  childGroups = "tab",
  args = {
    desc = {
      type = "description",
      name = L["Customizable replacement for Blizzard's Action Bars"],
    },
    global = {
      type = "group",
      name = L["Global Settings"],
      desc = L["Global configuration settings"],
      args = { 
        unlock = {
          type     = "toggle",
          handler  = module,
          name     = L["Unlock Bars"],
          desc     = L["Unlock bars for dragging and resizing with the mouse"],
          get      = function() return module.configMode end,
          set      = function(info, value) module:SetConfigMode(value) end,
          disabled = InCombatLockdown,
          order    = 1
        },
      },
      order = 1,
    },
    module = {
      type = "group",
      childGroups = "select",
      name = L["Module Settings"],
      desc = L["Configuration settings for each module"],
      args = { 
        configUI = {
          type = "group",
          name = "Config UI",
          desc = "description",
          args = {
            foo = {
              type = "toggle",
              handler = module,
              name = "foo",
              desc = "description",
              get = function() return true end,
              set = function() end,
            }
          }
        },
      },
      order = -1,
    },
  },
  plugins = { }
}

-- module methods
function module:OnInitialize()
  self.db = ReAction.db:RegisterNamespace( moduleID,
    {
      profile = { }
    }
  )
  self:InitializeOptions()
  LibStub("AceConfig-3.0"):RegisterOptionsTable("ReAction",self.configOptions)
  LibStub("AceConfigDialog-3.0"):AddToBlizOptions("ReAction", "ReAction")
  self:RegisterEvent("PLAYER_REGEN_DISABLED")
end

function module:InitializeOptions()
  for _, m in pairs(ReAction:GetOptions("global")) do
    for k, v in pairs(m) do
      self.configOptions.args.global.args[k] = v
    end
  end
  ReAction.RegisterCallback(self,"OnOptionsRegistered")
end

function module:OnOptionsRegistered(evt, context, module, opts)
  if context == "global" then
    for k, v in pairs(opts) do
      self.configOptions.args.global.args[k] = v
    end
  elseif context == "module" then
    for k, v in pairs(opts) do
      self.configOptions.args.module.args[k] = v
    end
  elseif context == "bar" then

  elseif context == "barMenu" then

  end
end

function module:PLAYER_REGEN_DISABLED()
  if self.configMode == true then
    UIErrorsFrame:AddMessage(L["ReAction config mode disabled during combat."])
    self:SetConfigMode(false)
  end
end

function module:SetConfigMode( mode )
  ReAction:CallMethodOnAllBars("ShowControls",mode)
  ReAction:CallMethodOnAllModules("ApplyConfigMode",mode,ReAction.bars)
  self.configMode = mode
end

function module:ApplyConfigMode( mode, bars )
  if not(mode) then
    -- close open dewdrop menu
    local p = Dewdrop:GetOpenedParent()
    if p then
      for _, bar in pairs(bars) do
        if bar then
          if p == bar.controlFrame then
            Dewdrop:Close()
          end
        end
      end
    end
  end
end

local function safecall(module, method, ...)
  if module and type(module[method]) == "function" then
    return module[method](method, ...)
  end
end

function module:OpenConfig(bar)
  Dewdrop:Close()
  InterfaceOptionsFrame_OpenToFrame("ReAction")
end

function module:ApplyToBar(bar)
  if self.configMode then
    bar:ShowControls(self.configMode)
  end
end

function module:RemoveFromBar(bar)
  if bar.controlFrame then
    bar.controlFrame:SetParent(UIParent)
    bar.controlFrame:ClearAllPoints()
    bar.controlFrame:Hide()
    bar.controlFrame = nil
  end
end





--
-- Bar config overlay
--
-- import some of these for small OnUpdate performance boost
local Bar           = ReAction.Bar.prototype
local GetSize       = Bar.GetSize
local GetButtonSize = Bar.GetButtonSize
local GetButtonGrid = Bar.GetButtonGrid
local SetSize       = Bar.SetSize
local SetButtonSize = Bar.SetButtonSize
local SetButtonGrid = Bar.SetButtonGrid
local ApplyAnchor   = Bar.ApplyAnchor
local floor         = math.floor
local min           = math.min
local format        = string.format
local GameTooltip   = GameTooltip

local function StoreExtents(bar)
  local f = bar.frame
  local point, relativeTo, relativePoint, x, y = f:GetPoint(1)
  relativeTo = relativeTo or f:GetParent()
  local anchorTo
  for name, b in pairs(ReAction.bars) do
    if b then
      if b:GetFrame() == relativeTo then
        anchorTo = name
        break
      end
    end
  end
  anchorTo = anchorTo or relativeTo:GetName()
  local c = bar.config
  c.anchor = point
  c.anchorTo = anchorTo
  c.relativePoint = relativePoint
  c.x = x
  c.y = y
  c.width, c.height = f:GetWidth(), f:GetHeight()
end

local function RecomputeButtonSize(bar)
  local w, h = GetSize(bar)
  local bw, bh = GetButtonSize(bar)
  local r, c, s = GetButtonGrid(bar)

  local scaleW = (floor(w/c) - s) / bw
  local scaleH = (floor(h/r) - s) / bh
  local scale = min(scaleW, scaleH)

  SetButtonSize(bar, scale * bw, scale * bh, s)
end

local function RecomputeButtonSpacing(bar)
  local w, h = GetSize(bar)
  local bw, bh = GetButtonSize(bar)
  local r, c, s = GetButtonGrid(bar)

  SetButtonGrid(bar,r,c,min(floor(w/c) - bw, floor(h/r) - bh))
end

local function RecomputeGrid(bar)
  local w, h = GetSize(bar)
  local bw, bh = GetButtonSize(bar)
  local r, c, s = GetButtonGrid(bar)

  SetButtonGrid(bar, floor(h/(bh+s)), floor(w/(bw+s)), s)
end

local function ClampToButtons(bar)
  local bw, bh = GetButtonSize(bar)
  local r, c, s = GetButtonGrid(bar)
  SetSize(bar, (bw+s)*c, (bh+s)*r )
end

local function HideGameTooltip()
  GameTooltip:Hide()
end

local function CreateControls(bar)
  local f = bar.frame

  f:SetMovable(true)
  f:SetResizable(true)
  f:SetClampedToScreen(true)

  -- buttons on the bar should be direct children of the bar frame.
  -- The control elements need to float on top of this, which we could
  -- do with SetFrameLevel() or Raise(), but it's more reliable to do it
  -- via frame nesting, hence good old foo's appearance here.
  local foo = CreateFrame("Frame",nil,f)
  foo:SetAllPoints()

  local control = CreateFrame("Button", nil, foo)
  control:EnableMouse(true)
  control:SetToplevel(true)
  control:SetPoint("TOPLEFT", -4, 4)
  control:SetPoint("BOTTOMRIGHT", 4, -4)
  control:SetBackdrop({
    edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
    tile = true,
    tileSize = 16,
    edgeSize = 16,
    insets = { left = 0, right = 0, top = 0, bottom = 0 },
  })

  -- textures
  local bgTex = control:CreateTexture(nil,"BACKGROUND")
  bgTex:SetTexture(0.7,0.7,1.0,0.2)
  bgTex:SetPoint("TOPLEFT",4,-4)
  bgTex:SetPoint("BOTTOMRIGHT",-4,4)
  local hTex = control:CreateTexture(nil,"HIGHLIGHT")
  hTex:SetTexture(0.7,0.7,1.0,0.2)
  hTex:SetPoint("TOPLEFT",4,-4)
  hTex:SetPoint("BOTTOMRIGHT",-4,4)
  hTex:SetBlendMode("ADD")

  -- label
  local label = control:CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
  label:SetAllPoints()
  label:SetJustifyH("CENTER")
  label:SetShadowColor(0,0,0,1)
  label:SetShadowOffset(2,-2)
  label:SetTextColor(1,1,1,1)
  label:SetText(bar:GetName())
  label:Show()
  bar.controlLabelString = label  -- so that bar:SetName() can update it

  local StopResize = function()
    f:StopMovingOrSizing()
    f.isMoving = false
    f:SetScript("OnUpdate",nil)
    StoreExtents(bar)
    ClampToButtons(bar)
    ApplyAnchor(bar)
  end

  -- edge drag handles
  for _, point in pairs({"LEFT","TOP","RIGHT","BOTTOM"}) do
    local edge = CreateFrame("Frame",nil,control)
    edge:EnableMouse(true)
    edge:SetWidth(8)
    edge:SetHeight(8)
    if point == "TOP" or point == "BOTTOM" then
      edge:SetPoint(point.."LEFT")
      edge:SetPoint(point.."RIGHT")
    else
      edge:SetPoint("TOP"..point)
      edge:SetPoint("BOTTOM"..point)
    end
    local tex = edge:CreateTexture(nil,"HIGHLIGHT")
    tex:SetTexture(1.0,0.82,0,0.7)
    tex:SetBlendMode("ADD")
    tex:SetAllPoints()
    edge:RegisterForDrag("LeftButton")
    edge:SetScript("OnMouseDown",
      function()
        local bw, bh = GetButtonSize(bar)
        local r, c, s = GetButtonGrid(bar)
        f:SetMinResize( bw+s+1, bh+s+1 )
        f:StartSizing(point)
        f:SetScript("OnUpdate", 
          function()
            RecomputeGrid(bar)
            bar:RefreshLayout()
          end
        )
      end
    )
    edge:SetScript("OnMouseUp", StopResize)
    edge:SetScript("OnEnter",
      function()
        GameTooltip:SetOwner(f, "ANCHOR_"..point)
        GameTooltip:AddLine(L["Drag to add/remove buttons"])
        GameTooltip:Show()
      end
    )
    edge:SetScript("OnLeave", HideGameTooltip)
    edge:Show()
  end

  -- corner drag handles, again nested in an anonymous frame so that they are on top
  local foo2 = CreateFrame("Frame",nil,control)
  foo2:SetAllPoints(true)
  for _, point in pairs({"BOTTOMLEFT","TOPLEFT","BOTTOMRIGHT","TOPRIGHT"}) do
    local corner = CreateFrame("Frame",nil,foo2)
    corner:EnableMouse(true)
    corner:SetWidth(12)
    corner:SetHeight(12)
    corner:SetPoint(point)
    local tex = corner:CreateTexture(nil,"HIGHLIGHT")
    tex:SetTexture(1.0,0.82,0,0.7)
    tex:SetBlendMode("ADD")
    tex:SetAllPoints()
    corner:RegisterForDrag("LeftButton","RightButton")
    local updateTooltip = function()
      local size, size2 = bar:GetButtonSize()
      local rows, cols, spacing = bar:GetButtonGrid()
      size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
      GameTooltipTextRight4:SetText(size)
      GameTooltipTextRight5:SetText(tostring(spacing))
    end
    corner:SetScript("OnMouseDown",
      function(_,btn)
        local bw, bh = GetButtonSize(bar)
        local r, c, s = GetButtonGrid(bar)
        if btn == "LeftButton" then -- button resize
          f:SetMinResize( (s+12)*c+1, (s+12)*r+1 )
          f:SetScript("OnUpdate", 
            function()
              RecomputeButtonSize(bar)
              bar:RefreshLayout()
              updateTooltip()
            end
          )
        elseif btn == "RightButton" then -- spacing resize
          f:SetMinResize( bw*c, bh*r )
          f:SetScript("OnUpdate", 
            function()
              RecomputeButtonSpacing(bar)
              bar:RefreshLayout()
              updateTooltip()
            end
          )
        end
        f:StartSizing(point)
      end
    )
    corner:SetScript("OnMouseUp",StopResize)
    corner:SetScript("OnEnter",
      function()
        GameTooltip:SetOwner(f, "ANCHOR_"..point)
        GameTooltip:AddLine(L["Drag to resize buttons"])
        GameTooltip:AddLine(L["Right-click-drag"])
        GameTooltip:AddLine(L["to change spacing"])
        local size, size2 = bar:GetButtonSize()
        local rows, cols, spacing = bar:GetButtonGrid()
        size = (size == size2) and tostring(size) or format("%dx%d",size,size2)
        GameTooltip:AddDoubleLine(L["Size:"], size)
        GameTooltip:AddDoubleLine(L["Spacing:"], tostring(spacing))
        GameTooltip:Show()
      end
    )
    corner:SetScript("OnLeave", 
      function()
        GameTooltip:Hide()
        f:SetScript("OnUpdate",nil)
      end
    )

  end

  control:RegisterForDrag("LeftButton")
  control:RegisterForClicks("RightButtonDown")
  
  control:SetScript("OnDragStart",
    function()
      f:StartMoving()
      f.isMoving = true
      -- TODO: snap indicator update install
    end
  )

  control:SetScript("OnDragStop",
    function()
      f:StopMovingOrSizing()
      f.isMoving = false
      f:SetScript("OnUpdate",nil)
      -- TODO: snap frame here
      StoreExtents(bar)
    end
  )

  control:SetScript("OnEnter",
    function()
      -- add bar type and status information to name
      local name = bar.name
      for _, m in ReAction:IterateModules() do
        local suffix = safecall(m,"GetBarNameModifier",bar)
        if suffix then
          name = ("%s %s"):format(name,suffix)
        end
      end
      
      GameTooltip:SetOwner(f, "ANCHOR_TOPRIGHT")
      GameTooltip:AddLine(name)
      GameTooltip:AddLine(L["Drag to move"])
      --GameTooltip:AddLine(L["Shift-drag for sticky mode"])
      GameTooltip:AddLine(L["Right-click for options"])
      GameTooltip:Show()
    end
  )

  control:SetScript("OnLeave", HideGameTooltip)

  control:SetScript("OnClick",
    function()
      bar:ShowMenu()
    end
  )

  return control
end

function Bar:ShowControls(show)
  if show then
    if not self.controlFrame then
      self.controlFrame = CreateControls(self)
    end
    self.controlFrame:Show()
  elseif self.controlFrame then
    self.controlFrame:Hide()
  end
end

function Bar:ShowMenu()
  if not self.menuOpts then
    self.menuOpts = {
      type = "group",
      args = {
        openConfig = {
          type = "execute",
          name = L["Configure..."],
          desc = L["Open the configuration dialogue for this bar"],
          func = function() module:OpenConfig(self) end,
          disabled = InCombatLockdown,
          order = 1
        },
        delete = {
          type = "execute",
          name = L["Delete Bar"],
          desc = L["Remove the bar from the current profile"],
          func = function() ReAction:EraseBar(self) end,
          order = 2
        },
      }
    }
  end
  if self.modMenuOpts == nil then
    self.modMenuOpts = { }
  end
  for _, m in ReAction:IterateModules() do
    local opts = safecall(m,"GetBarMenuOptions",self,module)
    if opts then
      for k, v in pairs(opts) do
        self.menuOpts.args[k] = v
      end
    end
  end
  Dewdrop:Open(self.controlFrame, "children", self.menuOpts, "cursorX", true, "cursorY", true)
end

local Bar_SuperSetName = Bar.SetName
function Bar:SetName(name)
  Bar_SuperSetName(self,name)  
  if self.controlLabelString then
    self.controlLabelString:SetText(self.name)
  end
end