Skip to content

Commit

Permalink
forward SDL_TouchFingerEvent to window(touch_event_t)
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Nov 28, 2024
1 parent 44e62a8 commit 991ee18
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
2024/11/28
* 增加函数 bitmap\_set\_dirty/bitmap\_is\_dirty
* 重构bitmap,用bitmap\_init\_ex 实现bitmap\_create\_ex
* 将SDL_TouchFingerEvent转发到top windows

2024/11/26
* 增加tab\_button\_group删除tab\_button和对应page的函数(感谢智明提供补丁)
Expand Down
26 changes: 26 additions & 0 deletions src/base/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,32 @@ event_t* system_event_init(system_event_t* event, void* target, void* sdl_event)
return (event_t*)event;
}

touch_event_t* touch_event_cast(event_t* event) {
return_value_if_fail(event != NULL, NULL);
return_value_if_fail(
event->type == EVT_TOUCH_DOWN || event->type == EVT_TOUCH_UP || event->type == EVT_TOUCH_MOVE,
NULL);
return_value_if_fail(event->size == sizeof(touch_event_t), NULL);

return (touch_event_t*)event;
}

event_t* touch_event_init(touch_event_t* event, uint32_t type, void* target, int64_t touch_id,
int64_t finger_id, float x, float y, float pressure) {
return_value_if_fail(event != NULL, NULL);
memset(event, 0x00, sizeof(touch_event_t));

event->e = event_init(type, target);
event->e.size = sizeof(*event);
event->touch_id = touch_id;
event->finger_id = finger_id;
event->x = x;
event->y = y;
event->pressure = pressure;

return (event_t*)event;
}

ui_load_event_t* ui_load_event_cast(event_t* event) {
return_value_if_fail(event != NULL, NULL);
return_value_if_fail(event->type == EVT_UI_LOAD, NULL);
Expand Down
84 changes: 84 additions & 0 deletions src/base/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,21 @@ typedef enum _event_type_t {
* UI加载完成事件(event_t)。
*/
EVT_UI_LOAD,
/**
* @const EVT_TOUCH_DOWN
* 触摸按下事件名(touch_event_t)。
*/
EVT_TOUCH_DOWN,
/**
* @const EVT_TOUCH_MOVE
* 触摸移动事件名(touch_event_t)。
*/
EVT_TOUCH_MOVE,
/**
* @const EVT_TOUCH_UP
* 触摸抬起事件名(touch_event_t)。
*/
EVT_TOUCH_UP,
/**
* @const EVT_REQ_START
* event queue其它请求编号起始值。
Expand Down Expand Up @@ -1309,6 +1324,75 @@ system_event_t* system_event_cast(event_t* event);
*/
event_t* system_event_init(system_event_t* event, void* target, void* sdl_event);

/**
* @class touch_event_t
* @annotation ["scriptable"]
* @parent event_t
* 多点触摸事件(目前主要对接 SDL_TouchFingerEvent(SDL_FINGERMOTION/SDL_FINGERDOWN/SDL_FINGERUP))。
*/
typedef struct _touch_event_t {
event_t e;

/**
* @property {int64_t} touch_id
* @annotation ["readable", "scriptable"]
* 触摸ID。
*/
int64_t touch_id;
/**
* @property {int64_t} finger_id
* @annotation ["readable", "scriptable"]
* 手指ID。
*/
int64_t finger_id;
/**
* @property {float} x
* @annotation ["readable", "scriptable"]
* x坐标。
*/
float x;
/**
* @property {float} y
* @annotation ["readable", "scriptable"]
* y坐标。
*/
float y;
/**
* @property {float} pressure
* @annotation ["readable", "scriptable"]
* 压力。
*/
float pressure;

} touch_event_t;

/**
* @method touch_event_cast
* @annotation ["cast", "scriptable"]
* 把event对象转touch_event_t对象。
* @param {event_t*} event event对象。
*
* @return {touch_event_t*} event 对象。
*/
touch_event_t* touch_event_cast(event_t* event);

/**
* @method touch_event_init
* 初始化事件。
* @param {touch_event_t*} event event对象。
* @param {uint32_t} type 事件类型。
* @param {void*} target 事件目标。
* @param {int64_t} touch_id 触摸ID。
* @param {int64_t} finger_id 手指ID。
* @param {float} x x坐标。
* @param {float} y y坐标。
* @param {float} pressure 压力。
*
* @return {event_t*} event对象。
*/
event_t* touch_event_init(touch_event_t* event, uint32_t type, void* target, int64_t touch_id,
int64_t finger_id, float x, float y, float pressure);

/**
* @class ui_load_event_t
* @annotation ["scriptable"]
Expand Down
47 changes: 46 additions & 1 deletion src/main_loop/main_loop_sdl.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* File: main_loop_sdl.c
* Author: AWTK Develop Team
* Brief: sdl2 implemented main_loop interface
Expand Down Expand Up @@ -118,6 +118,45 @@ static ret_t main_loop_sdl2_dispatch_multi_gesture_event(main_loop_simple_t* loo
return RET_OK;
}

static ret_t main_loop_sdl2_dispatch_touch_event(main_loop_simple_t* loop, SDL_Event* sdl_event) {
event_t* e = NULL;
touch_event_t event;
int type = EVT_TOUCH_DOWN;
widget_t* widget = loop->base.wm;
SDL_TouchFingerEvent* finger_event = (SDL_TouchFingerEvent*)sdl_event;

memset(&event, 0x00, sizeof(event));
switch (sdl_event->type) {
case SDL_FINGERDOWN: {
type = EVT_TOUCH_DOWN;
break;
}
case SDL_FINGERUP: {
type = EVT_TOUCH_UP;
break;
}
case SDL_FINGERMOTION: {
type = EVT_TOUCH_MOVE;
break;
}
default:
break;
}

e = touch_event_init(&event, type, widget, finger_event->touchId, finger_event->fingerId,
finger_event->x, finger_event->y, finger_event->pressure);

if (e != NULL) {
widget_t* win = window_manager_get_top_window(widget);
widget_dispatch(win, e);
}

log_debug("touch event: type=%d touch_id=% " PRId64 " finger_id=%" PRId64 " x=%f y=%f\n", type,
event.touch_id, event.finger_id, event.x, event.y);

return RET_OK;
}

static ret_t main_loop_sdl2_dispatch_mouse_event(main_loop_simple_t* loop, SDL_Event* sdl_event) {
key_event_t key_event;
pointer_event_t event;
Expand Down Expand Up @@ -317,6 +356,12 @@ ret_t main_loop_sdl2_dispatch(main_loop_simple_t* loop) {
ret = main_loop_sdl2_dispatch_mouse_event(loop, &event);
break;
}
case SDL_FINGERDOWN:
case SDL_FINGERUP:
case SDL_FINGERMOTION: {
ret = main_loop_sdl2_dispatch_touch_event(loop, &event);
break;
}
case SDL_TEXTINPUT: {
ret = main_loop_sdl2_dispatch_text_input(loop, &event);
break;
Expand Down

0 comments on commit 991ee18

Please sign in to comment.