sync on
 
a = calc(10,10,rgb(200,200,200),1)
 
ink rgb(255,255,255),0
print a
 
suspend for key
 
end
 
`the function
function calc(posx, posy, color, move)
 
	`preparations
	get image 65535,0,0,screen width(),screen height()
	defaultfont$ = text font$()
	defaultsize = text size()
	defaultstyle = text style()
	defaultback = text background type()
 
	dim number$(2)
	cnumber = 1
	answer$ = ""
	display$ = ""
	operator = 0
 
	set text font "Fixedsys"
	set text size 16
	set text to normal
	set text transparent
 
	`start loop
	do
 
		`background save
		paste image 65535,0,0
 
		`main window
		endx = posx + 120
		endy = posy + 120
		if endx > screen width() - 1 then endx = screen width() - 1
		if endy > screen height() - 1 then endy = screen height() - 1
		ink rgb(255,255,255),0
		box posx, posy, endx - 1, endy - 1
		ink rgb(10,10,10),0
		box posx + 1, posy + 1, endx, endy
		ink color,0
		box posx + 1, posy + 1, endx - 1, endy - 1
 
		`display
		endx = posx + 115
		endy = posy + 25
		if endx > screen width() - 1 then endx = screen width() - 1
		if endy > screen height() - 1 then endy = screen height() - 1
		ink rgb(10,10,10),0
		box posx + 5, posy + 5, endx - 1, endy - 1
		ink rgb(255,255,255),0
		box posx + 6, posy + 6, endx, endy
		ink rgb(255,255,255),0
		box posx + 6, posy + 6, endx - 1, endy - 1
		ink 0,0
		display$ = number$(cnumber)
		if display$ = "" then display$ = "0"
		text posx + 6, posy + 6, display$
 
		`calc_buttons
		for x = 1 to 3
			for y = 1 to 3
				nr = ((x-1)*3) + y
				if calc_button((posx + 5) + ((y-1)*21), (posy + 30) + ((x-1)*21), str$(nr), color) > 0 and hold = 0 and len(number$(cnumber)) < 10
					if operator = 5 then operator = 0 : number$(1) = ""
					hold = 1
					number$(cnumber) = number$(cnumber) + str$(nr)
				endif
			next y
		next x
		if calc_button(posx + 5, posy + 93, "0", color) > 0 and hold = 0 and len(number$(cnumber)) < 10
			if operator = 5 then operator = 0 : number$(1) = ""
			hold = 1 
			number$(cnumber) = number$(cnumber) + "0"
		endif
		if calc_button(posx + 26, posy + 93,".", color) > 0 and hold = 0 then hold = 1 : number$(cnumber) = number$(cnumber) + "."
		`operators
		if calc_button(posx + 68, posy + 30, "CA", color) > 0 and hold = 0
			hold = 1
			number$(1) = ""
			number$(2) = ""
			operator = 0
		endif
		if calc_button(posx + 68, posy + 51, "+", color) > 0 and hold = 0
			hold = 1
			if cnumber = 1
				inc cnumber
				number$(2) = ""
			else
				first# = val(number$(1))
				sec# = val(number$(2))
				select operator
					case 1 : number$(1) = str$(first# + sec#) : endcase
					case 2 : number$(1) = str$(first# - sec#) : endcase
					case 3 : number$(1) = str$(first# * sec#) : endcase
					case 4 : number$(1) = str$(first# / sec#) : endcase
				endselect
				if len(number$(1)) > 10 then number$(1) = "ERROR"
				number$(2) = ""
			endif
			operator = 1
		endif
		if calc_button(posx + 68, posy + 72, "-", color) > 0 and hold = 0
			hold = 1
			if cnumber = 1
				inc cnumber
				number$(2) = ""
			else
				first# = val(number$(1))
				sec# = val(number$(2))
				select operator
					case 1 : number$(1) = str$(first# + sec#) : endcase
					case 2 : number$(1) = str$(first# - sec#) : endcase
					case 3 : number$(1) = str$(first# * sec#) : endcase
					case 4 : number$(1) = str$(first# / sec#) : endcase
				endselect
				if len(number$(1)) > 15 then number$(1) = "ERROR"
				number$(2) = ""
			endif
			operator = 2
		endif
		if calc_button(posx + 89, posy + 51, "*", color) > 0 and hold = 0
			hold = 1
			if cnumber = 1
				inc cnumber
				number$(2) = ""
			else
				first# = val(number$(1))
				sec# = val(number$(2))
				select operator
					case 1 : number$(1) = str$(first# + sec#) : endcase
					case 2 : number$(1) = str$(first# - sec#) : endcase
					case 3 : number$(1) = str$(first# * sec#) : endcase
					case 4 : number$(1) = str$(first# / sec#) : endcase
				endselect
				if len(number$(1)) > 15 then number$(1) = "ERROR"
				number$(2) = ""
			endif
			operator = 3
		endif
		if calc_button(posx + 89, posy + 72, "/", color) > 0 and hold = 0
			hold = 1
			if cnumber = 1
				inc cnumber
			else
				first# = val(number$(1))
				sec# = val(number$(2))
				select operator
					case 1 : number$(1) = str$(first# + sec#) : endcase
					case 2 : number$(1) = str$(first# - sec#) : endcase
					case 3 : number$(1) = str$(first# * sec#) : endcase
					case 4 : number$(1) = str$(first# / sec#) : endcase
				endselect
				if len(number$(1)) > 15 then number$(1) = "ERROR"
				number$(2) = ""
			endif
			operator = 4
		endif
		if calc_button(posx + 47, posy + 93, "-#", color) > 0 and hold = 0 and len(number$(cnumber)) = 0
			hold = 1
			number$(cnumber) = number$(cnumber) + "-"
		endif
		if calc_button(posx + 89, posy + 93, "=", color) > 0 and hold = 0 and cnumber = 2
			cnumber = 1
			first# = val(number$(1))
			sec# = val(number$(2))
			select operator
				case 1 : number$(1) = str$(first# + sec#) : endcase
				case 2 : number$(1) = str$(first# - sec#) : endcase
				case 3 : number$(1) = str$(first# * sec#) : endcase
				case 4 : number$(1) = str$(first# / sec#) : endcase
			endselect
			if len(number$(1)) > 15 then number$(1) = "ERROR"
			operator = 5
		endif
 
		`exit calc_button
		if calc_button(posx + 89, posy + 30, "EX", color) > 0 and hold = 0 then hold = 1 : exit
 
		`unlock mouse
		if mouseclick() = 0 then hold = 0
 
		`if moving is allowed
		if move = 1
			if mousex() > posx and mousex() < posx + 120
				if mousey() > posy and mousey() < posy + 5
					if mouseclick() = 1 and moving = 0
						moving = 1
						difx = mousex() - posx
						dify = mousey() - posy
					endif
				endif
			endif
			if moving = 1
				posx = mousex() - difx
				posy = mousey() - dify
				if mouseclick() = 0 then moving = 0
			endif
		endif
		`boundries
		if posx < 0 then posx = 0
		if posy < 0 then posy = 0
		if posx > screen width() - 1 then posx = screen width() - 1
		if posy > screen height() - 1 then posy = screen height() - 1		
 
		sync
	loop
 
	`reset all
	cls
	set text font defaultfont$
	set text size defaultsize
	select defaultstyle
		case 1 : set text to italic : endcase
		case 2 : set text to bold : endcase
		case 3 : set text to bolditalic : endcase
	endselect
	if defaultback = 1
		set text transparent
	else
		set text opaque
	endif
 
endfunction val(number$(1))
 
function calc_button(x,y,content$,color)
	pressed = 0
	`layout
	if mousex() < x + 20 and mousex() > x
		if mousey() < y + 20 and mousey() > y
			if mouseclick() = 1 then pressed = 1
		endif
	endif
	endx = x + 20
	endy = y + 20
	if endx > screen width() - 1 then endx = screen width() - 1
	if endy > screen height() - 1 then endy = screen height() - 1
	if pressed = 1	
		ink rgb(20,20,20),0
		box x, y, endx - 1, endy - 1
		ink rgb(255,255,255),0
		box x + 1, y + 1, endx, endy
	else
		ink rgb(20,20,20),0
		box x + 1, y + 1, endx, endy
		ink rgb(255,255,255),0
		box x, y, endx - 1, endy - 1
	endif
	ink color,0
	box x + 1, y + 1, endx - 1, endy - 1 
	ink 0,0
	center text x + 10, y + 10 - (text size()/2), content$
endfunction pressed