View Single Post
01-31-12, 04:38 PM   #21
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
That ensures the file it's included in has an upvalued version of the name and the shared table. Personally, I prefer NOT putting that table in the global space and instead using it as a shared private namespace.
How do you do that? Originally my goal was to setup a core file that i could put functions in that would be later used in other files as well as setup a handful of things.

Also i have my options and settings setup for LOD thanks to Vrul so i needed functions in my files to not only work within the addon but as well as with the LOD options files. If its made a private name space will the LOD config still be able to pull the functions?

This is currently what my core looks like...
Code:
local addonName, addon = ...
_G[addonName] = addon

--[[-----------------------------------------------------------------------------
Event handling - Use one frame to process multiple individual OnEvent needs
-------------------------------------------------------------------------------]]
do
	local frame, select, next, events = CreateFrame('Frame'), select, pairs({ })
	--local frame, select, next, events = CreateFrame('Frame'), select, next, {} -- maybe a fix? but no...
	local register, unregister = frame.RegisterEvent, frame.UnregisterEvent
	frame:Hide()

	frame:SetScript('OnEvent', function(self, event, ...)
		for reference, func in next, events[event], nil do
			func(reference, event, ...)
		end
	end)

	function addon.RegisterEvent(reference, event, func)
		if not events[event] then
			events[event] = { }
			register(frame, event)
		end
		events[event][reference] = func
	end

	function addon.RegisterEvents(reference, func, ...)
		local event
		for index = 1, select('#', ...) do
			event = select(index, ...)
			if not events[event] then
				events[event] = { }
				register(frame, event)
			end
			events[event][reference] = func
		end
	end

	function addon.UnregisterEvent(reference, event)
		if events[event] then
			events[event][reference] = nil
			if not next(events[event]) then
				events[event] = nil
				unregister(frame, event)
			end
		end
	end

	function addon.UnregisterAllEvents(reference)
		for event, registry in next, events, nil do
			registry[reference] = nil
			if not next(registry) then
				events[event] = nil
				unregister(frame, event)
			end
		end
	end
end

--[[-----------------------------------------------------------------------------
SafeCall - Queue a method of addon to run once combat ends if needed
-------------------------------------------------------------------------------]]
do
	local combatLockdown, queue = InCombatLockdown(), { }

	addon.RegisterEvent("Core-SafeCall-EnterCombat", 'PLAYER_REGEN_DISABLED', function(self)
		combatLockdown = true
	end)

	addon.RegisterEvent("Core-SafeCall-LeaveCombat", 'PLAYER_REGEN_ENABLED', function(self)
		combatLockdown = nil
		for method, arg in pairs(queue) do
			addon[method](addon, arg)
			queue[method] = nil
		end
	end)

	function addon:SafeCall(method, arg)
		if combatLockdown then
			queue[method] = arg or false
		else
			addon[method](addon, arg)
		end
	end
end

--[[-----------------------------------------------------------------------------
Localized class name to file class name look up table
-------------------------------------------------------------------------------]]
local CLASS = { }
for fileName, localName in pairs(LOCALIZED_CLASS_NAMES_MALE) do
	CLASS[localName] = fileName
end
for fileName, localName in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do
	CLASS[localName] = fileName
end
addon.CLASS = CLASS

--[[-----------------------------------------------------------------------------
MoneyToString - Get formatted text from an ammount of copper
-------------------------------------------------------------------------------]]
function addon:MoneyToString(ammount, forceIcons)
	if type(ammount) ~= 'number' then
		ammount = 0
	end
	local cu = ammount % 100
	ammount = floor(ammount / 100)
	local ag, au = ammount % 100, floor(ammount / 100)
	local cuInd, agInd, auInd
	if GetCVarBool('showChatIcons') or forceIcons then
		cuInd = [[|TInterface\MoneyFrame\UI-CopperIcon:0|t]]
		agInd = [[|TInterface\MoneyFrame\UI-SilverIcon:0|t ]]
		auInd = [[|TInterface\MoneyFrame\UI-GoldIcon:0|t ]]
	else
		cuInd = "|cffeda55fc|r"
		agInd = "|cffc7c7cfs|r "
		auInd = "|cffffd700g|r "
	end
	local string = ""
	if au > 0 then
		string = au .. auInd .. ag .. agInd
	elseif ag > 0 then
		string = ag .. agInd
	end
	return string .. cu .. cuInd
end

--[[-----------------------------------------------------------------------------
DoNothing - A simple dummie/do nothing function
-------------------------------------------------------------------------------]]
local function DoNothing()
end
addon.DoNothing = DoNothing

--[[-----------------------------------------------------------------------------
SetUIScale - Set UI Scale to its smallest size.
-------------------------------------------------------------------------------]]
function addon:SetUIScale()
	SetCVar("useUiScale", 1)
	SetCVar( "uiScale", 0.63999998569489);
end

--[[-----------------------------------------------------------------------------
HideTooltip - Hide GameTooltip (several frames use this)
-------------------------------------------------------------------------------]]
function addon:HideTooltip()
	GameTooltip:Hide()
	self.tooltip = nil
end

--[[-----------------------------------------------------------------------------
HideFrame - Hide a frame, prevents it from being reshown
-------------------------------------------------------------------------------]]
function addon:HideFrame(reference)
	local frame = type(reference) == 'string' and _G[reference] or reference
	if type(frame) ~= 'table' then return end
	frame.Show = DoNothing
	frame:Hide()
end

--[[-----------------------------------------------------------------------------
ShowFrame - Show a frame, undoing :HideFrame behavior
-------------------------------------------------------------------------------]]
function addon:ShowFrame(reference)
	local frame = type(reference) == 'string' and _G[reference] or reference
	if type(frame) ~= 'table' then return end
	frame.Show = nil
	frame:Show()
end

--[[-----------------------------------------------------------------------------
LockFrame - Lock a frame, prevents SetPoint and ClearAllPoints from working
-------------------------------------------------------------------------------]]
function addon:LockFrame(reference)
	local frame = type(reference) == 'string' and _G[reference] or reference
	if type(frame) ~= 'table' then return end
	frame.SetPoint = DoNothing
	frame.ClearAllPoints = DoNothing
end

--[[-----------------------------------------------------------------------------
UnlockFrame - Undo :LockFrame
-------------------------------------------------------------------------------]]
function addon:UnlockFrame(reference)
	local frame = type(reference) == 'string' and _G[reference] or reference
	if type(frame) ~= 'table' then return end
	frame.SetPoint = nil
	frame.ClearAllPoints = nil
end

--[[-----------------------------------------------------------------------------
RunSlashCmd - This no longer functions correctly as of patch 4.3
-------------------------------------------------------------------------------]]
local _G = _G
function addon:RunSlashCmd(cmd)
  local slash, rest = cmd:match("^(%S+)%s*(.-)$")
  for name, func in pairs(SlashCmdList) do
     local i, slashCmd = 1
     repeat
        slashCmd, i = _G["SLASH_"..name..i], i + 1
        if slashCmd == slash then
           return true, func(rest)
        end
     until not slashCmd
  end
  -- Okay, so it's not a slash command. It may also be an emote.
  local i = 1
  while _G["EMOTE" .. i .. "_TOKEN"] do
     local j, cn = 2, _G["EMOTE" .. i .. "_CMD1"]
     while cn do
        if cn == slash then
           return true, DoEmote(_G["EMOTE" .. i .. "_TOKEN"], rest);
        end
        j, cn = j+1, _G["EMOTE" .. i .. "_CMD" .. j]
     end
     i = i + 1
  end
end 

--[[-----------------------------------------------------------------------------
GetSlashFunc - returns a slash command function on success or an informative error function on failure.
-------------------------------------------------------------------------------]]
function addon:GetSlashFunc(cmd)
	if not cmd then
		return function(cmd) print("You must supply a command.") end
	end
	if cmd:sub(1, 1) ~= "/" then
		cmd = "/" .. cmd
	end
	for id, val in pairs(_G) do
		if id:sub(1, 5) == "SLASH" and val == cmd then
			local slashID = id:match("SLASH_(%a*)%d*")
			return SlashCmdList[slashID]
		end
	end
	-- Didn't find one?
	return function(cmd) print(cmd, "doesn't exist.") end
end

--[[-----------------------------------------------------------------------------
Decimal Truncate Function
-------------------------------------------------------------------------------]]
function addon:truncate(number, decimals)
    return number - (number % (0.1 ^ decimals))
end

--[[-----------------------------------------------------------------------------
Class/Reaction type Coloring
-------------------------------------------------------------------------------]]
do 
	local format = string.format
	GCHex = function(color)
		return format('|cff%02x%02x%02x', color.r * 255, color.g * 255, color.b * 255)
	end
end
function addon:unitColor(unit)
	local color = { r=1, g=1, b=1 }
	if UnitIsPlayer(unit) then
		local _, class = UnitClass(unit)
		color = RAID_CLASS_COLORS[class]
		return color
	else
		local reaction = UnitReaction(unit, "player")
		if reaction then
			color = FACTION_BAR_COLORS[reaction]
			return color
		end
	end
	return color
end

--[[-----------------------------------------------------------------------------
Durability % Coloring
-------------------------------------------------------------------------------]]
local tmpString = ""
function addon:DurColor(percent)
	if percent >= 80 then
		tmpString = "|cff00FF00"
	elseif percent >= 60 then
		tmpString = "|cff99FF00"
	elseif percent >= 40 then
		tmpString = "|cffFFFF00"
	elseif percent >= 20 then
		tmpString = "|cffFF9900"
	elseif percent >= 0 then
		tmpString = "|cffFF2000"
	end
	return tmpString;
end

--[[-----------------------------------------------------------------------------
Delay a function
-------------------------------------------------------------------------------]]
local waitTable = {};
local waitFrame = nil;
function addon:FuncDelay(delay, func, ...)
  if(type(delay)~="number" or type(func)~="function") then
    return false;
  end
  if(waitFrame == nil) then
    waitFrame = CreateFrame("Frame","WaitFrame", UIParent);
    waitFrame:SetScript("onUpdate",function (self,elapse)
      local count = #waitTable;
      local i = 1;
      while(i<=count) do
        local waitRecord = tremove(waitTable,i);
        local d = tremove(waitRecord,1);
        local f = tremove(waitRecord,1);
        local p = tremove(waitRecord,1);
        if(d>elapse) then
          tinsert(waitTable,i,{d-elapse,f,p});
          i = i + 1;
        else
          count = count - 1;
          f(unpack(p));
        end
      end
    end);
  end
  tinsert(waitTable,{delay,func,{...}});
  return true;
end

--[[-----------------------------------------------------------------------------
Blizzard Build comparisonn for ptr/beta/live compatible versions
-------------------------------------------------------------------------------]]
function addon:CompareBuild(comver, combuild, comref, lowcomref, highcomref)
	local version, build, bdate, toc = GetBuildInfo()
	if version > comver and build > combuild then
		addon[highcomref]()
	elseif version < comver and build < combuild then
		addon[lowcomref]()
	elseif version == comver and build == combuild then
		addon[comref]()
	end
end

--[[-----------------------------------------------------------------------------
Reload Frame
-------------------------------------------------------------------------------]]
function addon:OpenReloadFrame(myfunc)
	local reloadFrame
	if(reloadFrame) then
		return reloadFrame:Show()
	end

	reloadFrame = CreateFrame("Frame", nil, UIParent)
	reloadFrame:SetHeight(160)
	reloadFrame:SetWidth(350)
	reloadFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 200)
	reloadFrame:SetBackdrop{
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
		edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
		tile = true,
		tileSize = 32,
		edgeSize = 32,
		insets = {
			left = 11,
			right = 12,
			top = 12,
			bottom = 11
		}
	}

	local text = reloadFrame:CreateFontString(nil, "ARTWORK", "GameFontNormalHuge")
	text:SetText("GrimUI: Reload")
	text:SetPoint("TOP", 0, -20)

	local text2 = reloadFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	text2:SetText("GrimUI requires a reload\n to finish its configuration.")
	text2:SetPoint("TOP", 0, -60)

	local button1 = CreateFrame("Button", "okay", reloadFrame, "UIPanelButtonTemplate")
	button1:SetHeight(45)
	button1:SetWidth(125)
	button1:SetPoint("BOTTOMLEFT", 15, 15)
	button1:SetText("Reload")
	button1:RegisterForClicks("AnyUp")
	button1:SetScript("OnClick", myfunc)

	local button2 = CreateFrame("Button", "cancel", reloadFrame, "UIPanelButtonTemplate")
	button2:SetHeight(45)
	button2:SetWidth(125)
	button2:SetPoint("BOTTOMRIGHT", -15, 15)
	button2:SetText("Cancel")
	button2:RegisterForClicks("AnyUp")
	button2:SetScript("OnClick", function() reloadFrame:Hide() end)
end

--[[-----------------------------------------------------------------------------
LibSharedMedia support
-------------------------------------------------------------------------------]]
local LSM = LibStub('LibSharedMedia-3.0')
LSM:Register('background', addonName, [[Interface\Addons\]] .. addonName .. [[\Media\MainBG]])
LSM:Register('font', "Franklin Gothing Medium", [[Interface\Addons\]] .. addonName .. [[\Media\Font1.ttf]])
LSM:Register('font', "Comic Sans MS", [[Interface\Addons\]] .. addonName .. [[\Media\Font3.ttf]])

--[[-----------------------------------------------------------------------------
Localization support. AceLocale
-------------------------------------------------------------------------------]]
local AceLocale = LibStub("AceLocale-3.0")
local L = AceLocale:GetLocale( "GrimUI", true )
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote