Skip to content

Commit 9a169a6

Browse files
committed
renaming things to remove reference to child and view
1 parent 1413bc3 commit 9a169a6

File tree

4 files changed

+104
-51
lines changed

4 files changed

+104
-51
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
`grid_layout`
77
================================================================================
88
9-
A layout that organizes children into a grid table structure.
9+
A layout that organizes cells into a grid table structure.
1010
1111
1212
* Author(s): Kevin Matocha, Tim Cocks
@@ -34,73 +34,76 @@ class GridLayout(displayio.Group):
3434
"""
3535
A layout that organizes children into a grid table structure.
3636
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+
3940
"""
4041

4142
# 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)
4447
self.x = x
4548
self.y = y
4649
self._width = width
4750
self._height = height
4851
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 = []
5154

52-
def _layout_sub_views(self):
55+
def _layout_cells(self):
5356

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:
5659
grid_size_x = self.grid_size[0]
5760
grid_size_y = self.grid_size[1]
5861

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]
6164

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]
6467

6568
print(
6669
"setting width to: {}".format(
6770
int(button_size_x * self._width / grid_size_x)
68-
- 2 * self.child_padding
71+
- 2 * self.cell_padding
6972
)
7073
)
71-
sub_view["view"].width = (
74+
cell["content"].width = (
7275
int(button_size_x * self._width / grid_size_x)
73-
- 2 * self.child_padding
76+
- 2 * self.cell_padding
7477
)
75-
sub_view["view"].height = (
78+
cell["content"].height = (
7679
int(button_size_y * self._height / grid_size_y)
77-
- 2 * self.child_padding
80+
- 2 * self.cell_padding
7881
)
7982

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
8385
)
84-
sub_view["view"].y = (
86+
cell["content"].y = (
8587
int(grid_position_y * self._height / grid_size_y)
86-
+ self.child_padding
88+
+ self.cell_padding
8789
)
8890

89-
self.append(sub_view["view"])
91+
self.append(cell["content"])
9092

91-
def add_sub_view(self, new_view, grid_position, view_grid_size):
93+
def add_content(self, cell_content, grid_position, cell_size):
9294
"""Add a child to the grid.
9395
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.
9598
:param grid_position: where in the grid it should go. Tuple with
9699
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
99102
:return: None"""
100103
sub_view_obj = {
101-
"view": new_view,
104+
"content": cell_content,
102105
"grid_position": grid_position,
103-
"view_grid_size": view_grid_size,
106+
"cell_size": cell_size,
104107
}
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()

examples/displayio_layout_gridlayout_pygame_display_simpletest.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2020 Tim C, written for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44
"""
@@ -15,8 +15,6 @@
1515
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
1616

1717
display = PyGameDisplay(width=320, height=240)
18-
19-
# Make the display context
2018
main_group = displayio.Group(max_size=10)
2119
display.show(main_group)
2220

@@ -26,8 +24,7 @@
2624
width=320,
2725
height=100,
2826
grid_size=(2, 2),
29-
child_padding=8,
30-
max_children=10,
27+
cell_padding=8,
3128
)
3229
_labels = []
3330

@@ -36,17 +33,17 @@
3633
terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077
3734
)
3835
)
39-
layout.add_sub_view(_labels[0], grid_position=(0, 0), view_grid_size=(1, 1))
36+
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1))
4037
_labels.append(
4138
label.Label(
4239
terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700
4340
)
4441
)
45-
layout.add_sub_view(_labels[1], grid_position=(1, 0), view_grid_size=(1, 1))
42+
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1))
4643
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello"))
47-
layout.add_sub_view(_labels[2], grid_position=(0, 1), view_grid_size=(1, 1))
44+
layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1))
4845
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid"))
49-
layout.add_sub_view(_labels[3], grid_position=(1, 1), view_grid_size=(1, 1))
46+
layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1))
5047

5148
main_group.append(layout)
5249
while display.running:

examples/displayio_layout_gridlayout_simpletest.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2020 Tim C, written for Adafruit Industries
1+
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: MIT
44
"""
@@ -9,8 +9,6 @@
99
import displayio
1010
import terminalio
1111
from adafruit_display_text import label
12-
13-
# Make the display context. Change size if you want
1412
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
1513

1614
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
@@ -28,8 +26,8 @@
2826
width=200,
2927
height=100,
3028
grid_size=(2, 2),
31-
child_padding=8,
32-
max_children=10,
29+
cell_padding=8,
30+
max_cells=10,
3331
)
3432
_labels = []
3533

@@ -38,17 +36,17 @@
3836
terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077
3937
)
4038
)
41-
layout.add_sub_view(_labels[0], grid_position=(0, 0), view_grid_size=(1, 1))
39+
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1))
4240
_labels.append(
4341
label.Label(
4442
terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700
4543
)
4644
)
47-
layout.add_sub_view(_labels[1], grid_position=(1, 0), view_grid_size=(1, 1))
45+
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1))
4846
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello"))
49-
layout.add_sub_view(_labels[2], grid_position=(0, 1), view_grid_size=(1, 1))
47+
layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1))
5048
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid"))
51-
layout.add_sub_view(_labels[3], grid_position=(1, 1), view_grid_size=(1, 1))
49+
layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1))
5250

5351
main_group.append(layout)
5452
while True:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Currently contains GridLayout example.
6+
7+
Make green and purple rectangles and a
8+
"Hello World" label.
9+
"""
10+
import board
11+
import displayio
12+
import terminalio
13+
from adafruit_display_text import label
14+
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
15+
16+
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
17+
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
18+
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
19+
display = board.DISPLAY
20+
21+
# Make the display context
22+
main_group = displayio.Group(max_size=10)
23+
display.show(main_group)
24+
25+
layout = GridLayout(
26+
x=10,
27+
y=10,
28+
width=200,
29+
height=100,
30+
grid_size=(2, 2),
31+
cell_padding=8,
32+
max_cells=10,
33+
)
34+
_labels = []
35+
36+
_labels.append(
37+
label.Label(
38+
terminalio.FONT, scale=2, x=0, y=0, text="Hello", background_color=0x770077
39+
)
40+
)
41+
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1))
42+
_labels.append(
43+
label.Label(
44+
terminalio.FONT, scale=2, x=0, y=0, text="World", background_color=0x007700
45+
)
46+
)
47+
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1))
48+
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Hello"))
49+
layout.add_content(_labels[2], grid_position=(0, 1), cell_size=(1, 1))
50+
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Grid"))
51+
layout.add_content(_labels[3], grid_position=(1, 1), cell_size=(1, 1))
52+
53+
main_group.append(layout)
54+
while True:
55+
pass

0 commit comments

Comments
 (0)