elements.c 9.4 KB

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