; Draw.jlogo - A Simple Drawing Application
; ----------

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

to black
  output 0
  end
to white
  output 7
  end

to north
  output 0
  end
to east
  output 90
  end
to south
  output 180
  end

to buttonLabelHeight
  output 14
  end
to buttonWidth
  output 65
  end
to buttonHeight
  output 25
  end
to buttonGap
  output 10
  end
to buttonsY
  output sum (sum 10 buttonHeight) minus quotient canvasheight 2
  end

to pudButtonX
  output sum 10 minus quotient canvaswidth 2
  end

; draw the outline of a rectangle
; the top-left corner is at x,y
to drawRect :x :y :width :height
  penup setxy :x :y pendown
  setheading east
  repeat 2 [forward :width right 90 forward :height right 90]   
  end

; fill the specified rectangular area
; the top-left corner is at x,y
to fillRect :x :y :width :height
  penup setxy (sum :x (quotient :width 2)) :y pendown
  setpensize :width setheading south
  forward :height
  end

; draw a button
; the top, left corner of the button is at x,y
; the button is labeled with text's value
to drawButtonAt :x :y :text
  setpencolor white
  fillRect :x :y buttonWidth buttonHeight
  setpencolor black setpensize 2
  drawRect :x :y buttonWidth buttonHeight
  penup setxy (sum :x 4) (sum 8 (difference :y buttonHeight)) pendown
  setlabelheight buttonLabelHeight label :text
  end

; output TRUE if point is within a rectangular area, otherwise FALSE
; point is a sentence, an x,y coordinate pair
; x is the left edge of the rectangular area, y is the top edge
; width and height specify its size
to inRect? :point :x :y :width :height
  if less? (first :point) :x [output "false]
  if greater? (last :point) :y [output "false]
  if greater? (first :point) sum :x (difference :width 1) [output "false]
  if less? (last :point) difference :y (difference :height 1) [output "false]
  output "true
  end

; output TRUE if the most recent mouse click was within the
; bounds of a specified rectangle, otherwise it outputs FALSE
; the top-left corner of the rectangle is at x,y
to mouseInRect? :x :y :height :width
  output inRect? (sentence mousex mousey) :x :y :width :height
  end

to liftPen
  drawButtonAt pudButtonX buttonsY "PenDown
  penup setpencolor white
  end
to lowerPen
  drawButtonAt pudButtonX buttonsY "PenUp
  pendown setpencolor black
  end
to flipPenState
  ifelse pendown? [liftPen] [lowerPen]
  end

to init
  clean
  liftPen
  home setheading north
  end

to mouseclicked
  if mouseInRect? pudButtonX buttonsY buttonHeight buttonWidth [flipPenState stop]
  setxy mousex mousey
  end

to main
  init
  end

main