zori.c 18 KB

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