Date:2013-09-07 17:09:56 (7 months 11 days ago)
Author:Paul Cercueil
Commit:3ce314c65d003ded8ea3081e725a4063998521b1
Message:Make the InputManager handle analog sticks as input

Files: src/inputmanager.cpp (6 diffs)
src/inputmanager.h (1 diff)

Change Details

src/inputmanager.cpp
4848        return;
4949    }
5050
51    for (i = 0; i < SDL_NumJoysticks(); i++)
52        joysticks.push_back(SDL_JoystickOpen(i));
51    for (i = 0; i < SDL_NumJoysticks(); i++) {
52        struct Joystick joystick = {
53            SDL_JoystickOpen(i), false, false, false, false,
54        };
55        joysticks.push_back(joystick);
56    }
57
5358    DEBUG("Opening %i joysticks\n", i);
5459#endif
5560}
...... 
5762InputManager::~InputManager()
5863{
5964#ifndef SDL_JOYSTICK_DISABLED
60    for (std::vector<SDL_Joystick *>::iterator it = joysticks.begin();
61                it < joysticks.end(); it++)
62        SDL_JoystickClose(*it);
65    for (auto it : joysticks)
66        SDL_JoystickClose(it.joystick);
6367#endif
6468}
6569
...... 
130134    //TODO: when an event is processed, program a new event
131135    //in some time, and when it occurs, do a key repeat
132136
133    int i;
134
135137#ifndef SDL_JOYSTICK_DISABLED
136138    if (joysticks.size() > 0)
137139        SDL_JoystickUpdate();
...... 
152154        case SDL_JOYBUTTONDOWN:
153155            source = JOYSTICK;
154156            break;
157        case SDL_JOYAXISMOTION: {
158                source = JOYSTICK;
159
160                unsigned int axis = event.jaxis.axis;
161                /* We only handle the first joystick */
162                if (axis > 1)
163                    return false;
164
165                bool *axisState = joysticks[event.jaxis.which].axisState[axis];
166
167                if (event.jaxis.value < -20000) {
168                    if (axisState[AXIS_STATE_NEGATIVE])
169                        return false;
170                    axisState[AXIS_STATE_NEGATIVE] = true;
171                    axisState[AXIS_STATE_POSITIVE] = false;
172                    *button = axis ? UP : LEFT;
173                } else if (event.jaxis.value > 20000) {
174                    if (axisState[AXIS_STATE_POSITIVE])
175                        return false;
176                    axisState[AXIS_STATE_NEGATIVE] = false;
177                    axisState[AXIS_STATE_POSITIVE] = true;
178                    *button = axis ? DOWN : RIGHT;
179                } else {
180                    axisState[0] = axisState[1] = false;
181                    return false;
182                }
183                break;
184            }
155185#endif
156186        case SDL_USEREVENT:
157187            switch ((enum EventCode) event.user.code) {
...... 
180210            return false;
181211    }
182212
213    int i = 0;
183214    if (source == KEYBOARD) {
184215        for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
185216            if (buttonMap[i].source == KEYBOARD
...... 
189220            }
190221        }
191222#ifndef SDL_JOYSTICK_DISABLED
192    } else if (source == JOYSTICK) {
223    } else if (source == JOYSTICK && event.type != SDL_JOYAXISMOTION) {
193224        for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
194225            if (buttonMap[i].source == JOYSTICK
195226                    && (unsigned int)event.jbutton.button == buttonMap[i].code) {
src/inputmanager.h
6666
6767    ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
6868#ifndef SDL_JOYSTICK_DISABLED
69    std::vector<SDL_Joystick *> joysticks;
69#define AXIS_STATE_POSITIVE 0
70#define AXIS_STATE_NEGATIVE 1
71    struct Joystick {
72        SDL_Joystick *joystick;
73        bool axisState[2][2];
74    };
75
76    std::vector<Joystick> joysticks;
7077#endif
7178};
7279

Archive Download the corresponding diff file



interactive