Introduction to Checkboxes in Quorum
What is a Checkbox?
In a previous tutorial we learned how to use Buttons. A button is simple interface tool that allows for running behaviors when it is activated. Standard buttons have many uses but there is a special class of buttons that expand functionality beyond a simple press to activate. This special class of buttons are known as Toggle Buttons and they add toggle states. A checkbox one of the toggle buttons that allows for activating the box to toggle on and then activating it again to toggle it off.
Having two toggle states differentiates Checkboxes from standard buttons because now you can have the box behave differently when activated, depending on the current toggle state of the checkbox. In Quorum this is accomplished by calling the activation Behavior’s Run action when toggled on and the Behavior’s Dispose action when toggled off. For more information on these two actions they are a topic in the Behavior tutorial.
For this tutorial we will add a Checkbox to our game and set a behavior for it as we start to activate it. To start, create a new Game application.
Making a Checkbox
In order to use Checkboxes, we must use the Checkbox Library with the following use statement:
We want the Checkbox to be usable as the game begins so the our code will mainly be in the Create Game action. To make the checkbox we simply declare it and then we can give it a name and position it. The name for a checkbox is similar to the name of a button in that the text displayed by the button will be the same as the name. We will also change the font size to make the button bigger since the box will scale with the font size. Don’t forget to add the box to the game and we will also put focus on the checkbox so that it is accessible and we can use the Space Key to activate it.
Checkbox box box:SetName("My Checkbox") box:SetPosition(200, 350) box:SetFontSize(30) SetFocus(box) Add(box)
So far the main class is as follows:
use Libraries.Game.Game use Libraries.Interface.Controls.Checkbox class Main is Game action Main StartGame() end action CreateGame Checkbox box box:SetPosition(200, 350) box:SetName("My Checkbox") box:SetFontSize(30) SetFocus(box) Add(box) end end
Now when you run the program, the checkbox should be in the window and clicking the box or pressing the Spacebar will cause it to change toggle states. By default, when toggled on the checkbox icon will change colors and a checkmark will appear inside the box. When toggled off the checkbox icon will simply be gray and the checkmark will not be visible.
The Checkbox also has one little extra customization feature where you can change the color of the checkmark inside the box. This is purely for appearance but if you wish to change that you can accomplish it with these lines of code:
Color c box:SetCheckColor(c:White())
Setting a Behavior
Like a standard Button we can set a Behavior to run when the Checkbox is activated, but a Checkbox has two states so two different actions that will be called when toggled on or off. Internally, what will happen is that when toggled on the Run action will be called and when toggled off the Dispose action is called.
To show this we will create a new Quorum class for our behavior and we name it "CheckBehavior.quorum." Our class will inherit from the Behavior class and we will override the Run and Dispose actions.
All we will the Behaviors actions do is say and output whether they we checked or unchecked. Run will output that the box was checked while Dispose will output that the box was unchecked. The full behavior is shown in the following code block:
use Libraries.Interface.Behaviors.Behavior use Libraries.Interface.Events.BehaviorEvent use Libraries.Sound.Speech use Libraries.Interface.Item class CheckBehavior is Behavior Item item = undefined Speech speech action Run(BehaviorEvent event) item = event:GetItem() speech:Say(item:GetName() + " is now checked") output item:GetName() + " is now checked" end action Dispose() speech:Say(item:GetName() + " is now unchecked") output item:GetName() + " is now unchecked" end end
Now we simply go back to main class and append to our CreateGame action our declaration for the CheckBehavior object and then set the Behavior to the Checkbox’s activation behavior. This is done with the following lines of code:
CheckBehavior behavior box:SetBehavior(behavior)
Now when you run the program, activating and causing to the checkbox to toggle on will cause the program to say and output that the box was checked. Toggling the checkbox off will cause the program to say and output that the box was unchecked.
In the next tutorial, we will discuss Radio Buttons, which describes how to use Radio Buttons.