Class: Doodl::Layout

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Observable
Defined in:
lib/layout/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Layout

Returns a new instance of Layout.



14
15
16
17
# File 'lib/layout/layout.rb', line 14

def initialize(view)
  @view = view
  @locations = Hash.new
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



12
13
14
# File 'lib/layout/layout.rb', line 12

def graph
  @graph
end

#iterationsObject (readonly)

Returns the value of attribute iterations.



12
13
14
# File 'lib/layout/layout.rb', line 12

def iterations
  @iterations
end

#locationsObject (readonly)

Returns the value of attribute locations.



12
13
14
# File 'lib/layout/layout.rb', line 12

def locations
  @locations
end

Instance Method Details

#center_xObject



88
89
90
91
92
93
# File 'lib/layout/layout.rb', line 88

def center_x
  pad_l = @locations.values.min { |a, b| a.x <=> b.x }.x
  pad_r = @view.width - @locations.values.max { |a, b| a.x <=> b.x }.x
  pad = ((pad_l + pad_r) / 2.0) - pad_l
  @locations.each_value { |location| location.add!(pad, 0)  }
end

#center_yObject



95
96
97
98
99
100
# File 'lib/layout/layout.rb', line 95

def center_y
  pad_t = @locations.values.min { |a, b| a.y <=> b.y }.y
  pad_b = @view.height - @locations.values.max { |a, b| a.y <=> b.y }.y
  pad = ((pad_t + pad_b) / 2.0) - pad_t
  @locations.each_value { |location| location.add!(0, pad)  }
end

#deep_copyObject



102
103
104
105
106
# File 'lib/layout/layout.rb', line 102

def deep_copy
  clone = self.clone
  clone.locations = self.locations.clone
  return clone
end

#finished?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/layout/layout.rb', line 27

def finished?
  @finished
end

#get_nearest_node(x, y) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/layout/layout.rb', line 50

def get_nearest_node(x, y)
  origin = Location.new(x, y)
  nearest = @locations.inject do |memo, cmp|
    memo[1].dist(origin) < cmp[1].dist(origin) ? memo : cmp
  end
  return nearest[0]
end

#get_nearest_node_within(x, y, radius) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/layout/layout.rb', line 58

def get_nearest_node_within(x, y, radius)
  node = get_nearest_node(x, y)
  if @locations[node].dist(Location.new(x, y)) < radius
    return node
  else
    return nil
  end
end

#init(graph) ⇒ Object



24
25
# File 'lib/layout/layout.rb', line 24

def init(graph)
end

#layout(g = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/layout/layout.rb', line 31

def layout(g = nil)
  if g
    self.graph = g
  end
  if @graph
    @finished = false
    @iterations = 0
    init(@graph)
    dolayout(@graph)
    @finished = true
    changed
    notify_observers(self)
  end
end

#move(pad_x, pad_y) ⇒ Object



84
85
86
# File 'lib/layout/layout.rb', line 84

def move(pad_x, pad_y)
  @locations.each_value { |location| location.add!(pad_x, pad_y ) }
end

#rotate(theta) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/layout/layout.rb', line 75

def rotate(theta)
  center = Location.new(@view.width/2, @view.height/2)
  @graph.each_node do |node|
    vector = @locations[node] - center
    vector.rotate!(theta)
    @locations[node] = vector.add!(center)
  end
end

#set_location(node, x, y, notify = false) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/layout/layout.rb', line 67

def set_location(node, x, y, notify = false)
  @locations[node] = Location.new(x, y)
  if notify
    changed
    notify_observers(nil)
  end
end