zori.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #ifndef zori_H_INCLUDED
  2. #define zori_H_INCLUDED
  3. #include "eruta.h"
  4. #include "rebox.h"
  5. #include "miao.h"
  6. /* Typedefs for possible later portability. */
  7. typedef ALLEGRO_COLOR zori_color;
  8. typedef ALLEGRO_BITMAP zori_bitmap;
  9. typedef ALLEGRO_FONT zori_font;
  10. typedef ALLEGRO_EVENT zori_system_event;
  11. typedef ALLEGRO_EVENT_TYPE zori_event_type;
  12. typedef ALLEGRO_DISPLAY zori_display;
  13. typedef Point zori_point;
  14. typedef Rebox zori_rebox;
  15. typedef int zori_id;
  16. #define ZORI_ID_OK_P(ID) ((ID) > -1)
  17. #define ZORI_ID_OK ((zori_id)(0))
  18. #define ZORI_ID_ERROR ((zori_id)(-1))
  19. #define ZORI_ID_ENOMEM ((zori_id)(-2))
  20. #define ZORI_ID_EINVAL ((zori_id)(-3))
  21. /* Macro: ZORI_CONTAINER_OF(PTR, TYPE, MEMBER)
  22. This macro returns, for the given pointer, a pointer to a containing struct
  23. of type TYPE, in which PTR is a member named MEMBER.
  24. This enables cool ways of type genericity and extension in plain C.
  25. It does not run afoul of strict aliasing since it passes over a char * pointer
  26. and a pointer of a containing struct or union.
  27. */
  28. #define ZORI_CONTAINER_OF(PTR, TYPE, MEMBER) \
  29. ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, MEMBER)))
  30. /** Custom event types, used in conjunction with Allegro event types. */
  31. enum zori_custom_event_type {
  32. ZORI_EVENT_CUSTOM = ALLEGRO_GET_EVENT_TYPE('z', 'o', 'r', 'i'),
  33. ZORI_EVENT_UPDATE,
  34. ZORI_EVENT_DRAW,
  35. };
  36. struct zori_widget;
  37. struct zori_event {
  38. zori_system_event sysev;
  39. struct zori_widget * widget;
  40. void * data;
  41. };
  42. struct zori_stylepart {
  43. zori_color color;
  44. zori_bitmap * image;
  45. zori_font * font;
  46. };
  47. struct zori_style {
  48. struct zori_stylepart fore;
  49. struct zori_stylepart back;
  50. struct zori_stylepart text;
  51. struct zori_stylepart border;
  52. };
  53. struct zori_widget;
  54. typedef int zori_handler_func(struct zori_event * event);
  55. /* A single event handler */
  56. struct zori_handler {
  57. zori_event_type type;
  58. zori_handler_func * handler;
  59. void * data;
  60. };
  61. /* A dynamic array of event handlers event handlers. */
  62. struct zori_handlers miao_of_type(struct zori_handler);
  63. /* An entry in a widget registry. */
  64. struct zori_registry_entry {
  65. zori_id id;
  66. struct zori_widget * widget;
  67. };
  68. /* A widget registry as a dynamic array of entries. */
  69. struct zori_registry miao_of_type(struct zori_registry_entry);
  70. /* Generic flags for several zori structs. */
  71. enum zori_flag {
  72. /* The object is not visible, though it may still be interacted with.*/
  73. ZORI_FLAG_HIDDEN = 1 << 0,
  74. /* The object cannot be interacted with, though it is still visible. */
  75. ZORI_FLAG_DISABLED = 1 << 1,
  76. /* The object is both hidden and disabled. */
  77. ZORI_FLAG_DEACTIVATED = ZORI_FLAG_HIDDEN | ZORI_FLAG_DISABLED,
  78. };
  79. /* Mouse or keyboard/joystick cursor. */
  80. struct zori_cursor {
  81. zori_point p;
  82. struct zori_widget * hover;
  83. struct zori_widget * focus;
  84. zori_bitmap * bitmap;
  85. enum zori_flag flags;
  86. };
  87. /* Support multiple cursors... */
  88. struct zori_cursors {
  89. struct zori_cursor mouse;
  90. struct zori_cursor keyjoy;
  91. };
  92. /*
  93. on_enter
  94. on_enter(data = {})
  95. on_event(*args)
  96. on_event(*data)
  97. on_key_down(*args)
  98. on_leave(name=nil)
  99. on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  100. on_mouse_button_down(t, x, y, z, w, b)
  101. on_mouse_button_up(t, x, y, z, w, b)
  102. on_mouse_in(x, y, from)
  103. on_mouse_out(x, y, to)
  104. on_resize
  105. */
  106. struct zori_widget {
  107. /* ID of the widget, used in most external API's. */
  108. zori_id id;
  109. /* Root level widget under which this widget is active. */
  110. struct zori_widget * root;
  111. /* Position and size of the widget. */
  112. zori_rebox box;
  113. /* Z ordering. */
  114. int z;
  115. /* Style. */
  116. struct zori_style style;
  117. /* Handlers. */
  118. struct zori_handlers handlers;
  119. /* Related widgets. */
  120. struct zori_widget * parent;
  121. struct zori_widget * child;
  122. struct zori_widget * sibling;
  123. /* Flags. */
  124. enum zori_flag flags;
  125. };
  126. /* An array of widget pointers. */
  127. struct zori_widget_array miao_of_type(struct zori_widget *);
  128. /* forward declaration. */
  129. struct zori_screen;
  130. /*
  131. * Root level widget, my spread out over several displays.
  132. * In Zori, there can only be a single root level widget active.
  133. * It's ID is always 0;
  134. */
  135. struct zori_root {
  136. /* A root is a widget. */
  137. struct zori_widget widget;
  138. /* It has an array of all GUI widgets it manages. */
  139. struct zori_widget_array * widgets;
  140. /* Current active screen widget if any. */
  141. struct zori_screen * active_screen;
  142. };
  143. /* Forward declaration of a page. */
  144. struct zori_page;
  145. /* The top level widget for a single display. */
  146. struct zori_screen {
  147. /* A screen is a widget. */
  148. struct zori_widget widget;
  149. /* It also manages the cursors. */
  150. struct zori_cursors cursors;
  151. /* Display this screen is on. */
  152. zori_display * display;
  153. /* The GUI page that is active on this screen if any. */
  154. struct zori_page * active_page;
  155. };
  156. /* In Zori, the GUI is paginated. This means that on any
  157. * screen, only a single GUI page can be active. The intent is to
  158. * support different GUI modes such as startup screen, status view,
  159. * settings, HUD, and so on between which can be switched easily. */
  160. struct zori_page {
  161. /* A page is a widget. */
  162. struct zori_widget widget;
  163. };
  164. /* Initializes Zori and creates a top level widget. Returns 0 on success
  165. * or negative on error. The style will be copied and set as default
  166. * if it is not NULL. Otherwise a built-in style will be used.
  167. * Not that ZORI will NOT clean up any images or fonts it uses by itself.
  168. */
  169. zori_id zori_start(struct zori_style * default_style);
  170. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  171. * negative on error.
  172. */
  173. zori_id zori_shutdown();
  174. /* Creates a new screen widget. Normally this should be the first widget
  175. * you create after zori_start. */
  176. zori_id zori_new_screen(zori_display * display);
  177. /* Creates a new page widget on the given screen. The page is not
  178. * made the active page, unless if it is the first one created. */
  179. zori_id zori_new_page(zori_id screen);
  180. /* Activates the page on it's display. All other pages are dectivated and
  181. * hidden. */
  182. zori_id zori_activate_page(zori_id page);
  183. /* Creates a new generic widget on the given screen with the given
  184. * dimensions. */
  185. zori_id zori_new(zori_id screen, zori_rebox * box);
  186. /* Sets the flags of a widget. */
  187. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  188. /* Sets the whole style of a widget. */
  189. zori_id zori_set_style(zori_id id, struct zori_style * style);
  190. /* Sets the background color of the widget. */
  191. zori_id zori_set_background_color(zori_id id, zori_color color);
  192. /* Sets the foreground color of the widget. */
  193. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  194. /* Creates a new frame widget. */
  195. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  196. /* Creates a new (vertical) menu widget. */
  197. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  198. /* Creates a new button widget. */
  199. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  200. /* Creates a new conversation widget. */
  201. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  202. /* Draws the whole UI and all visible parts. */
  203. void zori_draw_all(void);
  204. /* Updates the state of the UI. Pass in the time passed since last update. */
  205. void zori_update(double dt);
  206. /* Registers an event handler for a widget. */
  207. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  208. #endif