Send touch events from the client

On SDL touch events, send control messages to the server.
This commit is contained in:
Romain Vimont 2019-09-22 21:30:05 +02:00
parent f765aae352
commit b5a2d99bc2
5 changed files with 52 additions and 0 deletions

View file

@ -207,6 +207,34 @@ convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
return true; return true;
} }
static bool
convert_touch_action(SDL_EventType from, enum android_motionevent_action *to) {
switch (from) {
MAP(SDL_FINGERMOTION, AMOTION_EVENT_ACTION_MOVE);
MAP(SDL_FINGERDOWN, AMOTION_EVENT_ACTION_DOWN);
MAP(SDL_FINGERUP, AMOTION_EVENT_ACTION_UP);
FAIL;
}
}
bool
convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
struct control_msg *to) {
to->type = CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT;
if (!convert_touch_action(from->type, &to->inject_touch_event.action)) {
return false;
}
to->inject_touch_event.pointer_id = from->fingerId;
to->inject_touch_event.position.screen_size = screen_size;
// SDL touch event coordinates are normalized in the range [0; 1]
to->inject_touch_event.position.point.x = from->x * screen_size.width;
to->inject_touch_event.position.point.y = from->y * screen_size.height;
to->inject_touch_event.pressure = from->pressure;
return true;
}
bool bool
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position, convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,
struct control_msg *to) { struct control_msg *to) {

View file

@ -30,6 +30,10 @@ bool
convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size, convert_mouse_motion(const SDL_MouseMotionEvent *from, struct size screen_size,
struct control_msg *to); struct control_msg *to);
bool
convert_touch(const SDL_TouchFingerEvent *from, struct size screen_size,
struct control_msg *to);
// on Android, a scroll event requires the current mouse position // on Android, a scroll event requires the current mouse position
bool bool
convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position, convert_mouse_wheel(const SDL_MouseWheelEvent *from, struct position position,

View file

@ -397,6 +397,17 @@ input_manager_process_mouse_motion(struct input_manager *input_manager,
} }
} }
void
input_manager_process_touch(struct input_manager *input_manager,
const SDL_TouchFingerEvent *event) {
struct control_msg msg;
if (convert_touch(event, input_manager->screen->frame_size, &msg)) {
if (!controller_push_msg(input_manager->controller, &msg)) {
LOGW("Could not request 'inject touch event'");
}
}
}
static bool static bool
is_outside_device_screen(struct input_manager *input_manager, int x, int y) is_outside_device_screen(struct input_manager *input_manager, int x, int y)
{ {

View file

@ -29,6 +29,10 @@ void
input_manager_process_mouse_motion(struct input_manager *input_manager, input_manager_process_mouse_motion(struct input_manager *input_manager,
const SDL_MouseMotionEvent *event); const SDL_MouseMotionEvent *event);
void
input_manager_process_touch(struct input_manager *input_manager,
const SDL_TouchFingerEvent *event);
void void
input_manager_process_mouse_button(struct input_manager *input_manager, input_manager_process_mouse_button(struct input_manager *input_manager,
const SDL_MouseButtonEvent *event, const SDL_MouseButtonEvent *event,

View file

@ -181,6 +181,11 @@ handle_event(SDL_Event *event, bool control) {
input_manager_process_mouse_button(&input_manager, &event->button, input_manager_process_mouse_button(&input_manager, &event->button,
control); control);
break; break;
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
input_manager_process_touch(&input_manager, &event->tfinger);
break;
case SDL_DROPFILE: { case SDL_DROPFILE: {
if (!control) { if (!control) {
break; break;