select_pokemon.cpp 5.6 KB

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