WoW:UIHANDLER OnClick: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
mNo edit summary
(→‎Example: +XML handlers)
Line 1: Line 1:
{{widgethandler}}<br>
{{widgethandler}}
 
The '''OnClick''' handler is called when a UI widget that supports clicking (i.e. is derived from a Button) is clicked. Region derivatives like Frame only implement OnMouseDown / OnMouseUp handlers.
== Description ==
 
The OnClick handler is called when a UI element is clicked with the mouse.


== Arguments ==
== Arguments ==
;self : widget being clicked
;button : [mouse] button used to click the widget. Due to widget:Click(button, down), button argument may be anything, although the following five values are common for normal mouse clicks: "LeftButton", "RightButton", "MiddleButton", "Button4", "Button5".
;down : Boolean - 1 (true) if the trigger button is currently being held down, nil (false) otherwise.


self - the button being clicked
==Example==
 
local taunts = {"I'm a button.", "Quit clicking me.", "That's enough.", "Stop it!", "I'm leaving!"};
button - a string representing the button that was clicked.
local widget = CreateFrame("Button", "TauntingButton", UIParent, "UIPanelButonTemplate");
Can be any of the following:
widget:SetWidth(200); widget:SetHeight(24); widget:SetPoint("CENTER");
*LeftButton
widget:RegisterForClicks("AnyUp");
*RightButton
widget:SetHandler("OnClick", function (self, button, down)
*MiddleButton
  self:SetID((self:GetID() or 1) + 1);
*Button4
  if taunts[self:GetID()] then
*Button5
  self:SetText(taunts[self:GetID()]);
According to how the UI element is setup with [[API_Button_RegisterForClicks]]
  else
  self:Hide();
  end
end);


down - whether the button is down (1 or nil? true or false? someone please provide more info)
The named arguments are also available in XML handlers:
<Button name="foo" ...>
  ...
  <Scripts>
    ...
    <OnClick>self:SetText("You clicked with " .. button);</OnClick>
  </Scripts>
  </Button>

Revision as of 19:59, 11 October 2008

Widget handlers < OnClick

The OnClick handler is called when a UI widget that supports clicking (i.e. is derived from a Button) is clicked. Region derivatives like Frame only implement OnMouseDown / OnMouseUp handlers.

Arguments

self
widget being clicked
button
[mouse] button used to click the widget. Due to widget:Click(button, down), button argument may be anything, although the following five values are common for normal mouse clicks: "LeftButton", "RightButton", "MiddleButton", "Button4", "Button5".
down
Boolean - 1 (true) if the trigger button is currently being held down, nil (false) otherwise.

Example

local taunts = {"I'm a button.", "Quit clicking me.", "That's enough.", "Stop it!", "I'm leaving!"};
local widget = CreateFrame("Button", "TauntingButton", UIParent, "UIPanelButonTemplate");
widget:SetWidth(200); widget:SetHeight(24); widget:SetPoint("CENTER");
widget:RegisterForClicks("AnyUp");
widget:SetHandler("OnClick", function (self, button, down)
 self:SetID((self:GetID() or 1) + 1);
 if taunts[self:GetID()] then
  self:SetText(taunts[self:GetID()]);
 else
  self:Hide();
 end
end);

The named arguments are also available in XML handlers:

<Button name="foo" ...>
  ...
  <Scripts>
    ...
   <OnClick>self:SetText("You clicked with " .. button);</OnClick>
  </Scripts>
 </Button>