Skip to content
Flecs v4.1
script_event.h
Go to the documentation of this file.
1/**
2 * @file addons/script_event.h
3 * @brief Mouse and keyboard events for Flecs script.
4 */
5
6#ifdef FLECS_SCRIPT_EVENT
7
8#ifndef FLECS_SCRIPT
9#define FLECS_SCRIPT
10#endif
11
12#ifndef FLECS_SCRIPT_ASYNC
13#define FLECS_SCRIPT_ASYNC
14#endif
15
16/**
17 * @defgroup c_addons_script_event Script Event
18 * @ingroup c_addons
19 * Mouse and keyboard events for Flecs script.
20 * @{
21 */
22
23#ifndef FLECS_SCRIPT_EVENT_H
24#define FLECS_SCRIPT_EVENT_H
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/** Mouse event.
31 * The application resolves the entity under the pointer, passes it as target
32 * and provides local_x and local_y relative to that entity. An event is
33 * delivered to its target only; when ecs_script_mouse_event() returns true the
34 * application propagates the event to the parent of the target by calling it
35 * again with the parent as target and coordinates relative to the parent. A
36 * propagated event keeps the same screen position and button state.
37 *
38 * The module derives the kind of event from the button state of the previous
39 * event: buttons that were not held produce press and drag, moving with a
40 * held button produces drag (delivered to the pressed entity and its parents),
41 * buttons that are no longer held produce release, and click when the target
42 * is still the pressed entity. Moving without buttons produces move. When the
43 * target changes, leave is delivered to the entities that are no longer under
44 * the pointer and enter to the entities that newly are, so that moving
45 * between children of an entity does not leave and enter the entity.
46 */
47typedef struct EcsScriptMouseEvent {
48 ecs_entity_t target; /**< Entity under the pointer, 0 if none. */
49
50 float screen_x; /**< X position in window coordinates. */
51 float screen_y; /**< Y position in window coordinates. */
52
53 float local_x; /**< X position relative to the target. */
54 float local_y; /**< Y position relative to the target. */
55
56 float delta_x; /**< Horizontal movement since the previous event. */
57 float delta_y; /**< Vertical movement since the previous event. */
58
59 int32_t button; /**< Button that changed state: 0=left, 1=middle, 2=right. */
60 uint32_t buttons; /**< Bitmask of buttons that are held down. */
61
62 bool ctrl; /**< Control key is held down. */
63 bool shift; /**< Shift key is held down. */
64 bool alt; /**< Alt/Option key is held down. */
65 bool meta; /**< Meta (Command/Windows) key is held down. */
66} EcsScriptMouseEvent;
67
68/** Keyboard event.
69 * Keyboard events are delivered to the entity that received the last mouse
70 * press and to its parents.
71 */
72typedef struct EcsScriptKeyboardEvent {
73 char *key; /**< Key value, for example "a", "Enter" or "ArrowLeft". Not owned. */
74 bool down; /**< True when the key was pressed, false when released. */
75 bool repeat; /**< Event was generated by key auto-repeat. */
76
77 bool ctrl; /**< Ctrl key is held down. */
78 bool shift; /**< Shift key is held down. */
79 bool alt; /**< Alt/Option key is held down. */
80 bool meta; /**< Meta (Command/Windows) key is held down. */
81} EcsScriptKeyboardEvent;
82
83FLECS_API
84extern ECS_COMPONENT_DECLARE(EcsScriptMouseEvent);
85
86FLECS_API
87extern ECS_COMPONENT_DECLARE(EcsScriptKeyboardEvent);
88
89/** Emit mouse event.
90 * Resolves the on.press, on.drag, on.release, on.click, on.move, on.enter and
91 * on.leave calls that wait for the event on its target and resumes the async
92 * blocks that became ready. When delta_x and delta_y are both zero they are
93 * computed from the previous event.
94 *
95 * @param world The world.
96 * @param evt The event.
97 * @return Whether the event should be propagated to the parent of the target.
98 */
99FLECS_API
100bool ecs_script_mouse_event(
101 ecs_world_t *world,
102 const EcsScriptMouseEvent *evt);
103
104/** Emit keyboard event.
105 * Resolves the on.key_down and on.key_up calls that wait for the event and
106 * resumes the async blocks that became ready.
107 *
108 * @param world The world.
109 * @param evt The event.
110 */
111FLECS_API
112void ecs_script_keyboard_event(
113 ecs_world_t *world,
114 const EcsScriptKeyboardEvent *evt);
115
116/** Script event import function.
117 * Usage:
118 * @code
119 * ECS_IMPORT(world, FlecsScriptEvent)
120 * @endcode
121 *
122 * @param world The world.
123 */
124FLECS_API
125void FlecsScriptEventImport(
126 ecs_world_t *world);
127
128#ifdef __cplusplus
129}
130#endif
131
132#endif
133
134/** @} */
135
136#endif
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:65