--[[ HoverOriginButton V1.3 by Tasyen
This System gives a easy way setup callbacks when a originframe Button is hovered. It uses Tooltips hence the executed callbacks are async.
I created this because one can do this Tooltip approach to know hovering only once per map so that systems don't have to fight over that feature but use it.
function HoverOriginButton.Add(originframeType, function(buttonIndex) end[, everyTime])
originframeType ORIGIN_FRAME_COMMAND_BUTTON or ORIGIN_FRAME_HERO_BUTTON ...
everyTime (false) only happens when the current Frame was not hovered lastTime.
action the callback
function(buttonIndex)
yourCode
yourCode
end
returns the new objects can be used for HoverOriginButton.Remove
function HoverOriginButton.AddClose(function() end)
add a callback that happens when the user stops hoveringer a Command/Item Button, async.
function()
yourCode
yourCode
end
function HoverOriginButton.Remove(object)
removes the table from the callback List
--]]
do
HoverOriginButton = {}
local SystemSpeed = 1/32.0 -- How Fast is the System
local this = HoverOriginButton
local function CreateTooltipForOrigin(originframeType, firstIndex, lastIndex)
for int = firstIndex, lastIndex do
local button = BlzGetOriginFrame(originframeType, int)
local frame = BlzCreateFrameByType("SIMPLEFRAME", "", button, "", 0)
BlzFrameSetTooltip(button, frame)
BlzFrameSetVisible(frame, false)
this[frame] = {originframeType, int}
table.insert(this.Frames, frame)
end
end
local function CreateTooltipForBuffs(firstIndex, lastIndex)
if ORIGIN_FRAME_UNIT_PANEL_BUFF_BAR then
local parent = BlzGetOriginFrame(ORIGIN_FRAME_UNIT_PANEL_BUFF_BAR, 0)
for int = firstIndex, lastIndex do
local button = BlzFrameGetChild(parent, int)
local frame = BlzCreateFrameByType("SIMPLEFRAME", "", button, "", 0)
BlzFrameSetTooltip(button, frame)
BlzFrameSetVisible(frame, false)
this[frame] = {ORIGIN_FRAME_UNIT_PANEL_BUFF_BAR, int}
table.insert(this.Frames, frame)
end
end
end
local function InitFrames()
this.Frames = {}
local frame, button, CurrentSelectedButtonIndex, selectedAnything
CreateTooltipForOrigin(ORIGIN_FRAME_COMMAND_BUTTON, 0, 11)
CreateTooltipForOrigin(ORIGIN_FRAME_ITEM_BUTTON, 0, 5)
CreateTooltipForOrigin(ORIGIN_FRAME_HERO_BUTTON, 0, 6)
CreateTooltipForOrigin(ORIGIN_FRAME_SYSTEM_BUTTON, 0, 3)
CreateTooltipForOrigin(ORIGIN_FRAME_MINIMAP_BUTTON, 0, 4)
CreateTooltipForBuffs(0, 7)
button = nil
frame = nil
-- saves the last selected Button, async
CurrentSelectedButtonIndex = nil
TimerStart(this.Timer, SystemSpeed, true, function()
selectedAnything = false
-- loop all tooltips and check for the visible one
for tooltipIndex, tooltip in ipairs(this.Frames) do
if BlzFrameIsVisible(tooltip) then
selectedAnything = true
local actionType = this[tooltip][1]
local buttonIndex = this[tooltip][2]
-- the new selected is not the same as the current one?
for _, v in ipairs(this.Actions[actionType]) do
if CurrentSelectedButtonIndex ~= tooltipIndex or v[2] then
v[1](buttonIndex)
end
end
CurrentSelectedButtonIndex = tooltipIndex
end
end
-- now selects nothing?
if not selectedAnything and CurrentSelectedButtonIndex then
for _, v in ipairs(this.ActionsClose) do v[1]() end
CurrentSelectedButtonIndex = nil
end
end)
end
local function Init()
this.Timer = CreateTimer()
InitFrames()
if FrameLoaderAdd then FrameLoaderAdd(InitFrames) end
this.Actions = {}
this.ActionsClose = {}
end
function HoverOriginButton.Add(originframeType, action, everyTime)
if not this.Timer then Init() end
if not everyTime then everyTime = false end
local object = {action, everyTime}
if not originframeType then originframeType = ORIGIN_FRAME_COMMAND_BUTTON end
if not this.Actions[originframeType] then this.Actions[originframeType] = {} end
table.insert(this.Actions[originframeType], object)
return object
end
function HoverOriginButton.AddClose(action)
if not this.Timer then Init() end
local object = {action}
table.insert(this.ActionsClose, object)
return object
end
function HoverOriginButton.Remove(object)
for _, actions in pairs(this.Actions) do
for i, v in ipairs(actions) do
if v == object then
table.remove(actions, i)
return
end
end
end
for i, v in ipairs(this.ActionsClose) do
if v == object then
table.remove(this.ActionsClose, i)
return
end
end
end
end