zori.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_root_widget(void) {
  62. if (!the_zori_root) {
  63. return NULL;
  64. }
  65. return &the_zori_root->widget;
  66. }
  67. struct zori_widget * zori_get_widget(zori_id id) {
  68. struct zori_registry * registry = zori_get_registry();
  69. if (!registry) return NULL;
  70. return zori_registry_lookup(registry, id);
  71. }
  72. zori_id zori_get_unused_id(void) {
  73. zori_id id = -1;
  74. struct zori_widget * found;
  75. do {
  76. id++;
  77. if (id == INT_MAX) { return ZORI_ID_ERROR; }
  78. found = zori_get_widget(id);
  79. } while(found);
  80. return id;
  81. }
  82. zori_id zori_initialize_root(void) {
  83. if (the_zori_root) return ZORI_ID_OK;
  84. the_zori_root = calloc(1, sizeof(*the_zori_root));
  85. if (!the_zori_root) return ZORI_ID_ENOMEM;
  86. return ZORI_ID_OK;
  87. }
  88. zori_id zori_start(struct zori_style * default_style) {
  89. zori_id res;
  90. struct zori_root * root = NULL;
  91. root = zori_get_root();
  92. if (root) return ZORI_ID_OK;
  93. res = zori_initialize_default_style();
  94. if (!ZORI_ID_OK_P(res)) return res;
  95. res = zori_initialize_registry();
  96. if (!ZORI_ID_OK_P(res)) return res;
  97. res = zori_initialize_root();
  98. if (!ZORI_ID_OK_P(res)) return res;
  99. root = zori_get_root();
  100. if (default_style) {
  101. root->widget.style = *default_style;
  102. } else {
  103. root->widget.style = *zori_get_default_style();
  104. }
  105. root->widget.id = 0;
  106. zori_registry_add(zori_get_registry(), the_zori_root->widget.id, &the_zori_root->widget);
  107. return the_zori_root->widget.id;
  108. }
  109. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom) {
  110. struct zori_widget * widget = zori_get_widget(id);
  111. return zori_widget_margins_(widget, left, top, right, bottom);
  112. }
  113. zori_id zori_set_margin(zori_id id, int size) {
  114. struct zori_widget * widget = zori_get_widget(id);
  115. return zori_widget_margin_(widget, size);
  116. }
  117. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom) {
  118. struct zori_widget * widget = zori_get_widget(id);
  119. return zori_widget_paddings_(widget, left, top, right, bottom);
  120. }
  121. zori_id zori_set_padding(zori_id id, int size) {
  122. struct zori_widget * widget = zori_get_widget(id);
  123. return zori_widget_padding_(widget, size);
  124. }
  125. zori_font * zori_text_font(zori_id id) {
  126. struct zori_widget * widget = zori_get_widget(id);
  127. return zori_widget_text_font(widget);
  128. }
  129. /** Destroy the Zori root widget */
  130. void zori_destroy_root(void) {
  131. if (the_zori_root) {
  132. zori_widget_free(&the_zori_root->widget);
  133. }
  134. /* free(the_zori_root); is not needed, as it is cleaned up a sa widget */
  135. the_zori_root = NULL;
  136. }
  137. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  138. * negative on error.
  139. */
  140. zori_id zori_shutdown() {
  141. zori_destroy_root();
  142. zori_destroy_default_style();
  143. zori_destroy_registry();
  144. return ZORI_ID_OK;
  145. }
  146. /* Creates a new screen widget. Normally this should be the first widget
  147. * you create after zori_start. */
  148. zori_id zori_new_screen(zori_id id, zori_display * display);
  149. /* Creates a new page widget on the given screen. The page is not
  150. * made the active page, unless if it is the first one created. */
  151. zori_id zori_new_page(zori_id id, zori_id screen);
  152. /* Activates the page on it's display. All other pages are dectivated and
  153. * hidden. */
  154. zori_id zori_activate_page(zori_id page);
  155. /* Creates a new generic widget on the given screen with the given
  156. * dimensions. */
  157. zori_id zori_new(zori_id screen, zori_rebox * box);
  158. /* Sets the flags of a widget. */
  159. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  160. /* Sets the whole style of a widget. */
  161. zori_id zori_set_style(zori_id id, struct zori_style * style);
  162. /* Sets the background color of the widget. */
  163. zori_id zori_set_background_color(zori_id id, zori_color color);
  164. /* Sets the foreground color of the widget. */
  165. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  166. /* Creates a new frame widget. */
  167. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  168. /* Creates a new (vertical) menu widget. */
  169. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  170. /* Creates a new button widget. */
  171. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  172. /* Creates a new conversation widget. */
  173. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  174. /* Draws the whole UI and all visible parts. */
  175. void zori_draw_all(void) {
  176. zori_widget_raise_draw_event(&the_zori_root->widget);
  177. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  178. }
  179. /* Dispatches system events throughout the GUI. */
  180. int zori_handle_system_event(zori_system_event * sysev) {
  181. /* All events are passed on to the console regardless if it is active.
  182. * Otherwise, the events go into the system. */
  183. if (!the_zori_root) {
  184. return ZORI_ID_EINVAL;
  185. }
  186. if (the_zori_root && the_zori_root->console) {
  187. if (zori_console_active(the_zori_root->console)) {
  188. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  189. if (!zori_propagate_event_p(res)) return res;
  190. }
  191. }
  192. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  193. }
  194. /* Updates the state of the UI. Pass in the time passed since last update. */
  195. void zori_update(double dt)
  196. {
  197. struct zori_root * root = zori_get_root();
  198. zori_widget_raise_update_event(&root->widget, dt);
  199. }
  200. /** Returns the "result" of a widget. 0 if no result was available
  201. * because the widget was not "ready" or not "clicked" yet.
  202. * Result will be negative if the widget was closed.
  203. */
  204. int zori_result(zori_id id) {
  205. int result = 0;
  206. struct zori_widget * widget = zori_get_widget(id);
  207. if (widget) {
  208. if (widget->result.ready) {
  209. switch (widget->result.type) {
  210. case ZORI_RESULT_TYPE_INTEGER:
  211. result = widget->result.value.integer;
  212. break;
  213. default:
  214. result = - widget->result.type;
  215. break;
  216. }
  217. widget->result.ready = 0;
  218. }
  219. }
  220. return result;
  221. }
  222. /** Returns the "result" of a widget. 0 if no result was available
  223. * because the widget was not "ready" or not "clicked" yet. */
  224. int zori_get_result(zori_id id, struct zori_result * result) {
  225. struct zori_widget * widget = zori_get_widget(id);
  226. if (widget && result) {
  227. if (widget->result.ready) {
  228. (*result) = widget->result;
  229. widget->result.ready = 0;
  230. return result->ready;
  231. }
  232. }
  233. return 0;
  234. }
  235. int zori_result_int(zori_id id, int * result);
  236. int zori_result_string(zori_id id, char ** result, size_t * size);
  237. int zori_result_buffer(zori_id id, char ** result, size_t * size);