src/video/emscripten/SDL_emscriptenevents.c
changeset 10338 638ec32cfcce
parent 10337 8b18f8e41813
child 10541 727f7d796b8d
     1.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Tue Sep 13 00:04:00 2016 -0700
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Tue Sep 13 00:09:21 2016 -0700
     1.3 @@ -350,8 +350,10 @@
     1.4          default:
     1.5              return 0;
     1.6      }
     1.7 -    SDL_SendMouseButton(window_data->window, 0, eventType == EMSCRIPTEN_EVENT_MOUSEDOWN ? SDL_PRESSED : SDL_RELEASED, sdl_button);
     1.8 -    return 1;
     1.9 +
    1.10 +    SDL_EventType sdl_event_type = (eventType == EMSCRIPTEN_EVENT_MOUSEDOWN ? SDL_PRESSED : SDL_RELEASED);
    1.11 +    SDL_SendMouseButton(window_data->window, 0, sdl_event_type, sdl_button);
    1.12 +    return SDL_GetEventState(sdl_event_type) == SDL_ENABLE;
    1.13  }
    1.14  
    1.15  EM_BOOL
    1.16 @@ -377,7 +379,7 @@
    1.17      }
    1.18  
    1.19      SDL_SetMouseFocus(eventType == EMSCRIPTEN_EVENT_MOUSEENTER ? window_data->window : NULL);
    1.20 -    return 1;
    1.21 +    return SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE;
    1.22  }
    1.23  
    1.24  EM_BOOL
    1.25 @@ -385,7 +387,7 @@
    1.26  {
    1.27      SDL_WindowData *window_data = userData;
    1.28      SDL_SendMouseWheel(window_data->window, 0, wheelEvent->deltaX, -wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
    1.29 -    return 1;
    1.30 +    return SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE;
    1.31  }
    1.32  
    1.33  EM_BOOL
    1.34 @@ -397,8 +399,10 @@
    1.35      if (eventType == EMSCRIPTEN_EVENT_BLUR) {
    1.36          SDL_ResetKeyboard();
    1.37      }
    1.38 +
    1.39 +
    1.40      SDL_SendWindowEvent(window_data->window, eventType == EMSCRIPTEN_EVENT_FOCUS ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
    1.41 -    return 1;
    1.42 +    return SDL_GetEventState(SDL_WINDOWEVENT) == SDL_ENABLE;
    1.43  }
    1.44  
    1.45  EM_BOOL
    1.46 @@ -407,6 +411,7 @@
    1.47      SDL_WindowData *window_data = userData;
    1.48      int i;
    1.49      double client_w, client_h;
    1.50 +    int preventDefault = 0;
    1.51  
    1.52      SDL_TouchID deviceId = 1;
    1.53      if (SDL_AddTouch(deviceId, "") < 0) {
    1.54 @@ -434,22 +439,33 @@
    1.55                  SDL_SendMouseButton(window_data->window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
    1.56              }
    1.57              SDL_SendTouch(deviceId, id, SDL_TRUE, x, y, 1.0f);
    1.58 +
    1.59 +            if (!preventDefault && SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
    1.60 +                preventDefault = 1;
    1.61 +            }
    1.62          } else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) {
    1.63              if ((window_data->finger_touching) && (window_data->first_finger == id)) {
    1.64                  SDL_SendMouseMotion(window_data->window, SDL_TOUCH_MOUSEID, 0, x, y);
    1.65              }
    1.66              SDL_SendTouchMotion(deviceId, id, x, y, 1.0f);
    1.67 +
    1.68 +            if (!preventDefault && SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
    1.69 +                preventDefault = 1;
    1.70 +            }
    1.71          } else {
    1.72              if ((window_data->finger_touching) && (window_data->first_finger == id)) {
    1.73                  SDL_SendMouseButton(window_data->window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
    1.74                  window_data->finger_touching = SDL_FALSE;
    1.75              }
    1.76              SDL_SendTouch(deviceId, id, SDL_FALSE, x, y, 1.0f);
    1.77 +
    1.78 +            if (!preventDefault && SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
    1.79 +                preventDefault = 1;
    1.80 +            }
    1.81          }
    1.82      }
    1.83  
    1.84 -
    1.85 -    return 1;
    1.86 +    return preventDefault;
    1.87  }
    1.88  
    1.89  EM_BOOL
    1.90 @@ -479,16 +495,19 @@
    1.91                          break;
    1.92                  }
    1.93              }
    1.94 -            SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ?
    1.95 -                                SDL_PRESSED : SDL_RELEASED, scancode);
    1.96 +            SDL_SendKeyboardKey(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_PRESSED : SDL_RELEASED, scancode);
    1.97          }
    1.98      }
    1.99  
   1.100 -    /* if we prevent keydown, we won't get keypress
   1.101 -     * also we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
   1.102 +    SDL_bool prevent_default = SDL_GetEventState(eventType == EMSCRIPTEN_EVENT_KEYDOWN ? SDL_KEYDOWN : SDL_KEYUP) == SDL_ENABLE;
   1.103 +
   1.104 +    /* if TEXTINPUT events are enabled we can't prevent keydown or we won't get keypress
   1.105 +     * we need to ALWAYS prevent backspace and tab otherwise chrome takes action and does bad navigation UX
   1.106       */
   1.107 -    return SDL_GetEventState(SDL_TEXTINPUT) != SDL_ENABLE || eventType != EMSCRIPTEN_EVENT_KEYDOWN
   1.108 -            || keyEvent->keyCode == 8 /* backspace */ || keyEvent->keyCode == 9 /* tab */;
   1.109 +    if (eventType == EMSCRIPTEN_EVENT_KEYDOWN && SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE && keyEvent->keyCode != 8 /* backspace */ && keyEvent->keyCode != 9 /* tab */)
   1.110 +        prevent_default = SDL_FALSE;
   1.111 +
   1.112 +    return prevent_default;
   1.113  }
   1.114  
   1.115  EM_BOOL
   1.116 @@ -498,7 +517,7 @@
   1.117      if (Emscripten_ConvertUTF32toUTF8(keyEvent->charCode, text)) {
   1.118          SDL_SendKeyboardText(text);
   1.119      }
   1.120 -    return 1;
   1.121 +    return SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE;
   1.122  }
   1.123  
   1.124  EM_BOOL