Skip to content

Commit

Permalink
iOS: double-tap for context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
singalen authored and Vultraz committed Jul 8, 2018
1 parent 8f28f8d commit fe630e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/controller_base.cpp
Expand Up @@ -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());
Expand Down Expand Up @@ -200,6 +203,23 @@ void controller_base::handle_event(const SDL_Event& event)
get_display());
}
break;
case DOUBLE_CLICK_EVENT:
{
int x = static_cast<int>(reinterpret_cast<std::intptr_t>(event.user.data1));
int y = static_cast<int>(reinterpret_cast<std::intptr_t>(event.user.data2));
if(event.user.code == static_cast<int>(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
Expand Down
8 changes: 6 additions & 2 deletions src/events.cpp
Expand Up @@ -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<SDL_Event*>(&user_event));
}

Expand Down
3 changes: 2 additions & 1 deletion src/sdl/userevent.hpp
Expand Up @@ -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<void*>(data1);
event_.data2 = reinterpret_cast<void*>(data2);
}
Expand Down

0 comments on commit fe630e1

Please sign in to comment.