Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Mambouna
Copy link

@Mambouna Mambouna commented Apr 3, 2025

Removes usage of old mouse enum.

Introduces new mouse built-in object that is backwards compatible with old mouse enum. Old projects can stay the same and should continue working.

Functions of new mouse:

  • mouse.pressed to get a tuple of all button states.
  • mouse.pressed_left to get just the state of the left mouse button. pressed_right and pressed_middle are also available.
  • mouse.pos gives the current position. Can also be set to change the mouse position.
  • mouse.rel gives the relative position change of the mouse since the last frame.
  • mouse.last_called_pos and last_called_rel give the position and change since the last time this property was accessed.
  • mouse.recent_pos and recent_rel give tuples of the last n positions and relative changes of the mouse. Default for n is 60 and can be changed via mouse.recent_pos_max and recent_rel_max.
  • mouse.visible gives the boolean of the visibility state. Can also be set.
  • mouse.focused gives the boolean of whether the window is focused.
  • TEMP REMOVED: mouse.cursor gives the current cursor name if it is of type system or a custom cursor image. Can also be set to a few different system cursors or a custom image. If a custom image is used, a hotspot can be given as well.
  • TEMP REMOVED: mouse.cursor_name gives just the name of the cursor. Can't be set. Same goes for mouse.cursor_hotspot.

Documentation has been updated.

Fixes #334. Fixes #212.

Mambouna added 4 commits April 3, 2025 19:29
Removes usage of old mouse enum.

Introduces new mouse built-in object that is backwards compatible with old mouse enum. Old projects can stay the same and should continue working.

Functions of new mouse:
- `mouse.pressed` to get a tuple of all button states.
- `mouse.pressed_left` to get just the state of the left mouse button. `pressed_right` and `pressed_middle` are also available.
- `mouse.pos` gives the current position. Can also be set to change the mouse position.
- `mouse.rel` gives the last relative position change of the mouse.
- `mouse.visible` gives the boolean of the visibility state. Can also be set.
- `mouse.focused` gives the boolean of whether the window is focused.
- `mouse.cursor` gives the current cursor name if it is of type `system` or a custom cursor image. Can also be set to a few different system cursors or a custom image. If a custom image is used, a hotspot can be given as well.
- `mouse.cursor_name` gives just the name of the cursor. Can't be set. Same goes for `mouse.cursor_hotspot`.

Functionality of a deque of recent positions as well as updated documentation will be added in further commits.
Added deques of the last relative position changes by mouse-move events as well as the absolute positions after all those movements.
- `mouse.recent_rel` and `mouse.recent_pos` to access a tuple of those movements.
- `mouse.recent_rel_max` and `mouse.recent_pos_max` control how many move events back are recorded.

Reworked the majority of attributes to perform better and more consistently.
- `mouse.pressed` is now not recalculated every call. Same goes for the individual buttons.
- Same for `mouse.pos` and `mouse.rel`. Rel additionally sums the relative movement of all move events over one frame which makes it consistent with calling `pygame.mouse.get_rel()` every frame.
- To retain the function of `get_rel()` separately, `mouse.last_called_rel` and `mouse.last_called_pos` were added. These give the position and relative change from when they were last accessed.
@lordmauve
Copy link
Owner

This looks very good. I need to do a more detailed review though.

@Mambouna
Copy link
Author

Mambouna commented Apr 7, 2025

Thanks for the heads up, the one thing I am unsure about currently is whether to put the cursor functionality in attributes too. In the meantime, I'll try to suss out the reason for the failed test. It doesn't reproduce immediately on my system but I'll try to look into it more.

@Mambouna
Copy link
Author

Hopefully the tests run fine now, it was a dumb oversight by me.

@lordmauve lordmauve changed the title FIX #334 - Expanded mouse functionality Expanded mouse functionality Apr 14, 2025
@Mambouna
Copy link
Author

Okay, at this point I'm somewhat confused. The error that we had beforehand with the test is fixed by importing the new mouse, which made sense to me. However, now flake8 acts up because we don't use it in that specific file.
My familiarity with unittesting in python is only basic, so I don't know if we can bring pgzero.mouse into scope without making flake8 angry that it isn't used. Maybe I'm missing something obvious, if that's the case feel free to correct it directly if that's less hassle for you.

@Mambouna
Copy link
Author

@lordmauve Any idea how to not import the unused mouse but not have the tests fail?

@Mambouna
Copy link
Author

I added a full suite of unittests for the new mouse now. These aren't quite optimal yet, since some functionality relies of triggering Pygame events and I haven't found a way to wait for one of those to resolve before doing an assert, but I am quite certain that the assumptions made getting around those restrictions will always remain static, so I hope they are still good tests.

Again, I'm not that familiar with testing yet, so feedback would be appreciated.

@Mambouna
Copy link
Author

@lordmauve I think the code fails on MacOS with a strange error about the cursor. From my understanding, that shouldn't really happen since when an OS doesn't support a certain system cursor type, it should fall back to the default cursor, which it for some reason did not do here.

I'll try to find out what's going on but if it's a bug in Pygame, maybe we take out access to the cursor for now and add that back in later?

@Mambouna
Copy link
Author

After some research and testing, I couldn't find a good way to get reproducible results or more info on the cause of the error. In the interest of not bricking MacOS, I've removed the cursor customization for now and propose we look at it again once the other stuff is sorted out successfully.

@Mambouna
Copy link
Author

@lordmauve I've got a workaround where if a system cursor is not available, it simply ignores the requested change and prints a warning. That would be an easy adjustment and I've also got the tests working again I think. The question remains if we want to split the capabilities based on platform like this. Another option would be to only allow custom cursors from images instead. This way, the normal errors if an image aren't available are already familiar and there is no disconnect between platforms. What to do you think?

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.

Add methods/attributes to mouse builtin Request Hide Mouse method

2 participants