' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2023.10.26.02.14]) on 2023.11.08 at 03:53 (Coordinated Universal Time)
_TITLE "BAM Draw"
OPTION EXPLICIT
' Program by Charlie Veniot

' 🟠🟠🟠 DECLARATIONS

' multi-use variables
  VAR temp$, what_drawing$, this_mark$, this_marker$, this_f_color%, this_b_color%
  VAR i%, iy%, ix%

' screen variables
  CONST minw% = 33, maxw% = 260, minh% = 1, maxh% = 51
  VAR w% = 0, h% = 0
  VAR grid% = TRUE
  
' menu constants / variables
  CONST menuh% = 3
  CONST fc4bc%(0 to 15) = {15,15,15,15,15,15,15,0,15,15,0,0,0,0,0,0}
  VAR color_menu_mode$ = "Foreground"
  CONST mmp$(1 TO 3) = {"Grid","Wdth","Hgth"}
  FOR i% = 1 TO 3 : _MAPSET(mmp$(i%), i%) : NEXT i%
  VAR more_menu_pick$ = "Grid"
  
' marker variables
  VAR marker$ = " "
  VAR f_color% = 15, b_color% = 0
  
' mouse/touch variables  
  VAR x%, y%
  
' 🟠🟠🟠 MAIN PROGRAM

GOSUB ⚙init_program

🔃drawing_cycle:
  _DISPLAY
  LET temp$ = INKEY$
  IF temp$ <> "" THEN marker$ = temp$ : GOSUB ✒set_marker
  IF _MOUSEBUTTON THEN
     IF _MOUSEZONE (0, 0, _WIDTH, [_HEIGHT-(menuh%*8)] ) _
        THEN GOSUB ✒do_marker_mark : GOTO ⏩done
     IF _MOUSEZONE(40, yMAX - 7, 64, 8 ) _
        THEN GOSUB ✒change_marker : GOTO ⏩done
     IF _MOUSEZONE(0, yMAX - 7, 32, 8 ) _
        THEN GOSUB 🖥do_variable_menu_handling : GOTO ⏩done
     IF _MOUSEZONE(0, yMAX - 15, 32, 8 ) _
        THEN GOSUB 🖥toggle_variable_menu : GOTO ⏩done
     IF _MOUSEZONE(8*5, yMAX - 15, 88, 8 ) _
        THEN GOSUB 🎨toggle_color_menu_mode : GOTO ⏩done
     IF _MOUSEZONE(8*6+88, yMAX - 15, 128, 16 ) _
        THEN GOSUB 🎨change_color : GOTO ⏩done
     ⏩done:
  END IF
  
  GOTO 🔃drawing_cycle

END

' 🟠🟠🟠 SUBROUTINES

⚙init_program:
  IF GetLocalStorageItem( "draw:height" ) <> "" _
     THEN LET what_drawing$ = _PROMPT("Continue previous drawing or start a new drawing?\n\n  'C' to continue previous drawing, 'N' for new drawing", "C")
  IF UCASE$( what_drawing$ ) = "C" _
     THEN GOSUB ⚙do_continue_canvas _
     ELSE GOSUB ⚙do_new_canvas
  RETURN
  
⚙do_new_canvas:
     GOSUB ⚙prompt_width
     GOSUB ⚙prompt_height
     FOR iy% = 1 TO MAX( minh% , GetLocalStorageItem( "draw:width" ) )
         FOR ix% = 1 TO MAX( minw%, GetLocalStorageItem( "draw:width" ) )
             RemoveLocalStorageItem("draw:mark:" + iy% +"," + ix%)
         NEXT ix%
     NEXT iy%
     GOSUB ✒set_marker
     GOSUB 🖥setup_screen
  RETURN
  
⚙prompt_width:
  LET i% = _PROMPT( "Canvas Width (i.e. how many characters wide, minimum = " + minw% + ")", MAX(w%/8,minw%) )
  LET w% = IFF(i% "" THEN
             LET this_marker$ = LEFT$(this_mark$, 1) : LET this_mark$ = RIGHT$(this_mark$, LEN(this_mark$) - 2)
				     LET this_f_color% = VAL(LEFT$(this_mark$, INSTR(this_mark$, ",") - 1))
				     LET this_b_color% = VAL(RIGHT$(this_mark$, LEN(this_mark$) - INSTR(this_mark$, ",")))
				     COLOR this_f_color%, this_b_color%
             LOCATE iy%, ix% : PRINT this_marker$;
          END IF
      NEXT ix%
  NEXT iy%
  COLOR f_color%, b_color%
  RETURN

🖥toggle_variable_menu:
  WHILE _MOUSEBUTTON : WEND
  i% = _MAPGET(more_menu_pick$) + 1 : IF i% > 3 THEN i% = 1
  more_menu_pick$ = mmp$(i%)
  GOSUB 🖥show_more_menu_pick
  RETURN

🖥do_variable_menu_handling:
  WHILE _MOUSEBUTTON : WEND
  ON _MAPGET(more_menu_pick$) GOSUB 🖥toggle_grid, ⚙prompt_width, ⚙prompt_height
  IF BETWEEN(_MAPGET(more_menu_pick$), 2, 3) THEN GOSUB 🖥setup_screen : GOSUB 🖥refresh_canvas
  RETURN

🖥toggle_grid:
  grid% = NOT grid%
  IF grid% THEN GOSUB 🖥do_grid ELSE GOSUB 🖥undo_grid
  RETURN

🖥do_grid:
     FOR i% = 0 TO xMAX STEP 8
  	     LINE (i%,0) TO (i%,(yMAX-menuh%*8)), 7
     NEXT i%
     FOR i% = 7 TO (yMAX-menuh%*8) STEP 8
         LINE (0,i%) TO (xMAX,i%), 7
     NEXT i%
  RETURN
  
🖥undo_grid:
     FOR i% = 0 TO xMAX STEP 8
  	     LINE (i%,0) TO (i%,(yMAX-menuh%*8)), 0
     NEXT i%
     FOR i% = 7 TO (yMAX-menuh%*8) STEP 8
         LINE (0,i%) TO (xMAX,i%), 0
     NEXT i%
     GOSUB 🖥refresh_canvas
  RETURN
  
🖥setup_screen:
'  IF w% <> [ MAX(minw%, GetLocalStorageItem( "draw:width" ) ) ] * 8                _
'     OR h% <> [ MAX(minh%, GetLocalStorageItem( "draw:height" ) ) + menuh% ] * 8 _
'    THEN
      LET w% = [ MAX(minw%, GetLocalStorageItem( "draw:width" ) ) ] * 8
      LET h% = [ MAX(minh%, GetLocalStorageItem( "draw:height" ) ) + menuh% ] * 8 
      SCREEN _NEWIMAGE( w%, h%, 7 )
      GOSUB 🖥do_grid
      COLOR 15, 0
      LINE (0,_HEIGHT-20) TO (xMAX,_HEIGHT-20) : LINE (35,_HEIGHT-20) TO (36, yMAX), ,B
      LOCATE (_HEIGHT/8)-1, 1 : PRINT STRING$(4,CHR$(240));
      LOCATE (_HEIGHT/8), 6 : PRINT "Marker:"; : GOSUB ✒set_marker
      GOSUB 🖥show_more_menu_pick
      GOSUB 🎨show_color_menu
      GOSUB 🎨show_color_picks
'  END IF
  RETURN
  
🖥show_more_menu_pick:
  COLOR 15, 0
  LOCATE (_HEIGHT/8), 1 : PRINT more_menu_pick$;
  COLOR f_color%, b_color%
  RETURN
  
✒change_marker:
  WHILE _MOUSEBUTTON : WEND
  LET marker$ = LEFT$(_PROMPT("Enter one simple keyboard character as a new marker shape.", marker$)+marker$, 1)
  GOSUB ✒set_marker
  RETURN
  
✒do_marker_mark:
  LET x% = _MOUSEX : LET y% = _MOUSEY
  LET iy% = FIX(y%/8)+1 : LET ix% = FIX(x%/8)+1
  COLOR f_color%, b_color%
  LOCATE iy%, ix% : PRINT marker$;
  IF grid% THEN GOSUB 🖥do_grid 
  SetLocalStorageItem( "draw:mark:" + iy% + "," + ix%, marker$ + " " + f_color% + "," + b_color%)
  RETURN

✒set_marker:
	LOCATE (_HEIGHT/8), 13 : COLOR f_color%, b_color% : PRINT marker$;
	SetLocalStorageItem( "draw:marker", marker$ )
	RETURN
  
🎨toggle_color_menu_mode:
  WHILE _MOUSEBUTTON : WEND
  LET color_menu_mode$ = IFF(color_menu_mode$="Foreground", "Background", "Foreground")
  GOSUB 🎨show_color_menu
  RETURN
  
🎨show_color_menu:
  COLOR 15,0
  LOCATE (_HEIGHT/8)-1, 6 : PRINT color_menu_mode$ + ":";
  COLOR f_color%, b_color%
  RETURN
  
🎨show_color_picks:
  FOR i% = 0 TO 15
	   LOCATE (_HEIGHT/8)-IFF(i%<8,1,0),17+([i%-IFF(i%<8,0,8)]*2) : COLOR i%, 0 : PRINT CHR$(219) + CHR$(219);
     IF i% = f_color% THEN LOCATE (_HEIGHT/8)-IFF(i%<8,1,0),17+([i% - IFF(i%<8,0,8)]*2) : COLOR fc4bc%(i%),i% : PRINT "F";
     IF i% = b_color% THEN LOCATE (_HEIGHT/8)-IFF(i%<8,1,0),17+([i% - IFF(i%<8,0,8)]*2)+1 : COLOR fc4bc%(i%),i% : PRINT "B";
  NEXT i%
	COLOR 15,0
  RETURN
  
🎨change_color:
  WHILE _MOUSEBUTTON : WEND
  LET f_color% = IFF(color_menu_mode$="Foreground", POINT(_MOUSEX,_MOUSEY), f_color%)
  LET b_color% = IFF(color_menu_mode$="Background", POINT(_MOUSEX,_MOUSEY), b_color%)
	SetLocalStorageItem( "draw:f_color", f_color% )
	SetLocalStorageItem( "draw:b_color", b_color% )
  GOSUB 🎨show_color_picks
  GOSUB ✒set_marker
  RETURN