复制 typedef struct LCUI_WidgetEventRec_ {
uint32_t type;
void *data;
LCUI_Widget target;
LCUI_BOOL cancel_bubble;
union {
LCUI_WidgetMouseMotionEvent motion;
LCUI_WidgetMouseButtonEvent button;
LCUI_WidgetMouseWheelEvent wheel;
LCUI_WidgetKeyboardEvent key;
LCUI_WidgetTouchEvent touch;
LCUI_WidgetTextInputEvent text;
};
} LCUI_WidgetEventRec, *LCUI_WidgetEvent;
利用事件冒泡机制,我们可以实现事件委托,也就是将子组件的事件委托给父组件处理,这种做法适用于需要为大量组件设置事件处理器的场景,能避免因设置大量事件处理器而导致的性能降低和内存占用增加的问题。
复制 typedef struct LCUI_TouchPointRec_ {
int x;
int y;
int id;
int state;
LCUI_BOOL is_primary;
} LCUI_TouchPointRec, *LCUI_TouchPoint;
typedef struct LCUI_TouchEvent_ {
int n_points;
LCUI_TouchPoint points;
} LCUI_TouchEvent;
typedef LCUI_TouchEvent LCUI_WidgetTouchEvent;
复制 /** test_touch.c -- test touch support */
#include <stdio.h>
#include <stdlib.h>
#include <LCUI.h>
#include <LCUI/graph.h>
#include <LCUI/display.h>
#include <LCUI/gui/widget.h>
/** 触点绑定记录 */
typedef struct TouchPointBindingRec_ {
int point_id; /**< 触点 ID */
LCUI_Widget widget; /**< 组件 */
LinkedListNode node; /**< 在链表中的结点 */
LCUI_BOOL is_valid; /**< 是否有效 */
} TouchPointBindingRec, *TouchPointBinding;
/** 触点绑定记录列表 */
static LinkedList touch_bindings;
static void OnTouchWidget(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
{
LCUI_TouchPoint point;
TouchPointBinding binding;
if (e->touch.n_points == 0) {
return;
}
binding = e->data;
point = &e->touch.points[0];
switch (point->state) {
case LCUI_WEVENT_TOUCHMOVE:
Widget_Move(w, point->x - 32.0f, point->y - 32.0f);
break;
case LCUI_WEVENT_TOUCHUP:
if (!binding->is_valid) {
break;
}
/* 当触点释放后销毁组件及绑定记录 */
Widget_ReleaseTouchCapture(w, -1);
LinkedList_Unlink(&touch_bindings, &binding->node);
binding->is_valid = FALSE;
Widget_Destroy(w);
free(binding);
break;
case LCUI_WEVENT_TOUCHDOWN:
default:
break;
}
}
static void OnTouch(LCUI_SysEvent e, void *arg)
{
int i;
LCUI_Widget w;
LinkedListNode *node;
LCUI_TouchPoint point;
LCUI_Color bgcolor = RGB(255, 0, 0);
for (i = 0; i < e->touch.n_points; ++i) {
TouchPointBinding binding;
LCUI_BOOL is_existed = FALSE;
point = &e->touch.points[i];
_DEBUG_MSG("point: %d\n", point->id);
/* 检查该触点是否已经被绑定 */
for (LinkedList_Each(node, &touch_bindings)) {
binding = node->data;
if (binding->point_id == point->id) {
is_existed = TRUE;
}
}
if (is_existed) {
continue;
}
w = LCUIWidget_New(NULL);
/* 新建绑定记录 */
binding = NEW(TouchPointBindingRec, 1);
binding->point_id = point->id;
binding->node.data = binding;
binding->is_valid = TRUE;
binding->widget = w;
Widget_Resize(w, 64, 64);
Widget_Move(w, point->x - 32.0f, point->y - 32.0f);
/* 设置让该组件捕获当前触点 */
Widget_SetTouchCapture(w, binding->point_id);
Widget_BindEvent(w, "touch", OnTouchWidget, binding, NULL);
Widget_SetStyle(w, key_position, SV_ABSOLUTE, style);
Widget_SetStyle(w, key_background_color, bgcolor, color);
LinkedList_AppendNode(&touch_bindings, &binding->node);
Widget_Top(w);
}
}
int main(int argc, char **argv)
{
LCUI_Init();
LinkedList_Init(&touch_bindings);
LCUI_BindEvent(LCUI_TOUCH, OnTouch, NULL, NULL);
return LCUI_Main();
}