arcade icon indicating copy to clipboard operation
arcade copied to clipboard

Reinvent the Rectangle, or The First Step Towards Rectangle Unification.

Open DigiDuncan opened this issue 1 year ago • 4 comments

Take that, Thales of Miletus.

For years, Arcade has suffered from Rectangle fragmentation. The GUI, GL, Camera, draw commands, they all redefine and reinvent the rectangle. Need proof? We have proof. But why have many rectangle when one Rect do trick?

With Rect, you can:

  • Get every side, vertex, and size you'd ever want
  • Reorient and resize the Rect to your heart's content
  • Align it in frankly an absurd amount of ways
  • Constrain and expand the heck out of it
  • Union and intersect it with other Rects (now with bonus | and & support)
  • Find if points are in and on them
  • Turn them into GL-friendly (and even GL-unfriendly!) tuples

They have a beautiful __str__ method, backwards-compatible aliases, dead-simple property names, and implement every way rectangles were used in the library.

You can create them from **kwargs, or using one of several DX-friendly helper methods, including LRBT, LBWH, XYWH, XYRR (you're welcome, @einarf!), and Viewport (you're welcome @DragonMoffon!)

And they're fast. All eights values are pre-calculated on __init__ (despite only needing four values to create), and they subclass NamedTuple, so dot-access is Sonic-level fast.

Immutable. Extendable. Human-usable.

Merging in this PR allows a full rework of every rectangle in Arcade, making them easy to use for contributors, developers, and end users.

And as a bonus, it cleans the heck out of draw_commands.

DigiDuncan avatar May 22 '24 02:05 DigiDuncan

Could we get a mutable rect as well?

benjamin-kirkbride avatar May 22 '24 21:05 benjamin-kirkbride

Just agreed (with @DigiDuncan) that RectInt should be removed after we merged this huge PR.

eruvanos avatar May 24 '24 20:05 eruvanos

Could we get a mutable rect as well?

Right now we stick to the immutable version only. From my point of view, the convenience benefits are not big enough to introduce a second Rect option, which is mutable.

eruvanos avatar May 24 '24 21:05 eruvanos

So summary:

I am fine with the PR. @einarf @pvcraven please give feedback what you think about the naming of the Rect constructor functions (XYWH, LBWH, ...) being uppercase.

I would merge the PR over the next day, if you give feedback afterwards. We can still do it in another PR.

Background: The camera changes are blocked and they are one of the major points holding us back from 3.0 so I would like to unblock them.

eruvanos avatar May 24 '24 21:05 eruvanos

@DigiDuncan Can you check, if you can make the PR mergable?

eruvanos avatar May 25 '24 13:05 eruvanos