elements.c 27 KB

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