Class: Vizcore::Shape::PrimitiveBuilder

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

Overview

Builder used by DrawContext for primitive arrays.

Constant Summary collapse

NO_ARGUMENT =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrimitiveBuilder

Returns a new instance of PrimitiveBuilder.



397
398
399
400
# File 'lib/vizcore/shape.rb', line 397

def initialize
  @shapes = []
  @group_stack = [{}]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



531
532
533
534
535
536
537
538
# File 'lib/vizcore/shape.rb', line 531

def method_missing(method_name, *args, &block)
  if @current_shape && block.nil? && args.length == 1
    @current_shape[method_name.to_sym] = args.first
    return args.first
  end

  super
end

Instance Attribute Details

#shapesObject (readonly)

Returns the value of attribute shapes.



402
403
404
# File 'lib/vizcore/shape.rb', line 402

def shapes
  @shapes
end

Instance Method Details

#arc_to(rx, ry, rotation, large_arc, sweep, x, y) ⇒ Object



523
524
525
# File 'lib/vizcore/shape.rb', line 523

def arc_to(rx, ry, rotation, large_arc, sweep, x, y)
  append_path_command("A", rx, ry, rotation, large_arc, sweep, x, y)
end

#bezier(id = nil, from:, to:, control: nil, c1: nil, c2: nil, **options, &block) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
# File 'lib/vizcore/shape.rb', line 433

def bezier(id = nil, from:, to:, control: nil, c1: nil, c2: nil, **options, &block)
  commands = [["M", *point_values(from)]]
  if control
    commands << ["Q", *point_values(control), *point_values(to)]
  elsif c1 && c2
    commands << ["C", *point_values(c1), *point_values(c2), *point_values(to)]
  else
    raise ArgumentError, "bezier requires either :control or both :c1 and :c2"
  end
  build_shape(:path, shape_options(id, options).merge(commands: commands), &block)
end

#blend(value) ⇒ Object



468
469
470
# File 'lib/vizcore/shape.rb', line 468

def blend(value)
  target[:blend] = value.to_sym
end

#circle(id = nil, **options, &block) ⇒ Object



409
410
411
# File 'lib/vizcore/shape.rb', line 409

def circle(id = nil, **options, &block)
  build_shape(:circle, shape_options(id, options), &block)
end

#closeObject



527
528
529
# File 'lib/vizcore/shape.rb', line 527

def close
  append_path_command("Z")
end

#cubic_to(c1x, c1y, c2x, c2y, x, y) ⇒ Object



519
520
521
# File 'lib/vizcore/shape.rb', line 519

def cubic_to(c1x, c1y, c2x, c2y, x, y)
  append_path_command("C", c1x, c1y, c2x, c2y, x, y)
end

#draw(&block) ⇒ Object



404
405
406
407
# File 'lib/vizcore/shape.rb', line 404

def draw(&block)
  instance_eval(&block) if block
  shapes
end

#fill(value) ⇒ Object



457
458
459
# File 'lib/vizcore/shape.rb', line 457

def fill(value)
  target[:fill] = value.to_s
end

#group(_id = nil, **attrs, &block) ⇒ Object



449
450
451
452
453
454
455
# File 'lib/vizcore/shape.rb', line 449

def group(_id = nil, **attrs, &block)
  @group_stack << merge_group(current_group, normalize_group(attrs))
  instance_eval(&block) if block
  shapes
ensure
  @group_stack.pop
end

#horizontal_to(x) ⇒ Object



507
508
509
# File 'lib/vizcore/shape.rb', line 507

def horizontal_to(x)
  append_path_command("H", x)
end

#line(id = nil, **options, &block) ⇒ Object



413
414
415
# File 'lib/vizcore/shape.rb', line 413

def line(id = nil, **options, &block)
  build_shape(:line, shape_options(id, options), &block)
end

#line_to(x, y) ⇒ Object



503
504
505
# File 'lib/vizcore/shape.rb', line 503

def line_to(x, y)
  append_path_command("L", x, y)
end

#move_to(x, y) ⇒ Object



499
500
501
# File 'lib/vizcore/shape.rb', line 499

def move_to(x, y)
  append_path_command("M", x, y)
end

#opacity(value) ⇒ Object



472
473
474
475
476
477
478
# File 'lib/vizcore/shape.rb', line 472

def opacity(value)
  if @current_shape || !target.key?(:opacity)
    target[:opacity] = number(value, :opacity)
  else
    target[:opacity] = number(target[:opacity], :opacity) * number(value, :opacity)
  end
end

#origin(*args, x: nil, y: nil) ⇒ Object



495
496
497
# File 'lib/vizcore/shape.rb', line 495

def origin(*args, x: nil, y: nil)
  target_transform[:origin] = xy_args(args, x: x, y: y, name: :origin)
end

#path(id = nil, **options, &block) ⇒ Object



429
430
431
# File 'lib/vizcore/shape.rb', line 429

def path(id = nil, **options, &block)
  build_shape(:path, shape_options(id, options).merge(commands: []), &block)
end

#polygon(id = nil, **options, &block) ⇒ Object



421
422
423
# File 'lib/vizcore/shape.rb', line 421

def polygon(id = nil, **options, &block)
  build_shape(:polygon, shape_options(id, options), &block)
end

#polyline(id = nil, **options, &block) ⇒ Object



425
426
427
# File 'lib/vizcore/shape.rb', line 425

def polyline(id = nil, **options, &block)
  build_shape(:polyline, shape_options(id, options).merge(closed: false), &block)
end

#quad_to(cx, cy, x, y) ⇒ Object



515
516
517
# File 'lib/vizcore/shape.rb', line 515

def quad_to(cx, cy, x, y)
  append_path_command("Q", cx, cy, x, y)
end

#rect(id = nil, **options, &block) ⇒ Object



417
418
419
# File 'lib/vizcore/shape.rb', line 417

def rect(id = nil, **options, &block)
  build_shape(:rect, shape_options(id, options), &block)
end

#respond_to_missing?(_method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


540
541
542
# File 'lib/vizcore/shape.rb', line 540

def respond_to_missing?(_method_name, include_private = false)
  !!@current_shape || super
end

#rotate(value) ⇒ Object



485
486
487
488
# File 'lib/vizcore/shape.rb', line 485

def rotate(value)
  rotation = number(value, :rotate)
  target_transform[:rotate] = @current_shape ? rotation : number(target_transform[:rotate] || 0, :rotate) + rotation
end

#scale(value = NO_ARGUMENT, x: nil, y: nil) ⇒ Object



490
491
492
493
# File 'lib/vizcore/shape.rb', line 490

def scale(value = NO_ARGUMENT, x: nil, y: nil)
  values = scale_args(value, x: x, y: y)
  target_transform[:scale] = @current_shape ? values : multiply_scale(target_transform[:scale], values)
end

#star(id = nil, **options, &block) ⇒ Object



445
446
447
# File 'lib/vizcore/shape.rb', line 445

def star(id = nil, **options, &block)
  build_shape(:star, shape_options(id, options), &block)
end

#stroke(value = NO_ARGUMENT, width: nil, color: nil) ⇒ Object



461
462
463
464
465
466
# File 'lib/vizcore/shape.rb', line 461

def stroke(value = NO_ARGUMENT, width: nil, color: nil)
  target[:stroke] = non_negative_number(value, :stroke) unless value.equal?(NO_ARGUMENT)
  target[:stroke_width] = non_negative_number(width, :stroke_width) unless width.nil?
  target[:stroke_color] = color.to_s unless color.nil?
  target
end

#translate(*args, x: nil, y: nil) ⇒ Object



480
481
482
483
# File 'lib/vizcore/shape.rb', line 480

def translate(*args, x: nil, y: nil)
  values = xy_args(args, x: x, y: y, name: :translate)
  target_transform[:translate] = @current_shape ? values : add_xy(target_transform[:translate], values)
end

#vertical_to(y) ⇒ Object



511
512
513
# File 'lib/vizcore/shape.rb', line 511

def vertical_to(y)
  append_path_command("V", y)
end