containersWindow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include "pikaConfig.h"
  2. #if !PIKA_SHOULD_REMOVE_EDITOR
  3. #include "containersWindow.h"
  4. #include <pikaImgui/pikaImgui.h>
  5. #include "imguiComboSearch.h"
  6. #include <imgui_spinner.h>
  7. #include <validatePath.h>
  8. void pika::ContainersWindow::init(pika::pikaImgui::ImGuiIdsManager &imguiIdsManager)
  9. {
  10. imguiIds = imguiIdsManager.getImguiIds(10);
  11. }
  12. void pika::ContainersWindow::update(pika::LogManager &logManager, bool &open, pika::LoadedDll &loadedDll,
  13. pika::ContainerManager &containerManager, pika::pikaImgui::ImGuiIdsManager &imguiIdsManager, pika::ConsoleWindow *consoleWindow)
  14. {
  15. ImGui::PushID(imguiIds);
  16. if (!ImGui::Begin(ICON_NAME, &open))
  17. {
  18. ImGui::End();
  19. ImGui::PopID();
  20. return;
  21. }
  22. static int selected = 0;
  23. std::string selectedContainerToLaunch = "";
  24. {
  25. ImGui::BeginGroup();
  26. ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing())); // Leave room for 1 line below us
  27. if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_Reorderable))
  28. {
  29. if (ImGui::BeginTabItem(ICON_FK_PLUS_SQUARE_O " Create container"))
  30. {
  31. ImGui::Text("Available containers");
  32. ImGui::Separator();
  33. //left
  34. ImGui::PushID(imguiIds +1);
  35. ImGui::BeginGroup();
  36. {
  37. static char filter[256] = {};
  38. std::vector<std::string> containerNames;
  39. containerNames.reserve(loadedDll.containerInfo.size());
  40. for (auto &i : loadedDll.containerInfo)
  41. {
  42. containerNames.push_back(i.containerName);
  43. }
  44. auto contentSize = ImGui::GetItemRectSize();
  45. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  46. contentSize.x /= 2;
  47. ImGui::ListWithFilter("##list box container info", &itemCurrentAvailableCOntainers, filter, sizeof(filter),
  48. containerNames, contentSize);
  49. }
  50. ImGui::EndGroup();
  51. ImGui::PopID();
  52. ImGui::SameLine();
  53. //right
  54. ImGui::PushID(imguiIds + 2);
  55. ImGui::BeginGroup();
  56. {
  57. if (itemCurrentAvailableCOntainers < loadedDll.containerInfo.size())
  58. {
  59. auto &c = loadedDll.containerInfo[itemCurrentAvailableCOntainers];
  60. ImGui::Text("Container info: %s", c.containerName.c_str());
  61. ImGui::Separator();
  62. selectedContainerToLaunch = c.containerName;
  63. ImGui::Text(ICON_FK_PIE_CHART " Memory");
  64. ImGui::Separator();
  65. ImGui::NewLine();
  66. #pragma region total memory requirement
  67. size_t totalHeapMemory = c.containerStaticInfo.defaultHeapMemorySize;
  68. for (auto i : c.containerStaticInfo.bonusAllocators)
  69. {
  70. totalHeapMemory += i;
  71. }
  72. size_t totalMemory = totalHeapMemory + c.containerStructBaseSize;
  73. ImGui::Text("Total Memory requirement: ");
  74. ImGui::SameLine();
  75. pika::pikaImgui::displayMemorySizeValue(totalMemory);
  76. ImGui::Text("Total Heap requirement: ");
  77. ImGui::SameLine();
  78. pika::pikaImgui::displayMemorySizeValue(totalHeapMemory);
  79. #pragma endregion
  80. ImGui::NewLine();
  81. ImGui::Text("Static Memory requirement: ");
  82. ImGui::SameLine();
  83. pika::pikaImgui::displayMemorySizeValue(c.containerStructBaseSize);
  84. ImGui::Text("Default Heap Memory requirement: ");
  85. ImGui::SameLine();
  86. pika::pikaImgui::displayMemorySizeValue(c.containerStaticInfo.defaultHeapMemorySize);
  87. ImGui::Text("Other Heap Memory Allocators count: ");
  88. ImGui::SameLine();
  89. pika::pikaImgui::displayMemorySizeValue(c.containerStaticInfo.bonusAllocators.size());
  90. if (!c.containerStaticInfo.bonusAllocators.empty())
  91. {
  92. if (ImGui::BeginChild("##heap allocators",
  93. {0, 100}, true, ImGuiWindowFlags_AlwaysVerticalScrollbar))
  94. {
  95. for (auto i : c.containerStaticInfo.bonusAllocators)
  96. {
  97. pika::pikaImgui::displayMemorySizeValue(i);
  98. }
  99. }
  100. ImGui::EndChild();
  101. }
  102. ImGui::NewLine();
  103. ImGui::Text(ICON_FK_PLUS_SQUARE_O " Launch");
  104. ImGui::Separator();
  105. ImGui::NewLine();
  106. if (!selectedContainerToLaunch.empty()
  107. && ImGui::Button(ICON_FK_PLAY " Launch a default configuration"))
  108. {
  109. if (createAtSpecificMemoryRegion)
  110. {
  111. containerManager.createContainer(selectedContainerToLaunch, loadedDll,
  112. logManager, imguiIdsManager, consoleWindow, std::string(), pika::TB(1));
  113. }
  114. else
  115. {
  116. containerManager.createContainer(selectedContainerToLaunch, loadedDll, logManager,
  117. imguiIdsManager, consoleWindow, std::string());
  118. }
  119. }
  120. ImGui::Checkbox("allocate at specific memory region", &createAtSpecificMemoryRegion);
  121. //ImGui::NewLine();
  122. {
  123. static int currentSelectedSnapshot = 0;
  124. auto snapshots = pika::getAvailableSnapshotsAnyMemoryPosition(c);
  125. if (!selectedContainerToLaunch.empty()
  126. && ImGui::Button(ICON_FK_PICTURE_O " Launch a snapshot"))
  127. {
  128. auto s = snapshots[currentSelectedSnapshot];
  129. auto memPos = getSnapshotMemoryPosition(s.c_str());
  130. if (memPos == nullptr)
  131. {
  132. logManager.log("Failes to get snapshot info", pika::logError);
  133. }
  134. else
  135. {
  136. auto c = containerManager.createContainer(
  137. selectedContainerToLaunch, loadedDll, logManager,
  138. imguiIdsManager, consoleWindow, std::string(), (size_t)memPos);
  139. //no need to log error since create container does that
  140. if (c != 0)
  141. {
  142. if (!containerManager.setSnapshotToContainer(c,
  143. s.c_str(), logManager, imguiIdsManager))
  144. {
  145. containerManager.destroyContainer(c, loadedDll, logManager);
  146. }
  147. }
  148. }
  149. }
  150. auto contentSize = ImGui::GetItemRectSize();
  151. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  152. //contentSize.x /= 2;
  153. ImGui::ListWithFilter("##list box snapshots", &currentSelectedSnapshot,
  154. filterSnapshots, sizeof(filterSnapshots),
  155. snapshots, contentSize);
  156. }
  157. }
  158. else
  159. {
  160. ImGui::Text("Container info:");
  161. ImGui::Separator();
  162. }
  163. }
  164. ImGui::EndGroup();
  165. ImGui::PopID();
  166. ImGui::EndTabItem();
  167. }
  168. if (ImGui::BeginTabItem(ICON_FK_MICROCHIP " Running containers"))
  169. {
  170. ImGui::Text("Running containers");
  171. ImGui::SameLine();
  172. pika::pikaImgui::helpMarker(
  173. ICON_FK_BOLT ": Container is running;\n"
  174. ICON_FK_PAUSE_CIRCLE_O ": Container is on pause;\n"
  175. ICON_FK_VIDEO_CAMERA ": Container is being recorded;\n"
  176. ICON_FK_REPEAT ": Container is on input playback."
  177. );
  178. ImGui::Separator();
  179. //left
  180. std::vector<pika::containerId_t> containerIds;
  181. std::vector<std::string> containerNames;
  182. ImGui::PushID(imguiIds + 3);
  183. ImGui::BeginGroup();
  184. {
  185. containerIds.reserve(containerManager.runningContainers.size());
  186. containerNames.reserve(containerManager.runningContainers.size());
  187. for (auto &i : containerManager.runningContainers)
  188. {
  189. containerIds.push_back(i.first);
  190. containerNames.push_back(
  191. std::string(i.second.flags.getStatusIcon()) + " " + i.second.baseContainerName + ": " + std::to_string(i.first));
  192. }
  193. auto contentSize = ImGui::GetItemRectSize();
  194. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  195. contentSize.x /= 2;
  196. ImGui::ListWithFilter("##list box container info", &itemCurrentCreatedContainers,
  197. filterContainerInfo, sizeof(filterContainerInfo),
  198. containerNames, contentSize);
  199. }
  200. ImGui::EndGroup();
  201. ImGui::PopID();
  202. ImGui::SameLine();
  203. //right
  204. ImGui::PushID(imguiIds + 4);
  205. ImGui::BeginGroup();
  206. {
  207. if (itemCurrentCreatedContainers < containerIds.size())
  208. {
  209. auto &c = containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]];
  210. ImGui::Text("Running container: %s #%u", c.baseContainerName, containerIds[itemCurrentCreatedContainers]);
  211. ImGui::Separator();
  212. #pragma region buttons
  213. //calculate cursor pos for 3 buttons
  214. {
  215. ImGuiStyle &style = ImGui::GetStyle();
  216. float width = 0.0f;
  217. width += ImGui::CalcTextSize(ICON_FK_PAUSE).x;
  218. width += style.ItemSpacing.x;
  219. width += ImGui::CalcTextSize(ICON_FK_PAUSE).x;
  220. width += style.ItemSpacing.x;
  221. width += ImGui::CalcTextSize(ICON_FK_PAUSE).x;
  222. pika::pikaImgui::alignForWidth(width);
  223. }
  224. if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_RUNNING)
  225. {
  226. if (ImGui::Button(ICON_FK_PAUSE))
  227. {
  228. c.flags.status = pika::RuntimeContainer::FLAGS::STATUS_PAUSE;
  229. }
  230. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  231. {
  232. ImGui::SetTooltip("Pause container.");
  233. }
  234. }
  235. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_PAUSE)
  236. {
  237. if (ImGui::Button(ICON_FK_PLAY))
  238. {
  239. c.flags.status = pika::RuntimeContainer::FLAGS::STATUS_RUNNING;
  240. }
  241. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  242. {
  243. ImGui::SetTooltip("Resume container.");
  244. }
  245. }
  246. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  247. {
  248. ImGui::BeginDisabled();
  249. if (ImGui::Button(ICON_FK_PAUSE))
  250. {
  251. }
  252. ImGui::EndDisabled();
  253. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  254. {
  255. ImGui::SetTooltip("Can't pause container while it is recorded."); //todo implement
  256. }
  257. }
  258. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_PLAYBACK)
  259. {
  260. if (ImGui::Button(ICON_FK_REPEAT))
  261. {
  262. c.flags.status = pika::RuntimeContainer::FLAGS::STATUS_RUNNING;
  263. }
  264. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  265. {
  266. ImGui::SetTooltip("Stop playback");
  267. }
  268. }
  269. ImGui::SameLine();
  270. bool stopped = false;
  271. if (ImGui::Button(ICON_FK_STOP))
  272. {
  273. //todo mabe defer here when api is made
  274. containerManager.destroyContainer(containerIds[itemCurrentCreatedContainers], loadedDll, logManager);
  275. stopped = true;
  276. }
  277. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  278. {
  279. ImGui::SetTooltip("Stop container.");
  280. }
  281. ImGui::SameLine();
  282. if (ImGui::Button(ICON_FK_EJECT))
  283. {
  284. containerManager.forceTerminateContainer(containerIds[itemCurrentCreatedContainers], loadedDll, logManager);
  285. stopped = true;
  286. }
  287. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  288. {
  289. ImGui::SetTooltip("Force stop container (not recomended).\nThis won't call any destructors.");
  290. }
  291. #pragma endregion
  292. if(!stopped)
  293. {
  294. ImGui::Text("Status: %s", c.flags.getStatusName());
  295. ImGui::Text("Time (ms): %f", c.currentMs);
  296. ImGui::Separator();
  297. if (ImGui::Button("Calculate memory usage"))
  298. {
  299. c.allocator.calculateMemoryMetrics(c.availableMemory, c.biggestBlock, c.freeBlocks);
  300. }
  301. ImGui::Text("Available memory: %" IM_PRIu64, c.availableMemory);
  302. ImGui::Text("Biggest block: %" IM_PRIu64, c.biggestBlock);
  303. ImGui::Text("Free blocks: %d", c.freeBlocks);
  304. ImGui::Separator();
  305. if (ImGui::BeginTabBar("##Tabs for play and record", ImGuiTabBarFlags_Reorderable))
  306. {
  307. if (ImGui::BeginTabItem(ICON_FK_CAMERA " Snapshot"))
  308. {
  309. //snapshot button
  310. {
  311. if (ImGui::Button(ICON_FK_CAMERA))
  312. {
  313. if (pika::isFileNameValid(snapshotName, sizeof(snapshotName)))
  314. {
  315. if (!containerManager.makeSnapshot(containerIds[itemCurrentCreatedContainers], logManager, snapshotName))
  316. {
  317. logManager.log("Coultn't make snapshot", pika::logError);
  318. }
  319. else
  320. {
  321. logManager.log("Successfully created snapshot.");
  322. }
  323. }
  324. else
  325. {
  326. logManager.log("File name invalid", pika::logError);
  327. }
  328. }
  329. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  330. {
  331. ImGui::SetTooltip("Make snapshot");
  332. }
  333. ImGui::SameLine();
  334. ImGui::InputText("snapshot name", snapshotName, sizeof(snapshotName));
  335. }
  336. ImGui::NewLine();
  337. ImGui::Separator();
  338. #pragma region snapshots
  339. {
  340. auto snapshots = pika::getAvailableSnapshots(
  341. containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]]);
  342. auto contentSize = ImGui::GetItemRectSize();
  343. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  344. contentSize.x /= 2;
  345. ImGui::ListWithFilter("##list box snapshots", &currentSelectedSnapshot,
  346. filterSnapshots, sizeof(filterSnapshots),
  347. snapshots, contentSize);
  348. ImGui::SameLine();
  349. if (snapshots.size() == 0 || currentSelectedSnapshot >= snapshots.size())
  350. {
  351. ImGui::BeginDisabled(true);
  352. }
  353. else
  354. {
  355. ImGui::BeginDisabled(false);
  356. }
  357. if (ImGui::Button(ICON_FK_PLAY "##play snapshot"))
  358. {
  359. if (!containerManager.setSnapshotToContainer(
  360. containerIds[itemCurrentCreatedContainers],
  361. snapshots[currentSelectedSnapshot].c_str(), logManager, imguiIdsManager
  362. ))
  363. {
  364. logManager.log("Failed to assign snapshot", pika::logError);
  365. }
  366. }
  367. ImGui::EndDisabled();
  368. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  369. {
  370. ImGui::SetTooltip("Play this snapshot to this container");
  371. }
  372. }
  373. #pragma endregion
  374. ImGui::EndTabItem();
  375. }
  376. if (ImGui::BeginTabItem(ICON_FK_VIDEO_CAMERA " Record"))
  377. {
  378. //recording
  379. {
  380. if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_RUNNING)
  381. {
  382. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  383. {
  384. if (pika::isFileNameValid(recordingName, sizeof(recordingName)))
  385. {
  386. containerManager.startRecordingContainer
  387. (containerIds[itemCurrentCreatedContainers], logManager, recordingName);
  388. }
  389. else
  390. {
  391. logManager.log("File name invalid", pika::logError);
  392. }
  393. }
  394. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  395. {
  396. ImGui::SetTooltip("start recording");
  397. }
  398. }
  399. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  400. {
  401. if (ImGui::Button(ICON_FK_STOP_CIRCLE))
  402. {
  403. containerManager.stopRecordingContainer
  404. (containerIds[itemCurrentCreatedContainers], logManager);
  405. }
  406. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  407. {
  408. ImGui::SetTooltip("stop recording");
  409. }
  410. }
  411. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_PAUSE)
  412. {
  413. ImGui::BeginDisabled(1);
  414. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  415. {
  416. }
  417. ImGui::EndDisabled();
  418. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  419. {
  420. ImGui::SetTooltip("Can't record while container is paused");
  421. }
  422. }
  423. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_PLAYBACK)
  424. {
  425. ImGui::BeginDisabled(1);
  426. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  427. {
  428. }
  429. ImGui::EndDisabled();
  430. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  431. {
  432. ImGui::SetTooltip("Can't record while container is on playback");
  433. }
  434. }
  435. ImGui::BeginDisabled(c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED);
  436. ImGui::SameLine();
  437. ImGui::InputText("recording name", recordingName, sizeof(recordingName));
  438. ImGui::EndDisabled();
  439. }
  440. ImGui::NewLine();
  441. ImGui::Separator();
  442. #pragma region recordings
  443. auto recordings = pika::getAvailableRecordings(
  444. containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]]);
  445. auto contentSize = ImGui::GetItemRectSize();
  446. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  447. contentSize.x /= 2;
  448. ImGui::ListWithFilter("##list box recordings", &currentSelectedRecording,
  449. filterSnapshots, sizeof(filterSnapshots),
  450. recordings, contentSize);
  451. ImGui::SameLine();
  452. if (recordings.size() == 0 || currentSelectedRecording >= recordings.size()
  453. || c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  454. {
  455. ImGui::BeginDisabled(true);
  456. }
  457. else
  458. {
  459. ImGui::BeginDisabled(false);
  460. }
  461. if (ImGui::Button(ICON_FK_PLAY "##play recording"))
  462. {
  463. if (!containerManager.setRecordingToContainer(
  464. containerIds[itemCurrentCreatedContainers],
  465. recordings[currentSelectedSnapshot].c_str(), logManager, imguiIdsManager
  466. ))
  467. {
  468. logManager.log("Failed to assign recording", pika::logError);
  469. }
  470. }
  471. ImGui::EndDisabled();
  472. if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  473. {
  474. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  475. {
  476. ImGui::SetTooltip("Can't play a recording because the container is being recorded at the moment");
  477. }
  478. }
  479. else
  480. {
  481. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  482. {
  483. ImGui::SetTooltip("Play this recording to this container");
  484. }
  485. }
  486. #pragma endregion
  487. ImGui::EndTabItem();
  488. }
  489. ImGui::EndTabBar();
  490. }
  491. }
  492. }
  493. else
  494. {
  495. ImGui::Text("Running container:");
  496. ImGui::Separator();
  497. }
  498. }
  499. ImGui::EndGroup();
  500. ImGui::PopID();
  501. ImGui::EndTabItem();
  502. }
  503. ImGui::EndTabBar();
  504. }
  505. ImGui::EndChild();
  506. //you can add buttons here
  507. ImGui::EndGroup();
  508. }
  509. //todo container profiler will have an instance per container with imgui id and stuff.
  510. ImGui::End();
  511. ImGui::PopID();
  512. }
  513. //todo options hide show push notidications also engine push notifications that are forced
  514. #endif