diff Devian.lua @ 13:080dfa4990fb

necessary abstractions for running multiple console buffers in place message prefix can act as a channel selector when tagged in savedvars more frame vars defined in the XML XML parentKey names organized
author Nenue
date Sat, 19 Dec 2015 07:59:12 -0500
parents 65fcb81f4e0a
children 5254d1ba6013
line wrap: on
line diff
--- a/Devian.lua	Sat Dec 19 03:14:25 2015 -0500
+++ b/Devian.lua	Sat Dec 19 07:59:12 2015 -0500
@@ -4,57 +4,51 @@
   print('Something has happened...')
 end
 Devian = LibStub("AceAddon-3.0"):NewAddon("Devian", "AceConsole-3.0", "AceEvent-3.0")
+local MAJOR, MINOR = 'Devian-1.3', 'r@project-revision@'
 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 DEVIAN_FRAME = 'DevianConsole'
 local print = function(...)
-  _G.print('DVN', ...)
+  _G.print('Dvn', ...)
 end
 local db
+local defaults = {
+  ['global'] = {[STATE_LOW] = {}, [STATE_HIGH] = {}},
+  ['tags'] = {},
+  ['channels'] = {[1] = {signature = 'Dvn', name = 'Main', header = "%n [%t]", x = 100, y = 800, height = 500, width = 600, enabled = true}},
+  current_channel = 1,
+  console = true,
+  dnd_status = true,
+  dnd_message = "Debugging. Your messages may get eaten.",
+  font = [[Interface\Addons\Devian\font\SourceCodePro-Regular.ttf]],
+  fontsize = 13,
+  fontoutline = 'NONE',
+}
 
 
-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 function ScanAddOnList(cmd, ...)
   local list_state
 
-  local mode = tonumber(args:match('^%d$'))
+  local mode = tonumber(cmd:match("%d"))
 
-  print('ScanAddOnList(', mode, ')')
+
+  print('Starting Addons scan. arg=', 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.')
+      print('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]
@@ -69,6 +63,7 @@
       char_list[name] = enableState
       global_list[name] = globalState
     else
+      if char_list[name] or global_list[name] then
 
       if char_list[name] ~= 0 and global_list[name] ~= 0 then
         local value = false
@@ -87,6 +82,7 @@
         print('DisableAddOn(', i, ',', value,')')
         DisableAddOn(i,value)
       end
+      end
 
     end
   end
@@ -101,75 +97,127 @@
   end
 end
 
--- Debug info
-local function CreateMessenger()
-  if db.console == true and db.enabled == false 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
+local function Console_MinMax(self)
+  if self.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()
+
+local function Console_Minimize(self)
+  self:SetHeight(20)
+  self:SetMaxResize(GetScreenWidth(),20)
+  self.minimized = true
+  return self.out:Hide()
 end
 
-local prefix_cache = {}
+local function Console_Maximize(self)
+  local db = db.channels[self.index]
+  self:SetHeight(db.height)
+  self:SetMaxResize(GetScreenWidth(),GetScreenHeight())
+  self.minimized = nil
+  self.grip:SetTexture(0.5,0.5,0.5)
+  return self.out:Show()
+end
+
+
+local function Console_Save(self)
+  local db = db.channels[self.index]
+  db.y = (self:GetTop() - GetScreenHeight())
+  db.x = self:GetLeft()
+  db.width = self:GetWidth()
+  if not self.minimized then
+    db.height = self:GetHeight()
+    db.minimized = nil
+  else
+    db.minimized = true
+  end
+  if self:IsVisible() then
+    db.enabled = true
+  else
+    db.enabled = nil
+  end
+  self:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', db.x, db.y)
+end
+
+
+local function Console_Toggle(input)
+  local key = input:gmatch("[%a%d]")
+  local search
+  if key and D.sig[key] then
+    search = {D.sig[key]}
+  elseif D.console[key] then
+    search = {D.console[key]}
+  else
+    search = D.console
+  end
+
+  for _, c in ipairs(search) do
+    if c:IsVisible() then
+      c:Hide()
+    else
+      c:Show()
+      c:Maximize()
+    end
+    c:Save()
+  end
+end
+
+--
+--
+-- Construct a console frame
+local function CreateConsole(i, vars)
+  local f = CreateFrame('Frame', 'DevianChannelFrame' .. tostring(i), UIParent, DEVIAN_FRAME)
+  f:SetPoint('TOPLEFT', UIParent, 'TOPLEFT', db.channels[i].x, db.channels[i].y)
+  f:SetSize(vars.width, vars.height)
+  f:Lower()
+  f.out:SetFont(db.font, db.fontsize, db.fontoutline)
+  f.Save = Console_Save
+  f.Minimize = Console_Minimize
+  f.Maximize = Console_Maximize
+  f.MinMax = Console_MinMax
+  f.Toggle = D.Console_Toggle
+  f.name = vars.name
+  f.index = i
+
+  if db.channels[i].enabled then
+    f:Show()
+  end
+  if db.channels[i].minimized then
+    f:Minimize()
+  end
+
+  return f
+end
+
+--
+--
+-- Send a message somewhere
 local function Message(prefix, ...)
   if prefix == nil then
-    oldprint('Tried to print nothing.')
-    return
+    prefix = 1
   end
 
+  local channel
+  local byName = true
+  if D.sig[prefix] then
+    channel = D.sig[prefix]
+  elseif D.console[prefix] then
+    channel = D.console[prefix]
+    byName = nil
+  else
+    channel = D.console[1]
+  end
+
+
+
   -- color me timbers
   local pcolor
-  if db.tags[prefix] then
+  if D.tags[prefix] then
     pcolor = db.tags[prefix]
-  elseif not prefix_cache[prefix] then
+  elseif byName then
     local c  = {0, 0, 0 }
     local max = string.len(prefix)
     for i = 1, max, 3 do
@@ -186,10 +234,10 @@
         c[k] = 255
       end
     end
-    prefix_cache[prefix] = string.format('%02X%02X%02X', unpack(c))
+    D.tags[prefix] = string.format('%02X%02X%02X', unpack(c))
   end
 
-  local buffer = {'|cFF'.. prefix_cache[prefix]..prefix ..'|r'}
+  local buffer = {'|cFF'.. D.tags[prefix]..prefix ..'|r'}
   for i = 1, select('#',...) do
     local var = select(i, ...)
 
@@ -205,40 +253,45 @@
 
     table.insert(buffer, var)
   end
-  DEVIAN_FRAME.console:AddMessage(table.concat(buffer, ' '))
+  channel.out:AddMessage(table.concat(buffer, ' '))
   table.wipe(buffer)
 end
 
 function D:OnEnable()
+  -- commands
+  local cmdlist = {
+    ['dvn'] = ScanAddOnList,
+    ['devian'] = ScanAddOnList,
+    ['dvc'] = Console_Toggle,
+  }
+  for cmd, func in pairs(cmdlist) do
+    self:RegisterChatCommand(cmd, func, true)
+  end
+
   if db.enabled == true then
     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()
+  -- emergency button
+  self:RegisterChatCommand("cleandvn", function(args)
+    DevianDB = nil
+    ReloadUI()
+    end)
+
+  -- savedvars
+  local cherry = false
   if not _G.DevianDB then
-    _G.DevianDB = {
-      ['global'] = {[STATE_LOW] = {}, [STATE_HIGH] = {}},
-      ['tags'] = {},
-      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"
+    _G.DevianDB = defaults
+    cherry = "Type /dvnsave to snapshot your current UI"
   end
   db = _G.DevianDB
 
-  print(PLAYER_REALM)
   if not db[PLAYER_REALM] then
     db[PLAYER_REALM] = {[STATE_LOW] = {}, [STATE_HIGH] = {}}
     if not cherry then
@@ -246,29 +299,25 @@
     end
   end
 
-  -- inform the user if we have a virgin data table
+  -- copy tags
+  self.tags = {}
+  for n, c in pairs(db.tags) do
+    self.tags[n] = c
+  end
   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
+
+  self.console = {}
+  self.sig = {}
+  for i, cinfo in ipairs(db.channels) do
+    self.console[i] = CreateConsole(i, cinfo)
+    self.sig[cinfo.signature] = self.console[i]
+  end
   setprinthandler(Message)
-  self:RegisterChatCommand("dvn", ScanAddOnList)
-  self:RegisterChatCommand("dvc", function(args)
-    db.console = (not db.console) and true or nil
-    if db.console then
-      DEVIAN_FRAME:Show()
-      D:Maximize()
-    else
-      DEVIAN_FRAME:Hide()
-    end
-  end)
-
-  print('Master! I am here.')
+  print(MAJOR, MINOR)
 end
\ No newline at end of file