Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created September 26, 2020 14:31
Show Gist options
  • Save FoamyGuy/3d5826ede385554b54175b66c624113a to your computer and use it in GitHub Desktop.
Save FoamyGuy/3d5826ede385554b54175b66c624113a to your computer and use it in GitHub Desktop.
import displayio
class GridLayout(displayio.Group):
def __init__(
self,
x,
y,
width,
height,
grid_size,
child_padding,
max_children=4
):
super().__init__(x=x, y=y, max_size=max_children)
self.x = x
self.y = y
self._width = width
self._height = height
self.grid_size = grid_size
self.child_padding = child_padding
self._sub_views = []
def _layout_sub_views(self):
for sub_view in self._sub_views:
if sub_view["view"] not in self:
grid_size_x=self.grid_size[0]
grid_size_y=self.grid_size[1]
grid_position_x=sub_view["grid_position"][0]
grid_position_y=sub_view["grid_position"][1]
button_size_x=sub_view["view_grid_size"][0]
button_size_y=sub_view["view_grid_size"][1]
sub_view["view"].width=int(button_size_x*self._width/grid_size_x)-2*self.child_padding
sub_view["view"].height=int(button_size_y*self._height/grid_size_y)-2*self.child_padding
sub_view["view"].x=int(grid_position_x*self._width/grid_size_x)+self.child_padding
sub_view["view"].y=int(grid_position_y*self._height/grid_size_y)+self.child_padding
self.append(sub_view["view"])
def add_sub_view(self, new_view, grid_position, view_grid_size):
sub_view_obj = {
"view": new_view,
"grid_position": grid_position,
"view_grid_size": view_grid_size
}
self._sub_views.append(sub_view_obj)
self._layout_sub_views()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment