zori.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "zori.h"
  2. #include "miao.h"
  3. #include <allegro5/allegro_color.h>
  4. /*
  5. * Pardon the pun name, but Zori is the submodule that handles the user
  6. * interface and the menus.
  7. */
  8. /* registry functionality */
  9. /** Compare registry entries. */
  10. int zori_registry_entry_compare(const void * v1, const void * v2) {
  11. const struct zori_registry_entry * entry1 = v1;
  12. const struct zori_registry_entry * entry2 = v2;
  13. return (entry2->id - entry1->id);
  14. }
  15. /** Initialize the registry. */
  16. zori_id zori_registry_init(struct zori_registry * registry) {
  17. miao_init(registry);
  18. return ZORI_ID_OK;
  19. }
  20. /** Add an entry to the registry. */
  21. zori_id
  22. zori_registry_add(struct zori_registry * registry, zori_id id,
  23. struct zori_widget * widget) {
  24. struct zori_registry_entry entry = { id, widget };
  25. if (miao_push(registry, entry)) {
  26. miao_qsort(registry, zori_registry_entry_compare);
  27. return ZORI_ID_OK;
  28. }
  29. return ZORI_ID_ENOMEM;
  30. }
  31. /** Look up an entry in the registry. */
  32. struct zori_registry_entry *
  33. zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) {
  34. struct zori_registry_entry key = { id, NULL };
  35. return miao_bsearch(registry, zori_registry_entry_compare, &key);
  36. }
  37. /** Look up a widget in the registry. */
  38. struct zori_widget *
  39. zori_registry_lookup(struct zori_registry * registry, zori_id id) {
  40. struct zori_widget * result = NULL;
  41. struct zori_registry_entry * entry = NULL;
  42. entry = zori_registry_lookup_entry(registry, id);
  43. if (entry) {
  44. result = entry->widget;
  45. }
  46. return result;
  47. }
  48. /** Remove an entry from the registry. */
  49. zori_id
  50. zori_registry_remove(struct zori_registry * registry, zori_id id) {
  51. struct zori_registry_entry * entry = NULL;
  52. entry = zori_registry_lookup_entry(registry, id);
  53. if (entry) {
  54. miao_delete_entry(registry, entry);
  55. }
  56. return ZORI_ID_OK;
  57. }
  58. /** Handler functionality */
  59. int zori_handler_compare(const void * v1, const void * v2) {
  60. const struct zori_handler * h1 = v1;
  61. const struct zori_handler * h2 = v2;
  62. if (h1->type < h2->type) return -1;
  63. if (h1->type > h2->type) return 1;
  64. return 0;
  65. }
  66. struct zori_handler * zori_handlers_add(struct zori_handlers * me, zori_event_type type, zori_handler_func * handler, void * data) {
  67. struct zori_handler * result = NULL;
  68. result = miao_push_ptr(me);
  69. if (result) {
  70. result->type = type;
  71. result->handler = handler;
  72. result->data = data;
  73. }
  74. miao_qsort(me, zori_handler_compare);
  75. return result;
  76. }
  77. void zori_handlers_done(struct zori_handlers * me) {
  78. miao_done(me);
  79. }
  80. void zori_handlers_init(struct zori_handlers * me) {
  81. miao_init(me);
  82. }
  83. struct zori_handler * zori_handlers_search(struct zori_handlers * me, zori_event_type type) {
  84. struct zori_handler * result;
  85. struct zori_handler key;
  86. key.type = type;
  87. key.handler = NULL;
  88. key.data = NULL;
  89. if (!me) return NULL;
  90. result = miao_bsearch(me, zori_handler_compare, &key);
  91. return result;
  92. }
  93. int zori_handlers_handle(struct zori_handlers * me, struct zori_event * event, struct zori_widget * widget) {
  94. struct zori_handler * handler = zori_handlers_search(me, event->sysev.type);
  95. if (!handler) return 0;
  96. return handler->handler(event);
  97. }
  98. static struct zori_root * the_zori_root = NULL;
  99. static struct zori_style * the_default_style = NULL;
  100. static struct zori_registry * the_zori_registry = NULL;
  101. int zori_widget_compare(const void * v1, const void * v2) {
  102. const struct zori_widget * w1 = v1, * w2 = v2;
  103. return (w2->id - w1->id);
  104. }
  105. struct zori_widget * zori_get_widget(zori_id id) {
  106. struct zori_widget key = {0};
  107. key.id = id;
  108. return miao_bsearch(the_zori_root->widgets, zori_widget_compare, &key);
  109. }
  110. zori_id zori_get_unused_id(void) {
  111. zori_id id = -1;
  112. struct zori_widget * found;
  113. do {
  114. id++;
  115. if (id == INT_MAX) { return ZORI_ID_ERROR; }
  116. found = zori_get_widget(id);
  117. } while(found);
  118. return id;
  119. }
  120. zori_id zori_start(struct zori_style * default_style) {
  121. if (the_zori_root) return ZORI_ID_OK;
  122. the_zori_root = calloc(1, sizeof(*the_zori_root));
  123. if (!the_zori_root) return ZORI_ID_ENOMEM;
  124. the_default_style = calloc(1, sizeof(*the_default_style));
  125. if (!the_default_style) return ZORI_ID_ENOMEM;
  126. the_zori_registry = calloc(1, sizeof(*the_zori_registry));
  127. if (!the_zori_registry) return ZORI_ID_ENOMEM;
  128. the_default_style->text.font = al_create_builtin_font();
  129. the_default_style->text.color = al_color_name("white");
  130. the_default_style->border.color = al_color_name("white");
  131. the_default_style->back.color = al_color_name("green");
  132. if (default_style) {
  133. the_zori_root->widget.style = *default_style;
  134. } else {
  135. the_zori_root->widget.style = *the_default_style;
  136. }
  137. miao_init(the_zori_root->widgets);
  138. the_zori_root->widget.id = 0;
  139. zori_registry_add(the_zori_registry, the_zori_root->widget.id, &the_zori_root->widget);
  140. /*
  141. * miao_push(the_zori_root->widgets, &the_zori_root->widget);
  142. * miao_qsort(the_zori_root->widgets, zori_widget_compare);
  143. */
  144. return the_zori_root->widget.id;
  145. }
  146. void zori_widget_free(struct zori_widget * widget);
  147. struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  148. struct zori_widget * aid, * next;
  149. if (!widget) return widget;
  150. if (widget->child) {
  151. /* Free all children. */
  152. aid = widget->child;
  153. while (aid) {
  154. next = aid->sibling;
  155. zori_widget_free(aid);
  156. aid = next;
  157. }
  158. }
  159. return widget;
  160. }
  161. void zori_widget_free(struct zori_widget * widget) {
  162. /* XXX remove from widget registry too... */
  163. return free(zori_widget_done(widget));
  164. }
  165. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  166. * negative on error.
  167. */
  168. zori_id zori_shutdown() {
  169. miao_done(the_zori_registry);
  170. free(the_zori_registry);
  171. the_zori_registry = NULL;
  172. free(the_default_style);
  173. the_default_style = NULL;
  174. assert((void *)(&the_zori_root->widget) == (void *)the_zori_root);
  175. zori_widget_free(&the_zori_root->widget);
  176. the_zori_root = NULL;
  177. return ZORI_ID_OK;
  178. }
  179. /* Creates a new screen widget. Normally this should be the first widget
  180. * you create after zori_start. */
  181. zori_id zori_new_screen(zori_display * display);
  182. /* Creates a new page widget on the given screen. The page is not
  183. * made the active page, unless if it is the first one created. */
  184. zori_id zori_new_page(zori_id screen);
  185. /* Activates the page on it's display. All other pages are dectivated and
  186. * hidden. */
  187. zori_id zori_activate_page(zori_id page);
  188. /* Creates a new generic widget on the given screen with the given
  189. * dimensions. */
  190. zori_id zori_new(zori_id screen, zori_rebox * box);
  191. /* Sets the flags of a widget. */
  192. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  193. /* Sets the whole style of a widget. */
  194. zori_id zori_set_style(zori_id id, struct zori_style * style);
  195. /* Sets the background color of the widget. */
  196. zori_id zori_set_background_color(zori_id id, zori_color color);
  197. /* Sets the foreground color of the widget. */
  198. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  199. /* Creates a new frame widget. */
  200. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  201. /* Creates a new (vertical) menu widget. */
  202. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  203. /* Creates a new button widget. */
  204. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  205. /* Creates a new conversation widget. */
  206. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  207. /* Draws the whole UI and all visible parts. */
  208. void zori_draw_all(void) {
  209. }
  210. /* Updates the state of the UI. Pass in the time passed since last update. */
  211. void zori_update(double dt)
  212. {
  213. struct zori_event event;
  214. event.sysev.type = ZORI_EVENT_UPDATE;
  215. event.sysev.user.data1 = (intptr_t)&dt;
  216. zori_handlers_handle(&the_zori_root->widget.handlers, &event, &the_zori_root->widget);
  217. }
  218. /* Registers an event handler for a widget. */
  219. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);