make section table management with adding and positioning tables
This commit is contained in:
@@ -78,4 +78,82 @@ class Section
|
||||
end
|
||||
@for_tables_as_json = h
|
||||
end
|
||||
|
||||
def arrange_tables_in_grid
|
||||
w = width
|
||||
h = height
|
||||
n = tables.size
|
||||
l = Math.sqrt((w*h).to_f/n)
|
||||
epsilon = (10 ** -(Float::DIG - 1)).to_f
|
||||
while (w/l).floor * (h/l).floor < n # find a fitting combination
|
||||
l = if w.remainder(l) < epsilon then (h/l).ceil
|
||||
elsif h.remainder(l) < epsilon then (w/l).ceil
|
||||
elsif w.remainder(l) < epsilon && h.remainder(l) < epsilon then [w / (w/l).ceil.succ, h / (h/l).ceil.succ].max # Failsafe for when both are valid and it still does not fit
|
||||
else [w / (w/l).ceil, h / (h/l).ceil].max end
|
||||
end
|
||||
x0 = w.remainder(l - epsilon)/2 # Start with half a remainder space, will end with the other halve
|
||||
x = x0
|
||||
y = h.remainder(l - epsilon)/2
|
||||
saves = []
|
||||
for table in tables.sort_by(&:number)
|
||||
if x + l > w + (1e4*epsilon) # New row, error = epsilon times possible tables
|
||||
x = x0
|
||||
y += l
|
||||
end
|
||||
table.position_x = x + (l/2) - (table_width/2) # Starting point of square + half the square (center) minus half the table size
|
||||
table.position_y = y + (l/2) - (table_height/2)
|
||||
saves << table.save
|
||||
x += l
|
||||
end
|
||||
[l, saves.all?]
|
||||
end
|
||||
|
||||
def arrange_tables_in_rows_of(n)
|
||||
return unless n.present?
|
||||
n = n.to_i
|
||||
return unless n > 0
|
||||
dx = width / n
|
||||
dy = height / (tables.size.to_f/n).ceil
|
||||
x = 0.0
|
||||
y = 0.0
|
||||
tables.sort_by(&:number).each.with_index do |table, i|
|
||||
table.position_x = x + (dx/2) - (table_width/2)
|
||||
table.position_y = y + (dy/2) - (table_height/2)
|
||||
x += dx
|
||||
if (i + 1).multiple_of?( n )
|
||||
x = 0.0
|
||||
y += dy
|
||||
end
|
||||
table.save
|
||||
end
|
||||
end
|
||||
def arrange_tables_in_columns_of(n)
|
||||
return unless n.present?
|
||||
n = n.to_i
|
||||
return unless n > 0
|
||||
dx = width / (tables.size.to_f/n).ceil
|
||||
dy = height / n
|
||||
x = 0.0
|
||||
y = 0.0
|
||||
tables.sort_by(&:number).each.with_index do |table, i|
|
||||
table.position_x = x + (dx/2) - (table_width/2)
|
||||
table.position_y = y + (dy/2) - (table_height/2)
|
||||
y += dy
|
||||
if (i + 1).multiple_of?( n )
|
||||
y = 0.0
|
||||
x += dx
|
||||
end
|
||||
table.save
|
||||
end
|
||||
end
|
||||
|
||||
# Method returning the sections table width
|
||||
def table_width
|
||||
2.0
|
||||
end
|
||||
|
||||
# Method returning the sections table height
|
||||
def table_height
|
||||
2.0
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user