Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: create sf::Joystick::Button enum  (Read 3114 times)

0 Members and 1 Guest are viewing this topic.

jgreene

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
create sf::Joystick::Button enum
« on: April 21, 2017, 03:57:40 pm »
Both sf::Keyboard and sf:Mouse have enums for all buttons/keys supported. would it be plausible to create sf::Joystick::Button enums.. even something like Joy0Button0 through Joy7Button31 ? Doing so would be useful to facilitate the handling of input configuration.
I'm looking into it, but am an amateur coder at best. It may be that doing so would necessitate changes to sf:Joystick:isButtonPressed which I realize may be undesired.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: create sf::Joystick::Button enum
« Reply #1 on: April 21, 2017, 06:41:04 pm »
I have no idea how putting the name "Button0" on button index 0 would improve anything. Would you mind explaining?
Laurent Gomila - SFML developer

jgreene

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: create sf::Joystick::Button enum
« Reply #2 on: April 21, 2017, 08:31:56 pm »
Say I'm coding up a preferences/hotkey config routine for my game. I can convert strings like "A" to sf::Keyboard::A, or "MouseMiddle" to sf::Mouse::Middle. I can then in my functions associate "Fire" to {type=Mouse,key=sf::Mouse::Middle} and in my function easily call either sf::Mouse::isButtonPressed(my::HotKey::Fire["key"]) based on Fire being assigned to the enum value with the type set to Mouse. 
I cannot do that (or can I?) with Joystick Buttons as I have to pass 2 values Joystick number and Button number to sf::Joystick::isButtonPressed(unsigned int joystick, unsigned int button). I mean if I could assign my::HotKey::Fire["key"] to be {0,0} and pass Fire["key"] to sf::Joystick::isButtonPressed(my::HotKey::Fire["key"]), then I guess my request would make less sense... as I said I am an amateur at this. Maybe there's already a clean way to do this and I need to go find a nice class  to take or book to read..  :)

jgreene

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: create sf::Joystick::Button enum
« Reply #3 on: April 21, 2017, 08:47:19 pm »
maybe I should just create a local enum  my::HotKey::"HotKeyName"  as a struct {type="Joystick";key={0,1}}  and rewrite some of the other code I'm working on(probably have to anyway).. I could just be trying to oversimplify by getting it so sf::Joystick::isButtonPressed() takes a single enumerated value (like the other major input types) rather than a  pair...granted it'd be an enum list 256 entries long.. but if it was a single byte...
0x00-0x1f could be Joystick0Button0-Joystick0Button31
0x20-0x3f could be Joystick1Button0-Joystick1Button31
0x40-0x5f could be Joystick2Button0-Joystick2Button31
0x60-0x7f could be Joystick3Button0-Joystick3Button31
0x80-0x9f could be Joystick4Button0-Joystick4Button31
0xa0-0xbf could be Joystick5Button0-Joystick5Button31
0xc0-0xdf could be Joystick6Button0-Joystick6Button31
0xe0-0xff could be Joystick7Button0-Joystick7Button31
8 sticks, 32 buttons each...
on the other hand I could be completely misunderstanding  how sf::Joystick works....Mouse and Keyboard have nonumeric instance IDs to worry with.
« Last Edit: April 21, 2017, 08:49:58 pm by jgreene »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: create sf::Joystick::Button enum
« Reply #4 on: April 21, 2017, 09:26:02 pm »
If all you want is to have a single value, for consistency with mouse and keyboard, then you can easily combine the joystick index and the button number into one value yourself, and decompose it when it comes to calling the sf::Joystick functions.

An enum would not only be a big unmaintainable mess, but worst: it would break the API, as we would no longer need to pass the joystick index and button number separately. It would also be inconsistent with other joystick functions still taking the joystick index.

Keyboard an Mouse should use indices too, nothing prevents one from plugging and using multiple keyboards or mice (don't know about keyboard, but it actually happens for mice). Since it's quite uncommon, SFML keeps things simple and assumes a single keyboard and mouse. Obviously, the same simplification doesn't apply for joysticks.
Laurent Gomila - SFML developer

jgreene

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: create sf::Joystick::Button enum
« Reply #5 on: April 22, 2017, 02:37:59 am »
ok.