elements.c 7.5 KB

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