def test_multiple_fixed_2(self): grid = GridLayout(column_count=2, row_count=2, spacing=5) grid.set_fixed_column_sizing(0, 101) grid.set_fixed_column_sizing(1, 0) grid.set_fixed_row_sizing(0, 0) grid.set_fixed_row_sizing(1, 93) def create_child(rect): child = self.gui.create(Panel) child.parent = self.parent child.padding = (10, 8, 6, 4) child.margins = (11, 16, 8, 2) child.size = (9999, 9999) grid.add_rect(child, rect) return child child_0_0 = create_child(Rect(0, 0, 1, 1)) child_1_0 = create_child(Rect(1, 0, 1, 1)) child_0_1 = create_child(Rect(0, 1, 1, 1)) child_1_1 = create_child(Rect(1, 1, 1, 1)) grid.layout(self.parent) self.assertEqual(Panel.Rect(2, 3, 101, 18), child_0_0.rect_outer) self.assertEqual(Panel.Rect(108, 3, 19, 18), child_1_0.rect_outer) self.assertEqual(Panel.Rect(2, 8, 101, 93), child_0_1.rect_outer) self.assertEqual(Panel.Rect(108, 8, 19, 93), child_1_1.rect_outer)
def test_tallest_child_in_column_or_row_3(self): def create_grid(): grid = GridLayout(column_count=5, row_count=5, spacing=3) for column in range(grid.column_count): grid.set_child_column_sizing(column) for row in range(grid.row_count): grid.set_child_row_sizing(row) return grid with self.subTest("column"): grid = create_grid() child = self.gui.create(Panel) child.size = (66, 38) grid.add_rect(child, Rect(2, 1, 3, 2)) self.assertEqual(20, grid.widest_child_in_column(2)) self.assertEqual(20, grid.widest_child_in_column(3)) self.assertEqual(20, grid.widest_child_in_column(4)) with self.subTest("row"): grid = create_grid() child = self.gui.create(Panel) child.size = (38, 66) grid.add_rect(child, Rect(1, 2, 2, 3)) self.assertEqual(20, grid.tallest_child_in_row(2)) self.assertEqual(20, grid.tallest_child_in_row(3)) self.assertEqual(20, grid.tallest_child_in_row(4))
def test_multiple_child_2(self): grid = GridLayout(column_count=2, row_count=2, spacing=5) grid.set_child_column_sizing(0) grid.set_child_column_sizing(1) grid.set_child_row_sizing(0) grid.set_child_row_sizing(1) def create_child(rect, size): child = self.gui.create(Panel) child.parent = self.parent child.padding = (10, 8, 6, 4) child.margins = (11, 16, 8, 2) child.size = size grid.add_rect(child, rect) return child child_0_0 = create_child(Rect(0, 0, 1, 1), (58, 31)) child_0_1 = create_child(Rect(0, 1, 1, 1), (61, 31)) child_1_0 = create_child(Rect(1, 0, 1, 2), (25, 87)) grid.layout(self.parent) self.assertEqual(Panel.Rect(2, 3, 80, 50), child_0_0.rect_outer) self.assertEqual(Panel.Rect(2, 58, 80, 50), child_0_1.rect_outer) self.assertEqual(Panel.Rect(87, 3, 44, 105), child_1_0.rect_outer)
def setUp(self): from gui.gui import Gui self.gui = Gui() self.panel = self.gui.create(Panel) self.panel.rect = Rect(50, 80, 100, 120) self.panel.padding = Rect(2, 3, 5, 11) self.panel.margins = Rect(17, 23, 29, 37) self.panel.layout_dirty = False self.gui.world.layout_dirty = False
def test_margins(self): self.assertEqual(Rect(17, 23, 29, 37), self.panel.margins) rect = Rect(11, 22, 33, 44) self.panel.margins = rect.as_tuple() self.assertEqual(rect, self.panel.margins) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.margins = rect self.assertFalse(self.panel.layout_dirty)
def test_padding(self): self.assertEqual(Rect(2, 3, 5, 11), self.panel.padding) rect = Rect(11, 22, 33, 44) self.panel.padding = rect.as_tuple() self.assertEqual(rect, self.panel.padding) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.padding = rect self.assertFalse(self.panel.layout_dirty)
def test_outer(self): self.assertEqual(Rect(33, 57, 146, 180), self.panel.rect_outer) rect = Rect(55, 66, 77, 88) self.panel.rect_outer = rect self.assertEqual(rect, self.panel.rect_outer) self.assertEqual(Rect(72, 89, 31, 28), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.rect_outer = rect self.assertFalse(self.panel.layout_dirty)
def test_inner(self): self.assertEqual(Rect(52, 83, 93, 106), self.panel.rect_inner) rect = Rect(22, 33, 44, 55) self.panel.rect_inner = rect self.assertEqual(rect, self.panel.rect_inner) self.assertEqual(Rect(20, 30, 51, 69), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.rect_inner = rect self.assertFalse(self.panel.layout_dirty)
def test_rect(self): self.assertEqual(Rect(50, 80, 100, 120), self.panel.rect) rect = Rect(11, 22, 33, 44) self.panel.rect = rect.as_tuple() self.assertEqual(rect, self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.rect = rect self.assertFalse(self.panel.layout_dirty) self.panel.rect = rect.as_tuple() self.assertFalse(self.panel.layout_dirty)
def test_area_empty(self): scenarios = [ (Rect(2, 0, 4, 2), Rect(1, 1, 9, 9), False), (Rect(10, 2, 4, 4), Rect(1, 1, 9, 9), True), ] for rect_left, rect_right, empty in scenarios: with self.subTest(rect_left=rect_left, rect_right=rect_right, empty=empty): grid = GridLayout(column_count=20, row_count=20, spacing=5) grid.add_rect(self.gui.create(Panel), rect_left) self.assertEqual(empty, grid.area_empty(rect_right))
def test_size(self): self.assertEqual((100, 120), self.panel.size) self.panel.size = (127, 99) self.assertEqual(Rect(50, 80, 127, 99), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.size = (127, 99) self.assertFalse(self.panel.layout_dirty)
def test_height(self): self.assertEqual(120, self.panel.height) self.panel.height = 127 self.assertEqual(Rect(50, 80, 100, 127), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.height = 127 self.assertFalse(self.panel.layout_dirty)
def test_width(self): self.assertEqual(100, self.panel.width) self.panel.width = 127 self.assertEqual(Rect(50, 80, 127, 120), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.width = 127 self.assertFalse(self.panel.layout_dirty)
def test_y(self): self.assertEqual(80, self.panel.y) self.panel.y = 127 self.assertEqual(Rect(50, 127, 100, 120), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.y = 127 self.assertFalse(self.panel.layout_dirty)
def test_x(self): self.assertEqual(50, self.panel.x) self.panel.x = 127 self.assertEqual(Rect(127, 80, 100, 120), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.x = 127 self.assertFalse(self.panel.layout_dirty)
def test_pos(self): self.assertEqual((50, 80), self.panel.pos) self.panel.pos = (127, 99) self.assertEqual(Rect(127, 99, 100, 120), self.panel.rect) self.assertTrue(self.panel.layout_dirty) self.panel.layout_dirty = False self.panel.pos = (127, 99) self.assertFalse(self.panel.layout_dirty)
def test_multiple_custom(self): def custom_sizing_1(area_size, remaining_size): return area_size**0.8 def custom_sizing_2(area_size, remaining_size): return area_size - area_size**0.8 grid = GridLayout(column_count=2, row_count=2, spacing=5) grid.set_custom_column_sizing(0, custom_sizing_1, partial(min, 1)) grid.set_custom_column_sizing(1, custom_sizing_2, partial(min, 1)) grid.set_custom_row_sizing(0, custom_sizing_2, partial(min, 1)) grid.set_custom_row_sizing(1, custom_sizing_1, partial(min, 1)) def create_child(rect): child = self.gui.create(Panel) child.parent = self.parent child.padding = (10, 8, 6, 4) child.margins = (11, 16, 8, 2) child.size = (9999, 9999) grid.add_rect(child, rect) return child child_0_0 = create_child(Rect(0, 0, 1, 1)) child_0_1 = create_child(Rect(0, 1, 1, 1)) child_1_0 = create_child(Rect(1, 0, 1, 1)) child_1_1 = create_child(Rect(1, 1, 1, 1)) grid.layout(self.parent) width_0 = int(custom_sizing_1(189, None)) + 1 width_1 = int(custom_sizing_2(189, None)) height_0 = int(custom_sizing_2(287, None)) + 1 height_1 = int(custom_sizing_1(287, None)) self.assertEqual(Panel.Rect(2, 3, width_0, height_0), child_0_0.rect_outer) self.assertEqual(Panel.Rect(2, 8 + height_0, width_0, height_1), child_0_1.rect_outer) self.assertEqual(Panel.Rect(7 + width_0, 3, width_1, height_0), child_1_0.rect_outer) self.assertEqual( Panel.Rect(7 + width_0, 8 + height_0, width_1, height_1), child_1_1.rect_outer)
def test_multiple_fill(self): grid = GridLayout(column_count=2, row_count=2, spacing=5) grid.set_percentage_column_sizing(0, 0.3333) grid.set_fill_column_sizing(1) grid.set_fill_row_sizing(0) grid.set_fixed_row_sizing(1, 100) def create_child(rect): child = self.gui.create(Panel) child.parent = self.parent child.padding = (10, 8, 6, 4) child.margins = (11, 16, 8, 2) child.size = (9999, 9999) grid.add_rect(child, rect) return child child_0_0 = create_child(Rect(0, 0, 1, 1)) child_0_1 = create_child(Rect(0, 1, 1, 1)) child_1_0 = create_child(Rect(1, 0, 1, 1)) child_1_1 = create_child(Rect(1, 1, 1, 1)) grid.layout(self.parent) width_0 = int(189 * 0.3333) width_1 = 189 - int(189 * 0.3333) height_0 = 287 - 100 height_1 = 100 self.assertEqual(Panel.Rect(2, 3, width_0, height_0), child_0_0.rect_outer) self.assertEqual(Panel.Rect(2, 8 + height_0, width_0, height_1), child_0_1.rect_outer) self.assertEqual(Panel.Rect(7 + width_0, 3, width_1, height_0), child_1_0.rect_outer) self.assertEqual( Panel.Rect(7 + width_0, 8 + height_0, width_1, height_1), child_1_1.rect_outer)
def widest_child_in_column(self, column): column_rect = Rect(column, 0, 1, self.row_count) rect_panel_tuples_that_intersect_column = list( filter( lambda rect_panel_tuple: rect_panel_tuple[0].intersects( column_rect), self.panels.items())) def calculate_width(rect_panel_tuple): rect, panel = rect_panel_tuple # In case a panel spans multiple columns, determine the height as a # proportional amount. return int( (panel.rect_outer.w - (rect.w - 1) * self.spacing) / rect.w) return reduce( max, map(calculate_width, rect_panel_tuples_that_intersect_column), 0)
def tallest_child_in_row(self, row): row_rect = Rect(0, row, self.column_count, 1) rect_panel_tuples_that_intersect_row = list( filter( lambda rect_panel_tuple: rect_panel_tuple[0].intersects( row_rect), self.panels.items())) def calculate_height(rect_panel_tuple): rect, panel = rect_panel_tuple # In case a panel spans multiple rows, determine the height as a # proportional amount. return int( (panel.rect_outer.h - (rect.h - 1) * self.spacing) / rect.h) return reduce( max, map(calculate_height, rect_panel_tuples_that_intersect_row), 0)
def add(self, panel, column, row, column_count=1, row_count=1): self.add_rect(panel, Rect(column, row, column_count, row_count))