%begin
%include "inc:mouse.imp"
%include "kcn:1point5.imp"

%begin
%const %integer left=10,right=670,down=10,up=502
%const %integer boxcur=1,cross=2,true=1,false=0
%short x=0,y=0,oldx,oldy,off,col,coord=50,q=128,num=1,miss=0

   %integer %function radius length(%integer X,Y)
      %result = int(sqrt((X*X)+(Y*Y)))
   %end

   %routine cursor(%integer X,Y ,%integer type)
      %if type=1 %then %start
      at(x,y)
      show symbol(q)
      %finish %else %start
         line(x,y,x,y+100)
         line(x,y+100,x+100,y+100)
         line(x+100,y+100,x+100,y)
         line(x+100,y,x,y)
      %finish
   %end

   %routine pan
      %own %integer up=false
      %if up = true %start
         up=false
         offset(0,512)
      %finish %else %start
         up=true
         offset(0,0)
      %finish
   %end

   %routine box(%short X0,Y0,X1,Y1)
      hline(X0,X1,Y0)
      vline(X0,Y0,Y1)
      hline(X0,X1,Y1)
      vline(X1,Y0,Y1)
   %end

   %byte %function get buttons(%integer delay)
      %byte M,N
      %integer i
      %cycle
         M=mouse buttons
         N = mouse buttons %for i=1,1,delay
      %repeat %until M=N
      %result=M
   %end

   read default font
   append font("dgd_1:cur1")
   mouseX=0
   mouseY=0
   disable(0)
   clear all
   offset(0,0)
   col=blue
   set color(col)
   fill(200,200,400,400)
   off=0
   oldx=x;oldY=y
   set color(red)
   box(left,down,right,up)
   at(30,30)
   show string("SELECT menu item")

   %routine get
      %return %if oldx=x %and oldY=y 
      disable(RGBplane)
      set color(black)
      cursor(oldx,oldY,boxcur)
      oldx=x;oldy=y
      set color(invert)
      cursor(x,y,boxcur)
      disable(0)
   %end

   %routine draw box
      %integer dx,dy
      dx=x;dy=y
      %while get buttons(400)=msl %cycle
         get
         x=(mouseX//4)&1023
         y=(mouseY//4)&1023
         %if x>left %and x<right %and y>down %and y<up %start
            disable(RGBplane)
            set color(black)
            box(dx,dy,oldx,oldy)
            set color(invert)
            box(dx,dy,x,y)
         %finish
      %repeat
      disable(0)
      set color(col)
      box(dx,dy,x,y)
   %end

   %routine draw ring
      %integer dx,dy,r
      dx=x;dy=y
      %while get buttons(300)=mslm %cycle
         get
         x=(mouseX//4)&1023
         y=(mouseY//4)&1023
         r=radius length(|dx-x|,|dy-y|)+1
         %if x-r>left %and x+r<right %and y-r>down %and y+r<up %start
            disable(RGBplane)
            set color(black)
            round(dx,dy,radius length(|dx-oldX|,|dy-oldY|)+1)
            set color(invert)
            round(dx,dy,r)
         %finish
      %repeat
      disable(0)
      set color(col)
      round(dx,dy,radius length(|dx-x|,|dy-y|)+1)
   %end

   %cycle
      at(400,coord)
      set color(black)
      show symbol(129)
      coord=coord+num
      at(400,coord)
      set color(col)
      show symbol(128)
      %if coord>=70 %then num=-1
      %if coord<=50 %then num=1
      x=(mouseX//4)&1023
      y=(mouseY//4)&1023
      %if get buttons(400) = msl %then draw box
      %if get buttons(600) = mslm %then draw ring
      %if get buttons(300) = msm %then q=q+1
      %if q>=135 %then q=128
      %if get buttons(200)=msr %then %start
         col=col+1
         %if col>=8 %then col=0
         set color(col)
         fill(200,200,400,400)
      %finish
      %if oldx#x %and oldY#y %then fill(200,200,400,400)
      get
   %repeat
%end
%endofprogram
