sync on
 
d=8
a=4
addx=0
addy=0
num$="0"
 
dim bp(d,a,2)
for i=1 to d
	for j=1 to a
		bp(i,j,1)=j*40+addx
		bp(i,j,2)=i*15+30+addy
	next j
next i
dim b$(d,a)
b$(1,1)="sqrt" :b$(1,2)="cos"		:b$(1,3)="sin"		:b$(1,4)="tan"
b$(2,1)="hex"	:b$(2,2)="acos"	:b$(2,3)="asin"	:b$(2,4)="atan"
b$(3,1)="bin"	:b$(3,2)="hcos"	:b$(3,3)="hsin"	:b$(3,4)="htan"
b$(4,1)="pwr"	:b$(4,2)="pstv"	:b$(4,3)="int"		:b$(4,4)="="
b$(5,1)="7"		:b$(5,2)="8"		:b$(5,3)="9"		:b$(5,4)="/"
b$(6,1)="4"		:b$(6,2)="5"		:b$(6,3)="6"		:b$(6,4)="*"
b$(7,1)="1"		:b$(7,2)="2"		:b$(7,3)="3"		:b$(7,4)="-"
b$(8,1)="CE"	:b$(8,2)="0"		:b$(8,3)="(.)"		:b$(8,4)="+"
 
ink rgb(130,130,130),0
box bp(1,1,1)-5,bp(1,1,2)-5,bp(d,a,1)+32+5,bp(d,a,2)+13+5
ink rgb(150,150,150),0
box bp(1,1,1)-5,bp(1,1,2)-5-20,bp(d,a,1)+32+5,bp(1,a,2)-5
ink rgb(50,50,50),0
box bp(1,1,1),bp(1,1,2)-5-18,bp(d,a,1)+32,bp(1,a,2)-8
ink rgb(255,255,255),0
linebox(bp(1,1,1)-5-1,bp(1,1,2)-5-20-1,bp(d,a,1)+32+5+1,bp(d,a,2)+13+5+1)
linebox(bp(1,1,1)-5-1,bp(1,1,2)-5-20,bp(d,a,1)+32+5+1,bp(1,a,2)-5+1)
 
for i=1 to d
	for j=1 to a
		ink rgb(170,170,170),0
		box bp(i,j,1),bp(i,j,2),bp(i,j,1)+32,bp(i,j,2)+13
		ink rgb(255,255,255),0		
		center text bp(i,j,1)+16,bp(i,j,2),b$(i,j)
	next j
next i
 
do
 
	gosub displaynum
	gosub handlebuttons
 
	sync
 
loop
 
end
 
rem ************
rem sub-routines
rem ************
 
displaynum:
	ink rgb(50,50,50),0
	box bp(1,1,1),bp(1,1,2)-5-18,bp(d,a,1)+32,bp(1,a,2)-8
	if text width(num$)>100
		while text width(num$)>100
			num$=right$(num$,len(num$)-1)
			sync
		endwhile
	endif
	ink rgb(255,255,255),0
	text (bp(d,a,1)+32)-text width(num$),bp(1,a,2)-5-18,num$
return
 
handlebuttons:
	if mouseclick()=1
		if mouseclicked=0
			mouseclicked=1
			for i=1 to 8
				for j=1 to 4
					if mousewithin(bp(i,j,1),bp(i,j,2),bp(i,j,1)+32,bp(i,j,2)+13)
						num$=getavalue(num$,b$(i,j),addx,addy) 
					endif
				next j
			next i
		endif
	else
		mouseclicked=0
	endif
return
 
rem *********
rem functions
rem *********
 
function linebox(x1,y1,x2,y2)
 
	line x1,y1,x1,y2
	line x1,y2,x2,y2
	line x2,y2,x2,y1
	line x2,y1,x1,y1
 
endfunction
 
 
function mousewithin(x1,y1,x2,y2)
  if mousex()>x1 and mousex()<x2 and mousey()>y1 and mousey()<y2 then exitfunction 1
endfunction 0
 
function getavalue(num$,t$,addx,addy)
 
	mode=0
	value$=num$
 
	`if a=0
		if t$="0" or t$="1" or t$="2" or t$="3" or t$="4" or t$="5" or t$="6" or t$="7" or t$="8" or t$="9"
			value$=str$(val(num$+t$))
	`		a=1
		endif
	`else
	`	num$="0" : a=0
	`endif
	if t$="CE" then value$="0"
	if t$="sqrt" then value$=str$(int(sqrt(val(num$))))
	if t$="cos" then value$=str$(int(cos(val(num$))))
	if t$="sin" then value$=str$(int(sin(val(num$))))
	if t$="tan" then value$=str$(int(tan(val(num$))))
	if t$="acos" then value$=str$(int(acos(val(num$))))
	if t$="asin" then value$=str$(int(asin(val(num$))))
	if t$="atan" then value$=str$(int(atan(val(num$))))
	if t$="hcos" then value$=str$(int(hcos(val(num$))))
	if t$="hsin" then value$=str$(int(hsin(val(num$))))
	if t$="htan" then value$=str$(int(htan(val(num$))))
	if t$="pstv" then value$=str$(int(abs(val(num$))))
	if t$="bin" then value$=bin$(val(num$))
	if t$="hex" then value$=str$(val(hex$(val(num$))))
	if t$="pwr" then mode=1
	if t$="/" then mode=2
	if t$="*" then mode=3
	if t$="-" then mode=4
	if t$="+" then mode=5
 
	if mode>0 then value$=getavalueloop(num$,mode,addx,addy)
 
endfunction value$
 
 
function getavalueloop(num$,mode,addx,addy)
 
	done=0
	d=8
	a=4
	temp$="0"
 
	while done=0
		if mouseclick()=1
			if mouseclicked=0
				mouseclicked=1
				for i=1 to 8
					for j=1 to 4
						if mousewithin(bp(i,j,1),bp(i,j,2),bp(i,j,1)+32,bp(i,j,2)+13)
							if b$(i,j)="CE" then value$=num$ : done=2
							if b$(i,j)="0" or b$(i,j)="1" or b$(i,j)="2" or b$(i,j)="3" or b$(i,j)="4" or b$(i,j)="5" or b$(i,j)="6" or b$(i,j)="7" or b$(i,j)="8" or b$(i,j)="9"
								temp$=str$(val(temp$+b$(i,j)))
							endif
							if b$(i,j)="=" then done=1
						endif
					next j
				next i
			endif
		else
			mouseclicked=0
		endif
		ink rgb(50,50,50),0
		box bp(1,1,1),bp(1,1,2)-5-18,bp(d,a,1)+32,bp(1,a,2)-8
		if text width(temp$)>100
			while text width(temp$)>100
				temp$=right$(temp$,len(temp$)-1)
				sync
			endwhile
		endif
		ink rgb(255,255,255),0
		text (bp(d,a,1)+32)-text width(temp$),bp(1,a,2)-5-18,temp$
		sync
	endwhile
	if done=1
		temp2=val(num$)
		if mode=1 then temp2=exp(val(temp$)) : value$=str$(temp2)
		if mode=2 then value$=str$(temp2/val(temp$))
		if mode=3 then value$=str$(temp2*val(temp$))
		if mode=4 then value$=str$(temp2-val(temp$))
		if mode=5 then value$=str$(temp2+val(temp$))
		value$=str$(int(val(value$)))
	endif
endfunction value$