[cairo-commit] rcairo/samples gtkcairo_pong.rb, NONE, 1.1 gtkcairo_text.rb, NONE, 1.1 pdf.rb, NONE, 1.1 png.rb, NONE, 1.1 png2.rb, NONE, 1.1 ps.rb, NONE, 1.1

Oeyvind Kolaas commit at pdx.freedesktop.org
Mon Feb 7 17:33:02 PST 2005


Committed by: pippin

Update of /cvs/cairo/rcairo/samples
In directory gabe:/tmp/cvs-serv11955

Added Files:
	gtkcairo_pong.rb gtkcairo_text.rb pdf.rb png.rb png2.rb ps.rb 
Log Message:
adding new ruby cairo samples

--- NEW FILE: gtkcairo_pong.rb ---
#!/usr/bin/env ruby
#
# pong by Martin Evans
#
# vim: set ts=2 sw=2 et :

$LOAD_PATH.unshift "../packages/gtkcairo/ext/"
$LOAD_PATH.unshift "../packages/cairo/ext/"
$LOAD_PATH.unshift "../packages/cairo/lib/"

require 'gtk2'
require 'cairo'
require 'gtkcairo'

class CenteredRect
  attr_accessor :x, :y
  def initialize(x, y, width, height)
    @x, @y, @width, @height = x, y, width, height
  end
  def minx; @x - @width/2; end
  def maxx; @x + @width/2; end
  def miny; @y - @height/2; end
  def maxy; @y + @height/2; end
  def draw(cr)
    cr.rectangle(minx, miny, @width, @height)
    cr.fill
  end
end

class Ball < CenteredRect
  attr_accessor :dx, :dy
  def initialize
    super(180, 50, 4, 4)
    @dx = @dy = 5
  end
  def update
    @x += @dx; @y += @dy
  end
end

class Paddle < CenteredRect
  def initialize(field, x, y)
    @field = field
    super(x, y, 10, 60)
    @speed = 4
  end
  def update(ball, center)
    # is the ball coming towards us?
    if (ball.x < @x and ball.dx > 0) or
       (ball.x > @x and ball.dx < 0)
      # move to intercept it
      #@y += @speed*(ball.y <=> @y)
      @y = ball.y
    else
      # otherwise, head back towards the center
      if (center - @y).abs > @speed
        @y += @speed*(center <=> @y)
      else
        @y = center
      end
    end
  end
  def ballhittest(ball)
    if ball.y > miny and ball.y < maxy
      if ball.minx < @x and ball.maxx > minx # hit our left side
        ball.x -= (ball.maxx - minx)
        ball.dx = -ball.dx
      elsif ball.maxx > @x and ball.minx < maxx # hit our right side
        ball.x += (maxx - ball.minx)
        ball.dx = -ball.dx
      end
    end
  end
end

class Field
  Margin = 10
  Width = 250
  Height = 200

  attr_accessor :width, :height

  def initialize
    @width = Width
    @height = Height

    @p1 = Paddle.new(self, Margin, 50)
    @p2 = Paddle.new(self, Width-Margin, 80)
    @paddles = [@p1, @p2]
    @ball = Ball.new
  end

  def update
    @paddles.each {|p| p.update(@ball, Height/2)}
    @ball.update

    # ball bouncing
    if @ball.maxy > Height
      @ball.y = Height-(@ball.maxy-Height)
      @ball.dy = - at ball.dy
    elsif @ball.miny < 0
      @ball.y -= @ball.miny
      @ball.dy = - at ball.dy
    end

    if @ball.maxx > Width
      @ball.x = Width-(@ball.maxx-Width)
      @ball.dx = - at ball.dx
    elsif @ball.minx < 0
      @ball.x -= @ball.minx
      @ball.dx = - at ball.dx
    end

    @paddles.each { |p| p.ballhittest(@ball) }

    return TRUE
  end

  def draw(cr)
    cr.set_rgb_color(1,1,1)

    @p1.draw(cr)
    @p2.draw(cr)
    @ball.draw(cr)
  end
end

class PongWindow < Gtk::Window
  Speed = 30

	def initialize
    super

    #set_default_size(200, 200)
    self.title = 'Pong Demonstration'
    signal_connect('destroy') { Gtk.main_quit }

    @field = Field.new

    @gc = Gtk::CairoWidget.new
    @gc.modify_bg(Gtk::STATE_NORMAL, Gdk::Color.new(0,0,0))
    @gc.set_size_request(@field.width, @field.height)
    @gc.signal_connect('paint') { paint }

    Gtk::timeout_add(Speed) { @field.update; @gc.queue_draw }

    vb = Gtk::VBox.new(FALSE, 5)
    vb.border_width = 10
    vb.pack_start(@gc, TRUE, TRUE, 0)
    vb.show_all
    add(vb)
  end

  def paint
    cr = @gc.cairo
    @field.draw(cr)
  end
end

Gtk.init
win = PongWindow.new
win.show
Gtk.main

--- NEW FILE: gtkcairo_text.rb ---
#!/usr/bin/env ruby

$LOAD_PATH.unshift "../packages/gtkcairo/ext/"
$LOAD_PATH.unshift "../packages/cairo/ext/"
$LOAD_PATH.unshift "../packages/cairo/lib/"

require 'gtk2'
require 'cairo'
require 'gtkcairo'

class Canvas < Gtk::CairoWidget
    attr_accessor :x, :y

    def initialize
        super

        @x = 10
        @y = 10
        
        add_events Gdk::Event::BUTTON_PRESS_MASK

        signal_connect('paint') { paint }
        signal_connect('button_press_event') {|w,e|
            press_event e.x, e.y, e.button
        }
    end
    def press_event x,y,b
        @x = x
        @y = y
        queue_draw
    end
    def paint
        cr = cairo

        cr.save
          cr.set_rgb_color 0,0,0
          cr.move_to @x, @y
          cr.line_to 20,20
          cr.stroke
          cr.move_to @x, @y
          
          cr.select_font "Sans", Cairo::FONT_WEIGHT_NORMAL, Cairo::FONT_SLANT_NORMAL
          cr.scale_font 10
          cr.show_text "foo"
        cr.restore
    end
end

Gtk.init

win = Gtk::Window.new
win.signal_connect('destroy') { Gtk.main_quit }
win.set_default_size(512,512)
canvas = Canvas.new
win.add(canvas)
canvas.show
win.show

Gtk.main

--- NEW FILE: pdf.rb ---
#!/usr/bin/env ruby
# vim: set ts=2 sw=2 et :

$LOAD_PATH.unshift "../packages/cairo/lib/"
$LOAD_PATH.unshift "../packages/cairo/ext/"

require 'cairo'

cr = Cairo::Context.new

# tweak these:
SIZE = 4
DPI = 200
FILENAME = 'test.pdf'

# derived constants:
PIXELS = SIZE * DPI
MARGIN = PIXELS/40

File.open(FILENAME, "w") { |f|
  cr.set_target_pdf(f, SIZE, SIZE, DPI, DPI)
  cr.set_rgb_color(0,0,0)
  cr.line_cap = Cairo::LINE_CAP_ROUND
  cr.line_width = PIXELS/40
  cr.stroke {
    cr.move_to(MARGIN, MARGIN)
    cr.line_to(PIXELS-MARGIN, PIXELS-MARGIN)
  }
  cr.show_page
}
puts "wrote #{SIZE}x#{SIZE}in, #{DPI}dpi image to #{FILENAME}."

puts "foo"
exit


--- NEW FILE: png.rb ---
#!/usr/bin/env ruby
# vim: filetype=ruby:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :

$LOAD_PATH.unshift "../packages/cairo/ext/"
$LOAD_PATH.unshift "../packages/cairo/lib/"

$:.push File.join(File.dirname($0), "cairo")

require 'cairo'
include Cairo

File.open("test-0.png", "wb") {|stream|
    cr = Context.new
    cr.set_target_png(stream, FORMAT_ARGB32, 200, 200);

    cr.new_path
    cr.rectangle(0, 0, 200, 200)

    cr.set_rgb_color(1.0, 1.0, 1.0)
    cr.fill

    cr.new_path
    cr.move_to(50, 50)
    cr.curve_to(100, 25, 100, 75, 150, 50)
    cr.line_to(150, 150)
    cr.line_to(50, 150)
    cr.close_path

    cr.save # fill will consume the path
    cr.set_rgb_color(0.0, 0.0, 0.0)
    cr.fill
    cr.restore

    cr.set_rgb_color(1.0, 0.0, 0.0)
    cr.line_join = LINE_JOIN_MITER
    cr.line_width = 4
    cr.stroke

    cr.show_page
}


--- NEW FILE: png2.rb ---
#!/usr/bin/env ruby
# vim: filetype=ruby:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :

$LOAD_PATH.unshift "../packages/cairo/lib/"
$LOAD_PATH.unshift "../packages/cairo/ext/"

$:.push File.join(File.dirname($0), "cairo")

require 'cairo'
include Cairo

def draw_quad(ctx, x0, y0, x1, y1, x2, y2)
    ctx.new_path
    ctx.move_to(x0, y0)
    ctx.line_to(x1, y1)
    ctx.line_to(x2, y2)

    ctx.set_rgb_color(1.0, 0.0, 0.0)
    ctx.save
    ctx.alpha = 0.2
    ctx.fill
    ctx.restore
    ctx.line_width = 1
    ctx.stroke

    ctx.new_path
    ctx.move_to(x0, y0)
    ctx.quad_to(x1, y1, x2, y2)
    
    ctx.set_rgb_color(0.0, 0.0, 0.0)
    ctx.line_width = 3
    ctx.stroke
end

File.open("test-1.png", "wb") {|stream|
    ctx = Context.new
    ctx.set_target_png(stream, FORMAT_ARGB32, 200, 200);

    ctx.new_path
    ctx.rectangle(0, 0, 200, 200)

    ctx.set_rgb_color(1.0, 1.0, 1.0)
    ctx.fill

    draw_quad(ctx, 20, 20, 110, 25, 180, 180)

    ctx.show_page
}


--- NEW FILE: ps.rb ---
#!/usr/bin/env ruby
# vim: set ts=2 sw=2 et :

$LOAD_PATH.unshift "../packages/cairo/lib/"
$LOAD_PATH.unshift "../packages/cairo/ext/"

require 'cairo'

cr = Cairo::Context.new

# tweak these:
SIZE = 4
DPI = 200
FILENAME = 'test.ps'

# derived constants:
PIXELS = SIZE * DPI
MARGIN = PIXELS/40

File.open(FILENAME, "w") { |f|
  cr.set_target_ps(f, SIZE, SIZE, DPI, DPI)
  cr.set_rgb_color(0,0,0)
  cr.line_cap = Cairo::LINE_CAP_ROUND
  cr.line_width = PIXELS/40
  cr.stroke {
    cr.move_to(MARGIN, MARGIN)
    cr.line_to(PIXELS-MARGIN, PIXELS-MARGIN)
  }
  cr.show_page
}
puts "wrote #{SIZE}x#{SIZE}in, #{DPI}dpi image to #{FILENAME}."





More information about the cairo-commit mailing list