laser_tag_view.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "laser_tag_view.h"
  2. #include "laser_tag_icons.h"
  3. #include <furi.h>
  4. #include <gui/elements.h>
  5. struct LaserTagView {
  6. View* view;
  7. };
  8. typedef struct {
  9. LaserTagTeam team;
  10. uint8_t health;
  11. uint16_t ammo;
  12. uint16_t score;
  13. uint32_t game_time;
  14. bool game_over;
  15. } LaserTagViewModel;
  16. static void laser_tag_view_draw_callback(Canvas* canvas, void* model) {
  17. LaserTagViewModel* m = model;
  18. furi_assert(m);
  19. furi_assert(canvas);
  20. canvas_clear(canvas);
  21. canvas_set_color(canvas, ColorBlack);
  22. canvas_draw_icon(canvas, 0, 0, m->team == TeamRed ? TEAM_RED_ICON : TEAM_BLUE_ICON);
  23. canvas_draw_icon(canvas, 0, 10, HEALTH_ICON);
  24. FuriString* str = furi_string_alloc_printf("%d", m->health);
  25. canvas_draw_str_aligned(canvas, 20, 14, AlignLeft, AlignBottom, furi_string_get_cstr(str));
  26. canvas_draw_icon(canvas, 0, 20, AMMO_ICON);
  27. furi_string_reset(str);
  28. furi_string_printf(str, "%d", m->ammo);
  29. canvas_draw_str_aligned(canvas, 20, 24, AlignLeft, AlignBottom, furi_string_get_cstr(str));
  30. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, "Score:");
  31. furi_string_reset(str);
  32. furi_string_printf(str, "%d", m->score);
  33. canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignBottom, furi_string_get_cstr(str));
  34. uint32_t minutes = m->game_time / 60;
  35. uint32_t seconds = m->game_time % 60;
  36. furi_string_reset(str);
  37. furi_string_printf(str, "%02ld:%02ld", minutes, seconds);
  38. canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignBottom, furi_string_get_cstr(str));
  39. canvas_draw_icon(canvas, 112, 0, LASER_GUN_ICON);
  40. if(m->game_over) {
  41. canvas_draw_icon(canvas, 56, 28, GAME_OVER_ICON);
  42. }
  43. furi_string_free(str);
  44. }
  45. static bool laser_tag_view_input_callback(InputEvent* event, void* context) {
  46. UNUSED(event);
  47. UNUSED(context);
  48. return false;
  49. }
  50. LaserTagView* laser_tag_view_alloc() {
  51. LaserTagView* laser_tag_view = malloc(sizeof(LaserTagView));
  52. if(!laser_tag_view) {
  53. return NULL;
  54. }
  55. laser_tag_view->view = view_alloc();
  56. if(!laser_tag_view->view) {
  57. free(laser_tag_view);
  58. return NULL;
  59. }
  60. view_set_context(laser_tag_view->view, laser_tag_view);
  61. view_allocate_model(laser_tag_view->view, ViewModelTypeLocking, sizeof(LaserTagViewModel));
  62. view_set_draw_callback(laser_tag_view->view, laser_tag_view_draw_callback);
  63. view_set_input_callback(laser_tag_view->view, laser_tag_view_input_callback);
  64. return laser_tag_view;
  65. }
  66. void laser_tag_view_free(LaserTagView* laser_tag_view) {
  67. if(!laser_tag_view) return;
  68. if(laser_tag_view->view) {
  69. view_free(laser_tag_view->view);
  70. }
  71. free(laser_tag_view);
  72. }
  73. void laser_tag_view_draw(View* view, Canvas* canvas) {
  74. furi_assert(view);
  75. furi_assert(canvas);
  76. LaserTagViewModel* model = view_get_model(view);
  77. laser_tag_view_draw_callback(canvas, model);
  78. view_commit_model(view, false);
  79. }
  80. View* laser_tag_view_get_view(LaserTagView* laser_tag_view) {
  81. furi_assert(laser_tag_view);
  82. return laser_tag_view->view;
  83. }
  84. void laser_tag_view_update(LaserTagView* laser_tag_view, GameState* game_state) {
  85. furi_assert(laser_tag_view);
  86. furi_assert(game_state);
  87. with_view_model(
  88. laser_tag_view->view,
  89. LaserTagViewModel * model,
  90. {
  91. model->team = game_state_get_team(game_state);
  92. model->health = game_state_get_health(game_state);
  93. model->ammo = game_state_get_ammo(game_state);
  94. model->score = game_state_get_score(game_state);
  95. model->game_time = game_state_get_time(game_state);
  96. model->game_over = game_state_is_game_over(game_state);
  97. },
  98. true);
  99. }