zori.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. struct zori_widget * widget = zori_get_widget(id);
  163. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  164. widget->style = *style;
  165. return widget->id;
  166. }
  167. /* Gets the whole style of a widget. */
  168. zori_id zori_get_style(zori_id id, struct zori_style * style) {
  169. struct zori_widget * widget = zori_get_widget(id);
  170. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  171. *style = widget->style;
  172. return widget->id;
  173. }
  174. /* Sets the background color of the widget. */
  175. zori_id zori_set_background_color(zori_id id, zori_color color) {
  176. struct zori_widget * widget = zori_get_widget(id);
  177. if (!widget) return ZORI_ID_ERROR;
  178. widget->style.back.color = color;
  179. return widget->id;
  180. }
  181. /* Sets the background bitmap of the widget. */
  182. zori_id zori_set_background_bitmap(zori_id id, zori_bitmap * bitmap) {
  183. struct zori_widget * widget = zori_get_widget(id);
  184. if (!widget) return ZORI_ID_ERROR;
  185. widget->style.back.image = bitmap;
  186. return widget->id;
  187. }
  188. /* Sets the border color of the widget. */
  189. zori_id zori_set_border_color(zori_id id, zori_color color) {
  190. struct zori_widget * widget = zori_get_widget(id);
  191. if (!widget) return ZORI_ID_ERROR;
  192. widget->style.border.color = color;
  193. return widget->id;
  194. }
  195. /* Sets the border bitmap of the widget. */
  196. zori_id zori_set_border_bitmap(zori_id id, zori_bitmap * bitmap) {
  197. struct zori_widget * widget = zori_get_widget(id);
  198. if (!widget) return ZORI_ID_ERROR;
  199. widget->style.border.image = bitmap;
  200. return widget->id;
  201. }
  202. /* Sets the text font of the widget. */
  203. zori_id zori_set_text_font(zori_id id, zori_font * font) {
  204. struct zori_widget * widget = zori_get_widget(id);
  205. if (!widget) return ZORI_ID_ERROR;
  206. widget->style.text.font = font;
  207. return widget->id;
  208. }
  209. /* Sets the text font flags of the widget. */
  210. zori_id zori_set_text_font_flags(zori_id id, int flags) {
  211. struct zori_widget * widget = zori_get_widget(id);
  212. if (!widget) return ZORI_ID_ERROR;
  213. widget->style.text.flags = flags;
  214. return widget->id;
  215. }
  216. /* Creates a new frame widget. */
  217. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  218. /* Creates a new (vertical) menu widget. */
  219. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  220. /* Creates a new button widget. */
  221. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  222. /* Creates a new conversation widget. */
  223. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  224. /* Draws the whole UI and all visible parts. */
  225. void zori_draw_all(void) {
  226. zori_widget_raise_draw_event(&the_zori_root->widget);
  227. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  228. }
  229. /* Dispatches system events throughout the GUI. */
  230. int zori_handle_system_event(zori_system_event * sysev) {
  231. /* All events are passed on to the console regardless if it is active.
  232. * Otherwise, theget events go into the system. */
  233. if (!the_zori_root) {
  234. return ZORI_ID_EINVAL;
  235. }
  236. if (the_zori_root && the_zori_root->console) {
  237. if (zori_console_active(the_zori_root->console)) {
  238. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  239. if (!zori_propagate_event_p(res)) return res;
  240. }
  241. }
  242. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  243. }
  244. /* Updates the state of the UI. Pass in the time passed since last update. */
  245. void zori_update(double dt)
  246. {
  247. struct zori_root * root = zori_get_root();
  248. zori_widget_raise_update_event(&root->widget, dt);
  249. }
  250. /** Returns the "result" of a widget. 0 if no result was available
  251. * because the widget was not "ready" or not "clicked" yet.
  252. * Result will be negative if the widget was closed.
  253. */
  254. int zori_result(zori_id id) {
  255. int result = 0;
  256. struct zori_widget * widget = zori_get_widget(id);
  257. if (widget) {
  258. if (widget->result.ready) {
  259. switch (widget->result.type) {
  260. case ZORI_RESULT_TYPE_INTEGER:
  261. result = widget->result.value.integer;
  262. break;
  263. default:
  264. result = - widget->result.type;
  265. break;
  266. }
  267. widget->result.ready = 0;
  268. }
  269. }
  270. return result;
  271. }
  272. /** Returns the "result" of a widget. 0 if no result was available
  273. * because the widget was not "ready" or not "clicked" yet. */
  274. int zori_get_result(zori_id id, struct zori_result * result) {
  275. struct zori_widget * widget = zori_get_widget(id);
  276. if (widget && result) {
  277. if (widget->result.ready) {
  278. (*result) = widget->result;
  279. widget->result.ready = 0;
  280. return result->ready;
  281. }
  282. }
  283. return 0;
  284. }
  285. int zori_result_int(zori_id id, int * result);
  286. int zori_result_string(zori_id id, char ** result, size_t * size);
  287. int zori_result_buffer(zori_id id, char ** result, size_t * size);