zori_longtext.c 16 KB

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