class R2dEngine::DrawingInstructions

Attributes

area[RW]

Public Class Methods

new(window, debug: false) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 195
def initialize(window, debug: false)

  @window, @debug = window, debug     

end

Public Instance Methods

draw_arc(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 201
def draw_arc(args)

  dimensions, style = args

  x, y, width, height = dimensions

  #gc = gc_ini(fill: style[:fill] || :none)
  #@area.window.draw_arc(gc, 1, x, y, width, height, 0, 64 * 360)
end
draw_circle(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 211
def draw_circle(args)

  coords, radius, fill, style, e = args

  x1, y1 = coords

  if @debug then
    puts 'inside draw_circle'.info
    puts ('style: ' + style.inspect).debug 
  end 

  obj = Circle.new(
    x: x1, y: y1,
    radius: radius,
    sectors: 32,
    color: style[:fill],
    z: style[:"z-index"].to_i
  )
  e.obj = obj if e.respond_to? :obj=
  @window.add obj

end
draw_image(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 234
def draw_image(args)

  dimensions, src, style, e = args

  x, y, width, height = dimensions      
  
  filepath = Tempfile.new('r2dsvg').path + File.basename(src)
  puts 'filepath: ' + filepath.inspect if @debug
  File.write filepath, RXFHelper.read(src).first
    
  
  if File.exists? filepath then

    obj = Image.new(
      filepath,
      x: x, y: y,
      width: width, height: height,
      color: style[:fill],
      z: style[:"z-index"].to_i
    )        

    e.obj = obj if e.respond_to? :obj=
    @window.add obj
  end
end
draw_line(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 260
def draw_line(args)

  coords, style, e = args

  x1, y1, x2, y2 = coords

  if @debug then
    puts 'inside draw_rectangle'.info
    puts ('style: ' + style.inspect).debug 
  end 

  obj = Line.new(
    x1: x1, y1: y1,
    x2: x2, y2: y2,
    width: style[:"stroke-width"].to_f,
    color: style[:"stroke"],
    z: style[:"z-index"].to_i
  ) 
  
  e.obj = obj if e.respond_to? :obj=
  @window.add obj
  
end
draw_lines(args) click to toggle source

not yet implemented

# File lib/r2dsvg/r2dsvg_module.rb, line 313
def draw_lines(args)

  coords, width, style, e = args

  x1, y1, x2, y2 = coords


end
draw_polygon(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 284
def draw_polygon(args)

  vertices, style, e = args

  puts ('vertices: ' + vertices.inspect).debug if @debug      

  if @debug then
    puts 'inside draw_polygon'.info
    puts ('style: ' + style.inspect).debug 
  end 
  
  puts ('vertices: ' + vertices.inspect).debug if @debug      
  
  h = vertices.map.with_index do |coords,i|

    %w(x y).zip(coords).map {|key, c| [(key + (i+1).to_s).to_sym, c] }
    
  end.flatten(1).to_h
  puts ('triangle h: ' + h.inspect).debug if @debug      

  puts ('triangle h merged: ' + h.inspect).debug if @debug
  obj = Triangle.new(h.merge({color: style[:fill], z: style[:"z-index"].to_i}))
  e.obj = obj if e.respond_to? :obj=
  @window.add obj
end
draw_rectangle(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 322
def draw_rectangle(args)

  coords, style, e = args

  x1, y1, x2, y2 = coords

  if @debug then
    puts 'inside draw_rectangle'.info
    puts ('style: ' + style.inspect).debug 
  end 

  obj = Rectangle.new(
    x: x1, y: y1,
    width: x2 - x1, height: y2 - y1,
    color: style[:fill],
    z: style[:"z-index"].to_i
  )
  e.obj = obj if e.respond_to? :obj=
  @window.add obj

end
draw_text(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 344
def draw_text(args)

  coords, text, style, e = args

  x, y = coords

  if @debug then
    puts 'inside draw_text'.info
    puts ('style: ' + style.inspect).debug 
  end 

  obj = Text.new(
    text,
    x: x, y: y,
    #font: 'vera.ttf',
    size: style[:font_size].to_i,
    color: style[:color],
    z: style[:"z-index"].to_i
  )
  puts 'e: ' + e.inspect
  e.obj = obj
  @window.add obj

end
embed_audio(args) click to toggle source

Ruby 2D supports a number of popular audio formats, including WAV, MP3, Ogg Vorbis, and FLAC.

# File lib/r2dsvg/r2dsvg_module.rb, line 372
def embed_audio(args)
  
  sources, e = args

  if @debug then
    puts 'sources: ' + sources.inspect if @debug
    puts 'inside embed_audio'.info
  end 

  
  audio_files = sources.map do |source|
    
    filepath = Tempfile.new('r2dsvg').path + File.basename(source[:src])
    File.write filepath, RXFHelper.read(source[:src]).first
    filepath
  end
  
  audio = audio_files.find {|file| File.exists? file }
  
  return unless audio
  
  file = File.exists?(audio) ? audio : nil
  puts 'file: ' + file.inspect if @debug

  obj = Sound.new(file)
  e.obj = obj
  
  def e.play() self.obj.play()  end

end
render(a) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 406
def render(a)
 
  stylex, e = a[-3..-2]
  
  if e.respond_to? :attr_map then
    
    h = e.attr_map.inject({}) do |r, x|
      key, value = x
      r.merge!({value => stylex[key]})
    end
    a[-3].merge!(h)        
  end
  

  method(a[0]).call(args=a[1..2])
  draw a[3]
end
script(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 424
def script(args)

end
style(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 428
def style(args)
end
window(args) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 403
def window(args)
end

Private Instance Methods

draw(a) click to toggle source
# File lib/r2dsvg/r2dsvg_module.rb, line 433
def draw(a)
  
  #threads = []
  
  a.each do |rawx|
  
    #threads << Thread.new do
      puts 'rawx: ;' + rawx.inspect if @debug
      x, *remaining = rawx

      puts 'x: ;' + x.inspect if @debug
      puts 'remaining: ' + remaining.inspect if @debug
      
      if x.is_a? Symbol then
        method(x).call(args=remaining)
        draw(remaining[3])
      elsif x.is_a? String then
        draw remaining
      elsif x.is_a? Array
        draw remaining
      else        
        method(x).call(remaining.take 2)
      end
    #end
                    
  end
  
  #threads.join

end