pokemon_nickname.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <ctype.h>
  2. #include <furi.h>
  3. #include <gui/modules/text_input.h>
  4. #include <gui/view_dispatcher.h>
  5. #include <stdlib.h>
  6. #include "../pokemon_app.h"
  7. #include "../pokemon_char_encode.h"
  8. #include "pokemon_menu.h"
  9. static char name_buf[11];
  10. /* NOTE:
  11. * It would be nice if we could cleanly default to the pokemon's name as their
  12. * nickname. The issue is that if you enter a blank line to text input, it does
  13. * call this function, but returning true does nothing. However, I've found that
  14. * if you check for the first char of the buffer being \0, you can then set the
  15. * buffer and then return true. This has the effect of staying in the text_input
  16. * screen, but, prepopulating the text entry with the buffer AND staying on the
  17. * save button.
  18. */
  19. static bool select_nickname_input_validator(const char* text, FuriString* error, void* context) {
  20. PokemonFap* pokemon_fap = (PokemonFap*)context;
  21. bool rc = true;
  22. if(text[0] == '\0') {
  23. /* Get the pokemon's name and populate our buffer with it */
  24. /* XXX: Nidoran M/F are still a problem with this. */
  25. pokemon_trade_block_set_default_name(name_buf, pokemon_fap, sizeof(name_buf));
  26. return true;
  27. }
  28. if(rc == false) {
  29. furi_string_printf(error, "Some error?");
  30. } else {
  31. /* Clear existing nickname in trade block*/
  32. memset(pokemon_fap->trade_block->nickname, TERM_, sizeof(struct name));
  33. /* Encode string to nickname */
  34. pokemon_str_to_encoded_array(
  35. (uint8_t*)pokemon_fap->trade_block->nickname, (char*)text, strlen(text));
  36. }
  37. return rc;
  38. }
  39. static void select_nickname_input_callback(void* context) {
  40. PokemonFap* pokemon_fap = (PokemonFap*)context;
  41. scene_manager_previous_scene(pokemon_fap->scene_manager);
  42. }
  43. void select_nickname_scene_on_exit(void* context) {
  44. PokemonFap* pokemon_fap = (PokemonFap*)context;
  45. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  46. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewOpts);
  47. }
  48. void select_nickname_scene_on_enter(void* context) {
  49. PokemonFap* pokemon_fap = (PokemonFap*)context;
  50. text_input_reset(pokemon_fap->text_input);
  51. text_input_set_validator(
  52. pokemon_fap->text_input, select_nickname_input_validator, pokemon_fap);
  53. text_input_set_result_callback(
  54. pokemon_fap->text_input,
  55. select_nickname_input_callback,
  56. pokemon_fap,
  57. name_buf,
  58. sizeof(name_buf),
  59. true);
  60. text_input_set_header_text(pokemon_fap->text_input, "Nickname (none for default)");
  61. view_dispatcher_add_view(
  62. pokemon_fap->view_dispatcher, AppViewOpts, text_input_get_view(pokemon_fap->text_input));
  63. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewOpts);
  64. }