elements.c 9.0 KB

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