|
6 | 6 | `grid_layout`
|
7 | 7 | ================================================================================
|
8 | 8 |
|
9 |
| -A layout that organizes children into a grid table structure. |
| 9 | +A layout that organizes cells into a grid table structure. |
10 | 10 |
|
11 | 11 |
|
12 | 12 | * Author(s): Kevin Matocha, Tim Cocks
|
@@ -34,73 +34,76 @@ class GridLayout(displayio.Group):
|
34 | 34 | """
|
35 | 35 | A layout that organizes children into a grid table structure.
|
36 | 36 |
|
37 |
| - :param int x: x location the layout should be placed |
38 |
| - :param int y: y location the layout should be placed |
| 37 | + :param int x: x location the layout should be placed. Pixel coordinates. |
| 38 | + :param int y: y location the layout should be placed. Pixel coordinates. |
| 39 | +
|
39 | 40 | """
|
40 | 41 |
|
41 | 42 | # pylint: disable=too-many-arguments
|
42 |
| - def __init__(self, x, y, width, height, grid_size, child_padding, max_children=4): |
43 |
| - super().__init__(x=x, y=y, max_size=max_children) |
| 43 | + def __init__(self, x, y, width, height, grid_size, cell_padding, max_size=None): |
| 44 | + if not max_size: |
| 45 | + max_size = grid_size[0] * grid_size[1] |
| 46 | + super().__init__(x=x, y=y, max_size=max_size) |
44 | 47 | self.x = x
|
45 | 48 | self.y = y
|
46 | 49 | self._width = width
|
47 | 50 | self._height = height
|
48 | 51 | self.grid_size = grid_size
|
49 |
| - self.child_padding = child_padding |
50 |
| - self._sub_views = [] |
| 52 | + self.cell_padding = cell_padding |
| 53 | + self._cell_content_list = [] |
51 | 54 |
|
52 |
| - def _layout_sub_views(self): |
| 55 | + def _layout_cells(self): |
53 | 56 |
|
54 |
| - for sub_view in self._sub_views: |
55 |
| - if sub_view["view"] not in self: |
| 57 | + for cell in self._cell_content_list: |
| 58 | + if cell["content"] not in self: |
56 | 59 | grid_size_x = self.grid_size[0]
|
57 | 60 | grid_size_y = self.grid_size[1]
|
58 | 61 |
|
59 |
| - grid_position_x = sub_view["grid_position"][0] |
60 |
| - grid_position_y = sub_view["grid_position"][1] |
| 62 | + grid_position_x = cell["grid_position"][0] |
| 63 | + grid_position_y = cell["grid_position"][1] |
61 | 64 |
|
62 |
| - button_size_x = sub_view["view_grid_size"][0] |
63 |
| - button_size_y = sub_view["view_grid_size"][1] |
| 65 | + button_size_x = cell["cell_size"][0] |
| 66 | + button_size_y = cell["cell_size"][1] |
64 | 67 |
|
65 | 68 | print(
|
66 | 69 | "setting width to: {}".format(
|
67 | 70 | int(button_size_x * self._width / grid_size_x)
|
68 |
| - - 2 * self.child_padding |
| 71 | + - 2 * self.cell_padding |
69 | 72 | )
|
70 | 73 | )
|
71 |
| - sub_view["view"].width = ( |
| 74 | + cell["content"].width = ( |
72 | 75 | int(button_size_x * self._width / grid_size_x)
|
73 |
| - - 2 * self.child_padding |
| 76 | + - 2 * self.cell_padding |
74 | 77 | )
|
75 |
| - sub_view["view"].height = ( |
| 78 | + cell["content"].height = ( |
76 | 79 | int(button_size_y * self._height / grid_size_y)
|
77 |
| - - 2 * self.child_padding |
| 80 | + - 2 * self.cell_padding |
78 | 81 | )
|
79 | 82 |
|
80 |
| - sub_view["view"].x = ( |
81 |
| - int(grid_position_x * self._width / grid_size_x) |
82 |
| - + self.child_padding |
| 83 | + cell["content"].x = ( |
| 84 | + int(grid_position_x * self._width / grid_size_x) + self.cell_padding |
83 | 85 | )
|
84 |
| - sub_view["view"].y = ( |
| 86 | + cell["content"].y = ( |
85 | 87 | int(grid_position_y * self._height / grid_size_y)
|
86 |
| - + self.child_padding |
| 88 | + + self.cell_padding |
87 | 89 | )
|
88 | 90 |
|
89 |
| - self.append(sub_view["view"]) |
| 91 | + self.append(cell["content"]) |
90 | 92 |
|
91 |
| - def add_sub_view(self, new_view, grid_position, view_grid_size): |
| 93 | + def add_content(self, cell_content, grid_position, cell_size): |
92 | 94 | """Add a child to the grid.
|
93 | 95 |
|
94 |
| - :param new_view: the child object to add e.g. label, button, etc... |
| 96 | + :param cell_content: the content to add to this cell e.g. label, button, etc... |
| 97 | + Group subclass that have width and height properties can be used. |
95 | 98 | :param grid_position: where in the grid it should go. Tuple with
|
96 | 99 | x,y coordinates in grid cells. e.g. (1,0)
|
97 |
| - :param view_grid_size: the size and shape of cells that the new |
98 |
| - child should occupy. |
| 100 | + :param cell_size: the size and shape that the new cell should |
| 101 | + occupy |
99 | 102 | :return: None"""
|
100 | 103 | sub_view_obj = {
|
101 |
| - "view": new_view, |
| 104 | + "content": cell_content, |
102 | 105 | "grid_position": grid_position,
|
103 |
| - "view_grid_size": view_grid_size, |
| 106 | + "cell_size": cell_size, |
104 | 107 | }
|
105 |
| - self._sub_views.append(sub_view_obj) |
106 |
| - self._layout_sub_views() |
| 108 | + self._cell_content_list.append(sub_view_obj) |
| 109 | + self._layout_cells() |
0 commit comments