elements.c 19 KB

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