zori.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. typedef ALLEGRO_USTR zori_string;
  29. /* Macros for possible portability. */
  30. #define zori_font_lineheight(FONT) al_get_font_line_height(FONT)
  31. #define zori_font_drawstr(FONT, COLOR, X, Y, ATTR, TEXT) al_draw_ustr(FONT, COLOR, X, Y, ATTR, TEXT)
  32. /* Default sizes of a widget. */
  33. #define ZORI_WIDGET_DEFAULT_W 640
  34. #define ZORI_WIDGET_DEFAULT_H 480
  35. /* Zori ID's. */
  36. #define ZORI_ID_OK_P(ID) ((ID) > -1)
  37. #define ZORI_ID_OK ((zori_id)(0))
  38. #define ZORI_ID_ERROR ((zori_id)(-1))
  39. #define ZORI_ID_ENOMEM ((zori_id)(-2))
  40. #define ZORI_ID_EINVAL ((zori_id)(-3))
  41. /* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
  42. This macro returns, for the given pointer, a pointer to a containing struct
  43. of type TYPE, in which PTR is a member named MEMBER.
  44. This enables cool ways of type genericity and extension in plain C.
  45. It should not run afoul of strict aliasing since it passes over a char * pointer
  46. and a pointer of a containing struct or union.
  47. */
  48. #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
  49. ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER)))
  50. /** Custom event types, used in conjunction with Allegro event types. */
  51. enum zori_custom_event_type {
  52. ZORI_EVENT_CUSTOM = ALLEGRO_GET_EVENT_TYPE('z', 'o', 'r', 'i'),
  53. ZORI_EVENT_UPDATE,
  54. ZORI_EVENT_DRAW,
  55. ZORI_EVENT_DONE,
  56. ZORI_EVENT_FREE,
  57. ZORI_EVENT_ACTION,
  58. };
  59. struct zori_widget;
  60. /* Common fields for an event. */
  61. struct zori_any_event {
  62. /* Type of the event, possibly copied from sysev. */
  63. zori_event_type type;
  64. /* Widget the event is sent to. */
  65. struct zori_widget * widget;
  66. /* Data specified in the handler. */
  67. void * data;
  68. };
  69. /* System event, that is coming from the underlying library used, e.g. Allegro. */
  70. struct zori_sys_event {
  71. struct zori_any_event any;
  72. zori_system_event * ev;
  73. };
  74. /* Update event, when UI has to update (animations, etc). */
  75. struct zori_update_event {
  76. struct zori_any_event any;
  77. double dt;
  78. };
  79. /* Draw event when the UI has to draw itself. */
  80. struct zori_draw_event {
  81. struct zori_any_event any;
  82. };
  83. /* Cleanup event. */
  84. struct zori_done_event {
  85. struct zori_any_event any;
  86. };
  87. /* Cleanup event. */
  88. struct zori_free_event {
  89. struct zori_any_event any;
  90. };
  91. /* Action event. */
  92. struct zori_action_event {
  93. struct zori_any_event any;
  94. };
  95. /** An event that ZORI can handle. The union is cunningly overlaid so
  96. * that the type field and the any field can be used in all cases. */
  97. union zori_event {
  98. /* Type of the event, possibly copied from sysev. */
  99. zori_event_type type;
  100. struct zori_any_event any;
  101. struct zori_sys_event sys;
  102. struct zori_draw_event draw;
  103. struct zori_update_event update;
  104. struct zori_done_event done;
  105. struct zori_free_event free;
  106. struct zori_action_event action;
  107. };
  108. /* Helper functions to get widget from an event. */
  109. static inline struct zori_widget *
  110. zori_event_widget(union zori_event * event) {
  111. if (!event) return NULL;
  112. return event->any.widget;
  113. }
  114. /* Helper functions to get data from an event. */
  115. static inline void * zori_event_data(union zori_event * event) {
  116. if (!event) return NULL;
  117. return event->any.data;
  118. }
  119. /* Helper function that checks if an event is a sys_event. */
  120. static inline bool zori_event_is_sys_event(union zori_event * event) {
  121. if (!event) return false;
  122. return (!ALLEGRO_EVENT_TYPE_IS_USER(event->type));
  123. }
  124. /* Helper functions to get system event from an event. Type checks the type. */
  125. static inline zori_system_event * zori_event_system_event(union zori_event * event) {
  126. if (!event) return NULL;
  127. if (!zori_event_is_sys_event(event)) return NULL;
  128. return event->sys.ev;
  129. }
  130. /* Helper functions to set widget for an event. */
  131. static inline struct zori_widget *
  132. zori_event_set_widget(union zori_event * event, struct zori_widget * widget) {
  133. if (!event) return NULL;
  134. return event->any.widget = widget;
  135. }
  136. /* Helper functions to set data for an event. */
  137. static inline void * zori_event_set_data(union zori_event * event, void * data) {
  138. if (!event) return NULL;
  139. return event->any.data = data;
  140. }
  141. /* A style part is a color, image and font applied to a part of the GUI. */
  142. struct zori_stylepart {
  143. zori_color color;
  144. zori_bitmap * image;
  145. zori_font * font;
  146. };
  147. /* A style is a set of style parts for different parts of the GUI. */
  148. struct zori_style {
  149. struct zori_stylepart fore;
  150. struct zori_stylepart back;
  151. struct zori_stylepart text;
  152. struct zori_stylepart border;
  153. };
  154. struct zori_widget;
  155. /* Event handler results. */
  156. enum zori_handle_result {
  157. ZORI_HANDLE_ERROR = -1,
  158. ZORI_HANDLE_OK = 0,
  159. ZORI_HANDLE_IGNORE = 1,
  160. };
  161. typedef enum zori_handle_result (zori_handler_func)(union zori_event * event);
  162. /* A single event handler */
  163. struct zori_handler {
  164. zori_event_type type;
  165. zori_handler_func * handler;
  166. void * data;
  167. };
  168. /* A dynamic array of event handlers event handlers. */
  169. struct zori_handlers miao_of_type(struct zori_handler);
  170. /* An entry in a widget registry. */
  171. struct zori_registry_entry {
  172. zori_id id;
  173. struct zori_widget * widget;
  174. };
  175. /* A widget registry as a dynamic array of entries. */
  176. struct zori_registry miao_of_type(struct zori_registry_entry);
  177. /* Generic flags for several zori structs. */
  178. enum zori_flag {
  179. /* The object is not visible, though it may still be interacted with.*/
  180. ZORI_FLAG_HIDDEN = 1 << 0,
  181. /* The object cannot be interacted with, though it is still visible. */
  182. ZORI_FLAG_DISABLED = 1 << 1,
  183. /* The object is both hidden and disabled. */
  184. ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
  185. };
  186. /* Mouse or keyboard/joystick cursor. */
  187. struct zori_cursor {
  188. zori_point p;
  189. struct zori_widget * hover;
  190. struct zori_widget * focus;
  191. zori_bitmap * bitmap;
  192. enum zori_flag flags;
  193. };
  194. /* Support multiple cursors... */
  195. struct zori_cursors {
  196. struct zori_cursor mouse;
  197. struct zori_cursor keyjoy;
  198. };
  199. /*
  200. on_enter
  201. on_enter(data = {})
  202. on_event(*args)
  203. on_event(*data)
  204. on_key_down(*args)
  205. on_leave(name=nil)
  206. on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  207. on_mouse_button_down(t, x, y, z, w, b)
  208. on_mouse_button_up(t, x, y, z, w, b)
  209. on_mouse_in(x, y, from)
  210. on_mouse_out(x, y, to)
  211. on_resize
  212. */
  213. struct zori_widget;
  214. struct zori_widget_list miao_of_type(struct zori_widget *);
  215. struct zori_widget {
  216. /* ID of the widget, used in most external API's. */
  217. zori_id id;
  218. /* Root level widget under which this widget is active. */
  219. struct zori_widget * root;
  220. /* Position and size of the widget. */
  221. zori_rebox box;
  222. /* Z ordering. */
  223. int z;
  224. /* Style. */
  225. struct zori_style style;
  226. /* Handlers. */
  227. struct zori_handlers handlers;
  228. /* Related widgets. */
  229. struct zori_widget * parent;
  230. struct zori_widget_list children;
  231. /* Flags. */
  232. enum zori_flag flags;
  233. };
  234. /* An array of widget pointers. */
  235. struct zori_widget_array miao_of_type(struct zori_widget *);
  236. /* forward declaration. */
  237. struct zori_screen;
  238. /*
  239. * Root level widget, my spread out over several displays.
  240. * In Zori, there can only be a single root level widget active.
  241. * It's ID is always 0;
  242. */
  243. struct zori_root {
  244. /* A root is a widget. */
  245. struct zori_widget widget;
  246. /* Current active screen widget if any. */
  247. struct zori_screen * active_screen;
  248. };
  249. /* Forward declaration of a page. */
  250. struct zori_page;
  251. struct zori_root * zori_get_root(void);
  252. /* Initializes Zori and creates a top level widget. Returns 0 on success
  253. * or negative on error. The style will be copied and set as default
  254. * if it is not NULL. Otherwise a built-in style will be used.
  255. * Not that ZORI will NOT clean up any images or fonts it uses by itself.
  256. */
  257. zori_id zori_start(struct zori_style * default_style);
  258. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  259. * negative on error.
  260. */
  261. zori_id zori_shutdown();
  262. /* Creates a new screen widget. Normally this should be the first widget
  263. * you create after zori_start. */
  264. /* Activates the page on it's display. All other pages are dectivated and
  265. * hidden. */
  266. zori_id zori_activate_page(zori_id page);
  267. /* Creates a new generic widget on the given screen with the given
  268. * dimensions. */
  269. zori_id zori_new(zori_id screen, zori_rebox * box);
  270. /* Sets the flags of a widget. */
  271. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  272. /* Sets the whole style of a widget. */
  273. zori_id zori_set_style(zori_id id, struct zori_style * style);
  274. /* Sets the background color of the widget. */
  275. zori_id zori_set_background_color(zori_id id, zori_color color);
  276. /* Sets the foreground color of the widget. */
  277. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  278. /* Creates a new frame widget. */
  279. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  280. /* Creates a new (vertical) menu widget. */
  281. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  282. /* Creates a new button widget. */
  283. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  284. /* Creates a new conversation widget. */
  285. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  286. /* Draws the whole UI and all visible parts. */
  287. void zori_draw_all(void);
  288. /* Updates the state of the UI. Pass in the time passed since last update. */
  289. void zori_update(double dt);
  290. /* Registers an event handler for a widget. */
  291. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  292. int zori_registry_entry_compare(const void *v1, const void *v2);
  293. zori_id zori_registry_init(struct zori_registry *registry);
  294. zori_id zori_registry_add(struct zori_registry *registry, zori_id id, struct zori_widget *widget);
  295. struct zori_registry_entry *zori_registry_lookup_entry(struct zori_registry *registry, zori_id id);
  296. struct zori_widget *zori_registry_lookup(struct zori_registry *registry, zori_id id);
  297. zori_id zori_registry_remove(struct zori_registry *registry, zori_id id);
  298. int zori_handler_compare(const void *v1, const void *v2);
  299. struct zori_handler *zori_handlers_add(struct zori_handlers *me, zori_event_type type, zori_handler_func *handler, void *data);
  300. void zori_handlers_done(struct zori_handlers *me);
  301. void zori_handlers_init(struct zori_handlers *me);
  302. struct zori_handler *zori_handlers_search(struct zori_handlers *me, zori_event_type type);
  303. int zori_handlers_handle(struct zori_handlers *me, union zori_event *event);
  304. int zori_widget_raise_system_event(struct zori_widget *widget, zori_system_event *sysev);
  305. int zori_widget_raise_draw_event(struct zori_widget *widget);
  306. int zori_widget_raise_done_event(struct zori_widget *widget);
  307. int zori_widget_raise_free_event(struct zori_widget *widget);
  308. int zori_widget_raise_update_event(struct zori_widget *widget, double dt);
  309. int zori_widget_raise_action_event(struct zori_widget *widget);
  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