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

C#: Can't compare ButtonIndex property (int) to ButtonList enum value, e.g. ButtonList.Left #41552

Closed
MinimumEntropy opened this issue Aug 26, 2020 · 6 comments

Comments

@MinimumEntropy
Copy link

Godot version: 3.2.2.stable.mono.official

OS/device including version: Windows 10

Issue description:
I wrote C# code to catch the _Input event, then checked for multiple clicks by first casting to InputEventMouseButton with the is keyword, then checking if the InputEventMouseButton's ButtonIndex.Equals(ButtonList.Left). This never was true. However, upon replacing the ButtonList.Left with simply 1, it works perfectly. I find this strange as it works with (for example) WheelUp and WheelDown.

Steps to reproduce: Please simply download below, run, and observe the debugging output! My code is also written below:

using Godot;

public class example : Spatial
{
    public override void _Input(InputEvent inputEvent)
    {
        if (inputEvent is InputEventMouseButton buttonEvent)
        {
            if (buttonEvent.IsPressed()) // limits prints per click to one for readability, can remove if needed
            {
                if (buttonEvent.ButtonIndex.Equals(1)) GD.Print("Mouse Button Pressed Using 1");
                if (buttonEvent.ButtonIndex.Equals(ButtonList.Left)) GD.Print("Mouse Button Pressed Using ButtonList");
            }
        }
    }
}

Minimal reproduction project:
MinimalExampleForButtonListIssue.zip

@MinimumEntropy MinimumEntropy changed the title ButtonList.Left Does not seem to defined as 1 ButtonList.Left does not seem to defined as 1 Aug 26, 2020
@akien-mga
Copy link
Member

akien-mga commented Aug 27, 2020

I'm not sure it's a bug, C# probably doesn't auto-cast enum types to int, and buttonEvent.ButtonIndex returns an int and not an enum value. Casting ButtonList.Left to int or buttonEvent.ButtonIndex to ButtonList works fine:

if (buttonEvent.ButtonIndex.Equals(1)) GD.Print("Mouse Button Pressed Using 1");
if (((ButtonList)buttonEvent.ButtonIndex).Equals(ButtonList.Left)) GD.Print("Mouse Button Pressed Using ButtonList (cast as enum)");
if (buttonEvent.ButtonIndex.Equals((int)ButtonList.Left)) GD.Print("Mouse Button Pressed Using ButtonList (cast to int)");

I agree that it's not the best UX wise, but I'm not sure how this can be solved in C#. CC @neikeq

@akien-mga akien-mga changed the title ButtonList.Left does not seem to defined as 1 C#: Can't compare ButtonIndex property (int) to ButtonList enum value, e.g. ButtonList.Left Aug 27, 2020
@neikeq
Copy link
Contributor

neikeq commented Aug 27, 2020

Can ButtonIndex return anything other than a value from the enum? If not then then it should return ButtonList instead of int.

@neikeq
Copy link
Contributor

neikeq commented Aug 27, 2020

This was going to be fixed by #27450. It's pending being salvaged by @aaronfranke. Unless he can't work on it anymore, in which case I would take care of it at some point before 4.0.

@aaronfranke
Copy link
Member

I can't salvage #27450 without conflicting with #38054, so I would prefer that be merged first.

@neikeq
Copy link
Contributor

neikeq commented Aug 27, 2020

@aaronfranke Yeah, no hurries. 4.0 is still far.

@raulsntos
Copy link
Member

This was implemented by #47378

@akien-mga akien-mga added this to the 4.0 milestone Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants