xremote_scene_create.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "../xremote.h"
  2. #include "../helpers/xremote_custom_event.h"
  3. typedef enum {
  4. ButtonIndexPlus = -2,
  5. ButtonIndexSave = -1,
  6. ButtonIndexNA = 0,
  7. } ButtonIndex;
  8. static void xremote_create_callback(void* context, int32_t index, InputType type) {
  9. XRemote* app = context;
  10. uint16_t custom_type;
  11. if(type == InputTypePress) {
  12. custom_type = XRemoteCustomEventMenuSelected;
  13. } else if(type == InputTypeRelease) {
  14. custom_type = XRemoteCustomEventMenuVoid;
  15. } else if(type == InputTypeShort) {
  16. custom_type = XRemoteCustomEventMenuSelected;
  17. } else {
  18. furi_crash("Unexpected Input Type");
  19. }
  20. view_dispatcher_send_custom_event(
  21. app->view_dispatcher, xremote_custom_menu_event_pack(custom_type, index));
  22. }
  23. void xremote_scene_create_on_enter(void* context) {
  24. furi_assert(context);
  25. XRemote* app = context;
  26. ButtonMenu* button_menu = app->button_menu_create;
  27. size_t item_count = xremote_cross_remote_get_item_count(app->cross_remote);
  28. for(size_t i = 0; i < item_count; ++i) {
  29. CrossRemoteItem* item = xremote_cross_remote_get_item(app->cross_remote, i);
  30. button_menu_add_item(
  31. button_menu,
  32. xremote_cross_remote_item_get_name(item),
  33. i,
  34. xremote_create_callback,
  35. ButtonMenuItemTypeCommon,
  36. context);
  37. }
  38. button_menu_add_item(
  39. button_menu,
  40. "+",
  41. ButtonIndexPlus,
  42. xremote_create_callback,
  43. ButtonMenuItemTypeControl,
  44. context);
  45. button_menu_add_item(
  46. button_menu,
  47. "Save",
  48. ButtonIndexSave,
  49. xremote_create_callback,
  50. ButtonMenuItemTypeControl,
  51. context);
  52. button_menu_set_header(button_menu, "Add Cmd");
  53. const int16_t button_index =
  54. (signed)scene_manager_get_scene_state(app->scene_manager, XRemoteViewIdCreate);
  55. button_menu_set_selected_item(button_menu, button_index);
  56. scene_manager_set_scene_state(app->scene_manager, XRemoteSceneCreate, ButtonIndexNA);
  57. view_dispatcher_switch_to_view(app->view_dispatcher, XRemoteViewIdCreate);
  58. }
  59. bool xremote_scene_create_on_event(void* context, SceneManagerEvent event) {
  60. XRemote* app = context;
  61. bool consumed = false;
  62. if(event.type == SceneManagerEventTypeBack) {
  63. scene_manager_next_scene(app->scene_manager, XRemoteSceneMenu);
  64. consumed = true;
  65. } else if(event.type == SceneManagerEventTypeCustom) {
  66. const uint16_t custom_type = xremote_custom_menu_event_get_type(event.event);
  67. const int16_t button_index = xremote_custom_menu_event_get_value(event.event);
  68. scene_manager_set_scene_state(
  69. app->scene_manager, XRemoteSceneCreate, (unsigned)button_index);
  70. if(custom_type == XRemoteCustomEventMenuSelected && button_index < 0) {
  71. //scene_manager_set_scene_state(
  72. // app->scene_manager, XRemoteSceneCreate, (unsigned)button_index);
  73. if(button_index == ButtonIndexPlus) {
  74. scene_manager_next_scene(app->scene_manager, XRemoteSceneCreateAdd);
  75. consumed = true;
  76. } else if(button_index == ButtonIndexSave) {
  77. scene_manager_next_scene(app->scene_manager, XRemoteSceneSaveRemote);
  78. }
  79. } else if(custom_type == XRemoteCustomEventMenuSelected) {
  80. app->edit_item = button_index;
  81. scene_manager_next_scene(app->scene_manager, XRemoteSceneEditItem);
  82. }
  83. }
  84. return consumed;
  85. }
  86. void xremote_scene_create_on_exit(void* context) {
  87. XRemote* app = context;
  88. button_menu_reset(app->button_menu_create);
  89. }