pokemon_ot_name.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 ot_name_buf[8];
  10. static bool select_ot_name_input_validator(const char* text, FuriString* error, void* context) {
  11. PokemonFap* pokemon_fap = (PokemonFap*)context;
  12. bool rc = true;
  13. unsigned int i;
  14. // XXX If no pokemon name, use default. How TF to have text input handle that?
  15. // OT name is 7 chars max on gen 1, so only take that and then fill the rest of the 11 bytes with term
  16. for(i = 0; i < sizeof(ot_name_buf); i++) {
  17. if(!isalnum((unsigned int)text[i])) {
  18. if(text[i] == '\0') break;
  19. rc = false;
  20. break;
  21. }
  22. }
  23. if(rc == false) {
  24. furi_string_printf(error, "Some error?");
  25. } else {
  26. /* Clear existing OT Name in trade block*/
  27. memset(pokemon_fap->trade_block->ot_name, TERM_, sizeof(struct name));
  28. /* Encode string to OT Name */
  29. pokemon_str_to_encoded_array(
  30. (uint8_t*)pokemon_fap->trade_block->ot_name, (char*)text, strlen(text));
  31. }
  32. FURI_LOG_D(TAG, "[ot_name] Set OT name to %s", text);
  33. return rc;
  34. }
  35. static void select_ot_name_input_callback(void* context) {
  36. PokemonFap* pokemon_fap = (PokemonFap*)context;
  37. scene_manager_previous_scene(pokemon_fap->scene_manager);
  38. }
  39. void select_ot_name_scene_on_exit(void* context) {
  40. PokemonFap* pokemon_fap = (PokemonFap*)context;
  41. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewMainMenu);
  42. view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewOpts);
  43. }
  44. void select_ot_name_scene_on_enter(void* context) {
  45. PokemonFap* pokemon_fap = (PokemonFap*)context;
  46. text_input_reset(pokemon_fap->text_input);
  47. text_input_set_validator(pokemon_fap->text_input, select_ot_name_input_validator, pokemon_fap);
  48. text_input_set_result_callback(
  49. pokemon_fap->text_input,
  50. select_ot_name_input_callback,
  51. pokemon_fap,
  52. ot_name_buf,
  53. sizeof(ot_name_buf),
  54. true);
  55. text_input_set_header_text(pokemon_fap->text_input, "Enter OT Name");
  56. view_dispatcher_add_view(
  57. pokemon_fap->view_dispatcher, AppViewOpts, text_input_get_view(pokemon_fap->text_input));
  58. view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewOpts);
  59. }