+void HandleMotionEvent(MotionEvent *event)
+{
+ if (button_status == MB_RELEASED && game_status != GAME_MODE_EDITOR)
+ return;
+
+ motion_status = TRUE;
+
+#if DEBUG_EVENTS_MOTION
+ Error(ERR_DEBUG, "MOTION EVENT: button %d moved, x/y %d/%d\n",
+ button_status, event->x, event->y);
+#endif
+
+ HandleButton(event->x, event->y, button_status, button_status);
+}
+
+void HandleWheelEvent(WheelEvent *event)
+{
+ int button_nr;
+
+#if DEBUG_EVENTS_WHEEL
+#if 1
+ Error(ERR_DEBUG, "WHEEL EVENT: mouse == %d, x/y == %d/%d\n",
+ event->which, event->x, event->y);
+#else
+ // (SDL_MOUSEWHEEL_NORMAL/SDL_MOUSEWHEEL_FLIPPED needs SDL 2.0.4 or newer)
+ Error(ERR_DEBUG, "WHEEL EVENT: mouse == %d, x/y == %d/%d, direction == %s\n",
+ event->which, event->x, event->y,
+ (event->direction == SDL_MOUSEWHEEL_NORMAL ? "SDL_MOUSEWHEEL_NORMAL" :
+ "SDL_MOUSEWHEEL_FLIPPED"));
+#endif
+#endif
+
+ button_nr = (event->x < 0 ? MB_WHEEL_LEFT :
+ event->x > 0 ? MB_WHEEL_RIGHT :
+ event->y < 0 ? MB_WHEEL_DOWN :
+ event->y > 0 ? MB_WHEEL_UP : 0);
+
+#if defined(PLATFORM_WIN32) || defined(PLATFORM_MACOSX)
+ // accelerated mouse wheel available on Mac and Windows
+ wheel_steps = (event->x ? ABS(event->x) : ABS(event->y));
+#else
+ // no accelerated mouse wheel available on Unix/Linux
+ wheel_steps = DEFAULT_WHEEL_STEPS;
+#endif
+
+ motion_status = FALSE;
+
+ button_status = button_nr;
+ HandleButton(0, 0, button_status, -button_nr);
+
+ button_status = MB_RELEASED;
+ HandleButton(0, 0, button_status, -button_nr);
+}
+
+void HandleWindowEvent(WindowEvent *event)
+{
+#if DEBUG_EVENTS_WINDOW
+ int subtype = event->event;
+
+ char *event_name =
+ (subtype == SDL_WINDOWEVENT_SHOWN ? "SDL_WINDOWEVENT_SHOWN" :
+ subtype == SDL_WINDOWEVENT_HIDDEN ? "SDL_WINDOWEVENT_HIDDEN" :
+ subtype == SDL_WINDOWEVENT_EXPOSED ? "SDL_WINDOWEVENT_EXPOSED" :
+ subtype == SDL_WINDOWEVENT_MOVED ? "SDL_WINDOWEVENT_MOVED" :
+ subtype == SDL_WINDOWEVENT_SIZE_CHANGED ? "SDL_WINDOWEVENT_SIZE_CHANGED" :
+ subtype == SDL_WINDOWEVENT_RESIZED ? "SDL_WINDOWEVENT_RESIZED" :
+ subtype == SDL_WINDOWEVENT_MINIMIZED ? "SDL_WINDOWEVENT_MINIMIZED" :
+ subtype == SDL_WINDOWEVENT_MAXIMIZED ? "SDL_WINDOWEVENT_MAXIMIZED" :
+ subtype == SDL_WINDOWEVENT_RESTORED ? "SDL_WINDOWEVENT_RESTORED" :
+ subtype == SDL_WINDOWEVENT_ENTER ? "SDL_WINDOWEVENT_ENTER" :
+ subtype == SDL_WINDOWEVENT_LEAVE ? "SDL_WINDOWEVENT_LEAVE" :
+ subtype == SDL_WINDOWEVENT_FOCUS_GAINED ? "SDL_WINDOWEVENT_FOCUS_GAINED" :
+ subtype == SDL_WINDOWEVENT_FOCUS_LOST ? "SDL_WINDOWEVENT_FOCUS_LOST" :
+ subtype == SDL_WINDOWEVENT_CLOSE ? "SDL_WINDOWEVENT_CLOSE" :
+ "(UNKNOWN)");
+
+ Error(ERR_DEBUG, "WINDOW EVENT: '%s', %ld, %ld",
+ event_name, event->data1, event->data2);
+#endif
+
+#if 0
+ // (not needed, as the screen gets redrawn every 20 ms anyway)
+ if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
+ event->event == SDL_WINDOWEVENT_RESIZED ||
+ event->event == SDL_WINDOWEVENT_EXPOSED)
+ SDLRedrawWindow();
+#endif
+
+ if (event->event == SDL_WINDOWEVENT_RESIZED)
+ {
+ if (!video.fullscreen_enabled)
+ {
+ int new_window_width = event->data1;
+ int new_window_height = event->data2;
+
+ // if window size has changed after resizing, calculate new scaling factor
+ if (new_window_width != video.window_width ||
+ new_window_height != video.window_height)
+ {
+ int new_xpercent = 100.0 * new_window_width / video.screen_width + .5;
+ int new_ypercent = 100.0 * new_window_height / video.screen_height + .5;
+
+ // (extreme window scaling allowed, but cannot be saved permanently)
+ video.window_scaling_percent = MIN(new_xpercent, new_ypercent);
+ setup.window_scaling_percent =
+ MIN(MAX(MIN_WINDOW_SCALING_PERCENT, video.window_scaling_percent),
+ MAX_WINDOW_SCALING_PERCENT);
+
+ video.window_width = new_window_width;
+ video.window_height = new_window_height;
+
+ if (game_status == GAME_MODE_SETUP)
+ RedrawSetupScreenAfterFullscreenToggle();
+
+ UpdateMousePosition();
+
+ SetWindowTitle();
+ }
+ }
+#if defined(PLATFORM_ANDROID)
+ else
+ {
+ int new_display_width = event->data1;
+ int new_display_height = event->data2;
+
+ // if fullscreen display size has changed, device has been rotated
+ if (new_display_width != video.display_width ||
+ new_display_height != video.display_height)
+ {
+ int nr = GRID_ACTIVE_NR(); // previous screen orientation
+
+ video.display_width = new_display_width;
+ video.display_height = new_display_height;
+
+ SDLSetScreenProperties();
+ SetGadgetsPosition_OverlayTouchButtons();
+
+ // check if screen orientation has changed (should always be true here)
+ if (nr != GRID_ACTIVE_NR())
+ {
+ int x, y;
+
+ if (game_status == GAME_MODE_SETUP)
+ RedrawSetupScreenAfterScreenRotation(nr);
+
+ nr = GRID_ACTIVE_NR();
+
+ overlay.grid_xsize = setup.touch.grid_xsize[nr];
+ overlay.grid_ysize = setup.touch.grid_ysize[nr];
+
+ for (x = 0; x < MAX_GRID_XSIZE; x++)
+ for (y = 0; y < MAX_GRID_YSIZE; y++)
+ overlay.grid_button[x][y] = setup.touch.grid_button[nr][x][y];
+ }
+ }
+ }
+#endif
+ }
+}
+
+#define NUM_TOUCH_FINGERS 3
+
+static struct
+{
+ boolean touched;
+ SDL_FingerID finger_id;
+ int counter;
+ Key key;
+ byte action;
+} touch_info[NUM_TOUCH_FINGERS];
+
+static void HandleFingerEvent_VirtualButtons(FingerEvent *event)
+{
+ int x = event->x * overlay.grid_xsize;
+ int y = event->y * overlay.grid_ysize;
+ int grid_button = overlay.grid_button[x][y];
+ int grid_button_action = GET_ACTION_FROM_GRID_BUTTON(grid_button);
+ Key key = GetKeyFromGridButton(grid_button);
+ int key_status = (event->type == EVENT_FINGERRELEASE ? KEY_RELEASED :
+ KEY_PRESSED);
+ char *key_status_name = (key_status == KEY_RELEASED ? "KEY_RELEASED" :
+ "KEY_PRESSED");
+ int i;
+
+ // for any touch input event, enable overlay buttons (if activated)
+ SetOverlayEnabled(TRUE);
+
+ Error(ERR_DEBUG, "::: key '%s' was '%s' [fingerId: %lld]",
+ getKeyNameFromKey(key), key_status_name, event->fingerId);
+
+ if (key_status == KEY_PRESSED)
+ overlay.grid_button_action |= grid_button_action;
+ else
+ overlay.grid_button_action &= ~grid_button_action;
+
+ // check if we already know this touch event's finger id
+ for (i = 0; i < NUM_TOUCH_FINGERS; i++)
+ {
+ if (touch_info[i].touched &&
+ touch_info[i].finger_id == event->fingerId)
+ {
+ // Error(ERR_DEBUG, "MARK 1: %d", i);
+
+ break;
+ }
+ }
+
+ if (i >= NUM_TOUCH_FINGERS)
+ {
+ if (key_status == KEY_PRESSED)
+ {
+ int oldest_pos = 0, oldest_counter = touch_info[0].counter;
+
+ // unknown finger id -- get new, empty slot, if available
+ for (i = 0; i < NUM_TOUCH_FINGERS; i++)
+ {
+ if (touch_info[i].counter < oldest_counter)
+ {
+ oldest_pos = i;
+ oldest_counter = touch_info[i].counter;
+
+ // Error(ERR_DEBUG, "MARK 2: %d", i);
+ }
+
+ if (!touch_info[i].touched)
+ {
+ // Error(ERR_DEBUG, "MARK 3: %d", i);
+
+ break;
+ }
+ }
+
+ if (i >= NUM_TOUCH_FINGERS)
+ {
+ // all slots allocated -- use oldest slot
+ i = oldest_pos;
+
+ // Error(ERR_DEBUG, "MARK 4: %d", i);
+ }
+ }
+ else
+ {
+ // release of previously unknown key (should not happen)
+
+ if (key != KSYM_UNDEFINED)
+ {
+ HandleKey(key, KEY_RELEASED);
+
+ Error(ERR_DEBUG, "=> key == '%s', key_status == '%s' [slot %d] [1]",
+ getKeyNameFromKey(key), "KEY_RELEASED", i);
+ }
+ }
+ }
+
+ if (i < NUM_TOUCH_FINGERS)
+ {
+ if (key_status == KEY_PRESSED)
+ {
+ if (touch_info[i].key != key)
+ {
+ if (touch_info[i].key != KSYM_UNDEFINED)
+ {
+ HandleKey(touch_info[i].key, KEY_RELEASED);
+
+ // undraw previous grid button when moving finger away
+ overlay.grid_button_action &= ~touch_info[i].action;
+
+ Error(ERR_DEBUG, "=> key == '%s', key_status == '%s' [slot %d] [2]",
+ getKeyNameFromKey(touch_info[i].key), "KEY_RELEASED", i);
+ }
+
+ if (key != KSYM_UNDEFINED)
+ {
+ HandleKey(key, KEY_PRESSED);
+
+ Error(ERR_DEBUG, "=> key == '%s', key_status == '%s' [slot %d] [3]",
+ getKeyNameFromKey(key), "KEY_PRESSED", i);
+ }
+ }
+
+ touch_info[i].touched = TRUE;
+ touch_info[i].finger_id = event->fingerId;
+ touch_info[i].counter = Counter();
+ touch_info[i].key = key;
+ touch_info[i].action = grid_button_action;
+ }
+ else
+ {
+ if (touch_info[i].key != KSYM_UNDEFINED)
+ {
+ HandleKey(touch_info[i].key, KEY_RELEASED);
+
+ Error(ERR_DEBUG, "=> key == '%s', key_status == '%s' [slot %d] [4]",
+ getKeyNameFromKey(touch_info[i].key), "KEY_RELEASED", i);
+ }
+
+ touch_info[i].touched = FALSE;
+ touch_info[i].finger_id = 0;
+ touch_info[i].counter = 0;
+ touch_info[i].key = 0;
+ touch_info[i].action = JOY_NO_ACTION;
+ }
+ }
+}
+
+static void HandleFingerEvent_WipeGestures(FingerEvent *event)
+{
+ static Key motion_key_x = KSYM_UNDEFINED;
+ static Key motion_key_y = KSYM_UNDEFINED;
+ static Key button_key = KSYM_UNDEFINED;
+ static float motion_x1, motion_y1;
+ static float button_x1, button_y1;
+ static SDL_FingerID motion_id = -1;
+ static SDL_FingerID button_id = -1;
+ int move_trigger_distance_percent = setup.touch.move_distance;
+ int drop_trigger_distance_percent = setup.touch.drop_distance;
+ float move_trigger_distance = (float)move_trigger_distance_percent / 100;
+ float drop_trigger_distance = (float)drop_trigger_distance_percent / 100;
+ float event_x = event->x;
+ float event_y = event->y;
+
+ if (event->type == EVENT_FINGERPRESS)
+ {
+ if (event_x > 1.0 / 3.0)
+ {
+ // motion area
+
+ motion_id = event->fingerId;
+
+ motion_x1 = event_x;
+ motion_y1 = event_y;
+
+ motion_key_x = KSYM_UNDEFINED;
+ motion_key_y = KSYM_UNDEFINED;
+
+ Error(ERR_DEBUG, "---------- MOVE STARTED (WAIT) ----------");
+ }
+ else
+ {
+ // button area
+
+ button_id = event->fingerId;
+
+ button_x1 = event_x;
+ button_y1 = event_y;
+
+ button_key = setup.input[0].key.snap;
+
+ HandleKey(button_key, KEY_PRESSED);
+
+ Error(ERR_DEBUG, "---------- SNAP STARTED ----------");
+ }
+ }
+ else if (event->type == EVENT_FINGERRELEASE)
+ {
+ if (event->fingerId == motion_id)
+ {
+ motion_id = -1;
+
+ if (motion_key_x != KSYM_UNDEFINED)
+ HandleKey(motion_key_x, KEY_RELEASED);
+ if (motion_key_y != KSYM_UNDEFINED)
+ HandleKey(motion_key_y, KEY_RELEASED);
+
+ motion_key_x = KSYM_UNDEFINED;
+ motion_key_y = KSYM_UNDEFINED;
+
+ Error(ERR_DEBUG, "---------- MOVE STOPPED ----------");
+ }
+ else if (event->fingerId == button_id)
+ {
+ button_id = -1;
+
+ if (button_key != KSYM_UNDEFINED)
+ HandleKey(button_key, KEY_RELEASED);
+
+ button_key = KSYM_UNDEFINED;
+
+ Error(ERR_DEBUG, "---------- SNAP STOPPED ----------");
+ }
+ }
+ else if (event->type == EVENT_FINGERMOTION)
+ {
+ if (event->fingerId == motion_id)
+ {
+ float distance_x = ABS(event_x - motion_x1);
+ float distance_y = ABS(event_y - motion_y1);
+ Key new_motion_key_x = (event_x < motion_x1 ? setup.input[0].key.left :
+ event_x > motion_x1 ? setup.input[0].key.right :
+ KSYM_UNDEFINED);
+ Key new_motion_key_y = (event_y < motion_y1 ? setup.input[0].key.up :
+ event_y > motion_y1 ? setup.input[0].key.down :
+ KSYM_UNDEFINED);
+
+ if (distance_x < move_trigger_distance / 2 ||
+ distance_x < distance_y)
+ new_motion_key_x = KSYM_UNDEFINED;
+
+ if (distance_y < move_trigger_distance / 2 ||
+ distance_y < distance_x)
+ new_motion_key_y = KSYM_UNDEFINED;
+
+ if (distance_x > move_trigger_distance ||
+ distance_y > move_trigger_distance)
+ {
+ if (new_motion_key_x != motion_key_x)
+ {
+ if (motion_key_x != KSYM_UNDEFINED)
+ HandleKey(motion_key_x, KEY_RELEASED);
+ if (new_motion_key_x != KSYM_UNDEFINED)
+ HandleKey(new_motion_key_x, KEY_PRESSED);
+ }
+
+ if (new_motion_key_y != motion_key_y)
+ {
+ if (motion_key_y != KSYM_UNDEFINED)
+ HandleKey(motion_key_y, KEY_RELEASED);
+ if (new_motion_key_y != KSYM_UNDEFINED)
+ HandleKey(new_motion_key_y, KEY_PRESSED);
+ }
+
+ motion_x1 = event_x;
+ motion_y1 = event_y;
+
+ motion_key_x = new_motion_key_x;
+ motion_key_y = new_motion_key_y;
+
+ Error(ERR_DEBUG, "---------- MOVE STARTED (MOVE) ----------");
+ }
+ }
+ else if (event->fingerId == button_id)
+ {
+ float distance_x = ABS(event_x - button_x1);
+ float distance_y = ABS(event_y - button_y1);
+
+ if (distance_x < drop_trigger_distance / 2 &&
+ distance_y > drop_trigger_distance)
+ {
+ if (button_key == setup.input[0].key.snap)
+ HandleKey(button_key, KEY_RELEASED);
+
+ button_x1 = event_x;
+ button_y1 = event_y;
+
+ button_key = setup.input[0].key.drop;
+
+ HandleKey(button_key, KEY_PRESSED);
+
+ Error(ERR_DEBUG, "---------- DROP STARTED ----------");
+ }
+ }
+ }
+}
+
+void HandleFingerEvent(FingerEvent *event)
+{
+#if DEBUG_EVENTS_FINGER
+ Error(ERR_DEBUG, "FINGER EVENT: finger was %s, touch ID %lld, finger ID %lld, x/y %f/%f, dx/dy %f/%f, pressure %f",
+ event->type == EVENT_FINGERPRESS ? "pressed" :
+ event->type == EVENT_FINGERRELEASE ? "released" : "moved",
+ event->touchId,
+ event->fingerId,
+ event->x, event->y,
+ event->dx, event->dy,
+ event->pressure);
+#endif
+
+ runtime.uses_touch_device = TRUE;
+
+ if (game_status != GAME_MODE_PLAYING)
+ return;
+
+ if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
+ {
+ if (strEqual(setup.touch.control_type, TOUCH_CONTROL_OFF))
+ local_player->mouse_action.button_hint =
+ (event->type == EVENT_FINGERRELEASE ? MB_NOT_PRESSED :
+ event->x < 0.5 ? MB_LEFTBUTTON :
+ event->x > 0.5 ? MB_RIGHTBUTTON :
+ MB_NOT_PRESSED);
+
+ return;
+ }
+
+ if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
+ HandleFingerEvent_VirtualButtons(event);
+ else if (strEqual(setup.touch.control_type, TOUCH_CONTROL_WIPE_GESTURES))
+ HandleFingerEvent_WipeGestures(event);
+}
+
+static void HandleButtonOrFinger_WipeGestures_MM(int mx, int my, int button)
+{
+ static int old_mx = 0, old_my = 0;
+ static int last_button = MB_LEFTBUTTON;
+ static boolean touched = FALSE;
+ static boolean tapped = FALSE;
+
+ // screen tile was tapped (but finger not touching the screen anymore)
+ // (this point will also be reached without receiving a touch event)
+ if (tapped && !touched)
+ {
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ tapped = FALSE;
+ }
+
+ // stop here if this function was not triggered by a touch event
+ if (button == -1)
+ return;
+
+ if (button == MB_PRESSED && IN_GFX_FIELD_PLAY(mx, my))
+ {
+ // finger started touching the screen
+
+ touched = TRUE;
+ tapped = TRUE;
+
+ if (!motion_status)
+ {
+ old_mx = mx;
+ old_my = my;
+
+ ClearPlayerMouseAction();
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STARTED ----------");
+ }
+ }
+ else if (button == MB_RELEASED && touched)
+ {
+ // finger stopped touching the screen
+
+ touched = FALSE;
+
+ if (tapped)
+ SetPlayerMouseAction(old_mx, old_my, last_button);
+ else
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STOPPED ----------");
+ }
+
+ if (touched)
+ {
+ // finger moved while touching the screen
+
+ int old_x = getLevelFromScreenX(old_mx);
+ int old_y = getLevelFromScreenY(old_my);
+ int new_x = getLevelFromScreenX(mx);
+ int new_y = getLevelFromScreenY(my);
+
+ if (new_x != old_x || new_y != old_y)
+ tapped = FALSE;
+
+ if (new_x != old_x)
+ {
+ // finger moved left or right from (horizontal) starting position
+
+ int button_nr = (new_x < old_x ? MB_LEFTBUTTON : MB_RIGHTBUTTON);
+
+ SetPlayerMouseAction(old_mx, old_my, button_nr);
+
+ last_button = button_nr;
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION: ROTATING ----------");
+ }
+ else
+ {
+ // finger stays at or returned to (horizontal) starting position
+
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION PAUSED ----------");
+ }
+ }
+}
+
+static void HandleButtonOrFinger_FollowFinger_MM(int mx, int my, int button)
+{
+ static int old_mx = 0, old_my = 0;
+ static int last_button = MB_LEFTBUTTON;
+ static boolean touched = FALSE;
+ static boolean tapped = FALSE;
+
+ // screen tile was tapped (but finger not touching the screen anymore)
+ // (this point will also be reached without receiving a touch event)
+ if (tapped && !touched)
+ {
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ tapped = FALSE;
+ }
+
+ // stop here if this function was not triggered by a touch event
+ if (button == -1)
+ return;
+
+ if (button == MB_PRESSED && IN_GFX_FIELD_PLAY(mx, my))
+ {
+ // finger started touching the screen
+
+ touched = TRUE;
+ tapped = TRUE;
+
+ if (!motion_status)
+ {
+ old_mx = mx;
+ old_my = my;
+
+ ClearPlayerMouseAction();
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STARTED ----------");
+ }
+ }
+ else if (button == MB_RELEASED && touched)
+ {
+ // finger stopped touching the screen
+
+ touched = FALSE;
+
+ if (tapped)
+ SetPlayerMouseAction(old_mx, old_my, last_button);
+ else
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STOPPED ----------");
+ }
+
+ if (touched)
+ {
+ // finger moved while touching the screen
+
+ int old_x = getLevelFromScreenX(old_mx);
+ int old_y = getLevelFromScreenY(old_my);
+ int new_x = getLevelFromScreenX(mx);
+ int new_y = getLevelFromScreenY(my);
+
+ if (new_x != old_x || new_y != old_y)
+ {
+ // finger moved away from starting position
+
+ int button_nr = getButtonFromTouchPosition(old_x, old_y, mx, my);
+
+ // quickly alternate between clicking and releasing for maximum speed
+ if (FrameCounter % 2 == 0)
+ button_nr = MB_RELEASED;
+
+ SetPlayerMouseAction(old_mx, old_my, button_nr);
+
+ if (button_nr)
+ last_button = button_nr;
+
+ tapped = FALSE;
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION: ROTATING ----------");
+ }
+ else
+ {
+ // finger stays at or returned to starting position
+
+ SetPlayerMouseAction(old_mx, old_my, MB_RELEASED);
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION PAUSED ----------");
+ }
+ }
+}
+
+static void HandleButtonOrFinger_FollowFinger(int mx, int my, int button)
+{
+ static int old_mx = 0, old_my = 0;
+ static Key motion_key_x = KSYM_UNDEFINED;
+ static Key motion_key_y = KSYM_UNDEFINED;
+ static boolean touched = FALSE;
+ static boolean started_on_player = FALSE;
+ static boolean player_is_dropping = FALSE;
+ static int player_drop_count = 0;
+ static int last_player_x = -1;
+ static int last_player_y = -1;
+
+ if (button == MB_PRESSED && IN_GFX_FIELD_PLAY(mx, my))
+ {
+ touched = TRUE;
+
+ old_mx = mx;
+ old_my = my;
+
+ if (!motion_status)
+ {
+ started_on_player = FALSE;
+ player_is_dropping = FALSE;
+ player_drop_count = 0;
+ last_player_x = -1;
+ last_player_y = -1;
+
+ motion_key_x = KSYM_UNDEFINED;
+ motion_key_y = KSYM_UNDEFINED;
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STARTED ----------");
+ }
+ }
+ else if (button == MB_RELEASED && touched)
+ {
+ touched = FALSE;
+
+ old_mx = 0;
+ old_my = 0;
+
+ if (motion_key_x != KSYM_UNDEFINED)
+ HandleKey(motion_key_x, KEY_RELEASED);
+ if (motion_key_y != KSYM_UNDEFINED)
+ HandleKey(motion_key_y, KEY_RELEASED);
+
+ if (started_on_player)
+ {
+ if (player_is_dropping)
+ {
+ Error(ERR_DEBUG, "---------- DROP STOPPED ----------");
+
+ HandleKey(setup.input[0].key.drop, KEY_RELEASED);
+ }
+ else
+ {
+ Error(ERR_DEBUG, "---------- SNAP STOPPED ----------");
+
+ HandleKey(setup.input[0].key.snap, KEY_RELEASED);
+ }
+ }
+
+ motion_key_x = KSYM_UNDEFINED;
+ motion_key_y = KSYM_UNDEFINED;
+
+ Error(ERR_DEBUG, "---------- TOUCH ACTION STOPPED ----------");
+ }
+
+ if (touched)
+ {
+ int src_x = local_player->jx;
+ int src_y = local_player->jy;
+ int dst_x = getLevelFromScreenX(old_mx);
+ int dst_y = getLevelFromScreenY(old_my);
+ int dx = dst_x - src_x;
+ int dy = dst_y - src_y;
+ Key new_motion_key_x = (dx < 0 ? setup.input[0].key.left :
+ dx > 0 ? setup.input[0].key.right :
+ KSYM_UNDEFINED);
+ Key new_motion_key_y = (dy < 0 ? setup.input[0].key.up :
+ dy > 0 ? setup.input[0].key.down :
+ KSYM_UNDEFINED);
+
+ if (dx != 0 && dy != 0 && ABS(dx) != ABS(dy) &&
+ (last_player_x != local_player->jx ||
+ last_player_y != local_player->jy))
+ {
+ // in case of asymmetric diagonal movement, use "preferred" direction
+
+ int last_move_dir = (ABS(dx) > ABS(dy) ? MV_VERTICAL : MV_HORIZONTAL);
+
+ if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+ game_em.ply[0]->last_move_dir = last_move_dir;
+ else
+ local_player->last_move_dir = last_move_dir;
+
+ // (required to prevent accidentally forcing direction for next movement)
+ last_player_x = local_player->jx;
+ last_player_y = local_player->jy;
+ }
+
+ if (button == MB_PRESSED && !motion_status && dx == 0 && dy == 0)