elements.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. px_left = canvas_width(canvas) - (x - len_px / 2);
  170. } else if(horizontal == AlignLeft) {
  171. px_left = canvas_width(canvas) - x;
  172. } else if(horizontal == AlignRight) {
  173. px_left = x;
  174. } else {
  175. furi_assert(0);
  176. }
  177. if(len_px > px_left) {
  178. uint8_t excess_symbols_approximately =
  179. ((float)len_px - px_left) / ((float)len_px / text_size);
  180. // reduce to 5 to be sure dash fit, and next line will be at least 5 symbols long
  181. excess_symbols_approximately = MAX(excess_symbols_approximately, 5);
  182. if(text_size > (excess_symbols_approximately + 5)) {
  183. result = text_size - excess_symbols_approximately - 5;
  184. } else {
  185. result = text_size - 1;
  186. }
  187. } else {
  188. result = text_size;
  189. }
  190. string_clear(str);
  191. return result;
  192. }
  193. void elements_multiline_text_aligned(
  194. Canvas* canvas,
  195. uint8_t x,
  196. uint8_t y,
  197. Align horizontal,
  198. Align vertical,
  199. const char* text) {
  200. furi_assert(canvas);
  201. furi_assert(text);
  202. uint8_t lines_count = 0;
  203. uint8_t font_height = canvas_current_font_height(canvas);
  204. string_t line;
  205. /* go through text line by line and count lines */
  206. for(const char* start = text; start[0];) {
  207. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  208. ++lines_count;
  209. start += chars_fit;
  210. start += start[0] == '\n' ? 1 : 0;
  211. }
  212. if(vertical == AlignBottom) {
  213. y -= font_height * (lines_count - 1);
  214. } else if(vertical == AlignCenter) {
  215. y -= (font_height * (lines_count - 1)) / 2;
  216. }
  217. /* go through text line by line and print them */
  218. for(const char* start = text; start[0];) {
  219. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  220. if((start[chars_fit] == '\n') || (start[chars_fit] == 0)) {
  221. string_init_printf(line, "%.*s", chars_fit, start);
  222. } else {
  223. string_init_printf(line, "%.*s-\n", chars_fit, start);
  224. }
  225. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, string_get_cstr(line));
  226. string_clear(line);
  227. y += font_height;
  228. start += chars_fit;
  229. start += start[0] == '\n' ? 1 : 0;
  230. }
  231. }
  232. void elements_multiline_text(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  233. furi_assert(canvas);
  234. furi_assert(text);
  235. uint8_t font_height = canvas_current_font_height(canvas);
  236. string_t str;
  237. string_init(str);
  238. const char* start = text;
  239. char* end;
  240. do {
  241. end = strchr(start, '\n');
  242. if(end) {
  243. string_set_strn(str, start, end - start);
  244. } else {
  245. string_set_str(str, start);
  246. }
  247. canvas_draw_str(canvas, x, y, string_get_cstr(str));
  248. start = end + 1;
  249. y += font_height;
  250. } while(end && y < 64);
  251. string_clear(str);
  252. }
  253. void elements_multiline_text_framed(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  254. furi_assert(canvas);
  255. furi_assert(text);
  256. uint8_t font_y = canvas_current_font_height(canvas);
  257. uint16_t str_width = canvas_string_width(canvas, text);
  258. // count \n's
  259. uint8_t lines = 1;
  260. const char* t = text;
  261. while(*t != '\0') {
  262. if(*t == '\n') {
  263. lines++;
  264. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  265. str_width = temp_width > str_width ? temp_width : str_width;
  266. }
  267. t++;
  268. }
  269. canvas_set_color(canvas, ColorWhite);
  270. canvas_draw_box(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  271. canvas_set_color(canvas, ColorBlack);
  272. elements_multiline_text(canvas, x + 4, y - 1, text);
  273. elements_frame(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  274. }
  275. void elements_slightly_rounded_frame(
  276. Canvas* canvas,
  277. uint8_t x,
  278. uint8_t y,
  279. uint8_t width,
  280. uint8_t height) {
  281. furi_assert(canvas);
  282. canvas_draw_rframe(canvas, x, y, width, height, 1);
  283. }
  284. void elements_slightly_rounded_box(
  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_rbox(canvas, x, y, width, height, 1);
  292. }
  293. void elements_bold_rounded_frame(
  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_set_color(canvas, ColorWhite);
  301. canvas_draw_box(canvas, x + 2, y + 2, width - 3, height - 3);
  302. canvas_set_color(canvas, ColorBlack);
  303. canvas_draw_line(canvas, x + 3, y, x + width - 3, y);
  304. canvas_draw_line(canvas, x + 2, y + 1, x + width - 2, y + 1);
  305. canvas_draw_line(canvas, x, y + 3, x, y + height - 3);
  306. canvas_draw_line(canvas, x + 1, y + 2, x + 1, y + height - 2);
  307. canvas_draw_line(canvas, x + width, y + 3, x + width, y + height - 3);
  308. canvas_draw_line(canvas, x + width - 1, y + 2, x + width - 1, y + height - 2);
  309. canvas_draw_line(canvas, x + 3, y + height, x + width - 3, y + height);
  310. canvas_draw_line(canvas, x + 2, y + height - 1, x + width - 2, y + height - 1);
  311. canvas_draw_dot(canvas, x + 2, y + 2);
  312. canvas_draw_dot(canvas, x + 3, y + 2);
  313. canvas_draw_dot(canvas, x + 2, y + 3);
  314. canvas_draw_dot(canvas, x + width - 2, y + 2);
  315. canvas_draw_dot(canvas, x + width - 3, y + 2);
  316. canvas_draw_dot(canvas, x + width - 2, y + 3);
  317. canvas_draw_dot(canvas, x + 2, y + height - 2);
  318. canvas_draw_dot(canvas, x + 3, y + height - 2);
  319. canvas_draw_dot(canvas, x + 2, y + height - 3);
  320. canvas_draw_dot(canvas, x + width - 2, y + height - 2);
  321. canvas_draw_dot(canvas, x + width - 3, y + height - 2);
  322. canvas_draw_dot(canvas, x + width - 2, y + height - 3);
  323. }
  324. void elements_bubble(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  325. furi_assert(canvas);
  326. canvas_draw_rframe(canvas, x + 4, y, width, height, 3);
  327. uint8_t y_corner = y + height * 2 / 3;
  328. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner - 4);
  329. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner + 4);
  330. canvas_set_color(canvas, ColorWhite);
  331. canvas_draw_line(canvas, x + 4, y_corner - 3, x + 4, y_corner + 3);
  332. canvas_set_color(canvas, ColorBlack);
  333. }
  334. void elements_bubble_str(
  335. Canvas* canvas,
  336. uint8_t x,
  337. uint8_t y,
  338. const char* text,
  339. Align horizontal,
  340. Align vertical) {
  341. furi_assert(canvas);
  342. furi_assert(text);
  343. uint8_t font_y = canvas_current_font_height(canvas);
  344. uint16_t str_width = canvas_string_width(canvas, text);
  345. // count \n's
  346. uint8_t lines = 1;
  347. const char* t = text;
  348. while(*t != '\0') {
  349. if(*t == '\n') {
  350. lines++;
  351. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  352. str_width = temp_width > str_width ? temp_width : str_width;
  353. }
  354. t++;
  355. }
  356. uint8_t frame_x = x;
  357. uint8_t frame_y = y;
  358. uint8_t frame_width = str_width + 8;
  359. uint8_t frame_height = font_y * lines + 4;
  360. canvas_set_color(canvas, ColorWhite);
  361. canvas_draw_box(canvas, frame_x + 1, frame_y + 1, frame_width - 2, frame_height - 2);
  362. canvas_set_color(canvas, ColorBlack);
  363. canvas_draw_rframe(canvas, frame_x, frame_y, frame_width, frame_height, 1);
  364. elements_multiline_text(canvas, x + 4, y - 1 + font_y, text);
  365. uint8_t x1 = 0;
  366. uint8_t x2 = 0;
  367. uint8_t x3 = 0;
  368. uint8_t y1 = 0;
  369. uint8_t y2 = 0;
  370. uint8_t y3 = 0;
  371. if((horizontal == AlignLeft) && (vertical == AlignTop)) {
  372. x1 = frame_x;
  373. y1 = frame_y;
  374. x2 = frame_x - 4;
  375. y2 = frame_y;
  376. x3 = frame_x;
  377. y3 = frame_y + 4;
  378. canvas_set_color(canvas, ColorWhite);
  379. canvas_draw_box(canvas, x2 + 2, y2 + 1, 2, 2);
  380. canvas_set_color(canvas, ColorBlack);
  381. } else if((horizontal == AlignLeft) && (vertical == AlignCenter)) {
  382. x1 = frame_x;
  383. y1 = frame_y + (frame_height - 1) / 2 - 4;
  384. x2 = frame_x - 4;
  385. y2 = frame_y + (frame_height - 1) / 2;
  386. x3 = frame_x;
  387. y3 = frame_y + (frame_height - 1) / 2 + 4;
  388. canvas_set_color(canvas, ColorWhite);
  389. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 5);
  390. canvas_draw_dot(canvas, x2 + 1, y2);
  391. canvas_set_color(canvas, ColorBlack);
  392. } else if((horizontal == AlignLeft) && (vertical == AlignBottom)) {
  393. x1 = frame_x;
  394. y1 = frame_y + (frame_height - 1) - 4;
  395. x2 = frame_x - 4;
  396. y2 = frame_y + (frame_height - 1);
  397. x3 = frame_x;
  398. y3 = frame_y + (frame_height - 1);
  399. canvas_set_color(canvas, ColorWhite);
  400. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 2);
  401. canvas_set_color(canvas, ColorBlack);
  402. } else if((horizontal == AlignRight) && (vertical == AlignTop)) {
  403. x1 = frame_x + (frame_width - 1);
  404. y1 = frame_y;
  405. x2 = frame_x + (frame_width - 1) + 4;
  406. y2 = frame_y;
  407. x3 = frame_x + (frame_width - 1);
  408. y3 = frame_y + 4;
  409. canvas_set_color(canvas, ColorWhite);
  410. canvas_draw_box(canvas, x2 - 3, y2 + 1, 2, 2);
  411. canvas_set_color(canvas, ColorBlack);
  412. } else if((horizontal == AlignRight) && (vertical == AlignCenter)) {
  413. x1 = frame_x + (frame_width - 1);
  414. y1 = frame_y + (frame_height - 1) / 2 - 4;
  415. x2 = frame_x + (frame_width - 1) + 4;
  416. y2 = frame_y + (frame_height - 1) / 2;
  417. x3 = frame_x + (frame_width - 1);
  418. y3 = frame_y + (frame_height - 1) / 2 + 4;
  419. canvas_set_color(canvas, ColorWhite);
  420. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 5);
  421. canvas_draw_dot(canvas, x2 - 1, y2);
  422. canvas_set_color(canvas, ColorBlack);
  423. } else if((horizontal == AlignRight) && (vertical == AlignBottom)) {
  424. x1 = frame_x + (frame_width - 1);
  425. y1 = frame_y + (frame_height - 1) - 4;
  426. x2 = frame_x + (frame_width - 1) + 4;
  427. y2 = frame_y + (frame_height - 1);
  428. x3 = frame_x + (frame_width - 1);
  429. y3 = frame_y + (frame_height - 1);
  430. canvas_set_color(canvas, ColorWhite);
  431. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 2);
  432. canvas_set_color(canvas, ColorBlack);
  433. } else if((horizontal == AlignCenter) && (vertical == AlignTop)) {
  434. x1 = frame_x + (frame_width - 1) / 2 - 4;
  435. y1 = frame_y;
  436. x2 = frame_x + (frame_width - 1) / 2;
  437. y2 = frame_y - 4;
  438. x3 = frame_x + (frame_width - 1) / 2 + 4;
  439. y3 = frame_y;
  440. canvas_set_color(canvas, ColorWhite);
  441. canvas_draw_box(canvas, x2 - 2, y2 + 2, 5, 2);
  442. canvas_draw_dot(canvas, x2, y2 + 1);
  443. canvas_set_color(canvas, ColorBlack);
  444. } else if((horizontal == AlignCenter) && (vertical == AlignBottom)) {
  445. x1 = frame_x + (frame_width - 1) / 2 - 4;
  446. y1 = frame_y + (frame_height - 1);
  447. x2 = frame_x + (frame_width - 1) / 2;
  448. y2 = frame_y + (frame_height - 1) + 4;
  449. x3 = frame_x + (frame_width - 1) / 2 + 4;
  450. y3 = frame_y + (frame_height - 1);
  451. canvas_set_color(canvas, ColorWhite);
  452. canvas_draw_box(canvas, x2 - 2, y2 - 3, 5, 2);
  453. canvas_draw_dot(canvas, x2, y2 - 1);
  454. canvas_set_color(canvas, ColorBlack);
  455. }
  456. canvas_set_color(canvas, ColorWhite);
  457. canvas_draw_line(canvas, x3, y3, x1, y1);
  458. canvas_set_color(canvas, ColorBlack);
  459. canvas_draw_line(canvas, x1, y1, x2, y2);
  460. canvas_draw_line(canvas, x2, y2, x3, y3);
  461. }
  462. void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width) {
  463. furi_assert(canvas);
  464. furi_assert(string);
  465. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(string));
  466. if(len_px > width) {
  467. width -= canvas_string_width(canvas, "...");
  468. do {
  469. string_left(string, string_size(string) - 1);
  470. len_px = canvas_string_width(canvas, string_get_cstr(string));
  471. } while(len_px > width);
  472. string_cat(string, "...");
  473. }
  474. }
  475. void elements_text_box(
  476. Canvas* canvas,
  477. uint8_t x,
  478. uint8_t y,
  479. uint8_t width,
  480. uint8_t height,
  481. Align horizontal,
  482. Align vertical,
  483. const char* text) {
  484. furi_assert(canvas);
  485. ElementTextBoxLine line[ELEMENTS_MAX_LINES_NUM];
  486. bool bold = false;
  487. bool mono = false;
  488. bool inversed = false;
  489. bool inversed_present = false;
  490. Font current_font = FontSecondary;
  491. Font prev_font = FontSecondary;
  492. CanvasFontParameters* font_params = canvas_get_font_params(canvas, current_font);
  493. // Fill line parameters
  494. uint8_t line_leading_min = font_params->leading_min;
  495. uint8_t line_leading_default = font_params->leading_default;
  496. uint8_t line_height = font_params->height;
  497. uint8_t line_descender = font_params->descender;
  498. uint8_t line_num = 0;
  499. uint8_t line_width = 0;
  500. uint8_t line_len = 0;
  501. uint8_t total_height_min = 0;
  502. uint8_t total_height_default = 0;
  503. uint16_t i = 0;
  504. bool full_text_processed = false;
  505. canvas_set_font(canvas, FontSecondary);
  506. // Fill all lines
  507. line[0].text = text;
  508. for(i = 0; !full_text_processed; i++) {
  509. line_len++;
  510. // Identify line height
  511. if(prev_font != current_font) {
  512. font_params = canvas_get_font_params(canvas, current_font);
  513. line_leading_min = MAX(line_leading_min, font_params->leading_min);
  514. line_leading_default = MAX(line_leading_default, font_params->leading_default);
  515. line_height = MAX(line_height, font_params->height);
  516. line_descender = MAX(line_descender, font_params->descender);
  517. prev_font = current_font;
  518. }
  519. // Set the font
  520. if(text[i] == '\e' && text[i + 1]) {
  521. i++;
  522. line_len++;
  523. if(text[i] == ELEMENTS_BOLD_MARKER) {
  524. if(bold) {
  525. current_font = FontSecondary;
  526. } else {
  527. current_font = FontPrimary;
  528. }
  529. canvas_set_font(canvas, current_font);
  530. bold = !bold;
  531. }
  532. if(text[i] == ELEMENTS_MONO_MARKER) {
  533. if(mono) {
  534. current_font = FontSecondary;
  535. } else {
  536. current_font = FontKeyboard;
  537. }
  538. canvas_set_font(canvas, FontKeyboard);
  539. mono = !mono;
  540. }
  541. if(text[i] == ELEMENTS_INVERSED_MARKER) {
  542. inversed_present = true;
  543. }
  544. continue;
  545. }
  546. if(text[i] != '\n') {
  547. line_width += canvas_glyph_width(canvas, text[i]);
  548. }
  549. // Process new line
  550. if(text[i] == '\n' || text[i] == '\0' || line_width > width) {
  551. if(line_width > width) {
  552. line_width -= canvas_glyph_width(canvas, text[i--]);
  553. line_len--;
  554. }
  555. if(text[i] == '\0') {
  556. full_text_processed = true;
  557. }
  558. if(inversed_present) {
  559. line_leading_min += 1;
  560. line_leading_default += 1;
  561. inversed_present = false;
  562. }
  563. line[line_num].leading_min = line_leading_min;
  564. line[line_num].leading_default = line_leading_default;
  565. line[line_num].height = line_height;
  566. line[line_num].descender = line_descender;
  567. if(total_height_min + line_leading_min > height) {
  568. break;
  569. }
  570. total_height_min += line_leading_min;
  571. total_height_default += line_leading_default;
  572. line[line_num].len = line_len;
  573. if(horizontal == AlignCenter) {
  574. line[line_num].x = x + (width - line_width) / 2;
  575. } else if(horizontal == AlignRight) {
  576. line[line_num].x = x + (width - line_width);
  577. } else {
  578. line[line_num].x = x;
  579. }
  580. line[line_num].y = total_height_min;
  581. line_num++;
  582. if(text[i + 1]) {
  583. line[line_num].text = &text[i + 1];
  584. }
  585. line_leading_min = font_params->leading_min;
  586. line_height = font_params->height;
  587. line_descender = font_params->descender;
  588. line_width = 0;
  589. line_len = 0;
  590. }
  591. }
  592. // Set vertical alignment for all lines
  593. if(full_text_processed) {
  594. if(total_height_default < height) {
  595. if(vertical == AlignTop) {
  596. line[0].y = y + line[0].height;
  597. } else if(vertical == AlignCenter) {
  598. line[0].y = y + line[0].height + (height - total_height_default) / 2;
  599. } else if(vertical == AlignBottom) {
  600. line[0].y = y + line[0].height + (height - total_height_default);
  601. }
  602. if(line_num > 1) {
  603. for(uint8_t i = 1; i < line_num; i++) {
  604. line[i].y = line[i - 1].y + line[i - 1].leading_default;
  605. }
  606. }
  607. } else if(line_num > 1) {
  608. uint8_t free_pixel_num = height - total_height_min;
  609. uint8_t fill_pixel = 0;
  610. uint8_t j = 1;
  611. line[0].y = y + line[0].height;
  612. while(fill_pixel < free_pixel_num) {
  613. line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
  614. fill_pixel++;
  615. j = j % (line_num - 1) + 1;
  616. }
  617. }
  618. }
  619. // Draw line by line
  620. canvas_set_font(canvas, FontSecondary);
  621. bold = false;
  622. mono = false;
  623. inversed = false;
  624. for(uint8_t i = 0; i < line_num; i++) {
  625. for(uint8_t j = 0; j < line[i].len; j++) {
  626. // Process format symbols
  627. if(line[i].text[j] == ELEMENTS_BOLD_MARKER) {
  628. if(bold) {
  629. current_font = FontSecondary;
  630. } else {
  631. current_font = FontPrimary;
  632. }
  633. canvas_set_font(canvas, current_font);
  634. bold = !bold;
  635. continue;
  636. }
  637. if(line[i].text[j] == ELEMENTS_MONO_MARKER) {
  638. if(mono) {
  639. current_font = FontSecondary;
  640. } else {
  641. current_font = FontKeyboard;
  642. }
  643. canvas_set_font(canvas, current_font);
  644. mono = !mono;
  645. continue;
  646. }
  647. if(line[i].text[j] == ELEMENTS_INVERSED_MARKER) {
  648. inversed = !inversed;
  649. continue;
  650. }
  651. if(inversed) {
  652. canvas_draw_box(
  653. canvas,
  654. line[i].x - 1,
  655. line[i].y - line[i].height - 1,
  656. canvas_glyph_width(canvas, line[i].text[j]) + 1,
  657. line[i].height + line[i].descender + 2);
  658. canvas_invert_color(canvas);
  659. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  660. canvas_invert_color(canvas);
  661. } else {
  662. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  663. }
  664. line[i].x += canvas_glyph_width(canvas, line[i].text[j]);
  665. }
  666. }
  667. canvas_set_font(canvas, FontSecondary);
  668. }