1#include <events.h>
2#include <stdio.h>
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18typedef struct {
19 float x, y;
20} Position;
21
23
24const char *code =
25 "using flecs.script\n"
26 "\n"
27
28
29
30
31
32 "template Button {\n"
33 " Focusable\n"
34 " mut count = 0\n"
35 " async {\n"
36 " while true {\n"
37 " const e = await on.activate(this)\n"
38 " count = count + 1\n"
39 " if e.keyboard {\n"
40 " await print(\"{this.name()}: activated by key ({count})\")\n"
41 " } else {\n"
42 " await print(\"{this.name()}: activated by click ({count})\")\n"
43 " }\n"
44 " }\n"
45 " }\n"
46 "}\n"
47 "\n"
48
49
50
51
52 "template Slider {\n"
53 " Focusable\n"
54 " mut value = 50\n"
55 " async {\n"
56 " while true {\n"
57 " const e = await on.drag(this)\n"
58 " if e.delta_x != 0 {\n"
59 " value = value + e.delta_x\n"
60 " await print(\"{this.name()}: dragged to {value}\")\n"
61 " }\n"
62 " }\n"
63 " }\n"
64 " async {\n"
65 " while true {\n"
66 " const e = await on.key_down(this)\n"
67 " if e.key == \"ArrowRight\" {\n"
68 " value = value + 1\n"
69 " await print(\"{this.name()}: stepped to {value}\")\n"
70 " }\n"
71 " }\n"
72 " }\n"
73 "}\n"
74 "\n"
75
76
77
78
79 "template TextField {\n"
80 " Focusable\n"
81 " mut text = \"\"\n"
82 " async {\n"
83 " while true {\n"
84 " await on.focus(this)\n"
85 " await print(\"{this.name()}: focused\")\n"
86 " }\n"
87 " }\n"
88 " async {\n"
89 " while true {\n"
90 " await on.blur(this)\n"
91 " await print(\"{this.name()}: lost focus\")\n"
92 " }\n"
93 " }\n"
94 " async {\n"
95 " while true {\n"
96 " const e = await on.key_down(this)\n"
97 " if e.key != \"Escape\" {\n"
98 " text = \"{text}{e.key}\"\n"
99 " await print(\"{this.name()}: text is '{text}'\")\n"
100 " await e.stop_propagation()\n"
101 " }\n"
102 " }\n"
103 " }\n"
104 "}\n"
105 "\n"
106
107
108
109
110
111
112 "template Panel {\n"
113 " async {\n"
114 " while true {\n"
115 " const e = await on.press(this)\n"
116 " const x: i32 = e.local_x\n"
117 " const y: i32 = e.local_y\n"
118 " await print(\"{this.name()}: press at {x}, {y}\")\n"
119 " }\n"
120 " }\n"
121 " async {\n"
122 " while true {\n"
123 " const e = await on.focus(this)\n"
124 " await print(\"{this.name()}: focus moved to {e.target.name()}\")\n"
125 " }\n"
126 " }\n"
127 " async {\n"
128 " while true {\n"
129 " const e = await on.key_down(this)\n"
130 " if e.key == \"Escape\" {\n"
131 " await focus(0)\n"
132 " await print(\"{this.name()}: Escape cleared the focus\")\n"
133 " } else {\n"
134 " await print(\"{this.name()}: received key {e.key}\")\n"
135 " }\n"
136 " }\n"
137 " }\n"
138 "}\n"
139 "\n"
140 "Panel panel {\n"
141 " Position: {100, 100}\n"
142 " TextField name { Position: {10, 10} }\n"
143 " Slider volume { Position: {10, 40} }\n"
144 " Button ok {\n"
145 " Position: {10, 70}\n"
146 " label { Position: {5, 5} }\n"
147 " }\n"
148 "}\n";
149
150
151
152static void print(
154 int32_t argc,
156 ecs_script_future_t *future)
157{
158 (void)ctx;
159 (void)argc;
160 bool ok = true;
161 printf(" %s\n", *(char**)argv[0].ptr);
162 ecs_script_future_resolve(future,
164 ecs_script_future_release(future);
165}
166
167
168
169
170
175 ecs_script_focus_source_t source,
176 void *ctx)
177{
178 (void)ctx;
179
180
181 if (source != EcsScriptFocusPress) {
183 }
184
186 if (
ecs_has(world, entity, Focusable)) {
188 }
189 }
190
191 return current;
192}
193
194
195
196
197static void to_local(
200 float screen_x,
201 float screen_y,
202 float *local_x,
203 float *local_y,
204 void *ctx)
205{
206 (void)ctx;
207
208 *local_x = screen_x;
209 *local_y = screen_y;
210
211
213 const Position *p =
ecs_get(world, entity, Position);
214 if (p) {
215 *local_x -= p->x;
216 *local_y -= p->y;
217 }
218 }
219}
220
221
222
223
224
225
226static void mouse(
229 float x,
230 float y,
231 uint32_t buttons)
232{
233 EcsScriptMouseEvent evt = {
234 .target = target,
235 .screen_x = x, .screen_y = y,
236 .buttons = buttons
237 };
238
239 to_local(world, target, x, y, &evt.local_x, &evt.local_y, NULL);
240 ecs_script_mouse_event(world, &evt);
241}
242
243
244static void key(
246 const char *name)
247{
248 char *k = ECS_CONST_CAST(char*, name);
249 ecs_script_keyboard_event(world, &(EcsScriptKeyboardEvent){
250 .key = k, .down = true });
251 ecs_script_keyboard_event(world, &(EcsScriptKeyboardEvent){
252 .key = k, .down = false });
253}
254
255static void print_focus(
257{
259 printf(
" [app] focus is %s\n", focus ?
ecs_get_name(world, focus) :
"none");
260}
261
262int main(int argc, char *argv[]) {
264
265
267
270
271
273 .entity =
ecs_id(Position),
274 .members = {
277 }
278 });
279
280 ecs_async_function(world, {
281 .name = "print",
284 .callback = print
285 });
286
287 ecs_script_set_event_hooks(world, &(ecs_script_event_hooks_t){
288 .to_local = to_local,
289 .to_focus = to_focus
290 });
291
292 if (!ecs_script(world, { .code = code })) {
293 printf("script failed to run\n");
294 return -1;
295 }
296
297
298 ecs_script_tasks_progress(world);
299
305
306
307
308 printf("Click the label of the ok button:\n");
309 mouse(world, label, 117, 177, 1);
310 mouse(world, label, 117, 177, 0);
311 print_focus(world);
312
313
314 printf("\nClick the name field and type:\n");
315 mouse(world, name, 120, 115, 1);
316 mouse(world, name, 120, 115, 0);
317 key(world, "h");
318 key(world, "i");
319
320
321 printf("\nClick the panel background:\n");
322 mouse(world, panel, 180, 190, 1);
323 mouse(world, panel, 180, 190, 0);
324 print_focus(world);
325
326
327 printf("\nDrag the volume slider:\n");
328 mouse(world, volume, 120, 145, 0);
329 mouse(world, volume, 120, 145, 1);
330 mouse(world, volume, 140, 145, 1);
331 mouse(world, volume, 155, 145, 1);
332 mouse(world, volume, 155, 145, 0);
333
334
335 printf("\nPress a key on the slider:\n");
336 key(world, "ArrowRight");
337
338
339 printf("\nMove the focus from the application:\n");
340 ecs_script_set_focus(world, ok);
341 key(world, "Enter");
342
343 printf("\nPress Escape:\n");
344 key(world, "Escape");
345 print_focus(world);
346
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386}
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
#define ECS_IMPORT(world, id)
Wrapper around ecs_import().
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
ecs_entity_t ecs_get_parent(const ecs_world_t *world, ecs_entity_t entity)
Get the parent (target of the ChildOf relationship) for an entity.
#define ECS_TAG_DECLARE
Forward declare a tag.
#define ECS_COMPONENT_DEFINE(world, id_)
Define a forward-declared component.
#define ECS_TAG_DEFINE(world, id_)
Define a forward-declared tag.
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
#define ecs_get(world, entity, T)
Get a component.
#define ecs_has(world, entity, T)
Test if an entity has a component.
ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
Look up an entity by its path.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
flecs::entity entity(Args &&... args) const
Create an entity.