zori.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. #ifndef zori_H_INCLUDED
  2. #define zori_H_INCLUDED
  3. /*
  4. * ZORI is a a GUI subsystem for use with Allegro 5 that handles
  5. * the user interface of Eruta.
  6. *
  7. * ZORI is a classic retained-mode GUI system that has an additional
  8. * pseudo-direct mode API. ZORI uses a handler system where events are handled
  9. * by call back functions which can be installed as specific handlers that are
  10. * activated on certain events. But, in the spirit of direct mode GUI's, all
  11. * widgets also keep state flags and value flags that can be used to see if they
  12. * have been activated and that can be inspected in a direct-mode GUI fashion.
  13. * With ZORI you can have your retained mode GUI pie and also eat it in direct
  14. * mode.
  15. *
  16. * In ZORI, there is only one top-level widget, the root, however, every widget
  17. * can contain any amount of child widgets. One level below the root widget are
  18. * the screen widgets which are intent to handle the GUI on one physical screen
  19. * or display. Every GUI screen can have different "pages", but only one page
  20. * should be active per screen. The idea is that a GUI page represents a single
  21. * GUI mode such as the main menu, the settings menu, the in game menu, etc,
  22. * where you definitely want only one to be active at any given time.
  23. *
  24. * The box model used in Zori will be as follows (XXX implement this):
  25. * ......................................
  26. * .margin .
  27. * . +-------------border-----------+ .
  28. * . | padding | .
  29. * . | .......................... | .
  30. * . | . . | .
  31. * . | . contents . | .
  32. * . | . . | .
  33. * . | .......................... | .
  34. * . | padding | .
  35. * . +------------------------------+ .
  36. * .margin .
  37. * ......................................
  38. *
  39. * The padding determines the minimal distance between the border or the
  40. * parent widget and it's contents and/or child widgets.
  41. *
  42. * The border's thickness is only relevant for visual effects.
  43. * It does not change the layout. The border is effectively "inside"
  44. * the padding and/or contents of the widget and is unrelated to the padding.
  45. *
  46. * The margin of a widget determines how closely that widget may be packed
  47. * to it's sibling widgets.
  48. *
  49. * The work in UI is divided between the top-level widgets,
  50. * namely the root and the page, and the other widgets.
  51. * The root and the page handle everything that depends on and/or may influence
  52. * several widgets at once, such as event dispatching but also setting the
  53. * focus, determining which widget is being hovered, or dragged, etc.
  54. * The latter functions change the state of several widgets, so they
  55. * are handled on the level of the container widgets.
  56. * The zori_widget and the other concrete widgets handle the individual
  57. * state and actions of the various widgets individually.
  58. *
  59. *
  60. *
  61. *
  62. */
  63. #include <stdbool.h>
  64. #include "eruta.h"
  65. #include "rebox.h"
  66. #include "miao.h"
  67. #include "str.h"
  68. /* Defines for C11/C99/C89 portability */
  69. #ifndef inline
  70. #define inline inline
  71. #endif
  72. /* Macros for possible later system library portability. */
  73. #define ZORI_EVENT_TYPE_IS_USER(T) ALLEGRO_EVENT_TYPE_IS_USER(T)
  74. #define ZORI_SYSTEM_EVENT_KEY_DOWN ALLEGRO_EVENT_KEY_DOWN
  75. #define ZORI_SYSTEM_EVENT_KEY_UP ALLEGRO_EVENT_KEY_UP
  76. #define ZORI_SYSTEM_EVENT_KEY_CHAR ALLEGRO_EVENT_KEY_CHAR
  77. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  78. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  79. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  80. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP ALLEGRO_EVENT_MOUSE_BUTTON_UP
  81. /* Typedefs for possible later system library portability. */
  82. typedef ALLEGRO_COLOR zori_color;
  83. typedef ALLEGRO_BITMAP zori_bitmap;
  84. typedef ALLEGRO_FONT zori_font;
  85. typedef ALLEGRO_EVENT zori_system_event;
  86. typedef ALLEGRO_EVENT_TYPE zori_event_type;
  87. typedef ALLEGRO_DISPLAY zori_display;
  88. typedef Point zori_point;
  89. typedef Rebox zori_rebox;
  90. typedef Rebox zori_box;
  91. typedef int zori_id;
  92. typedef ALLEGRO_USTR zori_string;
  93. typedef ALLEGRO_USTR zori_text;
  94. /* Macros for possible portability. */
  95. #define zori_font_lineheight(FONT) al_get_font_line_height(FONT)
  96. #define zori_font_drawstr(FONT, COLOR, X, Y, ATTR, TEXT) al_draw_ustr(FONT, COLOR, X, Y, ATTR, TEXT)
  97. /* Default sizes of a widget. */
  98. #define ZORI_WIDGET_DEFAULT_W 640
  99. #define ZORI_WIDGET_DEFAULT_H 480
  100. /* Defaut padding and margin. */
  101. #define ZORI_PADDING_DEFAULT 4
  102. #define ZORI_MARGIN_DEFAULT 0
  103. /* Zori ID's. */
  104. #define ZORI_ID_OK_P(ID) ((ID) > -1)
  105. #define ZORI_ID_OK ((zori_id)(0))
  106. #define ZOTI_ID_GENERATE ((zori_id)(-1))
  107. #define ZORI_ID_ERROR ((zori_id)(-1))
  108. #define ZORI_ID_ENOMEM ((zori_id)(-2))
  109. #define ZORI_ID_EINVAL ((zori_id)(-3))
  110. /** Macro for the number of elements of an array .*/
  111. #define ZORI_ARRAY_AMOUNT(A) ((size_t)(sizeof(A))/(sizeof(*A)))
  112. /** Macro that expands to the the array itself, and the amount of elements
  113. * in that array, in that order. Only works on a statically sized array. */
  114. #define ZORI_ARRAY_AND_AMOUNT(A) (A), ZORI_ARRAY_AMOUNT(A)
  115. /* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
  116. This macro returns, for the given pointer, a pointer to a containing struct
  117. of type TYPE, in which PTR is a member named MEMBER.
  118. This enables cool ways of type genericity and extension in plain C.
  119. It should not run afoul of strict aliasing since it passes over a char * pointer
  120. and a pointer of a containing struct or union.
  121. */
  122. #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
  123. ((PTR) ? ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER))) : NULL)
  124. /** Custom event types, used in conjunction with Allegro event types. */
  125. enum zori_custom_event_type {
  126. ZORI_EVENT_CUSTOM = ALLEGRO_GET_EVENT_TYPE('z', 'o', 'r', 'i'),
  127. /** Update event, called at regular intervals to update the state of the UI.*/
  128. ZORI_EVENT_UPDATE,
  129. /** First draw pass. */
  130. ZORI_EVENT_DRAW,
  131. /** Second draw pass for cursors. */
  132. ZORI_EVENT_OVERDRAW,
  133. /** Cleanup event. */
  134. ZORI_EVENT_DONE,
  135. /** Destruction event. */
  136. ZORI_EVENT_FREE,
  137. /** Activation event, such as a button being clicked or a menu being opened. */
  138. ZORI_EVENT_ACTION,
  139. /** Close event, such as a menu being closed. Not for cleanup.*/
  140. ZORI_EVENT_CLOSE,
  141. /** Child added event to ease setup. */
  142. ZORI_EVENT_NEW_CHILD,
  143. };
  144. struct zori_widget;
  145. /* Common fields for an event. */
  146. struct zori_any_event {
  147. /* Type of the event, possibly copied from sysev. */
  148. zori_event_type type;
  149. /* Widget the event is sent to. */
  150. struct zori_widget * widget;
  151. /* Data specified in the handler. */
  152. void * data;
  153. };
  154. /* System event, that is coming from the underlying library used, e.g. Allegro. */
  155. struct zori_sys_event {
  156. struct zori_any_event any;
  157. zori_system_event * ev;
  158. };
  159. /* Update event, when UI has to update (animations, etc). */
  160. struct zori_update_event {
  161. struct zori_any_event any;
  162. double dt;
  163. };
  164. /* Draw event when the UI has to draw itself. */
  165. struct zori_draw_event {
  166. struct zori_any_event any;
  167. };
  168. /* Cleanup event. */
  169. struct zori_done_event {
  170. struct zori_any_event any;
  171. };
  172. /* Cleanup event. */
  173. struct zori_free_event {
  174. struct zori_any_event any;
  175. };
  176. /* Action event. */
  177. struct zori_action_event {
  178. struct zori_any_event any;
  179. };
  180. /* Close event. */
  181. struct zori_close_event {
  182. struct zori_any_event any;
  183. /* The widget that was closed and that sent this message to it's parent. */
  184. struct zori_widget * from;
  185. };
  186. /* New child event. */
  187. struct zori_new_child_event {
  188. struct zori_any_event any;
  189. /* The widget that was added as a child and that sent this message to it's parent. */
  190. struct zori_widget * child;
  191. };
  192. /** An event that ZORI can handle. The union is cunningly overlaid so
  193. * that the type field and the any field can be used in all cases. */
  194. union zori_event {
  195. /* Type of the event, possibly copied from sysev. */
  196. zori_event_type type;
  197. struct zori_any_event any;
  198. struct zori_sys_event sys;
  199. struct zori_draw_event draw;
  200. struct zori_update_event update;
  201. struct zori_done_event done;
  202. struct zori_free_event free;
  203. struct zori_action_event action;
  204. struct zori_close_event close;
  205. struct zori_new_child_event new_child;
  206. };
  207. /* Helper functions to get widget from an event. */
  208. static inline struct zori_widget *
  209. zori_event_widget(union zori_event * event) {
  210. if (!event) return NULL;
  211. return event->any.widget;
  212. }
  213. /* Helper functions to get data from an event. */
  214. static inline void * zori_event_data(union zori_event * event) {
  215. if (!event) return NULL;
  216. return event->any.data;
  217. }
  218. /* Helper function that checks if an event is a sys_event. */
  219. static inline bool zori_event_is_sys_event(union zori_event * event) {
  220. if (!event) return false;
  221. return (!ALLEGRO_EVENT_TYPE_IS_USER(event->type));
  222. }
  223. /* Helper functions to get system event from an event. Type checks the type. */
  224. static inline zori_system_event * zori_event_system_event(union zori_event * event) {
  225. if (!event) return NULL;
  226. if (!zori_event_is_sys_event(event)) return NULL;
  227. return event->sys.ev;
  228. }
  229. /* Helper functions to set widget for an event. */
  230. static inline struct zori_widget *
  231. zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
  232. if (!event) return NULL;
  233. return event->any.widget = widget;
  234. }
  235. /* Helper functions to set data for an event. */
  236. static inline void * zori_event_set_data(union zori_event * event, void * data) {
  237. if (!event) return NULL;
  238. return event->any.data = data;
  239. }
  240. /* Style flags */
  241. enum zori_style_flag {
  242. ZORI_STYLE_FLAG_BORDER = 1,
  243. ZORI_STYLE_FLAG_FILL = 2,
  244. ZORI_STYLE_FLAG_DEFAULT= (ZORI_STYLE_FLAG_BORDER | ZORI_STYLE_FLAG_FILL),
  245. };
  246. /* A graphic is a color, image, and style flags applied to a part of the GUI. */
  247. struct zori_graphic_style {
  248. zori_color color;
  249. zori_bitmap * bitmap;
  250. int style_flags;
  251. };
  252. /* A text style has all elements needed to style a piece of text. I consists of the text color, font and font flags flags applied to a part of the GUI.
  253. */
  254. struct zori_text_style {
  255. zori_color color;
  256. zori_font * font;
  257. int font_flags;
  258. };
  259. /* A style is a set of style parts for different parts of the GUI. */
  260. struct zori_style {
  261. struct zori_graphic_style fore;
  262. struct zori_graphic_style back;
  263. struct zori_graphic_style border;
  264. struct zori_graphic_style hover;
  265. struct zori_graphic_style mark;
  266. struct zori_text_style text;
  267. };
  268. struct zori_widget;
  269. /* Event handler results. */
  270. enum zori_handle_result {
  271. ZORI_HANDLE_ERROR = -1, /* An error ocurred, stop propagating to children.*/
  272. ZORI_HANDLE_DONE = 0, /* The event was handled and consumed, no need to propagate to children automatically (widget may re-send event manually to children.)*/
  273. ZORI_HANDLE_IGNORE = 1, /* Event wasn't handled, propagate to children. */
  274. ZORI_HANDLE_PASS = 2, /* Event was handled, but needs to be propagated.*/
  275. };
  276. /* Returns whether or not a event needs to be propagated based on the
  277. * result of a handler. */
  278. static int zori_propagate_event_p(enum zori_handle_result result) {
  279. switch(result) {
  280. case ZORI_HANDLE_ERROR: return 0;
  281. case ZORI_HANDLE_DONE: return 0;
  282. case ZORI_HANDLE_IGNORE: return !0;
  283. case ZORI_HANDLE_PASS: return !0;
  284. default: return !0;
  285. }
  286. }
  287. typedef enum zori_handle_result (zori_handler_func)(union zori_event * event);
  288. /* A single event handler */
  289. struct zori_handler {
  290. zori_event_type type;
  291. zori_handler_func * handler;
  292. void * data;
  293. };
  294. /* A dynamic array of event handlers event handlers. */
  295. struct zori_handlers miao_of_type(struct zori_handler);
  296. /* An entry in a widget registry. */
  297. struct zori_registry_entry {
  298. zori_id id;
  299. struct zori_widget * widget;
  300. };
  301. /* A widget registry as a dynamic array of entries. */
  302. struct zori_registry miao_of_type(struct zori_registry_entry);
  303. /* Generic state flags for several zori structs. */
  304. enum zori_flag {
  305. /* The object is not visible, though it may still be interacted with.*/
  306. ZORI_FLAG_HIDDEN = 1 << 0,
  307. /* The object cannot be interacted with, though it is still visible. */
  308. ZORI_FLAG_DISABLED = 1 << 1,
  309. /* The object is both hidden and disabled. */
  310. ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
  311. /* The object is being hovered by the mouse cursor. */
  312. ZORI_FLAG_HOVERED = 1 << 2,
  313. /* The object is "marked" for activation by the keyjoy cursor */
  314. ZORI_FLAG_MARKED = 1 << 3,
  315. /* The object is ready to report a "result".
  316. * What that is depends on the object. */
  317. ZORI_FLAG_READY = 1 << 4,
  318. };
  319. /* Typedef for the type of a widget.
  320. * Not an enum since every widget may define this itself.
  321. */
  322. typedef uint32_t zori_widget_type;
  323. /* Generic widget types. */
  324. #define ZORI_WIDGET_TYPE_NONE ((zori_widget_type)(0))
  325. /* Macro to help generate widget types. */
  326. #define ZORI_WIDGET_TYPE(A, B, C, D) ((zori_widget_type)((A<<24) | (B<<16) | (C<<8) | D))
  327. /* Mouse or keyboard/joystick cursor. */
  328. struct zori_cursor {
  329. zori_point p;
  330. struct zori_widget * hover;
  331. struct zori_widget * focus;
  332. zori_bitmap * bitmap;
  333. enum zori_flag flags;
  334. struct zori_style style;
  335. };
  336. /* Support multiple cursors... */
  337. struct zori_cursors {
  338. struct zori_cursor mouse;
  339. struct zori_cursor keyjoy;
  340. };
  341. /* The value of a result of a widget, see below. */
  342. union zori_result_value {
  343. int integer;
  344. zori_string * string;
  345. };
  346. /* The "result" of a widget. If the flag ready is set, the
  347. * widget has a result to report . This normally happens when it was
  348. * clicked, or when a menu item was selected, */
  349. struct zori_result {
  350. int ready;
  351. union zori_result_value value;
  352. void * extra;
  353. };
  354. /*
  355. on_enter
  356. on_enter(data = {})
  357. on_event(*args)
  358. on_event(*data)
  359. on_key_down(*args)
  360. on_leave(name=nil)
  361. on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  362. on_mouse_button_down(t, x, y, z, w, b)
  363. on_mouse_button_up(t, x, y, z, w, b)
  364. on_mouse_in(x, y, from)
  365. on_mouse_out(x, y, to)
  366. on_resize
  367. */
  368. struct zori_widget;
  369. struct zori_widget_list miao_of_type(struct zori_widget *);
  370. struct zori_widget {
  371. /* ID of the widget, used in most external API's. */
  372. zori_id id;
  373. /* Root level widget under which this widget is active. */
  374. struct zori_widget * root;
  375. /* Position and size of the widget. */
  376. zori_rebox box;
  377. /* Outer rectangle of the widget, with the margin added. */
  378. zori_rebox outer;
  379. /* Inner rectangle of the widget, with the padding removed. */
  380. zori_rebox inner;
  381. /* Z ordering. */
  382. int z;
  383. /* Style. */
  384. struct zori_style style;
  385. /* Handlers. */
  386. struct zori_handlers handlers;
  387. /* Related widgets. */
  388. struct zori_widget * parent;
  389. struct zori_widget_list children;
  390. /* Flags. */
  391. enum zori_flag flags;
  392. /* Type of the widget. */
  393. zori_widget_type type;
  394. /* Generic "result", of last operation on widget. */
  395. struct zori_result result;
  396. /* Cannot easily use the handers for destroying the widget, so in stead
  397. * provide a destructor. */
  398. void (*destroy)(struct zori_widget * widget);
  399. };
  400. /* An array of widget pointers. */
  401. struct zori_widget_array miao_of_type(struct zori_widget *);
  402. /* Forward declarations. */
  403. struct zori_screen;
  404. struct zori_console;
  405. /*
  406. * Root level widget, my spread out over several displays.
  407. * In Zori, there can only be a single root level widget active.
  408. * It's ID is always 0;
  409. */
  410. struct zori_root {
  411. /* A root is a widget. */
  412. struct zori_widget widget;
  413. /* Current active screen widget if any. */
  414. struct zori_screen * active_screen;
  415. /* Current active console if any. */
  416. struct zori_console * console;
  417. };
  418. /* Forward declaration of a page. */
  419. struct zori_page;
  420. struct zori_root * zori_get_root(void);
  421. /* Initializes Zori and creates a top level widget. Returns 0 on success
  422. * or negative on error. The style will be copied and set as default
  423. * if it is not NULL. Otherwise a built-in style will be used.
  424. * Not that ZORI will NOT clean up any images or fonts it uses by itself.
  425. */
  426. zori_id zori_start(struct zori_style * default_style);
  427. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  428. * negative on error.
  429. */
  430. zori_id zori_shutdown();
  431. /* Creates a new screen widget. Normally this should be the first widget
  432. * you create after zori_start. */
  433. /* Activates the page on it's display. All other pages are dectivated and
  434. * hidden. */
  435. zori_id zori_activate_page(zori_id page);
  436. /* Creates a new generic widget on the given screen with the given
  437. * dimensions. */
  438. zori_id zori_new(zori_id screen, zori_rebox * box);
  439. /* Sets the flags of a widget. */
  440. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  441. /* Sets the whole style of a widget. */
  442. zori_id zori_set_style(zori_id id, struct zori_style * style);
  443. /* Sets the background color of the widget. */
  444. zori_id zori_set_background_color(zori_id id, zori_color color);
  445. /* Sets the foreground color of the widget. */
  446. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  447. /* Creates a new frame widget. */
  448. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  449. /* Creates a new (vertical) menu widget. */
  450. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  451. /* Creates a new button widget. */
  452. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  453. /* Creates a new conversation widget. */
  454. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  455. /* Draws the whole UI and all visible parts. */
  456. void zori_draw_all(void);
  457. /* Updates the state of the UI. Pass in the time passed since last update. */
  458. void zori_update(double dt);
  459. /* Registers an event handler for a widget. */
  460. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  461. int zori_handler_compare(const void *v1, const void *v2);
  462. struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
  463. void zori_handlers_done(struct zori_handlers *me);
  464. void zori_handlers_init(struct zori_handlers *me);
  465. struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
  466. int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
  467. struct zori_root *zori_get_root(void);
  468. struct zori_widget *zori_get_root_widget(void);
  469. struct zori_widget *zori_get_widget(zori_id id);
  470. zori_id zori_get_unused_id(void);
  471. zori_id zori_initialize_root(void);
  472. zori_id zori_start(struct zori_style *default_style);
  473. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom);
  474. zori_id zori_set_margin(zori_id id, int size);
  475. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom);
  476. zori_id zori_set_padding(zori_id id, int size);
  477. zori_font *zori_text_font(zori_id id);
  478. void zori_destroy_root(void);
  479. zori_id zori_shutdown(void);
  480. void zori_draw_all(void);
  481. int zori_handle_system_event(zori_system_event *sysev);
  482. void zori_update(double dt);
  483. int zori_result(zori_id id);
  484. #endif