select_pokemon.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* We only handle InputTypePress at the moment */
  25. if(event->type != InputTypePress) return consumed;
  26. switch(event->key) {
  27. /* Advance to next view with the selected pokemon */
  28. case InputKeyOk:
  29. with_view_model_cpp(
  30. select_pokemon->view,
  31. SelectPokemonModel*,
  32. model,
  33. {
  34. select_pokemon->app->current_pokemon = model->current_pokemon;
  35. select_pokemon->app->pokemon_hex_code = pokemon_table[model->current_pokemon].hex;
  36. },
  37. false);
  38. view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, AppViewTrade);
  39. consumed = true;
  40. break;
  41. /* Return to the previous view */
  42. case InputKeyBack:
  43. view_dispatcher_switch_to_view(select_pokemon->app->view_dispatcher, VIEW_NONE);
  44. consumed = true;
  45. break;
  46. /* Move back one through the pokedex listing */
  47. case InputKeyLeft:
  48. with_view_model_cpp(
  49. select_pokemon->view,
  50. SelectPokemonModel*,
  51. model,
  52. {
  53. if(model->current_pokemon == 0) {
  54. model->current_pokemon = 150;
  55. } else {
  56. model->current_pokemon--;
  57. }
  58. },
  59. true);
  60. consumed = true;
  61. break;
  62. /* Move back ten through the pokemon listing, wrap to max pokemon on
  63. * underflow.
  64. */
  65. case InputKeyDown:
  66. with_view_model_cpp(
  67. select_pokemon->view,
  68. SelectPokemonModel*,
  69. model,
  70. {
  71. if(model->current_pokemon >= 10) {
  72. model->current_pokemon -= 10;
  73. } else {
  74. model->current_pokemon = 150;
  75. }
  76. },
  77. true);
  78. consumed = true;
  79. break;
  80. /* Move forward one through the pokedex listing */
  81. case InputKeyRight:
  82. with_view_model_cpp(
  83. select_pokemon->view,
  84. SelectPokemonModel*,
  85. model,
  86. {
  87. if(model->current_pokemon == 150) {
  88. model->current_pokemon = 0;
  89. } else {
  90. model->current_pokemon++;
  91. }
  92. },
  93. true);
  94. consumed = true;
  95. break;
  96. /* Move forward ten through the pokemon listing, wrap to min pokemon on
  97. * overflow.
  98. */
  99. case InputKeyUp:
  100. with_view_model_cpp(
  101. select_pokemon->view,
  102. SelectPokemonModel*,
  103. model,
  104. {
  105. if(model->current_pokemon <= 140) {
  106. model->current_pokemon += 10;
  107. } else {
  108. model->current_pokemon = 0;
  109. }
  110. },
  111. true);
  112. consumed = true;
  113. break;
  114. default:
  115. // Do Nothing
  116. break;
  117. }
  118. return consumed;
  119. }
  120. void select_pokemon_enter_callback(void* context) {
  121. furi_assert(context);
  122. UNUSED(context);
  123. }
  124. bool select_pokemon_custom_callback(uint32_t event, void* context) {
  125. UNUSED(event);
  126. furi_assert(context);
  127. SelectPokemon* select_pokemon = (SelectPokemon*)context;
  128. view_dispatcher_send_custom_event(select_pokemon->app->view_dispatcher, 0);
  129. return true;
  130. }
  131. void select_pokemon_exit_callback(void* context) {
  132. furi_assert(context);
  133. UNUSED(context);
  134. }
  135. SelectPokemon* select_pokemon_alloc(App* app) {
  136. SelectPokemon* select_pokemon = (SelectPokemon*)malloc(sizeof(SelectPokemon));
  137. select_pokemon->app = app;
  138. select_pokemon->view = view_alloc();
  139. view_set_context(select_pokemon->view, select_pokemon);
  140. view_allocate_model(select_pokemon->view, ViewModelTypeLockFree, sizeof(SelectPokemonModel));
  141. with_view_model_cpp(
  142. select_pokemon->view,
  143. SelectPokemonModel*,
  144. model,
  145. {
  146. model->current_pokemon = app->current_pokemon;
  147. model->pokemon_hex_code = app->pokemon_hex_code;
  148. },
  149. true);
  150. view_set_draw_callback(select_pokemon->view, select_pokemon_render_callback);
  151. view_set_input_callback(select_pokemon->view, select_pokemon_input_callback);
  152. view_set_enter_callback(select_pokemon->view, select_pokemon_enter_callback);
  153. view_set_custom_callback(select_pokemon->view, select_pokemon_custom_callback);
  154. view_set_exit_callback(select_pokemon->view, select_pokemon_exit_callback);
  155. return select_pokemon;
  156. }
  157. void select_pokemon_free(App* app) {
  158. furi_assert(app->select_pokemon);
  159. view_free(app->select_pokemon->view);
  160. free(app->select_pokemon);
  161. }
  162. View* select_pokemon_get_view(App* app) {
  163. furi_assert(app->select_pokemon);
  164. return app->select_pokemon->view;
  165. }