LiveLoggingWindowSession.cpp 18 KB

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