ui_controls.c 3.7 KB

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