2021-03-12 16:19:43 +01:00
|
|
|
//---
|
|
|
|
// JustUI.jscene: Root object that provides keyboard focus and event handling
|
|
|
|
//---
|
|
|
|
|
|
|
|
#ifndef _J_JSCENE
|
|
|
|
#define _J_JSCENE
|
|
|
|
|
|
|
|
#include <justui/defs.h>
|
|
|
|
#include <justui/jwidget.h>
|
|
|
|
#include <justui/jevent.h>
|
|
|
|
|
|
|
|
#define JSCENE_QUEUE_SIZE 32
|
|
|
|
|
|
|
|
/* jscene: A widget scene with keyboard focus and event handling
|
|
|
|
|
|
|
|
This widget is designed to be the root of a widget tree. It keeps track of
|
|
|
|
widgets with keyboard focus, feeds them keyboard events, and catches other
|
|
|
|
useful events to store them in an event queue. */
|
|
|
|
typedef struct {
|
|
|
|
jwidget widget;
|
|
|
|
|
|
|
|
/* Location on screen */
|
|
|
|
int16_t x, y;
|
|
|
|
/* Widget with focus */
|
|
|
|
jwidget *focus;
|
|
|
|
|
|
|
|
/* Circular event queue */
|
|
|
|
jevent queue[JSCENE_QUEUE_SIZE];
|
|
|
|
uint8_t queue_first;
|
|
|
|
uint8_t queue_next;
|
|
|
|
|
|
|
|
/* Number of events lots */
|
|
|
|
uint16_t lost_events;
|
2022-06-24 20:28:44 +02:00
|
|
|
/* Whether jscene_run() returns to the main menu */
|
|
|
|
bool mainmenu;
|
2021-03-12 16:19:43 +01:00
|
|
|
|
|
|
|
} jscene;
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
extern uint16_t JSCENE_NONE;
|
|
|
|
extern uint16_t JSCENE_PAINT;
|
2021-05-30 10:40:58 +02:00
|
|
|
extern uint16_t JSCENE_KEY; /* Deprecated, merged with JWIDGET_KEY */
|
2021-03-12 16:19:43 +01:00
|
|
|
|
|
|
|
/* jscene_create(): Create a new scene at that specified screen position */
|
|
|
|
jscene *jscene_create(int x, int y, int w, int h, void *parent);
|
|
|
|
|
|
|
|
/* jscene_create_fullscreen(): Create a fullscreen scene
|
|
|
|
The position is (0,0) and the size is (DWIDTH,DHEIGHT). */
|
|
|
|
jscene *jscene_create_fullscreen(void *parent);
|
|
|
|
|
2021-05-12 15:24:12 +02:00
|
|
|
/* jscene_owning(): Find the scene that manages a widget (if any)
|
|
|
|
This functions walks up the widget tree and locates the scene owning the
|
|
|
|
specified widget, if there is one; otherwise returns NULL. */
|
|
|
|
jscene *jscene_owning(void *widget);
|
|
|
|
|
2021-03-12 16:19:43 +01:00
|
|
|
/* jscene_layout(): Layout a scene
|
|
|
|
This is automatically called by jscene_render(), but may be useful if you
|
|
|
|
need to know the size of your widgets before rendering. The layout is
|
|
|
|
recomputed only if something in the scene has changed. */
|
|
|
|
#define jscene_layout jwidget_layout
|
|
|
|
|
|
|
|
/* jscene_render(): Layout and render a scene
|
|
|
|
Layout is lazy and performed only if needed. The scene is rendered at its
|
|
|
|
(x,y) point. */
|
|
|
|
void jscene_render(jscene *scene);
|
|
|
|
|
|
|
|
//---
|
|
|
|
// Events sent from the scene to the user
|
|
|
|
//---
|
|
|
|
|
|
|
|
/* jscene_read_event(): Get the next upwards event from the queue
|
|
|
|
If there is no event, returns an event of type JSCENE_NONE. */
|
|
|
|
jevent jscene_read_event(jscene *scene);
|
|
|
|
|
|
|
|
/* jscene_queue_event(): Queue an upwards event to be later read by the user
|
|
|
|
This function records an event in the scene's queue, which will later be
|
|
|
|
returned by jscene_pollevent(). This is mostly used by widget code to signal
|
|
|
|
stuff that warrants attention. */
|
|
|
|
void jscene_queue_event(jscene *scene, jevent event);
|
|
|
|
|
|
|
|
//---
|
|
|
|
// Keyboard focus and keyboard events
|
|
|
|
//---
|
|
|
|
|
|
|
|
/* jscene_focused_widget(): Query the widget that currently has focus */
|
|
|
|
void *jscene_focused_widget(jscene *scene);
|
|
|
|
|
|
|
|
/* jscene_set_focused_widget(): Move the focus to a widget
|
|
|
|
The selected widget, obviously, must be a descendant of the scene. */
|
|
|
|
void jscene_set_focused_widget(jscene *scene, void *widget);
|
|
|
|
|
2022-06-19 23:45:05 +02:00
|
|
|
/* jscene_show_and_focus(): Make a widget visible and focus it
|
|
|
|
This function does three things:
|
|
|
|
1. Set the visibility of the target widget to [true]
|
|
|
|
2. Configure any parent with a stacked layout to show the child that
|
|
|
|
contains the target widget
|
|
|
|
3. Focus the target widget */
|
|
|
|
void jscene_show_and_focus(jscene *scene, void *widget);
|
|
|
|
|
2021-05-12 17:04:49 +02:00
|
|
|
/* jscene_process_key_event(): Send a key event to the focused widget
|
2021-03-12 16:19:43 +01:00
|
|
|
Returns true if the event was accepted, false if it was ignored. */
|
2021-05-12 17:04:49 +02:00
|
|
|
bool jscene_process_key_event(jscene *scene, key_event_t event);
|
2021-03-12 16:19:43 +01:00
|
|
|
|
2021-05-12 17:04:49 +02:00
|
|
|
/* jscene_process_event(): Bubble an event up from its source
|
|
|
|
Returns true if the event was accepted along the way, false if ignored. */
|
|
|
|
bool jscene_process_event(jscene *scene, jevent event);
|
2021-03-12 16:19:43 +01:00
|
|
|
|
2022-06-24 20:28:44 +02:00
|
|
|
/* jscene_set_mainmenu(): Set whether jscene_run() will return to main menu
|
|
|
|
Disabling this is useful if you want to catch the MENU key or clean up
|
|
|
|
resources before invoking the main menu. */
|
|
|
|
void jscene_set_mainmenu(jscene *scene, bool mainmenu);
|
2021-03-12 16:19:43 +01:00
|
|
|
|
|
|
|
/* jscene_run(): Run a scene's main loop
|
|
|
|
|
|
|
|
This function implements a main control loop that sleeps when there is
|
|
|
|
nothing to do, forwards all key events to the scene, and returns only to
|
|
|
|
notify GUI events or hand over key events that have been ignored by the
|
|
|
|
scene.
|
|
|
|
|
|
|
|
If a scene event occurs, returns it. If a key event occurs, an event of type
|
2021-05-30 10:40:58 +02:00
|
|
|
JWIDGET_KEY is return and its .key attribute contains the details of the
|
2021-03-12 16:19:43 +01:00
|
|
|
forwarded keyboard event. */
|
|
|
|
jevent jscene_run(jscene *scene);
|
|
|
|
|
|
|
|
#endif /* _J_JSCENE */
|