LiveLoggingWindowSession.cpp 16 KB

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