elements.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. void elements_progress_bar(
  11. Canvas* canvas,
  12. uint8_t x,
  13. uint8_t y,
  14. uint8_t width,
  15. uint8_t progress,
  16. uint8_t total) {
  17. furi_assert(canvas);
  18. furi_assert(total > 0);
  19. uint8_t height = 9;
  20. uint8_t marker_width = 7;
  21. furi_assert(width > marker_width);
  22. uint8_t progress_length = ((float)progress / total) * (width - marker_width - 2);
  23. // rframe doesnt work if (radius * 2) > any rect side, so write manually
  24. uint8_t x_max = x + width - 1;
  25. uint8_t y_max = y + height - 1;
  26. canvas_draw_line(canvas, x + 3, y, x_max - 3, y);
  27. canvas_draw_line(canvas, x_max - 3, y, x_max, y + 3);
  28. canvas_draw_line(canvas, x_max, y + 3, x_max, y_max - 3);
  29. canvas_draw_line(canvas, x_max, y_max - 3, x_max - 3, y_max);
  30. canvas_draw_line(canvas, x_max - 3, y_max, x + 3, y_max);
  31. canvas_draw_line(canvas, x + 3, y_max, x, y_max - 3);
  32. canvas_draw_line(canvas, x, y_max - 3, x, y + 3);
  33. canvas_draw_line(canvas, x, y + 3, x + 3, y);
  34. canvas_draw_rbox(canvas, x + 1, y + 1, marker_width + progress_length, height - 2, 3);
  35. canvas_invert_color(canvas);
  36. canvas_draw_dot(canvas, x + progress_length + 3, y + 2);
  37. canvas_draw_dot(canvas, x + progress_length + 4, y + 2);
  38. canvas_draw_dot(canvas, x + progress_length + 5, y + 3);
  39. canvas_draw_dot(canvas, x + progress_length + 6, y + 4);
  40. canvas_invert_color(canvas);
  41. }
  42. void elements_scrollbar_pos(
  43. Canvas* canvas,
  44. uint8_t x,
  45. uint8_t y,
  46. uint8_t height,
  47. uint16_t pos,
  48. uint16_t total) {
  49. furi_assert(canvas);
  50. // prevent overflows
  51. canvas_set_color(canvas, ColorWhite);
  52. canvas_draw_box(canvas, x - 3, y, 3, height);
  53. // dot line
  54. canvas_set_color(canvas, ColorBlack);
  55. for(uint8_t i = y; i < height + y; i += 2) {
  56. canvas_draw_dot(canvas, x - 2, i);
  57. }
  58. // Position block
  59. if(total) {
  60. float block_h = ((float)height) / total;
  61. canvas_draw_box(canvas, x - 3, y + (block_h * pos), 3, MAX(block_h, 1));
  62. }
  63. }
  64. void elements_scrollbar(Canvas* canvas, uint16_t pos, uint16_t total) {
  65. furi_assert(canvas);
  66. uint8_t width = canvas_width(canvas);
  67. uint8_t height = canvas_height(canvas);
  68. // prevent overflows
  69. canvas_set_color(canvas, ColorWhite);
  70. canvas_draw_box(canvas, width - 3, 0, 3, height);
  71. // dot line
  72. canvas_set_color(canvas, ColorBlack);
  73. for(uint8_t i = 0; i < height; i += 2) {
  74. canvas_draw_dot(canvas, width - 2, i);
  75. }
  76. // Position block
  77. if(total) {
  78. float block_h = ((float)height) / total;
  79. canvas_draw_box(canvas, width - 3, block_h * pos, 3, MAX(block_h, 1));
  80. }
  81. }
  82. void elements_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  83. furi_assert(canvas);
  84. canvas_draw_line(canvas, x + 2, y, x + width - 2, y);
  85. canvas_draw_line(canvas, x + 1, y + height - 1, x + width, y + height - 1);
  86. canvas_draw_line(canvas, x + 2, y + height, x + width - 1, y + height);
  87. canvas_draw_line(canvas, x, y + 2, x, y + height - 2);
  88. canvas_draw_line(canvas, x + width - 1, y + 1, x + width - 1, y + height - 2);
  89. canvas_draw_line(canvas, x + width, y + 2, x + width, y + height - 2);
  90. canvas_draw_dot(canvas, x + 1, y + 1);
  91. }
  92. void elements_button_left(Canvas* canvas, const char* str) {
  93. const uint8_t button_height = 13;
  94. const uint8_t vertical_offset = 3;
  95. const uint8_t horizontal_offset = 3;
  96. const uint8_t string_width = canvas_string_width(canvas, str);
  97. const Icon* icon = &I_ButtonLeft_4x7;
  98. const uint8_t icon_offset = 3;
  99. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  100. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  101. const uint8_t x = 0;
  102. const uint8_t y = canvas_height(canvas);
  103. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  104. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  105. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  106. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  107. canvas_invert_color(canvas);
  108. canvas_draw_icon(
  109. canvas, x + horizontal_offset, y - button_height + vertical_offset, &I_ButtonLeft_4x7);
  110. canvas_draw_str(
  111. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  112. canvas_invert_color(canvas);
  113. }
  114. void elements_button_right(Canvas* canvas, const char* str) {
  115. const uint8_t button_height = 13;
  116. const uint8_t vertical_offset = 3;
  117. const uint8_t horizontal_offset = 3;
  118. const uint8_t string_width = canvas_string_width(canvas, str);
  119. const Icon* icon = &I_ButtonRight_4x7;
  120. const uint8_t icon_offset = 3;
  121. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  122. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  123. const uint8_t x = canvas_width(canvas);
  124. const uint8_t y = canvas_height(canvas);
  125. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  126. canvas_draw_line(canvas, x - button_width - 1, y, x - button_width - 1, y - button_height + 0);
  127. canvas_draw_line(canvas, x - button_width - 2, y, x - button_width - 2, y - button_height + 1);
  128. canvas_draw_line(canvas, x - button_width - 3, y, x - button_width - 3, y - button_height + 2);
  129. canvas_invert_color(canvas);
  130. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  131. canvas_draw_icon(
  132. canvas,
  133. x - horizontal_offset - icon->width,
  134. y - button_height + vertical_offset,
  135. &I_ButtonRight_4x7);
  136. canvas_invert_color(canvas);
  137. }
  138. void elements_button_center(Canvas* canvas, const char* str) {
  139. const uint8_t button_height = 13;
  140. const uint8_t vertical_offset = 3;
  141. const uint8_t horizontal_offset = 1;
  142. const uint8_t string_width = canvas_string_width(canvas, str);
  143. const Icon* icon = &I_ButtonCenter_7x7;
  144. const uint8_t icon_offset = 3;
  145. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  146. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  147. const uint8_t x = (canvas_width(canvas) - button_width) / 2;
  148. const uint8_t y = canvas_height(canvas);
  149. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  150. canvas_draw_line(canvas, x - 1, y, x - 1, y - button_height + 0);
  151. canvas_draw_line(canvas, x - 2, y, x - 2, y - button_height + 1);
  152. canvas_draw_line(canvas, x - 3, y, x - 3, y - button_height + 2);
  153. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  154. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  155. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  156. canvas_invert_color(canvas);
  157. canvas_draw_icon(
  158. canvas, x + horizontal_offset, y - button_height + vertical_offset, &I_ButtonCenter_7x7);
  159. canvas_draw_str(
  160. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  161. canvas_invert_color(canvas);
  162. }
  163. void elements_multiline_text_aligned(
  164. Canvas* canvas,
  165. uint8_t x,
  166. uint8_t y,
  167. Align horizontal,
  168. Align vertical,
  169. const char* text) {
  170. furi_assert(canvas);
  171. furi_assert(text);
  172. uint8_t font_height = canvas_current_font_height(canvas);
  173. string_t str;
  174. string_init(str);
  175. const char* start = text;
  176. char* end;
  177. // get lines count
  178. uint8_t i, lines_count;
  179. for(i = 0, lines_count = 0; text[i]; i++) lines_count += (text[i] == '\n');
  180. switch(vertical) {
  181. case AlignBottom:
  182. y -= font_height * lines_count;
  183. break;
  184. case AlignCenter:
  185. y -= (font_height * lines_count) / 2;
  186. break;
  187. case AlignTop:
  188. default:
  189. break;
  190. }
  191. do {
  192. end = strchr(start, '\n');
  193. if(end) {
  194. string_set_strn(str, start, end - start);
  195. } else {
  196. string_set_str(str, start);
  197. }
  198. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(str));
  199. uint8_t px_left =
  200. canvas_width(canvas) - (x - (horizontal == AlignCenter ? len_px / 2 : 0));
  201. // hacky
  202. if(len_px > px_left) {
  203. string_t buff;
  204. string_init_set(buff, str);
  205. size_t s_len = string_size(str);
  206. uint8_t end_pos = s_len - ((len_px - px_left) / (len_px / s_len) + 5);
  207. string_left(buff, end_pos);
  208. string_cat(buff, "-");
  209. string_right(str, end_pos);
  210. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, string_get_cstr(buff));
  211. string_clear(buff);
  212. start = end + 1;
  213. y += font_height;
  214. }
  215. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, string_get_cstr(str));
  216. start = end + 1;
  217. y += font_height;
  218. } while(end);
  219. string_clear(str);
  220. }
  221. void elements_multiline_text(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  222. furi_assert(canvas);
  223. furi_assert(text);
  224. uint8_t font_height = canvas_current_font_height(canvas);
  225. string_t str;
  226. string_init(str);
  227. const char* start = text;
  228. char* end;
  229. do {
  230. end = strchr(start, '\n');
  231. if(end) {
  232. string_set_strn(str, start, end - start);
  233. } else {
  234. string_set_str(str, start);
  235. }
  236. canvas_draw_str(canvas, x, y, string_get_cstr(str));
  237. start = end + 1;
  238. y += font_height;
  239. } while(end);
  240. string_clear(str);
  241. }
  242. void elements_multiline_text_framed(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  243. furi_assert(canvas);
  244. furi_assert(text);
  245. uint8_t font_y = canvas_current_font_height(canvas);
  246. uint16_t str_width = canvas_string_width(canvas, text);
  247. // count \n's
  248. uint8_t lines = 1;
  249. const char* t = text;
  250. while(*t != '\0') {
  251. if(*t == '\n') {
  252. lines++;
  253. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  254. str_width = temp_width > str_width ? temp_width : str_width;
  255. }
  256. t++;
  257. }
  258. canvas_set_color(canvas, ColorWhite);
  259. canvas_draw_box(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  260. canvas_set_color(canvas, ColorBlack);
  261. elements_multiline_text(canvas, x + 4, y - 1, text);
  262. elements_frame(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  263. }
  264. void elements_slightly_rounded_frame(
  265. Canvas* canvas,
  266. uint8_t x,
  267. uint8_t y,
  268. uint8_t width,
  269. uint8_t height) {
  270. furi_assert(canvas);
  271. canvas_draw_rframe(canvas, x, y, width, height, 1);
  272. }
  273. void elements_slightly_rounded_box(
  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_rbox(canvas, x, y, width, height, 1);
  281. }
  282. void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width) {
  283. furi_assert(canvas);
  284. furi_assert(string);
  285. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(string));
  286. if(len_px > width) {
  287. size_t s_len = string_size(string);
  288. uint8_t end_pos = s_len - ((len_px - width) / ((len_px / s_len) + 2) + 2);
  289. string_mid(string, 0, end_pos);
  290. string_cat(string, "...");
  291. }
  292. }