ui_controls.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. canvas_draw_str_aligned(
  40. canvas, TEXT_BOX_MARGIN + 2, TEXT_BOX_MARGIN + 3 + y, AlignLeft, AlignTop, text);
  41. }
  42. void ui_control_select_render(
  43. Canvas* const canvas,
  44. int16_t x,
  45. int16_t y,
  46. uint8_t width,
  47. const char* text,
  48. bool is_selected) {
  49. if(y < -TEXT_BOX_HEIGHT) {
  50. return;
  51. }
  52. if(is_selected) {
  53. canvas_draw_rframe(
  54. canvas,
  55. x + TEXT_BOX_MARGIN,
  56. TEXT_BOX_MARGIN + y,
  57. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  58. TEXT_BOX_HEIGHT,
  59. 0);
  60. canvas_draw_rframe(
  61. canvas,
  62. x + TEXT_BOX_MARGIN - 1,
  63. TEXT_BOX_MARGIN + y - 1,
  64. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN + 2,
  65. TEXT_BOX_HEIGHT + 2,
  66. 1);
  67. } else {
  68. canvas_draw_rframe(
  69. canvas,
  70. x + TEXT_BOX_MARGIN,
  71. TEXT_BOX_MARGIN + y,
  72. width - TEXT_BOX_MARGIN - TEXT_BOX_MARGIN,
  73. TEXT_BOX_HEIGHT,
  74. 1);
  75. }
  76. canvas_draw_str_aligned(
  77. canvas, x + (width >> 1), TEXT_BOX_MARGIN + 3 + y, AlignCenter, AlignTop, text);
  78. canvas_draw_icon(
  79. canvas, x + TEXT_BOX_MARGIN + 2, TEXT_BOX_MARGIN + 2 + y, &I_totp_arrow_left_8x9);
  80. canvas_draw_icon(
  81. canvas, x + width - TEXT_BOX_MARGIN - 10, TEXT_BOX_MARGIN + 2 + y, &I_totp_arrow_right_8x9);
  82. }
  83. void ui_control_button_render(
  84. Canvas* const canvas,
  85. int16_t x,
  86. int16_t y,
  87. uint8_t width,
  88. uint8_t height,
  89. const char* text,
  90. bool is_selected) {
  91. if(y < -height) {
  92. return;
  93. }
  94. if(is_selected) {
  95. canvas_draw_rbox(canvas, x, y, width, height, 1);
  96. canvas_set_color(canvas, ColorWhite);
  97. } else {
  98. canvas_draw_rframe(canvas, x, y, width, height, 1);
  99. }
  100. canvas_draw_str_aligned(
  101. canvas, x + (width >> 1), y + (height >> 1) + 1, AlignCenter, AlignCenter, text);
  102. if(is_selected) {
  103. canvas_set_color(canvas, ColorBlack);
  104. }
  105. }
  106. void ui_control_vscroll_render(
  107. Canvas* const canvas,
  108. uint8_t x,
  109. uint8_t y,
  110. uint8_t height,
  111. uint8_t position,
  112. uint8_t max_position) {
  113. canvas_draw_line(canvas, x, y, x, y + height);
  114. uint8_t block_height = height / MIN(10, max_position);
  115. uint8_t block_position_y =
  116. height * ((float)position / (float)max_position) - (block_height >> 1);
  117. uint8_t block_position_y_abs = y + block_position_y;
  118. if(block_position_y_abs + block_height > height) {
  119. block_position_y_abs = height - block_height;
  120. }
  121. canvas_draw_box(
  122. canvas,
  123. x - (UI_CONTROL_VSCROLL_WIDTH >> 1),
  124. block_position_y_abs,
  125. UI_CONTROL_VSCROLL_WIDTH,
  126. block_height);
  127. }