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

Derive Ord for GamepadButtonType. #11791

Merged
merged 1 commit into from
Feb 11, 2024

Conversation

shanecelis
Copy link
Contributor

Objective

Use GamepadButtonType with library that requires Ord.

Motivation

KeyCode derives Ord that I'm using with a trie for recognizing input sequences. I would like to do the same for GamepadButtonType but am stymied by it not deriving Ord.

Solution

This PR add derivations PartialOrd and Ord for GamepadButtonType.

Workaround

If deriving Ord is not possible, I'd be happy to know how I might coerce GamepadButtonType into a u32 or something else that is Ord, so I can wrap GamepadButtonType in a newtype. I suppose serializing with serde may work or reflect?

KeyCode derives Ord that I'm using with a trie for recognizing [input
sequences](https://github.com/shanecelis/bevy-input-sequence/tree/trie).
I would like to do the same for GamepadButtonType but am stymied by it
not deriving Ord.

If deriving Ord is not possible, I'd be happy to know how I might coerce
GamepadButtonType into a `u32` or something else that is Ord.
@Kanabenki Kanabenki added A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A simple quality-of-life change that makes Bevy easier to use labels Feb 9, 2024
Copy link
Contributor

@Kanabenki Kanabenki left a comment

Choose a reason for hiding this comment

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

I don't see any reason not to implement those traits here, looks good to me 🙂

@shanecelis
Copy link
Contributor Author

shanecelis commented Feb 9, 2024

Reflect Workaround

I did find this cursed workaround.

#[derive(PartialEq, Eq, Clone, Debug)]
pub(crate) struct GamepadButton(GamepadButtonType);

impl From<GamepadButtonType> for GamepadButton {
    fn from(a: GamepadButtonType) -> Self {
        Self(a)
    }
}

impl PartialOrd for GamepadButton {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        use bevy::reflect::Enum;
        self.0.variant_index().partial_cmp(&other.0.variant_index())
    }
}

impl Ord for GamepadButton {
    fn cmp(&self, other: &Self) -> Ordering {
        use bevy::reflect::Enum;
        self.0.variant_index().cmp(&other.0.variant_index())
    }
}

@james7132 james7132 added this pull request to the merge queue Feb 11, 2024
Merged via the queue into bevyengine:main with commit 61e01e4 Feb 11, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A simple quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants