; Draw.jlogo - Simple Drawing program (Lesson 14 - Local Variables)
; ----------

; Symbolic Constants
; ------------------

; SETPENCOLOR inputs
to black
  output 0
  end
to white
  output 7
  end

; SETHEADING inputs
to north
  output 0
  end
to east
  output 90
  end

; SETLABELFONT input
to sansSerif
  output 4
  end


; General Purpose Procedures
; --------------------------

; output the input number divided by two
to half :num
  output quotient :num 2
  end

; draw a rectangle given its dimensions and location
; its sides are oriented north-south and east-west
; the turtle's current pen width and color are used
to drawRect :leftX :bottomY :width :height
  penup  setxy :leftX :bottomY  pendown  setheading north
  repeat 2 [forward :height right 90 forward :width right 90]
  end

; draw a solid rectangle given its dimensions and location
; its sides are oriented north-south and east-west
; the rectangle is painted with the current pen color
to fillRect :leftX :bottomY :width :height
  penup  setxy :leftX (sum :bottomY (half :height))
  setpensize :height setheading east
  pendown forward :width
  end

; output TRUE if a point is in a rectangle, otherwise output FALSE
; point is a sentence, an x and y coordinate pair
; the bottom-left corner of the rectangle is at leftX, bottomY
; inputs width and height specify its size
to inRect? :point :leftX :bottomY :width :height
  if less? (first :point) :leftX [output "false]
  if less? (last :point) :bottomY [output "false]
  if greater? (first :point) (sum :leftX (difference :width 1)) [output "false]
  if greater? (last :point) (sum :bottomY (difference :height 1)) [output "false]
  output "true
  end

; outputs TRUE if the most recent mouse click was within the
; bounds of a specified rectangle, otherwise it outputs FALSE
; the bottom-left corner of the rectangle is at leftX, bottomY
; inputs width and height specify its size
to mouseInRect? :leftX :bottomY :height :width
  output inRect? (sentence mousex mousey) :leftX :bottomY :width :height
  end


; Button Stuff
; ------------

; height of a button
to buttonHeight
  output 25
  end
; width of a button
to buttonWidth
  output 65
  end
;text height of a button's label/name
to buttonLabelHeight
  output 10
  end
; buttons base Y coordinate - 10 turtle steps above bottom of canvas
to buttonsBottomY
  output sum (minus (quotient canvasheight 2)) 10
  end
; amount of space between buttons
to buttonsGap
  output 10
  end

; draw a button
; the bottom-left corner of the button is at leftX,bottomY
; the button is labeled with :text's value
to drawButtonAt :leftX :bottomY :text
  setpencolor white fillRect :leftX :bottomY buttonWidth buttonHeight
  setpencolor black setpensize 2
  drawRect :leftX :bottomY buttonWidth buttonHeight
  setlabelfont sansSerif
  setlabelheight buttonLabelHeight
  penup setxy :leftX :bottomY
  setheading east
  forward half (difference buttonWidth (labelwidth :text))
  setheading north
  forward half (difference buttonHeight buttonLabelHeight)
  label :text
  end

; leftmost X coordinate for the "Clear button
; 10 turtle steps right of the graphics canvas's left edge
to clearButtonLeftX
  output sum (minus (quotient canvaswidth 2)) 10
  end

; leftmost X coordinate for the PenUp/PenDown button
; buttonsGap turtle steps right of right edge of the Clear button
to pudButtonLeftX
  output sum clearButtonLeftX (sum buttonWidth buttonsGap)
  end


; User Interaction Stuff
; ----------------------

; handle a mouse click on the PenUp button
to liftPen
  drawButtonAt pudButtonLeftX buttonsBottomY "PenDown
  penup setpencolor white
  end

; handle a mouse click on the PenDown button
to lowerPen
  drawButtonAt pudButtonLeftX buttonsBottomY "PenUp
  pendown setpencolor black
  end

; handle a mouse click on the PenUp/Pendown button
to flipPenState
  ifelse pendown? [liftPen] [lowerPen]
  end

; initialize program
; clear the graphics canvas, draw the buttons, and
; start off with the turtle's pen in the up state
to init
  clean
  drawButtonAt clearButtonLeftX buttonsBottomY "Clear
  liftPen
  home setheading north
  end

; handle a mouse click event
to mouseclicked
  if mouseInRect? clearButtonLeftX buttonsBottomY buttonHeight buttonWidth [init stop]
  if mouseInRect? pudButtonLeftX buttonsBottomY buttonHeight buttonWidth [flipPenState stop]
  setxy mousex mousey
  end

; MAIN
; ----
to main
  init
  end

main