zori.h 16 KB

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