This is still Lab, because still thinking though it. But the idea is to make a grid of a rectangle returning a list of lists (2 dimensional array) populated with ui.Rects.
This gives you a type of spreadsheet access to your grid.
My thinking is this an alternative to positioning ui elements in a view using ratios etc.
So place them using your grid reference. It's a little wasteful as you calculate all the Rects for the grid, you only may use one or 2 of the references. But it should be so fast it does not really matter.
The reason to go this way, I think we can more easily relate to positioning items in a grid or in relation to a grid item.
Hmmm, I don't think I am explaing this well. The code below, I use 2 methods of creating a grid. One based on passing in the number of required rows and columns, the other based on width and height of the cells. Other grids could be created using different metrics that make sense.
I know the code is nothing earth shattering. It's more about the concept. I also realise that the the functions written could be done in a calculated fashion. Aka. A virtual grid. Only calculate the values Upon request.
But to start this is ok. Can be refined.
Not sure about other users, but for me often placing items in a view is a pain and a lot of relative object calculations.
This way, you can slice the view in a way that makes sense. Position your objects. Then slice the same view again to position other items that don't fit into the previous grid, so on and so on.
Most things will fit into a grid. But often a functional view's items will not be on the same grid. Hmmm....spaghetti talk...
Anyway, the below example is pretty crappy. But if you can try and think in terms of sizing and placing a ui.element inside a view.
# Pythonista Forum - @Phuket2
import ui, editor
def grid_rc_(bounds, rows=1, columns=1):
# a grid based on rows and columns
# return a list of lists of ui.Rects
r = ui.Rect(*bounds)
w = r.width / columns
h = r.height / rows
rl = []
for i in range(rows):
lst = []
for j in range(columns):
lst.append(ui.Rect(j*w, h*i, w, h))
rl.append(lst)
return rl
def grid_wh_(bounds, w, h):
# a grid based on widths and heights
# return a list of lists of ui.Rects
r = ui.Rect(*bounds)
rl = []
for i in range(int(r.height / h)):
lst = []
for j in range(int(r.width / w)):
lst.append(ui.Rect(j*w, h*i, w, h))
rl.append(lst)
return rl
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# create the grids... static here. easy to call adhoc anytime,
# on any rect to give you a grid as per the params
self.grid_rc = grid_rc_(self.bounds, rows=3, columns=9)
self.grid_wh = grid_wh_(self.bounds, w=10, h=10)
def draw(self):
rc = self.grid_rc
wh = self.grid_wh
# using row, column grid
ui.set_color('teal')
s = ui.Path.rect(*rc[0][8])
s.fill()
s = ui.Path.rect(*rc[1][0])
s.fill()
s = ui.Path.rect(*rc[2][4])
s.fill()
# using wh grid
ui.set_color('red')
s = ui.Path.rect(*wh[5][20])
s.fill()
s = ui.Path.rect(*wh[0][0])
s.fill()
if __name__ == '__main__':
_use_theme = True
w, h = 600, 800
f = (0, 0, w, h)
style = 'sheet'
mc = MyClass(frame=f, bg_color='white')
if not _use_theme:
mc.present(style=style, animated=False)
else:
editor.present_themed(mc, theme_name='Oceanic', style=style, animated=False)
