elements.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "elements.h"
  2. #include "m-core.h"
  3. #include <assets_icons.h>
  4. #include "furi_hal_resources.h"
  5. #include <furi_hal.h>
  6. #include "gui/canvas.h"
  7. #include <gui/icon_i.h>
  8. #include <gui/icon_animation_i.h>
  9. #include <m-string.h>
  10. #include <furi.h>
  11. #include "canvas_i.h"
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. typedef struct {
  16. uint8_t x;
  17. uint8_t y;
  18. uint8_t leading_min;
  19. uint8_t leading_default;
  20. uint8_t height;
  21. uint8_t descender;
  22. uint8_t len;
  23. const char* text;
  24. } ElementTextBoxLine;
  25. void elements_progress_bar(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, float progress) {
  26. furi_assert(canvas);
  27. furi_assert((progress >= 0) && (progress <= 1.0));
  28. uint8_t height = 9;
  29. uint8_t progress_length = roundf(progress * (width - 2));
  30. canvas_set_color(canvas, ColorWhite);
  31. canvas_draw_box(canvas, x + 1, y + 1, width - 2, height - 2);
  32. canvas_set_color(canvas, ColorBlack);
  33. canvas_draw_rframe(canvas, x, y, width, height, 3);
  34. canvas_draw_box(canvas, x + 1, y + 1, progress_length, height - 2);
  35. }
  36. void elements_scrollbar_pos(
  37. Canvas* canvas,
  38. uint8_t x,
  39. uint8_t y,
  40. uint8_t height,
  41. uint16_t pos,
  42. uint16_t total) {
  43. furi_assert(canvas);
  44. // prevent overflows
  45. canvas_set_color(canvas, ColorWhite);
  46. canvas_draw_box(canvas, x - 3, y, 3, height);
  47. // dot line
  48. canvas_set_color(canvas, ColorBlack);
  49. for(uint8_t i = y; i < height + y; i += 2) {
  50. canvas_draw_dot(canvas, x - 2, i);
  51. }
  52. // Position block
  53. if(total) {
  54. float block_h = ((float)height) / total;
  55. canvas_draw_box(canvas, x - 3, y + (block_h * pos), 3, MAX(block_h, 1));
  56. }
  57. }
  58. void elements_scrollbar(Canvas* canvas, uint16_t pos, uint16_t total) {
  59. furi_assert(canvas);
  60. uint8_t width = canvas_width(canvas);
  61. uint8_t height = canvas_height(canvas);
  62. // prevent overflows
  63. canvas_set_color(canvas, ColorWhite);
  64. canvas_draw_box(canvas, width - 3, 0, 3, height);
  65. // dot line
  66. canvas_set_color(canvas, ColorBlack);
  67. for(uint8_t i = 0; i < height; i += 2) {
  68. canvas_draw_dot(canvas, width - 2, i);
  69. }
  70. // Position block
  71. if(total) {
  72. float block_h = ((float)height) / total;
  73. canvas_draw_box(canvas, width - 3, block_h * pos, 3, MAX(block_h, 1));
  74. }
  75. }
  76. void elements_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  77. furi_assert(canvas);
  78. canvas_draw_line(canvas, x + 2, y, x + width - 2, y);
  79. canvas_draw_line(canvas, x + 1, y + height - 1, x + width, y + height - 1);
  80. canvas_draw_line(canvas, x + 2, y + height, x + width - 1, y + height);
  81. canvas_draw_line(canvas, x, y + 2, x, y + height - 2);
  82. canvas_draw_line(canvas, x + width - 1, y + 1, x + width - 1, y + height - 2);
  83. canvas_draw_line(canvas, x + width, y + 2, x + width, y + height - 2);
  84. canvas_draw_dot(canvas, x + 1, y + 1);
  85. }
  86. void elements_button_left(Canvas* canvas, const char* str) {
  87. const uint8_t button_height = 12;
  88. const uint8_t vertical_offset = 3;
  89. const uint8_t horizontal_offset = 3;
  90. const uint8_t string_width = canvas_string_width(canvas, str);
  91. const Icon* icon = &I_ButtonLeft_4x7;
  92. const uint8_t icon_h_offset = 3;
  93. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  94. const uint8_t icon_v_offset = icon->height + vertical_offset;
  95. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  96. const uint8_t x = 0;
  97. const uint8_t y = canvas_height(canvas);
  98. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  99. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  100. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  101. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  102. canvas_invert_color(canvas);
  103. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonLeft_4x7);
  104. canvas_draw_str(
  105. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  106. canvas_invert_color(canvas);
  107. }
  108. void elements_button_right(Canvas* canvas, const char* str) {
  109. const uint8_t button_height = 12;
  110. const uint8_t vertical_offset = 3;
  111. const uint8_t horizontal_offset = 3;
  112. const uint8_t string_width = canvas_string_width(canvas, str);
  113. const Icon* icon = &I_ButtonRight_4x7;
  114. const uint8_t icon_h_offset = 3;
  115. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  116. const uint8_t icon_v_offset = icon->height + vertical_offset;
  117. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  118. const uint8_t x = canvas_width(canvas);
  119. const uint8_t y = canvas_height(canvas);
  120. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  121. canvas_draw_line(canvas, x - button_width - 1, y, x - button_width - 1, y - button_height + 0);
  122. canvas_draw_line(canvas, x - button_width - 2, y, x - button_width - 2, y - button_height + 1);
  123. canvas_draw_line(canvas, x - button_width - 3, y, x - button_width - 3, y - button_height + 2);
  124. canvas_invert_color(canvas);
  125. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  126. canvas_draw_icon(
  127. canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonRight_4x7);
  128. canvas_invert_color(canvas);
  129. }
  130. void elements_button_center(Canvas* canvas, const char* str) {
  131. const uint8_t button_height = 12;
  132. const uint8_t vertical_offset = 3;
  133. const uint8_t horizontal_offset = 1;
  134. const uint8_t string_width = canvas_string_width(canvas, str);
  135. const Icon* icon = &I_ButtonCenter_7x7;
  136. const uint8_t icon_h_offset = 3;
  137. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  138. const uint8_t icon_v_offset = icon->height + vertical_offset;
  139. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  140. const uint8_t x = (canvas_width(canvas) - button_width) / 2;
  141. const uint8_t y = canvas_height(canvas);
  142. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  143. canvas_draw_line(canvas, x - 1, y, x - 1, y - button_height + 0);
  144. canvas_draw_line(canvas, x - 2, y, x - 2, y - button_height + 1);
  145. canvas_draw_line(canvas, x - 3, y, x - 3, y - button_height + 2);
  146. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  147. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  148. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  149. canvas_invert_color(canvas);
  150. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonCenter_7x7);
  151. canvas_draw_str(
  152. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  153. canvas_invert_color(canvas);
  154. }
  155. static size_t
  156. elements_get_max_chars_to_fit(Canvas* canvas, Align horizontal, const char* text, uint8_t x) {
  157. const char* end = strchr(text, '\n');
  158. if(end == NULL) {
  159. end = text + strlen(text);
  160. }
  161. size_t text_size = end - text;
  162. string_t str;
  163. string_init_set_str(str, text);
  164. string_left(str, text_size);
  165. size_t result = 0;
  166. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(str));
  167. uint8_t px_left = 0;
  168. if(horizontal == AlignCenter) {
  169. if(x > (canvas_width(canvas) / 2)) {
  170. px_left = (canvas_width(canvas) - x) * 2;
  171. } else {
  172. px_left = x * 2;
  173. }
  174. } else if(horizontal == AlignLeft) {
  175. px_left = canvas_width(canvas) - x;
  176. } else if(horizontal == AlignRight) {
  177. px_left = x;
  178. } else {
  179. furi_assert(0);
  180. }
  181. if(len_px > px_left) {
  182. uint8_t excess_symbols_approximately =
  183. roundf((float)(len_px - px_left) / ((float)len_px / (float)text_size));
  184. // reduce to 5 to be sure dash fit, and next line will be at least 5 symbols long
  185. if(excess_symbols_approximately > 0) {
  186. excess_symbols_approximately = MAX(excess_symbols_approximately, 5);
  187. result = text_size - excess_symbols_approximately - 1;
  188. } else {
  189. result = text_size;
  190. }
  191. } else {
  192. result = text_size;
  193. }
  194. string_clear(str);
  195. return result;
  196. }
  197. void elements_multiline_text_aligned(
  198. Canvas* canvas,
  199. uint8_t x,
  200. uint8_t y,
  201. Align horizontal,
  202. Align vertical,
  203. const char* text) {
  204. furi_assert(canvas);
  205. furi_assert(text);
  206. uint8_t lines_count = 0;
  207. uint8_t font_height = canvas_current_font_height(canvas);
  208. string_t line;
  209. /* go through text line by line and count lines */
  210. for(const char* start = text; start[0];) {
  211. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  212. ++lines_count;
  213. start += chars_fit;
  214. start += start[0] == '\n' ? 1 : 0;
  215. }
  216. if(vertical == AlignBottom) {
  217. y -= font_height * (lines_count - 1);
  218. } else if(vertical == AlignCenter) {
  219. y -= (font_height * (lines_count - 1)) / 2;
  220. }
  221. /* go through text line by line and print them */
  222. for(const char* start = text; start[0];) {
  223. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  224. if((start[chars_fit] == '\n') || (start[chars_fit] == 0)) {
  225. string_init_printf(line, "%.*s", chars_fit, start);
  226. } else if((y + font_height) > canvas_height(canvas)) {
  227. string_init_printf(line, "%.*s...\n", chars_fit, start);
  228. } else {
  229. string_init_printf(line, "%.*s-\n", chars_fit, start);
  230. }
  231. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, string_get_cstr(line));
  232. string_clear(line);
  233. y += font_height;
  234. if(y > canvas_height(canvas)) {
  235. break;
  236. }
  237. start += chars_fit;
  238. start += start[0] == '\n' ? 1 : 0;
  239. }
  240. }
  241. void elements_multiline_text(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  242. furi_assert(canvas);
  243. furi_assert(text);
  244. uint8_t font_height = canvas_current_font_height(canvas);
  245. string_t str;
  246. string_init(str);
  247. const char* start = text;
  248. char* end;
  249. do {
  250. end = strchr(start, '\n');
  251. if(end) {
  252. string_set_strn(str, start, end - start);
  253. } else {
  254. string_set_str(str, start);
  255. }
  256. canvas_draw_str(canvas, x, y, string_get_cstr(str));
  257. start = end + 1;
  258. y += font_height;
  259. } while(end && y < 64);
  260. string_clear(str);
  261. }
  262. void elements_multiline_text_framed(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  263. furi_assert(canvas);
  264. furi_assert(text);
  265. uint8_t font_y = canvas_current_font_height(canvas);
  266. uint16_t str_width = canvas_string_width(canvas, text);
  267. // count \n's
  268. uint8_t lines = 1;
  269. const char* t = text;
  270. while(*t != '\0') {
  271. if(*t == '\n') {
  272. lines++;
  273. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  274. str_width = temp_width > str_width ? temp_width : str_width;
  275. }
  276. t++;
  277. }
  278. canvas_set_color(canvas, ColorWhite);
  279. canvas_draw_box(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  280. canvas_set_color(canvas, ColorBlack);
  281. elements_multiline_text(canvas, x + 4, y - 1, text);
  282. elements_frame(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  283. }
  284. void elements_slightly_rounded_frame(
  285. Canvas* canvas,
  286. uint8_t x,
  287. uint8_t y,
  288. uint8_t width,
  289. uint8_t height) {
  290. furi_assert(canvas);
  291. canvas_draw_rframe(canvas, x, y, width, height, 1);
  292. }
  293. void elements_slightly_rounded_box(
  294. Canvas* canvas,
  295. uint8_t x,
  296. uint8_t y,
  297. uint8_t width,
  298. uint8_t height) {
  299. furi_assert(canvas);
  300. canvas_draw_rbox(canvas, x, y, width, height, 1);
  301. }
  302. void elements_bold_rounded_frame(
  303. Canvas* canvas,
  304. uint8_t x,
  305. uint8_t y,
  306. uint8_t width,
  307. uint8_t height) {
  308. furi_assert(canvas);
  309. canvas_set_color(canvas, ColorWhite);
  310. canvas_draw_box(canvas, x + 2, y + 2, width - 3, height - 3);
  311. canvas_set_color(canvas, ColorBlack);
  312. canvas_draw_line(canvas, x + 3, y, x + width - 3, y);
  313. canvas_draw_line(canvas, x + 2, y + 1, x + width - 2, y + 1);
  314. canvas_draw_line(canvas, x, y + 3, x, y + height - 3);
  315. canvas_draw_line(canvas, x + 1, y + 2, x + 1, y + height - 2);
  316. canvas_draw_line(canvas, x + width, y + 3, x + width, y + height - 3);
  317. canvas_draw_line(canvas, x + width - 1, y + 2, x + width - 1, y + height - 2);
  318. canvas_draw_line(canvas, x + 3, y + height, x + width - 3, y + height);
  319. canvas_draw_line(canvas, x + 2, y + height - 1, x + width - 2, y + height - 1);
  320. canvas_draw_dot(canvas, x + 2, y + 2);
  321. canvas_draw_dot(canvas, x + 3, y + 2);
  322. canvas_draw_dot(canvas, x + 2, y + 3);
  323. canvas_draw_dot(canvas, x + width - 2, y + 2);
  324. canvas_draw_dot(canvas, x + width - 3, y + 2);
  325. canvas_draw_dot(canvas, x + width - 2, y + 3);
  326. canvas_draw_dot(canvas, x + 2, y + height - 2);
  327. canvas_draw_dot(canvas, x + 3, y + height - 2);
  328. canvas_draw_dot(canvas, x + 2, y + height - 3);
  329. canvas_draw_dot(canvas, x + width - 2, y + height - 2);
  330. canvas_draw_dot(canvas, x + width - 3, y + height - 2);
  331. canvas_draw_dot(canvas, x + width - 2, y + height - 3);
  332. }
  333. void elements_bubble(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  334. furi_assert(canvas);
  335. canvas_draw_rframe(canvas, x + 4, y, width, height, 3);
  336. uint8_t y_corner = y + height * 2 / 3;
  337. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner - 4);
  338. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner + 4);
  339. canvas_set_color(canvas, ColorWhite);
  340. canvas_draw_line(canvas, x + 4, y_corner - 3, x + 4, y_corner + 3);
  341. canvas_set_color(canvas, ColorBlack);
  342. }
  343. void elements_bubble_str(
  344. Canvas* canvas,
  345. uint8_t x,
  346. uint8_t y,
  347. const char* text,
  348. Align horizontal,
  349. Align vertical) {
  350. furi_assert(canvas);
  351. furi_assert(text);
  352. uint8_t font_y = canvas_current_font_height(canvas);
  353. uint16_t str_width = canvas_string_width(canvas, text);
  354. // count \n's
  355. uint8_t lines = 1;
  356. const char* t = text;
  357. while(*t != '\0') {
  358. if(*t == '\n') {
  359. lines++;
  360. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  361. str_width = temp_width > str_width ? temp_width : str_width;
  362. }
  363. t++;
  364. }
  365. uint8_t frame_x = x;
  366. uint8_t frame_y = y;
  367. uint8_t frame_width = str_width + 8;
  368. uint8_t frame_height = font_y * lines + 4;
  369. canvas_set_color(canvas, ColorWhite);
  370. canvas_draw_box(canvas, frame_x + 1, frame_y + 1, frame_width - 2, frame_height - 2);
  371. canvas_set_color(canvas, ColorBlack);
  372. canvas_draw_rframe(canvas, frame_x, frame_y, frame_width, frame_height, 1);
  373. elements_multiline_text(canvas, x + 4, y - 1 + font_y, text);
  374. uint8_t x1 = 0;
  375. uint8_t x2 = 0;
  376. uint8_t x3 = 0;
  377. uint8_t y1 = 0;
  378. uint8_t y2 = 0;
  379. uint8_t y3 = 0;
  380. if((horizontal == AlignLeft) && (vertical == AlignTop)) {
  381. x1 = frame_x;
  382. y1 = frame_y;
  383. x2 = frame_x - 4;
  384. y2 = frame_y;
  385. x3 = frame_x;
  386. y3 = frame_y + 4;
  387. canvas_set_color(canvas, ColorWhite);
  388. canvas_draw_box(canvas, x2 + 2, y2 + 1, 2, 2);
  389. canvas_set_color(canvas, ColorBlack);
  390. } else if((horizontal == AlignLeft) && (vertical == AlignCenter)) {
  391. x1 = frame_x;
  392. y1 = frame_y + (frame_height - 1) / 2 - 4;
  393. x2 = frame_x - 4;
  394. y2 = frame_y + (frame_height - 1) / 2;
  395. x3 = frame_x;
  396. y3 = frame_y + (frame_height - 1) / 2 + 4;
  397. canvas_set_color(canvas, ColorWhite);
  398. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 5);
  399. canvas_draw_dot(canvas, x2 + 1, y2);
  400. canvas_set_color(canvas, ColorBlack);
  401. } else if((horizontal == AlignLeft) && (vertical == AlignBottom)) {
  402. x1 = frame_x;
  403. y1 = frame_y + (frame_height - 1) - 4;
  404. x2 = frame_x - 4;
  405. y2 = frame_y + (frame_height - 1);
  406. x3 = frame_x;
  407. y3 = frame_y + (frame_height - 1);
  408. canvas_set_color(canvas, ColorWhite);
  409. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 2);
  410. canvas_set_color(canvas, ColorBlack);
  411. } else if((horizontal == AlignRight) && (vertical == AlignTop)) {
  412. x1 = frame_x + (frame_width - 1);
  413. y1 = frame_y;
  414. x2 = frame_x + (frame_width - 1) + 4;
  415. y2 = frame_y;
  416. x3 = frame_x + (frame_width - 1);
  417. y3 = frame_y + 4;
  418. canvas_set_color(canvas, ColorWhite);
  419. canvas_draw_box(canvas, x2 - 3, y2 + 1, 2, 2);
  420. canvas_set_color(canvas, ColorBlack);
  421. } else if((horizontal == AlignRight) && (vertical == AlignCenter)) {
  422. x1 = frame_x + (frame_width - 1);
  423. y1 = frame_y + (frame_height - 1) / 2 - 4;
  424. x2 = frame_x + (frame_width - 1) + 4;
  425. y2 = frame_y + (frame_height - 1) / 2;
  426. x3 = frame_x + (frame_width - 1);
  427. y3 = frame_y + (frame_height - 1) / 2 + 4;
  428. canvas_set_color(canvas, ColorWhite);
  429. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 5);
  430. canvas_draw_dot(canvas, x2 - 1, y2);
  431. canvas_set_color(canvas, ColorBlack);
  432. } else if((horizontal == AlignRight) && (vertical == AlignBottom)) {
  433. x1 = frame_x + (frame_width - 1);
  434. y1 = frame_y + (frame_height - 1) - 4;
  435. x2 = frame_x + (frame_width - 1) + 4;
  436. y2 = frame_y + (frame_height - 1);
  437. x3 = frame_x + (frame_width - 1);
  438. y3 = frame_y + (frame_height - 1);
  439. canvas_set_color(canvas, ColorWhite);
  440. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 2);
  441. canvas_set_color(canvas, ColorBlack);
  442. } else if((horizontal == AlignCenter) && (vertical == AlignTop)) {
  443. x1 = frame_x + (frame_width - 1) / 2 - 4;
  444. y1 = frame_y;
  445. x2 = frame_x + (frame_width - 1) / 2;
  446. y2 = frame_y - 4;
  447. x3 = frame_x + (frame_width - 1) / 2 + 4;
  448. y3 = frame_y;
  449. canvas_set_color(canvas, ColorWhite);
  450. canvas_draw_box(canvas, x2 - 2, y2 + 2, 5, 2);
  451. canvas_draw_dot(canvas, x2, y2 + 1);
  452. canvas_set_color(canvas, ColorBlack);
  453. } else if((horizontal == AlignCenter) && (vertical == AlignBottom)) {
  454. x1 = frame_x + (frame_width - 1) / 2 - 4;
  455. y1 = frame_y + (frame_height - 1);
  456. x2 = frame_x + (frame_width - 1) / 2;
  457. y2 = frame_y + (frame_height - 1) + 4;
  458. x3 = frame_x + (frame_width - 1) / 2 + 4;
  459. y3 = frame_y + (frame_height - 1);
  460. canvas_set_color(canvas, ColorWhite);
  461. canvas_draw_box(canvas, x2 - 2, y2 - 3, 5, 2);
  462. canvas_draw_dot(canvas, x2, y2 - 1);
  463. canvas_set_color(canvas, ColorBlack);
  464. }
  465. canvas_set_color(canvas, ColorWhite);
  466. canvas_draw_line(canvas, x3, y3, x1, y1);
  467. canvas_set_color(canvas, ColorBlack);
  468. canvas_draw_line(canvas, x1, y1, x2, y2);
  469. canvas_draw_line(canvas, x2, y2, x3, y3);
  470. }
  471. void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width) {
  472. furi_assert(canvas);
  473. furi_assert(string);
  474. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(string));
  475. if(len_px > width) {
  476. width -= canvas_string_width(canvas, "...");
  477. do {
  478. string_left(string, string_size(string) - 1);
  479. len_px = canvas_string_width(canvas, string_get_cstr(string));
  480. } while(len_px > width);
  481. string_cat(string, "...");
  482. }
  483. }
  484. void elements_text_box(
  485. Canvas* canvas,
  486. uint8_t x,
  487. uint8_t y,
  488. uint8_t width,
  489. uint8_t height,
  490. Align horizontal,
  491. Align vertical,
  492. const char* text,
  493. bool strip_to_dots) {
  494. furi_assert(canvas);
  495. ElementTextBoxLine line[ELEMENTS_MAX_LINES_NUM];
  496. bool bold = false;
  497. bool mono = false;
  498. bool inversed = false;
  499. bool inversed_present = false;
  500. Font current_font = FontSecondary;
  501. Font prev_font = FontSecondary;
  502. CanvasFontParameters* font_params = canvas_get_font_params(canvas, current_font);
  503. // Fill line parameters
  504. uint8_t line_leading_min = font_params->leading_min;
  505. uint8_t line_leading_default = font_params->leading_default;
  506. uint8_t line_height = font_params->height;
  507. uint8_t line_descender = font_params->descender;
  508. uint8_t line_num = 0;
  509. uint8_t line_width = 0;
  510. uint8_t line_len = 0;
  511. uint8_t total_height_min = 0;
  512. uint8_t total_height_default = 0;
  513. uint16_t i = 0;
  514. bool full_text_processed = false;
  515. uint16_t dots_width = canvas_string_width(canvas, "...");
  516. canvas_set_font(canvas, FontSecondary);
  517. // Fill all lines
  518. line[0].text = text;
  519. for(i = 0; !full_text_processed; i++) {
  520. line_len++;
  521. // Identify line height
  522. if(prev_font != current_font) {
  523. font_params = canvas_get_font_params(canvas, current_font);
  524. line_leading_min = MAX(line_leading_min, font_params->leading_min);
  525. line_leading_default = MAX(line_leading_default, font_params->leading_default);
  526. line_height = MAX(line_height, font_params->height);
  527. line_descender = MAX(line_descender, font_params->descender);
  528. prev_font = current_font;
  529. }
  530. // Set the font
  531. if(text[i] == '\e' && text[i + 1]) {
  532. i++;
  533. line_len++;
  534. if(text[i] == ELEMENTS_BOLD_MARKER) {
  535. if(bold) {
  536. current_font = FontSecondary;
  537. } else {
  538. current_font = FontPrimary;
  539. }
  540. canvas_set_font(canvas, current_font);
  541. bold = !bold;
  542. }
  543. if(text[i] == ELEMENTS_MONO_MARKER) {
  544. if(mono) {
  545. current_font = FontSecondary;
  546. } else {
  547. current_font = FontKeyboard;
  548. }
  549. canvas_set_font(canvas, FontKeyboard);
  550. mono = !mono;
  551. }
  552. if(text[i] == ELEMENTS_INVERSED_MARKER) {
  553. inversed_present = true;
  554. }
  555. continue;
  556. }
  557. if(text[i] != '\n') {
  558. line_width += canvas_glyph_width(canvas, text[i]);
  559. }
  560. // Process new line
  561. if(text[i] == '\n' || text[i] == '\0' || line_width > width) {
  562. if(line_width > width) {
  563. line_width -= canvas_glyph_width(canvas, text[i--]);
  564. line_len--;
  565. }
  566. if(text[i] == '\0') {
  567. full_text_processed = true;
  568. }
  569. if(inversed_present) {
  570. line_leading_min += 1;
  571. line_leading_default += 1;
  572. inversed_present = false;
  573. }
  574. line[line_num].leading_min = line_leading_min;
  575. line[line_num].leading_default = line_leading_default;
  576. line[line_num].height = line_height;
  577. line[line_num].descender = line_descender;
  578. if(total_height_min + line_leading_min > height) {
  579. break;
  580. }
  581. total_height_min += line_leading_min;
  582. total_height_default += line_leading_default;
  583. line[line_num].len = line_len;
  584. if(horizontal == AlignCenter) {
  585. line[line_num].x = x + (width - line_width) / 2;
  586. } else if(horizontal == AlignRight) {
  587. line[line_num].x = x + (width - line_width);
  588. } else {
  589. line[line_num].x = x;
  590. }
  591. line[line_num].y = total_height_min;
  592. line_num++;
  593. if(text[i + 1]) {
  594. line[line_num].text = &text[i + 1];
  595. }
  596. line_leading_min = font_params->leading_min;
  597. line_height = font_params->height;
  598. line_descender = font_params->descender;
  599. line_width = 0;
  600. line_len = 0;
  601. }
  602. }
  603. // Set vertical alignment for all lines
  604. if(total_height_default < height) {
  605. if(vertical == AlignTop) {
  606. line[0].y = y + line[0].height;
  607. } else if(vertical == AlignCenter) {
  608. line[0].y = y + line[0].height + (height - total_height_default) / 2;
  609. } else if(vertical == AlignBottom) {
  610. line[0].y = y + line[0].height + (height - total_height_default);
  611. }
  612. if(line_num > 1) {
  613. for(uint8_t i = 1; i < line_num; i++) {
  614. line[i].y = line[i - 1].y + line[i - 1].leading_default;
  615. }
  616. }
  617. } else if(line_num > 1) {
  618. uint8_t free_pixel_num = height - total_height_min;
  619. uint8_t fill_pixel = 0;
  620. uint8_t j = 1;
  621. line[0].y = y + line[0].height;
  622. while(fill_pixel < free_pixel_num) {
  623. line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
  624. fill_pixel++;
  625. j = j % (line_num - 1) + 1;
  626. }
  627. }
  628. // Draw line by line
  629. canvas_set_font(canvas, FontSecondary);
  630. bold = false;
  631. mono = false;
  632. inversed = false;
  633. for(uint8_t i = 0; i < line_num; i++) {
  634. for(uint8_t j = 0; j < line[i].len; j++) {
  635. // Process format symbols
  636. if(line[i].text[j] == ELEMENTS_BOLD_MARKER) {
  637. if(bold) {
  638. current_font = FontSecondary;
  639. } else {
  640. current_font = FontPrimary;
  641. }
  642. canvas_set_font(canvas, current_font);
  643. bold = !bold;
  644. continue;
  645. }
  646. if(line[i].text[j] == ELEMENTS_MONO_MARKER) {
  647. if(mono) {
  648. current_font = FontSecondary;
  649. } else {
  650. current_font = FontKeyboard;
  651. }
  652. canvas_set_font(canvas, current_font);
  653. mono = !mono;
  654. continue;
  655. }
  656. if(line[i].text[j] == ELEMENTS_INVERSED_MARKER) {
  657. inversed = !inversed;
  658. continue;
  659. }
  660. if(inversed) {
  661. canvas_draw_box(
  662. canvas,
  663. line[i].x - 1,
  664. line[i].y - line[i].height - 1,
  665. canvas_glyph_width(canvas, line[i].text[j]) + 1,
  666. line[i].height + line[i].descender + 2);
  667. canvas_invert_color(canvas);
  668. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  669. canvas_invert_color(canvas);
  670. } else {
  671. if((i == line_num - 1) && strip_to_dots) {
  672. uint8_t next_symbol_width = canvas_glyph_width(canvas, line[i].text[j]);
  673. if(line[i].x + next_symbol_width + dots_width > x + width) {
  674. canvas_draw_str(canvas, line[i].x, line[i].y, "...");
  675. break;
  676. }
  677. }
  678. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  679. }
  680. line[i].x += canvas_glyph_width(canvas, line[i].text[j]);
  681. }
  682. }
  683. canvas_set_font(canvas, FontSecondary);
  684. }