Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:15

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_JOYSTICK_HPP
0026 #define SFML_JOYSTICK_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Window/Export.hpp>
0032 #include <SFML/System/String.hpp>
0033 
0034 
0035 namespace sf
0036 {
0037 ////////////////////////////////////////////////////////////
0038 /// \brief Give access to the real-time state of the joysticks
0039 ///
0040 ////////////////////////////////////////////////////////////
0041 class SFML_WINDOW_API Joystick
0042 {
0043 public:
0044 
0045     ////////////////////////////////////////////////////////////
0046     /// \brief Constants related to joysticks capabilities
0047     ///
0048     ////////////////////////////////////////////////////////////
0049     enum
0050     {
0051         Count       = 8,  //!< Maximum number of supported joysticks
0052         ButtonCount = 32, //!< Maximum number of supported buttons
0053         AxisCount   = 8   //!< Maximum number of supported axes
0054     };
0055 
0056     ////////////////////////////////////////////////////////////
0057     /// \brief Axes supported by SFML joysticks
0058     ///
0059     ////////////////////////////////////////////////////////////
0060     enum Axis
0061     {
0062         X,    //!< The X axis
0063         Y,    //!< The Y axis
0064         Z,    //!< The Z axis
0065         R,    //!< The R axis
0066         U,    //!< The U axis
0067         V,    //!< The V axis
0068         PovX, //!< The X axis of the point-of-view hat
0069         PovY  //!< The Y axis of the point-of-view hat
0070     };
0071 
0072     ////////////////////////////////////////////////////////////
0073     /// \brief Structure holding a joystick's identification
0074     ///
0075     ////////////////////////////////////////////////////////////
0076     struct SFML_WINDOW_API Identification
0077     {
0078         Identification();
0079 
0080         String       name;      //!< Name of the joystick
0081         unsigned int vendorId;  //!< Manufacturer identifier
0082         unsigned int productId; //!< Product identifier
0083     };
0084 
0085     ////////////////////////////////////////////////////////////
0086     /// \brief Check if a joystick is connected
0087     ///
0088     /// \param joystick Index of the joystick to check
0089     ///
0090     /// \return True if the joystick is connected, false otherwise
0091     ///
0092     ////////////////////////////////////////////////////////////
0093     static bool isConnected(unsigned int joystick);
0094 
0095     ////////////////////////////////////////////////////////////
0096     /// \brief Return the number of buttons supported by a joystick
0097     ///
0098     /// If the joystick is not connected, this function returns 0.
0099     ///
0100     /// \param joystick Index of the joystick
0101     ///
0102     /// \return Number of buttons supported by the joystick
0103     ///
0104     ////////////////////////////////////////////////////////////
0105     static unsigned int getButtonCount(unsigned int joystick);
0106 
0107     ////////////////////////////////////////////////////////////
0108     /// \brief Check if a joystick supports a given axis
0109     ///
0110     /// If the joystick is not connected, this function returns false.
0111     ///
0112     /// \param joystick Index of the joystick
0113     /// \param axis     Axis to check
0114     ///
0115     /// \return True if the joystick supports the axis, false otherwise
0116     ///
0117     ////////////////////////////////////////////////////////////
0118     static bool hasAxis(unsigned int joystick, Axis axis);
0119 
0120     ////////////////////////////////////////////////////////////
0121     /// \brief Check if a joystick button is pressed
0122     ///
0123     /// If the joystick is not connected, this function returns false.
0124     ///
0125     /// \param joystick Index of the joystick
0126     /// \param button   Button to check
0127     ///
0128     /// \return True if the button is pressed, false otherwise
0129     ///
0130     ////////////////////////////////////////////////////////////
0131     static bool isButtonPressed(unsigned int joystick, unsigned int button);
0132 
0133     ////////////////////////////////////////////////////////////
0134     /// \brief Get the current position of a joystick axis
0135     ///
0136     /// If the joystick is not connected, this function returns 0.
0137     ///
0138     /// \param joystick Index of the joystick
0139     /// \param axis     Axis to check
0140     ///
0141     /// \return Current position of the axis, in range [-100 .. 100]
0142     ///
0143     ////////////////////////////////////////////////////////////
0144     static float getAxisPosition(unsigned int joystick, Axis axis);
0145 
0146     ////////////////////////////////////////////////////////////
0147     /// \brief Get the joystick information
0148     ///
0149     /// \param joystick Index of the joystick
0150     ///
0151     /// \return Structure containing joystick information.
0152     ///
0153     ////////////////////////////////////////////////////////////
0154     static Identification getIdentification(unsigned int joystick);
0155 
0156     ////////////////////////////////////////////////////////////
0157     /// \brief Update the states of all joysticks
0158     ///
0159     /// This function is used internally by SFML, so you normally
0160     /// don't have to call it explicitly. However, you may need to
0161     /// call it if you have no window yet (or no window at all):
0162     /// in this case the joystick states are not updated automatically.
0163     ///
0164     ////////////////////////////////////////////////////////////
0165     static void update();
0166 };
0167 
0168 } // namespace sf
0169 
0170 
0171 #endif // SFML_JOYSTICK_HPP
0172 
0173 
0174 ////////////////////////////////////////////////////////////
0175 /// \class sf::Joystick
0176 /// \ingroup window
0177 ///
0178 /// sf::Joystick provides an interface to the state of the
0179 /// joysticks. It only contains static functions, so it's not
0180 /// meant to be instantiated. Instead, each joystick is identified
0181 /// by an index that is passed to the functions of this class.
0182 ///
0183 /// This class allows users to query the state of joysticks at any
0184 /// time and directly, without having to deal with a window and
0185 /// its events. Compared to the JoystickMoved, JoystickButtonPressed
0186 /// and JoystickButtonReleased events, sf::Joystick can retrieve the
0187 /// state of axes and buttons of joysticks at any time
0188 /// (you don't need to store and update a boolean on your side
0189 /// in order to know if a button is pressed or released), and you
0190 /// always get the real state of joysticks, even if they are
0191 /// moved, pressed or released when your window is out of focus
0192 /// and no event is triggered.
0193 ///
0194 /// SFML supports:
0195 /// \li 8 joysticks (sf::Joystick::Count)
0196 /// \li 32 buttons per joystick (sf::Joystick::ButtonCount)
0197 /// \li 8 axes per joystick (sf::Joystick::AxisCount)
0198 ///
0199 /// Unlike the keyboard or mouse, the state of joysticks is sometimes
0200 /// not directly available (depending on the OS), therefore an update()
0201 /// function must be called in order to update the current state of
0202 /// joysticks. When you have a window with event handling, this is done
0203 /// automatically, you don't need to call anything. But if you have no
0204 /// window, or if you want to check joysticks state before creating one,
0205 /// you must call sf::Joystick::update explicitly.
0206 ///
0207 /// Usage example:
0208 /// \code
0209 /// // Is joystick #0 connected?
0210 /// bool connected = sf::Joystick::isConnected(0);
0211 ///
0212 /// // How many buttons does joystick #0 support?
0213 /// unsigned int buttons = sf::Joystick::getButtonCount(0);
0214 ///
0215 /// // Does joystick #0 define a X axis?
0216 /// bool hasX = sf::Joystick::hasAxis(0, sf::Joystick::X);
0217 ///
0218 /// // Is button #2 pressed on joystick #0?
0219 /// bool pressed = sf::Joystick::isButtonPressed(0, 2);
0220 ///
0221 /// // What's the current position of the Y axis on joystick #0?
0222 /// float position = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
0223 /// \endcode
0224 ///
0225 /// \see sf::Keyboard, sf::Mouse
0226 ///
0227 ////////////////////////////////////////////////////////////