diff options
| author | bunnei <bunneidev@gmail.com> | 2018-10-09 19:02:41 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-10-09 19:02:41 -0400 | 
| commit | 0b3d4db98b5bc6f81a22e4618dbabbdafcec0ada (patch) | |
| tree | e3574b49f5a9e4c762e709ada72e0c1ba16e2fff /src/yuzu_cmd | |
| parent | fe16905de1433064ffcf601755d364f4c0438ea4 (diff) | |
| parent | 4f24343f32ed253efb2cedba03d20c620841dca1 (diff) | |
Merge pull request #1463 from FearlessTobi/port-4310
Port citra-emu/citra#4310: "Handle touch input"
Diffstat (limited to 'src/yuzu_cmd')
| -rw-r--r-- | src/yuzu_cmd/emu_window/emu_window_sdl2.cpp | 48 | ||||
| -rw-r--r-- | src/yuzu_cmd/emu_window/emu_window_sdl2.h | 12 | 
2 files changed, 58 insertions, 2 deletions
| diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp index 155095095..a9ad92a80 100644 --- a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp +++ b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp @@ -40,6 +40,35 @@ void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {      }  } +std::pair<unsigned, unsigned> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const { +    int w, h; +    SDL_GetWindowSize(render_window, &w, &h); + +    touch_x *= w; +    touch_y *= h; + +    return {static_cast<unsigned>(std::max(std::round(touch_x), 0.0f)), +            static_cast<unsigned>(std::max(std::round(touch_y), 0.0f))}; +} + +void EmuWindow_SDL2::OnFingerDown(float x, float y) { +    // TODO(NeatNit): keep track of multitouch using the fingerID and a dictionary of some kind +    // This isn't critical because the best we can do when we have that is to average them, like the +    // 3DS does + +    const auto [px, py] = TouchToPixelPos(x, y); +    TouchPressed(px, py); +} + +void EmuWindow_SDL2::OnFingerMotion(float x, float y) { +    const auto [px, py] = TouchToPixelPos(x, y); +    TouchMoved(px, py); +} + +void EmuWindow_SDL2::OnFingerUp() { +    TouchReleased(); +} +  void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {      if (state == SDL_PRESSED) {          InputCommon::GetKeyboard()->PressKey(key); @@ -219,11 +248,26 @@ void EmuWindow_SDL2::PollEvents() {              OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);              break;          case SDL_MOUSEMOTION: -            OnMouseMotion(event.motion.x, event.motion.y); +            // ignore if it came from touch +            if (event.button.which != SDL_TOUCH_MOUSEID) +                OnMouseMotion(event.motion.x, event.motion.y);              break;          case SDL_MOUSEBUTTONDOWN:          case SDL_MOUSEBUTTONUP: -            OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y); +            // ignore if it came from touch +            if (event.button.which != SDL_TOUCH_MOUSEID) { +                OnMouseButton(event.button.button, event.button.state, event.button.x, +                              event.button.y); +            } +            break; +        case SDL_FINGERDOWN: +            OnFingerDown(event.tfinger.x, event.tfinger.y); +            break; +        case SDL_FINGERMOTION: +            OnFingerMotion(event.tfinger.x, event.tfinger.y); +            break; +        case SDL_FINGERUP: +            OnFingerUp();              break;          case SDL_QUIT:              is_open = false; diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2.h b/src/yuzu_cmd/emu_window/emu_window_sdl2.h index d34902109..b0d4116cc 100644 --- a/src/yuzu_cmd/emu_window/emu_window_sdl2.h +++ b/src/yuzu_cmd/emu_window/emu_window_sdl2.h @@ -40,6 +40,18 @@ private:      /// Called by PollEvents when a mouse button is pressed or released      void OnMouseButton(u32 button, u8 state, s32 x, s32 y); +    /// Translates pixel position (0..1) to pixel positions +    std::pair<unsigned, unsigned> TouchToPixelPos(float touch_x, float touch_y) const; + +    /// Called by PollEvents when a finger starts touching the touchscreen +    void OnFingerDown(float x, float y); + +    /// Called by PollEvents when a finger moves while touching the touchscreen +    void OnFingerMotion(float x, float y); + +    /// Called by PollEvents when a finger stops touching the touchscreen +    void OnFingerUp(); +      /// Called by PollEvents when any event that may cause the window to be resized occurs      void OnResize(); | 
