Class | TurtleWindow |
In: |
Turtle.rb
|
Parent: | FXMainWindow |
The TurtleWindow class provides a graphical interface for Turtle objects.
DEG | = | Math::PI / 180 | Converts degrees to radians |
Initialize a new drawing window for the Turtle.
# File Turtle.rb, line 21 def initialize(app, turtle) @tortoises = [turtle] super(app, "RubyTurtle", nil, nil, DECOR_ALL, 0, 0, 600, 600) @scrollwin = FXScrollWindow.new(self, LAYOUT_FILL_X|LAYOUT_FILL_Y) scrollfix = FXPacker.new(@scrollwin, LAYOUT_FILL_X|LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # scrollwin has wrong range associated to its scrollbars # manually update to known real values @scrollwin.verticalScrollBar.range = 5000 @scrollwin.horizontalScrollBar.range = 5000 @scrollwin.setPosition(-2210, -2210) @turtlearea = FXCanvas.new(scrollfix, nil, 0, LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT, 0, 0, 5000, 5000) @turtlearea.connect(SEL_PAINT) { |sender, sel, event| FXDCWindow.new(@turtlearea, event) { |terrarium| terrarium.foreground = @tortoises[0].background # only first turtle controls bg terrarium.fillRectangle(event.rect.x, event.rect.y, event.rect.w, event.rect.h) @tortoises.each { |tortoise| # lines drawn by the turtles tortoise.getLines { |line| terrarium.foreground = line.color # defaults to 'black' if not a valid color begin # move coordinates to adapt to drawing area terrarium.drawLine(line.startx+2500, 2500-line.starty, line.endx+2500, 2500-line.endy) rescue RangeError tortoise.log "ERROR: Drawing coordinates out of range" end } } drawTurtle(terrarium) # draw turtles at their current positions } } refreshTurtle # update status for turtle end
Add the given turtle to the drawing area.
# File Turtle.rb, line 103 def addTurtle(turtle) @tortoises << turtle end
Draw the Turtles to the drawing area. The Turtles have the shape of an arrowhead and are drawn in their current pen color.
# File Turtle.rb, line 61 def drawTurtle(drawarea) @tortoises.each { |tortoise| if tortoise.visible drawarea.foreground = tortoise.color # rotate and move turtle to adapt to drawing area coordinate system dr = tortoise.direction + 180 x0 = tortoise.posx+2500 y0 = 2500-tortoise.posy x1 = x0 + 20 * Math.cos((dr-45) * DEG) y1 = y0 + 20 * Math.sin((dr-45) * DEG) x2 = x0 + 20 * Math.cos((dr+225) * DEG) y2 = y0 + 20 * Math.sin((dr+225) * DEG) x3 = x0 + 20 * Math.cos((dr+90) * DEG) y3 = y0 + 20 * Math.sin((dr+90) * DEG) begin drawarea.drawLine(x0, y0, x1, y1) drawarea.drawLine(x0, y0, x2, y2) drawarea.drawLine(x1, y1, x3, y3) drawarea.drawLine(x2, y2, x3, y3) rescue RangeError end end } end
Update the drawn image. Check if the color attribute of the Turtles is a compatible color string and replace it if necessary.
# File Turtle.rb, line 89 def refreshTurtle @tortoises.each { |tortoise| # GUI defaults to 'black' for unknown color names. Let's tell our turtle tortoise.color = "black" if fxcolorfromname(tortoise.color) == 0 # Default background should be white. Catch invalid names and replace tortoise.background = "white" if tortoise.background != "black" && fxcolorfromname(tortoise.background) == 0 } @turtlearea.update #@turtlearea.repaint end