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

Minor suggestion : add a NO_BUTTON mask for MouseMotion event #9

Closed
FractalWire opened this issue Feb 8, 2019 · 3 comments
Closed
Assignees
Labels

Comments

@FractalWire
Copy link

Hey there,

I'm currently fiddling with the Event module.
When using a MouseMotion event, you can know if a button is pressed with the state masks but you can't check if no button are pressed.
Of course, you could just do something like

if event.state == 0:
    #Do something

But it's less clear than having a mask I think.
So my suggestion would be to have a mask for this.
What do you think ?

@HexDecimal HexDecimal self-assigned this Feb 8, 2019
@HexDecimal
Copy link
Collaborator

Python conventions would recommend doing this for your example:

if not event.state:
    ...

The above code and your example are unambiguous, but you could add a comment if you needed it to be more verbose.

Realistically, the else clause could be used after checking for a state.

if event.state & tcod.event.BUTTON_LMASK:  # Left button held.
    ...
else:  # Left button not held.
    ...

Bit masks are meant to be used with bit-wise operators, so there isn't much reason to include a named zero. Make sure that you're using & and not == to check if a button is held.

The current names are derived directly from the SDL2 API, and probably won't change very much.

@FractalWire
Copy link
Author

I like the not event.state, I didn't thought about it.

The else clause is not useful in my case because I need to check specifically if there is no button pressed.
It's related to focus behaviour : an object will gain/lose focus when the mouse move and no button is pressed and is treated before any other event handling basically.

And thanks for the tips on bitwise operators.

@HexDecimal
Copy link
Collaborator

It's related to focus behaviour : an object will gain/lose focus when the mouse move and no button is pressed and is treated before any other event handling basically.

I'd still try to limit that behavior to only the buttons which will interact with those elements:

if not event.state & (tcod.event.BUTTON_LMASK | tcod.event.BUTTON_RMASK):
    ...  # Left and right buttons not held.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants