view Devian.lua @ 7:d0cd0e68213e v1.2-r7

Frame anchor set in a way that doesn't disjoint the other pieces when it's changed. Corrected arithmetic in SavePos; should be using y = GetTop - GetScreenHeight Height parameter only saved when maximized
author Nenue
date Fri, 18 Dec 2015 20:49:58 -0500
parents b526dc930b15
children 12d814c10962
line wrap: on
line source
-- User: Krakyn
-- Created: 11/30/2015 7:46 AM
if not LibStub then
  print('Something has happened...')
end
Devian = LibStub("AceAddon-3.0"):NewAddon("Devian", "AceConsole-3.0", "AceEvent-3.0")
local D = _G.Devian
local STATE_LOW, STATE_HIGH = 1, 2
local PLAYER_REALM = UnitName("player") .. '-' .. GetRealmName()
local DEVIAN_FRAME = DevianConsole
local cherry = false
local print = function(...)
  _G.print('DVN', ...)
end
if not DevianDB then
  DevianDB = {
    ['global'] = {[STATE_LOW] = {}, [STATE_HIGH] = {}},
    console = true,
    dnd_status = true,
    dnd_message = "Debugging. Your messages may get eaten.",
    x = 100,
    y = 800,
    height = 500,
    width = 600,
    font = [[Interface\Addons\Devian\font\SourceCodePro-Regular.ttf]],
    fontsize = 13,
    fontoutline = 'NONE',
  }
  cherry = "This is probably the first time, so:\n  /dvn 2 to save your regular addon list\n  /dvn 1 to save your development addon list\n  /dvn to switch between the two\n  /dvc to toggle print() frame"
end
local db = DevianDB

function D:SavePos(x,y)
  db.y = (DEVIAN_FRAME:GetTop() - GetScreenHeight())
  db.x = DEVIAN_FRAME:GetLeft()
  db.width = DEVIAN_FRAME:GetWidth()
  if not DEVIAN_FRAME.minimized then
    db.height = DEVIAN_FRAME:GetHeight()
  end

  print(db.y)
  DEVIAN_FRAME:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', db.x, db.y)
end

local ScanAddOnList = function(args)
  local list_state

  local mode = tonumber(args:match('^%d$'))

  print('ScanAddOnList(', mode, ')')
  if mode == nil then
    list_state = db.enabled and STATE_LOW or STATE_HIGH
    db.enabled = (db.enabled == false) and true or false
    print(list_state, db.enabled)

    if list_state == STATE_LOW then
      BNSetDND(true)
    end

  else
    if mode > 2 then
      print('ScanAddOnList(',mode,'): Something has happened.')
      return
    end
    list_state = mode == STATE_LOW and STATE_LOW or STATE_HIGH

    if not db.chat_size then
      db.chat_size = {}
    end
    if mode == STATE_LOW or mode == STATE_HIGH then
      db.chat_size[list_state] = ChatFrame1:GetHeight()
    end

  end
  local char_list, global_list = db[PLAYER_REALM][list_state], db.global[list_state]

  local playername = UnitName("player")

  for i = 1, GetNumAddOns() do
    local name = GetAddOnInfo(i)
    local enableState, globalState = GetAddOnEnableState(playername, i), GetAddOnEnableState(nil, i)

    if mode == STATE_LOW or mode == STATE_HIGH then
      char_list[name] = enableState
      global_list[name] = globalState
    else

      if char_list[name] ~= 0 and global_list[name] ~= 0 then
        local value = false
        if char_list[name] == 2 and global_list[name] == 1 then
          value = UnitName("player")
        elseif global_list[name] == 2 then
          value = true
        end
        print('EnableAddOn(', i, ',', value,')')
        EnableAddOn(i, value)
      else
        local value = true
        if char_list[name] == 2 and global_list[name] == 1 then
          value = UnitName("player")
        end
        print('DisableAddOn(', i, ',', value,')')
        DisableAddOn(i,value)
      end

    end
  end

  if mode == nil then
    ReloadUI()
  end
  if mode == STATE_LOW then
    D:Print('Developement AddOn list saved.')
  else
    D:Print('Standard AddOn list saved.')
  end
end

-- Debug info
local function CreateMessenger()
  if db.console == true then
    DEVIAN_FRAME:Show()
  end


  local f = DEVIAN_FRAME
  --[[
  f.backdrop = f:CreateTexture('backdrop', 'BACKGROUND')
  f.backdrop:SetTexture(1,1,1,1)
  f.backdrop:SetGradient('HORIZONTAL', 0.2, 0.2, 0.2, 1, 1, 1)
  f.backdrop:SetBlendMode('MOD')
  f.backdrop:SetAllPoints(f)
  f:AddMessage('init a thing')

  f:EnableMouseWheel(true)
  f:EnableKeyboard(true)
  f:SetScript('OnMouseWheel', function (self, delta)
  end)

  f:ScrollToBottom()
  f:SetMaxLines(500)
  --]]
  f.console:SetInsertMode('BOTTOM')
  f:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', db.x, db.y)
  f:SetSize(db.width, db.height)
  f:Lower()

  f.console:SetFading(false)
  f.console:SetTimeVisible(2147483647)
  f.console:SetFont(db.font, db.fontsize, db.fontoutline)
  --f:SetJustifyH('LEFT')


  D.debug_init = true
end
function D:ToggleMinMax()
  if DEVIAN_FRAME.minimized then
    self:Maximize()
  else
    self:Minimize()
  end
end
function D:Minimize()
  DEVIAN_FRAME:SetHeight(20)
  DEVIAN_FRAME.minimized = true
  DEVIAN_FRAME:SetMaxResize(GetScreenWidth(),20)
  return DEVIAN_FRAME.console:Hide()
end
function D:Maximize()
  DEVIAN_FRAME:SetHeight(db.height)
  DEVIAN_FRAME.minimized = nil
  DEVIAN_FRAME:SetMaxResize(GetScreenWidth(),GetScreenHeight())
  return DEVIAN_FRAME.console:Show()
end

local prefix_cache = {}
local function Message(prefix, ...)
  if prefix == nil then
    oldprint('Tried to print nothing.')
    return
  end
  -- colorize
  if not prefix_cache[prefix] then
    local c  = {0, 0, 0 }
    local max = string.len(prefix)
    for i = 1, max, 3 do
        for k, v in ipairs(c) do
          local j = i + (k - 1)
          c[k] = c[k] + (j <= max and string.byte(prefix,j) or 0)
        end
    end
    for k,v in ipairs(c) do
      c[k] = c[k] % 255
      if c[k] < 64 then
        c[k] = 0
      elseif c[k] > 127 then
        c[k] = 255
      end
    end
    prefix_cache[prefix] = string.format('%02X%02X%02X', unpack(c))
  end

  local buffer = {'|cFF'.. prefix_cache[prefix]..prefix ..'|r'}
  for i = 1, select('#',...) do
    local var = select(i, ...)

    if type(var) == 'table' then
      var = '<table>'
    elseif type(var) == 'boolean' then
      var = var and 'true' or 'false'
    elseif type(var) == 'function' then
      var = '<funcref>'
    elseif type(var) == 'nil' then
      var = 'nil'
    end

    table.insert(buffer, var)
  end
  DEVIAN_FRAME.console:AddMessage(table.concat(buffer, ' '))
  table.wipe(buffer)
end

function D:OnEnable()
  if db.enabled then
    DEVIAN_FRAME:Hide()
    D:Print('Standard AddOn list active. Type /dvn to switch to development mode.')
  else
    D:Print('Development AddOn list active. Type /dvn to revert to regular operation.')
  end
end


function D:OnInitialize()

  if not db[PLAYER_REALM] then
    db[PLAYER_REALM] = {[STATE_LOW] = {}, [STATE_HIGH] = {} }
    if not cherry then
      cherry = "This character didn't have an AddOn table."
    end
  end

  -- inform the user if we have a virgin data table
  if cherry then
    D:Print(cherry)
  end

  -- replace print handler and make the original available in global if possible

  CreateMessenger()
  D.oldprint = getprinthandler()
  if not _G.oldprint then
    _G.oldprint = D.oldprint
  end
  setprinthandler(Message)
  self:RegisterChatCommand("dvn", ScanAddOnList)
  self:RegisterChatCommand("dvc", function(args)
    db.console = (db.console == false) and true or false
    if db.console then
      DEVIAN_FRAME:Show()
      D:Maximize()
    else
      DEVIAN_FRAME:Hide()
    end
  end)

  print('Master! I am here.')
end