elements.c 26 KB

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