`--- creating a grid using the mouse ---
 
sync on
 
DO
 
`store stable mouse positions
if mousex() > xpos then mx = mousex()
if mousey() > ypos then my = mousey()
 
if mouseclick() = 1
   `prime release switch
   release_switch = 1
endif
 
if release_switch = 1 and mouseclick() = 0
   `reset release_switch
   release_switch = 0
   `next phase
   inc drag_switch
endif
 
Select drag_switch
   Case 1
      `set grid origin
      xpos= mx : ypos= my
      `go straight to width
      inc drag_switch
   endcase
   Case 2
      `grid width
      xsize = mx - xpos
      width = xsize
   endcase
   Case 3
      `grid height
      ysize= my - ypos
      length = ysize
   endcase
   Case 4
      `printing variable
      check = col
      `segment x
      col = (mx-xpos)/(width-xpos) * 20 : if col < 1 then col = 1
      `adjust cell width to keep grid width the same
      xsize = width/col
   endcase
   Case 5
      `segment y
      row = (my-ypos)/(length-ypos) * 20 : if row < 1 then row = 1
      `adjust cell width to keep grid width the same
      ysize = length/row
   endcase
   case >0
      if mx < xpos then mx = xpos
      if my < ypos then my = ypos
   endcase
   case 0
      `reset from previous grid
      xpos= 0 : ypos= 0 : col= 1 : row= 1 : xsize= 0 : ysize= 0
   endcase
   case default : drag_switch= 0 : endcase
Endselect
 
cls
set cursor 0,0
print check
position mouse mx,my
Grid(xpos,ypos,col,row,xsize,ysize,rgb(255,255,255),rgb(100,0,100))
sync
LOOP
 
END
 
Function Grid(XPosition,YPosition,Columns,Rows,XSize,YSize,CellColourF,CellColourB)
  Ink CellColourB,0
  Box XPosition,YPosition,XPosition+(Columns*(XSize+1)),YPosition+(Rows*(YSize+1))
  Ink CellColourF,0
  For N=0 To Rows
    Line XPosition,  N*(YSize+1)+YPosition,  Columns*(XSize+1)+XPosition,  N*(YSize+1)+YPosition
  Next N
  For N=0 To Columns
    Line N*(XSize+1)+XPosition,  YPosition,  N*(XSize+1)+XPosition,  Rows*(YSize+1)+YPosition
  Next N
Endfunction