/trunk/DirtyRecruit/libs/LibQTip
local MAJOR = "LibQTip-1.0" |
local MINOR = 29 -- Should be manually increased |
local MINOR = 22 -- Should be manually increased |
assert(LibStub, MAJOR.." requires LibStub") |
local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR) |
local AcquireTable, ReleaseTable |
local InitializeTooltip, SetTooltipSize, ResetTooltipSize, LayoutColspans |
local SetFrameScript, ClearFrameScripts |
------------------------------------------------------------------------------ |
-- Cache debugging. |
frame:SetParent(nil) |
frame:ClearAllPoints() |
frame:SetBackdrop(nil) |
ClearFrameScripts(frame) |
tinsert(frameHeap, frame) |
--[===[@debug@ |
usedFrames = usedFrames - 1 |
self.fontString:SetFontObject(GameTooltipText) |
end |
function labelPrototype:SetupCell(tooltip, value, justification, font, l_pad, r_pad, max_width, min_width, ...) |
function labelPrototype:SetupCell(tooltip, value, justification, font, ...) |
local fs = self.fontString |
fs:SetFontObject(font or tooltip:GetFont()) |
fs:SetJustifyH(justification) |
fs:SetText(tostring(value)) |
-- Variable argument checking |
local l_pad, r_pad, max_width, min_width |
local i, arg = 1, ... |
if arg == nil or type(arg) == "number" then |
i, l_pad, arg = i + 1, select(i, ...) |
end |
l_pad = l_pad or 0 |
if arg == nil or type(arg) == "number" then |
i, r_pad, arg = i + 1, select(i, ...) |
end |
r_pad = r_pad or 0 |
if arg == nil or type(arg) == "number" then |
i, max_width, arg = i + 1, select(i, ...) |
end |
if arg == nil or type(arg) == "number" then |
i, min_width = (i + 1), arg |
end |
-- Use GetHeight() instead of GetStringHeight() so lines which are longer than width will wrap. |
local height = fs:GetHeight() |
local width = fs:GetStringWidth() + l_pad + r_pad |
fs:SetPoint("TOPLEFT", self, "TOPLEFT", l_pad, 0) |
if max_width and min_width and (max_width < min_width) then |
error("maximum width cannot be lower than minimum width: "..tostring(max_width).." < "..tostring(min_width), 2) |
end |
if min_width and width < min_width then width = min_width end |
if max_width and max_width < width then width = max_width end |
fs:SetWidth(width) |
if min_width and width < min_width then width = min_width end |
if max_width and (max_width < width) then |
width = max_width |
fs:SetWidth(width) |
height = fs:GetHeight() |
end |
fs:Show() |
-- Use GetHeight() instead of GetStringHeight() so lines which are longer than width will wrap. |
return width, fs:GetHeight() |
return width, height |
end |
function labelPrototype:GetPosition() return self._line, self._column end |
function AcquireCell(tooltip, provider) |
local cell = provider:AcquireCell(tooltip) |
cell:SetParent(tooltip.scrollChild) |
cell:SetFrameLevel(tooltip.scrollChild:GetFrameLevel() + 3) |
cell:SetFrameLevel(tooltip.scrollChild:GetFrameLevel() + 1) |
cell._provider = provider |
return cell |
end |
cell:Hide() |
cell:ClearAllPoints() |
cell:SetParent(nil) |
cell:SetBackdrop(nil) |
ClearFrameScripts(cell) |
cell._font, cell._justification, cell._colSpan, cell._line, cell._column = nil |
cell._provider:ReleaseCell(cell) |
tooltip:SetBackdropColor(GameTooltip:GetBackdropColor()) |
tooltip:SetBackdropBorderColor(GameTooltip:GetBackdropBorderColor()) |
tooltip:SetScale(GameTooltip:GetScale()) |
tooltip:SetAlpha(1) |
tooltip:SetAlpha(GameTooltip:GetAlpha()) |
tooltip:SetFrameStrata("TOOLTIP") |
tooltip:SetClampedToScreen(false) |
checkJustification(justification, 2) |
local colNum = #self.columns + 1 |
local column = self.columns[colNum] or AcquireFrame(self.scrollChild) |
column:SetFrameLevel(self.scrollChild:GetFrameLevel() + 1) |
local column = self.columns[colNum] or AcquireFrame(self) |
column:SetParent(self.scrollChild) |
column.justification = justification |
column.width = 0 |
column:SetWidth(1) |
-- all data is in the tooltip; fix colspan width and prevent the layout cleaner from messing up the tooltip later |
LayoutColspans(self) |
layoutCleaner.registry[self] = nil |
local topside = self:GetTop() |
local bottomside = self:GetBottom() |
local screensize = UIParent:GetHeight() |
error("column layout should be defined before adding line", 3) |
end |
local lineNum = #tooltip.lines + 1 |
local line = tooltip.lines[lineNum] or AcquireFrame(tooltip.scrollChild) |
line:SetFrameLevel(tooltip.scrollChild:GetFrameLevel() + 2) |
local line = tooltip.lines[lineNum] or AcquireFrame(tooltip) |
line:SetPoint('LEFT', tooltip.scrollChild) |
line:SetPoint('RIGHT', tooltip.scrollChild) |
if lineNum > 1 then |
return lineNum, colNum |
end |
function tipPrototype:SetCellColor(lineNum, colNum, r, g, b, a) |
local cell = self.lines[lineNum].cells[colNum] |
if cell then |
local sr, sg, sb, sa = self:GetBackdropColor() |
cell:SetBackdrop(GenericBackdrop) |
cell:SetBackdropColor(r or sr, g or sg, b or sb, a or sa) |
end |
end |
function tipPrototype:SetColumnColor(colNum, r, g, b, a) |
local column = self.columns[colNum] |
if column then |
local sr, sg, sb, sa = self:GetBackdropColor() |
column:SetBackdrop(GenericBackdrop) |
column:SetBackdropColor(r or sr, g or sg, b or sb, a or sa) |
if type(colNum) ~= "number" then |
error("column number must be a number, not: "..tostring(colNum), 2) |
elseif colNum < 1 or colNum > #self.columns then |
error("column number out of range: "..tostring(colNum), 2) |
end |
local column = self.columns[colNum] |
local sr, sg, sb, sa = self:GetBackdropColor() |
column:SetBackdrop(GenericBackdrop) |
column:SetBackdropColor(r or sr, g or sg, b or sb, a or sa) |
end |
function tipPrototype:SetLineColor(lineNum, r, g, b, a) |
local line = self.lines[lineNum] |
if line then |
local sr, sg, sb, sa = self:GetBackdropColor() |
line:SetBackdrop(GenericBackdrop) |
line:SetBackdropColor(r or sr, g or sg, b or sb, a or sa) |
if type(lineNum) ~= "number" then |
error("line number must be a number, not: "..tostring(lineNum), 2) |
elseif lineNum < 1 or lineNum > #self.lines then |
error("line number out of range: "..tostring(lineNum), 2) |
end |
local line = self.lines[lineNum] |
local sr, sg, sb, sa = self:GetBackdropColor() |
line:SetBackdrop(GenericBackdrop) |
line:SetBackdropColor(r or sr, g or sg, b or sb, a or sa) |
end |
-- TODO: fixed argument positions / remove checks for performance? |
function tipPrototype:SetCell(lineNum, colNum, value, ...) |
-- Mandatory argument checking |
if type(lineNum) ~= "number" then |
function tipPrototype:GetColumnCount() return #self.columns end |
------------------------------------------------------------------------------ |
-- Frame Scripts |
------------------------------------------------------------------------------ |
local highlight = CreateFrame("Frame", nil, UIParent) |
highlight:SetFrameStrata("TOOLTIP") |
highlight:Hide() |
highlight._texture = highlight:CreateTexture(nil, "OVERLAY") |
highlight._texture:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight") |
highlight._texture:SetBlendMode("ADD") |
highlight._texture:SetAllPoints(highlight) |
local scripts = { |
OnEnter = function(frame, ...) |
highlight:SetParent(frame) |
highlight:SetAllPoints(frame) |
highlight:Show() |
if frame._OnEnter_func then |
frame:_OnEnter_func(frame._OnEnter_arg, ...) |
end |
end, |
OnLeave = function(frame, ...) |
highlight:Hide() |
highlight:ClearAllPoints() |
highlight:SetParent(nil) |
if frame._OnLeave_func then |
frame:_OnLeave_func(frame._OnLeave_arg, ...) |
end |
end, |
OnMouseDown = function(frame, ...) |
frame:_OnMouseDown_func(frame._OnMouseDown_arg, ...) |
end, |
OnMouseUp = function(frame, ...) |
frame:_OnMouseUp_func(frame._OnMouseUp_arg, ...) |
end, |
} |
function SetFrameScript(frame, script, func, arg) |
if not scripts[script] then |
return |
end |
frame["_"..script.."_func"] = func |
frame["_"..script.."_arg"] = arg |
if script == "OnMouseDown" or script == "OnMouseUp" then |
if func then |
frame:SetScript(script, scripts[script]) |
else |
frame:SetScript(script, nil) |
end |
end |
-- if at least one script is set, set the OnEnter/OnLeave scripts for the highlight |
if frame._OnEnter_func or frame._OnLeave_func or frame._OnMouseDown_func or frame._OnMouseUp_func then |
frame:EnableMouse(true) |
frame:SetScript("OnEnter", scripts.OnEnter) |
frame:SetScript("OnLeave", scripts.OnLeave) |
else |
frame:EnableMouse(false) |
frame:SetScript("OnEnter", nil) |
frame:SetScript("OnLeave", nil) |
end |
end |
function ClearFrameScripts(frame) |
if frame._OnEnter_func or frame._OnLeave_func or frame._OnMouseDown_func or frame._OnMouseUp_func then |
frame:EnableMouse(false) |
frame:SetScript("OnEnter", nil) |
frame._OnEnter_func = nil |
frame._OnEnter_arg = nil |
frame:SetScript("OnLeave", nil) |
frame._OnLeave_func = nil |
frame._OnLeave_arg = nil |
frame:SetScript("OnMouseDown", nil) |
frame._OnMouseDown_func = nil |
frame._OnMouseDown_arg = nil |
frame:SetScript("OnMouseUp", nil) |
frame._OnMouseUp_func = nil |
frame._OnMouseUp_arg = nil |
end |
end |
function tipPrototype:SetLineScript(lineNum, script, func, arg) |
SetFrameScript(self.lines[lineNum], script, func, arg) |
end |
function tipPrototype:SetColumnScript(colNum, script, func, arg) |
SetFrameScript(self.columns[colNum], script, func, arg) |
end |
function tipPrototype:SetCellScript(lineNum, colNum, script, func, arg) |
local cell = self.lines[lineNum].cells[colNum] |
if cell then |
SetFrameScript(cell, script, func, arg) |
end |
end |
------------------------------------------------------------------------------ |
-- Auto-hiding feature |
------------------------------------------------------------------------------ |
-- Script of the auto-hiding child frame |
local function AutoHideTimerFrame_OnUpdate(self, elapsed) |
self.checkElapsed = self.checkElapsed + elapsed |
if self.checkElapsed > 0.1 then |
if MouseIsOver(self.parent) or (self.alternateFrame and MouseIsOver(self.alternateFrame)) then |
self.elapsed = 0 |
else |
self.elapsed = self.elapsed + self.checkElapsed |
if self.elapsed >= self.delay then |
lib:Release(self.parent) |
end |
if MouseIsOver(self:GetParent()) or (self.alternateFrame and MouseIsOver(self.alternateFrame)) then |
self.elapsed = 0 |
else |
self.elapsed = self.elapsed + elapsed |
if self.elapsed > self.delay then |
lib:Release(self:GetParent()) |
end |
self.checkElapsed = 0 |
end |
end |
-- :SetAutoHideDelay(0.25, someFrame) => hides after 0.25sec outside of both the tooltip and someFrame |
-- :SetAutoHideDelay() => disable auto-hiding (default) |
function tipPrototype:SetAutoHideDelay(delay, alternateFrame) |
local timerFrame = self.autoHideTimerFrame |
delay = tonumber(delay) or 0 |
local timerFrame = self.autoHideTimerFrame |
if delay > 0 then |
if not timerFrame then |
timerFrame = AcquireFrame(self) |
timerFrame:SetScript("OnUpdate", AutoHideTimerFrame_OnUpdate) |
self.autoHideTimerFrame = timerFrame |
end |
timerFrame.parent = self |
timerFrame.checkElapsed = 0 |
timerFrame.elapsed = 0 |
timerFrame.delay = delay |
timerFrame.alternateFrame = alternateFrame |