Rem Mouse/Grid Selection
Rem Cory Thornsberry
Rem 8/23/07
 
Rem Startup------------------------------------------------
Sync On : Sync Rate 30
Backdrop On
Color Backdrop 0
Hide Mouse
 
Rem Main Variables-----------------------------------------
` The grid can be as large as you want (supposedly)
Width=50 : Height=50
Columns=8 : Rows=8
R=0 : G=255 : B=0
 
Debug=1
 
Rem Setup-World--------------------------------------------
GoSub Setup_World
GoSub New_Game
Update_Grid(Width,Height,Columns,Rows,R,G,B)
 
Rem Main Loop----------------------------------------------
do
   ` Other
   Time1=Timer()
   Set Cursor 1,(Rows*(Height+1))
 
   ` 2-D
   Sprite 1,0,0,1 : ` Grid
   Sprite 2,MouseX()-12,MouseY()-12,2 : ` Cursor
   Print "Loop took ",Time2," ms to run"
 
   ` GoSubs
   If MouseClick() = 1 then GoSub Grid_Selection
   If ControlKey() = 1 then C=0 : R=0 : Update_Grid(Width,Height,Columns,Rows,R,G,B)
   If C > 0 and R > 0
      GoSub Mark_Selection
      If C > 0 and R > 0 then Print "Current Cell ",C,",",R
   EndIf
 
   ` Loop Timer
   Time2=Timer()-Time1
 
   Sync
Loop
 
Rem Sub-Routines-------------------------------------------
Setup_World:
   ` Draw Cursor
   INK RGB(255-R,255-G,255-B),0 : ` Invert Color
   Create Bitmap 1,24,24
   Line 12,0,12,24
   Line 0,12,24,12
   Get Image 2,0,0,24,24
   Delete Bitmap 1
Return
 
Grid_Selection:
   ` Check grid bounds
   If MouseX() < (Columns*(Height+1))
      If MouseY() < (Rows*(Width+1))
         ` Check Columns
         For n=0 to Columns-1
            If MouseX() > (n*(Width+1)) and MouseX() < ((n+1)*(Width+1)) then C=(n+1)
         Next n
 
         ` Check Rows
         For n=0 to Rows-1
            If MouseY() > (n*(Height+1)) and MouseY() < ((n+1)*(Height+1)) then R=(n+1)
         Next n
      EndIf
   EndIf
Return
 
Mark_Selection:
   If Debug = 1
      ` Make a selection marker
      INK RGB(0,0,255),0
      Box ((C-1)*(Width+1)),((R-1)*(Height+1)),(C*(Width+1)),(R*(Height+1))
      INK RGB(R,G,B),0
   EndIf
Return
 
Rem Functions----------------------------------------------
Function Update_Grid(w,h,c,r,Rd,Gn,Bl)
   ` Top, Bottom, and Sides
   INK RGB(Rd,Gn,Bl),0
   Top=(w*c)+c : Print Top
   Side=(h*r)+r : Print Side
 
   ` Bitmap Commands
   Create Bitmap 1,(c*(h+1))+1,(r*(w+1))+1
   Set Current Bitmap 1
 
   ` Vertical Lines
   For n=0 to c
      Line (n*(w+1)),0,(n*(w+1)),Side
   Next n
 
   ` Horizontal Lines
   For n=0 to r
      Line 0,(n*(h+1)),Top,(n*(h+1))
   Next n
 
   ` Grid Sprite (faster than drawing every loop)
   If Image Exist(1)=1 then Delete Image 1
   Get Image 1,0,0,(c*(h+1))+1,(r*(w+1))+1
   Delete Bitmap 1
EndFunction