Class: R2dSvg::DrawingInstructions

Inherits:
Object
  • Object
show all
Defined in:
lib/r2dsvg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, debug: false) ⇒ DrawingInstructions

Returns a new instance of DrawingInstructions.



219
220
221
222
223
# File 'lib/r2dsvg.rb', line 219

def initialize(window, debug: false)

  @window, @debug = window, debug     

end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



216
217
218
# File 'lib/r2dsvg.rb', line 216

def area
  @area
end

Instance Method Details

#draw_arc(args) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/r2dsvg.rb', line 225

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) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/r2dsvg.rb', line 235

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) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/r2dsvg.rb', line 258

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) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/r2dsvg.rb', line 284

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) ⇒ Object

not yet implemented



337
338
339
340
341
342
343
344
# File 'lib/r2dsvg.rb', line 337

def draw_lines(args)

  coords, width, style, e = args

  x1, y1, x2, y2 = coords

 
end

#draw_polygon(args) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/r2dsvg.rb', line 308

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) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/r2dsvg.rb', line 346

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) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/r2dsvg.rb', line 368

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) ⇒ Object

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



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/r2dsvg.rb', line 396

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) ⇒ Object



430
431
432
433
# File 'lib/r2dsvg.rb', line 430

def render(a)
  method(a[0]).call(args=a[1..2])
  draw a[3]
end

#script(args) ⇒ Object



435
436
437
# File 'lib/r2dsvg.rb', line 435

def script(args)

end

#style(args) ⇒ Object



439
440
# File 'lib/r2dsvg.rb', line 439

def style(args)
end

#window(args) ⇒ Object



427
428
# File 'lib/r2dsvg.rb', line 427

def window(args)
end