canvas_helper.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <gui/gui.h>
  2. #define WIDTH 128
  3. #define HEIGHT 64
  4. void draw_centered_boxed_str(Canvas* canvas, int x, int y, int height, int pad, const char* text) {
  5. // get width of text
  6. int w = canvas_string_width(canvas, text);
  7. canvas_draw_rframe(canvas, x, y, w + pad, height, 2);
  8. canvas_draw_str_aligned(canvas, x + pad / 2, y + height / 2, AlignLeft, AlignCenter, text);
  9. }
  10. void draw_corner_aligned(Canvas* canvas, int width, int height, Align horizontal, Align vertical) {
  11. canvas_set_color(canvas, ColorBlack);
  12. switch(horizontal) {
  13. case AlignLeft:
  14. switch(vertical) {
  15. case AlignTop:
  16. canvas_draw_rbox(canvas, 0, 0, width, height, 3);
  17. canvas_draw_box(canvas, 0, 0, width, 3);
  18. canvas_draw_box(canvas, 0, 0, 3, height);
  19. break;
  20. case AlignCenter:
  21. canvas_draw_rbox(canvas, 0, HEIGHT - height / 2, width, height, 3);
  22. canvas_draw_box(canvas, 0, HEIGHT - height / 2, 3, height);
  23. break;
  24. case AlignBottom:
  25. canvas_draw_rbox(canvas, 0, HEIGHT - height, width, height, 3);
  26. canvas_draw_box(canvas, 0, HEIGHT - height, 3, height);
  27. canvas_draw_box(canvas, 0, HEIGHT - 3, width, 3);
  28. break;
  29. default:
  30. break;
  31. }
  32. break;
  33. case AlignRight:
  34. switch(vertical) {
  35. case AlignTop:
  36. canvas_draw_rbox(canvas, WIDTH - width, 0, width, height, 3);
  37. canvas_draw_box(canvas, WIDTH - width, 0, width, 3); // bottom corner
  38. canvas_draw_box(canvas, WIDTH - 3, 0, 3, height); // right corner
  39. break;
  40. case AlignCenter:
  41. canvas_draw_rbox(canvas, WIDTH - width, HEIGHT / 2 - height / 2, width, height, 3);
  42. canvas_draw_box(canvas, WIDTH - 3, HEIGHT / 2 - height / 2, 3, height); // right corner
  43. break;
  44. case AlignBottom:
  45. canvas_draw_rbox(canvas, WIDTH - width, HEIGHT - height, width, height, 3);
  46. canvas_draw_box(canvas, WIDTH - 3, HEIGHT - height, 3, height); // right corner
  47. canvas_draw_box(canvas, WIDTH - width, HEIGHT - 3, width, 3); // bottom corner
  48. break;
  49. default:
  50. break;
  51. }
  52. break;
  53. case AlignCenter:
  54. switch(vertical) {
  55. case AlignTop:
  56. canvas_draw_rbox(canvas, WIDTH / 2 - width / 2, 0, width, height, 3);
  57. canvas_draw_box(canvas, WIDTH / 2 - width / 2, 0, width, 3); // bottom corner
  58. canvas_draw_box(canvas, WIDTH / 2 - 3, 0, 3, height); // right corner
  59. break;
  60. case AlignCenter:
  61. canvas_draw_rbox(
  62. canvas, WIDTH / 2 - width / 2, HEIGHT / 2 - height / 2, width, height, 3);
  63. canvas_draw_box(
  64. canvas, WIDTH / 2 - 3, HEIGHT / 2 - height / 2, 3, height); // right corner
  65. break;
  66. case AlignBottom:
  67. canvas_draw_rbox(canvas, WIDTH / 2 - width / 2, HEIGHT - height, width, height, 3);
  68. canvas_draw_box(canvas, WIDTH / 2 - 3, HEIGHT - height, 3, height); // right corner
  69. canvas_draw_box(canvas, WIDTH / 2 - width / 2, HEIGHT - 3, width, 3); // bottom corner
  70. break;
  71. default:
  72. break;
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. }