ui_controls.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "ui_controls.h"
  2. #include <totp_icons.h>
  3. #include "constants.h"
  4. #include <assets_icons.h>
  5. #define TEXT_BOX_HEIGHT (13)
  6. #define TEXT_BOX_MARGIN (4)
  7. void ui_control_text_box_render(
  8. Canvas* const canvas,
  9. int16_t y,
  10. const char* text,
  11. bool is_selected) {
  12. if(y < -TEXT_BOX_HEIGHT) {
  13. return;
  14. }
  15. if(is_selected) {
  16. canvas_draw_rframe(
  17. canvas,
  18. TEXT_BOX_MARGIN,
  19. TEXT_BOX_MARGIN + y,
  20. SCREEN_WIDTH - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  21. TEXT_BOX_HEIGHT,
  22. 0);
  23. canvas_draw_rframe(
  24. canvas,
  25. TEXT_BOX_MARGIN - 1,
  26. TEXT_BOX_MARGIN + y - 1,
  27. SCREEN_WIDTH - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN + 2,
  28. TEXT_BOX_HEIGHT + 2,
  29. 1);
  30. } else {
  31. canvas_draw_rframe(
  32. canvas,
  33. TEXT_BOX_MARGIN,
  34. TEXT_BOX_MARGIN + y,
  35. SCREEN_WIDTH - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  36. TEXT_BOX_HEIGHT,
  37. 1);
  38. }
  39. if(text != NULL) {
  40. canvas_draw_str_aligned(
  41. canvas, TEXT_BOX_MARGIN + 2, TEXT_BOX_MARGIN + 3 + y, AlignLeft, AlignTop, text);
  42. }
  43. }
  44. void ui_control_select_render(
  45. Canvas* const canvas,
  46. int16_t x,
  47. int16_t y,
  48. uint8_t width,
  49. const char* text,
  50. bool is_selected) {
  51. if(y < -TEXT_BOX_HEIGHT) {
  52. return;
  53. }
  54. if(is_selected) {
  55. canvas_draw_rframe(
  56. canvas,
  57. x + TEXT_BOX_MARGIN,
  58. TEXT_BOX_MARGIN + y,
  59. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  60. TEXT_BOX_HEIGHT,
  61. 0);
  62. canvas_draw_rframe(
  63. canvas,
  64. x + TEXT_BOX_MARGIN - 1,
  65. TEXT_BOX_MARGIN + y - 1,
  66. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN + 2,
  67. TEXT_BOX_HEIGHT + 2,
  68. 1);
  69. } else {
  70. canvas_draw_rframe(
  71. canvas,
  72. x + TEXT_BOX_MARGIN,
  73. TEXT_BOX_MARGIN + y,
  74. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  75. TEXT_BOX_HEIGHT,
  76. 1);
  77. }
  78. canvas_draw_str_aligned(
  79. canvas, x + (width >> 1), TEXT_BOX_MARGIN + 3 + y, AlignCenter, AlignTop, text);
  80. canvas_draw_icon(
  81. canvas, x + TEXT_BOX_MARGIN + 2, TEXT_BOX_MARGIN + 2 + y, &I_totp_arrow_left_8x9);
  82. canvas_draw_icon(
  83. canvas, x + width - TEXT_BOX_MARGIN - 10, TEXT_BOX_MARGIN + 2 + y, &I_totp_arrow_right_8x9);
  84. }
  85. void ui_control_button_render(
  86. Canvas* const canvas,
  87. int16_t x,
  88. int16_t y,
  89. uint8_t width,
  90. uint8_t height,
  91. const char* text,
  92. bool is_selected) {
  93. if(y < -height) {
  94. return;
  95. }
  96. if(is_selected) {
  97. canvas_draw_rbox(canvas, x, y, width, height, 1);
  98. canvas_set_color(canvas, ColorWhite);
  99. } else {
  100. canvas_draw_rframe(canvas, x, y, width, height, 1);
  101. }
  102. canvas_draw_str_aligned(
  103. canvas, x + (width >> 1), y + (height >> 1) + 1, AlignCenter, AlignCenter, text);
  104. if(is_selected) {
  105. canvas_set_color(canvas, ColorBlack);
  106. }
  107. }
  108. void ui_control_vscroll_render(
  109. Canvas* const canvas,
  110. uint8_t x,
  111. uint8_t y,
  112. uint8_t height,
  113. uint8_t position,
  114. uint8_t max_position) {
  115. canvas_draw_line(canvas, x, y, x, y + height);
  116. uint8_t block_height = height / MIN(10, max_position);
  117. uint8_t block_position_y =
  118. height * ((float)position / (float)max_position) - (block_height >> 1);
  119. uint8_t block_position_y_abs = y + block_position_y;
  120. if(block_position_y_abs + block_height > height) {
  121. block_position_y_abs = height - block_height;
  122. }
  123. canvas_draw_box(
  124. canvas,
  125. x - (UI_CONTROL_VSCROLL_WIDTH >> 1),
  126. block_position_y_abs,
  127. UI_CONTROL_VSCROLL_WIDTH,
  128. block_height);
  129. }