zori_longtext.c 16 KB

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