WhiteBoxEdgeTranslationModifier.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "EditorWhiteBoxComponentModeBus.h"
  9. #include "EditorWhiteBoxEdgeModifierBus.h"
  10. #include "SubComponentModes/EditorWhiteBoxDefaultModeBus.h"
  11. #include "Util/WhiteBoxMathUtil.h"
  12. #include "Viewport/WhiteBoxModifierUtil.h"
  13. #include "Viewport/WhiteBoxViewportConstants.h"
  14. #include "WhiteBoxEdgeTranslationModifier.h"
  15. #include "WhiteBoxManipulatorViews.h"
  16. #include <AzCore/std/sort.h>
  17. #include <AzCore/std/numeric.h>
  18. #include <AzToolsFramework/Manipulators/ManipulatorManager.h>
  19. #include <AzToolsFramework/Manipulators/ManipulatorView.h>
  20. #include <AzToolsFramework/Manipulators/PlanarManipulator.h>
  21. #include <WhiteBox/EditorWhiteBoxComponentBus.h>
  22. namespace WhiteBox
  23. {
  24. AZ_CLASS_ALLOCATOR_IMPL(EdgeTranslationModifier, AZ::SystemAllocator)
  25. static bool BeginningExtrude(
  26. const AzToolsFramework::PlanarManipulator::Action& action, const AppendStage appendStage)
  27. {
  28. return action.m_modifiers.Ctrl() && appendStage == AppendStage::None;
  29. }
  30. static bool EndingExtrude(const AzToolsFramework::PlanarManipulator::Action& action, const AppendStage appendStage)
  31. {
  32. return !action.m_modifiers.Ctrl() && appendStage != AppendStage::None;
  33. }
  34. static bool AppendInactive(const AppendStage appendStage)
  35. {
  36. return appendStage == AppendStage::None || appendStage == AppendStage::Complete;
  37. }
  38. AZStd::array<AZ::Vector3, 2> GetEdgeNormalAxes(const AZ::Vector3& start, const AZ::Vector3& end)
  39. {
  40. AZ::Vector3 axis1, axis2;
  41. CalculateOrthonormalBasis((start - end).GetNormalized(), axis1, axis2);
  42. return {axis1, axis2};
  43. }
  44. EdgeTranslationModifier::EdgeTranslationModifier(
  45. const AZ::EntityComponentIdPair& entityComponentIdPair, const Api::EdgeHandle edgeHandle,
  46. [[maybe_unused]] const AZ::Vector3& intersectionPoint)
  47. : m_hoveredEdgeHandle{edgeHandle}
  48. , m_entityComponentIdPair(entityComponentIdPair)
  49. {
  50. CreateManipulator();
  51. }
  52. EdgeTranslationModifier::~EdgeTranslationModifier()
  53. {
  54. DestroyManipulator();
  55. }
  56. // return all vertex handles from a collection of edge handles
  57. // (ensure to remove duplicates as vertices will be shared across edges)
  58. static Api::VertexHandles VertexHandlesForEdges(const WhiteBoxMesh& whiteBox, const Api::EdgeHandles& edgeHandles)
  59. {
  60. Api::VertexHandles vertexHandles = AZStd::accumulate(
  61. edgeHandles.cbegin(), edgeHandles.cend(), Api::VertexHandles{},
  62. [&whiteBox](Api::VertexHandles vertexHandles, const Api::EdgeHandle edgeHandle)
  63. {
  64. const auto edgeVertexHandles = Api::EdgeVertexHandles(whiteBox, edgeHandle);
  65. vertexHandles.insert(vertexHandles.end(), edgeVertexHandles.begin(), edgeVertexHandles.end());
  66. return vertexHandles;
  67. });
  68. AZStd::sort(vertexHandles.begin(), vertexHandles.end());
  69. vertexHandles.erase(AZStd::unique(vertexHandles.begin(), vertexHandles.end()), vertexHandles.end());
  70. return vertexHandles;
  71. }
  72. // note: edgeHandles is an inout param and will be modified if its size is 1
  73. static Api::EdgeHandle AttemptEdgeAppend(
  74. WhiteBoxMesh& whiteBox, Api::EdgeHandle hoveredEdgeHandle, Api::EdgeHandles& edgeHandles,
  75. const AZ::Vector3& extrudeVector)
  76. {
  77. // only allow edge extrusion with a single edge
  78. if (edgeHandles.size() == 1)
  79. {
  80. edgeHandles = {Api::TranslateEdgeAppend(whiteBox, hoveredEdgeHandle, extrudeVector)};
  81. return edgeHandles.front();
  82. }
  83. // no append occurred, return original edge handle
  84. return hoveredEdgeHandle;
  85. }
  86. void EdgeTranslationModifier::CreateManipulator()
  87. {
  88. WhiteBoxMesh* whiteBox = nullptr;
  89. EditorWhiteBoxComponentRequestBus::EventResult(
  90. whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
  91. // calculate edge handle group (will be > 1 if connecting vertices have been hidden)
  92. m_edgeHandles = Api::EdgeGrouping(*whiteBox, m_hoveredEdgeHandle);
  93. const auto vertexPositions = Api::EdgeVertexPositions(*whiteBox, m_hoveredEdgeHandle);
  94. const auto axes = GetEdgeNormalAxes(vertexPositions[0], vertexPositions[1]);
  95. m_translationManipulator = AzToolsFramework::PlanarManipulator::MakeShared(
  96. AzToolsFramework::WorldFromLocalWithUniformScale(m_entityComponentIdPair.GetEntityId()));
  97. m_translationManipulator->AddEntityComponentIdPair(m_entityComponentIdPair);
  98. m_translationManipulator->SetLocalPosition(Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle));
  99. m_translationManipulator->SetAxes(axes[0], axes[1]);
  100. CreateView();
  101. m_translationManipulator->Register(AzToolsFramework::g_mainManipulatorManagerId);
  102. struct SharedState
  103. {
  104. // the previous position when moving the manipulator, used to calculate manipulator delta position
  105. AZ::Vector3 m_prevPosition;
  106. // the midpoint of the edge manipulator
  107. AZ::Vector3 m_edgeMidpoint = AZ::Vector3::CreateZero();
  108. // the position of the manipulator the moment an append is initiated
  109. AZ::Vector3 m_initiateAppendPosition = AZ::Vector3::CreateZero();
  110. // the distance the manipulator has moved from where it started when an append begins
  111. AZ::Vector3 m_activeAppendOffset = AZ::Vector3::CreateZero();
  112. // what state of appending are we currently in
  113. AppendStage m_appendStage = AppendStage::None;
  114. // has the modifier moved during the action
  115. bool m_moved = false;
  116. };
  117. auto sharedState = AZStd::make_shared<SharedState>();
  118. m_translationManipulator->InstallLeftMouseDownCallback(
  119. [this, sharedState](const AzToolsFramework::PlanarManipulator::Action& action)
  120. {
  121. WhiteBoxMesh* whiteBox = nullptr;
  122. EditorWhiteBoxComponentRequestBus::EventResult(
  123. whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
  124. // record initial state at mouse down
  125. sharedState->m_prevPosition = action.LocalPosition();
  126. sharedState->m_edgeMidpoint = Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle);
  127. sharedState->m_appendStage = AppendStage::None;
  128. sharedState->m_moved = false;
  129. });
  130. m_translationManipulator->InstallMouseMoveCallback(
  131. [this, sharedState](const AzToolsFramework::PlanarManipulator::Action& action)
  132. {
  133. WhiteBoxMesh* whiteBox = nullptr;
  134. EditorWhiteBoxComponentRequestBus::EventResult(
  135. whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
  136. // has the modifier moved during this interaction
  137. sharedState->m_moved = sharedState->m_moved ||
  138. action.LocalPositionOffset().GetLength() >= cl_whiteBoxMouseClickDeltaThreshold;
  139. // reset append
  140. if (EndingExtrude(action, sharedState->m_appendStage))
  141. {
  142. sharedState->m_appendStage = AppendStage::None;
  143. }
  144. // start trying to extrude
  145. if (BeginningExtrude(action, sharedState->m_appendStage))
  146. {
  147. sharedState->m_appendStage = AppendStage::Initiated;
  148. sharedState->m_initiateAppendPosition = action.LocalPosition();
  149. }
  150. const AZ::Vector3 position = action.LocalPosition();
  151. if (sharedState->m_appendStage == AppendStage::Initiated)
  152. {
  153. const AZ::Vector3 extrudeVector = action.LocalPosition() - sharedState->m_initiateAppendPosition;
  154. float extrudeMagnitude = AZ::GetAbs(extrudeVector.Dot(action.m_fixed.m_axis1)) +
  155. AZ::GetAbs(extrudeVector.Dot(action.m_fixed.m_axis2));
  156. // only extrude after having moved a small amount (to prevent overlapping verts
  157. // and normals being calculated incorrectly)
  158. if (extrudeMagnitude > 0)
  159. {
  160. sharedState->m_activeAppendOffset = action.LocalPositionOffset();
  161. const Api::EdgeHandle nextEdgeHandle =
  162. AttemptEdgeAppend(*whiteBox, m_hoveredEdgeHandle, m_edgeHandles, extrudeVector);
  163. sharedState->m_edgeMidpoint = Api::EdgeMidpoint(*whiteBox, nextEdgeHandle);
  164. sharedState->m_appendStage = AppendStage::Complete;
  165. EditorWhiteBoxEdgeModifierNotificationBus::Broadcast(
  166. &EditorWhiteBoxEdgeModifierNotificationBus::Events::OnEdgeModifierUpdatedEdgeHandle,
  167. m_hoveredEdgeHandle, nextEdgeHandle);
  168. m_hoveredEdgeHandle = nextEdgeHandle;
  169. }
  170. }
  171. else if (AppendInactive(sharedState->m_appendStage))
  172. {
  173. // get the distance the manipulator has moved since the last mouse move
  174. const AZ::Vector3 displacement = position - sharedState->m_prevPosition;
  175. // have to make sure we don't move verts more than once
  176. for (const auto& vertexHandle : VertexHandlesForEdges(*whiteBox, m_edgeHandles))
  177. {
  178. SetVertexPosition(
  179. *whiteBox, vertexHandle, VertexPosition(*whiteBox, vertexHandle) + displacement);
  180. }
  181. }
  182. sharedState->m_prevPosition = position;
  183. // regular movement/translation of vertices
  184. if (AppendInactive(sharedState->m_appendStage))
  185. {
  186. m_translationManipulator->SetLocalPosition(
  187. sharedState->m_edgeMidpoint + action.LocalPositionOffset() - sharedState->m_activeAppendOffset);
  188. EditorWhiteBoxComponentModeRequestBus::Event(
  189. m_entityComponentIdPair,
  190. &EditorWhiteBoxComponentModeRequestBus::Events::MarkWhiteBoxIntersectionDataDirty);
  191. EditorWhiteBoxDefaultModeRequestBus::Event(
  192. m_entityComponentIdPair,
  193. &EditorWhiteBoxDefaultModeRequestBus::Events::RefreshPolygonScaleModifier);
  194. EditorWhiteBoxDefaultModeRequestBus::Event(
  195. m_entityComponentIdPair,
  196. &EditorWhiteBoxDefaultModeRequestBus::Events::RefreshEdgeScaleModifier);
  197. EditorWhiteBoxDefaultModeRequestBus::Event(
  198. m_entityComponentIdPair,
  199. &EditorWhiteBoxDefaultModeRequestBus::Events::RefreshPolygonTranslationModifier);
  200. EditorWhiteBoxDefaultModeRequestBus::Event(
  201. m_entityComponentIdPair,
  202. &EditorWhiteBoxDefaultModeRequestBus::Events::RefreshEdgeTranslationModifier);
  203. EditorWhiteBoxDefaultModeRequestBus::Event(
  204. m_entityComponentIdPair,
  205. &EditorWhiteBoxDefaultModeRequestBus::Events::RefreshVertexSelectionModifier);
  206. }
  207. Api::CalculateNormals(*whiteBox);
  208. Api::CalculatePlanarUVs(*whiteBox);
  209. EditorWhiteBoxComponentNotificationBus::Event(
  210. m_entityComponentIdPair, &EditorWhiteBoxComponentNotificationBus::Events::OnWhiteBoxMeshModified);
  211. });
  212. m_translationManipulator->InstallLeftMouseUpCallback(
  213. [this, sharedState]([[maybe_unused]] const AzToolsFramework::PlanarManipulator::Action& action)
  214. {
  215. // we haven't moved, count as a click
  216. if (!sharedState->m_moved)
  217. {
  218. EditorWhiteBoxDefaultModeRequestBus::Event(
  219. m_entityComponentIdPair, &EditorWhiteBoxDefaultModeRequestBus::Events::CreateEdgeScaleModifier,
  220. m_hoveredEdgeHandle);
  221. EditorWhiteBoxDefaultModeRequestBus::Event(
  222. m_entityComponentIdPair,
  223. &EditorWhiteBoxDefaultModeRequestBus::Events::AssignSelectedEdgeTranslationModifier);
  224. }
  225. else
  226. {
  227. EditorWhiteBoxComponentRequestBus::Event(
  228. m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::SerializeWhiteBox);
  229. }
  230. });
  231. }
  232. void EdgeTranslationModifier::DestroyManipulator()
  233. {
  234. m_translationManipulator->Unregister();
  235. m_translationManipulator.reset();
  236. }
  237. bool EdgeTranslationModifier::MouseOver() const
  238. {
  239. return m_translationManipulator->MouseOver();
  240. }
  241. void EdgeTranslationModifier::ForwardMouseOverEvent(
  242. const AzToolsFramework::ViewportInteraction::MouseInteraction& interaction)
  243. {
  244. m_translationManipulator->ForwardMouseOverEvent(interaction);
  245. }
  246. void EdgeTranslationModifier::Refresh()
  247. {
  248. DestroyManipulator();
  249. CreateManipulator();
  250. }
  251. void EdgeTranslationModifier::CreateView()
  252. {
  253. WhiteBoxMesh* whiteBox = nullptr;
  254. EditorWhiteBoxComponentRequestBus::EventResult(
  255. whiteBox, m_entityComponentIdPair, &EditorWhiteBoxComponentRequests::GetWhiteBoxMesh);
  256. const AZ::Vector3 edgeMidpoint = Api::EdgeMidpoint(*whiteBox, m_hoveredEdgeHandle);
  257. // if the size of the edge handles and views has changed
  258. // we know we need to either add or remove views
  259. if (m_edgeViews.size() != m_edgeHandles.size())
  260. {
  261. m_edgeViews.resize(m_edgeHandles.size());
  262. AZStd::generate(
  263. AZStd::begin(m_edgeViews), AZStd::end(m_edgeViews),
  264. []
  265. {
  266. return AZStd::make_shared<ManipulatorViewEdge>();
  267. });
  268. }
  269. for (size_t edgeIndex = 0; edgeIndex < m_edgeHandles.size(); ++edgeIndex)
  270. {
  271. auto view = m_edgeViews[edgeIndex];
  272. const auto edgeHandle = m_edgeHandles[edgeIndex];
  273. const auto vertexHandles = Api::EdgeVertexHandles(*whiteBox, edgeHandle);
  274. // vertex positions in the local space of the entity
  275. const auto vertexPositions = Api::EdgeVertexPositions(*whiteBox, edgeHandle);
  276. // transform edge start/end positions to be in manipulator space (see UpdateIntersectionPoint)
  277. // (relative to m_translationManipulator local position)
  278. view->m_start = vertexPositions[0] - edgeMidpoint;
  279. view->m_end = vertexPositions[1] - edgeMidpoint;
  280. // record if start/end handles are hidden to adjust dimensions of manipulator view
  281. view->m_vertexStartEndHidden[0] = Api::VertexIsHidden(*whiteBox, vertexHandles[0]);
  282. view->m_vertexStartEndHidden[1] = Api::VertexIsHidden(*whiteBox, vertexHandles[1]);
  283. // only do selection colors for 'selected/hovered' edge handle
  284. if (edgeHandle == m_hoveredEdgeHandle)
  285. {
  286. view->SetColor(m_color, m_hoverColor);
  287. }
  288. else
  289. {
  290. view->SetColor(ed_whiteBoxOutlineHover, ed_whiteBoxOutlineHover);
  291. }
  292. view->SetWidth(m_width, m_hoverWidth);
  293. }
  294. m_translationManipulator->SetViews(
  295. AzToolsFramework::ManipulatorViews{m_edgeViews.cbegin(), m_edgeViews.cend()});
  296. }
  297. void EdgeTranslationModifier::SetColors(const AZ::Color& color, const AZ::Color& hoverColor)
  298. {
  299. m_color = color;
  300. m_hoverColor = hoverColor;
  301. }
  302. void EdgeTranslationModifier::SetWidths(const float width, const float hoverWidth)
  303. {
  304. m_width = width;
  305. m_hoverWidth = hoverWidth;
  306. }
  307. bool EdgeTranslationModifier::PerformingAction() const
  308. {
  309. return m_translationManipulator->PerformingAction();
  310. }
  311. } // namespace WhiteBox