Getting Started

Usage

Get a reference to the library:

 local QTC = LibStub("LibQTipClick-1.1")

Other than the callbacks, usage is exactly as in LibQTip-1.0.

Supported callbacks

  • OnEnter
  • OnLeave
  • OnMouseDown
  • OnMouseUp

These functions are set as callbacks by the tooltip itself. Not setting a function uses the library default.

Examples

In these examples, we assign a callback handler to the tooltip for OnMouseDown, and assign values to the cells. Note that "MouseHandler", "EnterHandler", and "LeaveHandler" are simply the names used in these examples - in your code they can be whatever you want.

 local function myFunc()
         tooltip = QTC:Acquire("MyAddonNameTooltip", 3, "LEFT", "LEFT", "CENTER")
         -- Assign the callback to the tooltip
         tooltip:SetCallback("OnMouseDown", MouseHandler)
         local y, x = tooltip:AddLine()
         y, x = tooltip:SetCell(y, 1, "Cell 1", "c1")
         y, x = tooltip:SetCell(y, 2, "Cell 2", "c2")
         y, x = tooltip:SetCell(y, 3, "Cell 3", "c3")
         tooltip:Show()
 end

This example uses closures, which should generally be avoided because it makes more work for Lua's garbage collector by creating new functions every time the example function is called.

 local function myFunc()
         tooltip = QTC:Acquire("MyAddonNameTooltip", 3, "LEFT", "LEFT", "CENTER")
         -- Assign the callback to the tooltip
         tooltip:SetCallback("OnMouseDown", MouseHandler)
         local y, x = tooltip:AddLine()
         y, x = tooltip:SetCell(y, 1, "Cell 1", function() return "You clicked cell 1!" end)
         y, x = tooltip:SetCell(y, 2, "Cell 2", function() return "You clicked cell 2!" end)
         y, x = tooltip:SetCell(y, 3, "Cell 3", function() return "You clicked cell 3!" end)
         tooltip:Show()
 end

The MouseHandler function

This is a very simple handler: It assigns a string based on the value of "arg" and prints it to the default chat frame.

local function MouseHandler(event, cell, arg, button)
        local str
        if arg == "c1" then
                str = "#1"
        elseif arg == "c2" then
                str = "#2"
        elseif arg == "c3" then
                str = "#3"
        end
        DEFAULT_CHAT_FRAME:AddMessage("You clicked cell "..str)
 end

In this version of MouseHandler, "arg" has been passed as a function which returns a string.

 local function MouseHandler(event, cell, arg, button)
         local text = arg()
         DEFAULT_CHAT_FRAME:AddMessage(text)
 end

This example illustrates how to handle cases where "arg" can be of mixed types. For the purposes of this example, "arg" returns no value when passed as a function and has members named "message" and "func" when passed as a table.

 local function MouseHandler(event, cell, arg, button)
        if type(arg) == "function" then
                arg()
        elseif type(arg) == "table" then
                DEFAULT_CHAT_FRAME:AddMessage(arg.message)
                arg.func()
        elseif arg == "c1" then
                DEFAULT_CHAT_FRAME:AddMessage("You clicked cell #1")
        end
 end

Here, we want to do different things based on which mouse button is used.

local function MouseHandler(event, cell, arg, button)
        if button == "LeftButton" then
                -- Do something with arg, perhaps.
        elseif button == "MiddleButton" then
                -- Do something with arg, perhaps.
        else
                -- Do something with arg, perhaps.
        end
end

The EnterHandler and LeaveHandler functions

There are occasions when changing the default behavior for mousing into and out of a cell is desirable. The main difference between these and the OnMouseDown handler is that these lack a "button" parameter.

However, once they are changed the default behavior of highlighting the cell no longer functions unless explicitly called from within the new handler using the library's default OnEnter and OnLeave functions. Since the cell must be passed as the only parameter to these functions, dot-notation must be used instead of colon-notation.

We set a custom OnEnter handler, but wish to retain the highlighting behavior.

local function EnterHandler(event, cell, arg)
        DEFAULT_CHAT_FRAME:AddMessage("The cell's arg parameter is "..arg)
        QTC.OnEnter(event, cell, arg)
end

If a custom OnLeave handler was specified and the OnEnter handler highlights the cell, we must explicitly un-highlight it when we call OnLeave.

local function LeaveHandler(event, cell, arg)
        DEFAULT_CHAT_FRAME:AddMessage("The cell's arg parameter is "..arg)
        QTC.OnLeave(event, cell, arg)
end

Setting Callbacks on Non-Tooltip Objects

The Library also has the ability to designate another object's embedded methods as a callback. In this case, dot-notation (tooltip.SetCallback) is used instead of colon-notation (tooltip:SetCallback) because we'll be passing the object as the first argument to SetCallBack(). In this example, we'll use the main AddOn object.

 local function myFunc()
         tooltip = QTC:Acquire("MyAddonNameTooltip", 3, "LEFT", "LEFT", "CENTER")
         -- Assign the callback to the AddOn object
         tooltip.SetCallback(myAddOn, "OnMouseDown", "someFunc")
         -- The rest of the code goes below.
 end

This would cause myAddon:someFunc() to fire as the OnMouseDown handler for the tooltip.


Comments

  • To post a comment, please or register a new account.
Posts Quoted:
Reply
Clear All Quotes