-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for pop syntax #171
Comments
I agree that explicit-pop support would be a great option to have -- especially given that most processing materials are written in other dialects and it would aid translation. Explicit pop actions were recently removed because they were not implemented, and that caused buggy behavior. Changing to context-managers-only fixed that bug: 13b6a2d One subtlety to think about with both context manager push and explicit push-pop support is to make sure there is correct behavior if a sketch mixes using both. Another thing to think about is how explicit-push interacts with draw. A weird thing about explicit push in Processing (Java mode) is that a push state can be open over multiple frames, but its contents (the current matrix) are reset when draw loops. Here is an example. You might expect this to rotate the rectangle some amount between 0.2 and 1.0:
...but even though the stack depth may increase to 5 in a random walk, the rotation is always either 0 or 0.2, because the contents of open stack frames are always wiped. Re:
One way around heavy indenting of pushes (as with heavy indent in Python in general) is breaking up deep indent chains with functions calls. |
Hi Jeremy, Those are good points! Here are my thoughts:
The Processing Language throws a RuntimeException when more This is the option that requires no extra code (except checking for whether the stack is empty). There may exist other options that are more intuitive but I have yet to come up with one.
This is very interesting, especially the fact that the program won't error even if a
This is a technique I did not know before in explicit form. Thanks for sharing! |
Currently,
push_matrix
is implemented as a context-manager that automatically pops the transformation matrix at the end of a with block. While this is very pythonic and introduces great readability in most cases, this could lead to code that’s too far indented to the right when multiple with blocks are nested. Furthermore, this introduces additional work when the user attempts to adapt their code from the Processing Language to p5py. Therefore, I propose retainingpush_matrix
's ability of being a context manager while making it usable as a normal function, much like python’s built-inopen
. In addition, I will implement apop_matrix
function corresponding to thepopMatrix
function in Processing.A similar argument exists for
push_style
andpop_style
.Implementation
The general approach is explicitly storing the state in a global stack created in
p5.py
instead of using the stack of a context manager.Here's a snippet of example code for
push_matrix
andpop_matrix
. I used print statements as placeholders to better visualize the effect.Example of the code in action:
push_style
andpop_style
can be implemented using the same idea. However, there is a large number of style components to be recorded. Instead of creating a stack for each style component, I plan to write aStyle
class and two helper functions to encode and decode (+restore) it.Todo
pushMatrix
&popMatrix
pushStyle
&popStyle
The text was updated successfully, but these errors were encountered: