zori_longtext.c 16 KB

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