From fe630e1f5989e11dbec67580941022cbf7430c6d Mon Sep 17 00:00:00 2001 From: Victor Sergienko Date: Wed, 25 Apr 2018 23:24:54 -0700 Subject: [PATCH] iOS: double-tap for context menu --- src/controller_base.cpp | 22 +++++++++++++++++++++- src/events.cpp | 8 ++++++-- src/sdl/userevent.hpp | 3 ++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/controller_base.cpp b/src/controller_base.cpp index b3c9a4e96dad..8d33eb97ca16 100644 --- a/src/controller_base.cpp +++ b/src/controller_base.cpp @@ -76,7 +76,10 @@ void controller_base::long_touch_callback(int x, int y) int threshold = get_mouse_handler_base().drag_threshold(); bool yes_actually_dragging = dx * dx + dy * dy >= threshold * threshold; - if(!yes_actually_dragging && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0) { + if(!yes_actually_dragging + && (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0 + && sdl::point_in_rect(x_now, y_now, get_display().map_area())) + { const theme::menu* const m = get_mouse_handler_base().gui().get_theme().context_menu(); if(m != nullptr) { show_menu(get_display().get_theme().context_menu()->items(), x_now, y_now, true, get_display()); @@ -200,6 +203,23 @@ void controller_base::handle_event(const SDL_Event& event) get_display()); } break; + case DOUBLE_CLICK_EVENT: + { + int x = static_cast(reinterpret_cast(event.user.data1)); + int y = static_cast(reinterpret_cast(event.user.data2)); + if(event.user.code == static_cast(SDL_TOUCH_MOUSEID) + // TODO: Move to right_click_show_menu? + && sdl::point_in_rect(x, y, get_display().map_area()) + // TODO: This chain repeats in several places, move to a method. + && get_display().get_theme().context_menu() != nullptr) { + show_menu(get_display().get_theme().context_menu()->items(), + x, + y, + true, + get_display()); + } + } + break; case SDL_FINGERUP: // handled by mouse case diff --git a/src/events.cpp b/src/events.cpp index 3f3474a20259..80a476db1797 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -617,15 +617,19 @@ void pump() case SDL_MOUSEBUTTONDOWN: { // Always make sure a cursor is displayed if the mouse moves or if the user clicks cursor::set_focus(true); - if(event.button.button == SDL_BUTTON_LEFT) { + if(event.button.button == SDL_BUTTON_LEFT || event.button.which == SDL_TOUCH_MOUSEID) { static const int DoubleClickTime = 500; +#ifdef __IPHONEOS__ + static const int DoubleClickMaxMove = 15; +#else static const int DoubleClickMaxMove = 3; +#endif if(last_mouse_down >= 0 && info.ticks() - last_mouse_down < DoubleClickTime && std::abs(event.button.x - last_click_x) < DoubleClickMaxMove && std::abs(event.button.y - last_click_y) < DoubleClickMaxMove ) { - sdl::UserEvent user_event(DOUBLE_CLICK_EVENT, event.button.x, event.button.y); + sdl::UserEvent user_event(DOUBLE_CLICK_EVENT, event.button.which, event.button.x, event.button.y); ::SDL_PushEvent(reinterpret_cast(&user_event)); } diff --git a/src/sdl/userevent.hpp b/src/sdl/userevent.hpp index cac6f78605f6..10953485b310 100644 --- a/src/sdl/userevent.hpp +++ b/src/sdl/userevent.hpp @@ -38,8 +38,9 @@ class UserEvent event_.code = code; } - UserEvent(int type, int data1, int data2) : UserEvent(type) + UserEvent(int type, int code, int data1, int data2) : UserEvent(type) { + event_.code = code; event_.data1 = reinterpret_cast(data1); event_.data2 = reinterpret_cast(data2); }