Class: BracketNotation::View::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_notation/views/node.rb

Direct Known Subclasses

Branch, Leaf

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, content) ⇒ Node

Returns a new instance of Node.



36
37
38
39
40
41
42
43
# File 'lib/bracket_notation/views/node.rb', line 36

def initialize(tree, content)
  @tree = tree
  @content = content
  @parent = nil
  @children = []
  @rect = BracketNotation::Geometry::Rect.new
  @align_to_grid = true
end

Instance Attribute Details

#align_to_gridObject

Returns the value of attribute align_to_grid.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def align_to_grid
  @align_to_grid
end

#childrenObject

Returns the value of attribute children.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def children
  @children
end

#contentObject

Returns the value of attribute content.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def content
  @content
end

#parentObject

Returns the value of attribute parent.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def parent
  @parent
end

#rectObject

Returns the value of attribute rect.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def rect
  @rect
end

#treeObject

Returns the value of attribute tree.



34
35
36
# File 'lib/bracket_notation/views/node.rb', line 34

def tree
  @tree
end

Instance Method Details

#ancestorsObject

Return the list of nodes leading from the current node to the tree root, starting with the current node’s parent.



84
85
86
87
88
89
90
# File 'lib/bracket_notation/views/node.rb', line 84

def ancestors
  node_list = []
  next_up = self
  node_list << next_up while next_up = next_up.parent
  
  return node_list
end

#corner_bottom_leftObject

Return the coordinates of the node’s bottom left corner.



127
128
129
# File 'lib/bracket_notation/views/node.rb', line 127

def corner_bottom_left
  BracketNotation::Geometry::Point.new(@rect.origin.x, @rect.origin.y + @rect.size.height)
end

#corner_bottom_rightObject

Return the coordinates of the node’s bottom right corner.



122
123
124
# File 'lib/bracket_notation/views/node.rb', line 122

def corner_bottom_right
  BracketNotation::Geometry::Point.new(@rect.origin.x + @rect.size.width, @rect.origin.y + @rect.size.height)
end

#corner_top_leftObject

Return the coordinates of the node’s top left corner.



112
113
114
# File 'lib/bracket_notation/views/node.rb', line 112

def corner_top_left
  @rect.origin
end

#corner_top_rightObject

Return the coordinates of the node’s top right corner.



117
118
119
# File 'lib/bracket_notation/views/node.rb', line 117

def corner_top_right
  BracketNotation::Geometry::Point.new(@rect.origin.x + @rect.size.width, @rect.origin.y)
end

#left_siblingObject

Return the node’s left sibling, or nil if the node is the leftmost child of its parent.



61
62
63
64
65
66
67
68
69
# File 'lib/bracket_notation/views/node.rb', line 61

def left_sibling
  return nil if @parent.nil?
  
  left_sibling = nil
  self_index = @parent.children.index(self)
  left_sibling = @parent.children[self_index - 1] if self_index > 0
  
  return left_sibling
end

#right_siblingObject

Return the node’s right sibling, or nil if the node is the rightmost child of its parent.



73
74
75
76
77
78
79
80
# File 'lib/bracket_notation/views/node.rb', line 73

def right_sibling
  return nil if @parent.nil?
  
  self_index = @parent.children.index(self)
  right_sibling = @parent.children[self_index + 1]
  
  return right_sibling
end

#side_middle_bottomObject

Return the coordinates of the middle of the node’s bottom side.



142
143
144
# File 'lib/bracket_notation/views/node.rb', line 142

def side_middle_bottom
  BracketNotation::Geometry::Point.new(@rect.origin.x + (@rect.size.width / 2), @rect.origin.y + @rect.size.height)
end

#side_middle_leftObject

Return the coordinates of the middle of the node’s left side.



147
148
149
# File 'lib/bracket_notation/views/node.rb', line 147

def side_middle_left
  BracketNotation::Geometry::Point.new(@rect.origin.x, @rect.origin.y + (@rect.size.height / 2))
end

#side_middle_rightObject

Return the coordinates of the middle of the node’s right side.



137
138
139
# File 'lib/bracket_notation/views/node.rb', line 137

def side_middle_right
  BracketNotation::Geometry::Point.new(@rect.origin.x + @rect.size.width, @rect.origin.y + (@rect.size.height / 2))
end

#side_middle_topObject

Return the coordinates of the middle of the node’s top side.



132
133
134
# File 'lib/bracket_notation/views/node.rb', line 132

def side_middle_top
  BracketNotation::Geometry::Point.new(@rect.origin.x + (@rect.size.width / 2), @rect.origin.y)
end

#subtree_sizeObject

Return the dimensions of the rect that contains the nod and all of its descendants



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bracket_notation/views/node.rb', line 94

def subtree_size
  return @rect.size if kind_of? Leaf or @children.count == 0
  
  new_subtree_size = BracketNotation::Geometry::Size.new(0, @rect.size.height)
  subtree_widths = []
  subtree_heights = []
  
  @children.each do |child|
    child_subtree_size = child.subtree_size
    new_subtree_size = new_subtree_size.size_by_adding_to_width(child_subtree_size.width)
    subtree_heights << child_subtree_size.height
  end
  
  new_subtree_size = new_subtree_size.size_by_adding_to_width_and_height(@tree.node_h_margin * (children.count - 1), @tree.node_v_margin + subtree_heights.sort.last)
  return BracketNotation::Geometry::Size.new(@rect.size.width > new_subtree_size.width ? @rect.size.width : new_subtree_size.width, new_subtree_size.height)
end