select_pokemon.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "../pokemon_app.h"
  2. #include "select_pokemon.hpp"
  3. static void select_pokemon_render_callback(Canvas* canvas, void* context) {
  4. canvas_clear(canvas);
  5. SelectPokemonModel* model = (SelectPokemonModel*)context;
  6. const uint8_t current_index = model->current_pokemon;
  7. char pokedex_num[5];
  8. snprintf(pokedex_num, sizeof(pokedex_num), "#%03d", current_index + 1);
  9. canvas_set_font(canvas, FontPrimary);
  10. canvas_draw_str_aligned(
  11. canvas, 55, 54 / 2, AlignLeft, AlignTop, pokemon_table[current_index].name);
  12. canvas_set_font(canvas, FontSecondary);
  13. canvas_draw_str_aligned(canvas, 55, 38, AlignLeft, AlignTop, pokedex_num);
  14. canvas_draw_icon(canvas, 0, 0, pokemon_table[current_index].icon);
  15. canvas_draw_icon(canvas, 128 - 80, 0, &I_Space_80x18);
  16. canvas_draw_str_aligned(canvas, (128 - 40), 5, AlignCenter, AlignTop, "Select Pokemon");
  17. canvas_set_font(canvas, FontPrimary);
  18. elements_button_center(canvas, "OK");
  19. }
  20. static bool select_pokemon_input_callback(InputEvent* event, void* context) {
  21. furi_assert(context);
  22. SelectPokemon* select_pokemon = (SelectPokemon*)context;
  23. bool consumed = false;
  24. if(event->type == InputTypePress && event->key == InputKeyOk) {
  25. with_view_model_cpp(
  26. select_pokemon->view,
  27. SelectPokemonModel*,
  28. model,
  29. {
  30. select_pokemon->app->current_pokemon = model->current_pokemon;
  31. select_pokemon->app->pokemon_hex_code = pokemon_table[model->current_pokemon].hex;
  32. },
  33. false);
  34. view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, AppViewTrade);
  35. consumed = true;
  36. } else if(event->type == InputTypePress && event->key == InputKeyBack) {
  37. view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, VIEW_NONE);
  38. consumed = true;
  39. } else if(event->type == InputTypePress && event->key == InputKeyLeft) {
  40. with_view_model_cpp(
  41. select_pokemon->view,
  42. SelectPokemonModel*,
  43. model,
  44. {
  45. if(model->current_pokemon == 0) {
  46. model->current_pokemon = 150;
  47. } else {
  48. model->current_pokemon--;
  49. }
  50. },
  51. true);
  52. consumed = true;
  53. } else if(event->type == InputTypePress && event->key == InputKeyDown) {
  54. with_view_model_cpp(
  55. select_pokemon->view,
  56. SelectPokemonModel*,
  57. model,
  58. {
  59. if(model->current_pokemon >= 10) {
  60. model->current_pokemon -= 10;
  61. } else {
  62. model->current_pokemon = 150;
  63. }
  64. },
  65. true);
  66. consumed = true;
  67. } else if(event->type == InputTypePress && event->key == InputKeyRight) {
  68. with_view_model_cpp(
  69. select_pokemon->view,
  70. SelectPokemonModel*,
  71. model,
  72. {
  73. if(model->current_pokemon == 150) {
  74. model->current_pokemon = 0;
  75. } else {
  76. model->current_pokemon++;
  77. }
  78. },
  79. true);
  80. consumed = true;
  81. } else if(event->type == InputTypePress && event->key == InputKeyUp) {
  82. with_view_model_cpp(
  83. select_pokemon->view,
  84. SelectPokemonModel*,
  85. model,
  86. {
  87. if(model->current_pokemon <= 140) {
  88. model->current_pokemon += 10;
  89. } else {
  90. model->current_pokemon = 0;
  91. ;
  92. }
  93. },
  94. true);
  95. consumed = true;
  96. }
  97. return consumed;
  98. }
  99. void select_pokemon_enter_callback(void* context) {
  100. furi_assert(context);
  101. UNUSED(context);
  102. }
  103. bool select_pokemon_custom_callback(uint32_t event, void* context) {
  104. UNUSED(event);
  105. furi_assert(context);
  106. SelectPokemon* select_pokemon = (SelectPokemon*)context;
  107. view_dispatcher_send_custom_event(select_pokemon->app->view_dispatcher, 0);
  108. return true;
  109. }
  110. void select_pokemon_exit_callback(void* context) {
  111. furi_assert(context);
  112. UNUSED(context);
  113. }
  114. SelectPokemon* select_pokemon_alloc(App* app) {
  115. SelectPokemon* select_pokemon = (SelectPokemon*)malloc(sizeof(SelectPokemon));
  116. select_pokemon->app = app;
  117. select_pokemon->view = view_alloc();
  118. view_set_context(select_pokemon->view, select_pokemon);
  119. view_allocate_model(select_pokemon->view, ViewModelTypeLockFree, sizeof(SelectPokemonModel));
  120. with_view_model_cpp(
  121. select_pokemon->view,
  122. SelectPokemonModel*,
  123. model,
  124. {
  125. model->current_pokemon = app->current_pokemon;
  126. model->pokemon_hex_code = app->pokemon_hex_code;
  127. },
  128. true);
  129. view_set_draw_callback(select_pokemon->view, select_pokemon_render_callback);
  130. view_set_input_callback(select_pokemon->view, select_pokemon_input_callback);
  131. view_set_enter_callback(select_pokemon->view, select_pokemon_enter_callback);
  132. view_set_custom_callback(select_pokemon->view, select_pokemon_custom_callback);
  133. view_set_exit_callback(select_pokemon->view, select_pokemon_exit_callback);
  134. return select_pokemon;
  135. }
  136. void select_pokemon_free(App* app) {
  137. furi_assert(app->select_pokemon);
  138. view_free(app->select_pokemon->view);
  139. free(app->select_pokemon);
  140. }
  141. View* select_pokemon_get_view(App* app) {
  142. furi_assert(app->select_pokemon);
  143. return app->select_pokemon->view;
  144. }