LiveLoggingWindowSession.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Interface/Interface.h>
  9. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  10. #include <EditorCoreAPI.h>
  11. #include <IEditor.h>
  12. #include <Editor/View/Widgets/LoggingPanel/LiveWindowSession/LiveLoggingWindowSession.h>
  13. #include <ScriptCanvas/Utils/ScriptCanvasConstants.h>
  14. namespace ScriptCanvasEditor
  15. {
  16. ///////////////////////
  17. // TargetManagerModel
  18. ///////////////////////
  19. TargetManagerModel::TargetManagerModel()
  20. {
  21. AzFramework::RemoteToolsEndpointInfo editorTargetInfo("Editor");
  22. m_targetInfo.push_back(editorTargetInfo);
  23. ScrapeTargetInfo();
  24. }
  25. int TargetManagerModel::rowCount([[maybe_unused]] const QModelIndex& parent) const
  26. {
  27. return static_cast<int>(m_targetInfo.size());
  28. }
  29. QVariant TargetManagerModel::data(const QModelIndex& index, int role) const
  30. {
  31. if (!index.isValid())
  32. {
  33. return QVariant();
  34. }
  35. switch (role)
  36. {
  37. case Qt::DisplayRole:
  38. {
  39. const AzFramework::RemoteToolsEndpointInfo& targetInfo = m_targetInfo[index.row()];
  40. if (index.row() > 0)
  41. {
  42. return QString("%1 (%2)").arg(targetInfo.GetDisplayName(), QString::number(targetInfo.GetPersistentId(), 16));
  43. }
  44. else
  45. {
  46. return QString(targetInfo.GetDisplayName());
  47. }
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. return QVariant();
  54. }
  55. void TargetManagerModel::TargetJoinedNetwork(AzFramework::RemoteToolsEndpointInfo info)
  56. {
  57. if (!info.IsSelf())
  58. {
  59. int element = GetRowForTarget(info.GetPersistentId());
  60. if (element < 0)
  61. {
  62. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  63. m_targetInfo.push_back(info);
  64. endInsertRows();
  65. }
  66. }
  67. else
  68. {
  69. ScrapeTargetInfo();
  70. }
  71. }
  72. void TargetManagerModel::TargetLeftNetwork(AzFramework::RemoteToolsEndpointInfo info)
  73. {
  74. int element = GetRowForTarget(info.GetPersistentId());
  75. // 0 is reserved for our fake Editor one.
  76. // And we don't want to remove it.
  77. if (element > 0)
  78. {
  79. beginRemoveRows(QModelIndex(), element, element);
  80. m_targetInfo.erase(m_targetInfo.begin() + element);
  81. endRemoveRows();
  82. }
  83. }
  84. AzFramework::RemoteToolsEndpointInfo TargetManagerModel::FindTargetInfoForRow(int row)
  85. {
  86. if (row < 0 && row >= m_targetInfo.size())
  87. {
  88. return AzFramework::RemoteToolsEndpointInfo();
  89. }
  90. return m_targetInfo[row];
  91. }
  92. int TargetManagerModel::GetRowForTarget(AZ::u32 targetId)
  93. {
  94. for (size_t i = 0; i < m_targetInfo.size(); ++i)
  95. {
  96. if (m_targetInfo[i].GetPersistentId() == targetId)
  97. {
  98. return static_cast<int>(i);
  99. }
  100. }
  101. return -1;
  102. }
  103. void TargetManagerModel::ScrapeTargetInfo()
  104. {
  105. AzFramework::IRemoteTools* remoteTools = AzFramework::RemoteToolsInterface::Get();
  106. AzFramework::RemoteToolsEndpointContainer targets;
  107. if (remoteTools)
  108. {
  109. remoteTools->EnumTargetInfos(ScriptCanvas::RemoteToolsKey, targets);
  110. }
  111. for (const auto& targetPair : targets)
  112. {
  113. if (!targetPair.second.IsSelf())
  114. {
  115. m_targetInfo.push_back(targetPair.second);
  116. }
  117. }
  118. }
  119. ////////////////////////////
  120. // LiveLoggingUserSettings
  121. ////////////////////////////
  122. AZStd::intrusive_ptr<LiveLoggingUserSettings> LiveLoggingUserSettings::FindSettingsInstance()
  123. {
  124. return AZ::UserSettings::CreateFind<LiveLoggingUserSettings>(AZ_CRC("ScriptCanvas::LiveLoggingUserSettings", 0xc79efe7b), AZ::UserSettings::CT_LOCAL);
  125. }
  126. void LiveLoggingUserSettings::Reflect(AZ::ReflectContext* reflectContext)
  127. {
  128. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  129. if (serializeContext)
  130. {
  131. serializeContext->Class<LiveLoggingUserSettings>()
  132. ->Version(1)
  133. ->Field("AutoCapturing", &LiveLoggingUserSettings::m_isAutoCaptureEnabled)
  134. ->Field("LiveUpdating", &LiveLoggingUserSettings::m_enableLiveUpdates)
  135. ;
  136. }
  137. }
  138. void LiveLoggingUserSettings::SetAutoCaptureEnabled(bool enabled)
  139. {
  140. m_isAutoCaptureEnabled = enabled;
  141. }
  142. bool LiveLoggingUserSettings::IsAutoCaptureEnabled() const
  143. {
  144. return m_isAutoCaptureEnabled;
  145. }
  146. void LiveLoggingUserSettings::SetLiveUpdates(bool enabled)
  147. {
  148. m_enableLiveUpdates = enabled;
  149. }
  150. bool LiveLoggingUserSettings::IsLiveUpdating() const
  151. {
  152. return m_enableLiveUpdates;
  153. }
  154. /////////////////////////////
  155. // LiveLoggingWindowSession
  156. /////////////////////////////
  157. LiveLoggingWindowSession::LiveLoggingWindowSession(QWidget* parent)
  158. : LoggingWindowSession(parent)
  159. , m_startedSession(false)
  160. , m_encodeStaticEntities(false)
  161. , m_isCapturing(false)
  162. {
  163. m_targetManagerModel = aznew TargetManagerModel();
  164. {
  165. QSignalBlocker signalBlocker(m_ui->targetSelector);
  166. m_ui->targetSelector->setModel(m_targetManagerModel);
  167. }
  168. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  169. ScriptCanvas::Debugger::ServiceNotificationsBus::Handler::BusConnect();
  170. SetDataId(m_liveDataAggregator.GetDataId());
  171. RegisterTreeRoot(m_liveDataAggregator.GetTreeRoot());
  172. m_userSettings = LiveLoggingUserSettings::FindSettingsInstance();
  173. if (!m_userSettings->IsLiveUpdating())
  174. {
  175. m_liveDataAggregator.GetTreeRoot()->SetUpdatePolicy(DebugLogRootItem::UpdatePolicy::SingleTime);
  176. }
  177. else
  178. {
  179. m_liveDataAggregator.GetTreeRoot()->SetUpdatePolicy(DebugLogRootItem::UpdatePolicy::RealTime);
  180. }
  181. // Despite being apart of the base menu for now, the LiveLoggingWindow is the only one that needs to utilize these buttons.
  182. // Going to control them from here.
  183. m_ui->liveUpdatesToggle->setChecked(m_userSettings->IsLiveUpdating());
  184. QObject::connect(m_ui->liveUpdatesToggle, &QToolButton::toggled, this, &LiveLoggingWindowSession::OnLiveUpdateToggled);
  185. m_ui->autoCaptureToggle->setChecked(m_userSettings->IsAutoCaptureEnabled());
  186. QObject::connect(m_ui->autoCaptureToggle, &QToolButton::toggled, this, &LiveLoggingWindowSession::OnAutoCaptureToggled);
  187. }
  188. LiveLoggingWindowSession::~LiveLoggingWindowSession()
  189. {
  190. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  191. ScriptCanvas::Debugger::ServiceNotificationsBus::Handler::BusDisconnect();
  192. }
  193. void LiveLoggingWindowSession::DesiredTargetChanged(AZ::u32 newId, [[maybe_unused]] AZ::u32 oldId)
  194. {
  195. {
  196. QSignalBlocker signalBlocker(m_ui->targetSelector);
  197. int row = m_targetManagerModel->GetRowForTarget(newId);
  198. if (row < 0)
  199. {
  200. m_ui->targetSelector->setCurrentIndex(0);
  201. }
  202. else
  203. {
  204. m_ui->targetSelector->setCurrentIndex(row);
  205. }
  206. }
  207. }
  208. void LiveLoggingWindowSession::DesiredTargetConnected(bool connected)
  209. {
  210. {
  211. QSignalBlocker signalBlocker(m_ui->targetSelector);
  212. bool useFallback = !connected;
  213. if (connected)
  214. {
  215. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  216. const AzFramework::RemoteToolsEndpointInfo& desiredInfo =
  217. AzFramework::RemoteToolsInterface::Get()->GetDesiredEndpoint(ScriptCanvas::RemoteToolsKey);
  218. if (desiredInfo.IsValid() && !desiredInfo.IsSelf())
  219. {
  220. int index = m_targetManagerModel->GetRowForTarget(desiredInfo.GetPersistentId());
  221. if (index > 0)
  222. {
  223. m_ui->targetSelector->setCurrentIndex(index);
  224. }
  225. }
  226. else
  227. {
  228. useFallback = true;
  229. }
  230. }
  231. else if (m_isCapturing)
  232. {
  233. SetIsCapturing(false);
  234. }
  235. if (useFallback)
  236. {
  237. if (!AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusIsConnected())
  238. {
  239. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  240. }
  241. m_ui->targetSelector->setCurrentIndex(0);
  242. }
  243. }
  244. }
  245. void LiveLoggingWindowSession::TargetJoinedNetwork(AzFramework::RemoteToolsEndpointInfo info)
  246. {
  247. {
  248. QSignalBlocker signalBlocker(m_ui->targetSelector);
  249. m_targetManagerModel->TargetJoinedNetwork(info);
  250. }
  251. }
  252. void LiveLoggingWindowSession::TargetLeftNetwork(AzFramework::RemoteToolsEndpointInfo info)
  253. {
  254. {
  255. QSignalBlocker signalBlocker(m_ui->targetSelector);
  256. m_targetManagerModel->TargetLeftNetwork(info);
  257. }
  258. }
  259. void LiveLoggingWindowSession::OnStartPlayInEditorBegin()
  260. {
  261. if (isVisible())
  262. {
  263. m_encodeStaticEntities = true;
  264. ScriptCanvas::Debugger::ClientUIRequestBus::Broadcast(&ScriptCanvas::Debugger::ClientUIRequests::StartEditorSession);
  265. if ((m_userSettings->IsAutoCaptureEnabled()) || m_startedSession)
  266. {
  267. SetIsCapturing(true);
  268. }
  269. }
  270. }
  271. void LiveLoggingWindowSession::OnStopPlayInEditor()
  272. {
  273. if (isVisible())
  274. {
  275. SetIsCapturing(false);
  276. m_startedSession = false;
  277. ScriptCanvas::Debugger::ClientUIRequestBus::Broadcast(&ScriptCanvas::Debugger::ClientUIRequests::StopEditorSession);
  278. m_encodeStaticEntities = false;
  279. }
  280. }
  281. void LiveLoggingWindowSession::Connected([[maybe_unused]] const ScriptCanvas::Debugger::Target& target)
  282. {
  283. if (m_userSettings->IsAutoCaptureEnabled() && isVisible())
  284. {
  285. SetIsCapturing(true);
  286. }
  287. }
  288. void LiveLoggingWindowSession::OnCaptureButtonPressed()
  289. {
  290. bool isSelfTarget = false;
  291. ScriptCanvas::Debugger::ClientRequestsBus::BroadcastResult(isSelfTarget, &ScriptCanvas::Debugger::ClientRequests::IsConnectedToSelf);
  292. if (isSelfTarget)
  293. {
  294. if (!m_startedSession)
  295. {
  296. bool isRunningGame = false;
  297. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(isRunningGame, &AzToolsFramework::EditorEntityContextRequests::IsEditorRunningGame);
  298. if (!isRunningGame)
  299. {
  300. if (GetIEditor()->IsLevelLoaded())
  301. {
  302. m_startedSession = true;
  303. GetIEditor()->SetInGameMode(true);
  304. }
  305. return;
  306. }
  307. }
  308. else
  309. {
  310. GetIEditor()->SetInGameMode(false);
  311. return;
  312. }
  313. }
  314. SetIsCapturing(!m_isCapturing);
  315. }
  316. void LiveLoggingWindowSession::OnPlaybackButtonPressed()
  317. {
  318. // Nothing to do in the LiveLoggingWindowSession
  319. }
  320. void LiveLoggingWindowSession::OnOptionsButtonPressed()
  321. {
  322. QPoint point = QCursor::pos();
  323. QMenu optionsMenu;
  324. QAction* autoCaptureAction = optionsMenu.addAction("Auto Capture");
  325. autoCaptureAction->setCheckable(true);
  326. autoCaptureAction->setChecked(m_userSettings->IsAutoCaptureEnabled());
  327. QObject::connect(autoCaptureAction, &QAction::toggled, this, &LiveLoggingWindowSession::OnAutoCaptureToggled);
  328. QAction* liveUpdateAction = optionsMenu.addAction("Live Updates");
  329. liveUpdateAction->setCheckable(true);
  330. liveUpdateAction->setChecked(m_userSettings->IsLiveUpdating());
  331. QObject::connect(liveUpdateAction, &QAction::toggled, this, &LiveLoggingWindowSession::OnLiveUpdateToggled);
  332. optionsMenu.exec(point);
  333. }
  334. void LiveLoggingWindowSession::OnTargetChanged(int index)
  335. {
  336. // Special case out the editor
  337. if (index == 0)
  338. {
  339. AzFramework::RemoteToolsInterface::Get()->SetDesiredEndpoint(ScriptCanvas::RemoteToolsKey, 0);
  340. }
  341. else
  342. {
  343. AzFramework::RemoteToolsEndpointInfo info = m_targetManagerModel->FindTargetInfoForRow(index);
  344. if (info.IsValid())
  345. {
  346. AzFramework::RemoteToolsInterface::Get()->SetDesiredEndpoint(ScriptCanvas::RemoteToolsKey, info.GetPersistentId());
  347. }
  348. }
  349. }
  350. void LiveLoggingWindowSession::OnAutoCaptureToggled(bool checked)
  351. {
  352. m_userSettings->SetAutoCaptureEnabled(checked);
  353. }
  354. void LiveLoggingWindowSession::OnLiveUpdateToggled(bool checked)
  355. {
  356. m_userSettings->SetLiveUpdates(checked);
  357. if (!m_userSettings->IsLiveUpdating())
  358. {
  359. m_liveDataAggregator.GetTreeRoot()->SetUpdatePolicy(DebugLogRootItem::UpdatePolicy::SingleTime);
  360. }
  361. else
  362. {
  363. // If we enable this we want to update the current display.
  364. m_liveDataAggregator.GetTreeRoot()->RedoLayout();
  365. m_liveDataAggregator.GetTreeRoot()->SetUpdatePolicy(DebugLogRootItem::UpdatePolicy::RealTime);
  366. }
  367. }
  368. void LiveLoggingWindowSession::StartDataCapture()
  369. {
  370. ScriptCanvas::Debugger::ScriptTarget captureInfo;
  371. ConfigureScriptTarget(captureInfo);
  372. m_liveDataAggregator.StartCaptureData();
  373. m_ui->captureButton->setIcon(QIcon(":/ScriptCanvasEditorResources/Resources/capture_live.png"));
  374. ScriptCanvas::Debugger::ClientUIRequestBus::Broadcast(&ScriptCanvas::Debugger::ClientUIRequests::StartLogging, captureInfo);
  375. }
  376. void LiveLoggingWindowSession::StopDataCapture()
  377. {
  378. m_liveDataAggregator.StopCaptureData();
  379. m_ui->captureButton->setIcon(QIcon(":/ScriptCanvasEditorResources/Resources/capture_offline.png"));
  380. ScriptCanvas::Debugger::ClientUIRequestBus::Broadcast(&ScriptCanvas::Debugger::ClientUIRequests::StopLogging);
  381. if (!m_userSettings->IsLiveUpdating())
  382. {
  383. m_liveDataAggregator.GetTreeRoot()->RedoLayout();
  384. }
  385. }
  386. void LiveLoggingWindowSession::ConfigureScriptTarget(ScriptCanvas::Debugger::ScriptTarget& captureInfo)
  387. {
  388. if (m_encodeStaticEntities)
  389. {
  390. const auto& staticRegistrations = m_liveDataAggregator.GetStaticRegistrations();
  391. for (const auto& registrationPair : staticRegistrations)
  392. {
  393. bool gotResult = false;
  394. AZ::EntityId runtimeId;
  395. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(gotResult, &AzToolsFramework::EditorEntityContextRequests::MapEditorIdToRuntimeId, registrationPair.first, runtimeId);
  396. if (runtimeId.IsValid())
  397. {
  398. auto entityIter = captureInfo.m_entities.find(runtimeId);
  399. if (entityIter == captureInfo.m_entities.end())
  400. {
  401. auto insertResult = captureInfo.m_entities.insert(AZStd::make_pair(runtimeId, AZStd::unordered_set< ScriptCanvas::GraphIdentifier >()));
  402. entityIter = insertResult.first;
  403. }
  404. entityIter->second.insert(registrationPair.second);
  405. m_liveDataAggregator.RegisterEntityName(runtimeId, registrationPair.first.GetName());
  406. }
  407. else
  408. {
  409. auto insertResult = captureInfo.m_staticEntities.insert(registrationPair.first);
  410. insertResult.first->second.insert(registrationPair.second);
  411. }
  412. }
  413. }
  414. const LoggingEntityMap& registrationMap = m_liveDataAggregator.GetLoggingEntityMap();
  415. for (const auto& registrationPair : registrationMap)
  416. {
  417. auto entityIter = captureInfo.m_entities.find(registrationPair.first);
  418. if (entityIter == captureInfo.m_entities.end())
  419. {
  420. auto insertResult = captureInfo.m_entities.insert(AZStd::make_pair(registrationPair.first, AZStd::unordered_set< ScriptCanvas::GraphIdentifier >()));
  421. entityIter = insertResult.first;
  422. }
  423. entityIter->second.insert(registrationPair.second);
  424. }
  425. const LoggingAssetSet& registrationSet = m_liveDataAggregator.GetLoggingAssetSet();
  426. for (const auto& graphIdentifier : registrationSet)
  427. {
  428. captureInfo.m_graphs.insert(graphIdentifier.m_assetId);
  429. }
  430. }
  431. void LiveLoggingWindowSession::SetIsCapturing(bool isCapturing)
  432. {
  433. if (isCapturing != m_isCapturing)
  434. {
  435. m_isCapturing = isCapturing;
  436. if (m_isCapturing)
  437. {
  438. StartDataCapture();
  439. }
  440. else
  441. {
  442. StopDataCapture();
  443. }
  444. }
  445. }
  446. #include <Editor/View/Widgets/LoggingPanel/LiveWindowSession/moc_LiveLoggingWindowSession.cpp>
  447. }