Class: Dedalus::ViewTraversal

Inherits:
Object
  • Object
show all
Includes:
Geometer::DimensionHelpers, Geometer::PointHelpers
Defined in:
lib/dedalus/view_traversal.rb

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ ViewTraversal

Returns a new instance of ViewTraversal.



6
7
8
# File 'lib/dedalus/view_traversal.rb', line 6

def initialize(&blk)
  instance_eval(&blk)
end

Instance Method Details

#on_atom(&blk) ⇒ Object



10
11
12
# File 'lib/dedalus/view_traversal.rb', line 10

def on_atom(&blk)
  @atom_callback = blk
end

#on_element(&blk) ⇒ Object



18
19
20
# File 'lib/dedalus/view_traversal.rb', line 18

def on_element(&blk)
  @element_callback = blk
end

#on_molecule(&blk) ⇒ Object



14
15
16
# File 'lib/dedalus/view_traversal.rb', line 14

def on_molecule(&blk)
  @molecule_callback = blk
end

#walk!(structure, origin:, dimensions:, freeform: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dedalus/view_traversal.rb', line 22

def walk!(structure, origin:, dimensions:, freeform: false)
  width, height = *dimensions
  height = structure.height if !structure.is_a?(Array) && structure.height
  width = structure.width if !structure.is_a?(Array) && structure.width
  x0, y0 = *origin

  if structure.is_a?(Dedalus::Atom)
    @atom_callback.call(structure, origin: origin, dimensions: dimensions, freeform: freeform) if @atom_callback
  elsif structure.is_a?(Dedalus::Element)
    # an element *other than* an atom, we need to call #show on it
    margin = structure.margin || 0.0
    x,y = x0 + margin, y0 + margin
    margin_origin = [ x, y ]
    margin_dims = [ width - margin*2, height - margin*2 ]

    if structure.is_a?(Dedalus::Molecule) && @molecule_callback
      @molecule_callback.call(structure, origin: margin_origin, dimensions: margin_dims)
    end

    if @element_callback
      @element_callback.call(structure, origin: margin_origin, dimensions: margin_dims)
    end

    pad = structure.padding || 0.0
    pad_origin = [x+pad,y+pad]
    pad_dims = [width - pad*2 - margin*2, height - pad*2 - margin*2 ]

    if structure.is_a?(LayerStack)
      layers = structure.layers
      layers.each do |layer|
        walk!(layer, origin: pad_origin, dimensions: pad_dims, freeform: layer.freeform?)
      end
    else
      walk!(structure.show, origin: pad_origin, dimensions: pad_dims, freeform: freeform)
    end
  elsif structure.is_a?(Array) # we have a set of rows
    walk_rows!(structure, origin: origin, dimensions: dimensions)
  end
end