initial commit
This commit is contained in:
126
LibTSM/UI/Tooltip.lua
Normal file
126
LibTSM/UI/Tooltip.lua
Normal file
@@ -0,0 +1,126 @@
|
||||
-- ------------------------------------------------------------------------------ --
|
||||
-- TradeSkillMaster --
|
||||
-- https://tradeskillmaster.com --
|
||||
-- All Rights Reserved - Detailed license information included with addon. --
|
||||
-- ------------------------------------------------------------------------------ --
|
||||
|
||||
--- UI tooltip functions.
|
||||
-- @module Tooltip
|
||||
|
||||
local _, TSM = ...
|
||||
local Tooltip = TSM.Init("UI.Tooltip")
|
||||
local ItemString = TSM.Include("Util.ItemString")
|
||||
local Vararg = TSM.Include("Util.Vararg")
|
||||
local Event = TSM.Include("Util.Event")
|
||||
local ItemInfo = TSM.Include("Service.ItemInfo")
|
||||
local private = {
|
||||
currentParent = nil,
|
||||
registeredEvent = false,
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- Module Functions
|
||||
-- ============================================================================
|
||||
|
||||
--- Shows a tooltip which is anchored to the frame.
|
||||
-- @tparam Frame parent The parent and anchor frame for the tooltip
|
||||
-- @tparam ?number|string|function data The tooltip information which can be either an itemId,
|
||||
-- itemString, raw text string, or function which returns one of the other options
|
||||
-- @tparam ?boolean noWrapping Disables wrapping of text lines
|
||||
-- @tparam[opt=0] number xOffset An extra x offset to apply to the anchor of the tooltip
|
||||
function Tooltip.Show(parent, data, noWrapping, xOffset)
|
||||
if not data then
|
||||
return
|
||||
elseif type(data) == "function" then
|
||||
local funcNoWrapping, funcXOffset = false, 0
|
||||
data, funcNoWrapping, funcXOffset = data()
|
||||
noWrapping = noWrapping or funcNoWrapping
|
||||
xOffset = xOffset or funcXOffset
|
||||
end
|
||||
local showCompare = false
|
||||
GameTooltip:SetOwner(parent, "ANCHOR_PRESERVE")
|
||||
GameTooltip:ClearAllPoints()
|
||||
GameTooltip:SetPoint("LEFT", parent, "RIGHT", xOffset or 0, 0)
|
||||
if type(data) == "number" then
|
||||
GameTooltip:SetHyperlink("item:"..data)
|
||||
showCompare = true
|
||||
elseif type(data) == "string" and strfind(data, "^craft:") then
|
||||
data = strmatch(data, "craft:(%d+)")
|
||||
GameTooltip:SetCraftSpell(tonumber(data))
|
||||
elseif type(data) == "string" and strfind(data, "^enchant:") then
|
||||
GameTooltip:SetHyperlink(data)
|
||||
elseif type(data) == "string" and strfind(data, "^currency:") then
|
||||
GameTooltip:SetCurrencyByID(strmatch(data, "currency:(%d+)"))
|
||||
elseif type(data) == "string" and (strfind(data, "^\124c.+\124Hitem:") or ItemString.IsItem(data)) then
|
||||
GameTooltip:SetHyperlink(ItemInfo.GetLink(data))
|
||||
showCompare = true
|
||||
elseif type(data) == "string" and (strfind(data, "^\124c.+\124Hbattlepet:") or ItemString.IsPet(data)) then
|
||||
if strmatch(data, "p:") then
|
||||
data = ItemInfo.GetLink(data)
|
||||
end
|
||||
local _, speciesID, level, breedQuality, maxHealth, power, speed = strsplit(":", data)
|
||||
BattlePetToolTip_Show(tonumber(speciesID), tonumber(level) or 0, tonumber(breedQuality) or 0, tonumber(maxHealth) or 0, tonumber(power) or 0, tonumber(speed) or 0, gsub(gsub(data, "^(.*)%[", ""), "%](.*)$", ""))
|
||||
else
|
||||
for _, line in Vararg.Iterator(strsplit("\n", data)) do
|
||||
local textLeft, textRight = strsplit(TSM.CONST.TOOLTIP_SEP, line)
|
||||
if textRight then
|
||||
GameTooltip:AddDoubleLine(textLeft, textRight, 1, 1, 1, 1, 1, 1)
|
||||
else
|
||||
GameTooltip:AddLine(textLeft, 1, 1, 1, not noWrapping)
|
||||
end
|
||||
end
|
||||
end
|
||||
GameTooltip:Show()
|
||||
private.currentParent = parent
|
||||
if showCompare then
|
||||
assert(not private.registeredEvent)
|
||||
private.registeredEvent = true
|
||||
Event.Register("MODIFIER_STATE_CHANGED", private.UpdateCompareState)
|
||||
private.UpdateCompareState()
|
||||
end
|
||||
end
|
||||
|
||||
--- Hides the current tooltip.
|
||||
function Tooltip.Hide()
|
||||
if private.registeredEvent then
|
||||
Event.Unregister("MODIFIER_STATE_CHANGED", private.UpdateCompareState)
|
||||
private.registeredEvent = false
|
||||
end
|
||||
private.currentParent = nil
|
||||
GameTooltip:SetOwner(UIParent, "ANCHOR_PRESERVE")
|
||||
GameTooltip:ClearAllPoints()
|
||||
GameTooltip:SetPoint("CENTER")
|
||||
GameTooltip:Hide()
|
||||
if not TSM.IsWowClassic() then
|
||||
BattlePetTooltip:ClearAllPoints()
|
||||
BattlePetTooltip:SetPoint("CENTER")
|
||||
BattlePetTooltip:Hide()
|
||||
end
|
||||
end
|
||||
|
||||
--- Checks if the tooltip is visible.
|
||||
-- @tparam[opt=nil] table frame An optional parent frame to check against
|
||||
-- @treturn boolean Whether or not the tooltip is visible
|
||||
function Tooltip.IsVisible(frame)
|
||||
if frame then
|
||||
return private.currentParent == frame
|
||||
else
|
||||
return private.currentParent and true or false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- Private Helper Functions
|
||||
-- ============================================================================
|
||||
|
||||
function private.UpdateCompareState()
|
||||
if private.currentParent and GameTooltip:IsVisible() and IsShiftKeyDown() and not GameTooltip:IsEquippedItem() then
|
||||
GameTooltip_ShowCompareItem(GameTooltip)
|
||||
else
|
||||
GameTooltip_HideShoppingTooltips(GameTooltip)
|
||||
end
|
||||
end
|
||||
139
LibTSM/UI/UIElements.lua
Normal file
139
LibTSM/UI/UIElements.lua
Normal file
@@ -0,0 +1,139 @@
|
||||
-- ------------------------------------------------------------------------------ --
|
||||
-- TradeSkillMaster --
|
||||
-- https://tradeskillmaster.com --
|
||||
-- All Rights Reserved - Detailed license information included with addon. --
|
||||
-- ------------------------------------------------------------------------------ --
|
||||
|
||||
--- UI element functions.
|
||||
-- @module UIElements
|
||||
|
||||
local _, TSM = ...
|
||||
local UIElements = TSM.Init("UI.UIElements")
|
||||
local ObjectPool = TSM.Include("Util.ObjectPool")
|
||||
local Table = TSM.Include("Util.Table")
|
||||
local private = {
|
||||
elementClasses = {},
|
||||
objectPools = {},
|
||||
namedElements = {},
|
||||
activeFrameElementMap = {},
|
||||
debugNameElementLookup = {},
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- Module Functions
|
||||
-- ============================================================================
|
||||
|
||||
|
||||
--- Registers a UI Element subclass.
|
||||
-- @tparam Element class The element subclass
|
||||
function UIElements.Register(class)
|
||||
private.elementClasses[class.__name] = class
|
||||
end
|
||||
|
||||
--- Creates a new UI element.
|
||||
-- @tparam string elementType The name of the element class
|
||||
-- @tparam string id The id to assign to the element
|
||||
-- @param ... Tags to set on the element
|
||||
-- @treturn Element The created UI element object
|
||||
function UIElements.New(elementType, id, ...)
|
||||
return private.NewElementHelper(elementType, id, nil, ...)
|
||||
end
|
||||
|
||||
--- Creates a new named UI element.
|
||||
-- @tparam string elementType The name of the element class
|
||||
-- @tparam string id The id to assign to the element
|
||||
-- @tparam string name The global name of the element
|
||||
-- @treturn Element The created UI element object
|
||||
function UIElements.NewNamed(elementType, id, name, ...)
|
||||
assert(name)
|
||||
return private.NewElementHelper(elementType, id, name, ...)
|
||||
end
|
||||
|
||||
--- Recycles a UI element.
|
||||
-- @tparam Element element The UI element object
|
||||
function UIElements.Recycle(element)
|
||||
private.activeFrameElementMap[element:_GetBaseFrame()] = nil
|
||||
if not Table.KeyByValue(private.namedElements, element) then
|
||||
private.objectPools[element.__class]:Recycle(element)
|
||||
end
|
||||
end
|
||||
|
||||
--- Gets a UI element by its frame (for TSM's frame stack).
|
||||
-- @tparam table frame The frame
|
||||
-- @treturn ?Element The element or nil if the frame doesn't correspond to an element
|
||||
function UIElements.GetByFrame(frame)
|
||||
return private.activeFrameElementMap[frame]
|
||||
end
|
||||
|
||||
--- Creates a WoW UI object and tracks it for easier debugging.
|
||||
-- @tparam Element element The TSM UI element object
|
||||
-- @tparam string frameType The type of the WoW UI object
|
||||
-- @tparam ?string frameName The global name
|
||||
-- @tparam ?table parentFrame The parent WoW UI object
|
||||
-- @tparam ?string inheritsFrame A WoW UI template to inherit from
|
||||
-- @treturn table The WoW UI object
|
||||
function UIElements.CreateFrame(element, frameType, frameName, parentFrame, inheritsFrame)
|
||||
local isNamed = frameName and true or false
|
||||
if not frameName then
|
||||
-- generate a debug name to aid in later lookup
|
||||
frameName = private.GetDebugName(element)
|
||||
end
|
||||
private.debugNameElementLookup[frameName] = element
|
||||
local frame = CreateFrame(frameType, frameName, parentFrame, inheritsFrame)
|
||||
if not isNamed then
|
||||
_G[frameName] = nil
|
||||
end
|
||||
return frame
|
||||
end
|
||||
|
||||
--- Creates a WoW UI font string and tracks it for easier debugging.
|
||||
-- @tparam Element element The TSM UI element object
|
||||
-- @tparam table parentFrame The parent WoW UI frame
|
||||
-- @treturn table The WoW UI font string
|
||||
function UIElements.CreateFontString(element, parentFrame)
|
||||
local name = private.GetDebugName(element)
|
||||
private.debugNameElementLookup[name] = element
|
||||
local fontString = parentFrame:CreateFontString(name)
|
||||
_G[name] = nil
|
||||
return fontString
|
||||
end
|
||||
|
||||
--- Gets the debug name translations.
|
||||
-- @tparam table result The result table
|
||||
function UIElements.GetDebugNameTranslation(result)
|
||||
for name, element in pairs(private.debugNameElementLookup) do
|
||||
result[name] = "<"..tostring(element)..">"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- ============================================================================
|
||||
-- Private Helper Functions
|
||||
-- ============================================================================
|
||||
|
||||
function private.NewElementHelper(elementType, id, name, ...)
|
||||
local class = private.elementClasses[elementType]
|
||||
local element = nil
|
||||
if name then
|
||||
private.namedElements[name] = private.namedElements[name] or class(name)
|
||||
element = private.namedElements[name]
|
||||
assert(_G[name] == element:_GetBaseFrame())
|
||||
else
|
||||
if not private.objectPools[class] then
|
||||
private.objectPools[class] = ObjectPool.New("UI_"..class.__name, class, 1)
|
||||
end
|
||||
element = private.objectPools[class]:Get()
|
||||
end
|
||||
private.activeFrameElementMap[element:_GetBaseFrame()] = element
|
||||
element:SetId(id)
|
||||
element:SetTags(...)
|
||||
element:Acquire()
|
||||
return element
|
||||
end
|
||||
|
||||
function private.GetDebugName(element)
|
||||
return "TSM_UI_ELEMENT:"..element.__class.__name..":"..format("%06x", random(0, 2 ^ 24 - 1))
|
||||
end
|
||||
Reference in New Issue
Block a user