zori.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #include "zori.h"
  2. #include "zori_console.h"
  3. #include "miao.h"
  4. #include <allegro5/allegro_color.h>
  5. #include "str.h"
  6. #include "draw.h"
  7. /*
  8. * Pardon the pun name, but Zori is the submodule that handles the user
  9. * interface and the menus.
  10. */
  11. /* registry functionality */
  12. /** Compare registry entries. */
  13. int zori_registry_entry_compare(const void * v1, const void * v2) {
  14. const struct zori_registry_entry * entry1 = v1;
  15. const struct zori_registry_entry * entry2 = v2;
  16. return (entry2->id - entry1->id);
  17. }
  18. /** Initialize the registry. */
  19. zori_id zori_registry_init(struct zori_registry * registry) {
  20. miao_init(registry);
  21. return ZORI_ID_OK;
  22. }
  23. /** Add an entry to the registry. */
  24. zori_id
  25. zori_registry_add(struct zori_registry * registry, zori_id id,
  26. struct zori_widget * widget) {
  27. struct zori_registry_entry entry = { id, widget };
  28. if (miao_push(registry, entry)) {
  29. miao_qsort(registry, zori_registry_entry_compare);
  30. return ZORI_ID_OK;
  31. }
  32. return ZORI_ID_ENOMEM;
  33. }
  34. /** Look up an entry in the registry. */
  35. struct zori_registry_entry *
  36. zori_registry_lookup_entry(struct zori_registry * registry, zori_id id) {
  37. struct zori_registry_entry key = { id, NULL };
  38. return miao_bsearch(registry, zori_registry_entry_compare, &key);
  39. }
  40. /** Look up a widget in the registry. */
  41. struct zori_widget *
  42. zori_registry_lookup(struct zori_registry * registry, zori_id id) {
  43. struct zori_widget * result = NULL;
  44. struct zori_registry_entry * entry = NULL;
  45. entry = zori_registry_lookup_entry(registry, id);
  46. if (entry) {
  47. result = entry->widget;
  48. }
  49. return result;
  50. }
  51. /** Remove an entry from the registry. */
  52. zori_id
  53. zori_registry_remove(struct zori_registry * registry, zori_id id) {
  54. struct zori_registry_entry * entry = NULL;
  55. entry = zori_registry_lookup_entry(registry, id);
  56. if (entry) {
  57. miao_delete_entry(registry, entry);
  58. }
  59. return ZORI_ID_OK;
  60. }
  61. /** Handler functionality */
  62. int zori_handler_compare(const void * v1, const void * v2) {
  63. const struct zori_handler * h1 = v1;
  64. const struct zori_handler * h2 = v2;
  65. if (h1->type < h2->type) return -1;
  66. if (h1->type > h2->type) return 1;
  67. return 0;
  68. }
  69. struct zori_handler * zori_handlers_add(struct zori_handlers * me, zori_event_type type, zori_handler_func * handler, void * data) {
  70. struct zori_handler * result = NULL;
  71. result = miao_push_ptr(me);
  72. if (result) {
  73. result->type = type;
  74. result->handler = handler;
  75. result->data = data;
  76. }
  77. miao_qsort(me, zori_handler_compare);
  78. return result;
  79. }
  80. void zori_handlers_done(struct zori_handlers * me) {
  81. miao_done(me);
  82. }
  83. void zori_handlers_init(struct zori_handlers * me) {
  84. miao_init(me);
  85. }
  86. struct zori_handler * zori_handlers_search(struct zori_handlers * me, zori_event_type type) {
  87. struct zori_handler * result;
  88. struct zori_handler key;
  89. key.type = type;
  90. key.handler = NULL;
  91. key.data = NULL;
  92. if (!me) return NULL;
  93. result = miao_bsearch(me, zori_handler_compare, &key);
  94. return result;
  95. }
  96. int zori_handlers_handle(struct zori_handlers * me, union zori_event * event) {
  97. struct zori_handler * handler = zori_handlers_search(me, event->type);
  98. if (!handler) return ZORI_HANDLE_IGNORE;
  99. event->any.data = handler->data;
  100. return handler->handler(event);
  101. }
  102. /** Returns whether or not a widget will even handle an event in the first place.
  103. * For example, a hidden widget won't draw, and a disabled widget won't
  104. * accept any system events, and a NULL event doesn't accept events at all.. */
  105. int zori_widget_accepts_event(struct zori_widget * widget, union zori_event * event) {
  106. if (!widget) return 0;
  107. switch(event->any.type) {
  108. case ZORI_EVENT_DRAW:
  109. return zori_widget_visible(widget);
  110. default:
  111. return zori_widget_active(widget);
  112. }
  113. }
  114. /** Raises an event on the widget itself only. */
  115. int zori_widget_self_raise_event(struct zori_widget * widget,
  116. union zori_event * event) {
  117. enum zori_handle_result result;
  118. if (zori_widget_accepts_event(widget, event)) {
  119. event->any.widget = widget;
  120. return zori_handlers_handle(&widget->handlers, event);
  121. }
  122. /* If the event is not accepted, it is passed on to the child widgets. */
  123. return ZORI_HANDLE_PASS;
  124. }
  125. /* Raises an event on the widget, and if necessary, propagates it on to it's
  126. * children automatically and recursively. */
  127. int
  128. zori_widget_raise_event(struct zori_widget * widget, union zori_event * event) {
  129. enum zori_handle_result result =
  130. zori_widget_self_raise_event(widget, event);
  131. if (zori_propagate_event_p(result)) {
  132. enum zori_handle_result sub;
  133. size_t index;
  134. for (index = 0; index < miao_size(&widget->children); index++) {
  135. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  136. event->any.widget = child;
  137. sub = zori_widget_raise_event(child, event);
  138. if (sub == ZORI_HANDLE_DONE) {
  139. result = ZORI_HANDLE_DONE;
  140. break;
  141. }
  142. }
  143. }
  144. return result;
  145. }
  146. int zori_widget_raise_system_event
  147. (struct zori_widget * widget, zori_system_event * sysev) {
  148. union zori_event event;
  149. event.sys.any.type = sysev->type;
  150. event.sys.any.widget = widget;
  151. event.sys.ev = sysev;
  152. return zori_widget_raise_event(widget, &event);
  153. }
  154. int zori_widget_raise_draw_event(struct zori_widget * widget) {
  155. union zori_event event;
  156. event.draw.any.type = ZORI_EVENT_DRAW;
  157. event.draw.any.widget = widget;
  158. return zori_widget_raise_event(widget, &event);
  159. }
  160. int zori_widget_raise_overdraw_event(struct zori_widget * widget) {
  161. union zori_event event;
  162. event.draw.any.type = ZORI_EVENT_OVERDRAW;
  163. event.draw.any.widget = widget;
  164. return zori_widget_raise_event(widget, &event);
  165. }
  166. int zori_widget_raise_done_event(struct zori_widget * widget) {
  167. union zori_event event;
  168. event.done.any.type = ZORI_EVENT_DONE;
  169. event.done.any.widget = widget;
  170. return zori_widget_raise_event(widget, &event);
  171. }
  172. int zori_widget_raise_free_event(struct zori_widget * widget) {
  173. union zori_event event;
  174. event.free.any.type = ZORI_EVENT_FREE;
  175. event.free.any.widget = widget;
  176. return zori_widget_raise_event(widget, &event);
  177. }
  178. int zori_widget_raise_update_event
  179. (struct zori_widget * widget, double dt) {
  180. union zori_event event;
  181. event.update.any.type = ZORI_EVENT_DONE;
  182. event.update.any.widget = widget;
  183. event.update.dt = dt;
  184. return zori_widget_raise_event(widget, &event);
  185. }
  186. int zori_widget_raise_action_event
  187. (struct zori_widget * widget) {
  188. union zori_event event;
  189. event.action.any.type = ZORI_EVENT_ACTION;
  190. event.action.any.widget = widget;
  191. return zori_widget_raise_event(widget, &event);
  192. }
  193. static struct zori_root * the_zori_root = NULL;
  194. static struct zori_style * the_default_style = NULL;
  195. static struct zori_registry * the_zori_registry = NULL;
  196. struct zori_root * zori_get_root(void) {
  197. return the_zori_root;
  198. }
  199. int zori_widget_compare(const void * v1, const void * v2) {
  200. const struct zori_widget * w1 = v1, * w2 = v2;
  201. return (w2->id - w1->id);
  202. }
  203. struct zori_widget * zori_get_widget(zori_id id) {
  204. if (!the_zori_registry) return NULL;
  205. return zori_registry_lookup(the_zori_registry, id);
  206. }
  207. zori_id zori_get_unused_id(void) {
  208. zori_id id = -1;
  209. struct zori_widget * found;
  210. do {
  211. id++;
  212. if (id == INT_MAX) { return ZORI_ID_ERROR; }
  213. found = zori_get_widget(id);
  214. } while(found);
  215. return id;
  216. }
  217. struct zori_handler * zori_widget_add_handler
  218. (struct zori_widget * widget, zori_event_type type, zori_handler_func * handler, void * data) {
  219. if ((!widget) || (!handler)) return NULL;
  220. return zori_handlers_add(&widget->handlers, type, handler, data);
  221. }
  222. struct zori_handler * zori_widget_add_handlers
  223. (struct zori_widget * widget, struct zori_handler * handlers, size_t amount) {
  224. size_t i;
  225. for (i = 0; i < amount; i++) {
  226. struct zori_handler * handler = handlers + i;
  227. if (!zori_widget_add_handler(widget, handler->type, handler->handler, handler->data)) return NULL;
  228. }
  229. return handlers + amount - 1;
  230. }
  231. zori_id zori_start(struct zori_style * default_style) {
  232. if (the_zori_root) return ZORI_ID_OK;
  233. the_zori_root = calloc(1, sizeof(*the_zori_root));
  234. if (!the_zori_root) return ZORI_ID_ENOMEM;
  235. the_default_style = calloc(1, sizeof(*the_default_style));
  236. if (!the_default_style) return ZORI_ID_ENOMEM;
  237. the_zori_registry = calloc(1, sizeof(*the_zori_registry));
  238. if (!the_zori_registry) return ZORI_ID_ENOMEM;
  239. the_default_style->text.font = al_create_builtin_font();
  240. the_default_style->text.color = al_color_name("white");
  241. the_default_style->border.color = al_color_name("white");
  242. the_default_style->back.color = al_color_name("green");
  243. the_default_style->fore.color = al_color_name("white");
  244. if (default_style) {
  245. the_zori_root->widget.style = *default_style;
  246. } else {
  247. the_zori_root->widget.style = *the_default_style;
  248. }
  249. the_zori_root->widget.id = 0;
  250. zori_registry_add(the_zori_registry, the_zori_root->widget.id, &the_zori_root->widget);
  251. return the_zori_root->widget.id;
  252. }
  253. void zori_widget_free(struct zori_widget * widget);
  254. struct zori_widget * zori_widget_done(struct zori_widget * widget) {
  255. size_t index;
  256. struct zori_widget * aid, * next;
  257. if (!widget) return widget;
  258. for (index = 0; index < miao_size(&widget->children); index ++) {
  259. struct zori_widget * child = miao_unsafe_get(&widget->children, index);
  260. zori_widget_free(child);
  261. }
  262. miao_done(&widget->handlers);
  263. miao_done(&widget->children);
  264. return widget;
  265. }
  266. void zori_widget_free(struct zori_widget * widget) {
  267. if (widget) {
  268. zori_registry_remove(the_zori_registry, widget->id);
  269. }
  270. zori_widget_done(widget);
  271. free(widget);
  272. }
  273. struct zori_widget *
  274. zori_widget_add_child(struct zori_widget * parent, struct zori_widget * child) {
  275. if (parent) {
  276. miao_push((&parent->children), child);
  277. child->parent = child;
  278. }
  279. return child;
  280. }
  281. struct zori_widget * zori_widget_init
  282. ( struct zori_widget * widget,
  283. zori_id id,
  284. struct zori_widget * parent,
  285. zori_rebox * box, struct zori_style * style) {
  286. if (!widget) return NULL;
  287. miao_init(&widget->children);
  288. miao_init(&widget->handlers);
  289. widget->z = 0;
  290. widget->flags = 0;
  291. widget->id = ( (id < 0) ? widget->id : id);
  292. if (style) {
  293. widget->style = *style;
  294. if (!widget->style.text.font) {
  295. widget->style.text.font = the_default_style->text.font;
  296. }
  297. } else {
  298. widget->style = *the_default_style;
  299. }
  300. if (box) {
  301. widget->box = *box;
  302. } else if (parent) {
  303. widget->box = parent->box;
  304. } else {
  305. widget->box = rebox_make(0, 0, ZORI_WIDGET_DEFAULT_W, ZORI_WIDGET_DEFAULT_H);
  306. }
  307. zori_widget_add_child(parent, widget);
  308. zori_registry_add(the_zori_registry, widget->id, widget);
  309. return widget;
  310. }
  311. /* Shut down Zori and destroys all widgets. Return 0 on succes or
  312. * negative on error.
  313. */
  314. zori_id zori_shutdown() {
  315. assert((void *)(&the_zori_root->widget) == (void *)the_zori_root);
  316. zori_widget_free(&the_zori_root->widget);
  317. the_zori_root = NULL;
  318. free(the_default_style);
  319. the_default_style = NULL;
  320. /* clean up registry last so zori_widget fre can unregister it's widgets.*/
  321. miao_done(the_zori_registry);
  322. free(the_zori_registry);
  323. the_zori_registry = NULL;
  324. return ZORI_ID_OK;
  325. }
  326. struct zori_widget *
  327. zori_widget_initall(struct zori_widget * widget, int id,
  328. struct zori_widget * parent, zori_rebox * box, struct zori_style * style,
  329. struct zori_handler * handlers, size_t amount) {
  330. if (zori_widget_init(widget, id, parent, box, style)) {
  331. zori_widget_add_handlers(widget, handlers, amount);
  332. }
  333. return widget;
  334. }
  335. /* Creates a new screen widget. Normally this should be the first widget
  336. * you create after zori_start. */
  337. zori_id zori_new_screen(zori_display * display);
  338. /* Creates a new page widget on the given screen. The page is not
  339. * made the active page, unless if it is the first one created. */
  340. zori_id zori_new_page(zori_id screen);
  341. /* Activates the page on it's display. All other pages are dectivated and
  342. * hidden. */
  343. zori_id zori_activate_page(zori_id page);
  344. /* Creates a new generic widget on the given screen with the given
  345. * dimensions. */
  346. zori_id zori_new(zori_id screen, zori_rebox * box);
  347. /* Sets the flags of a widget. */
  348. zori_id zori_set_flags(zori_id widget, enum zori_flag flags);
  349. /* Sets the whole style of a widget. */
  350. zori_id zori_set_style(zori_id id, struct zori_style * style);
  351. /* Sets the background color of the widget. */
  352. zori_id zori_set_background_color(zori_id id, zori_color color);
  353. /* Sets the foreground color of the widget. */
  354. zori_id zori_set_foreground_color(zori_id id, zori_color color);
  355. /* Creates a new frame widget. */
  356. zori_id zori_new_frame_widget(zori_id parent, zori_rebox box);
  357. /* Creates a new (vertical) menu widget. */
  358. zori_id zori_new_menu_widget(zori_id parent, zori_rebox box, char * text);
  359. /* Creates a new button widget. */
  360. zori_id zori_new_button_widget(zori_id parent, zori_rebox box, char * text);
  361. /* Creates a new conversation widget. */
  362. zori_id zori_new_conversation_widget(zori_id parent, zori_rebox box, char * text);
  363. /* Lets the widget handle an event and bubbles it on to it's children
  364. * recursively depending on the need to do that. */
  365. void zori_widget_handle_event(struct zori_widget * widget, union zori_event * event) {
  366. zori_widget_raise_event(widget, event);
  367. }
  368. /* Draws the whole UI and all visible parts. */
  369. void zori_draw_all(void) {
  370. zori_widget_raise_draw_event(&the_zori_root->widget);
  371. zori_widget_raise_overdraw_event(&the_zori_root->widget);
  372. }
  373. /* Dispatches system events throughout the GUI. */
  374. int zori_handle_system_event(zori_system_event * sysev) {
  375. /* All events are passed on to the console regardless if it is active.
  376. * Otherwise, the events go into the system. */
  377. if (the_zori_root->console ) {
  378. if (zori_console_active(the_zori_root->console)) {
  379. int res = zori_widget_raise_system_event(&the_zori_root->console->widget, sysev);
  380. if (!zori_propagate_event_p(res)) return res;
  381. }
  382. }
  383. return zori_widget_raise_system_event(&the_zori_root->widget, sysev);
  384. }
  385. int zori_widget_visible(struct zori_widget * widget) {
  386. return widget && ((widget->flags & ZORI_FLAG_HIDDEN) != ZORI_FLAG_HIDDEN);
  387. }
  388. int zori_widget_active(struct zori_widget * widget) {
  389. return widget && ((widget->flags & ZORI_FLAG_DISABLED) != ZORI_FLAG_DISABLED);
  390. }
  391. int zori_widget_active_(struct zori_widget * widget, int set) {
  392. if (set) {
  393. widget->flags = widget->flags & (~ZORI_FLAG_DISABLED);
  394. } else {
  395. widget->flags = widget->flags | ZORI_FLAG_DISABLED;
  396. }
  397. return zori_widget_active(widget);
  398. }
  399. int zori_widget_visible_(struct zori_widget * widget, int set) {
  400. if (set) {
  401. widget->flags = widget->flags & (~ZORI_FLAG_HIDDEN);
  402. } else {
  403. widget->flags = widget->flags | ZORI_FLAG_HIDDEN;
  404. }
  405. return zori_widget_active(widget);
  406. }
  407. int zori_widget_hover(struct zori_widget * widget) {
  408. return widget && ((widget->flags & ZORI_FLAG_HOVERED) != ZORI_FLAG_HOVERED);
  409. }
  410. int zori_widget_hover_(struct zori_widget * widget, int set) {
  411. if (set) {
  412. widget->flags = widget->flags & (~ZORI_FLAG_HOVERED);
  413. } else {
  414. widget->flags = widget->flags | ZORI_FLAG_HOVERED;
  415. }
  416. return zori_widget_hover(widget);
  417. }
  418. int zori_widget_marked(struct zori_widget * widget) {
  419. return widget && ((widget->flags & ZORI_FLAG_MARKED) != ZORI_FLAG_MARKED);
  420. }
  421. int zori_widget_marked_(struct zori_widget * widget, int set) {
  422. if (set) {
  423. widget->flags = widget->flags & (~ZORI_FLAG_MARKED);
  424. } else {
  425. widget->flags = widget->flags | ZORI_FLAG_MARKED;
  426. }
  427. return zori_widget_hover(widget);
  428. }
  429. int zori_xy_inside_widget_p(struct zori_widget * widget, double x, double y) {
  430. return (rebox_coordinates_inside_p(&widget->box, x, y));
  431. }
  432. /* Updates the state of the UI. Pass in the time passed since last update. */
  433. void zori_update(double dt)
  434. {
  435. zori_widget_raise_update_event(&the_zori_root->widget, dt);
  436. }
  437. /* Registers an event handler for a widget. */
  438. zori_id zori_register(zori_id id, zori_event_type type, zori_handler_func handler, void * extra);
  439. zori_font * zori_widget_font(struct zori_widget * widget) {
  440. return widget->style.text.font;
  441. }
  442. zori_color zori_widget_forecolor(struct zori_widget * widget) {
  443. return widget->style.text.color;
  444. }
  445. zori_color zori_widget_backcolor(struct zori_widget * widget) {
  446. return widget->style.back.color;
  447. }
  448. int zori_widget_h(struct zori_widget * widget) {
  449. return widget->box.size.y;
  450. }
  451. int zori_widget_w(struct zori_widget * widget) {
  452. return widget->box.size.x;
  453. }
  454. int zori_widget_x(struct zori_widget * widget) {
  455. return widget->box.at.x;
  456. }
  457. int zori_widget_y(struct zori_widget * widget) {
  458. return widget->box.at.y;
  459. }