Multiple buttons
// Project: ButtonGUI
// Created: 2017-10-29
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "ButtonGUI" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
global mouse as integer // create a sprite to move with the mouse and use sprite collisions to detect hits
mouse=createsprite(getimage(0,0,1,1)) // create a 1x1 pixel for the mouse
// initialise the buttons
type _buttons
normal, pressed, hover //there images
endtype
global button as _buttons[8] // s many buttons as you need
for a=1 to button.length
button[a].normal = CreateSprite(Loadimage("button1.png"))
button[a].hover = CreateSprite(Loadimage("button1hover.png"))
button[a].pressed = CreateSprite(Loadimage("button1pressed.png"))
next
// ThreestateButton (posx,posy,sizex,sizey,norm_image,hover_image,pressed_image)
do
//place the buttons (increase the array length (line 28) to add more or less
ThreeStateButton(1,100,100)
ThreeStateButton(2,100,200)
ThreeStateButton(3,100,300)
ThreeStateButton(4,100,400)
ThreeStateButton(5,400,100)
ThreeStateButton(6,400,200)
ThreeStateButton(7,400,300)
ThreeStateButton(8,400,400)
displaycoords()
//Print( ScreenFPS() )
Sync()
loop
//FUNCTION FOR 3 STATE BUTTON
Function ThreeStateButton(but,mx1,my1)
// position all subimages
SetSpritePosition(button[but].normal,mx1,my1)
SetSpritePosition(button[but].pressed,mx1,my1)
SetSpritePosition(button[but].hover,mx1,my1)
// Reset them to invisible except the normal
SetSpriteVisible(button[but].pressed,0)
SetSpriteVisible(button[but].hover,0)
// by default display the normal image
SetSpriteVisible(button[but].normal,1)
// if the mouse sprite hits the normal image
if GetSpriteCollision(mouse, button[but].normal) //check if mouse collides with normal image
SetSpriteVisible(button[but].hover,1) // show hover
endif
// if the mouse sprite hits the normal image and the mouse button is pressed
if GetSpriteCollision(mouse, button[but].normal) and GetPointerstate()=1 // check if mouse collides with normal image and button is perssed
SetSpriteVisible(button[but].pressed,1) // show pressed
endif
Endfunction
Function displaycoords()
// attach the 1 x 1 mouse sprite to the mouseitself
x= getpointerX()
y= getpointerY()
SetSpritePosition(mouse,x,y)
print(str(x) + " ," + str(y))
Endfunction
EDIT:- added a buttonpressed (to let the app know which was pressed
// Project: ButtonGUI
// Created: 2017-10-29
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "ButtonGUI" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
global mouse,buttonpressed as integer // create a sprite to move with the mouse and use sprite collisions to detect hits
mouse=createsprite(getimage(0,0,1,1)) // create a 1x1 pixel for the mouse
// initialise the buttons
type _buttons
normal, pressed, hover //there images
endtype
global button as _buttons[8] // s many buttons as you need
for a=1 to button.length
button[a].normal = CreateSprite(Loadimage("button1.png"))
button[a].hover = CreateSprite(Loadimage("button1hover.png"))
button[a].pressed = CreateSprite(Loadimage("button1pressed.png"))
next
// ThreestateButton (posx,posy,sizex,sizey,norm_image,hover_image,pressed_image)
do
//place the buttons (increase the array length (line 28) to add more or less
ThreeStateButton(1,100,100)
ThreeStateButton(2,100,200)
ThreeStateButton(3,100,300)
ThreeStateButton(4,100,400)
ThreeStateButton(5,400,100)
ThreeStateButton(6,400,200)
ThreeStateButton(7,400,300)
ThreeStateButton(8,400,400)
displaycoords()
//Print( ScreenFPS() )
Sync()
loop
//FUNCTION FOR 3 STATE BUTTON
Function ThreeStateButton(but,mx1,my1)
// position all subimages
SetSpritePosition(button[but].normal,mx1,my1)
SetSpritePosition(button[but].pressed,mx1,my1)
SetSpritePosition(button[but].hover,mx1,my1)
// Reset them to invisible except the normal
SetSpriteVisible(button[but].pressed,0)
SetSpriteVisible(button[but].hover,0)
// by default display the normal image
SetSpriteVisible(button[but].normal,1)
// if the mouse sprite hits the normal image
if GetSpriteCollision(mouse, button[but].normal) //check if mouse collides with normal image
SetSpriteVisible(button[but].hover,1) // show hover
endif
// if the mouse sprite hits the normal image and the mouse button is pressed
if GetSpriteCollision(mouse, button[but].normal) and GetPointerstate()=1 // check if mouse collides with normal image and button is perssed
SetSpriteVisible(button[but].pressed,1) // show pressed
buttonpressed=but
endif
Endfunction but
Function displaycoords()
// attach the 1 x 1 mouse sprite to the mouseitself
x= getpointerX()
y= getpointerY()
SetSpritePosition(mouse,x,y)
print(str(x) + " ," + str(y))
print ("Button pressed:- " + str(buttonpressed))
Endfunction
D