Look in Attachments - Buttons.zip
Simple example:
SetErrorMode(2)
// set window properties
SetWindowTitle( "Buttons" )
SetWindowSize( 540, 960, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
//--------------------------------------------------------
global AppWidth as float, AppHeight as float
AppWidth = 540
AppHeight = Round(540*GetDeviceHeight()/GetDeviceWidth())
SetVirtualResolution( AppWidth, AppHeight )
//--------------------------------------------------------
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
SetClearColor(255,255,255)
//========================================================
type class_button
id as integer
text as integer
touch as integer
endtype
global btn1 as class_button
btn1 = NewButton('btn1.png', 'My Button1')
SetButtonPosition(btn1, AppWidth/2, AppHeight/2-50 )
global btn2 as class_button
btn2 = NewButton('btn2.png', 'Hello from AGK!')
SetButtonPosition(btn2, AppWidth/2, AppHeight/2+50 )
//--------------------------------------------------------
global text : text = CreateText('Unknown')
SetTextSize(text, 20)
SetTextColor(text, 0,0,0,255)
//========================================================
global scene = 0
do
select scene
case 0:
SceneLoop_0()
endcase
case 1:
endcase
endselect
Sync()
loop
//========================================================
function NewButton( imagefile as string, label as string )
obj as class_button
obj.id = LoadSprite(imagefile)
obj.text = CreateText(label)
SetTextAlignment(obj.text,1)
SetTextSize(obj.text, 40)
TextToFit(obj.text, GetSpriteWidth(obj.id)-30)
SetTextPosition(obj.text, GetSpriteX(obj.id)+GetSpriteWidth(obj.id)/2, GetSpriteY(obj.id)+GetSpriteHeight(obj.id)/2-GetTextTotalHeight(obj.text)/2)
endfunction obj
//--------------------------------------------------------
function TextToFit( _text, _width# )
if getTextTotalWidth(_text) > _width#
setTextSize(_text, (GetTextSize( _text) * _width#) / (getTextTotalWidth(_text)))
endif
endfunction
//========================================================
//position
//--------------------------------------------------------
function SetButtonX(obj ref as class_button, x as float )
SetSpritePositionByOffset(obj.id, x, GetSpriteYByOffset(obj.id) )
SetTextPosition(obj.text, GetSpriteX(obj.id)+GetSpriteWidth(obj.id)/2, GetSpriteY(obj.id)+GetSpriteHeight(obj.id)/2-GetTextTotalHeight(obj.text)/2-2)
endfunction
//--------------------------------------------------------
function SetButtonY(obj ref as class_button, y as float )
SetSpritePositionByOffset(obj.id, GetSpriteXByOffset(obj.id), y )
SetTextPosition(obj.text, GetSpriteX(obj.id)+GetSpriteWidth(obj.id)/2, GetSpriteY(obj.id)+GetSpriteHeight(obj.id)/2-GetTextTotalHeight(obj.text)/2-2)
endfunction
//--------------------------------------------------------
function SetButtonPosition(obj ref as class_button, x as float, y as float)
SetButtonX(obj, x)
SetButtonY(obj, y)
endfunction
//========================================================
function BtnListiner(obj ref as class_button)
if GetPointerPressed() = 1 and GetSpriteHitTest(obj.id, GetPointerX(), GetPointerY())
if obj.touch = 0
obj.touch = 1
SetButtonY(obj, GetSpriteYByOffset(obj.id)+10)
endif
else
if GetPointerReleased() = 1
if obj.touch = 1
obj.touch = 0
if GetSpriteHitTest(obj.id, GetPointerX(), GetPointerY()) = 1
SetButtonY(obj, GetSpriteYByOffset(obj.id)-10)
select obj.id
case btn1.id:
btn1Press()
endcase
case btn2.id:
btn2Press()
endcase
endselect
else
SetButtonY(obj, GetSpriteYByOffset(obj.id)-10)
endif
endif
endif
endif
endfunction
//========================================================
function btn1Press()
SetTextString(text, GetTextString(btn1.text) )
endfunction
//--------------------------------------------------------
function btn2Press()
SetTextString(text, GetTextString(btn2.text) )
endfunction
//========================================================
function SceneLoop_0()
BtnListiner(btn1)
BtnListiner(btn2)
endfunction
//========================================================