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.



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

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



561
562
563
564
565
566
567
568
# File 'lib/vizcore/shape.rb', line 561

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.



432
433
434
# File 'lib/vizcore/shape.rb', line 432

def shapes
  @shapes
end

Instance Method Details

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



553
554
555
# File 'lib/vizcore/shape.rb', line 553

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



463
464
465
466
467
468
469
470
471
472
473
# File 'lib/vizcore/shape.rb', line 463

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



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

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

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



439
440
441
# File 'lib/vizcore/shape.rb', line 439

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

#closeObject



557
558
559
# File 'lib/vizcore/shape.rb', line 557

def close
  append_path_command("Z")
end

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



549
550
551
# File 'lib/vizcore/shape.rb', line 549

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

#draw(&block) ⇒ Object



434
435
436
437
# File 'lib/vizcore/shape.rb', line 434

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

#fill(value) ⇒ Object



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

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

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



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

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



537
538
539
# File 'lib/vizcore/shape.rb', line 537

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

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



443
444
445
# File 'lib/vizcore/shape.rb', line 443

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

#line_to(x, y) ⇒ Object



533
534
535
# File 'lib/vizcore/shape.rb', line 533

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

#move_to(x, y) ⇒ Object



529
530
531
# File 'lib/vizcore/shape.rb', line 529

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

#opacity(value) ⇒ Object



502
503
504
505
506
507
508
# File 'lib/vizcore/shape.rb', line 502

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



525
526
527
# File 'lib/vizcore/shape.rb', line 525

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



459
460
461
# File 'lib/vizcore/shape.rb', line 459

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

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



451
452
453
# File 'lib/vizcore/shape.rb', line 451

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

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



455
456
457
# File 'lib/vizcore/shape.rb', line 455

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



545
546
547
# File 'lib/vizcore/shape.rb', line 545

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

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



447
448
449
# File 'lib/vizcore/shape.rb', line 447

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)


570
571
572
# File 'lib/vizcore/shape.rb', line 570

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

#rotate(value) ⇒ Object



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

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



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

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



475
476
477
# File 'lib/vizcore/shape.rb', line 475

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

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



491
492
493
494
495
496
# File 'lib/vizcore/shape.rb', line 491

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



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

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



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

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