ui.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. /* Return the ID of the currently selected subview, of the current
  5. * view. */
  6. int get_current_subview(ProtoViewApp *app) {
  7. return app->current_subview[app->current_view];
  8. }
  9. /* Called by view rendering callback that has subviews, to show small triangles
  10. * facing down/up if there are other subviews the user can access with up
  11. * and down. */
  12. void show_available_subviews(Canvas *canvas, ProtoViewApp *app,
  13. int last_subview)
  14. {
  15. int subview = get_current_subview(app);
  16. if (subview != 0)
  17. canvas_draw_triangle(canvas,120,5,8,5,CanvasDirectionBottomToTop);
  18. if (subview != last_subview-1)
  19. canvas_draw_triangle(canvas,120,59,8,5,CanvasDirectionTopToBottom);
  20. }
  21. /* Handle up/down keys when we are in a subview. If the function catched
  22. * such keypress, it returns true, so that the actual view input callback
  23. * knows it can just return ASAP without doing anything. */
  24. bool process_subview_updown(ProtoViewApp *app, InputEvent input, int last_subview) {
  25. int subview = get_current_subview(app);
  26. if (input.type == InputTypePress) {
  27. if (input.key == InputKeyUp) {
  28. if (subview != 0)
  29. app->current_subview[app->current_view]--;
  30. return true;
  31. } else if (input.key == InputKeyDown) {
  32. if (subview != last_subview-1)
  33. app->current_subview[app->current_view]++;
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color)
  40. {
  41. struct {
  42. uint8_t x; uint8_t y;
  43. } dir[8] = {
  44. {-1,-1},
  45. {0,-1},
  46. {1,-1},
  47. {1,0},
  48. {1,1},
  49. {0,1},
  50. {-1,1},
  51. {-1,0}
  52. };
  53. /* Rotate in all the directions writing the same string to create a
  54. * border, then write the actual string in the other color in the
  55. * middle. */
  56. canvas_set_color(canvas, border_color);
  57. for (int j = 0; j < 8; j++)
  58. canvas_draw_str(canvas,x+dir[j].x,y+dir[j].y,str);
  59. canvas_set_color(canvas, text_color);
  60. canvas_draw_str(canvas,x,y,str);
  61. canvas_set_color(canvas, ColorBlack);
  62. }