Class: Maxy::Gen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/maxy/gen/generator.rb

Constant Summary collapse

OFFSET_X =
20
OFFSET_Y =
20
STEP_X =
70
STEP_Y =
40
HEIGHT =
22
WIDTH =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



16
17
18
19
20
21
22
# File 'lib/maxy/gen/generator.rb', line 16

def initialize
  raise 'No object definitions were found. please run `maxy-gen install` first' unless File.exist?("#{ENV['HOME']}/.maxy-gen/library.yml")

  @object_count = 1
  @patch = Psych.load_file(File.join(__dir__, '../../../assets/blank.yml')).dup
  @library = Psych.load_file("#{ENV['HOME']}/.maxy-gen/library.yml").dup
end

Instance Attribute Details

#libraryObject

Returns the value of attribute library.



14
15
16
# File 'lib/maxy/gen/generator.rb', line 14

def library
  @library
end

Instance Method Details

#align_tree(node, x_rank = 1, y_rank = 1) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/maxy/gen/generator.rb', line 72

def align_tree(node, x_rank = 1, y_rank = 1)
  node.x_rank = x_rank
  node.y_rank = y_rank

  y_rank += 1

  node.child_nodes.each do |child|
    align_tree(child, x_rank, y_rank)
    x_rank += 1
  end

  node
end

#generate(root_node) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/maxy/gen/generator.rb', line 24

def generate(root_node)
  return JSON.generate(@patch) if root_node.nil?

  node = align_tree(root_node.child_nodes[0])
  generate_node(node, "obj_#{@object_count}")
  @patch['patcher']['boxes'].compact!
  @patch['patcher']['lines'].compact!
  JSON.generate(@patch)
end

#generate_node(node, id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maxy/gen/generator.rb', line 34

def generate_node(node, id)
  @patch['patcher']['boxes'] << make_box(node, id)
  @object_count += 1

  node.child_nodes.each_with_index do |child_node, index|
    child_id = "obj_#{@object_count}"
    generate_node(child_node, child_id)
    if node.flags.include? :connect_children_individually
      @patch['patcher']['lines'] << make_line(id, child_id, index, 0)
    elsif node.flags.include? :connect_all_child_inlets
      0.upto(child_node.args.split.count - 1) do |i|
        @patch['patcher']['lines'] << make_line(id, child_id, 0, i)
      end
    else
      @patch['patcher']['lines'] << make_line(id, child_id)
    end
  end
end

#make_box(node, id) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/maxy/gen/generator.rb', line 53

def make_box(node, id)
  box = @library[:objects][node.name].dup
  box['id'] = id
  box['patching_rect'] = [OFFSET_X + (node.x_rank - 1) * STEP_X, OFFSET_Y + (node.y_rank - 1) * STEP_Y, box['width'] || WIDTH, box['height'] || HEIGHT]
  box['text'] += " #{node.args}" unless box['text'].nil?

  box
end

#make_line(parent_id, child_id, parent_outlet = 0, child_inlet = 0) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/maxy/gen/generator.rb', line 62

def make_line(parent_id, child_id, parent_outlet = 0, child_inlet = 0)
  {
    patchline:
    {
      destination: [child_id, child_inlet],
      source: [parent_id, parent_outlet]
    }
  }
end