laser_tag_view.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. canvas_clear(canvas);
  19. canvas_set_color(canvas, ColorBlack);
  20. canvas_draw_icon(canvas, 0, 0, m->team == TeamRed ? TEAM_RED_ICON : TEAM_BLUE_ICON);
  21. canvas_draw_icon(canvas, 0, 10, HEALTH_ICON);
  22. canvas_draw_str_aligned(canvas, 20, 14, AlignLeft, AlignBottom, furi_string_get_cstr(furi_string_alloc_printf("%d", m->health)));
  23. canvas_draw_icon(canvas, 0, 20, AMMO_ICON);
  24. canvas_draw_str_aligned(canvas, 20, 24, AlignLeft, AlignBottom, furi_string_get_cstr(furi_string_alloc_printf("%d", m->ammo)));
  25. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, "Score:");
  26. canvas_draw_str_aligned(canvas, 64, 20, AlignCenter, AlignBottom, furi_string_get_cstr(furi_string_alloc_printf("%d", m->score)));
  27. uint32_t minutes = m->game_time / 60;
  28. uint32_t seconds = m->game_time % 60;
  29. canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignBottom, furi_string_get_cstr(furi_string_alloc_printf("%02ld:%02ld", minutes, seconds)));
  30. canvas_draw_icon(canvas, 112, 0, LASER_GUN_ICON);
  31. if(m->game_over) {
  32. canvas_draw_icon(canvas, 56, 28, GAME_OVER_ICON);
  33. }
  34. }
  35. static bool laser_tag_view_input_callback(InputEvent* event, void* context) {
  36. UNUSED(event);
  37. UNUSED(context);
  38. return false;
  39. }
  40. LaserTagView* laser_tag_view_alloc() {
  41. LaserTagView* laser_tag_view = malloc(sizeof(LaserTagView));
  42. laser_tag_view->view = view_alloc();
  43. view_set_context(laser_tag_view->view, laser_tag_view);
  44. view_allocate_model(laser_tag_view->view, ViewModelTypeLocking, sizeof(LaserTagViewModel));
  45. view_set_draw_callback(laser_tag_view->view, laser_tag_view_draw_callback);
  46. view_set_input_callback(laser_tag_view->view, laser_tag_view_input_callback);
  47. return laser_tag_view;
  48. }
  49. void laser_tag_view_draw(View* view, Canvas* canvas) {
  50. LaserTagViewModel* model = view_get_model(view);
  51. laser_tag_view_draw_callback(canvas, model);
  52. view_commit_model(view, false);
  53. }
  54. void laser_tag_view_free(LaserTagView* laser_tag_view) {
  55. furi_assert(laser_tag_view);
  56. view_free(laser_tag_view->view);
  57. free(laser_tag_view);
  58. }
  59. View* laser_tag_view_get_view(LaserTagView* laser_tag_view) {
  60. furi_assert(laser_tag_view);
  61. return laser_tag_view->view;
  62. }
  63. void laser_tag_view_update(LaserTagView* laser_tag_view, GameState* game_state) {
  64. furi_assert(laser_tag_view);
  65. furi_assert(game_state);
  66. with_view_model(
  67. laser_tag_view->view,
  68. LaserTagViewModel * model,
  69. {
  70. model->team = game_state_get_team(game_state);
  71. model->health = game_state_get_health(game_state);
  72. model->ammo = game_state_get_ammo(game_state);
  73. model->score = game_state_get_score(game_state);
  74. model->game_time = game_state_get_time(game_state);
  75. model->game_over = game_state_is_game_over(game_state);
  76. },
  77. true);
  78. }