zori.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* Magic comment for runcprotoall: @generate_cproto@ */
  2. #include "zori.h"
  3. #include "zori_registry.h"
  4. #include "zori_style.h"
  5. #include "zori_widget.h"
  6. #include "zori_console.h"
  7. #include "zori_screen.h"
  8. #include "miao.h"
  9. #include <allegro5/allegro_color.h>
  10. #include "str.h"
  11. #include "draw.h"
  12. #include "monolog.h"
  13. /*
  14. * Pardon the pun name, but Zori is the submodule that handles the user
  15. * interface and the menus.
  16. */
  17. /** Handler functionality */
  18. int zori_handler_compare(const void * v1, const void * v2) {
  19. const struct zori_handler * h1 = v1;
  20. const struct zori_handler * h2 = v2;
  21. if (h1->type < h2->type) return -1;
  22. if (h1->type > h2->type) return 1;
  23. return 0;
  24. }
  25. struct zori_handler * zori_handlers_add(struct zori_handlers * me, zori_event_type type, zori_handler_func * handler, void * data) {
  26. struct zori_handler * result = NULL;
  27. result = miao_push_ptr(me);
  28. if (result) {
  29. result->type = type;
  30. result->handler = handler;
  31. result->data = data;
  32. }
  33. miao_qsort(me, zori_handler_compare);
  34. return result;
  35. }
  36. void zori_handlers_done(struct zori_handlers * me) {
  37. miao_done(me);
  38. }
  39. void zori_handlers_init(struct zori_handlers * me) {
  40. miao_init(me);
  41. }
  42. struct zori_handler * zori_handlers_search(struct zori_handlers * me, zori_event_type type) {
  43. struct zori_handler * result;
  44. struct zori_handler key;
  45. key.type = type;
  46. key.handler = NULL;
  47. key.data = NULL;
  48. if (!me) return NULL;
  49. result = miao_bsearch(me, zori_handler_compare, &key);
  50. return result;
  51. }
  52. int zori_handlers_handle(struct zori_handlers * me, union zori_event * event) {
  53. struct zori_handler * handler = zori_handlers_search(me, event->type);
  54. if (!handler) return ZORI_HANDLE_IGNORE;
  55. event->any.data = handler->data;
  56. return handler->handler(event);
  57. }
  58. static struct zori_root * the_zori_root = NULL;
  59. struct zori_root * zori_get_root(void) {
  60. return the_zori_root;
  61. }
  62. struct zori_widget * zori_get_root_widget(void) {
  63. if (!the_zori_root) {
  64. return NULL;
  65. }
  66. return &the_zori_root->widget;
  67. }
  68. struct zori_widget * zori_get_widget(zori_id id) {
  69. struct zori_registry * registry = zori_get_registry();
  70. if (!registry) return NULL;
  71. return zori_registry_lookup(registry, id);
  72. }
  73. zori_id zori_get_unused_id(void) {
  74. zori_id id = -1;
  75. struct zori_widget * found;
  76. do {
  77. id++;
  78. if (id == INT_MAX) { return ZORI_ID_ERROR; }
  79. found = zori_get_widget(id);
  80. } while(found);
  81. return id;
  82. }
  83. zori_id zori_initialize_root(void) {
  84. if (the_zori_root) return ZORI_ID_OK;
  85. the_zori_root = calloc(1, sizeof(*the_zori_root));
  86. if (!the_zori_root) return ZORI_ID_ENOMEM;
  87. return ZORI_ID_OK;
  88. }
  89. zori_id zori_start(struct zori_style * default_style) {
  90. zori_id res;
  91. struct zori_root * root = NULL;
  92. root = zori_get_root();
  93. if (root) return ZORI_ID_OK;
  94. res = zori_initialize_default_style();
  95. if (!ZORI_ID_OK_P(res)) return res;
  96. res = zori_initialize_registry();
  97. if (!ZORI_ID_OK_P(res)) return res;
  98. res = zori_initialize_root();
  99. if (!ZORI_ID_OK_P(res)) return res;
  100. root = zori_get_root();
  101. if (default_style) {
  102. root->widget.style = *default_style;
  103. } else {
  104. root->widget.style = *zori_get_default_style();
  105. }
  106. root->widget.id = 0;
  107. zori_registry_add(zori_get_registry(), the_zori_root->widget.id, &the_zori_root->widget);
  108. return the_zori_root->widget.id;
  109. }
  110. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom) {
  111. struct zori_widget * widget = zori_get_widget(id);
  112. return zori_widget_margins_(widget, left, top, right, bottom);
  113. }
  114. zori_id zori_set_margin(zori_id id, int size) {
  115. struct zori_widget * widget = zori_get_widget(id);
  116. return zori_widget_margin_(widget, size);
  117. }
  118. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom) {
  119. struct zori_widget * widget = zori_get_widget(id);
  120. return zori_widget_paddings_(widget, left, top, right, bottom);
  121. }
  122. zori_id zori_set_padding(zori_id id, int size) {
  123. struct zori_widget * widget = zori_get_widget(id);
  124. return zori_widget_padding_(widget, size);
  125. }
  126. zori_font * zori_text_font(zori_id id) {
  127. struct zori_widget * widget = zori_get_widget(id);
  128. return zori_widget_text_font(widget);
  129. }
  130. /** Destroy the Zori root widget */
  131. void zori_destroy_root(void) {
  132. if (the_zori_root) {
  133. zori_widget_free(&the_zori_root->widget);
  134. }
  135. /* free(the_zori_root); is not needed, as it is cleaned up a sa widget */
  136. the_zori_root = NULL;
  137. }
  138. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  139. * negative on error.
  140. */
  141. zori_id zori_shutdown() {
  142. zori_destroy_root();
  143. zori_destroy_default_style();
  144. zori_destroy_registry();
  145. return ZORI_ID_OK;
  146. }
  147. /* Creates a new screen widget. Normally this should be the first widget
  148. * you create after zori_start. */
  149. zori_id zori_new_screen(zori_id id, zori_display * display);
  150. /* Creates a new page widget on the given screen. The page is not
  151. * made the active page, unless if it is the first one created. */
  152. zori_id zori_new_page(zori_id id, zori_id screen);
  153. /* Activates the page on it's display. All other pages are dectivated and
  154. * hidden. */
  155. zori_id zori_activate_page(zori_id page);
  156. /* Creates a new generic widget on the given screen with the given
  157. * dimensions. */
  158. zori_id zori_new(zori_id screen, zori_rebox * box);
  159. /* Sets the flags of a widget. */
  160. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  161. /* Sets the whole style of a widget. */
  162. zori_id zori_set_style(zori_id id, struct zori_style * style) {
  163. struct zori_widget * widget = zori_get_widget(id);
  164. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  165. widget->style = *style;
  166. return widget->id;
  167. }
  168. /* Gets the whole style of a widget. */
  169. zori_id zori_get_style(zori_id id, struct zori_style * style) {
  170. struct zori_widget * widget = zori_get_widget(id);
  171. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  172. *style = widget->style;
  173. return widget->id;
  174. }
  175. /* Sets the background color of the widget. */
  176. zori_id zori_set_background_color(zori_id id, zori_color color) {
  177. struct zori_widget * widget = zori_get_widget(id);
  178. if (!widget) return ZORI_ID_ERROR;
  179. widget->style.back.color = color;
  180. return widget->id;
  181. }
  182. /* Sets the background bitmap of the widget. */
  183. zori_id zori_set_background_bitmap(zori_id id, zori_bitmap * bitmap) {
  184. struct zori_widget * widget = zori_get_widget(id);
  185. if (!widget) return ZORI_ID_ERROR;
  186. widget->style.back.image = bitmap;
  187. return widget->id;
  188. }
  189. /* Sets the border color of the widget. */
  190. zori_id zori_set_border_color(zori_id id, zori_color color) {
  191. struct zori_widget * widget = zori_get_widget(id);
  192. if (!widget) return ZORI_ID_ERROR;
  193. widget->style.border.color = color;
  194. return widget->id;
  195. }
  196. /* Sets the border bitmap of the widget. */
  197. zori_id zori_set_border_bitmap(zori_id id, zori_bitmap * bitmap) {
  198. struct zori_widget * widget = zori_get_widget(id);
  199. if (!widget) return ZORI_ID_ERROR;
  200. widget->style.border.image = bitmap;
  201. return widget->id;
  202. }
  203. /* Sets the text font of the widget. */
  204. zori_id zori_set_text_font(zori_id id, zori_font * font) {
  205. struct zori_widget * widget = zori_get_widget(id);
  206. if (!widget) return ZORI_ID_ERROR;
  207. widget->style.text.font = font;
  208. return widget->id;
  209. }
  210. /* Sets the text font flags of the widget. */
  211. zori_id zori_set_text_font_flags(zori_id id, int flags) {
  212. struct zori_widget * widget = zori_get_widget(id);
  213. if (!widget) return ZORI_ID_ERROR;
  214. widget->style.text.flags = flags;
  215. return widget->id;
  216. }
  217. /* Creates a new frame widget. */
  218. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  219. /* Creates a new (vertical) menu widget. */
  220. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  221. /* Creates a new button widget. */
  222. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  223. /* Creates a new conversation widget. */
  224. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  225. /* Draws the whole UI and all visible parts. */
  226. void zori_draw_all(void) {
  227. zori_widget_raise_draw_event(&the_zori_root->widget);
  228. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  229. }
  230. /* Dispatches system events throughout the GUI. */
  231. int zori_handle_system_event(zori_system_event * sysev) {
  232. /* All events are passed on to the console regardless if it is active.
  233. * Otherwise, theget events go into the system. */
  234. if (!the_zori_root) {
  235. return ZORI_ID_EINVAL;
  236. }
  237. if (the_zori_root && the_zori_root->console) {
  238. if (zori_console_active(the_zori_root->console)) {
  239. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  240. if (!zori_propagate_event_p(res)) return res;
  241. }
  242. }
  243. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  244. }
  245. /* Updates the state of the UI. Pass in the time passed since last update. */
  246. void zori_update(double dt)
  247. {
  248. struct zori_root * root = zori_get_root();
  249. zori_widget_raise_update_event(&root->widget, dt);
  250. }
  251. /** Returns the "result" of a widget. 0 if no result was available
  252. * because the widget was not "ready" or not "clicked" yet.
  253. * Result will be negative if the widget was closed.
  254. */
  255. int zori_result(zori_id id) {
  256. int result = 0;
  257. struct zori_widget * widget = zori_get_widget(id);
  258. if (widget) {
  259. if (widget->result.ready) {
  260. switch (widget->result.type) {
  261. case ZORI_RESULT_TYPE_INTEGER:
  262. result = widget->result.value.integer;
  263. break;
  264. default:
  265. result = - widget->result.type;
  266. break;
  267. }
  268. widget->result.ready = 0;
  269. }
  270. }
  271. return result;
  272. }
  273. /** Returns the "result" of a widget. 0 if no result was available
  274. * because the widget was not "ready" or not "clicked" yet. */
  275. int zori_get_result(zori_id id, struct zori_result * result) {
  276. struct zori_widget * widget = zori_get_widget(id);
  277. if (widget && result) {
  278. if (widget->result.ready) {
  279. (*result) = widget->result;
  280. widget->result.ready = 0;
  281. return result->ready;
  282. }
  283. }
  284. return 0;
  285. }
  286. int zori_result_int(zori_id id, int * result);
  287. int zori_result_string(zori_id id, char ** result, size_t * size);
  288. int zori_result_buffer(zori_id id, char ** result, size_t * size);