How to provide a dataobject

tekkub edited this page Sep 14, 2010 · 19 revisions

Before anyone can do anything, we need some data to display. This page provides examples of the two provided data specifications.

A quicklauncher

For our first example, we’ll provide a static dataobject that is simply declared on load and never changed after that. This implements the “Quicklauncher” spec below to create a button to open our config panel.

LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("MyAddonName", {
	type = "launcher",
	icon = "Interface\\Icons\\Spell_Nature_StormReach",
	OnClick = function(clickedframe, button)
		InterfaceOptionsFrame_OpenToFrame(myconfigframe)
	end,
})

Pretty simple eh? A display addon would probably make a button, apply our icon texture to it, and set it’s OnClick handler to our function. When a user clicks that button, we open up the configuration panel for our addon.

A data “feed”

Now why don’t we make a dataobject that implements the “data display” spec. This data can be used by a display addon to create an always-up frame, similar to a FuBar plugin.

local UPDATEPERIOD, elapsed = 0.5, 0
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local dataobj = ldb:NewDataObject("myFPS", {type = "data source", text = "75.0 FPS"})
local f = CreateFrame("frame")

f:SetScript("OnUpdate", function(self, elap)
	elapsed = elapsed + elap
	if elapsed < UPDATEPERIOD then return end

	elapsed = 0
	local fps = GetFramerate()
	dataobj.text = string.format("%.1f FPS", fps)
end)

function dataobj:OnTooltipShow()
	self:AddLine("myFPS")
end

function dataobj:OnEnter()
	GameTooltip:SetOwner(self, "ANCHOR_NONE")
	GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT")
	GameTooltip:ClearLines()
	dataobj.OnTooltipShow(GameTooltip)
	GameTooltip:Show()
end

function dataobj:OnLeave()
	GameTooltip:Hide()
end

Here our dataobject gets updated every half-second with the current FPS. We also provide a few tooltip options. The OnEnter and OnLeave script handlers can be attached to the frame a display addon uses for our data, the user will get a tooltip when they hover over that frame. If the display doesn’t implement that (some like to retain control over how the tooltip is anchored and when it is displayed), we also provide tooltiptext and OnTooltipShow. The display addon can choose which of these it will use, if any. Addons that provide a dynamic tooltip should probably not implement tooltiptext, as constantly refreshing this value will waste a decent amount of memory.