view Devian.lua @ 4:247118593c66

Console interface properly resides in XML, and has been fitted with a sizing widget. Console frame can be moved by clicking and dragging anywhere, and can be minimized by right click. Font paths point to the actual addon tree.
author Nenue
date Fri, 18 Dec 2015 19:48:10 -0500
parents 77adceb8ebe4
children ac644fc860cc
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
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 = -150,
    height = 500,
    width = 600,
  }
  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()
  db.x = DEVIAN_FRAME:GetLeft()
  db.width = DEVIAN_FRAME:GetWidth()
  db.height = DEVIAN_FRAME:GetHeight()
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('CENTER', UIParent, 'CENTER', db.x, db.y)
  f:SetSize(db.width, db.height)
  f:Lower()
  f.console:SetFont([[Interface\Addons\Turok\Media\font\SourceCodePro-Regular.ttf]], 13, 'NONE')

  f.console:SetFading(false)
  f.console:SetTimeVisible(2147483647)
  --f:SetJustifyH('LEFT')


  D.debug_init = true
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()
    else
      DEVIAN_FRAME:Hide()
    end
  end)

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