BrokerDisplayMap/display.lua

195 lines
7.0 KiB
Lua

local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db
local buttonTypes = {['launcher'] = true, ['data source'] = true}
local objtypes = {["data source"] = 1, ["launcher"] = 2}
local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
local buttons = {}
local count = {
['showText'] = 0,
['noText'] = 0,
}
local buttonSize = 16
local maxRow = 8
local function updateObj(event, objname, key, value, dataobj)
if(not buttons[objname] or not dataobj['type'] or not buttonTypes[strlower(dataobj['type'])]) then return end
buttons[objname][key] = value
if(key == "text" and buttons[objname]['buttontext']) then
if(db[objname]['showText']) then
buttons[objname]['buttontext']:SetText(value)
else
buttons[objname]['buttontext']:SetText("")
end
elseif(key == "iconCoords" and buttons[objname]['button']) then
buttons[objname]['button']['icon']:SetTexCoord(unpack(buttons[objname][key]))
elseif(key == "icon" and buttons[objname]['button']) then
buttons[objname]['button']['icon']:SetTexture(dataobj['icon'])
end
end
local function buttonClick(self, button, down)
if(buttons[self['objname']] and type(buttons[self['objname']]['obj']['OnClick']) == 'function') then
buttons[self['objname']]['obj']['OnClick'](self, button, down)
end
end
local function ttRefresh(self, res)
self['zzRefresh'] = self['zzRefresh'] - res
if(self['zzRefresh']<0) then
self['zzRefresh'] = 0.3
self['tooltip']:ClearLines()
self['dataObject']['OnTooltipShow'](self['tooltip'])
self['tooltip']:Show()
end
end
local function protoOnEnter(objname, dataobj, self, ...)
local tip = GameTooltip
tip:SetOwner(self, "ANCHOR_BOTTOM")
tip:ClearLines()
self['tooltip'] = tip
if(type(dataobj['OnEnter']) == 'function') then
dataobj['OnEnter'](self, ...)
elseif(type(dataobj['OnTooltipShow']) == 'function') then
self['zzRefresh'] = self['zzRefresh'] or 0
self:SetScript("OnUpdate", ttRefresh)
ttRefresh(self, 1)
else
tip:AddLine(objname)
tip:AddLine(dataobj['type'])
end
tip:Show()
end
local function protoOnLeave(objname, dataobj, self)
self:SetScript("OnUpdate", nil)
self['zzRefresh'] = nil
self['tooltip']:Hide()
self['tooltip'] = nil
end
local customDefaults = {
['anchor'] = "CENTER",
['relativTo'] = addonName,
['parentAnchor'] = "TOP",
['parentXOffset'] = 0,
['parentYOffset'] = 0,
}
local function addObj(event, objname, dataobj)
local brokerDefaults = {
['showText'] = (strlower(dataobj['type']) == "data source"),
['active'] = true,
['customTab'] = false,
['custom'] = CopyTable(customDefaults),
}
db[objname] = db[objname] or CopyTable(brokerDefaults)
if(db[objname]['active'] and buttonTypes[strlower(dataobj['type'])]) then
local button, fontstring
local lb = objtypes[strlower(dataobj['type'])]
button = CreateFrame("Button", format("$parent%s", objname), _G[addonName])
button:SetFrameStrata("MEDIUM")
local tex = button:CreateTexture()
tex:SetAllPoints(button)
tex:SetTexture(dataobj['icon'])
if(dataobj['iconCoords']) then
tex:SetTexCoord(unpack(dataobj['iconCoords']))
end
button['icon'] = tex
button:SetWidth(buttonSize); button:SetHeight(buttonSize)
if(db[objname]['customTab']) then
local tab = db[objname]['custom']
button:SetPoint(tab['anchor'] or "TOPLEFT", tab['relativTo'] or addonName, tab['parentAnchor'] or"TOPLEFT", tab['parentXOffset'] or 0, tab['parentYOffset'] or -buttonSize * count[strlower(dataobj['type'])])
else
local l = db[objname]['showText'] and 'showText' or 'noText'
if(l == 'noText') then
button:SetPoint("TOPLEFT", addonName, "TOPLEFT", 12 + buttonSize * floor(count[l] / maxRow), -30 -buttonSize * count[l] + buttonSize * floor(count[l] / maxRow) * maxRow)
else
button:SetPoint("TOPRIGHT", addonName, "TOPRIGHT", -40, -30 -buttonSize * count[l])
end
count[l] = count[l] + 1
end
button:EnableMouse(true)
button:RegisterForClicks("AnyUp")
button['objname'] = objname
button['dataObject'] = dataobj
if(dataobj['OnEnter']) then
button:SetScript("OnEnter", dataobj['OnEnter'])
if(dataobj['OnLeave']) then
button:SetScript("OnLeave", dataobj['OnLeave'])
end
else
button:SetScript("OnEnter", function(self, ...) protoOnEnter(objname, dataobj, self, ...) end)
if(dataobj['OnLeave']) then
button:SetScript("OnLeave", dataobj['OnLeave'])
else
button:SetScript("OnLeave", function(self,...) protoOnLeave(objname, dataobj, self, ...) end)
end
end
button:SetScript("OnClick", buttonClick)
fontstring = button:CreateFontString("$parentText")
fontstring:SetWidth(400); fontstring:SetHeight(buttonSize)
fontstring:SetPoint("TOPLEFT", button, "TOPRIGHT", 0, 0)
fontstring:SetFontObject("GameFontNormalSmall")
fontstring:SetJustifyH("LEFT")
fontstring:SetTextColor(1, 1, 1, 1)
if(db[objname]['showText']) then
fontstring:SetText(dataobj['text'] or dataobj['name'] or "N/A")
end
buttons[objname] = {
['name'] = objname,
['type'] = dataobj['type'],
['obj'] = dataobj,
['icon'] = dataobj['icon'],
['iconCoords'] = dataobj['iconCoords'],
['button'] = button,
['buttontext'] = fontstring,
}
child:updatePrefs(objname, brokerDefaults)
elseif(not db[objname]['active'] and buttonTypes[strlower(dataobj['type'])]) then
child:updatePrefs(objname, brokerDefaults)
end
end
local function listObj()
for objname,dataobj in ldb:DataObjectIterator() do
if(not buttons[objname])then
addObj("added", objname, dataobj)
end
end
end
function child:updateBrokerDisplay()
count['showText'] = 0
count['noText'] = 0
for objname,dataobj in ldb:DataObjectIterator() do
if(not buttons[objname])then
addObj("added",objname, dataobj)
end
local button = buttons[objname] and buttons[objname]['button'] or nil
if(button and db[objname]['active']) then
button:ClearAllPoints()
if(db[objname]['customTab']) then
local tab = db[objname]['custom']
button:SetPoint(tab['anchor'] or "TOPLEFT", tab['relativTo'] or addonName, tab['parentAnchor'] or"TOPLEFT", tab['parentXOffset'] or 0, tab['parentYOffset'] or -buttonSize * count[strlower(dataobj['type'])])
else
local l = db[objname]['showText'] and 'showText' or 'noText'
if(not db[objname]['showText']) then
button:SetPoint("TOPLEFT", addonName, "TOPLEFT", 12 + buttonSize * floor(count[l]/maxRow), -30 -buttonSize * count[l] + buttonSize*floor(count[l]/maxRow)*maxRow)
buttons[objname]['buttontext']:SetText("")
else
button:SetPoint("TOPRIGHT", addonName, "TOPRIGHT", -40, -30 -buttonSize * count[l])
buttons[objname]['buttontext']:SetText(buttons[objname]['text'] or objname)
end
count[l] = count[l] + 1
end
-- updateObj(event, objname, 'text', buttons[objname]['text'], dataobj)
button:Show()
elseif(button) then
button:Hide()
end
end
end
local init = {
Setup = function(self)
db = child['db']
ldb['RegisterCallback'](format("%sNew", addonName), "LibDataBroker_DataObjectCreated", addObj)
ldb['RegisterCallback'](format("%sUpdate", addonName), "LibDataBroker_AttributeChanged", updateObj)
listObj()
end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup')