- Joined
- Jul 18, 2010
- Messages
- 2,377
This is a variation of the system I used in TasAbilityFieldTooltip to know that a CommandButton is hovered. It allows to add/Remove callbacks to the hover feature and have many of them.
Changelog:
V1.1 API functions became a table member of HoverOriginButton
Lua:
--[[ HoverOriginButton V1.1 by Tasyen
This System gives a easy way setup callbacks when a Command/Item 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(forCommandButton, everyTime, action)
forCommandButton (true) add to commandButton Actions
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(action)
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 = {ItemButtonOffset = 30}
local this = HoverOriginButton
local function InitFrames()
this.Frames = {}
local frame, button, CurrentSelectedButtonIndex, selectedAnything
-- saves the last selected Button, async
CurrentSelectedButtonIndex = nil
--create one tooltip frame for each command button
for int = 0, 11 do
button = BlzGetOriginFrame(ORIGIN_FRAME_COMMAND_BUTTON, int)
frame = BlzCreateFrameByType("SIMPLEFRAME", "", button, "", 0)
BlzFrameSetTooltip(button, frame)
BlzFrameSetVisible(frame, false)
this.Frames[int] = frame
end
--create one tooltip frame for each command button
for int = 0, 5 do
button = BlzGetOriginFrame(ORIGIN_FRAME_ITEM_BUTTON, int)
frame = BlzCreateFrameByType("SIMPLEFRAME", "", button, "", 0)
BlzFrameSetTooltip(button, frame)
BlzFrameSetVisible(frame, false)
this.Frames[int + this.ItemButtonOffset] = frame
end
button = nil
frame = nil
TimerStart(this.Timer, 1.0/32, true, function()
selectedAnything = false
-- loop all tooltips and check for the visible one
for int = 0, 11 do
if BlzFrameIsVisible(this.Frames[int]) then
selectedAnything = true
-- the new selected is not the same as the current one?
for _, v in ipairs(this.ActionsCommand) do
if CurrentSelectedButtonIndex ~= int or v[2] then
v[1](int)
end
end
CurrentSelectedButtonIndex = int
end
end
local tableIndex
for int = 0, 5 do
tableIndex = int + this.ItemButtonOffset
if BlzFrameIsVisible(this.Frames[tableIndex]) then
selectedAnything = true
-- the new selected is not the same as the current one?
for _, v in ipairs(this.ActionsItem) do
if CurrentSelectedButtonIndex ~= tableIndex or v[2] then
v[1](int)
end
end
CurrentSelectedButtonIndex = tableIndex
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.ActionsCommand = {}
this.ActionsItem = {}
this.ActionsClose = {}
end
function HoverOriginButton.Add(forCommandButton, everyTime, action)
if not this.Timer then Init() end
local object = {action, everyTime}
if forCommandButton then
table.insert(this.ActionsCommand, object)
else
table.insert(this.ActionsItem, object)
end
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 i, v in ipairs(this.ActionsCommand) do
if v == object then
table.remove(this.ActionsCommand, i)
return
end
end
for i, v in ipairs(this.ActionsItem) do
if v == object then
table.remove(this.ActionsItem, i)
return
end
end
for i, v in ipairs(this.ActionsClose) do
if v == object then
table.remove(this.ActionsClose, i)
return
end
end
end
end
V1.1 API functions became a table member of HoverOriginButton
Last edited: