elements.c 8.4 KB

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