Page 1 of 1

Numerical keypad for quick number entry

Posted: Sun Jan 19, 2014 1:02 pm
by Henko

Code: Select all

' testprogram for numerical keypad object
'
' {/initgraph}
option base 1 ! option angle degrees
graphics ! graphics clear .8,.8,.8
fill color .8,.8,.8 ! draw color 0,0,0

draw text "number entered is " & numpad(100,100,60,-10,10) at 10,300
end

' numerical keypad object
' 
' produce a simpel keypad to quickly enter a number in an app
' upon entry, the keypad disappears
' left upper corner is placed at "xtop,ytop"
' "bs" is the button size (keypad becomes 4.3 times larger)
' size of number is accepted between "minval" and "maxval"
' if both "minval" and "maxval" are zero, then no restrictions
' max number of tokens in the number is 10 (minus and dot included)
'
def numpad(xtop,ytop,bs,minval,maxval)
sp=3 ! th=bs-16 ! pflag=0 ! sflag=0
if bs<20 then bs=20
draw font size .9*bs-18
ww=4*bs+5*sp ! hh=th+4*bs+6*sp
xbot=xtop+ww ! ybot=ytop+hh
sprite "bg" scan xtop-2,ytop-2,ww+4,hh+4

r=10 ! graphics lock ' open the keypad window
draw color 0,0,0 ! draw size 4
draw circle xtop,ytop to xtop+20,ytop+20
draw circle xbot-20,ytop to xbot,ytop+20
draw circle xtop,ybot-20 to xtop+20,ybot
draw circle xbot-20,ybot-20 to xbot,ybot
draw line xtop+r,ytop to xbot-r,ytop
draw line xtop+r,ybot to xbot-r,ybot
draw line xtop,ytop+r to xtop,ybot-r
draw line xbot,ytop+r to xbot,ybot-r
fill rect xtop+r,ytop+2 to xbot-r,ybot-2
fill rect xtop+2,ytop+r to xbot-2,ybot-r
draw line xtop,ytop+th to xbot,ytop+th
graphics unlock

button "0" title "0" at xtop+sp,ytop+th+3*bs+5*sp size bs,bs
button "1" title "1" at xtop+sp,ytop+th+2*bs+4*sp size bs,bs
button "2" title "2" at xtop+2*sp+bs,ytop+th+2*bs+4*sp size bs,bs
button "3" title "3" at xtop+3*sp+2*bs,ytop+th+2*bs+4*sp size bs,bs
button "4" title "4" at xtop+sp,ytop+th+bs+3*sp size bs,bs
button "5" title "5" at xtop+2*sp+bs,ytop+th+bs+3*sp size bs,bs
button "6" title "6" at xtop+3*sp+2*bs,ytop+th+bs+3*sp size bs,bs
button "7" title "7" at xtop+sp,ytop+th+2*sp size bs,bs
button "8" title "8" at xtop+2*sp+bs,ytop+th+2*sp size bs,bs
button "9" title "9" at xtop+3*sp+2*bs,ytop+th+2*sp size bs,bs
button "-" title "-" at xtop+2*sp+bs,ytop+th+3*bs+5*sp size bs,bs
button "." title "." at xtop+3*sp+2*bs,ytop+th+3*bs+5*sp size bs,bs
button "Cl" title "C" at xtop+4*sp+3*bs,ytop+th+2*sp size bs,bs
button "del" title "<-" at xtop+4*sp+3*bs,ytop+th+bs+3*sp size bs,bs
button "ok" title "ok" at xtop+4*sp+3*bs,ytop+th+2*bs+4*sp size bs,2*bs+sp


a$=""
nump1:
if b_s("ok") then
  number=val(a$)
  if minval<>0 or maxval<>0 then
    if number<minval or number>maxval then
      fill rect xtop+8,ytop+2 to xbot-10,ytop+th-2
      draw color .6,0,0 ! draw text "range err" at xtop+8,ytop+2
      pflag=0 ! a$="" ! pause 1
      fill rect xtop+8,ytop+2 to xbot-10,ytop+th-2
      sprite "bg" at xtop-2,ytop-2 ! sprite "bg" stamp
      goto nump1
      end if
    end if
  button "-" delete ! button "." delete ! button "Cl" delete
  button "del" delete ! button "ok" delete
  for i=0 to 9 ! button i delete ! next i
  fill rect xtop-2,ytop-2 to xbot+2,ybot+2
  draw font size 24
  numpad=number ! return
  end if

if b_s("Cl") then
  a$ = "" ! pflag=0 ! sflag=0 ! goto nump3
  end if
if b_s("del") and len(a$) then
  ll=len(a$) ! if substr$(a$,ll,ll)="." then pflag=0
  a$ = left$(a$,ll-1) ! sflag=0 ! goto nump3
  end if
if b_s("-") then
  a$ = "-" ! pflag=0 ! sflag=0 ! goto nump3
  end if
if b_s(".") and not pflag and not sflag then
  a$ &= "." ! pflag=1 ! goto nump3
  end if
for k=0 to 9
  t$=k
  if b_s(t$) and not sflag then
    a$ &= t$ ! goto nump3
    end if
  next k

goto nump1

nump3:
if len(a$)>10 then ! sflag=1 ! goto nump1 ! end if
fill rect xtop+8,ytop+2 to xbot-6,ytop+th-2
draw color 0,0,1 ! draw text a$ at xtop+8,ytop+2 ! draw color 0,0,0
goto nump1

end def

def b_s(n$) = button_pressed(n$)