containersWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. ImGui::Separator();
  293. ImGui::Checkbox("Pause", &c.deltaTimePaused);
  294. ImGui::SameLine();
  295. ImGui::DragFloat("Simulation speed", &c.simulationSpeed, 0.01, 0, 100);
  296. ImGui::Separator();
  297. if(!stopped)
  298. {
  299. ImGui::Text("Status: %s", c.flags.getStatusName());
  300. ImGui::Text("Time (ms): %f", c.currentMs);
  301. if (ImGui::Button("Calculate memory usage"))
  302. {
  303. c.allocator.calculateMemoryMetrics(c.availableMemory, c.biggestBlock, c.freeBlocks);
  304. }
  305. ImGui::Text("Available memory: %" IM_PRIu64, c.availableMemory);
  306. ImGui::Text("Biggest block: %" IM_PRIu64, c.biggestBlock);
  307. ImGui::Text("Free blocks: %d", c.freeBlocks);
  308. ImGui::Separator();
  309. if (ImGui::BeginTabBar("##Tabs for play and record", ImGuiTabBarFlags_Reorderable))
  310. {
  311. if (ImGui::BeginTabItem(ICON_FK_CAMERA " Snapshot"))
  312. {
  313. //snapshot button
  314. {
  315. if (ImGui::Button(ICON_FK_CAMERA))
  316. {
  317. if (pika::isFileNameValid(snapshotName, sizeof(snapshotName)))
  318. {
  319. if (!containerManager.makeSnapshot(containerIds[itemCurrentCreatedContainers], logManager, snapshotName))
  320. {
  321. logManager.log("Coultn't make snapshot", pika::logError);
  322. }
  323. else
  324. {
  325. logManager.log("Successfully created snapshot.");
  326. }
  327. }
  328. else
  329. {
  330. logManager.log("File name invalid", pika::logError);
  331. }
  332. }
  333. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  334. {
  335. ImGui::SetTooltip("Make snapshot");
  336. }
  337. ImGui::SameLine();
  338. ImGui::InputText("snapshot name", snapshotName, sizeof(snapshotName));
  339. }
  340. ImGui::NewLine();
  341. ImGui::Separator();
  342. #pragma region snapshots
  343. {
  344. auto snapshots = pika::getAvailableSnapshots(
  345. containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]]);
  346. auto contentSize = ImGui::GetItemRectSize();
  347. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  348. contentSize.x /= 2;
  349. ImGui::ListWithFilter("##list box snapshots", &currentSelectedSnapshot,
  350. filterSnapshots, sizeof(filterSnapshots),
  351. snapshots, contentSize);
  352. ImGui::SameLine();
  353. if (snapshots.size() == 0 || currentSelectedSnapshot >= snapshots.size())
  354. {
  355. ImGui::BeginDisabled(true);
  356. }
  357. else
  358. {
  359. ImGui::BeginDisabled(false);
  360. }
  361. if (ImGui::Button(ICON_FK_PLAY "##play snapshot"))
  362. {
  363. if (!containerManager.setSnapshotToContainer(
  364. containerIds[itemCurrentCreatedContainers],
  365. snapshots[currentSelectedSnapshot].c_str(), logManager, imguiIdsManager
  366. ))
  367. {
  368. logManager.log("Failed to assign snapshot", pika::logError);
  369. }
  370. }
  371. ImGui::EndDisabled();
  372. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  373. {
  374. ImGui::SetTooltip("Play this snapshot to this container");
  375. }
  376. }
  377. #pragma endregion
  378. ImGui::EndTabItem();
  379. }
  380. if (ImGui::BeginTabItem(ICON_FK_VIDEO_CAMERA " Record"))
  381. {
  382. //recording
  383. {
  384. if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_RUNNING)
  385. {
  386. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  387. {
  388. if (pika::isFileNameValid(recordingName, sizeof(recordingName)))
  389. {
  390. containerManager.startRecordingContainer
  391. (containerIds[itemCurrentCreatedContainers], logManager, recordingName);
  392. }
  393. else
  394. {
  395. logManager.log("File name invalid", pika::logError);
  396. }
  397. }
  398. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  399. {
  400. ImGui::SetTooltip("start recording");
  401. }
  402. }
  403. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  404. {
  405. if (ImGui::Button(ICON_FK_STOP_CIRCLE))
  406. {
  407. containerManager.stopRecordingContainer
  408. (containerIds[itemCurrentCreatedContainers], logManager);
  409. }
  410. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  411. {
  412. ImGui::SetTooltip("stop recording");
  413. }
  414. }
  415. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_PAUSE)
  416. {
  417. ImGui::BeginDisabled(1);
  418. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  419. {
  420. }
  421. ImGui::EndDisabled();
  422. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  423. {
  424. ImGui::SetTooltip("Can't record while container is paused");
  425. }
  426. }
  427. else if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_PLAYBACK)
  428. {
  429. ImGui::BeginDisabled(1);
  430. if (ImGui::Button(ICON_FK_VIDEO_CAMERA))
  431. {
  432. }
  433. ImGui::EndDisabled();
  434. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  435. {
  436. ImGui::SetTooltip("Can't record while container is on playback");
  437. }
  438. }
  439. ImGui::BeginDisabled(c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED);
  440. ImGui::SameLine();
  441. ImGui::InputText("recording name", recordingName, sizeof(recordingName));
  442. ImGui::EndDisabled();
  443. }
  444. ImGui::NewLine();
  445. ImGui::Separator();
  446. #pragma region recordings
  447. auto recordings = pika::getAvailableRecordings(
  448. containerManager.runningContainers[containerIds[itemCurrentCreatedContainers]]);
  449. auto contentSize = ImGui::GetItemRectSize();
  450. contentSize.y -= ImGui::GetFrameHeightWithSpacing();
  451. contentSize.x /= 2;
  452. ImGui::ListWithFilter("##list box recordings", &currentSelectedRecording,
  453. filterSnapshots, sizeof(filterSnapshots),
  454. recordings, contentSize);
  455. ImGui::SameLine();
  456. if (recordings.size() == 0 || currentSelectedRecording >= recordings.size()
  457. || c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  458. {
  459. ImGui::BeginDisabled(true);
  460. }
  461. else
  462. {
  463. ImGui::BeginDisabled(false);
  464. }
  465. if (ImGui::Button(ICON_FK_PLAY "##play recording"))
  466. {
  467. if (!containerManager.setRecordingToContainer(
  468. containerIds[itemCurrentCreatedContainers],
  469. recordings[currentSelectedSnapshot].c_str(), logManager, imguiIdsManager
  470. ))
  471. {
  472. logManager.log("Failed to assign recording", pika::logError);
  473. }
  474. }
  475. ImGui::EndDisabled();
  476. if (c.flags.status == pika::RuntimeContainer::FLAGS::STATUS_BEING_RECORDED)
  477. {
  478. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  479. {
  480. ImGui::SetTooltip("Can't play a recording because the container is being recorded at the moment");
  481. }
  482. }
  483. else
  484. {
  485. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  486. {
  487. ImGui::SetTooltip("Play this recording to this container");
  488. }
  489. }
  490. #pragma endregion
  491. ImGui::EndTabItem();
  492. }
  493. ImGui::EndTabBar();
  494. }
  495. }
  496. }
  497. else
  498. {
  499. ImGui::Text("Running container:");
  500. ImGui::Separator();
  501. }
  502. }
  503. ImGui::EndGroup();
  504. ImGui::PopID();
  505. ImGui::EndTabItem();
  506. }
  507. ImGui::EndTabBar();
  508. }
  509. ImGui::EndChild();
  510. //you can add buttons here
  511. ImGui::EndGroup();
  512. }
  513. //todo container profiler will have an instance per container with imgui id and stuff.
  514. ImGui::End();
  515. ImGui::PopID();
  516. }
  517. //todo options hide show push notidications also engine push notifications that are forced
  518. #endif