zori.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. the_zori_root->style.icons.paused = NULL;
  88. return ZORI_ID_OK;
  89. }
  90. zori_id zori_start(struct zori_style * default_style) {
  91. zori_id res;
  92. struct zori_root * root = NULL;
  93. root = zori_get_root();
  94. if (root) return ZORI_ID_OK;
  95. res = zori_initialize_default_style();
  96. if (!ZORI_ID_OK_P(res)) return res;
  97. res = zori_initialize_registry();
  98. if (!ZORI_ID_OK_P(res)) return res;
  99. res = zori_initialize_root();
  100. if (!ZORI_ID_OK_P(res)) return res;
  101. root = zori_get_root();
  102. if (default_style) {
  103. root->widget.style = *default_style;
  104. } else {
  105. root->widget.style = *zori_get_default_style();
  106. }
  107. root->widget.id = 0;
  108. zori_registry_add(zori_get_registry(), the_zori_root->widget.id, &the_zori_root->widget);
  109. return the_zori_root->widget.id;
  110. }
  111. zori_id zori_set_margins(zori_id id, int left, int top, int right, int bottom) {
  112. struct zori_widget * widget = zori_get_widget(id);
  113. return zori_widget_margins_(widget, left, top, right, bottom);
  114. }
  115. zori_id zori_set_margin(zori_id id, int size) {
  116. struct zori_widget * widget = zori_get_widget(id);
  117. return zori_widget_margin_(widget, size);
  118. }
  119. zori_id zori_set_paddings(zori_id id, int left, int top, int right, int bottom) {
  120. struct zori_widget * widget = zori_get_widget(id);
  121. return zori_widget_paddings_(widget, left, top, right, bottom);
  122. }
  123. zori_id zori_set_padding(zori_id id, int size) {
  124. struct zori_widget * widget = zori_get_widget(id);
  125. return zori_widget_padding_(widget, size);
  126. }
  127. zori_font * zori_text_font(zori_id id) {
  128. struct zori_widget * widget = zori_get_widget(id);
  129. return zori_widget_text_font(widget);
  130. }
  131. /** Destroy the Zori root widget */
  132. void zori_destroy_root(void) {
  133. if (the_zori_root) {
  134. zori_widget_free(&the_zori_root->widget);
  135. }
  136. /* free(the_zori_root); is not needed, as it is cleaned up a sa widget */
  137. the_zori_root = NULL;
  138. }
  139. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  140. * negative on error.
  141. */
  142. zori_id zori_shutdown() {
  143. zori_destroy_root();
  144. zori_destroy_default_style();
  145. zori_destroy_registry();
  146. return ZORI_ID_OK;
  147. }
  148. /* Creates a new screen widget. Normally this should be the first widget
  149. * you create after zori_start. */
  150. zori_id zori_new_screen(zori_id id, zori_display * display);
  151. /* Creates a new page widget on the given screen. The page is not
  152. * made the active page, unless if it is the first one created. */
  153. zori_id zori_new_page(zori_id id, zori_id screen);
  154. /* Activates the page on it's display. All other pages are dectivated and
  155. * hidden. */
  156. zori_id zori_activate_page(zori_id page);
  157. /* Creates a new generic widget on the given screen with the given
  158. * dimensions. */
  159. zori_id zori_new(zori_id screen, zori_rebox * box);
  160. /* Sets the flags of a widget. */
  161. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  162. /* Sets the whole style of a widget. */
  163. zori_id zori_set_style(zori_id id, struct zori_style * style) {
  164. struct zori_widget * widget = zori_get_widget(id);
  165. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  166. widget->style = *style;
  167. return widget->id;
  168. }
  169. /* Gets the whole style of a widget. */
  170. zori_id zori_get_style(zori_id id, struct zori_style * style) {
  171. struct zori_widget * widget = zori_get_widget(id);
  172. if ((!widget) || (!style)) return ZORI_ID_ERROR;
  173. *style = widget->style;
  174. return widget->id;
  175. }
  176. /* Sets the background color of the widget. */
  177. zori_id zori_set_background_color(zori_id id, zori_color color) {
  178. struct zori_widget * widget = zori_get_widget(id);
  179. if (!widget) return ZORI_ID_ERROR;
  180. widget->style.back.color = color;
  181. return widget->id;
  182. }
  183. /* Sets the background bitmap of the widget. */
  184. zori_id zori_set_background_bitmap(zori_id id, zori_bitmap * bitmap) {
  185. struct zori_widget * widget = zori_get_widget(id);
  186. if (!widget) return ZORI_ID_ERROR;
  187. widget->style.back.image = bitmap;
  188. return widget->id;
  189. }
  190. /* Sets the border color of the widget. */
  191. zori_id zori_set_border_color(zori_id id, zori_color color) {
  192. struct zori_widget * widget = zori_get_widget(id);
  193. if (!widget) return ZORI_ID_ERROR;
  194. widget->style.border.color = color;
  195. return widget->id;
  196. }
  197. /* Sets the border bitmap of the widget. */
  198. zori_id zori_set_border_bitmap(zori_id id, zori_bitmap * bitmap) {
  199. struct zori_widget * widget = zori_get_widget(id);
  200. if (!widget) return ZORI_ID_ERROR;
  201. widget->style.border.image = bitmap;
  202. return widget->id;
  203. }
  204. /* Sets the text font of the widget. */
  205. zori_id zori_set_text_font(zori_id id, zori_font * font) {
  206. struct zori_widget * widget = zori_get_widget(id);
  207. if (!widget) return ZORI_ID_ERROR;
  208. widget->style.text.font = font;
  209. return widget->id;
  210. }
  211. /* Sets the text font flags of the widget. */
  212. zori_id zori_set_text_font_flags(zori_id id, int flags) {
  213. struct zori_widget * widget = zori_get_widget(id);
  214. if (!widget) return ZORI_ID_ERROR;
  215. widget->style.text.flags = flags;
  216. return widget->id;
  217. }
  218. /* Creates a new frame widget. */
  219. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  220. /* Creates a new (vertical) menu widget. */
  221. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  222. /* Creates a new button widget. */
  223. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  224. /* Creates a new conversation widget. */
  225. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  226. /* Draws the whole UI and all visible parts. */
  227. void zori_draw_all(void) {
  228. zori_widget_raise_draw_event(&the_zori_root->widget);
  229. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  230. }
  231. /* Dispatches system events throughout the GUI. */
  232. int zori_handle_system_event(zori_system_event * sysev) {
  233. /* All events are passed on to the console if it is active.
  234. * Otherwise, the events are passed to the root widget. */
  235. if (!the_zori_root) {
  236. return ZORI_ID_EINVAL;
  237. }
  238. if (the_zori_root && the_zori_root->console) {
  239. if (zori_console_active(the_zori_root->console)) {
  240. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  241. if (!zori_propagate_event_p(res)) return res;
  242. }
  243. }
  244. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  245. }
  246. /* Updates the state of the UI. Pass in the time passed since last update. */
  247. void zori_update(double dt)
  248. {
  249. struct zori_root * root = zori_get_root();
  250. zori_widget_raise_update_event(&root->widget, dt);
  251. }
  252. /** Returns the "result" of a widget. 0 if no result was available
  253. * because the widget was not "ready" or not "clicked" yet.
  254. * Result will be negative if the widget was closed.
  255. */
  256. int zori_result(zori_id id) {
  257. int result = 0;
  258. struct zori_widget * widget = zori_get_widget(id);
  259. if (widget) {
  260. if (widget->result.ready) {
  261. switch (widget->result.type) {
  262. case ZORI_RESULT_TYPE_INTEGER:
  263. result = widget->result.value.integer;
  264. break;
  265. default:
  266. result = - widget->result.type;
  267. break;
  268. }
  269. widget->result.ready = 0;
  270. }
  271. }
  272. return result;
  273. }
  274. /** Returns the "result" of a widget. 0 if no result was available
  275. * because the widget was not "ready" or not "clicked" yet. */
  276. int zori_get_result(zori_id id, struct zori_result * result) {
  277. struct zori_widget * widget = zori_get_widget(id);
  278. if (widget && result) {
  279. if (widget->result.ready) {
  280. (*result) = widget->result;
  281. widget->result.ready = 0;
  282. return result->ready;
  283. }
  284. }
  285. return 0;
  286. }
  287. int zori_result_int(zori_id id, int * result);
  288. int zori_result_string(zori_id id, char ** result, size_t * size);
  289. int zori_result_buffer(zori_id id, char ** result, size_t * size);
  290. int zori_mouse_button_event_unpack
  291. (const union zori_event * event, float * x, float * y, int * b) {
  292. int ok = event->type == ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN;
  293. ok = ok || (event->type == ZORI_SYSTEM_EVENT_MOUSE_BUTTON_UP);
  294. if (!ok) return 0;
  295. * x = event->sys.ev->mouse.x;
  296. * y = event->sys.ev->mouse.y;
  297. * b = event->sys.ev->mouse.button;
  298. return event->sys.ev->mouse.button + 1;
  299. }