zori.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef zori_H_INCLUDED
  2. #define zori_H_INCLUDED
  3. #include <stdbool.h>
  4. #include "eruta.h"
  5. #include "rebox.h"
  6. #include "miao.h"
  7. #include "str.h"
  8. /* Defines for C11/C99/C89 portability */
  9. #ifndef inline
  10. #define inline inline
  11. #endif
  12. /* Macros for possible later system library portability. */
  13. #define ZORI_EVENT_TYPE_IS_USER(T) ALLEGRO_EVENT_TYPE_IS_USER(T)
  14. #define ZORI_SYSTEM_EVENT_KEY_DOWN ALLEGRO_EVENT_KEY_DOWN
  15. #define ZORI_SYSTEM_EVENT_KEY_UP ALLEGRO_EVENT_KEY_UP
  16. #define ZORI_SYSTEM_EVENT_KEY_CHAR ALLEGRO_EVENT_KEY_CHAR
  17. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  18. #define ZORI_SYSTEM_EVENT_MOUSE_AXES ALLEGRO_EVENT_MOUSE_AXES
  19. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
  20. #define ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP ALLEGRO_EVENT_MOUSE_BUTTON_UP
  21. /* Typedefs for possible later system library portability. */
  22. typedef ALLEGRO_COLOR zori_color;
  23. typedef ALLEGRO_BITMAP zori_bitmap;
  24. typedef ALLEGRO_FONT zori_font;
  25. typedef ALLEGRO_EVENT zori_system_event;
  26. typedef ALLEGRO_EVENT_TYPE zori_event_type;
  27. typedef ALLEGRO_DISPLAY zori_display;
  28. typedef Point zori_point;
  29. typedef Rebox zori_rebox;
  30. typedef Rebox zori_box;
  31. typedef int zori_id;
  32. typedef ALLEGRO_USTR zori_string;
  33. typedef ALLEGRO_USTR zori_text;
  34. /* Macros for possible portability. */
  35. #define zori_font_lineheight(FONT) al_get_font_line_height(FONT)
  36. #define zori_font_drawstr(FONT, COLOR, X, Y, ATTR, TEXT) al_draw_ustr(FONT, COLOR, X, Y, ATTR, TEXT)
  37. /* Default sizes of a widget. */
  38. #define ZORI_WIDGET_DEFAULT_W 640
  39. #define ZORI_WIDGET_DEFAULT_H 480
  40. /* Zori ID's. */
  41. #define ZORI_ID_OK_P(ID) ((ID) > -1)
  42. #define ZORI_ID_OK ((zori_id)(0))
  43. #define ZORI_ID_ERROR ((zori_id)(-1))
  44. #define ZORI_ID_ENOMEM ((zori_id)(-2))
  45. #define ZORI_ID_EINVAL ((zori_id)(-3))
  46. /** Macro for the number of elements of an array .*/
  47. #define ZORI_ARRAY_AMOUNT(A) ((size_t)(sizeof(A))/(sizeof(*A)))
  48. /** Macro that expands to the the array itself, and the amount of elements
  49. * in that array, in that order. Only works on a statically sized array. */
  50. #define ZORI_ARRAY_AND_AMOUNT(A) (A), ZORI_ARRAY_AMOUNT(A)
  51. /* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
  52. This macro returns, for the given pointer, a pointer to a containing struct
  53. of type TYPE, in which PTR is a member named MEMBER.
  54. This enables cool ways of type genericity and extension in plain C.
  55. It should not run afoul of strict aliasing since it passes over a char * pointer
  56. and a pointer of a containing struct or union.
  57. */
  58. #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
  59. ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER)))
  60. /** Custom event types, used in conjunction with Allegro event types. */
  61. enum zori_custom_event_type {
  62. ZORI_EVENT_CUSTOM = ALLEGRO_GET_EVENT_TYPE('z', 'o', 'r', 'i'),
  63. ZORI_EVENT_UPDATE,
  64. /** First draw pass. */
  65. ZORI_EVENT_DRAW,
  66. /** Second draw pass for cursors. */
  67. ZORI_EVENT_OVERDRAW,
  68. ZORI_EVENT_DONE,
  69. ZORI_EVENT_FREE,
  70. ZORI_EVENT_ACTION,
  71. };
  72. struct zori_widget;
  73. /* Common fields for an event. */
  74. struct zori_any_event {
  75. /* Type of the event, possibly copied from sysev. */
  76. zori_event_type type;
  77. /* Widget the event is sent to. */
  78. struct zori_widget * widget;
  79. /* Data specified in the handler. */
  80. void * data;
  81. };
  82. /* System event, that is coming from the underlying library used, e.g. Allegro. */
  83. struct zori_sys_event {
  84. struct zori_any_event any;
  85. zori_system_event * ev;
  86. };
  87. /* Update event, when UI has to update (animations, etc). */
  88. struct zori_update_event {
  89. struct zori_any_event any;
  90. double dt;
  91. };
  92. /* Draw event when the UI has to draw itself. */
  93. struct zori_draw_event {
  94. struct zori_any_event any;
  95. };
  96. /* Cleanup event. */
  97. struct zori_done_event {
  98. struct zori_any_event any;
  99. };
  100. /* Cleanup event. */
  101. struct zori_free_event {
  102. struct zori_any_event any;
  103. };
  104. /* Action event. */
  105. struct zori_action_event {
  106. struct zori_any_event any;
  107. };
  108. /** An event that ZORI can handle. The union is cunningly overlaid so
  109. * that the type field and the any field can be used in all cases. */
  110. union zori_event {
  111. /* Type of the event, possibly copied from sysev. */
  112. zori_event_type type;
  113. struct zori_any_event any;
  114. struct zori_sys_event sys;
  115. struct zori_draw_event draw;
  116. struct zori_update_event update;
  117. struct zori_done_event done;
  118. struct zori_free_event free;
  119. struct zori_action_event action;
  120. };
  121. /* Helper functions to get widget from an event. */
  122. static inline struct zori_widget *
  123. zori_event_widget(union zori_event * event) {
  124. if (!event) return NULL;
  125. return event->any.widget;
  126. }
  127. /* Helper functions to get data from an event. */
  128. static inline void * zori_event_data(union zori_event * event) {
  129. if (!event) return NULL;
  130. return event->any.data;
  131. }
  132. /* Helper function that checks if an event is a sys_event. */
  133. static inline bool zori_event_is_sys_event(union zori_event * event) {
  134. if (!event) return false;
  135. return (!ALLEGRO_EVENT_TYPE_IS_USER(event->type));
  136. }
  137. /* Helper functions to get system event from an event. Type checks the type. */
  138. static inline zori_system_event * zori_event_system_event(union zori_event * event) {
  139. if (!event) return NULL;
  140. if (!zori_event_is_sys_event(event)) return NULL;
  141. return event->sys.ev;
  142. }
  143. /* Helper functions to set widget for an event. */
  144. static inline struct zori_widget *
  145. zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
  146. if (!event) return NULL;
  147. return event->any.widget = widget;
  148. }
  149. /* Helper functions to set data for an event. */
  150. static inline void * zori_event_set_data(union zori_event * event, void * data) {
  151. if (!event) return NULL;
  152. return event->any.data = data;
  153. }
  154. /* A style part is a color, image and font applied to a part of the GUI. */
  155. struct zori_stylepart {
  156. zori_color color;
  157. zori_bitmap * image;
  158. zori_font * font;
  159. };
  160. /* A style is a set of style parts for different parts of the GUI. */
  161. struct zori_style {
  162. struct zori_stylepart fore;
  163. struct zori_stylepart back;
  164. struct zori_stylepart text;
  165. struct zori_stylepart border;
  166. };
  167. struct zori_widget;
  168. /* Event handler results. */
  169. enum zori_handle_result {
  170. ZORI_HANDLE_ERROR = -1, /* An error ocurred, stop propagating to children.*/
  171. 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.)*/
  172. ZORI_HANDLE_IGNORE = 1, /* Event wasn't handled, propagate to children. */
  173. ZORI_HANDLE_PASS = 2, /* Event was handled, but needs to be propagated.*/
  174. };
  175. /* Returns whether or not a even needds to be propagated based on the
  176. * result of a handler. */
  177. static int zori_propagate_event_p(enum zori_handle_result result) {
  178. switch(result) {
  179. case ZORI_HANDLE_ERROR: return 0;
  180. case ZORI_HANDLE_DONE: return 0;
  181. case ZORI_HANDLE_IGNORE: return !0;
  182. case ZORI_HANDLE_PASS: return !0;
  183. default: return !0;
  184. }
  185. }
  186. typedef enum zori_handle_result (zori_handler_func)(union zori_event * event);
  187. /* A single event handler */
  188. struct zori_handler {
  189. zori_event_type type;
  190. zori_handler_func * handler;
  191. void * data;
  192. };
  193. /* A dynamic array of event handlers event handlers. */
  194. struct zori_handlers miao_of_type(struct zori_handler);
  195. /* An entry in a widget registry. */
  196. struct zori_registry_entry {
  197. zori_id id;
  198. struct zori_widget * widget;
  199. };
  200. /* A widget registry as a dynamic array of entries. */
  201. struct zori_registry miao_of_type(struct zori_registry_entry);
  202. /* Generic flags for several zori structs. */
  203. enum zori_flag {
  204. /* The object is not visible, though it may still be interacted with.*/
  205. ZORI_FLAG_HIDDEN = 1 << 0,
  206. /* The object cannot be interacted with, though it is still visible. */
  207. ZORI_FLAG_DISABLED = 1 << 1,
  208. /* The object is both hidden and disabled. */
  209. ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
  210. /* The object is being hovered by the mouse cursor. */
  211. ZORI_FLAG_HOVERED = 1 << 2,
  212. /* The object is "marked" for actovation by the keyjoy cursor */
  213. ZORI_FLAG_MARKED = 1 << 3,
  214. };
  215. /* Mouse or keyboard/joystick cursor. */
  216. struct zori_cursor {
  217. zori_point p;
  218. struct zori_widget * hover;
  219. struct zori_widget * focus;
  220. zori_bitmap * bitmap;
  221. enum zori_flag flags;
  222. };
  223. /* Support multiple cursors... */
  224. struct zori_cursors {
  225. struct zori_cursor mouse;
  226. struct zori_cursor keyjoy;
  227. };
  228. /*
  229. on_enter
  230. on_enter(data = {})
  231. on_event(*args)
  232. on_event(*data)
  233. on_key_down(*args)
  234. on_leave(name=nil)
  235. on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  236. on_mouse_button_down(t, x, y, z, w, b)
  237. on_mouse_button_up(t, x, y, z, w, b)
  238. on_mouse_in(x, y, from)
  239. on_mouse_out(x, y, to)
  240. on_resize
  241. */
  242. struct zori_widget;
  243. struct zori_widget_list miao_of_type(struct zori_widget *);
  244. struct zori_widget {
  245. /* ID of the widget, used in most external API's. */
  246. zori_id id;
  247. /* Root level widget under which this widget is active. */
  248. struct zori_widget * root;
  249. /* Position and size of the widget. */
  250. zori_rebox box;
  251. /* Z ordering. */
  252. int z;
  253. /* Style. */
  254. struct zori_style style;
  255. /* Handlers. */
  256. struct zori_handlers handlers;
  257. /* Related widgets. */
  258. struct zori_widget * parent;
  259. struct zori_widget_list children;
  260. /* Flags. */
  261. enum zori_flag flags;
  262. };
  263. /* An array of widget pointers. */
  264. struct zori_widget_array miao_of_type(struct zori_widget *);
  265. /* forward declarations. */
  266. struct zori_screen;
  267. struct zori_console;
  268. /*
  269. * Root level widget, my spread out over several displays.
  270. * In Zori, there can only be a single root level widget active.
  271. * It's ID is always 0;
  272. */
  273. struct zori_root {
  274. /* A root is a widget. */
  275. struct zori_widget widget;
  276. /* Current active screen widget if any. */
  277. struct zori_screen * active_screen;
  278. /* Current active console if any. */
  279. struct zori_console * console;
  280. };
  281. /* Forward declaration of a page. */
  282. struct zori_page;
  283. struct zori_root * zori_get_root(void);
  284. /* Initializes Zori and creates a top level widget. Returns 0 on success
  285. * or negative on error. The style will be copied and set as default
  286. * if it is not NULL. Otherwise a built-in style will be used.
  287. * Not that ZORI will NOT clean up any images or fonts it uses by itself.
  288. */
  289. zori_id zori_start(struct zori_style * default_style);
  290. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  291. * negative on error.
  292. */
  293. zori_id zori_shutdown();
  294. /* Creates a new screen widget. Normally this should be the first widget
  295. * you create after zori_start. */
  296. /* Activates the page on it's display. All other pages are dectivated and
  297. * hidden. */
  298. zori_id zori_activate_page(zori_id page);
  299. /* Creates a new generic widget on the given screen with the given
  300. * dimensions. */
  301. zori_id zori_new(zori_id screen, zori_rebox * box);
  302. /* Sets the flags of a widget. */
  303. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  304. /* Sets the whole style of a widget. */
  305. zori_id zori_set_style(zori_id id, struct zori_style * style);
  306. /* Sets the background color of the widget. */
  307. zori_id zori_set_background_color(zori_id id, zori_color color);
  308. /* Sets the foreground color of the widget. */
  309. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  310. /* Creates a new frame widget. */
  311. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  312. /* Creates a new (vertical) menu widget. */
  313. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  314. /* Creates a new button widget. */
  315. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  316. /* Creates a new conversation widget. */
  317. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  318. /* Draws the whole UI and all visible parts. */
  319. void zori_draw_all(void);
  320. /* Updates the state of the UI. Pass in the time passed since last update. */
  321. void zori_update(double dt);
  322. /* Registers an event handler for a widget. */
  323. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  324. int zori_registry_entry_compare(const void *v1, const void *v2);
  325. zori_id zori_registry_init(struct zori_registry *registry);
  326. zori_id zori_registry_add(struct zori_registry *registry, zori_id id, struct zori_widget *widget);
  327. struct zori_registry_entry *zori_registry_lookup_entry(struct zori_registry *registry, zori_id id);
  328. struct zori_widget *zori_registry_lookup(struct zori_registry *registry, zori_id id);
  329. zori_id zori_registry_remove(struct zori_registry *registry, zori_id id);
  330. int zori_handler_compare(const void *v1, const void *v2);
  331. struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
  332. void zori_handlers_done(struct zori_handlers *me);
  333. void zori_handlers_init(struct zori_handlers *me);
  334. struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
  335. int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
  336. int zori_widget_raise_system_event(struct zori_widget *widget, zori_system_event *sysev);
  337. int zori_widget_raise_draw_event(struct zori_widget *widget);
  338. int zori_widget_raise_done_event(struct zori_widget *widget);
  339. int zori_widget_raise_free_event(struct zori_widget *widget);
  340. int zori_widget_raise_update_event(struct zori_widget *widget, double dt);
  341. int zori_widget_raise_action_event(struct zori_widget *widget);
  342. int zori_widget_compare(const void *v1, const void *v2);
  343. struct zori_widget *zori_get_widget(zori_id id);
  344. zori_id zori_get_unused_id(void);
  345. struct zori_handler *zori_widget_add_handler(struct zori_widget *widget, zori_event_type type, zori_handler_func *handler, void *data);
  346. struct zori_handler *zori_widget_add_handlers(struct zori_widget *widget, struct zori_handler *handlers, size_t amount);
  347. zori_id zori_start(struct zori_style *default_style);
  348. struct zori_widget *zori_widget_done(struct zori_widget *widget);
  349. void zori_widget_free(struct zori_widget *widget);
  350. struct zori_widget *zori_widget_add_child(struct zori_widget *parent, struct zori_widget *child);
  351. struct zori_widget *zori_widget_init(struct zori_widget *widget, zori_id id, struct zori_widget *parent, zori_rebox *box, struct zori_style *style);
  352. zori_id zori_shutdown(void);
  353. struct zori_widget *zori_widget_initall(struct zori_widget *widget, zori_id id, struct zori_widget *parent, zori_rebox *box, struct zori_style *style, struct zori_handler *handlers, size_t amount);
  354. void zori_draw_all(void);
  355. int zori_widget_visible(struct zori_widget *widget);
  356. int zori_widget_active(struct zori_widget *widget);
  357. int zori_widget_hover(struct zori_widget *widget);
  358. int zori_widget_hover_(struct zori_widget *widget, int set);
  359. int zori_widget_marked(struct zori_widget *widget);
  360. int zori_widget_marked_(struct zori_widget *widget, int set);
  361. int zori_widget_active_(struct zori_widget *widget, int set);
  362. void zori_update(double dt);
  363. zori_font *zori_widget_font(struct zori_widget *widget);
  364. zori_color zori_widget_forecolor(struct zori_widget *widget);
  365. zori_color zori_widget_backcolor(struct zori_widget *widget);
  366. int zori_widget_h(struct zori_widget *widget);
  367. int zori_widget_w(struct zori_widget *widget);
  368. int zori_widget_x(struct zori_widget *widget);
  369. int zori_widget_y(struct zori_widget *widget);
  370. int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y);
  371. void zori_widget_drawroundframe(struct zori_widget *self);
  372. int zori_handle_system_event(zori_system_event * sysev);
  373. #endif