lib/IFMapper/Map.rb in ifmapper-0.7 vs lib/IFMapper/Map.rb in ifmapper-0.8

- old
+ new

@@ -56,10 +56,56 @@ } } } end + # + # Change map's width and height to make sure all rooms and connections + # will fit in map + # + def fit + # First, adjust map's width and height + @width = @height = 3 + minXY = [] + maxXY = [] + + @sections.each { |section| + next if section.rooms.empty? + + sizes = section.min_max_rooms + minXY.push sizes[0] + maxXY.push sizes[1] + + w = maxXY[-1][0] - minXY[-1][0] + h = maxXY[-1][1] - minXY[-1][1] + + # We store +3 to allow for complex connections if needed. + @width = w + 3 if w >= @width - 2 + @height = h + 3 if h >= @height - 2 + } + + + # Okay, minXY[]/maxXY[] contains all the minXY/maxXY positions of + # each section. With that info and @weight/@height, we can + # start shifting all nodes in the section so that they will fit. + idx = 0 + @sections.each { |p| + next if p.rooms.size == 0 + x = y = 0 + x = 1 - minXY[idx][0] if minXY[idx][0] < 1 + y = 1 - minXY[idx][1] if minXY[idx][1] < 1 + x = @width - maxXY[idx][0] - 2 if maxXY[idx][0] >= @width - 1 + y = @height - maxXY[idx][1] - 2 if maxXY[idx][1] >= @height - 1 + idx += 1 + next if x == 0 and y == 0 # nothing to shift + p.rooms.each { |r| + r.x += x + r.y += y + } + } + end + def initialize(name) @section = 0 @name = name @creator = '' @@ -77,9 +123,20 @@ @name = b.name @creator = b.creator @width = b.width @height = b.height @date = b.date + end + + # + # Return true or false if map is free at location x,y + # + def free?(x, y) + return @sections[@section].free?(x,y) + end + + def shift(x, y, dx, dy) + @sections[@section].shift(x, y, dx, dy) end def delete_connection(c) @sections[@section].delete_connection(c) end