//
// Railroad program
// 
// Draw rr tracks
// 
// @author Kim Bruce
//

dialect "rtobjectdraw"

def Railroad: GraphicApplication = object {
   inherit graphicApplicationSize (600 @ 400)
   
   // distance between rails
   def gauge:Number = 100
   
   // y-coordinate of the top of the tracks
   def trackTop:Number = (canvas.height - gauge) / 2
   
   // dimensions of rail and ties
   def railWidth: Number = 10
   def tieWidth: Number = 20
   def tieExtend: Number = 20
   def tieLength: Number = gauge + (2 * railWidth) + (2 * tieExtend)
   def tieSpacing: Number = 25
   def tieTop: Number = trackTop - tieExtend
   
   // starting x-coordinate for the first tie
   def firstTieX: Number = 5
   
   // colors for the background, rails, and the ties
   def groundColor: Color = colorGen.r (50) g (150) b (50) 
   def railColor: Color = colorGen.r (200) g (200) b (200) 
   def tieColor: Color = colorGen.r (150) g (40) b (40)

   // the ground under the tracks
   def background: Graphic2D = filledRectAt (0 @ 0)
                                size (canvas.size) on (canvas)
   background.color := groundColor

   // draw the top rail
   def topRail:Graphic2D = filledRectAt (0 @ trackTop) size (canvas.width @ railWidth)
                         on (self.canvas)
   topRail.color := railColor

   // draw the bottom rail
   def bottomRail: Graphic2D = filledRectAt (0 @ (trackTop + gauge))
                        size (canvas.width @ railWidth) on (canvas)
   bottomRail.color := railColor

   // keep track of the x coordinate of the latest tie
   var tieXPosition: Number := firstTieX

   method onMousePress(pt: Point) -> Done {
      if (tieXPosition < canvas.width) then {
          def tie: Graphic2D = filledRectAt (tieXPosition @ tieTop) 
                                size (tieWidth @ tieLength) on (canvas)
          tie.color := tieColor
          tieXPosition := tieXPosition + tieWidth + tieSpacing
      }
      topRail.sendToFront
      bottomRail.sendToFront
   }

   // required to pop up window and start graphics
   startGraphics


}
