elements.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "elements.h"
  2. #include <assets_icons.h>
  3. #include <gui/icon_i.h>
  4. #include <furi.h>
  5. #include "canvas_i.h"
  6. #include <string.h>
  7. #include <stdint.h>
  8. void elements_scrollbar_pos(
  9. Canvas* canvas,
  10. uint8_t x,
  11. uint8_t y,
  12. uint8_t height,
  13. uint16_t pos,
  14. uint16_t total) {
  15. furi_assert(canvas);
  16. // prevent overflows
  17. canvas_set_color(canvas, ColorWhite);
  18. canvas_draw_box(canvas, x - 3, y, 3, height);
  19. // dot line
  20. canvas_set_color(canvas, ColorBlack);
  21. for(uint8_t i = y; i < height + y; i += 2) {
  22. canvas_draw_dot(canvas, x - 2, i);
  23. }
  24. // Position block
  25. if(total) {
  26. float block_h = ((float)height) / total;
  27. canvas_draw_box(canvas, x - 3, y + (block_h * pos), 3, MAX(block_h, 1));
  28. }
  29. }
  30. void elements_scrollbar(Canvas* canvas, uint8_t pos, uint8_t total) {
  31. furi_assert(canvas);
  32. uint8_t width = canvas_width(canvas);
  33. uint8_t height = canvas_height(canvas);
  34. // prevent overflows
  35. canvas_set_color(canvas, ColorWhite);
  36. canvas_draw_box(canvas, width - 3, 0, 3, height);
  37. // dot line
  38. canvas_set_color(canvas, ColorBlack);
  39. for(uint8_t i = 0; i < height; i += 2) {
  40. canvas_draw_dot(canvas, width - 2, i);
  41. }
  42. // Position block
  43. if(total) {
  44. uint8_t block_h = ((float)height) / total;
  45. canvas_draw_box(canvas, width - 3, block_h * pos, 3, MAX(block_h, 1));
  46. }
  47. }
  48. void elements_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  49. furi_assert(canvas);
  50. canvas_draw_line(canvas, x + 2, y, x + width - 2, y);
  51. canvas_draw_line(canvas, x + 1, y + height - 1, x + width, y + height - 1);
  52. canvas_draw_line(canvas, x + 2, y + height, x + width - 1, y + height);
  53. canvas_draw_line(canvas, x, y + 2, x, y + height - 2);
  54. canvas_draw_line(canvas, x + width - 1, y + 1, x + width - 1, y + height - 2);
  55. canvas_draw_line(canvas, x + width, y + 2, x + width, y + height - 2);
  56. canvas_draw_dot(canvas, x + 1, y + 1);
  57. }
  58. void elements_button_left(Canvas* canvas, const char* str) {
  59. const uint8_t button_height = 13;
  60. const uint8_t vertical_offset = 3;
  61. const uint8_t horizontal_offset = 3;
  62. const uint8_t string_width = canvas_string_width(canvas, str);
  63. const IconData* icon = assets_icons_get_data(I_ButtonLeft_4x7);
  64. const uint8_t icon_offset = 6;
  65. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  66. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  67. const uint8_t x = 0;
  68. const uint8_t y = canvas_height(canvas);
  69. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  70. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  71. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  72. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  73. canvas_invert_color(canvas);
  74. canvas_draw_icon_name(
  75. canvas, x + horizontal_offset, y - button_height + vertical_offset, I_ButtonLeft_4x7);
  76. canvas_draw_str(
  77. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  78. canvas_invert_color(canvas);
  79. }
  80. void elements_button_right(Canvas* canvas, const char* str) {
  81. const uint8_t button_height = 13;
  82. const uint8_t vertical_offset = 3;
  83. const uint8_t horizontal_offset = 3;
  84. const uint8_t string_width = canvas_string_width(canvas, str);
  85. const IconData* icon = assets_icons_get_data(I_ButtonRight_4x7);
  86. const uint8_t icon_offset = 6;
  87. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  88. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  89. const uint8_t x = canvas_width(canvas);
  90. const uint8_t y = canvas_height(canvas);
  91. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  92. canvas_draw_line(canvas, x - button_width - 1, y, x - button_width - 1, y - button_height + 0);
  93. canvas_draw_line(canvas, x - button_width - 2, y, x - button_width - 2, y - button_height + 1);
  94. canvas_draw_line(canvas, x - button_width - 3, y, x - button_width - 3, y - button_height + 2);
  95. canvas_invert_color(canvas);
  96. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  97. canvas_draw_icon_name(
  98. canvas,
  99. x - horizontal_offset - icon->width,
  100. y - button_height + vertical_offset,
  101. I_ButtonRight_4x7);
  102. canvas_invert_color(canvas);
  103. }
  104. void elements_button_center(Canvas* canvas, const char* str) {
  105. const uint8_t button_height = 13;
  106. const uint8_t vertical_offset = 3;
  107. const uint8_t horizontal_offset = 3;
  108. const uint8_t string_width = canvas_string_width(canvas, str);
  109. const IconData* icon = assets_icons_get_data(I_ButtonCenter_7x7);
  110. const uint8_t icon_offset = 6;
  111. const uint8_t icon_width_with_offset = icon->width + icon_offset;
  112. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  113. const uint8_t x = (canvas_width(canvas) - button_width) / 2;
  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 - 1, y, x - 1, y - button_height + 0);
  117. canvas_draw_line(canvas, x - 2, y, x - 2, y - button_height + 1);
  118. canvas_draw_line(canvas, x - 3, y, x - 3, y - button_height + 2);
  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_name(
  124. canvas, x + horizontal_offset, y - button_height + vertical_offset, I_ButtonCenter_7x7);
  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_multiline_text_aligned(
  130. Canvas* canvas,
  131. uint8_t x,
  132. uint8_t y,
  133. Align horizontal,
  134. Align vertical,
  135. const char* text) {
  136. furi_assert(canvas);
  137. furi_assert(text);
  138. uint8_t font_height = canvas_current_font_height(canvas) + 2;
  139. string_t str;
  140. string_init(str);
  141. const char* start = text;
  142. char* end;
  143. // get lines count
  144. uint8_t i, lines_count;
  145. for(i = 0, lines_count = 0; text[i]; i++) lines_count += (text[i] == '\n');
  146. switch(vertical) {
  147. case AlignBottom:
  148. y -= font_height * lines_count;
  149. break;
  150. case AlignCenter:
  151. y -= (font_height * lines_count) / 2;
  152. break;
  153. case AlignTop:
  154. default:
  155. break;
  156. }
  157. do {
  158. end = strchr(start, '\n');
  159. if(end) {
  160. string_set_strn(str, start, end - start);
  161. } else {
  162. string_set_str(str, start);
  163. }
  164. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, string_get_cstr(str));
  165. start = end + 1;
  166. y += font_height;
  167. } while(end);
  168. string_clear(str);
  169. }
  170. void elements_multiline_text(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  171. furi_assert(canvas);
  172. furi_assert(text);
  173. uint8_t font_height = canvas_current_font_height(canvas);
  174. string_t str;
  175. string_init(str);
  176. const char* start = text;
  177. char* end;
  178. do {
  179. end = strchr(start, '\n');
  180. if(end) {
  181. string_set_strn(str, start, end - start);
  182. } else {
  183. string_set_str(str, start);
  184. }
  185. canvas_draw_str(canvas, x, y, string_get_cstr(str));
  186. start = end + 1;
  187. y += font_height;
  188. } while(end);
  189. string_clear(str);
  190. }
  191. void elements_multiline_text_framed(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  192. furi_assert(canvas);
  193. furi_assert(text);
  194. uint8_t font_y = canvas_current_font_height(canvas);
  195. uint16_t str_width = canvas_string_width(canvas, text);
  196. // count \n's
  197. uint8_t lines = 1;
  198. const char* t = text;
  199. while(*t != '\0') {
  200. if(*t == '\n') {
  201. lines++;
  202. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  203. str_width = temp_width > str_width ? temp_width : str_width;
  204. }
  205. t++;
  206. }
  207. canvas_set_color(canvas, ColorWhite);
  208. canvas_draw_box(canvas, x, y - font_y, str_width + 8, font_y * lines + 6);
  209. canvas_set_color(canvas, ColorBlack);
  210. elements_multiline_text(canvas, x + 4, y + 1, text);
  211. elements_frame(canvas, x, y - font_y, str_width + 8, font_y * lines + 6);
  212. }
  213. void elements_slightly_rounded_frame(
  214. Canvas* canvas,
  215. uint8_t x,
  216. uint8_t y,
  217. uint8_t width,
  218. uint8_t height) {
  219. furi_assert(canvas);
  220. canvas_draw_frame(canvas, x, y, width, height);
  221. canvas_invert_color(canvas);
  222. canvas_draw_dot(canvas, x, y);
  223. canvas_draw_dot(canvas, x + width - 1, y + height - 1);
  224. canvas_draw_dot(canvas, x + width - 1, y);
  225. canvas_draw_dot(canvas, x, y + height - 1);
  226. canvas_invert_color(canvas);
  227. }
  228. void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width) {
  229. furi_assert(canvas);
  230. furi_assert(string);
  231. uint16_t len_px = canvas_string_width(canvas, string_get_cstr(string));
  232. if(len_px > width) {
  233. size_t s_len = strlen(string_get_cstr(string));
  234. uint8_t end_pos = s_len - ((len_px - width) / ((len_px / s_len) + 2) + 2);
  235. string_mid(string, 0, end_pos);
  236. string_cat(string, "...");
  237. }
  238. }