elements.c 26 KB

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