Date:2013-09-07 17:00:57 (7 months 16 days ago)
Author:Paul Cercueil
Commit:dba6c321097ef566d6ead3009b16fca61b2fa28f
Message:Change API of InputManager: we don't care about key release events

Files: src/gmenu2x.cpp (1 diff)
src/inputmanager.cpp (5 diffs)
src/inputmanager.h (2 diffs)
src/messagebox.cpp (1 diff)

Change Details

src/gmenu2x.cpp
628628        }
629629
630630        // Handle other input events.
631        InputManager::ButtonEvent event;
631        InputManager::Button button;
632632        bool gotEvent;
633633        const bool wait = !animating;
634634        do {
635            do {
636                gotEvent = input.getEvent(&event, wait);
637            } while (gotEvent && event.state != InputManager::PRESSED);
635            gotEvent = input.getButton(&button, wait);
638636        } while (wait && !gotEvent);
639637        if (gotEvent) {
640638            for (auto it = layers.rbegin(); it != layers.rend(); ++it) {
641                if ((*it)->handleButtonPress(event.button)) {
639                if ((*it)->handleButtonPress(button)) {
642640                    break;
643641                }
644642            }
src/inputmanager.cpp
117117}
118118
119119InputManager::Button InputManager::waitForPressedButton() {
120    ButtonEvent event;
121    while (!waitForEvent(&event) || event.state != PRESSED);
122    return event.button;
120    Button button;
121    while (!getButton(&button, true));
122    return button;
123123}
124124
125bool InputManager::waitForEvent(ButtonEvent *event) {
126    return getEvent(event, true);
125bool InputManager::pollButton(Button *button) {
126    return getButton(button, false);
127127}
128128
129bool InputManager::pollEvent(ButtonEvent *event) {
130    return getEvent(event, false);
131}
132
133bool InputManager::getEvent(ButtonEvent *bevent, bool wait) {
129bool InputManager::getButton(Button *button, bool wait) {
134130    //TODO: when an event is processed, program a new event
135131    //in some time, and when it occurs, do a key repeat
136132
...... 
142138#endif
143139
144140    SDL_Event event;
145    if (wait) {
141    if (wait)
146142        SDL_WaitEvent(&event);
147    } else {
148        bevent->state = RELEASED;
149        if (!SDL_PollEvent(&event)) {
150            return false;
151        }
152    }
143    else if (!SDL_PollEvent(&event))
144        return false;
153145
154146    ButtonSource source;
155147    switch(event.type) {
156148        case SDL_KEYDOWN:
157            bevent->state = PRESSED;
158            source = KEYBOARD;
159            break;
160        case SDL_KEYUP:
161            bevent->state = RELEASED;
162149            source = KEYBOARD;
163150            break;
164151#ifndef SDL_JOYSTICK_DISABLED
165152        case SDL_JOYBUTTONDOWN:
166            bevent->state = PRESSED;
167            source = JOYSTICK;
168            break;
169        case SDL_JOYBUTTONUP:
170            bevent->state = RELEASED;
171153            source = JOYSTICK;
172154            break;
173155#endif
...... 
191173
192174            if (event.user.data1)
193175                free(event.user.data1);
194            bevent->state = PRESSED;
195            bevent->button = REPAINT;
176            *button = REPAINT;
196177            return true;
197178
198179        default:
...... 
203184        for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
204185            if (buttonMap[i].source == KEYBOARD
205186                    && (unsigned int)event.key.keysym.sym == buttonMap[i].code) {
206                bevent->button = static_cast<Button>(i);
187                *button = static_cast<Button>(i);
207188                break;
208189            }
209190        }
...... 
212193        for (i = 0; i < BUTTON_TYPE_SIZE; i++) {
213194            if (buttonMap[i].source == JOYSTICK
214195                    && (unsigned int)event.jbutton.button == buttonMap[i].code) {
215                bevent->button = static_cast<Button>(i);
196                *button = static_cast<Button>(i);
216197                break;
217198            }
218199        }
src/inputmanager.h
4545    };
4646    #define BUTTON_TYPE_SIZE 10
4747
48    enum ButtonState { PRESSED, RELEASED };
49    struct ButtonEvent {
50        Button button;
51        ButtonState state;
52    };
53
5448    InputManager();
5549    ~InputManager();
5650
5751    void init(const std::string &conffile, Menu *menu);
58    bool waitForEvent(ButtonEvent *event);
5952    Button waitForPressedButton();
60    bool pollEvent(ButtonEvent *event);
61    bool getEvent(ButtonEvent *bevent, bool wait);
53    bool pollButton(Button *button);
54    bool getButton(Button *button, bool wait);
6255
6356private:
57    void readConfFile(const std::string &conffile);
58
6459    enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK };
6560    struct ButtonMapEntry {
6661        ButtonSource source;
...... 
6964
7065    Menu *menu;
7166
72    void readConfFile(const std::string &conffile);
73
7467    ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
7568#ifndef SDL_JOYSTICK_DISABLED
7669    std::vector<SDL_Joystick *> joysticks;
src/messagebox.cpp
9999
100100    int result = -1;
101101    while (result < 0) {
102        InputManager::ButtonEvent event;
103        if (gmenu2x->input.pollEvent(&event)
104                && event.state == InputManager::PRESSED
105                && !buttons[event.button].empty()) {
106            result = event.button;
102        InputManager::Button button;
103        if (gmenu2x->input.pollButton(&button)
104                && !buttons[button].empty()) {
105            result = button;
107106        }
108107
109108        usleep(LOOP_DELAY);

Archive Download the corresponding diff file



interactive