WoW:UIOBJECT CheckButton: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Move page script moved page UIOBJECT CheckButton to UIOBJECT CheckButton without leaving a redirect)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{wikify}}
{{wikify}}
{{widget}}


== Making Checkboxes ==
== Making Checkboxes ==
You can make a checkbox in WoW via the following commands:
You can make a checkbox in WoW via the following commands:
  myCheckButton = CreateFrame("CheckButton", "myCheckButton_GlobalName", parentFrame, "ChatConfigCheckButtonTemplate");
  myCheckButton = CreateFrame("CheckButton", "myCheckButton_GlobalName", parentFrame, "ChatConfigCheckButtonTemplate");


This is where you locate the checkbox relative to its parent frame. Coordinates are (0,0) is the top-left corner of the parent, so a negative y-coordinate means down and positive x-coord means right.
This is where you locate the checkbox relative to its parent frame. Coordinates are (0,0) is the top-left corner of the parent, so a negative y-coordinate means down and positive x-coord means right.
  myCheckButton:SetPoint("TOPLEFT", 200, -65);
  myCheckButton:SetPoint("TOPLEFT", 200, -65);


You can reference the name of your Check Box either by writing it explicitly, or by making a function call. Writing it explicitly "looks prettier", but might be confusing to anyone looking at your code, as the name for it seems made-up. Note arg2 in the creation call. It is the same as what is below, but we have appended it with "Text". This is necessary, I don't know why they didn't just set it up so you can do myCheckButton:Text, but we must work with what we're given.
You can reference the name of your Check Box either by writing it explicitly, or by making a function call. Writing it explicitly "looks prettier", but might be confusing to anyone looking at your code, as the name for it seems made-up. Note arg2 in the creation call. It is the same as what is below, but we have appended it with "Text". This is necessary, I don't know why they didn't just set it up so you can do myCheckButton:Text, but we must work with what we're given.
  myCheckButton_GlobalNameText:SetText("CheckBox Name");
  myCheckButton_GlobalNameText:SetText("CheckBox Name");
Here is the code to do the exact same thing, but it's not explicitly written. This is (arguably) less confusing.
 
Here is the code to do exactly the same thing, but it's not explicitly written. This is (arguably) less confusing.
  getglobal(myCheckButton:GetName() .. 'Text'):SetText("CheckBox Name");
  getglobal(myCheckButton:GetName() .. 'Text'):SetText("CheckBox Name");


When mousing over your checkbox, you can have a tooltip appear, which is useful to describe the actions resulting from clicking the checkbox, without putting it all in the name.
When mousing over your checkbox, you can have a tooltip appear, which is useful to describe the actions resulting from clicking the checkbox, without putting it all in the name.
  myCheckButton.tooltip = "This is where you place MouseOver Text.";
  myCheckButton.tooltip = "This is where you place MouseOver Text.";


 
Set up what happens when you click the checkbox. You can add other scripts similarly to how I have this one, you just need to change the "OnClick" to whatever else. [https://wowwiki.fandom.com/Category:Widget_event_handlers http://www.wowwiki.com/Category:Widget_event_handlers] Note that you cannot pass arguments to the "OnClick" script's function, but you can do other things as in a normal function
 
Set up what happens when you click the checkbox. You can add other scripts similarly to how I have this one, you just need to change the "OnClick" to whatever else. http://www.wowwiki.com/Category:Widget_event_handlers Note that you cannot pass arguments to the "OnClick" script's function, but you can do other things as in a normal function
  myCheckButton:SetScript("OnClick",  
  myCheckButton:SetScript("OnClick",  
   function()
   function()
Line 44: Line 37:




== Recommendation ==  
== Recommendation ==
However, I recommend creating a generic "checkbox-factory function", like this:
However, I recommend creating a generic "checkbox-factory function", like this:


Line 58: Line 51:
  end
  end
   
   
-- this is how you'd use it
  myCheckButton = bw_addon.createCheckbutton(UIParent, 400, -600, "A Checkbox");
  myCheckButton = bw_addon.createCheckbutton(UIParent, 400, -600 "A Checkbox");
  myCheckButton.tooltip = "If this is checked, nothing will happen, because this is a demo checkbox.";
  myCheckButton.tooltip = "If this is checked, nothing will happen, because this is a demo checkbox.";
  myCheckButton:SetScript("OnClick",  
  myCheckButton:SetScript("OnClick",  

Latest revision as of 04:49, 15 August 2023

Template:Wikify

Widget API < CheckButton

Making Checkboxes[edit]

You can make a checkbox in WoW via the following commands:

myCheckButton = CreateFrame("CheckButton", "myCheckButton_GlobalName", parentFrame, "ChatConfigCheckButtonTemplate");

This is where you locate the checkbox relative to its parent frame. Coordinates are (0,0) is the top-left corner of the parent, so a negative y-coordinate means down and positive x-coord means right.

myCheckButton:SetPoint("TOPLEFT", 200, -65);

You can reference the name of your Check Box either by writing it explicitly, or by making a function call. Writing it explicitly "looks prettier", but might be confusing to anyone looking at your code, as the name for it seems made-up. Note arg2 in the creation call. It is the same as what is below, but we have appended it with "Text". This is necessary, I don't know why they didn't just set it up so you can do myCheckButton:Text, but we must work with what we're given.

myCheckButton_GlobalNameText:SetText("CheckBox Name");

Here is the code to do exactly the same thing, but it's not explicitly written. This is (arguably) less confusing.

getglobal(myCheckButton:GetName() .. 'Text'):SetText("CheckBox Name");

When mousing over your checkbox, you can have a tooltip appear, which is useful to describe the actions resulting from clicking the checkbox, without putting it all in the name.

myCheckButton.tooltip = "This is where you place MouseOver Text.";

Set up what happens when you click the checkbox. You can add other scripts similarly to how I have this one, you just need to change the "OnClick" to whatever else. http://www.wowwiki.com/Category:Widget_event_handlers Note that you cannot pass arguments to the "OnClick" script's function, but you can do other things as in a normal function

myCheckButton:SetScript("OnClick", 
  function()
    --do stuff
  end);

Example entirely in Lua[edit]

myCheckButton = CreateFrame("CheckButton", "myCheckButton_GlobalName", UIParent, "ChatConfigCheckButtonTemplate");
myCheckButton:SetPoint("TOPLEFT", 200, -65);
myCheckButton_GlobalNameText:SetText("CheckBox Name");
myCheckButton.tooltip = "This is where you place MouseOver Text.";
myCheckButton:SetScript("OnClick", 
  function()
    --do stuff
  end
);


Recommendation[edit]

However, I recommend creating a generic "checkbox-factory function", like this:

local uniquealyzer = 1;
function createCheckbutton(parent, x_loc, y_loc, displayname)
	uniquealyzer = uniquealyzer + 1;
	
	local checkbutton = CreateFrame("CheckButton", "my_addon_checkbutton_0" .. uniquealyzer, parent, "ChatConfigCheckButtonTemplate");
	checkbutton:SetPoint("TOPLEFT", x_loc, y_loc);
	getglobal(checkbutton:GetName() .. 'Text'):SetText(displayname);

	return checkbutton;
end

myCheckButton = bw_addon.createCheckbutton(UIParent, 400, -600, "A Checkbox");
myCheckButton.tooltip = "If this is checked, nothing will happen, because this is a demo checkbox.";
myCheckButton:SetScript("OnClick", 
   function()
      -- do stuff
   end
);

My reason for recommending this is that it is easy to port the checkbox code into a new addon, and it is also a lot more compact (3 lines per checkbox, instead of 6). Also, it is harder to make copy/pasta mistakes with a checkbox-factory.