Skip to content
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

Shapes refactor #323

Merged
merged 3 commits into from
Dec 2, 2024
Merged

Shapes refactor #323

merged 3 commits into from
Dec 2, 2024

Conversation

manuq
Copy link
Contributor

@manuq manuq commented Nov 27, 2024

This is a refactor of the block background which has different shapes for the different block types. To make the work in #273 easier to rebase.

For readability, remove the ternary conditional operator, which was a
long one-liner.
Remove the separate stroke line "edge_polygon" which was used only for
control blocks. Since this line is connected to the "stroke_polygon" it
can be joined with it. Also the "edge_polygon" was adding a redundant
draw to non-control blocks.

Also remove the outline_middle calculation. Taking screenshots as proof,
this extra calculation has no effect in the drawing.
Copy link
Member

@wjt wjt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is easier to follow.

var box_shape = _get_box_shape(size)
var bottom_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, size.y))
bottom_knob_shape.reverse()
return box_shape.slice(0, 3) + bottom_knob_shape + box_shape.slice(3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers you're passing to slice() here and in similar functions rely on the fact that _get_box_shape() returns exactly 5 co-ordinates in clockwise order from the top-left. It works but feels a bit error-prone.

I'm not sure what I'd do differently though. I guess you would end up open-coding those 4 coordinates in each of these places. That might be OK, it's just various combinations of 0 and box_size.[xy]. Anyway, no need to change it, it just feels a bit off to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I added the "box shape" a couple minutes before submitting, and I'm not sure if it's worth it. Previously the only thing outside each draw function was the "get knob shape" and the rest was a straightforward list of points, although a bit of a boilerplate. I could go back to the boilerplate.

Another option would be to pass a dict to _get_box_shape() with optional tweaks for each side of the rectangle. Something like _get_box_shape(size, {"bottom": bottom_knob_shape}).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried these 2 options and non of them looked better to me.


if shift_top > 0:
stroke_polygon.append(Vector2(0.0, 0.0))
if block_type == Types.BlockType.ENTRY:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a match statement?

Refactor to have an easier to read separate draw function for each block
type. Each draw function is now straightforward, without if/else
conditionals.

Instead of having multiple attributes like "shift_top", "show_top" to
instruct the drawing, directly pass the block type. For control blocks
that have 2 backgrounds, one for top and one for the bottom part, also
expose a control_part attribute.
@manuq manuq merged commit a3cba20 into main Dec 2, 2024
3 checks passed
@manuq manuq deleted the shapes-refactor branch December 2, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants