alloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. if (!app)
  111. {
  112. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  113. return false;
  114. }
  115. char ssid[64];
  116. char pass[64];
  117. char username[64];
  118. char password[64];
  119. if (!app->variable_item_list)
  120. {
  121. switch (view_id)
  122. {
  123. case FlipWorldSubmenuIndexWiFiSettings:
  124. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_wifi_settings_select, callback_to_settings, &app->view_dispatcher, app))
  125. {
  126. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  127. return false;
  128. }
  129. if (!app->variable_item_list)
  130. {
  131. FURI_LOG_E(TAG, "Variable item list is NULL");
  132. return false;
  133. }
  134. if (!app->variable_item_wifi_ssid)
  135. {
  136. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  137. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  138. }
  139. if (!app->variable_item_wifi_pass)
  140. {
  141. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  142. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  143. }
  144. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  145. {
  146. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  147. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  148. save_char("WiFi-SSID", ssid);
  149. save_char("WiFi-Password", pass);
  150. save_char("Flip-Social-Username", username);
  151. save_char("Flip-Social-Password", password);
  152. }
  153. break;
  154. case FlipWorldSubmenuIndexGameSettings:
  155. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_game_settings_select, callback_to_settings, &app->view_dispatcher, app))
  156. {
  157. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  158. return false;
  159. }
  160. if (!app->variable_item_list)
  161. {
  162. FURI_LOG_E(TAG, "Variable item list is NULL");
  163. return false;
  164. }
  165. if (!app->variable_item_game_download_world)
  166. {
  167. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  168. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  169. }
  170. if (!app->variable_item_game_player_sprite)
  171. {
  172. app->variable_item_game_player_sprite = variable_item_list_add(app->variable_item_list, "Weapon", 4, callback_player_on_change, NULL);
  173. variable_item_set_current_value_index(app->variable_item_game_player_sprite, 1);
  174. variable_item_set_current_value_text(app->variable_item_game_player_sprite, player_sprite_choices[1]);
  175. }
  176. if (!app->variable_item_game_fps)
  177. {
  178. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, callback_fps_change, NULL);
  179. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  180. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[0]);
  181. }
  182. if (!app->variable_item_game_vgm_x)
  183. {
  184. app->variable_item_game_vgm_x = variable_item_list_add(app->variable_item_list, "VGM Horizontal", 12, callback_vgm_x_change, NULL);
  185. variable_item_set_current_value_index(app->variable_item_game_vgm_x, 2);
  186. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[2]);
  187. }
  188. if (!app->variable_item_game_vgm_y)
  189. {
  190. app->variable_item_game_vgm_y = variable_item_list_add(app->variable_item_list, "VGM Vertical", 12, callback_vgm_y_change, NULL);
  191. variable_item_set_current_value_index(app->variable_item_game_vgm_y, 2);
  192. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[2]);
  193. }
  194. if (!app->variable_item_game_screen_always_on)
  195. {
  196. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, callback_screen_on_change, NULL);
  197. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  198. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[1]);
  199. }
  200. if (!app->variable_item_game_sound_on)
  201. {
  202. app->variable_item_game_sound_on = variable_item_list_add(app->variable_item_list, "Sound On?", 2, callback_sound_on_change, NULL);
  203. variable_item_set_current_value_index(app->variable_item_game_sound_on, 0);
  204. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[0]);
  205. }
  206. if (!app->variable_item_game_vibration_on)
  207. {
  208. app->variable_item_game_vibration_on = variable_item_list_add(app->variable_item_list, "Vibration On?", 2, callback_vibration_on_change, NULL);
  209. variable_item_set_current_value_index(app->variable_item_game_vibration_on, 0);
  210. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[0]);
  211. }
  212. char _game_player_sprite[8];
  213. if (load_char("Game-Player-Sprite", _game_player_sprite, sizeof(_game_player_sprite)))
  214. {
  215. int index = is_str(_game_player_sprite, "naked") ? 0 : is_str(_game_player_sprite, "sword") ? 1
  216. : is_str(_game_player_sprite, "axe") ? 2
  217. : is_str(_game_player_sprite, "bow") ? 3
  218. : 0;
  219. variable_item_set_current_value_index(app->variable_item_game_player_sprite, index);
  220. variable_item_set_current_value_text(
  221. app->variable_item_game_player_sprite,
  222. is_str(player_sprite_choices[index], "naked") ? "None" : player_sprite_choices[index]);
  223. }
  224. char _game_fps[8];
  225. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  226. {
  227. int index = is_str(_game_fps, "30") ? 0 : is_str(_game_fps, "60") ? 1
  228. : is_str(_game_fps, "120") ? 2
  229. : is_str(_game_fps, "240") ? 3
  230. : 0;
  231. variable_item_set_current_value_text(app->variable_item_game_fps, fps_choices_str[index]);
  232. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  233. }
  234. char _game_vgm_x[8];
  235. if (load_char("Game-VGM-X", _game_vgm_x, sizeof(_game_vgm_x)))
  236. {
  237. int vgm_x = atoi(_game_vgm_x);
  238. int index = vgm_x == -2 ? 0 : vgm_x == -1 ? 1
  239. : vgm_x == 0 ? 2
  240. : vgm_x == 1 ? 3
  241. : vgm_x == 2 ? 4
  242. : vgm_x == 3 ? 5
  243. : vgm_x == 4 ? 6
  244. : vgm_x == 5 ? 7
  245. : vgm_x == 6 ? 8
  246. : vgm_x == 7 ? 9
  247. : vgm_x == 8 ? 10
  248. : vgm_x == 9 ? 11
  249. : vgm_x == 10 ? 12
  250. : 2;
  251. variable_item_set_current_value_index(app->variable_item_game_vgm_x, index);
  252. variable_item_set_current_value_text(app->variable_item_game_vgm_x, vgm_levels[index]);
  253. }
  254. char _game_vgm_y[8];
  255. if (load_char("Game-VGM-Y", _game_vgm_y, sizeof(_game_vgm_y)))
  256. {
  257. int vgm_y = atoi(_game_vgm_y);
  258. int index = vgm_y == -2 ? 0 : vgm_y == -1 ? 1
  259. : vgm_y == 0 ? 2
  260. : vgm_y == 1 ? 3
  261. : vgm_y == 2 ? 4
  262. : vgm_y == 3 ? 5
  263. : vgm_y == 4 ? 6
  264. : vgm_y == 5 ? 7
  265. : vgm_y == 6 ? 8
  266. : vgm_y == 7 ? 9
  267. : vgm_y == 8 ? 10
  268. : vgm_y == 9 ? 11
  269. : vgm_y == 10 ? 12
  270. : 2;
  271. variable_item_set_current_value_index(app->variable_item_game_vgm_y, index);
  272. variable_item_set_current_value_text(app->variable_item_game_vgm_y, vgm_levels[index]);
  273. }
  274. char _game_screen_always_on[8];
  275. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  276. {
  277. int index = is_str(_game_screen_always_on, "No") ? 0 : is_str(_game_screen_always_on, "Yes") ? 1
  278. : 0;
  279. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, yes_or_no_choices[index]);
  280. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  281. }
  282. char _game_sound_on[8];
  283. if (load_char("Game-Sound-On", _game_sound_on, sizeof(_game_sound_on)))
  284. {
  285. int index = is_str(_game_sound_on, "No") ? 0 : is_str(_game_sound_on, "Yes") ? 1
  286. : 0;
  287. variable_item_set_current_value_text(app->variable_item_game_sound_on, yes_or_no_choices[index]);
  288. variable_item_set_current_value_index(app->variable_item_game_sound_on, index);
  289. }
  290. char _game_vibration_on[8];
  291. if (load_char("Game-Vibration-On", _game_vibration_on, sizeof(_game_vibration_on)))
  292. {
  293. int index = is_str(_game_vibration_on, "No") ? 0 : is_str(_game_vibration_on, "Yes") ? 1
  294. : 0;
  295. variable_item_set_current_value_text(app->variable_item_game_vibration_on, yes_or_no_choices[index]);
  296. variable_item_set_current_value_index(app->variable_item_game_vibration_on, index);
  297. }
  298. break;
  299. case FlipWorldSubmenuIndexUserSettings:
  300. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, callback_user_settings_select, callback_to_settings, &app->view_dispatcher, app))
  301. {
  302. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  303. return false;
  304. }
  305. if (!app->variable_item_list)
  306. {
  307. FURI_LOG_E(TAG, "Variable item list is NULL");
  308. return false;
  309. }
  310. // if logged in, show profile info, otherwise show login/register
  311. if (is_logged_in() || is_logged_in_to_flip_social())
  312. {
  313. if (!app->variable_item_user_username)
  314. {
  315. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  316. variable_item_set_current_value_text(app->variable_item_user_username, "");
  317. }
  318. if (!app->variable_item_user_password)
  319. {
  320. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  321. variable_item_set_current_value_text(app->variable_item_user_password, "");
  322. }
  323. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  324. {
  325. variable_item_set_current_value_text(app->variable_item_user_username, username);
  326. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  327. }
  328. }
  329. else
  330. {
  331. if (!app->variable_item_user_username)
  332. {
  333. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  334. variable_item_set_current_value_text(app->variable_item_user_username, "");
  335. }
  336. if (!app->variable_item_user_password)
  337. {
  338. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  339. variable_item_set_current_value_text(app->variable_item_user_password, "");
  340. }
  341. }
  342. break;
  343. }
  344. }
  345. return true;
  346. }
  347. bool alloc_submenu_other(void *context, uint32_t view_id)
  348. {
  349. FlipWorldApp *app = (FlipWorldApp *)context;
  350. furi_check(app);
  351. if (app->submenu_other)
  352. {
  353. FURI_LOG_I(TAG, "Submenu already allocated");
  354. return true;
  355. }
  356. switch (view_id)
  357. {
  358. case FlipWorldViewSettings:
  359. if (!easy_flipper_set_submenu(&app->submenu_other, FlipWorldViewSubmenuOther, "Settings", callback_to_submenu, &app->view_dispatcher))
  360. {
  361. FURI_LOG_E(TAG, "Failed to allocate submenu settings");
  362. return false;
  363. }
  364. submenu_add_item(app->submenu_other, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  365. submenu_add_item(app->submenu_other, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  366. submenu_add_item(app->submenu_other, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  367. return true;
  368. case FlipWorldViewLobby:
  369. return easy_flipper_set_submenu(&app->submenu_other, FlipWorldViewSubmenuOther, "Lobbies", callback_to_submenu, &app->view_dispatcher);
  370. default:
  371. return false;
  372. }
  373. }
  374. bool alloc_game_submenu(void *context)
  375. {
  376. FlipWorldApp *app = (FlipWorldApp *)context;
  377. furi_check(app);
  378. if (!app->submenu_game)
  379. {
  380. if (!easy_flipper_set_submenu(&app->submenu_game, FlipWorldViewGameSubmenu, "Play", callback_to_submenu, &app->view_dispatcher))
  381. {
  382. return false;
  383. }
  384. if (!app->submenu_game)
  385. {
  386. return false;
  387. }
  388. submenu_add_item(app->submenu_game, "Tutorial", FlipWorldSubmenuIndexStory, callback_submenu_choices, app);
  389. submenu_add_item(app->submenu_game, "PvP (Beta)", FlipWorldSubmenuIndexPvP, callback_submenu_choices, app);
  390. submenu_add_item(app->submenu_game, "PvE", FlipWorldSubmenuIndexPvE, callback_submenu_choices, app);
  391. }
  392. return true;
  393. }