select_pokemon.cpp 5.5 KB

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