pokemon_nickname.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. unsigned int i;
  23. if(text[0] == '\0') {
  24. /* Get the pokemon's name and populate our buffer with it */
  25. /* XXX: Nidoran M/F are still a problem with this. */
  26. pokemon_trade_block_set_default_name(name_buf, pokemon_fap, sizeof(name_buf));
  27. return true;
  28. }
  29. for(i = 0; i < strlen(text); i++) {
  30. if(isdigit((unsigned int)text[i])) {
  31. furi_string_printf(error, "Name cannot\ncontain\nnumbers!");
  32. rc = false;
  33. }
  34. }
  35. if(rc == true) {
  36. /* Clear existing nickname in trade block*/
  37. memset(pokemon_fap->trade_block->nickname, TERM_, sizeof(struct name));
  38. /* Encode string to nickname */
  39. pokemon_str_to_encoded_array(
  40. (uint8_t*)pokemon_fap->trade_block->nickname, (char*)text, strlen(text));
  41. FURI_LOG_D(TAG, "[nickname] Set nickname to %s", text);
  42. }
  43. return rc;
  44. }
  45. static void select_nickname_input_callback(void* context) {
  46. PokemonFap* pokemon_fap = (PokemonFap*)context;
  47. scene_manager_previous_scene(pokemon_fap->scene_manager);
  48. }
  49. void select_nickname_scene_on_exit(void* context) {
  50. PokemonFap* pokemon_fap = (PokemonFap*)context;
  51. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  52. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewOpts);
  53. }
  54. void select_nickname_scene_on_enter(void* context) {
  55. PokemonFap* pokemon_fap = (PokemonFap*)context;
  56. text_input_reset(pokemon_fap->text_input);
  57. text_input_set_validator(
  58. pokemon_fap->text_input, select_nickname_input_validator, pokemon_fap);
  59. text_input_set_result_callback(
  60. pokemon_fap->text_input,
  61. select_nickname_input_callback,
  62. pokemon_fap,
  63. name_buf,
  64. sizeof(name_buf),
  65. true);
  66. text_input_set_header_text(pokemon_fap->text_input, "Nickname (none for default)");
  67. view_dispatcher_add_view(
  68. pokemon_fap->view_dispatcher, AppViewOpts, text_input_get_view(pokemon_fap->text_input));
  69. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewOpts);
  70. }