Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Scene within ui
-
I'm a Pythonista 3 and Python newcomer, so simpler answers are preferred...I'm wanting to display an n x n grid of cells within a ui which has some other fields and buttons. When any of the cells in the grid are touched, I'd like that cell to go from empty to filled or vice versa. I want to have an array that captures the content of each cell once the user is finished touching cells. Can someone provide some guidance on the best approach? I was assuming I'd have to define a scene within the ui view, but not clear on how these work since I don't want it to be full screen.
-
Look at SceneView, which lets you define a scene within a View.
Though in this case you could also just use an array of Button, or use a custom View class with custom touch_began and maybe touch_moved.
-
Thanks for pointing me to SceneView. I'm now looking into using an array of SpriteNodes.
-
@red_shift, nothing in what you describe requires mixing
scene
andui
, and although mixing them is definitely possible, it seems borrowing trouble for your first project, as they have different coordinate systems and different operating logics.So I would pick one and concentrate on learning the subtleties of that one first.
My take is that
scene
holds your hand a bit more, has better out-of-the-box animation capabilities and might be easier to start with, whileui
is much more versatile and will serve you in more projects. -
Since he wanted text fields,
ui
is needed sincescene
does not offer text entry.. I would probably tend towards a custom ui.View in this case, because ui is more touch event based, whereas scene feels more heavy, with the built in runloop. -
@JonB I agree, I don't see any request of animation in his initial post, only touching buttons
-
Thanks to all for the comments and guidance. I had written a couple of scripts using the ui module already and felt pretty comfortable with it. Although an array of buttons would meet my needs, I did want to try combining ui and SceneView, mostly for learning purposes. So far I've managed to place a Custom view into my ui, and populate it with a scene that contains an array of cells, which are SpriteNodes textured as circles. Next it's figuring out how to capture the location of touches and process them to change the texture of the sprite at that location. I need to figure out the best way to translate from a view location to which is sprite located there.
-
Touch_began provides the touch object, and touch.location provides coordinates within the scene.
(I assume you are implementing touch handling within the scene object, using a custom scene, not a custom View).
Then, assuming you have your SpriteNodes stored in some attribute of your scene, you can use something like:
for b in self.grid: if touch.location in b.bbox: # do something....
-
Rereading your last post make sure you implement your touch handling within the scene class, not the custom view. Getting coordinates converted from UI to scene can be a little tricky.