//
// Scribble program
// 
// When press mouse and drag, draw lines segments from each point to next,
// giving impression of a smooth scribble.  Each new scribble is given a
// chosen color
// 
// @author Kim Bruce
//

dialect "rtobjectdraw"
import "random" as randomGenerator

def scribble: GraphicApplication = object {
   inherit graphicApplicationSize (400 @ 400)

   // beginning point for next line
   var nextLineStarts: Point

   var currentColor: Color

   // Choose color randomly for next line and remember start
   method onMousePress (pt: Point) -> Done {
      nextLineStarts := pt
      def colorNumber: Number = randomGenerator.integerIn (1) to (4)
      print(colorNumber)
      if (colorNumber == 1) then {
        currentColor := colorGen.red
      } elseif {colorNumber == 2} then {
        currentColor := colorGen.blue
      } elseif {colorNumber == 3} then {
        currentColor := colorGen.green
      } else {
        currentColor := colorGen.magenta
      }
   }

   // Draw lines to follow mouse
   method onMouseDrag (mouseLoc: Point) -> Done {
      def segment: Line = lineFrom (nextLineStarts) to (mouseLoc) on (canvas)
      segment.color := currentColor

      nextLineStarts := mouseLoc
   }

   // required to pop up window and start graphics
   startGraphics
}