view libKT/libKT-1.0.lua @ 2:f7a0898d293c

adding library files, toc files, and a missing XML to versioning
author Nenue
date Tue, 21 Jun 2016 08:10:36 -0400
parents
children
line wrap: on
line source
--[[ Implements
--  KT.register(frame) to hook the following (all optional):
--    frame:init()                            run immediately after KT sets itself up
--    frame:profile("Name-TruncatedRealm")    called the first time SavedVars data becomes available
--    frame:variables()                       called by PLAYER_ENTERING_WORLD
--    frame:event(event, ...)                 replaces the event callback
--    frame:ui()                              called by /ui when activating
]]--

--GLOBALS: LibKT, KrakTool, KTErrorFrame, LibKTError, SlashCmdList, SLASH_RL1, SLASH_UI1
local CreateFrame, debugstack, tostring, select = CreateFrame, debugstack, tostring, select
local print, max, unpack, tinsert = print, max, unpack, tinsert
local ipairs, xpcall = ipairs, xpcall
local UI_TOGGLE = false

KrakTool = CreateFrame('Frame', 'KrakTool', UIParent)
LibKT = select(2, ...)
local KT = LibKT
KT.handler = KrakTool
KT.frames = {}

SLASH_RL1 = "/rl"
SlashCmdList.RL = function ()
  ReloadUI()
end

SLASH_UI1 = "/ui"
SlashCmdList.UI = function ()
  if UI_TOGGLE then
    UI_TOGGLE = false
  else
    UI_TOGGLE = true
  end
  for i, frame in pairs(KT.frames) do
    if UI_TOGGLE then
      if frame.close then
        frame.close()
      else
        frame:Hide()
      end
    else
      if frame.ui then
        frame.ui()
      end
      frame:Show()
    end
  end
end

LibKTError = function(msg)
  local dstack = debugstack()
  :gsub("Interface\\AddOns\\",'~\\')
  :gsub("<(.-)>", function(a) return '|cFF00FFFF<'.. a ..'>|r' end)
  KTErrorFrame.errmsg:SetText(msg)
  KTErrorFrame.debugstack:SetText(dstack)
  KTErrorFrame:SetHeight(KTErrorFrame.debugstack:GetStringHeight() + KTErrorFrame.errmsg:GetStringHeight() + 12)
  KTErrorFrame:Show()
end

local initStack = {}
local eventStub = function(self, event, ...)
  local isHandled
  local nodebug

  if self.event then
    nodebug = self.event(self, event, ...)
  end

  if self[event] then
    nodebug = nodebug or self[event](self, event, ...)
    self.missed = 0
    self.handled = self.handled + 1
    isHandled = true
  else
    self.firstEvent = false
    self.unhandled = self.unhandled + 1
    self.missed = self.missed + 1
  end

  if nodebug then
    return
  end

  print(self:GetName(), event, ...)

  -- debug outputs
  if self.status then
    self.status:SetText(event .. '\n|cFF00FF00' .. self.handled .. '|r |cFFFF8800' .. self.missed .. '|r |cFFFF4400' .. self.unhandled .. '|r')
    if isHandled then
      self.status:SetTextColor(0,1,0)
      if self.log then
        local logtext = event
        for i = 1, select('#',...) do
          logtext = logtext .. '\n' .. i .. ':' .. tostring(select(i,...))
        end
        self.log:SetText('|cFFFFFF00last|r\n' .. logtext)
        local newWidth = self.log:GetStringWidth()

        if self.logfirst then
          if not self.firstEvent then
          self.firstEvent = event
          self.logfirst:SetText('|cFF00FF88first|r\n' .. logtext)
          end

          newWidth = newWidth + self.logfirst:GetStringWidth()
        end
        if self.logdiff then
          if not event ~= self.firstEvent then
          self.firstEvent = event
          self.logdiff:SetText('|cFF0088FFdiff|r\n' .. logtext)
          end
          newWidth = newWidth + self.logdiff:GetStringWidth()
        end
        --self:SetWidth(newWidth)
      end
    else
      self.status:SetTextColor(1,0,0)
    end
  end
end

KT.register = function(frame, name, noGUI)
  if not name then
    name = frame:GetName()
  end

  KT.frames[name] = frame
  frame:SetScript('OnEvent', eventStub)
  frame.unhandled = 0
  frame.missed = 0
  frame.handled = 0
  frame.firstEvent = false
  tinsert(initStack, frame)

  if noGUI then
    return
  end

  frame.UIPanelAnchor = {'TOPLEFT', frame, 'TOPLEFT', 12, -12 }
  frame.UIPanelGrowth = {'TOPLEFT', 'TOPRIGHT', 14, 0}
  frame.button = KT.button
  frame.uibutton = KT.uibutton
  frame.tab = KT.tab
  frame.print = KT.print

  return KT
end

KT.handler:RegisterEvent('VARIABLES_LOADED')
KT.handler:SetScript('OnEvent', function(self, event, ...)
  print('KrakTool', event, ...)
  if not LibKTDB then
    LibKTDB = {}
  end
  KT.db = LibKTDB

  KT.db.runcount = KT.db.runcount or 1
  KT.db.runcount = KT.db.runcount + 1
  print(KT.db.runcount)

  for i, frame in ipairs(initStack) do
    print('|cFF00FF00', i, '|r', frame:GetName())
    if frame.init then
      xpcall(frame.init, LibKTError)
    end
  end

  self:RegisterEvent('PLAYER_ENTERING_WORLD')
  self:SetScript('OnEvent', function()
    for i, frame in ipairs(initStack) do
      print('|cFF00FFFF', i, '|r', frame:GetName())
      if frame.variables then
        xpcall(frame.variables, LibKTError)
      end
    end
  end)
end)

KT.print = function(module, ...)
  print('|cFF00FFFF'..module:GetName()..'|r:', ...)
end

--- Button generators

local GetButtonTemplate = function(name, parent, template, onClick)
  if _G[name] then
    return _G[name]
  end

  local button = CreateFrame('Button', name, parent, template)
  button:SetScript('OnMouseUp', onClick)
  return button
end

local SetButtonAnchor = function(self, collector, anchor, growth)
  if self:GetID() == 0 then
    self:SetID(#collector)
    print('registered TabButton #', self:GetID())
  end

  if self:GetID() == 1 then
    self:SetPoint(unpack(anchor))
  else
    growth[2] = collector[self:GetID()-1]
    self:SetPoint(unpack(growth))
  end
end

KT.tab = function(self, name, tooltip, texture, coords)
  local button = GetButtonTemplate(name, self,  'KTTabButton', self.SelectTab)
  button.icon:SetTexture(texture)
  button.tooltip = tooltip
  button:SetSize(unpack(self.tabSize))
  if coords then
    button.icon:SetTexCoord(unpack(coords))
  end
  SetButtonAnchor(button, self.tabButtons, self.tabAnchor, self.tabGrowth)
  return button
end

KT.button = function(self, name, text, tooltip, onClick)
  local button = GetButtonTemplate(name, self, 'KTButton', onClick)

  button.tooltip = tooltip
  button:SetText(text)
  button:SetWidth(max(button:GetWidth(), button:GetFontString():GetStringWidth() + 12))

  SetButtonAnchor(button, self.controls, self.controlsAnchor, self.controlsGrowth)
  return button
end

KT.uibutton = function(self, name, text, tooltip, onClick, texture, coords)
  local button = GetButtonTemplate(name, self, 'KTUIPanelButton', onClick)

  button.tooltip = tooltip
  button:SetText(text)
  button.icon:SetTexture(texture)
  button:SetWidth(button:GetFontString():GetStringWidth() + button.icon:GetWidth()/1.5)
  if coords then
    button.icon:SetTexCoord(unpack(coords))
  end
  SetButtonAnchor(button, self.UIPanels, self.UIPanelAnchor, self.UIPanelGrowth)
  return button
end