zori_longtext.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "monolog.h"
  2. #include "zori.h"
  3. #include "zori_longtext.h"
  4. #define ZORI_LONGTEXT_LINE_POS_MAX 99999
  5. struct zori_longtext * zori_widget_to_longtext(struct zori_widget * widget) {
  6. return ZORI_CONTAINER_OF(widget, struct zori_longtext, widget);
  7. }
  8. struct zori_longtext *
  9. zori_longtext_set(struct zori_longtext * longtext, const zori_string * text) {
  10. if (longtext) {
  11. if (longtext->text) {
  12. ustr_free(longtext->text);
  13. longtext->text = NULL;
  14. }
  15. if (text) {
  16. longtext->text = ustr_dup(text);
  17. if (!longtext->text) {
  18. LOG_ERROR("Out of memory in longtext setup.");
  19. }
  20. }
  21. }
  22. return longtext;
  23. }
  24. struct zori_longtext *
  25. zori_longtext_set_cstr(struct zori_longtext * longtext, const char * cstr) {
  26. const USTR * ustr;
  27. USTR_INFO info;
  28. ustr = ustr_refcstr(&info, cstr);
  29. return zori_longtext_set(longtext, ustr);
  30. }
  31. struct zori_longtext *
  32. zori_longtext_init_cstr(struct zori_longtext * longtext, const char * cstr) {
  33. longtext->text = NULL;
  34. return zori_longtext_set_cstr(longtext, cstr);
  35. }
  36. void zori_longtext_done(struct zori_longtext * longtext) {
  37. zori_longtext_set(longtext, NULL);
  38. }
  39. /** Gets the current text page for a longtext. */
  40. int zori_longtext_page(struct zori_longtext * longtext) {
  41. if (!longtext) return -2;
  42. return longtext->line_start / longtext->page_lines;
  43. }
  44. /** Gets the last page number for a longtext or negative on error. */
  45. int zori_longtext_last_page(struct zori_longtext * longtext) {
  46. if (!longtext) return -2;
  47. return longtext->line_max / longtext->page_lines;
  48. }
  49. /** Advances long text to the given page. Automatically unpauses as well. */
  50. int zori_longtext_page_(struct zori_longtext * longtext, int page) {
  51. if (!longtext) return -2;
  52. if (page < 0) return -3;
  53. int line = page * longtext->page_lines;
  54. /* Check for page overflow. */
  55. if (line >= longtext->line_max) {
  56. return -5;
  57. }
  58. longtext->paused = false;
  59. longtext->line_start = line;
  60. /* Negative delay is instant display. */
  61. if (longtext->delay < 0) {
  62. longtext->line_stop = longtext->line_start + longtext->page_lines;
  63. longtext->line_pos = ZORI_LONGTEXT_LINE_POS_MAX;
  64. } else {
  65. longtext->line_stop = longtext->line_start + 1;
  66. longtext->line_pos = 0;
  67. }
  68. return page;
  69. }
  70. /* Returns TRUE if the longtext node is at the end of the text to display,
  71. * false if not (more text to display) */
  72. int zori_longtext_at_end(struct zori_longtext * longtext) {
  73. if (!longtext) return FALSE;
  74. return ((longtext->line_stop >= longtext->line_max) &&
  75. (longtext->line_pos >= ZORI_LONGTEXT_LINE_POS_MAX));
  76. }
  77. /* This function is the helper callback that implements
  78. * updating scrolled/partial text for Longtexts.
  79. */
  80. static bool
  81. zori_longtext_update_custom_partial_text(int line_num, const char *line, int size, void * extra) {
  82. struct zori_longtext * longtext = extra;
  83. longtext->line_max = line_num;
  84. /* Don't draw lines before start but keep on drawing (for later lines) */
  85. if (line_num < longtext->line_start) return true;
  86. /* Don't draw lines after stop, but keep calculating to get correct amount of lines */
  87. if (line_num >= longtext->line_stop) return true;
  88. /* Reveal letter by letter on last line */
  89. if (line_num == (longtext->line_stop - 1)) {
  90. /* Advance the position automatically if not paused. */
  91. if (!longtext->paused) {
  92. longtext->line_pos++;
  93. }
  94. /* Reached eol, advance to next line. */
  95. if (longtext->line_pos >= size) {
  96. /* Is if the text window is full, pause, otherwise show the next line. */
  97. if ((longtext->line_stop - longtext->line_start) >= longtext->page_lines) {
  98. longtext->paused = true;
  99. } else {
  100. longtext->line_stop++;
  101. longtext->line_pos = 0;
  102. }
  103. }
  104. }
  105. (void) line;
  106. return true;
  107. }
  108. /* Updates the longtext, enables scrolling and per character display. */
  109. void zori_longtext_update_longtext(struct zori_longtext * longtext, double dt) {
  110. float width = longtext->widget.inner.size.x;
  111. zori_text * text = longtext->text;
  112. if (!text) {
  113. LOG_WARNING("Attempted to update a NULL longtext.");
  114. return;
  115. }
  116. /* Delay advance of characters somewhat. */
  117. longtext->delay_total += dt;
  118. if (longtext->delay_total < longtext->delay) return;
  119. longtext->delay_total = 0;
  120. longtext->line_max = 0;
  121. al_do_multiline_text(zori_widget_font(&longtext->widget), width,
  122. (const char *) text, zori_longtext_update_custom_partial_text, longtext);
  123. longtext->line_max++;
  124. /* pause if the last line is reached, and prevent overflow. */
  125. if (longtext->line_stop > longtext->line_max) {
  126. longtext->paused = true;
  127. longtext->line_pos = ZORI_LONGTEXT_LINE_POS_MAX;
  128. longtext->line_stop = longtext->line_max;
  129. }
  130. }
  131. /* Returns TRUE if the longtext node is at the end of the text to display,
  132. * false if not (more text to display) */
  133. int zori_longtext_longtext_at_end(struct zori_longtext * longtext) {
  134. return ((longtext->line_stop >= longtext->line_max) &&
  135. (longtext->line_pos >= ZORI_LONGTEXT_LINE_POS_MAX));
  136. }
  137. /* Calculates the x and y position where to draw text, taking alignment and
  138. * margin into consideration. */
  139. static BeVec
  140. zori_longtext_calculate_text_position(struct zori_longtext * longtext) {
  141. BeVec result;
  142. int flags = longtext->widget.style.text.font_flags;
  143. result.x = longtext->widget.inner.at.x;
  144. result.y = longtext->widget.inner.at.y;
  145. if (flags & ALLEGRO_ALIGN_CENTER) {
  146. result.x += longtext->widget.inner.size.x / 2;
  147. } else if (flags & ALLEGRO_ALIGN_RIGHT) {
  148. result.x += longtext->widget.inner.size.x;
  149. }
  150. return result;
  151. }
  152. void zori_longtext_draw_text(struct zori_longtext * longtext) {
  153. zori_font * font;
  154. int flags;
  155. BeVec pos = zori_longtext_calculate_text_position(longtext);
  156. font = zori_widget_font(&longtext->widget);
  157. flags = longtext->widget.style.text.font_flags | ALLEGRO_ALIGN_INTEGER;
  158. zori_color shadow = longtext->widget.style.back.color;
  159. zori_color fore = longtext->widget.style.text.color;
  160. /* Draw the text twice, once offset in bg color to produce a shadow,
  161. and once normally with foreground color. */
  162. al_draw_ustr(font, shadow, pos.x + 1, pos.y + 1,
  163. flags, longtext->text);
  164. al_draw_ustr(font, fore, pos.x, pos.y,
  165. flags, longtext->text);
  166. }
  167. /* This function is the helper callback that implements the actual drawing
  168. * for zori_longtext_draw_partial_text.
  169. */
  170. static bool zori_longtext_draw_custom_partial_text(int line_num, const char *line,
  171. int size, void *extra) {
  172. float x, y;
  173. struct zori_longtext * longtext = extra;
  174. ALLEGRO_USTR_INFO info;
  175. int real_size, flags;
  176. double width;
  177. ALLEGRO_FONT * font;
  178. BeVec pos;
  179. font = zori_widget_font(&longtext->widget);
  180. flags = longtext->widget.style.text.font_flags | ALLEGRO_ALIGN_INTEGER;
  181. pos = zori_longtext_calculate_text_position(longtext);
  182. /* Don't draw lines before start but keep on drawing (for later lines) */
  183. if (line_num < longtext->line_start) return true;
  184. /* Don't draw lines after stop, drawing is done */
  185. if (line_num >= longtext->line_stop) return false;
  186. real_size = size;
  187. /* calculate position */
  188. x = pos.x;
  189. y = pos.y + (longtext->line_height * (line_num - longtext->line_start));
  190. /* Reveal letter by letter on last line */
  191. if (line_num == (longtext->line_stop - 1)) {
  192. real_size = (longtext->line_pos < size) ? longtext->line_pos : size;
  193. /* Draw continuation marker if paused. XXX... this needs to be done
  194. * better... Maybe a bitmap member or so? */
  195. if (longtext->paused) {
  196. float x1, y1, x2, y2, x3, y3;
  197. y2 = y + 8;
  198. x2 = x + longtext->widget.inner.size.x;
  199. x1 = x2 - 8;
  200. y1 = y2;
  201. x3 = x2 - 4;
  202. y3 = y2 + 8;
  203. al_draw_filled_triangle(x1, y1, x2, y2, x3, y3, longtext->widget.style.text.color);
  204. }
  205. }
  206. zori_color shadow = longtext->widget.style.back.color;
  207. zori_color fore = longtext->widget.style.text.color;
  208. al_draw_ustr(font, shadow,
  209. x + 1, y + 1, flags, al_ref_buffer(&info, line, real_size));
  210. al_draw_ustr(font, fore,
  211. x, y, flags, al_ref_buffer(&info, line, real_size));
  212. return true;
  213. }
  214. /* Allows to draw a multi line text partially, from line_start up to
  215. * line_stop. Draws scrolling text from a prefilled struct. */
  216. void zori_longtext_draw_partial_text(struct zori_longtext * longtext) {
  217. float width = longtext->widget.inner.size.x;
  218. zori_font * font = zori_widget_font(&longtext->widget);
  219. if (!longtext->text) {
  220. LOG_WARNING("Trying to draw a NULL longtext.");
  221. return;
  222. }
  223. const char * text = ustr_c(longtext->text);
  224. /* It's a bit of a hack that this ends up here... */
  225. if (longtext->line_height < 1.0) {
  226. longtext->line_height = al_get_font_line_height(font);
  227. }
  228. al_do_multiline_text(font, width,
  229. (const char *) text, zori_longtext_draw_custom_partial_text, longtext);
  230. }
  231. void zori_draw_longtext(struct zori_longtext * longtext) {
  232. zori_longtext_draw_partial_text(longtext);
  233. }
  234. /** Sets the last line to display for a long text */
  235. zori_id zori_set_line_stop(zori_id index, int stop) {
  236. struct zori_widget * widget = zori_get_widget(index);
  237. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  238. if (!longtext) return ZORI_ID_EINVAL;
  239. longtext->line_stop = stop;
  240. return index;
  241. }
  242. /** Sets the first line to display for a long text */
  243. zori_id zori_line_start_(zori_id index, int start) {
  244. struct zori_widget * widget = zori_get_widget(index);
  245. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  246. if (!longtext) return ZORI_ID_EINVAL;
  247. longtext->line_start = start;
  248. return index;
  249. }
  250. /** Sets display delay between individual characters for a long text */
  251. int zori_delay_(zori_id index, double delay) {
  252. struct zori_widget * widget = zori_get_widget(index);
  253. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  254. if (!longtext) return ZORI_ID_EINVAL;
  255. longtext->delay = delay;
  256. return index;
  257. }
  258. /** Gets the last line to display for a long text or negative on error*/
  259. int zori_line_stop(int index) {
  260. struct zori_widget * widget = zori_get_widget(index);
  261. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  262. if (!longtext) return ZORI_ID_EINVAL;
  263. return longtext->line_stop;
  264. }
  265. /** Gets the first line to display for a long text */
  266. int zori_line_start(int index) {
  267. struct zori_widget * widget = zori_get_widget(index);
  268. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  269. if (!longtext) return ZORI_ID_EINVAL;
  270. return longtext->line_start;
  271. }
  272. /** Gets display delay between individual characters for a long text */
  273. double zori_delay(int index) {
  274. struct zori_widget * widget = zori_get_widget(index);
  275. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  276. if (!longtext) return ZORI_ID_EINVAL;
  277. return longtext->delay;
  278. }
  279. /** Sets amount of shown lines for a long text */
  280. int zori_page_lines_(zori_id index, int lines) {
  281. struct zori_widget * widget = zori_get_widget(index);
  282. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  283. if (!longtext) return ZORI_ID_EINVAL;
  284. longtext->page_lines = lines;
  285. return index;
  286. }
  287. /** Gets amount of lines for a "page" of long text */
  288. int zori_page_lines(int index) {
  289. struct zori_widget * widget = zori_get_widget(index);
  290. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  291. if (!longtext) return ZORI_ID_EINVAL;
  292. return longtext->page_lines;
  293. }
  294. /** Sets paused state of long text */
  295. int zori_paused_(zori_id index, int paused) {
  296. struct zori_widget * widget = zori_get_widget(index);
  297. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  298. if (!longtext) return ZORI_ID_EINVAL;
  299. longtext->paused = paused;
  300. return index;
  301. }
  302. /** Gets paused state of long text */
  303. int zori_paused(zori_id index) {
  304. struct zori_widget * widget = zori_get_widget(index);
  305. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  306. if (!longtext) return 0;
  307. return longtext->paused;
  308. }
  309. /** Gets the current text page for a longtext. */
  310. int zori_page(zori_id index) {
  311. struct zori_widget * widget = zori_get_widget(index);
  312. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  313. if (!longtext) return ZORI_ID_EINVAL;
  314. return zori_longtext_page(longtext);
  315. }
  316. /** Gets the number of the last text page for a longtext. */
  317. int zori_last_page(zori_id index) {
  318. struct zori_widget * widget = zori_get_widget(index);
  319. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  320. if (!longtext) return ZORI_ID_EINVAL;
  321. return zori_longtext_last_page(longtext);
  322. }
  323. /** Returns nonzero if the long text is at the end of it's display. */
  324. int zori_at_end(zori_id index) {
  325. struct zori_widget * widget = zori_get_widget(index);
  326. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  327. if (!longtext) return ZORI_ID_EINVAL;
  328. return zori_longtext_at_end(longtext);
  329. }
  330. /** Advances long text to the given page. Automatically unpauses as well. */
  331. int zori_page_(zori_id index, int page) {
  332. struct zori_widget * widget = zori_get_widget(index);
  333. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  334. return zori_longtext_page_(longtext, page);
  335. }
  336. /** Advances long text to the next page. Automatically unpauses as well. */
  337. int zori_next_page(zori_id index) {
  338. int page = zori_page(index);
  339. if (page < 0) return page;
  340. return zori_page_(index, page + 1);
  341. }
  342. /** Advances long text to the previous page. Automatically unpauses as well. */
  343. int zori_previous_page(int index) {
  344. int page = zori_page(index);
  345. if (page < 0) return page;
  346. return zori_page_(index, page - 1);
  347. }
  348. /** Handles a mouse axis event and pass it on. */
  349. int zori_longtext_on_mouse_axes(union zori_event * event) {
  350. struct zori_widget * widget = event->any.widget;
  351. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  352. float x = event->sys.ev->mouse.x;
  353. float y = event->sys.ev->mouse.y;
  354. if (zori_xy_inside_widget_p(widget, x, y)) {
  355. zori_widget_hover_(widget, TRUE);
  356. } else {
  357. zori_widget_hover_(widget, FALSE);
  358. }
  359. return ZORI_HANDLE_PASS;
  360. }
  361. /** Handles a mouse click event and pass it on. */
  362. int zori_longtext_on_mouse_click(union zori_event * event) {
  363. struct zori_widget * widget = event->any.widget;
  364. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  365. float x = event->sys.ev->mouse.x;
  366. float y = event->sys.ev->mouse.y;
  367. if (zori_xy_inside_widget_p(widget, x, y)) {
  368. widget->result = widget->id;
  369. LOG_NOTE("Longtext %d clicked: %d, result %d.\n", widget->id, widget->result);
  370. return zori_widget_raise_action_event(widget);
  371. }
  372. return ZORI_HANDLE_PASS;
  373. }
  374. int zori_longtext_on_draw(union zori_event * event) {
  375. struct zori_widget * widget = event->any.widget;
  376. struct zori_longtext * longtext = zori_widget_to_longtext(widget);
  377. zori_draw_longtext(longtext);
  378. return ZORI_HANDLE_PASS;
  379. }
  380. struct zori_handler zori_longtext_handlers[] = {
  381. { ZORI_SYSTEM_EVENT_MOUSE_BUTTON_DOWN , zori_longtext_on_mouse_click , NULL },
  382. { ZORI_SYSTEM_EVENT_MOUSE_AXES , zori_longtext_on_mouse_axes , NULL },
  383. { ZORI_EVENT_DRAW , zori_longtext_on_draw , NULL },
  384. { -1, NULL, NULL }
  385. };
  386. struct zori_longtext *
  387. zori_longtext_init(struct zori_longtext * longtext, const char * text) {
  388. if (longtext) {
  389. zori_longtext_set_cstr(longtext, text);
  390. }
  391. return longtext;
  392. }
  393. struct zori_longtext * zori_longtext_new(zori_id id, zori_id parent_id,
  394. zori_box * box, const char * text) {
  395. struct zori_longtext * longtext = NULL;
  396. longtext = calloc(1, sizeof(*longtext));
  397. if (!longtext) return NULL;
  398. zori_widget_initall(&longtext->widget, ZORI_WIDGET_TYPE_LONGTEXT, id, zori_get_widget(parent_id),
  399. box, NULL, ZORI_ARRAY_AND_AMOUNT(zori_longtext_handlers));
  400. if (!zori_longtext_init(longtext, text)) {
  401. free(longtext);
  402. longtext = NULL;
  403. }
  404. zori_widget_hover_(&longtext->widget, 0);
  405. return longtext;
  406. }
  407. zori_id zori_new_longtext(zori_id id, zori_id parent, zori_box * box, const char * text) {
  408. struct zori_longtext * longtext = zori_longtext_new(id, parent, box, text);
  409. if (!longtext) return ZORI_ID_ENOMEM;
  410. return longtext->widget.id;
  411. }