A method for managing multiple checkboxes for a gui, and how to do animated gui components. Checkboxes are only triggered when user clicks on them and released the mouse button from the same button. So say you click on a checkbox but then drag the mouse off the checkbox before releasing the button, the checkbox will not register a click.
setVirtualResolution(640,480)
// How many checkboxes the array can hold
#CONSTANT CHECKBOX_MAX = 10
// This equals the number of active checkboxes
GLOBAL _CheckBox_Index = 0
// Variables for each checkbox
Type T_CheckBox
sprite as integer
state as integer
btnDown as integer
EndType
// array for checkboxes (since agk doesn't have dynamic arrays)
dim checkboxes[CHECKBOX_MAX] as T_CheckBox
// create a checkbox
n1 = createCheckbox()
positionCheckbox(n1, 200, 100)
n2 = createCheckbox()
positionCheckbox(n2, 300, 200)
do
checkboxHandler()
print("Checkbox 1: "+str(getCheckboxState(n1)))
print("Checkbox 2: "+str(getCheckboxState(n2)))
Sync()
loop
////////////////////////////////////////////////////////////
// Handles checkbox functionality
////////////////////////////////////////////////////////////
function checkboxHandler()
// Get mouse coordinates
mx = getRawMouseX()
my = getRawMouseY()
btnLeftPressed = getRawMouseLeftPressed()
btnLeftReleased = getRawMouseLeftReleased()
btnLeftState = getRawMouseLeftState()
// Loop through all checkboxes
for i = 1 to _CheckBox_Index
// Make sure mouse is over the checkbox
// Checkbox sprite is offset so it's positioned by it's center. Use simple distance formula (pythagora)
// to see if mouse is within bounds of circular checkbox. In this demo, image is 32px, so 32*32 is 256.
if (mx-getSpriteXByOffset(checkboxes[i].sprite))^2 + (my-getSpriteYByOffset(checkboxes[i].sprite))^2 <= 256
// If checkbox has not been clicked yet
if checkboxes[i].btnDown = 0
// if left mouse button pressed
if btnLeftPressed = 1 then checkboxes[i].btnDown = 1
endif
// If checkbox has already been clicked on but mouse button has not released yet
if checkboxes[i].btnDown = 1
// If mouse button released then toggle the checkbox state
if btnLeftReleased = 1
if checkboxes[i].state = 0
checkboxes[i].state = 1
playSprite(checkboxes[i].sprite, 12, 1, 2, 10)
else
checkboxes[i].state = 0
stopSprite(checkboxes[i].sprite)
setSpriteFrame(checkboxes[i].sprite, 1)
endif
endif
endif
endif
if btnLeftState = 0 then checkboxes[i].btnDown = 0
next i
endfunction
////////////////////////////////////////////////////////////
// Create new checkboxes whenever you want easily
////////////////////////////////////////////////////////////
function createCheckbox()
if _CheckBox_Index = CHECKBOX_MAX then exitfunction 0
inc _CheckBox_Index
c = -1
if _CheckBox_Index = 1
c = createSprite(loadImage("checkbox.png"))
else
c = cloneSprite(checkboxes[1].sprite)
endif
setSpriteAnimation(c, 32, 32, 12)
setSpriteOffset(c, 16, 16)
checkboxes[_CheckBox_Index].sprite = c
endfunction _CheckBox_Index
////////////////////////////////////////////////////////////
// Position your checkbox
////////////////////////////////////////////////////////////
function positionCheckbox(id, x, y)
setSpritePositionByOffset(checkboxes[id].sprite, x, y)
endfunction
////////////////////////////////////////////////////////////
// Returns the state of a checkbox
////////////////////////////////////////////////////////////
function getCheckboxState(id)
s = checkboxes[id].state
endfunction s
"I like offending people, because I think people who get offended should be offended." - Linus Torvalds