alloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "callback/alloc.h"
  2. #include "alloc/alloc.h"
  3. #include "callback/callback.h"
  4. #include <flip_storage/storage.h>
  5. bool alloc_message_view(void *context, MessageState state)
  6. {
  7. FlipWorldApp *app = (FlipWorldApp *)context;
  8. furi_check(app);
  9. if (app->view_message)
  10. {
  11. FURI_LOG_E(TAG, "Message view already allocated");
  12. return false;
  13. }
  14. switch (state)
  15. {
  16. case MessageStateAbout:
  17. easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, NULL, callback_to_submenu, &app->view_dispatcher, app);
  18. break;
  19. case MessageStateLoading:
  20. easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, NULL, NULL, &app->view_dispatcher, app);
  21. break;
  22. case MessageStateWaitingLobby:
  23. easy_flipper_set_view(&app->view_message, FlipWorldViewMessage, callback_message_draw, callback_message_input, NULL, &app->view_dispatcher, app);
  24. break;
  25. }
  26. if (!app->view_message)
  27. {
  28. FURI_LOG_E(TAG, "Failed to allocate message view");
  29. return false;
  30. }
  31. view_allocate_model(app->view_message, ViewModelTypeLockFree, sizeof(MessageModel));
  32. MessageModel *model = view_get_model(app->view_message);
  33. model->message_state = state;
  34. return true;
  35. }
  36. bool alloc_text_input_view(void *context, char *title)
  37. {
  38. FlipWorldApp *app = (FlipWorldApp *)context;
  39. furi_check(app);
  40. if (!title)
  41. {
  42. FURI_LOG_E(TAG, "Title is NULL");
  43. return false;
  44. }
  45. app->text_input_buffer_size = 64;
  46. if (!app->text_input_buffer)
  47. {
  48. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  49. {
  50. return false;
  51. }
  52. }
  53. if (!app->text_input_temp_buffer)
  54. {
  55. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  56. {
  57. return false;
  58. }
  59. }
  60. if (!app->text_input)
  61. {
  62. if (!easy_flipper_set_uart_text_input(
  63. &app->text_input,
  64. FlipWorldViewTextInput,
  65. title,
  66. app->text_input_temp_buffer,
  67. app->text_input_buffer_size,
  68. is_str(title, "SSID") ? callback_updated_wifi_ssid : is_str(title, "Password") ? callback_updated_wifi_pass
  69. : is_str(title, "Username-Login") ? callback_updated_username
  70. : callback_updated_password,
  71. callback_to_wifi_settings,
  72. &app->view_dispatcher,
  73. app))
  74. {
  75. return false;
  76. }
  77. if (!app->text_input)
  78. {
  79. return false;
  80. }
  81. char ssid[64];
  82. char pass[64];
  83. char username[64];
  84. char password[64];
  85. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  86. {
  87. if (is_str(title, "SSID"))
  88. {
  89. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  90. }
  91. else if (is_str(title, "Password"))
  92. {
  93. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  94. }
  95. else if (is_str(title, "Username-Login"))
  96. {
  97. strncpy(app->text_input_temp_buffer, username, app->text_input_buffer_size);
  98. }
  99. else if (is_str(title, "Password-Login"))
  100. {
  101. strncpy(app->text_input_temp_buffer, password, app->text_input_buffer_size);
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. bool alloc_variable_item_list(void *context, uint32_t view_id)
  108. {
  109. FlipWorldApp *app = (FlipWorldApp *)context;
  110. furi_check(app, "FlipWorldApp is NULL");
  111. char ssid[64];
  112. char pass[64];
  113. char username[64];
  114. char password[64];
  115. if (!app->variable_item_list)
  116. {
  117. switch (view_id)
  118. {
  119. case FlipWorldSubmenuIndexWiFiSettings:
  120. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
  121. {
  122. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  123. return false;
  124. }
  125. if (!app->variable_item_list)
  126. {
  127. FURI_LOG_E(TAG, "Variable item list is NULL");
  128. return false;
  129. }
  130. if (!app->variable_item_wifi_ssid)
  131. {
  132. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  133. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  134. }
  135. if (!app->variable_item_wifi_pass)
  136. {
  137. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  138. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  139. }
  140. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  141. {
  142. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  143. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  144. save_char("WiFi-SSID", ssid);
  145. save_char("WiFi-Password", pass);
  146. save_char("Flip-Social-Username", username);
  147. save_char("Flip-Social-Password", password);
  148. }
  149. break;
  150. case FlipWorldSubmenuIndexGameSettings:
  151. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_game_settings_select, callback_to_settings, &app->view_dispatcher, app))
  152. {
  153. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  154. return false;
  155. }
  156. if (!app->variable_item_list)
  157. {
  158. FURI_LOG_E(TAG, "Variable item list is NULL");
  159. return false;
  160. }
  161. if (!app->variable_item_game_download_world)
  162. {
  163. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  164. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  165. }
  166. if (!app->variable_item_game_player_sprite)
  167. {
  168. app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, callback_player_on_change, NULL);
  169. variable_item_set_current_value_index(app->variable_item_game_player_sprite, 1);
  170. variable_item_set_current_value_text(app->variable_item_game_player_sprite, player_sprite_choices[1]);
  171. }
  172. if (!app->variable_item_game_fps)
  173. {
  174. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, callback_fps_change, NULL);
  175. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  176. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[0]);
  177. }
  178. if (!app->variable_item_game_vgm_x)
  179. {
  180. app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, callback_vgm_x_change, NULL);
  181. variable_item_set_current_value_index(app->variable_item_game_vgm_x, 2);
  182. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[2]);
  183. }
  184. if (!app->variable_item_game_vgm_y)
  185. {
  186. app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, callback_vgm_y_change, NULL);
  187. variable_item_set_current_value_index(app->variable_item_game_vgm_y, 2);
  188. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[2]);
  189. }
  190. if (!app->variable_item_game_screen_always_on)
  191. {
  192. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, callback_screen_on_change, NULL);
  193. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  194. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[1]);
  195. }
  196. if (!app->variable_item_game_sound_on)
  197. {
  198. app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, callback_sound_on_change, NULL);
  199. variable_item_set_current_value_index(app->variable_item_game_sound_on, 0);
  200. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[0]);
  201. }
  202. if (!app->variable_item_game_vibration_on)
  203. {
  204. app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, callback_vibration_on_change, NULL);
  205. variable_item_set_current_value_index(app->variable_item_game_vibration_on, 0);
  206. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[0]);
  207. }
  208. char _game_player_sprite[8];
  209. if (load_char("Game-Player-Sprite", _game_player_sprite, sizeof(_game_player_sprite)))
  210. {
  211. int index = is_str(_game_player_sprite, "naked") ? 0 : is_str(_game_player_sprite, "sword") ? 1
  212. : is_str(_game_player_sprite, "axe") ? 2
  213. : is_str(_game_player_sprite, "bow") ? 3
  214. : 0;
  215. variable_item_set_current_value_index(app->variable_item_game_player_sprite, index);
  216. variable_item_set_current_value_text(
  217. app->variable_item_game_player_sprite,
  218. is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  219. }
  220. char _game_fps[8];
  221. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  222. {
  223. int index = is_str(_game_fps, "30") ? 0 : is_str(_game_fps, "60") ? 1
  224. : is_str(_game_fps, "120") ? 2
  225. : is_str(_game_fps, "240") ? 3
  226. : 0;
  227. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[index]);
  228. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  229. }
  230. char _game_vgm_x[8];
  231. if (load_char("Game-VGM-X", _game_vgm_x, sizeof(_game_vgm_x)))
  232. {
  233. int vgm_x = atoi(_game_vgm_x);
  234. int index = vgm_x == -2 ? 0 : vgm_x == -1 ? 1
  235. : vgm_x == 0 ? 2
  236. : vgm_x == 1 ? 3
  237. : vgm_x == 2 ? 4
  238. : vgm_x == 3 ? 5
  239. : vgm_x == 4 ? 6
  240. : vgm_x == 5 ? 7
  241. : vgm_x == 6 ? 8
  242. : vgm_x == 7 ? 9
  243. : vgm_x == 8 ? 10
  244. : vgm_x == 9 ? 11
  245. : vgm_x == 10 ? 12
  246. : 2;
  247. variable_item_set_current_value_index(app->variable_item_game_vgm_x, index);
  248. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[index]);
  249. }
  250. char _game_vgm_y[8];
  251. if (load_char("Game-VGM-Y", _game_vgm_y, sizeof(_game_vgm_y)))
  252. {
  253. int vgm_y = atoi(_game_vgm_y);
  254. int index = vgm_y == -2 ? 0 : vgm_y == -1 ? 1
  255. : vgm_y == 0 ? 2
  256. : vgm_y == 1 ? 3
  257. : vgm_y == 2 ? 4
  258. : vgm_y == 3 ? 5
  259. : vgm_y == 4 ? 6
  260. : vgm_y == 5 ? 7
  261. : vgm_y == 6 ? 8
  262. : vgm_y == 7 ? 9
  263. : vgm_y == 8 ? 10
  264. : vgm_y == 9 ? 11
  265. : vgm_y == 10 ? 12
  266. : 2;
  267. variable_item_set_current_value_index(app->variable_item_game_vgm_y, index);
  268. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[index]);
  269. }
  270. char _game_screen_always_on[8];
  271. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  272. {
  273. int index = is_str(_game_screen_always_on, "No") ? 0 : is_str(_game_screen_always_on, "Yes") ? 1
  274. : 0;
  275. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[index]);
  276. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  277. }
  278. char _game_sound_on[8];
  279. if (load_char("Game-Sound-On", _game_sound_on, sizeof(_game_sound_on)))
  280. {
  281. int index = is_str(_game_sound_on, "No") ? 0 : is_str(_game_sound_on, "Yes") ? 1
  282. : 0;
  283. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[index]);
  284. variable_item_set_current_value_index(app->variable_item_game_sound_on, index);
  285. }
  286. char _game_vibration_on[8];
  287. if (load_char("Game-Vibration-On", _game_vibration_on, sizeof(_game_vibration_on)))
  288. {
  289. int index = is_str(_game_vibration_on, "No") ? 0 : is_str(_game_vibration_on, "Yes") ? 1
  290. : 0;
  291. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[index]);
  292. variable_item_set_current_value_index(app->variable_item_game_vibration_on, index);
  293. }
  294. break;
  295. case FlipWorldSubmenuIndexUserSettings:
  296. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_user_settings_select, callback_to_settings, &app->view_dispatcher, app))
  297. {
  298. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  299. return false;
  300. }
  301. if (!app->variable_item_list)
  302. {
  303. FURI_LOG_E(TAG, "Variable item list is NULL");
  304. return false;
  305. }
  306. // if logged in, show profile info, otherwise show login/register
  307. if (is_logged_in() || is_logged_in_to_flip_social())
  308. {
  309. if (!app->variable_item_user_username)
  310. {
  311. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  312. variable_item_set_current_value_text(app->variable_item_user_username, "");
  313. }
  314. if (!app->variable_item_user_password)
  315. {
  316. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  317. variable_item_set_current_value_text(app->variable_item_user_password, "");
  318. }
  319. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  320. {
  321. variable_item_set_current_value_text(app->variable_item_user_username, username);
  322. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  323. }
  324. }
  325. else
  326. {
  327. if (!app->variable_item_user_username)
  328. {
  329. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  330. variable_item_set_current_value_text(app->variable_item_user_username, "");
  331. }
  332. if (!app->variable_item_user_password)
  333. {
  334. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  335. variable_item_set_current_value_text(app->variable_item_user_password, "");
  336. }
  337. }
  338. break;
  339. }
  340. }
  341. return true;
  342. }
  343. bool alloc_submenu_other(void *context, uint32_t view_id)
  344. {
  345. FlipWorldApp *app = (FlipWorldApp *)context;
  346. furi_check(app);
  347. if (app->submenu_other)
  348. {
  349. FURI_LOG_I(TAG, "Submenu already allocated");
  350. return true;
  351. }
  352. switch (view_id)
  353. {
  354. case FlipWorldViewSettings:
  355. if (!easy_flipper_set_submenu(&app->submenu_other, FlipWorldViewSubmenuOther, "Settings", callback_to_submenu, &app->view_dispatcher))
  356. {
  357. FURI_LOG_E(TAG, "Failed to allocate submenu settings");
  358. return false;
  359. }
  360. submenu_add_item(app->submenu_other, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  361. submenu_add_item(app->submenu_other, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  362. submenu_add_item(app->submenu_other, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  363. return true;
  364. case FlipWorldViewLobby:
  365. return easy_flipper_set_submenu(&app->submenu_other, FlipWorldViewSubmenuOther, "Lobbies", callback_to_submenu, &app->view_dispatcher);
  366. default:
  367. return false;
  368. }
  369. }
  370. bool alloc_game_submenu(void *context)
  371. {
  372. FlipWorldApp *app = (FlipWorldApp *)context;
  373. furi_check(app);
  374. if (!app->submenu_game)
  375. {
  376. if (!easy_flipper_set_submenu(&app->submenu_game, FlipWorldViewGameSubmenu, "Play", callback_to_submenu, &app->view_dispatcher))
  377. {
  378. return false;
  379. }
  380. if (!app->submenu_game)
  381. {
  382. return false;
  383. }
  384. submenu_add_item(app->submenu_game, "Story", FlipWorldSubmenuIndexStory, callback_submenu_choices, app);
  385. submenu_add_item(app->submenu_game, "PvE", FlipWorldSubmenuIndexPvE, callback_submenu_choices, app);
  386. submenu_add_item(app->submenu_game, "PvP", FlipWorldSubmenuIndexPvP, callback_submenu_choices, app);
  387. }
  388. return true;
  389. }