ui.c 2.1 KB

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