Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] The easy way to create a checkbox in AGK

Author
Message
BOB Tellura
8
Years of Service
User Offline
Joined: 22nd Dec 2015
Location:
Posted: 22nd Dec 2015 21:58
Hi all,

I'm newbee in AppGameKit,
I would like create checkbox to setup my application
I supposed I need use prites and I created 2 images cheched and uncheched ...
Somone can explain me an easy way to create checkbox in AppGameKit?

Thanks

BOB

The author of this post has marked a post as an answer.

Go to answer

Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 23rd Dec 2015 08:28
This post has been marked by the post author as the answer.
You could have a sprite with 2 frames, like make an image with the unchecked box on the left, and the checked box on the right. Then, create a sprite, set the animation for the 2 frames, and then you can just set the sprites frame between 1 and 2 to check or uncheck it. Haven't got AppGameKit handy, but this sort of thing:.. assuming your image is 64x32.

img_checkbox=loadimage("check.png")
spr_checkbox=createsprite(img_checkbox)
setspriteanimation(spr_checkbox,32,32,2)
setspriteframe(spr_checkbox,1)

There are a number of ways you can check for collision, but the very most basic is to simply check the mouse pointer is within a range. Lets say you put the sprite at 10,10 on the screen....

setspriteposition(spr_checkbox,10,10)

And you set MX MY and MB to the mouse position and state...

MX=getrawpointerx()
MY=getrawpointery()
LMB=MB
MB=getrawpointerstate()

Recording LMB as the previous mouse state, so it's easy to tell when that first click happens....

if LMB=0 and MB=1
if mx>10 and my>10 and mx<42 and my<42
state=1-state
setspriteframe(spr_checkbox,1+state)
endif
endif

Now the variable 'state' is controlled by clicking that checkbox sprite. STATE=1-STATE is a neat way to toggle a variable between 0 and 1 with 1 line of code.
BOB Tellura
8
Years of Service
User Offline
Joined: 22nd Dec 2015
Location:
Posted: 23rd Dec 2015 22:24
Hello Van B

3 reasons to say thanks!
I tried to start with two Srites and it was complicated to switch… It’s really easier with frames

For the detection, I tried : “If ( GetPointerState() = 1 )” but it dosen’t work or I need to be quick , thanks for :
LMB=MB
MB=getpointerstate()
if LMB=0 and MB=1

I prefer to use:
hit = GetSpriteHit(GetPointerX(), GetPointerY())
if hit = spr_checkbox
I have 12 checkbox…

And the last Thanks is for :
state=1-state
setspriteframe(spr_checkbox,1+state)
Quick, intelligent top!

My proposition with your help :

// Project: TestCheckBox
// Created: 2015-12-22


spr_checkbox as integer
LMB as integer
MB as integer
mx as integer
my as integer
state as integer

// set window properties
SetWindowTitle( "TestCheckBox" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )


spr_checkbox=createsprite(loadimage("checkbox.png"))
setspriteanimation(spr_checkbox,102,86,2)
setspriteframe(spr_checkbox,1)

setspriteposition(spr_checkbox,10,10)

do
LMB=MB
MB=getpointerstate()

if LMB=0 and MB=1
hit = GetSpriteHit(GetPointerX(), GetPointerY())
if hit = spr_checkbox
state=1-state
setspriteframe(spr_checkbox,1+state)
Endif
Endif

Print( ScreenFPS() )
Sync()
loop

Thanks Van B

Bob
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Dec 2015 05:09 Edited at: 25th Dec 2015 05:17
Using getSpriteHit would only work if you have a sprite as your pointer. Also, for selecting GUI components it's inefficient.


It's christmas eve and I'm at work, so..... I'll make a demo for ya cause I'm bored!

It's very little code, maybe 50 lines if you take out the spacing and all the comments. This uses a UDT array to keep track of multiple checkboxes. I made an image for an animated checkbox. And not just animated in terms of having a checkbox on or off state, but a glowing effect when it's active. (like I said, I got bored).
Simple functions let you create a checkbox, position it, get it's current state, and a single function call in the main loop handles everything.




Another option if you really want to delve into GUI design, you can take a look at my iOS library to see how I did a lot of different things.
https://forum.thegamecreators.com/thread/210459

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-11-25 13:38:42
Your offset time is: 2024-11-25 13:38:42