This is a quick demo of Rich Dersheimer's awesome Window function, all I've done is create a very short main loop, and type up the parameters for quick reference ready for documentation. This code will create a dos style window at the click of a mouse.
Do
If GetPointerPressed() = 1 and flag = 0
flag = 1
create_window( 10, 10, 70, 70, 4, 5, "0000FFFF", "9999ffFF", "44444466" )
ElseIf GetPointerPressed() = 1 and flag = 1
flag = 0
delete_window()
EndIf
Sync()
Loop
` This function creates a window on the screen,
` which can then be used as a background for
` text, buttons, whatever you want really.
` It uses 3 sprites, and keeps track of them, so
` then can be deleted later to close the window.
` The sprites are set to depth 1, 2, and 3, so
` they will appear in front of the default sprite
` depth of 10. The window has separate parameters
` for size, position, border size, shadow size,
` and color params for window, border, and shadow.
` Due to AGK parameter limits, the colors are passed
` as hexidecimal string values of 8 characters each,
` in the format of "RRGGBBAA" for red, green, blue
` and alpha transparency.
//x pos, y pos, width, height, boarder size, shadow size, window colour hex string, boarder color hex string, shadow color hex string
function create_window(x, y, w, h, b, s, w$, b$, s$)
dim windowSprite[2] as integer
for count = 0 to 2
windowSprite[count] = 0
next count
windowSprite[0] = CreateSprite(0)
SetSpriteDepth(windowSprite[0], 1)
SetSpriteSize(windowSprite[0], w - b, h - b)
SetSpritePosition(windowSprite[0], x + (b/2), y + (b/2))
wR = val(mid(w$, 1, 2), 16)
wG = val(mid(w$, 3, 2), 16)
wB = val(mid(w$, 5, 2), 16)
wA = val(mid(w$, 7, 2), 16)
SetSpriteColor(windowSprite[0], wR, wG, wB, wA)
if b > 0
windowSprite[1] = CreateSprite(0)
SetSpriteDepth(windowSprite[1], 2)
SetSpriteSize(windowSprite[1], w, h)
SetSpritePosition(windowSprite[1], x, y)
wR = val(mid(b$, 1, 2), 16)
wG = val(mid(b$, 3, 2), 16)
wB = val(mid(b$, 5, 2), 16)
wA = val(mid(b$, 7, 2), 16)
SetSpriteColor(windowSprite[1], wR, wG, wB, wA)
endif
if s > 0
windowSprite[2] = CreateSprite(0)
SetSpriteDepth(windowSprite[2], 3)
SetSpriteSize(windowSprite[2], w, h)
SetSpritePosition(windowSprite[2], x + s, y + s)
wR = val(mid(s$, 1, 2), 16)
wG = val(mid(s$, 3, 2), 16)
wB = val(mid(s$, 5, 2), 16)
wA = val(mid(s$, 7, 2), 16)
SetSpriteColor(windowSprite[2], wR, wG, wB, wA)
endif
endfunction
` This function deletes the window that was created
` with the create_window() function. It deletes all
` three sprites, window, border, and shadow.
function delete_window
for count = 0 to 2
dim windowSprite[2] as integer
if GetSpriteExists(windowSprite[count]) = 1
DeleteSprite(windowSprite[count])
endif
next count
endfunction
I had some problems getting Easter bunnies code to run, this code only shows a white box after I included some extra parameters in the type. This is probably because it relies on an image rather than native AppGameKit commands. As I'm just using Image 0 to get the code to compile that's most likely the reason.
type button
id `Sprite ID
tid `TextID
tid2 `TextID
text as string
e `exists
i1 `image1
i2 `"
s `State
fixed
alpha
lx
ly
r
wx
wy
endtype
dim buts[25] as button
global p as button
`MakeButton(x,y,xs,ys,image,text as string)
`x=xPosition,y=yPosition
`xs=Width,ys=Height
`image - duh
` text, leave as "" if you don't want any text, otherwise this will center the text on the button
foo=MakeButton(10,10,50,20,0,"")
do
`state(buttonID). 0=Up, 1=Down, 2=Released
if state(foo)=1 then exit
DoButtons()
sync()
loop
DeleteButton(foo) `you should always use this function for deleting buttons to avoid spriteID problems
function MakeButton(x,y,xs,ys,i1,text as string)
for k=1 to 25
if buts[k].e=0
i=k
exit
endif
next
buts[i].id=CreateSprite(i1)
SetSpritePosition(buts[i].id,x,y)
SetSpriteSize(buts[i].id,xs,ys)
FixSpriteToScreen(buts[i].id,1)
if text<>""
buts[i].tid2=CreateText(text)
SetTextAlignment(buts[i].tid2,1)
SetTextPosition(buts[i].tid2,x+(xs/2),y-5)
FixTextToScreen(buts[i].tid2,1)
SetTextVisible(buts[i].tid2,0)
SetTextColor(buts[i].tid2,255,255,255,255)
SetTextDepth(buts[i].tid2,1)
endif
buts[i].fixed=1
buts[i].i1=i1
buts[i].e=1
buts[i].s=0
buts[i].alpha=255
endfunction i
function DeleteButton(i)
if buts[i].e=1
buts[i].e=0
DeleteSprite(buts[i].id)
if GetTextExists(buts[i].tid2) then DeleteText(buts[i].tid2)
buts[i].i1=0
buts[i].i2=0
buts[i].id=0
buts[i].s=0
buts[i].tid2=0
buts[i].text=""
endif
endfunction
function DeleteAllButtons()
for i=1 to 25
if buts[i].e=1
DeleteButton(i)
endif
next
endfunction
function State(i)
s=buts[i].s
endfunction s
function DoButtons()
for k=1 to 25
if buts[k].e=1
if p.s=1
if buts[k].fixed=0
if GetSpriteHitTest(buts[k].id,p.lx,p.ly)
setSpriteColorAlpha(buts[k].id,buts[k].alpha/2)
if GetTextExists(buts[k].tid2) then SetTextVisible(buts[k].tid2,1)
if GetTextExists(buts[k].tid) then settextColorAlpha(buts[k].tid,buts[k].alpha/2)
buts[k].s=1
if p.r and timer()-timeOfLastButton>0.1
buts[k].s=2
timeOfLastButton=timer()
endif
else
buts[k].s=0
setSpriteColorAlpha(buts[k].id,buts[k].alpha)
if GetTextExists(buts[k].tid2) then SetTextVisible(buts[k].tid2,0)
if GetTextExists(buts[k].tid) then settextColorAlpha(buts[k].tid,buts[k].alpha)
endif
else
if GetSpriteHitTest(buts[k].id,p.wx,p.wy)
setSpriteColorAlpha(buts[k].id,buts[k].alpha/2)
if GetTextExists(buts[k].tid2) then SetTextVisible(buts[k].tid2,1)
if GetTextExists(buts[k].tid) then settextColorAlpha(buts[k].tid,buts[k].alpha/2)
buts[k].s=1
if p.r and timer()-timeOfLastButton>0.1
buts[k].s=2
timeOfLastButton=timer()
endif
else
buts[k].s=0
setSpriteColorAlpha(buts[k].id,buts[k].alpha)
if GetTextExists(buts[k].tid2) then SetTextVisible(buts[k].tid2,0)
if GetTextExists(buts[k].tid) then settextColorAlpha(buts[k].tid,buts[k].alpha)
endif
endif
else
if buts[k].s=1
buts[k].s=2
timeOfLastButton=timer()
endif
buts[k].s=0
setSpriteColorAlpha(buts[k].id,buts[k].alpha)
if GetTextExists(buts[k].tid2) then SetTextVisible(buts[k].tid2,0)
if GetTextExists(buts[k].tid) then settextColorAlpha(buts[k].tid,buts[k].alpha)
endif
endif
next
endfunction
So here's a single click version of a button function which will work on mobiles and computers. I'll go back over easter bunnies code later when I get a chance and see if I missed anything, or just plain misunderstood.
Type _Button
id as integer
text_id as integer
_top as float
_bottom as float
_left as float
_right as float
x as float
y as float
r as integer
b as integer
g as integer
a as integer
Endtype
Global Button1 as _Button
Button1 = CreateFlatButton( 50, 50, 30, 10, "Button 1" + CHR( 10 ) + "Extra Line" + Chr( 10 ) + "Extra extra Line" , 128, 128, 128, 255 )
Repeat
Select PointerOverButton( Button1, 64, 64, 64, 255 )
Case 0
SetSpriteColor( Button1.id, 128, 128, 128, 255 )
EndCase
Case 1
Print("Button Pressed!")
EndCase
Case 2
SetSpriteColor( Button1.id, 255, 0, 255, 255 )
EndCase
EndSelect
Sync()
Until GetRawKeyPressed() = 1
End
Function AndroidExitKey()
If GetRawKeyPressed( 27 ) = 1 then End
Endfunction
Function CreateLabel( XPos as Float, YPos as Float, Size as Float, Alignment as Integer, Text as String )
id = CreateText( Text )
SetTextSize( id, Size )
SetTextAlignment( id, Alignment )
tmp = GetTextTotalHeight( id )
SetTextPosition( id, XPos, YPos - ( tmp / 2 ) )
EndFunction id
Function CreateFlatButton( x as float, y as float, sx as float, sy as float, text as string, r as integer, g as integer, b as integer, a as integer )
tmp as _Button
tmp.x = x
tmp.y = y
tmp.id = CreateSprite( 0 )
SetSpriteSize( tmp.id, sx, sy )
SetSpriteOffset( tmp.id, sx / 2, sy / 2 )
SetSpritePositionByOffset( tmp.id, tmp.x, tmp.y )
SetSpriteColor( tmp.id, r, g, b, a )
tmp.text_id = CreateLabel( tmp.x, tmp.y, sy / 4, 1, text )
tmp._top = y - ( sy / 2 )
tmp._bottom = y + ( sy / 2 )
tmp._left = x - ( sx / 2 )
tmp._right = x + ( sx / 2 )
tmp.r = r
tmp.b = b
tmp.g = g
tmp.a = a
EndFunction tmp
Function PointerOverButton( btmp as _Button, r as integer, g as integer, b as integer, a as integer )
px = GetPointerX()
py = GetPointerY()
If py > btmp._top and py < btmp._bottom and px > btmp._left and px < btmp._right
Output = 1
EndIf
If GetPointerState() = 1 and Output = 1
Flag = 1
SetSpriteColor( btmp.id, r, g, b, a )
ElseIf GetPointerReleased() = 1 and Output = 1
Flag = 2
SetSpriteColor( btmp.id, btmp.r, btmp.g, btmp.b, btmp.a )
ElseIf GetPointerReleased() = 1
SetSpriteColor( btmp.id, btmp.r, btmp.g, btmp.b, btmp.a )
EndIf
Endfunction Flag
There's absolutely nothing I need to do with Rich Dersheimer's Initial input system, the projects all packed up and ready to go. It's a great example, I'd never have thought of entering it in this way. But it really does make sense for touch screen devices.
Thanks for the contributions guys!