zori.h 15 KB

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