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

Encapsulate relative position logic in UITextInput.on_event #2327

Draft
wants to merge 5 commits into
base: development
Choose a base branch
from

Conversation

pushfoo
Copy link
Member

@pushfoo pushfoo commented Jul 29, 2024

  • Add relative position helpers
  • Use position helpers to clean up UITextInput.on_event
  • Some light refactoring for readability
  • Improve related docstrings and comments

* Add UIWidget._event_pos_relative_to_self

* Add UITextInput._event_pos_relative_to_self

* Use new method to clean up UITextInput.on_event
* Use self._event_pos_relative_to_self

* Restructure it to put the type check

* Add comments explaining when / why things are handled
* Use inside_xy in the lower block as well

* Improve UITextInput.on_event docstring + comments
@pushfoo pushfoo requested a review from eruvanos July 29, 2024 00:22
@pushfoo
Copy link
Member Author

pushfoo commented Jul 29, 2024

@DigiDuncan Let me know if the relative position helper seems to belong in Rect or if I somehow missed one like it already existing.

@pushfoo pushfoo added this to the Future milestone Sep 30, 2024
Comment on lines +181 to +194
def _event_pos_relative_to_self(self, mouse_event: UIMouseEvent) -> Vec2 | None:
"""Gets coords relative to bottom left if inside the widget.

Args:
mouse_event: Any :py:class:`UIMouseEvent`.
Returns:
``None`` if outside the widget or coords relative to
the widget's bottom left.
"""
pos = mouse_event.pos
if not self.rect.point_in_rect(pos):
return None
return Vec2(pos[0] - self.left, pos[1] - self.bottom)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Update to use the new Rect method.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't update it yet because I want to give it more thought after rereading the UI code and considering the following:

  1. 3.9 features will be our new minimum
  2. The new rect method seems to perform more ops
  3. It may add even more call overhead

@pushfoo pushfoo marked this pull request as draft October 1, 2024 00:46
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