BookmarkManagerComponent.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <qgraphicsitem.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <Components/BookmarkManagerComponent.h>
  11. #include <GraphCanvas/Components/VisualBus.h>
  12. #include <GraphCanvas/Components/SceneBus.h>
  13. #include <GraphCanvas/Editor/AssetEditorBus.h>
  14. #include <GraphCanvas/GraphCanvasBus.h>
  15. namespace GraphCanvas
  16. {
  17. /////////////////////////////
  18. // BookmarkManagerComponent
  19. /////////////////////////////
  20. void BookmarkManagerComponent::Reflect(AZ::ReflectContext* context)
  21. {
  22. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  23. if (serializeContext)
  24. {
  25. serializeContext->Class<BookmarkManagerComponent, AZ::Component>()
  26. ->Version(1)
  27. ->Field("QuickBookmarks", &BookmarkManagerComponent::m_shortcuts)
  28. ->Field("Bookmarks", &BookmarkManagerComponent::m_bookmarks)
  29. ;
  30. }
  31. }
  32. BookmarkManagerComponent::BookmarkManagerComponent()
  33. {
  34. }
  35. BookmarkManagerComponent::~BookmarkManagerComponent()
  36. {
  37. }
  38. void BookmarkManagerComponent::Init()
  39. {
  40. m_shortcuts.resize(m_shortcuts.max_size());
  41. }
  42. void BookmarkManagerComponent::Activate()
  43. {
  44. BookmarkManagerRequestBus::Handler::BusConnect(GetEntityId());
  45. }
  46. void BookmarkManagerComponent::Deactivate()
  47. {
  48. BookmarkManagerRequestBus::Handler::BusDisconnect();
  49. }
  50. bool BookmarkManagerComponent::CreateBookmarkAnchor(const AZ::Vector2& position, int bookmarkShortcut)
  51. {
  52. AZ::Entity* bookmarkAnchorEntity = nullptr;
  53. GraphCanvasRequestBus::BroadcastResult(bookmarkAnchorEntity, &GraphCanvasRequests::CreateBookmarkAnchorAndActivate);
  54. if (bookmarkAnchorEntity)
  55. {
  56. AZ::EntityId bookmarkId = bookmarkAnchorEntity->GetId();
  57. BookmarkRequestBus::Event(bookmarkId, &BookmarkRequests::SetShortcut, bookmarkShortcut);
  58. SceneRequestBus::Event(GetEntityId(), &SceneRequests::AddBookmarkAnchor, bookmarkId, position);
  59. }
  60. return bookmarkAnchorEntity != nullptr;
  61. }
  62. void BookmarkManagerComponent::RegisterBookmark(const AZ::EntityId& bookmarkId)
  63. {
  64. auto resultPair = m_bookmarks.insert(bookmarkId);
  65. if (resultPair.second)
  66. {
  67. int shortcut = k_unusedShortcut;
  68. BookmarkRequestBus::EventResult(shortcut, bookmarkId, &BookmarkRequests::GetShortcut);
  69. if (shortcut == k_findShortcut)
  70. {
  71. for (unsigned int i = 1; i < m_shortcuts.size(); ++i)
  72. {
  73. if (!m_shortcuts[i].IsValid())
  74. {
  75. shortcut = i;
  76. break;
  77. }
  78. }
  79. }
  80. if (shortcut > 0)
  81. {
  82. RequestShortcut(bookmarkId, shortcut);
  83. }
  84. else
  85. {
  86. BookmarkRequestBus::Event(bookmarkId, &BookmarkRequests::SetShortcut, k_unusedShortcut);
  87. }
  88. BookmarkNotificationBus::MultiHandler::BusConnect(bookmarkId);
  89. BookmarkManagerNotificationBus::Event(GetEntityId(), &BookmarkManagerNotifications::OnBookmarkAdded, bookmarkId);
  90. }
  91. }
  92. void BookmarkManagerComponent::UnregisterBookmark(const AZ::EntityId& bookmarkId)
  93. {
  94. auto bookmarkIter = m_bookmarks.find(bookmarkId);
  95. AZ_Warning("Graph Canvas", bookmarkIter != m_bookmarks.end(), "Trying to unregister a bookmark from a manager it is not registered to.");
  96. if (bookmarkIter != m_bookmarks.end())
  97. {
  98. UnregisterShortcut(bookmarkId);
  99. m_bookmarks.erase(bookmarkIter);
  100. BookmarkNotificationBus::MultiHandler::BusDisconnect(bookmarkId);
  101. BookmarkManagerNotificationBus::Event(GetEntityId(), &BookmarkManagerNotifications::OnBookmarkRemoved, bookmarkId);
  102. }
  103. }
  104. bool BookmarkManagerComponent::IsBookmarkRegistered(const AZ::EntityId& bookmarkId) const
  105. {
  106. return m_bookmarks.count(bookmarkId) > 0;
  107. }
  108. AZ::EntityId BookmarkManagerComponent::FindBookmarkForShortcut(int shortcut) const
  109. {
  110. if (shortcut <= 0 || shortcut >= m_shortcuts.size())
  111. {
  112. return AZ::EntityId();
  113. }
  114. return m_shortcuts[shortcut];
  115. }
  116. bool BookmarkManagerComponent::RequestShortcut(const AZ::EntityId& bookmark, int shortcut)
  117. {
  118. if ((shortcut <= 0 && shortcut != k_unusedShortcut) || shortcut >= m_shortcuts.size())
  119. {
  120. return false;
  121. }
  122. AZ::EntityId previousBookmark;
  123. if (shortcut != k_unusedShortcut)
  124. {
  125. // If something else is already using these values. Remove the quick index from the old one
  126. // And assign it to the new one.
  127. if (m_shortcuts[shortcut].IsValid())
  128. {
  129. previousBookmark = m_shortcuts[shortcut];
  130. BookmarkRequestBus::Event(m_shortcuts[shortcut], &BookmarkRequests::SetShortcut, k_unusedShortcut);
  131. }
  132. }
  133. // Order here matters. Since when creating a new anchor, the index get's preset. We want to track the previous values
  134. // so we can signal out updates correctly, so we want to unset the previous element first, then try to unset the bookmark's
  135. // previous. This handles both cases, with minimal extra steps.
  136. int previousIndex = k_unusedShortcut;
  137. BookmarkRequestBus::EventResult(previousIndex, bookmark, &BookmarkRequests::GetShortcut);
  138. UnregisterShortcut(bookmark);
  139. if (shortcut != k_unusedShortcut)
  140. {
  141. m_shortcuts[shortcut] = bookmark;
  142. }
  143. BookmarkRequestBus::Event(bookmark, &BookmarkRequests::SetShortcut, shortcut);
  144. BookmarkManagerNotificationBus::Event(GetEntityId(), &BookmarkManagerNotifications::OnShortcutChanged, shortcut, previousBookmark, bookmark);
  145. return true;
  146. }
  147. void BookmarkManagerComponent::ActivateShortcut(int shortcut)
  148. {
  149. JumpToBookmark(FindBookmarkForShortcut(shortcut));
  150. }
  151. void BookmarkManagerComponent::JumpToBookmark(const AZ::EntityId& bookmark)
  152. {
  153. if (!bookmark.IsValid())
  154. {
  155. return;
  156. }
  157. QRectF bookmarkTarget;
  158. BookmarkRequestBus::EventResult(bookmarkTarget, bookmark, &BookmarkRequests::GetBookmarkTarget);
  159. ViewId viewId;
  160. SceneRequestBus::EventResult(viewId, GetEntityId(), &SceneRequests::GetViewId);
  161. EditorId editorId;
  162. SceneRequestBus::EventResult(editorId, GetEntityId(), &SceneRequests::GetEditorId);
  163. bool enableViewportControl = false;
  164. AssetEditorSettingsRequestBus::EventResult(enableViewportControl, editorId, &AssetEditorSettingsRequests::IsBookmarkViewportControlEnabled);
  165. if (enableViewportControl)
  166. {
  167. ViewRequestBus::Event(viewId, &ViewRequests::DisplayArea, bookmarkTarget);
  168. }
  169. else
  170. {
  171. ViewRequestBus::Event(viewId, &ViewRequests::CenterOnArea, bookmarkTarget);
  172. }
  173. BookmarkNotificationBus::Event(bookmark, &BookmarkNotifications::OnBookmarkTriggered);
  174. }
  175. void BookmarkManagerComponent::UnregisterShortcut(const AZ::EntityId& bookmark)
  176. {
  177. int previousIndex = k_unusedShortcut;
  178. BookmarkRequestBus::EventResult(previousIndex, bookmark, &BookmarkRequests::GetShortcut);
  179. if (previousIndex >= 0 && previousIndex < m_shortcuts.size())
  180. {
  181. m_shortcuts[previousIndex].SetInvalid();
  182. }
  183. }
  184. }