zori.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "zori.h"
  2. #include "zori_registry.h"
  3. #include "zori_style.h"
  4. #include "zori_widget.h"
  5. #include "zori_console.h"
  6. #include "zori_screen.h"
  7. #include "miao.h"
  8. #include <allegro5/allegro_color.h>
  9. #include "str.h"
  10. #include "draw.h"
  11. #include "monolog.h"
  12. /*
  13. * Pardon the pun name, but Zori is the submodule that handles the user
  14. * interface and the menus.
  15. */
  16. /** Handler functionality */
  17. int zori_handler_compare(const void * v1, const void * v2) {
  18. const struct zori_handler * h1 = v1;
  19. const struct zori_handler * h2 = v2;
  20. if (h1->type < h2->type) return -1;
  21. if (h1->type > h2->type) return 1;
  22. return 0;
  23. }
  24. struct zori_handler * zori_handlers_add(struct zori_handlers * me, zori_event_type type, zori_handler_func * handler, void * data) {
  25. struct zori_handler * result = NULL;
  26. result = miao_push_ptr(me);
  27. if (result) {
  28. result->type = type;
  29. result->handler = handler;
  30. result->data = data;
  31. }
  32. miao_qsort(me, zori_handler_compare);
  33. return result;
  34. }
  35. void zori_handlers_done(struct zori_handlers * me) {
  36. miao_done(me);
  37. }
  38. void zori_handlers_init(struct zori_handlers * me) {
  39. miao_init(me);
  40. }
  41. struct zori_handler * zori_handlers_search(struct zori_handlers * me, zori_event_type type) {
  42. struct zori_handler * result;
  43. struct zori_handler key;
  44. key.type = type;
  45. key.handler = NULL;
  46. key.data = NULL;
  47. if (!me) return NULL;
  48. result = miao_bsearch(me, zori_handler_compare, &key);
  49. return result;
  50. }
  51. int zori_handlers_handle(struct zori_handlers * me, union zori_event * event) {
  52. struct zori_handler * handler = zori_handlers_search(me, event->type);
  53. if (!handler) return ZORI_HANDLE_IGNORE;
  54. event->any.data = handler->data;
  55. return handler->handler(event);
  56. }
  57. static struct zori_root * the_zori_root = NULL;
  58. struct zori_root * zori_get_root(void) {
  59. return the_zori_root;
  60. }
  61. struct zori_widget * zori_get_widget(zori_id id) {
  62. struct zori_registry * registry = zori_get_registry();
  63. if (!registry) return NULL;
  64. return zori_registry_lookup(registry, id);
  65. }
  66. zori_id zori_get_unused_id(void) {
  67. zori_id id = -1;
  68. struct zori_widget * found;
  69. do {
  70. id++;
  71. if (id == INT_MAX) { return ZORI_ID_ERROR; }
  72. found = zori_get_widget(id);
  73. } while(found);
  74. return id;
  75. }
  76. zori_id zori_initialize_root(void) {
  77. if (the_zori_root) return ZORI_ID_OK;
  78. the_zori_root = calloc(1, sizeof(*the_zori_root));
  79. if (!the_zori_root) return ZORI_ID_ENOMEM;
  80. return ZORI_ID_OK;
  81. }
  82. zori_id zori_start(struct zori_style * default_style) {
  83. zori_id res;
  84. struct zori_root * root = NULL;
  85. root = zori_get_root();
  86. if (root) return ZORI_ID_OK;
  87. res = zori_initialize_default_style();
  88. if (!ZORI_ID_OK_P(res)) return res;
  89. res = zori_initialize_registry();
  90. if (!ZORI_ID_OK_P(res)) return res;
  91. res = zori_initialize_root();
  92. if (!ZORI_ID_OK_P(res)) return res;
  93. root = zori_get_root();
  94. if (default_style) {
  95. root->widget.style = *default_style;
  96. } else {
  97. root->widget.style = *zori_get_default_style();
  98. }
  99. root->widget.id = 0;
  100. zori_registry_add(zori_get_registry(), the_zori_root->widget.id, &the_zori_root->widget);
  101. return the_zori_root->widget.id;
  102. }
  103. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom) {
  104. struct zori_widget * widget = zori_get_widget(id);
  105. return zori_widget_margins_(widget, left, top, right, bottom);
  106. }
  107. zori_id zori_set_margin(zori_id id, int size) {
  108. struct zori_widget * widget = zori_get_widget(id);
  109. return zori_widget_margin_(widget, size);
  110. }
  111. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom) {
  112. struct zori_widget * widget = zori_get_widget(id);
  113. return zori_widget_paddings_(widget, left, top, right, bottom);
  114. }
  115. zori_id zori_set_padding(zori_id id, int size) {
  116. struct zori_widget * widget = zori_get_widget(id);
  117. return zori_widget_padding_(widget, size);
  118. }
  119. zori_font * zori_text_font(zori_id id) {
  120. struct zori_widget * widget = zori_get_widget(id);
  121. return zori_widget_text_font(widget);
  122. }
  123. /** Destroy the Zori root widget */
  124. void zori_destroy_root(void) {
  125. if (the_zori_root) {
  126. zori_widget_free(&the_zori_root->widget);
  127. }
  128. /* free(the_zori_root); is not needed, as it is cleaned up a sa widget */
  129. the_zori_root = NULL;
  130. }
  131. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  132. * negative on error.
  133. */
  134. zori_id zori_shutdown() {
  135. zori_destroy_root();
  136. zori_destroy_default_style();
  137. zori_destroy_registry();
  138. return ZORI_ID_OK;
  139. }
  140. /* Creates a new screen widget. Normally this should be the first widget
  141. * you create after zori_start. */
  142. zori_id zori_new_screen(zori_id id, zori_display * display);
  143. /* Creates a new page widget on the given screen. The page is not
  144. * made the active page, unless if it is the first one created. */
  145. zori_id zori_new_page(zori_id id, zori_id screen);
  146. /* Activates the page on it's display. All other pages are dectivated and
  147. * hidden. */
  148. zori_id zori_activate_page(zori_id page);
  149. /* Creates a new generic widget on the given screen with the given
  150. * dimensions. */
  151. zori_id zori_new(zori_id screen, zori_rebox * box);
  152. /* Sets the flags of a widget. */
  153. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  154. /* Sets the whole style of a widget. */
  155. zori_id zori_set_style(zori_id id, struct zori_style * style);
  156. /* Sets the background color of the widget. */
  157. zori_id zori_set_background_color(zori_id id, zori_color color);
  158. /* Sets the foreground color of the widget. */
  159. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  160. /* Creates a new frame widget. */
  161. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  162. /* Creates a new (vertical) menu widget. */
  163. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  164. /* Creates a new button widget. */
  165. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  166. /* Creates a new conversation widget. */
  167. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  168. /* Draws the whole UI and all visible parts. */
  169. void zori_draw_all(void) {
  170. zori_widget_raise_draw_event(&the_zori_root->widget);
  171. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  172. }
  173. /* Dispatches system events throughout the GUI. */
  174. int zori_handle_system_event(zori_system_event * sysev) {
  175. /* All events are passed on to the console regardless if it is active.
  176. * Otherwise, the events go into the system. */
  177. if (!the_zori_root) {
  178. return ZORI_ID_EINVAL;
  179. }
  180. if (the_zori_root && the_zori_root->console) {
  181. if (zori_console_active(the_zori_root->console)) {
  182. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  183. if (!zori_propagate_event_p(res)) return res;
  184. }
  185. }
  186. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  187. }
  188. /* Updates the state of the UI. Pass in the time passed since last update. */
  189. void zori_update(double dt)
  190. {
  191. struct zori_root * root = zori_get_root();
  192. zori_widget_raise_update_event(&root->widget, dt);
  193. }
  194. int zori_result(zori_id id) {
  195. int result = 0;
  196. struct zori_widget * widget = zori_get_widget(id);
  197. if (widget) {
  198. result = widget->result;
  199. widget->result = 0;
  200. }
  201. return result;
  202. }