EditorTransformComponentSelectionTests.cpp 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039
  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/Math/IntersectSegment.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzFramework/Components/TransformComponent.h>
  12. #include <AzFramework/Entity/EntityContext.h>
  13. #include <AzFramework/Viewport/ViewportScreen.h>
  14. #include <AzManipulatorTestFramework/AzManipulatorTestFramework.h>
  15. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkTestHelpers.h>
  16. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
  17. #include <AzManipulatorTestFramework/ImmediateModeActionDispatcher.h>
  18. #include <AzManipulatorTestFramework/IndirectManipulatorViewportInteraction.h>
  19. #include <AzManipulatorTestFramework/ViewportInteraction.h>
  20. #include <AzQtComponents/Components/GlobalEventFilter.h>
  21. #include <AzTest/AzTest.h>
  22. #include <AzToolsFramework/Application/ToolsApplication.h>
  23. #include <AzToolsFramework/Entity/EditorEntityActionComponent.h>
  24. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  25. #include <AzToolsFramework/Entity/EditorEntityModel.h>
  26. #include <AzToolsFramework/ToolsComponents/EditorLockComponent.h>
  27. #include <AzToolsFramework/ToolsComponents/EditorVisibilityComponent.h>
  28. #include <AzToolsFramework/ToolsComponents/TransformComponent.h>
  29. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  30. #include <AzToolsFramework/Viewport/ActionBus.h>
  31. #include <AzToolsFramework/Viewport/ViewportSettings.h>
  32. #include <AzToolsFramework/ViewportSelection/EditorDefaultSelection.h>
  33. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  34. #include <AzToolsFramework/ViewportSelection/EditorPickEntitySelection.h>
  35. #include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
  36. #include <AzToolsFramework/ViewportSelection/EditorTransformComponentSelection.h>
  37. #include <AzToolsFramework/ViewportSelection/EditorVisibleEntityDataCache.h>
  38. #include <AzToolsFramework/ViewportUi/ViewportUiManager.h>
  39. #include <Tests/BoundsTestComponent.h>
  40. namespace UnitTest
  41. {
  42. using AzToolsFramework::ViewportInteraction::BuildMouseButtons;
  43. using AzToolsFramework::ViewportInteraction::BuildMouseInteraction;
  44. using AzToolsFramework::ViewportInteraction::BuildMousePick;
  45. AzToolsFramework::EntityIdList SelectedEntities()
  46. {
  47. AzToolsFramework::EntityIdList selectedEntitiesBefore;
  48. AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult(
  49. selectedEntitiesBefore, &AzToolsFramework::ToolsApplicationRequestBus::Events::GetSelectedEntities);
  50. return selectedEntitiesBefore;
  51. }
  52. class EditorEntityVisibilityCacheFixture : public ToolsApplicationFixture<>
  53. {
  54. public:
  55. AzToolsFramework::EntityIdList m_entityIds;
  56. AzToolsFramework::EditorVisibleEntityDataCache m_cache;
  57. };
  58. // Fixture to support testing EditorTransformComponentSelection functionality on an Entity selection.
  59. class EditorTransformComponentSelectionFixture : public ToolsApplicationFixture<>
  60. {
  61. public:
  62. void SetUpEditorFixtureImpl() override
  63. {
  64. m_entityId1 = CreateDefaultEditorEntity("Entity1");
  65. m_entityIds.push_back(m_entityId1);
  66. }
  67. public:
  68. AZ::EntityId m_entityId1;
  69. AzToolsFramework::EntityIdList m_entityIds;
  70. };
  71. AZ::EntityId CreateEntityWithBounds(const char* entityName)
  72. {
  73. AZ::Entity* entity = nullptr;
  74. AZ::EntityId entityId = CreateDefaultEditorEntity(entityName, &entity);
  75. entity->Deactivate();
  76. entity->CreateComponent<BoundsTestComponent>();
  77. entity->Activate();
  78. return entityId;
  79. }
  80. class EditorTransformComponentSelectionViewportPickingFixture : public ToolsApplicationFixture<>
  81. {
  82. public:
  83. void SetUpEditorFixtureImpl() override
  84. {
  85. auto* app = GetApplication();
  86. // register a simple component implementing BoundsRequestBus and EditorComponentSelectionRequestsBus
  87. app->RegisterComponentDescriptor(BoundsTestComponent::CreateDescriptor());
  88. m_entityId1 = CreateEntityWithBounds("Entity1");
  89. m_entityId2 = CreateEntityWithBounds("Entity2");
  90. m_entityId3 = CreateEntityWithBounds("Entity3");
  91. // ensure manipulator view base scale has a sensible default value
  92. AzToolsFramework::SetManipulatorViewBaseScale(1.0f);
  93. }
  94. void PositionEntities()
  95. {
  96. // the initial starting position of the entities
  97. AZ::TransformBus::Event(
  98. m_entityId1, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(Entity1WorldTranslation));
  99. AZ::TransformBus::Event(
  100. m_entityId2, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(Entity2WorldTranslation));
  101. AZ::TransformBus::Event(
  102. m_entityId3, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(Entity3WorldTranslation));
  103. }
  104. static void PositionCamera(AzFramework::CameraState& cameraState)
  105. {
  106. // initial camera position (looking down the negative x-axis)
  107. AzFramework::SetCameraTransform(
  108. cameraState,
  109. AZ::Transform::CreateFromQuaternionAndTranslation(
  110. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 15.0f, 10.0f)));
  111. }
  112. AZ::EntityId m_entityId1;
  113. AZ::EntityId m_entityId2;
  114. AZ::EntityId m_entityId3;
  115. static inline const AZ::Vector3 Entity1WorldTranslation = AZ::Vector3(5.0f, 15.0f, 10.0f);
  116. static inline const AZ::Vector3 Entity2WorldTranslation = AZ::Vector3(5.0f, 14.0f, 10.0f);
  117. static inline const AZ::Vector3 Entity3WorldTranslation = AZ::Vector3(5.0f, 16.0f, 10.0f);
  118. };
  119. void ArrangeIndividualRotatedEntitySelection(const AzToolsFramework::EntityIdList& entityIds, const AZ::Quaternion& orientation)
  120. {
  121. for (auto entityId : entityIds)
  122. {
  123. AZ::TransformBus::Event(entityId, &AZ::TransformBus::Events::SetLocalRotationQuaternion, orientation);
  124. }
  125. }
  126. AZStd::optional<AZ::Transform> GetManipulatorTransform()
  127. {
  128. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  129. AZStd::optional<AZ::Transform> manipulatorTransform;
  130. EditorTransformComponentSelectionRequestBus::EventResult(
  131. manipulatorTransform, AzToolsFramework::GetEntityContextId(),
  132. &EditorTransformComponentSelectionRequestBus::Events::GetManipulatorTransform);
  133. return manipulatorTransform;
  134. }
  135. void RefreshManipulators(const AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::RefreshType refreshType)
  136. {
  137. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  138. EditorTransformComponentSelectionRequestBus::Event(
  139. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::RefreshManipulators, refreshType);
  140. }
  141. void SetTransformMode(const AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::Mode transformMode)
  142. {
  143. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  144. EditorTransformComponentSelectionRequestBus::Event(
  145. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::SetTransformMode, transformMode);
  146. }
  147. void OverrideManipulatorOrientation(const AZ::Quaternion& orientation)
  148. {
  149. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  150. EditorTransformComponentSelectionRequestBus::Event(
  151. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::OverrideManipulatorOrientation,
  152. orientation);
  153. }
  154. void OverrideManipulatorTranslation(const AZ::Vector3& translation)
  155. {
  156. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  157. EditorTransformComponentSelectionRequestBus::Event(
  158. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::OverrideManipulatorTranslation,
  159. translation);
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. // EditorTransformComponentSelection Tests
  163. TEST_F(EditorTransformComponentSelectionFixture, FocusIsNotChangedWhileSwitchingViewportInteractionRequestInstance)
  164. {
  165. // setup a dummy widget and make it the active window to ensure focus in/out events are fired
  166. auto dummyWidget = AZStd::make_unique<QWidget>();
  167. QApplication::setActiveWindow(dummyWidget.get());
  168. // note: it is important to make sure the focus widget is parented to the dummy widget to have focus in/out events fire
  169. auto focusWidget = AZStd::make_unique<UnitTest::FocusInteractionWidget>(dummyWidget.get());
  170. const auto previousFocusWidget = QApplication::focusWidget();
  171. // Given
  172. // setup viewport ui system
  173. AzToolsFramework::ViewportUi::ViewportUiManager viewportUiManager;
  174. viewportUiManager.ConnectViewportUiBus(AzToolsFramework::ViewportUi::DefaultViewportId);
  175. viewportUiManager.InitializeViewportUi(&m_editorActions.m_defaultWidget, focusWidget.get());
  176. // begin EditorPickEntitySelection
  177. using AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus;
  178. EditorInteractionSystemViewportSelectionRequestBus::Event(
  179. AzToolsFramework::GetEntityContextId(), &EditorInteractionSystemViewportSelectionRequestBus::Events::SetHandler,
  180. [](const AzToolsFramework::EditorVisibleEntityDataCacheInterface* entityDataCache,
  181. [[maybe_unused]] AzToolsFramework::ViewportEditorModeTrackerInterface* viewportEditorModeTracker)
  182. {
  183. return AZStd::make_unique<AzToolsFramework::EditorPickEntitySelection>(entityDataCache, viewportEditorModeTracker);
  184. });
  185. // When
  186. // a mouse event is sent to the focus widget (set to be the render overlay in the viewport ui system)
  187. QTest::mouseClick(focusWidget.get(), Qt::MouseButton::LeftButton);
  188. // Then
  189. // focus should not change
  190. EXPECT_FALSE(focusWidget->hasFocus());
  191. EXPECT_EQ(previousFocusWidget, QApplication::focusWidget());
  192. // clean up
  193. viewportUiManager.DisconnectViewportUiBus();
  194. focusWidget.reset();
  195. dummyWidget.reset();
  196. }
  197. TEST_F(EditorTransformComponentSelectionFixture, ManipulatorOrientationIsResetWhenEntityOrientationIsReset)
  198. {
  199. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  200. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201. // Given
  202. AzToolsFramework::SelectEntity(m_entityId1);
  203. const auto entityTransform = AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f)));
  204. ArrangeIndividualRotatedEntitySelection(m_entityIds, entityTransform.GetRotation());
  205. RefreshManipulators(EditorTransformComponentSelectionRequestBus::Events::RefreshType::All);
  206. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Rotation);
  207. const AZ::Transform manipulatorTransformBefore = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  208. // check preconditions - manipulator transform matches the entity transform
  209. EXPECT_THAT(manipulatorTransformBefore, IsClose(entityTransform));
  210. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. // When
  213. // R - reset entity and manipulator orientation when in Rotation Mode
  214. QTest::keyPress(m_defaultMainWindow, Qt::Key_R);
  215. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  217. // Then
  218. const AZ::Transform manipulatorTransformAfter = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  219. // check postconditions - manipulator transform matches parent/world transform (identity)
  220. EXPECT_THAT(manipulatorTransformAfter.GetBasisY(), IsClose(AZ::Vector3::CreateAxisY()));
  221. EXPECT_THAT(manipulatorTransformAfter.GetBasisZ(), IsClose(AZ::Vector3::CreateAxisZ()));
  222. for (auto entityId : m_entityIds)
  223. {
  224. // create invalid starting orientation to guarantee correct data is coming from GetLocalRotationQuaternion
  225. AZ::Quaternion entityOrientation = AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisX(), 90.0f);
  226. AZ::TransformBus::EventResult(entityOrientation, entityId, &AZ::TransformBus::Events::GetLocalRotationQuaternion);
  227. // manipulator orientation matches entity orientation
  228. EXPECT_THAT(entityOrientation, IsClose(manipulatorTransformAfter.GetRotation()));
  229. }
  230. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  231. }
  232. TEST_F(EditorTransformComponentSelectionFixture, EntityOrientationRemainsConstantWhenOnlyManipulatorOrientationIsReset)
  233. {
  234. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  235. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  236. // Given
  237. AzToolsFramework::SelectEntity(m_entityId1);
  238. const AZ::Quaternion initialEntityOrientation = AZ::Quaternion::CreateRotationX(AZ::DegToRad(90.0f));
  239. ArrangeIndividualRotatedEntitySelection(m_entityIds, initialEntityOrientation);
  240. // assign new orientation to manipulator which does not match entity orientation
  241. OverrideManipulatorOrientation(AZ::Quaternion::CreateRotationZ(AZ::DegToRad(90.0f)));
  242. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Rotation);
  243. const AZ::Transform manipulatorTransformBefore = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  244. // check preconditions - manipulator transform matches manipulator orientation override (not entity transform)
  245. EXPECT_THAT(manipulatorTransformBefore.GetBasisX(), IsClose(AZ::Vector3::CreateAxisY()));
  246. EXPECT_THAT(manipulatorTransformBefore.GetBasisY(), IsClose(-AZ::Vector3::CreateAxisX()));
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  249. // When
  250. // Ctrl+R - reset only manipulator orientation when in Rotation Mode
  251. QTest::keyPress(m_defaultMainWindow, Qt::Key_R, Qt::ControlModifier);
  252. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. // Then
  255. const AZ::Transform manipulatorTransformAfter = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  256. // check postconditions - manipulator transform matches parent/world space (manipulator override was cleared)
  257. EXPECT_THAT(manipulatorTransformAfter.GetBasisY(), IsClose(AZ::Vector3::CreateAxisY()));
  258. EXPECT_THAT(manipulatorTransformAfter.GetBasisZ(), IsClose(AZ::Vector3::CreateAxisZ()));
  259. for (auto entityId : m_entityIds)
  260. {
  261. AZ::Quaternion entityOrientation;
  262. AZ::TransformBus::EventResult(entityOrientation, entityId, &AZ::TransformBus::Events::GetLocalRotationQuaternion);
  263. // entity transform matches initial (entity transform was not reset, only manipulator was)
  264. EXPECT_THAT(entityOrientation, IsClose(initialEntityOrientation));
  265. }
  266. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  267. }
  268. TEST_F(EditorTransformComponentSelectionFixture, CopyOrientationToSelectedEntitiesIndividualDoesNotAffectScale)
  269. {
  270. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  271. using ::testing::FloatNear;
  272. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  273. // Given
  274. const auto expectedRotation = AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisZ(), AZ::DegToRad(45.0f));
  275. AZ::TransformBus::Event(m_entityId1, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3::CreateAxisX(10.0f));
  276. AZ::TransformBus::Event(m_entityId1, &AZ::TransformBus::Events::SetLocalUniformScale, 2.0f);
  277. AZ::TransformBus::Event(m_entityId1, &AZ::TransformBus::Events::SetLocalRotationQuaternion, expectedRotation);
  278. AzToolsFramework::SelectEntity(m_entityId1);
  279. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  280. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  281. // When
  282. EditorTransformComponentSelectionRequestBus::Event(
  283. AzToolsFramework::GetEntityContextId(),
  284. &EditorTransformComponentSelectionRequestBus::Events::CopyOrientationToSelectedEntitiesIndividual, expectedRotation);
  285. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  286. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  287. // Then
  288. float scale = 0.0f;
  289. AZ::Quaternion rotation = AZ::Quaternion::CreateIdentity();
  290. AZ::TransformBus::EventResult(rotation, m_entityId1, &AZ::TransformBus::Events::GetLocalRotationQuaternion);
  291. AZ::TransformBus::EventResult(scale, m_entityId1, &AZ::TransformBus::Events::GetLocalUniformScale);
  292. EXPECT_THAT(rotation, IsClose(expectedRotation));
  293. EXPECT_THAT(scale, FloatNear(2.0f, 0.001f));
  294. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  295. }
  296. TEST_F(EditorTransformComponentSelectionFixture, InvertSelectionIgnoresLockedAndHiddenEntities)
  297. {
  298. using ::testing::UnorderedElementsAreArray;
  299. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  300. // Given
  301. // note: entity1 is created in the fixture setup
  302. AzToolsFramework::SelectEntity(m_entityId1);
  303. AZ::EntityId entity2 = CreateDefaultEditorEntity("Entity2");
  304. AZ::EntityId entity3 = CreateDefaultEditorEntity("Entity3");
  305. AZ::EntityId entity4 = CreateDefaultEditorEntity("Entity4");
  306. AZ::EntityId entity5 = CreateDefaultEditorEntity("Entity5");
  307. AZ::EntityId entity6 = CreateDefaultEditorEntity("Entity6");
  308. AzToolsFramework::SetEntityVisibility(entity2, false);
  309. AzToolsFramework::SetEntityLockState(entity3, true);
  310. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  311. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  312. // When
  313. // 'Invert Selection' shortcut
  314. QTest::keyPress(m_defaultMainWindow, Qt::Key_I, Qt::ControlModifier | Qt::ShiftModifier);
  315. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  316. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  317. // Then
  318. const AzToolsFramework::EntityIdList selectedEntities = SelectedEntities();
  319. const AzToolsFramework::EntityIdList expectedSelectedEntities = { entity4, entity5, entity6 };
  320. EXPECT_THAT(selectedEntities, UnorderedElementsAreArray(expectedSelectedEntities));
  321. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  322. }
  323. TEST_F(EditorTransformComponentSelectionFixture, SelectAllIgnoresLockedAndHiddenEntities)
  324. {
  325. using ::testing::UnorderedElementsAreArray;
  326. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  327. // Given
  328. AZ::EntityId entity2 = CreateDefaultEditorEntity("Entity2");
  329. AZ::EntityId entity3 = CreateDefaultEditorEntity("Entity3");
  330. AZ::EntityId entity4 = CreateDefaultEditorEntity("Entity4");
  331. AZ::EntityId entity5 = CreateDefaultEditorEntity("Entity5");
  332. AZ::EntityId entity6 = CreateDefaultEditorEntity("Entity6");
  333. AzToolsFramework::SetEntityVisibility(entity5, false);
  334. AzToolsFramework::SetEntityLockState(entity6, true);
  335. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  336. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  337. // When
  338. // 'Select All' shortcut
  339. QTest::keyPress(m_defaultMainWindow, Qt::Key_A, Qt::ControlModifier);
  340. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  341. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  342. // Then
  343. const AzToolsFramework::EntityIdList selectedEntities = SelectedEntities();
  344. const AzToolsFramework::EntityIdList expectedSelectedEntities = { m_entityId1, entity2, entity3, entity4 };
  345. EXPECT_THAT(selectedEntities, UnorderedElementsAreArray(expectedSelectedEntities));
  346. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  347. }
  348. // fixture for use with the indirect manipulator test framework
  349. using EditorTransformComponentSelectionViewportPickingManipulatorTestFixture =
  350. IndirectCallManipulatorViewportInteractionFixtureMixin<EditorTransformComponentSelectionViewportPickingFixture>;
  351. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, StickySingleClickWithNoSelectionWillSelectEntity)
  352. {
  353. PositionEntities();
  354. PositionCamera(m_cameraState);
  355. using ::testing::Eq;
  356. auto selectedEntitiesBefore = SelectedEntities();
  357. EXPECT_TRUE(selectedEntitiesBefore.empty());
  358. // calculate the position in screen space of the initial entity position
  359. const auto entity1ScreenPosition = AzFramework::WorldToScreen(Entity1WorldTranslation, m_cameraState);
  360. // click the entity in the viewport
  361. m_actionDispatcher->SetStickySelect(true)
  362. ->CameraState(m_cameraState)
  363. ->MousePosition(entity1ScreenPosition)
  364. ->MouseLButtonDown()
  365. ->MouseLButtonUp();
  366. // entity is selected
  367. auto selectedEntitiesAfter = SelectedEntities();
  368. EXPECT_THAT(selectedEntitiesAfter.size(), Eq(1));
  369. EXPECT_THAT(selectedEntitiesAfter.front(), Eq(m_entityId1));
  370. }
  371. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, UnstickySingleClickWithNoSelectionWillSelectEntity)
  372. {
  373. PositionEntities();
  374. PositionCamera(m_cameraState);
  375. using ::testing::Eq;
  376. auto selectedEntitiesBefore = SelectedEntities();
  377. EXPECT_TRUE(selectedEntitiesBefore.empty());
  378. // calculate the position in screen space of the initial entity position
  379. const auto entity1ScreenPosition = AzFramework::WorldToScreen(Entity1WorldTranslation, m_cameraState);
  380. // click the entity in the viewport
  381. m_actionDispatcher->SetStickySelect(false)
  382. ->CameraState(m_cameraState)
  383. ->MousePosition(entity1ScreenPosition)
  384. ->MouseLButtonDown()
  385. ->MouseLButtonUp();
  386. // entity is selected
  387. auto selectedEntitiesAfter = SelectedEntities();
  388. EXPECT_THAT(selectedEntitiesAfter.size(), Eq(1));
  389. EXPECT_THAT(selectedEntitiesAfter.front(), Eq(m_entityId1));
  390. }
  391. TEST_F(
  392. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  393. StickySingleClickOffEntityWithSelectionWillNotDeselectEntity)
  394. {
  395. PositionEntities();
  396. PositionCamera(m_cameraState);
  397. // position in space above the entities
  398. const auto clickOffPositionWorld = AZ::Vector3(5.0f, 15.0f, 12.0f);
  399. AzToolsFramework::SelectEntity(m_entityId1);
  400. // calculate the screen space position of the click
  401. const auto clickOffPositionScreen = AzFramework::WorldToScreen(clickOffPositionWorld, m_cameraState);
  402. // click the empty space in the viewport
  403. m_actionDispatcher->SetStickySelect(true)
  404. ->CameraState(m_cameraState)
  405. ->MousePosition(clickOffPositionScreen)
  406. ->MouseLButtonDown()
  407. ->MouseLButtonUp();
  408. // entity was not deselected
  409. using ::testing::Eq;
  410. auto selectedEntitiesAfter = SelectedEntities();
  411. EXPECT_THAT(selectedEntitiesAfter.size(), Eq(1));
  412. EXPECT_THAT(selectedEntitiesAfter.front(), Eq(m_entityId1));
  413. }
  414. TEST_F(
  415. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, UnstickySingleClickOffEntityWithSelectionWillDeselectEntity)
  416. {
  417. PositionEntities();
  418. PositionCamera(m_cameraState);
  419. AzToolsFramework::SelectEntity(m_entityId1);
  420. // position in space above the entities
  421. const auto clickOffPositionWorld = AZ::Vector3(5.0f, 15.0f, 12.0f);
  422. // calculate the screen space position of the click
  423. const auto clickOffPositionScreen = AzFramework::WorldToScreen(clickOffPositionWorld, m_cameraState);
  424. // click the empty space in the viewport
  425. m_actionDispatcher->SetStickySelect(false)
  426. ->CameraState(m_cameraState)
  427. ->MousePosition(clickOffPositionScreen)
  428. ->MouseLButtonDown()
  429. ->MouseLButtonUp();
  430. // entity was deselected
  431. auto selectedEntitiesAfter = SelectedEntities();
  432. EXPECT_TRUE(selectedEntitiesAfter.empty());
  433. }
  434. TEST_F(
  435. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  436. StickySingleClickOnNewEntityWithSelectionWillNotChangeSelectedEntity)
  437. {
  438. PositionEntities();
  439. PositionCamera(m_cameraState);
  440. AzToolsFramework::SelectEntity(m_entityId1);
  441. // calculate the position in screen space of the second entity
  442. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  443. // click the entity in the viewport
  444. m_actionDispatcher->SetStickySelect(true)
  445. ->CameraState(m_cameraState)
  446. ->MousePosition(entity2ScreenPosition)
  447. ->MouseLButtonDown()
  448. ->MouseLButtonUp();
  449. // entity selection was not changed
  450. using ::testing::Eq;
  451. auto selectedEntitiesAfter = SelectedEntities();
  452. EXPECT_THAT(selectedEntitiesAfter.size(), Eq(1));
  453. EXPECT_THAT(selectedEntitiesAfter.front(), Eq(m_entityId1));
  454. }
  455. TEST_F(
  456. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  457. UnstickySingleClickOnNewEntityWithSelectionWillChangeSelectedEntity)
  458. {
  459. PositionEntities();
  460. PositionCamera(m_cameraState);
  461. AzToolsFramework::SelectEntity(m_entityId1);
  462. // calculate the position in screen space of the second entity
  463. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  464. // click the entity in the viewport
  465. m_actionDispatcher->SetStickySelect(false)
  466. ->CameraState(m_cameraState)
  467. ->MousePosition(entity2ScreenPosition)
  468. ->MouseLButtonDown()
  469. ->MouseLButtonUp();
  470. // entity selection was changed
  471. using ::testing::Eq;
  472. auto selectedEntitiesAfter = SelectedEntities();
  473. EXPECT_THAT(selectedEntitiesAfter.size(), Eq(1));
  474. EXPECT_THAT(selectedEntitiesAfter.front(), Eq(m_entityId2));
  475. }
  476. TEST_F(
  477. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  478. StickyCtrlSingleClickOnNewEntityWithSelectionWillAppendSelectedEntityToSelection)
  479. {
  480. PositionEntities();
  481. PositionCamera(m_cameraState);
  482. AzToolsFramework::SelectEntity(m_entityId1);
  483. // calculate the position in screen space of the second entity
  484. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  485. // click the entity in the viewport
  486. m_actionDispatcher->SetStickySelect(true)
  487. ->CameraState(m_cameraState)
  488. ->MousePosition(entity2ScreenPosition)
  489. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  490. ->MouseLButtonDown()
  491. ->MouseLButtonUp();
  492. // entity selection was changed (one entity selected to two)
  493. using ::testing::UnorderedElementsAre;
  494. auto selectedEntitiesAfter = SelectedEntities();
  495. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1, m_entityId2));
  496. }
  497. TEST_F(
  498. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  499. UnstickyCtrlSingleClickOnNewEntityWithSelectionWillAppendSelectedEntityToSelection)
  500. {
  501. PositionEntities();
  502. PositionCamera(m_cameraState);
  503. AzToolsFramework::SelectEntity(m_entityId1);
  504. // calculate the position in screen space of the second entity
  505. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  506. // click the entity in the viewport
  507. m_actionDispatcher->SetStickySelect(false)
  508. ->CameraState(m_cameraState)
  509. ->MousePosition(entity2ScreenPosition)
  510. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  511. ->MouseLButtonDown()
  512. ->MouseLButtonUp();
  513. // entity selection was changed (one entity selected to two)
  514. using ::testing::UnorderedElementsAre;
  515. auto selectedEntitiesAfter = SelectedEntities();
  516. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1, m_entityId2));
  517. }
  518. TEST_F(
  519. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  520. StickyCtrlSingleClickOnEntityInSelectionWillRemoveEntityFromSelection)
  521. {
  522. PositionEntities();
  523. PositionCamera(m_cameraState);
  524. AzToolsFramework::SelectEntities({ m_entityId1, m_entityId2 });
  525. // calculate the position in screen space of the second entity
  526. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  527. // click the entity in the viewport
  528. m_actionDispatcher->SetStickySelect(true)
  529. ->CameraState(m_cameraState)
  530. ->MousePosition(entity2ScreenPosition)
  531. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  532. ->MouseLButtonDown()
  533. ->MouseLButtonUp();
  534. // entity selection was changed (entity2 was deselected)
  535. using ::testing::UnorderedElementsAre;
  536. auto selectedEntitiesAfter = SelectedEntities();
  537. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  538. }
  539. TEST_F(
  540. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  541. UnstickyCtrlSingleClickOnEntityInSelectionWillRemoveEntityFromSelection)
  542. {
  543. PositionEntities();
  544. PositionCamera(m_cameraState);
  545. AzToolsFramework::SelectEntities({ m_entityId1, m_entityId2 });
  546. // calculate the position in screen space of the second entity
  547. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  548. // click the entity in the viewport
  549. m_actionDispatcher->SetStickySelect(false)
  550. ->CameraState(m_cameraState)
  551. ->MousePosition(entity2ScreenPosition)
  552. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  553. ->MouseLButtonDown()
  554. ->MouseLButtonUp();
  555. // entity selection was changed (entity2 was deselected)
  556. using ::testing::UnorderedElementsAre;
  557. auto selectedEntitiesAfter = SelectedEntities();
  558. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  559. }
  560. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, BoxSelectWithNoInitialSelectionAddsEntitiesToSelection)
  561. {
  562. PositionEntities();
  563. PositionCamera(m_cameraState);
  564. using ::testing::Eq;
  565. auto selectedEntitiesBefore = SelectedEntities();
  566. EXPECT_THAT(selectedEntitiesBefore.size(), Eq(0));
  567. // calculate the position in screen space of where to begin and end the box select action
  568. const auto beginningPositionWorldBoxSelect = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 13.5f, 10.5f), m_cameraState);
  569. const auto endingPositionWorldBoxSelect = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 16.5f, 9.5f), m_cameraState);
  570. // perform a box select in the viewport
  571. m_actionDispatcher->SetStickySelect(true)
  572. ->CameraState(m_cameraState)
  573. ->MousePosition(beginningPositionWorldBoxSelect)
  574. ->MouseLButtonDown()
  575. ->MousePosition(endingPositionWorldBoxSelect)
  576. ->MouseLButtonUp();
  577. // entities are selected
  578. using ::testing::UnorderedElementsAre;
  579. auto selectedEntitiesAfter = SelectedEntities();
  580. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1, m_entityId2, m_entityId3));
  581. }
  582. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, BoxSelectWithSelectionAppendsEntitiesToSelection)
  583. {
  584. PositionEntities();
  585. PositionCamera(m_cameraState);
  586. AzToolsFramework::SelectEntity(m_entityId1);
  587. using ::testing::UnorderedElementsAre;
  588. auto selectedEntitiesBefore = SelectedEntities();
  589. EXPECT_THAT(selectedEntitiesBefore, UnorderedElementsAre(m_entityId1));
  590. // calculate the position in screen space of where to begin and end the box select action
  591. const auto beginningPositionWorldBoxSelect1 = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 14.5f, 10.5f), m_cameraState);
  592. const auto endingPositionWorldBoxSelect1 = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 13.5f, 9.5f), m_cameraState);
  593. const auto beginningPositionWorldBoxSelect2 = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 15.5f, 10.5f), m_cameraState);
  594. const auto endingPositionWorldBoxSelect2 = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 16.5f, 9.5f), m_cameraState);
  595. // perform a box select in the viewport (going left and right)
  596. m_actionDispatcher->SetStickySelect(true)
  597. ->CameraState(m_cameraState)
  598. ->MousePosition(beginningPositionWorldBoxSelect1)
  599. ->MouseLButtonDown()
  600. ->MousePosition(endingPositionWorldBoxSelect1)
  601. ->MouseLButtonUp()
  602. ->MousePosition(beginningPositionWorldBoxSelect2)
  603. ->MouseLButtonDown()
  604. ->MousePosition(endingPositionWorldBoxSelect2)
  605. ->MouseLButtonUp();
  606. // entities are selected
  607. auto selectedEntitiesAfter = SelectedEntities();
  608. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1, m_entityId2, m_entityId3));
  609. }
  610. TEST_F(
  611. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture,
  612. BoxSelectHoldingCtrlWithSelectionRemovesEntitiesFromSelection)
  613. {
  614. PositionEntities();
  615. PositionCamera(m_cameraState);
  616. AzToolsFramework::SelectEntities({ m_entityId1, m_entityId2, m_entityId3 });
  617. using ::testing::UnorderedElementsAre;
  618. auto selectedEntitiesBefore = SelectedEntities();
  619. EXPECT_THAT(selectedEntitiesBefore, UnorderedElementsAre(m_entityId1, m_entityId2, m_entityId3));
  620. // calculate the position in screen space of where to begin and end the box select action
  621. const auto beginningPositionWorldBoxSelect = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 13.5f, 10.5f), m_cameraState);
  622. const auto endingPositionWorldBoxSelect = AzFramework::WorldToScreen(AZ::Vector3(5.0f, 16.5f, 9.5f), m_cameraState);
  623. // perform a box select in the viewport
  624. m_actionDispatcher->SetStickySelect(true)
  625. ->CameraState(m_cameraState)
  626. ->MousePosition(beginningPositionWorldBoxSelect)
  627. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  628. ->MouseLButtonDown()
  629. ->MousePosition(endingPositionWorldBoxSelect)
  630. ->MouseLButtonUp();
  631. // entities are selected
  632. auto selectedEntitiesAfter = SelectedEntities();
  633. EXPECT_TRUE(selectedEntitiesAfter.empty());
  634. }
  635. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, StickyDoubleClickWithSelectionWillDeselectEntities)
  636. {
  637. PositionEntities();
  638. PositionCamera(m_cameraState);
  639. AzToolsFramework::SelectEntities({ m_entityId1, m_entityId2, m_entityId3 });
  640. using ::testing::UnorderedElementsAre;
  641. auto selectedEntitiesBefore = SelectedEntities();
  642. EXPECT_THAT(selectedEntitiesBefore, UnorderedElementsAre(m_entityId1, m_entityId2, m_entityId3));
  643. // position in space above the entities
  644. const auto clickOffPositionWorld = AZ::Vector3(5.0f, 15.0f, 12.0f);
  645. // calculate the screen space position of the click
  646. const auto clickOffPositionScreen = AzFramework::WorldToScreen(clickOffPositionWorld, m_cameraState);
  647. // double click to deselect entities
  648. m_actionDispatcher->SetStickySelect(true)
  649. ->CameraState(m_cameraState)
  650. ->MousePosition(clickOffPositionScreen)
  651. ->MouseLButtonDoubleClick();
  652. // no entities are selected
  653. auto selectedEntitiesAfter = SelectedEntities();
  654. EXPECT_TRUE(selectedEntitiesAfter.empty());
  655. }
  656. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, UnstickyUndoOperationForChangeInSelectionIsAtomic)
  657. {
  658. PositionEntities();
  659. PositionCamera(m_cameraState);
  660. AzToolsFramework::SelectEntity(m_entityId1);
  661. // calculate the position in screen space of the second entity
  662. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  663. // single click select entity2
  664. m_actionDispatcher->SetStickySelect(false)
  665. ->CameraState(m_cameraState)
  666. ->MousePosition(entity2ScreenPosition)
  667. ->MouseLButtonDown()
  668. ->MouseLButtonUp();
  669. // undo action
  670. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequestBus::Events::UndoPressed);
  671. // entity1 is selected after undo
  672. using ::testing::UnorderedElementsAre;
  673. auto selectedEntitiesAfter = SelectedEntities();
  674. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  675. }
  676. TEST_F(
  677. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, BoundsBetweenCameraAndNearClipPlaneDoesNotIntersectMouseRay)
  678. {
  679. // move camera to 10 units along the y-axis
  680. AzFramework::SetCameraTransform(m_cameraState, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  681. // send a very narrow bounds for entity1
  682. AZ::Entity* entity1 = AzToolsFramework::GetEntityById(m_entityId1);
  683. auto* boundTestComponent = entity1->FindComponent<BoundsTestComponent>();
  684. boundTestComponent->m_localBounds =
  685. AZ::Aabb::CreateFromMinMax(AZ::Vector3(-0.5f, -0.0025f, -0.5f), AZ::Vector3(0.5f, 0.0025f, 0.5f));
  686. // move entity1 in front of the camera between it and the near clip plane
  687. AZ::TransformBus::Event(
  688. m_entityId1, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.05f)));
  689. // move entity2 behind entity1
  690. AZ::TransformBus::Event(
  691. m_entityId2, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(15.0f)));
  692. const auto entity2ScreenPosition = AzFramework::WorldToScreen(AzToolsFramework::GetWorldTranslation(m_entityId2), m_cameraState);
  693. // ensure icons are not enabled to avoid them interfering with bound detection
  694. m_viewportManipulatorInteraction->GetViewportInteraction().SetIconsVisible(false);
  695. // click the entity in the viewport
  696. m_actionDispatcher->SetStickySelect(true)
  697. ->CameraState(m_cameraState)
  698. ->MousePosition(entity2ScreenPosition)
  699. ->CameraState(m_cameraState)
  700. ->MouseLButtonDown()
  701. ->MouseLButtonUp();
  702. // ensure entity1 is not selected as it is before the near clip plane
  703. using ::testing::UnorderedElementsAreArray;
  704. const AzToolsFramework::EntityIdList selectedEntities = SelectedEntities();
  705. const AzToolsFramework::EntityIdList expectedSelectedEntities = { m_entityId2 };
  706. EXPECT_THAT(selectedEntities, UnorderedElementsAreArray(expectedSelectedEntities));
  707. }
  708. // entity can be selected using icon
  709. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, CursorOverEntityIconReturnsThatEntityId)
  710. {
  711. const AZ::EntityId boundlessEntityId = CreateDefaultEditorEntity("BoundlessEntity");
  712. // camera (go to position format) -5.00, -8.00, 5.00, 0.00, 0.00
  713. AzFramework::SetCameraTransform(m_cameraState, AZ::Transform::CreateTranslation(AZ::Vector3(-5.0f, -8.0f, 5.0f)));
  714. // position entity in the world
  715. AZ::TransformBus::Event(boundlessEntityId, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(-5.0f, -1.0f, 5.0f));
  716. const float distanceFromCamera = m_cameraState.m_position.GetDistance(AzToolsFramework::GetWorldTranslation(boundlessEntityId));
  717. const auto quaterIconSize = AzToolsFramework::GetIconSize(distanceFromCamera) * 0.25f;
  718. const auto entity1ScreenPosition =
  719. AzFramework::WorldToScreen(AzToolsFramework::GetWorldTranslation(boundlessEntityId), m_cameraState) +
  720. AzFramework::ScreenVectorFromVector2(AZ::Vector2(quaterIconSize));
  721. AzToolsFramework::EditorVisibleEntityDataCache editorVisibleEntityDataCache;
  722. AzToolsFramework::EditorHelpers editorHelpers(&editorVisibleEntityDataCache);
  723. const auto viewportId = m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId();
  724. const auto mousePick = BuildMousePick(m_cameraState, entity1ScreenPosition);
  725. const auto mouseInteraction = BuildMouseInteraction(
  726. mousePick, BuildMouseButtons(AzToolsFramework::ViewportInteraction::MouseButton::None),
  727. AzToolsFramework::ViewportInteraction::InteractionId(AZ::EntityId(), viewportId),
  728. AzToolsFramework::ViewportInteraction::KeyboardModifiers());
  729. const auto mouseInteractionEvent = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  730. mouseInteraction, AzToolsFramework::ViewportInteraction::MouseEvent::Move, false);
  731. // mimic mouse move
  732. m_actionDispatcher->CameraState(m_cameraState)->MousePosition(entity1ScreenPosition);
  733. // simulate hovering over an icon in the viewport
  734. editorVisibleEntityDataCache.CalculateVisibleEntityDatas(AzFramework::ViewportInfo{ viewportId });
  735. auto entityIdUnderCursor = editorHelpers.FindEntityIdUnderCursor(m_cameraState, mouseInteractionEvent);
  736. using ::testing::Eq;
  737. EXPECT_THAT(entityIdUnderCursor.EntityIdUnderCursor(), Eq(boundlessEntityId));
  738. }
  739. // overlapping icons, nearest is detected
  740. TEST_F(EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, CursorOverOverlappingEntityIconsReturnsClosestEntityId)
  741. {
  742. const AZ::EntityId boundlessEntityId1 = CreateDefaultEditorEntity("BoundlessEntity1");
  743. const AZ::EntityId boundlessEntityId2 = CreateDefaultEditorEntity("BoundlessEntity2");
  744. // camera (go to position format) -5.00, -8.00, 5.00, 0.00, 0.00
  745. AzFramework::SetCameraTransform(m_cameraState, AZ::Transform::CreateTranslation(AZ::Vector3(-5.0f, -8.0f, 5.0f)));
  746. // position entities in the world
  747. AZ::TransformBus::Event(boundlessEntityId1, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(-5.0f, -1.0f, 5.0f));
  748. // note: boundlessEntityId2 is closer to the camera
  749. AZ::TransformBus::Event(boundlessEntityId2, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(-5.0f, -3.0f, 5.0f));
  750. const float distanceFromCamera = m_cameraState.m_position.GetDistance(AzToolsFramework::GetWorldTranslation(boundlessEntityId2));
  751. const auto quaterIconSize = AzToolsFramework::GetIconSize(distanceFromCamera) * 0.25f;
  752. const auto entity2ScreenPosition =
  753. AzFramework::WorldToScreen(AzToolsFramework::GetWorldTranslation(boundlessEntityId2), m_cameraState) +
  754. AzFramework::ScreenVectorFromVector2(AZ::Vector2(quaterIconSize));
  755. AzToolsFramework::EditorVisibleEntityDataCache editorVisibleEntityDataCache;
  756. AzToolsFramework::EditorHelpers editorHelpers(&editorVisibleEntityDataCache);
  757. const auto viewportId = m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId();
  758. const auto mousePick = BuildMousePick(m_cameraState, entity2ScreenPosition);
  759. const auto mouseInteraction = BuildMouseInteraction(
  760. mousePick, BuildMouseButtons(AzToolsFramework::ViewportInteraction::MouseButton::None),
  761. AzToolsFramework::ViewportInteraction::InteractionId(AZ::EntityId(), viewportId),
  762. AzToolsFramework::ViewportInteraction::KeyboardModifiers());
  763. const auto mouseInteractionEvent = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  764. mouseInteraction, AzToolsFramework::ViewportInteraction::MouseEvent::Move, false);
  765. // mimic mouse move
  766. m_actionDispatcher->CameraState(m_cameraState)->MousePosition(entity2ScreenPosition);
  767. // simulate hovering over an icon in the viewport
  768. editorVisibleEntityDataCache.CalculateVisibleEntityDatas(AzFramework::ViewportInfo{ viewportId });
  769. auto entityIdUnderCursor = editorHelpers.FindEntityIdUnderCursor(m_cameraState, mouseInteractionEvent);
  770. using ::testing::Eq;
  771. EXPECT_THAT(entityIdUnderCursor.EntityIdUnderCursor(), Eq(boundlessEntityId2));
  772. }
  773. // if an entity with an icon is behind an entity with a bound, the entity with the icon will be selected
  774. // even if the bound is closer (this is because icons are treated as if they are on the near clip plane)
  775. TEST_F(
  776. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture, FurtherAwayEntityWithIconReturnedWhenBoundEntityIsInFront)
  777. {
  778. const AZ::EntityId boundEntityId = CreateEntityWithBounds("BoundEntity");
  779. const AZ::EntityId boundlessEntityId = CreateDefaultEditorEntity("BoundlessEntity");
  780. auto* boundTestComponent = AzToolsFramework::GetEntityById(boundEntityId)->FindComponent<BoundsTestComponent>();
  781. boundTestComponent->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-1.5f, -0.5f, -0.5f), AZ::Vector3(1.5f, 0.5, 0.5f));
  782. // camera (go to position format) -5.00, -8.00, 5.00, 0.00, 0.00
  783. AzFramework::SetCameraTransform(m_cameraState, AZ::Transform::CreateTranslation(AZ::Vector3(-5.0f, -8.0f, 5.0f)));
  784. // position entities in the world
  785. AZ::TransformBus::Event(boundEntityId, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(-4.0f, -3.0f, 5.0f));
  786. // note: boundlessEntityId2 is closer to the camera
  787. AZ::TransformBus::Event(boundlessEntityId, &AZ::TransformBus::Events::SetWorldTranslation, AZ::Vector3(-5.0f, -1.0f, 5.0f));
  788. const float distanceFromCamera = m_cameraState.m_position.GetDistance(AzToolsFramework::GetWorldTranslation(boundlessEntityId));
  789. const auto quaterIconSize = AzToolsFramework::GetIconSize(distanceFromCamera) * 0.25f;
  790. const auto entity2ScreenPosition =
  791. AzFramework::WorldToScreen(AzToolsFramework::GetWorldTranslation(boundlessEntityId), m_cameraState) +
  792. AzFramework::ScreenVectorFromVector2(AZ::Vector2(quaterIconSize));
  793. AzToolsFramework::EditorVisibleEntityDataCache editorVisibleEntityDataCache;
  794. AzToolsFramework::EditorHelpers editorHelpers(&editorVisibleEntityDataCache);
  795. const auto viewportId = m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId();
  796. const auto mousePick = BuildMousePick(m_cameraState, entity2ScreenPosition);
  797. const auto mouseInteraction = BuildMouseInteraction(
  798. mousePick, BuildMouseButtons(AzToolsFramework::ViewportInteraction::MouseButton::None),
  799. AzToolsFramework::ViewportInteraction::InteractionId(AZ::EntityId(), viewportId),
  800. AzToolsFramework::ViewportInteraction::KeyboardModifiers());
  801. const auto mouseInteractionEvent = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  802. mouseInteraction, AzToolsFramework::ViewportInteraction::MouseEvent::Move, false);
  803. // mimic mouse move
  804. m_actionDispatcher->CameraState(m_cameraState)->MousePosition(entity2ScreenPosition);
  805. // simulate hovering over an icon in the viewport
  806. editorVisibleEntityDataCache.CalculateVisibleEntityDatas(AzFramework::ViewportInfo{ viewportId });
  807. auto entityIdUnderCursor = editorHelpers.FindEntityIdUnderCursor(m_cameraState, mouseInteractionEvent);
  808. using ::testing::Eq;
  809. EXPECT_THAT(entityIdUnderCursor.EntityIdUnderCursor(), Eq(boundlessEntityId));
  810. }
  811. class EditorTransformComponentSelectionViewportPickingManipulatorTestFixtureParam
  812. : public EditorTransformComponentSelectionViewportPickingManipulatorTestFixture
  813. , public ::testing::WithParamInterface<bool>
  814. {
  815. };
  816. TEST_P(
  817. EditorTransformComponentSelectionViewportPickingManipulatorTestFixtureParam,
  818. StickyAndUnstickyDittoManipulatorToOtherEntityChangesManipulatorAndDoesNotChangeSelection)
  819. {
  820. PositionEntities();
  821. PositionCamera(m_cameraState);
  822. AzToolsFramework::SelectEntity(m_entityId1);
  823. // calculate the position in screen space of the second entity
  824. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  825. // single click select entity2
  826. m_actionDispatcher->SetStickySelect(GetParam())
  827. ->CameraState(m_cameraState)
  828. ->MousePosition(entity2ScreenPosition)
  829. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  830. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt)
  831. ->MouseLButtonDown()
  832. ->MouseLButtonUp();
  833. // entity1 is still selected
  834. using ::testing::UnorderedElementsAre;
  835. auto selectedEntitiesAfter = SelectedEntities();
  836. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  837. AZStd::optional<AZ::Transform> manipulatorTransform;
  838. AzToolsFramework::EditorTransformComponentSelectionRequestBus::EventResult(
  839. manipulatorTransform, AzToolsFramework::GetEntityContextId(),
  840. &AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::GetManipulatorTransform);
  841. EXPECT_THAT(manipulatorTransform->GetTranslation(), IsClose(Entity2WorldTranslation));
  842. }
  843. TEST_P(
  844. EditorTransformComponentSelectionViewportPickingManipulatorTestFixtureParam,
  845. StickyAndUnstickyDittoManipulatorToOtherEntityChangesManipulatorAndClickOffHasNoEffect)
  846. {
  847. PositionEntities();
  848. PositionCamera(m_cameraState);
  849. AzToolsFramework::SelectEntity(m_entityId1);
  850. // calculate the position in screen space of the second entity
  851. const auto entity2ScreenPosition = AzFramework::WorldToScreen(Entity2WorldTranslation, m_cameraState);
  852. // position in space above the entities
  853. const auto clickOffPositionWorld = AZ::Vector3(5.0f, 15.0f, 12.0f);
  854. // calculate the screen space position of the click
  855. const auto clickOffPositionScreen = AzFramework::WorldToScreen(clickOffPositionWorld, m_cameraState);
  856. using ::testing::UnorderedElementsAre;
  857. // single click select entity2, then click off
  858. m_actionDispatcher->SetStickySelect(GetParam())
  859. ->CameraState(m_cameraState)
  860. ->MousePosition(entity2ScreenPosition)
  861. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  862. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt)
  863. ->MouseLButtonDown()
  864. ->MouseLButtonUp()
  865. ->ExecuteBlock(
  866. [this]()
  867. {
  868. auto selectedEntitiesAfter = SelectedEntities();
  869. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  870. AZStd::optional<AZ::Transform> manipulatorTransform;
  871. AzToolsFramework::EditorTransformComponentSelectionRequestBus::EventResult(
  872. manipulatorTransform, AzToolsFramework::GetEntityContextId(),
  873. &AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::GetManipulatorTransform);
  874. EXPECT_THAT(manipulatorTransform->GetTranslation(), IsClose(Entity2WorldTranslation));
  875. })
  876. ->MousePosition(clickOffPositionScreen)
  877. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  878. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt)
  879. ->MouseLButtonDown()
  880. ->MouseLButtonUp();
  881. auto selectedEntitiesAfter = SelectedEntities();
  882. EXPECT_THAT(selectedEntitiesAfter, UnorderedElementsAre(m_entityId1));
  883. AZStd::optional<AZ::Transform> manipulatorTransform;
  884. AzToolsFramework::EditorTransformComponentSelectionRequestBus::EventResult(
  885. manipulatorTransform, AzToolsFramework::GetEntityContextId(),
  886. &AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::GetManipulatorTransform);
  887. // manipulator transform remains where it was (when using Ctrl+Alt to update the position of the manipulator)
  888. EXPECT_THAT(manipulatorTransform->GetTranslation(), IsClose(Entity2WorldTranslation));
  889. }
  890. INSTANTIATE_TEST_SUITE_P(All, EditorTransformComponentSelectionViewportPickingManipulatorTestFixtureParam, testing::Values(true, false));
  891. // create alias for EditorTransformComponentSelectionViewportPickingManipulatorTestFixture to help group tests
  892. using EditorTransformComponentSelectionManipulatorInteractionTestFixture =
  893. EditorTransformComponentSelectionViewportPickingManipulatorTestFixture;
  894. // type to group related inputs and outcomes for parameterized tests (single entity)
  895. struct ManipulatorOptionsSingle
  896. {
  897. AzToolsFramework::ViewportInteraction::KeyboardModifier m_keyboardModifier;
  898. AZ::Transform m_expectedManipulatorTransformAfter;
  899. AZ::Transform m_expectedEntityTransformAfter;
  900. };
  901. class EditorTransformComponentSelectionRotationManipulatorSingleEntityTestFixtureParam
  902. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  903. , public ::testing::WithParamInterface<ManipulatorOptionsSingle>
  904. {
  905. };
  906. TEST_P(
  907. EditorTransformComponentSelectionRotationManipulatorSingleEntityTestFixtureParam,
  908. RotatingASingleEntityWithDifferentModifierCombinations)
  909. {
  910. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  911. PositionEntities();
  912. PositionCamera(m_cameraState);
  913. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Rotation);
  914. AzToolsFramework::SelectEntity(m_entityId1);
  915. const float screenToWorldMultiplier = AzToolsFramework::CalculateScreenToWorldMultiplier(Entity1WorldTranslation, m_cameraState);
  916. const float manipulatorRadius = 2.0f * screenToWorldMultiplier;
  917. const auto rotationManipulatorStartHoldWorldPosition = Entity1WorldTranslation +
  918. AZ::Quaternion::CreateRotationX(AZ::DegToRad(-45.0f)).TransformVector(AZ::Vector3::CreateAxisY(-manipulatorRadius));
  919. const auto rotationManipulatorEndHoldWorldPosition = Entity1WorldTranslation +
  920. AZ::Quaternion::CreateRotationX(AZ::DegToRad(-135.0f)).TransformVector(AZ::Vector3::CreateAxisY(-manipulatorRadius));
  921. // calculate screen space positions
  922. const auto rotationManipulatorHoldScreenPosition =
  923. AzFramework::WorldToScreen(rotationManipulatorStartHoldWorldPosition, m_cameraState);
  924. const auto rotationManipulatorEndHoldScreenPosition =
  925. AzFramework::WorldToScreen(rotationManipulatorEndHoldWorldPosition, m_cameraState);
  926. m_actionDispatcher->CameraState(m_cameraState)
  927. ->MousePosition(rotationManipulatorHoldScreenPosition)
  928. ->KeyboardModifierDown(GetParam().m_keyboardModifier)
  929. ->MouseLButtonDown()
  930. ->MousePosition(rotationManipulatorEndHoldScreenPosition)
  931. ->MouseLButtonUp();
  932. const auto expectedEntityTransform = GetParam().m_expectedEntityTransformAfter;
  933. const auto expectedManipulatorTransform = GetParam().m_expectedManipulatorTransformAfter;
  934. const auto manipulatorTransform = GetManipulatorTransform();
  935. const auto entityTransform = AzToolsFramework::GetWorldTransform(m_entityId1);
  936. EXPECT_THAT(*manipulatorTransform, IsClose(expectedManipulatorTransform));
  937. EXPECT_THAT(entityTransform, IsClose(expectedEntityTransform));
  938. }
  939. static const AZ::Transform ExpectedTransformAfterLocalRotationManipulatorMotion = AZ::Transform::CreateFromQuaternionAndTranslation(
  940. AZ::Quaternion::CreateRotationX(AZ::DegToRad(-90.0f)),
  941. EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation);
  942. INSTANTIATE_TEST_SUITE_P(
  943. All,
  944. EditorTransformComponentSelectionRotationManipulatorSingleEntityTestFixtureParam,
  945. testing::Values(
  946. // this replicates rotating an entity in local space with no modifiers held
  947. // manipulator and entity rotate
  948. ManipulatorOptionsSingle{ AzToolsFramework::ViewportInteraction::KeyboardModifier::None,
  949. ExpectedTransformAfterLocalRotationManipulatorMotion,
  950. ExpectedTransformAfterLocalRotationManipulatorMotion },
  951. // this replicates rotating an entity in local space with the alt modifier held
  952. // manipulator and entity rotate
  953. ManipulatorOptionsSingle{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt,
  954. ExpectedTransformAfterLocalRotationManipulatorMotion,
  955. ExpectedTransformAfterLocalRotationManipulatorMotion },
  956. // this replicates rotating an entity in world space with the shift modifier held
  957. // entity rotates, manipulator remains aligned to world
  958. ManipulatorOptionsSingle{
  959. AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift,
  960. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation),
  961. ExpectedTransformAfterLocalRotationManipulatorMotion },
  962. // this replicates rotating the manipulator in local space with the ctrl modifier held (entity is unchanged)
  963. ManipulatorOptionsSingle{
  964. AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl, ExpectedTransformAfterLocalRotationManipulatorMotion,
  965. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation) }));
  966. // type to group related inputs and outcomes for parameterized tests (two entities)
  967. struct ManipulatorOptionsMultiple
  968. {
  969. AzToolsFramework::ViewportInteraction::KeyboardModifier m_keyboardModifier;
  970. AZ::Transform m_expectedManipulatorTransformAfter;
  971. AZ::Transform m_firstExpectedEntityTransformAfter;
  972. AZ::Transform m_secondExpectedEntityTransformAfter;
  973. };
  974. class EditorTransformComponentSelectionRotationManipulatorMultipleEntityTestFixtureParam
  975. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  976. , public ::testing::WithParamInterface<ManipulatorOptionsMultiple>
  977. {
  978. };
  979. TEST_P(
  980. EditorTransformComponentSelectionRotationManipulatorMultipleEntityTestFixtureParam,
  981. RotatingMultipleEntitiesWithDifferentModifierCombinations)
  982. {
  983. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  984. PositionEntities();
  985. PositionCamera(m_cameraState);
  986. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Rotation);
  987. AzToolsFramework::SelectEntities({ m_entityId2, m_entityId3 });
  988. // manipulator should be centered between the two entities
  989. const auto initialManipulatorTransform = GetManipulatorTransform();
  990. const float screenToWorldMultiplier =
  991. AzToolsFramework::CalculateScreenToWorldMultiplier(initialManipulatorTransform->GetTranslation(), m_cameraState);
  992. const float manipulatorRadius = 2.0f * screenToWorldMultiplier;
  993. const auto rotationManipulatorStartHoldWorldPosition = initialManipulatorTransform->GetTranslation() +
  994. AZ::Quaternion::CreateRotationX(AZ::DegToRad(-45.0f)).TransformVector(AZ::Vector3::CreateAxisY(-manipulatorRadius));
  995. const auto rotationManipulatorEndHoldWorldPosition = initialManipulatorTransform->GetTranslation() +
  996. AZ::Quaternion::CreateRotationX(AZ::DegToRad(-135.0f)).TransformVector(AZ::Vector3::CreateAxisY(-manipulatorRadius));
  997. // calculate screen space positions
  998. const auto rotationManipulatorHoldScreenPosition =
  999. AzFramework::WorldToScreen(rotationManipulatorStartHoldWorldPosition, m_cameraState);
  1000. const auto rotationManipulatorEndHoldScreenPosition =
  1001. AzFramework::WorldToScreen(rotationManipulatorEndHoldWorldPosition, m_cameraState);
  1002. m_actionDispatcher->CameraState(m_cameraState)
  1003. ->MousePosition(rotationManipulatorHoldScreenPosition)
  1004. ->KeyboardModifierDown(GetParam().m_keyboardModifier)
  1005. ->MouseLButtonDown()
  1006. ->MousePosition(rotationManipulatorEndHoldScreenPosition)
  1007. ->MouseLButtonUp();
  1008. const auto expectedEntity2Transform = GetParam().m_firstExpectedEntityTransformAfter;
  1009. const auto expectedEntity3Transform = GetParam().m_secondExpectedEntityTransformAfter;
  1010. const auto expectedManipulatorTransform = GetParam().m_expectedManipulatorTransformAfter;
  1011. const auto manipulatorTransformAfter = GetManipulatorTransform();
  1012. const auto entity2Transform = AzToolsFramework::GetWorldTransform(m_entityId2);
  1013. const auto entity3Transform = AzToolsFramework::GetWorldTransform(m_entityId3);
  1014. EXPECT_THAT(*manipulatorTransformAfter, IsClose(expectedManipulatorTransform));
  1015. EXPECT_THAT(entity2Transform, IsClose(expectedEntity2Transform));
  1016. EXPECT_THAT(entity3Transform, IsClose(expectedEntity3Transform));
  1017. }
  1018. // note: The aggregate manipulator position will be the average of entity 2 and 3 combined which
  1019. // winds up being the same as entity 1
  1020. static const AZ::Vector3 AggregateManipulatorPositionWithEntity2and3Selected =
  1021. EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation;
  1022. static const AZ::Transform ExpectedEntity2TransformAfterLocalGroupRotationManipulatorMotion =
  1023. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected) *
  1024. AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationX(AZ::DegToRad(-90.0f))) *
  1025. AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(-1.0f));
  1026. static const AZ::Transform ExpectedEntity3TransformAfterLocalGroupRotationManipulatorMotion =
  1027. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected) *
  1028. AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationX(AZ::DegToRad(-90.0f))) *
  1029. AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(1.0f));
  1030. static const AZ::Transform ExpectedEntity2TransformAfterLocalIndividualRotationManipulatorMotion =
  1031. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation) *
  1032. AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationX(AZ::DegToRad(-90.0f)));
  1033. static const AZ::Transform ExpectedEntity3TransformAfterLocalIndividualRotationManipulatorMotion =
  1034. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) *
  1035. AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationX(AZ::DegToRad(-90.0f)));
  1036. INSTANTIATE_TEST_SUITE_P(
  1037. All,
  1038. EditorTransformComponentSelectionRotationManipulatorMultipleEntityTestFixtureParam,
  1039. testing::Values(
  1040. // this replicates rotating a group of entities in local space with no modifiers held
  1041. // manipulator and entity rotate
  1042. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::None,
  1043. ExpectedTransformAfterLocalRotationManipulatorMotion,
  1044. ExpectedEntity2TransformAfterLocalGroupRotationManipulatorMotion,
  1045. ExpectedEntity3TransformAfterLocalGroupRotationManipulatorMotion },
  1046. // this replicates rotating a group of entities in local space with the alt modifier held
  1047. // manipulator and entity rotate
  1048. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt,
  1049. ExpectedTransformAfterLocalRotationManipulatorMotion,
  1050. ExpectedEntity2TransformAfterLocalIndividualRotationManipulatorMotion,
  1051. ExpectedEntity3TransformAfterLocalIndividualRotationManipulatorMotion },
  1052. // this replicates rotating a group of entities in world space with the shift modifier held
  1053. // entity rotates, manipulator remains aligned to world
  1054. ManipulatorOptionsMultiple{
  1055. AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift,
  1056. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation),
  1057. ExpectedEntity2TransformAfterLocalGroupRotationManipulatorMotion,
  1058. ExpectedEntity3TransformAfterLocalGroupRotationManipulatorMotion },
  1059. // this replicates rotating the manipulator in local space with the ctrl modifier held (entity is unchanged)
  1060. ManipulatorOptionsMultiple{
  1061. AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl, ExpectedTransformAfterLocalRotationManipulatorMotion,
  1062. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation),
  1063. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) }));
  1064. class EditorTransformComponentSelectionTranslationManipulatorSingleEntityTestFixtureParam
  1065. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  1066. , public ::testing::WithParamInterface<ManipulatorOptionsSingle>
  1067. {
  1068. };
  1069. static const float LinearManipulatorYAxisMovement = -3.0f;
  1070. static const float LinearManipulatorZAxisMovement = 2.0f;
  1071. TEST_P(
  1072. EditorTransformComponentSelectionTranslationManipulatorSingleEntityTestFixtureParam,
  1073. TranslatingASingleEntityWithDifferentModifierCombinations)
  1074. {
  1075. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1076. PositionEntities();
  1077. // move camera up and to the left so it's just above the normal row of entities
  1078. AzFramework::SetCameraTransform(
  1079. m_cameraState,
  1080. AZ::Transform::CreateFromQuaternionAndTranslation(
  1081. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 14.5, 11.0f)));
  1082. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Translation);
  1083. AzToolsFramework::SelectEntity(m_entityId1);
  1084. const auto entity1Transform = AzToolsFramework::GetWorldTransform(m_entityId1);
  1085. const float screenToWorldMultiplier = AzToolsFramework::CalculateScreenToWorldMultiplier(
  1086. AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation(), m_cameraState);
  1087. // calculate positions for two click and drag motions (moving a linear manipulator)
  1088. // begin each click in the center of the line of the linear manipulators
  1089. const auto translationManipulatorStartHoldWorldPosition1 =
  1090. AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() + entity1Transform.GetBasisZ() * screenToWorldMultiplier;
  1091. const auto translationManipulatorEndHoldWorldPosition1 =
  1092. translationManipulatorStartHoldWorldPosition1 + AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovement);
  1093. const auto translationManipulatorStartHoldWorldPosition2 = AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() +
  1094. AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovement) - entity1Transform.GetBasisY() * screenToWorldMultiplier;
  1095. const auto translationManipulatorEndHoldWorldPosition2 =
  1096. translationManipulatorStartHoldWorldPosition2 + AZ::Vector3::CreateAxisY(LinearManipulatorYAxisMovement);
  1097. // transform to screen space
  1098. const auto translationManipulatorStartHoldScreenPosition1 =
  1099. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition1, m_cameraState);
  1100. const auto translationManipulatorEndHoldScreenPosition1 =
  1101. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition1, m_cameraState);
  1102. const auto translationManipulatorStartHoldScreenPosition2 =
  1103. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition2, m_cameraState);
  1104. const auto translationManipulatorEndHoldScreenPosition2 =
  1105. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition2, m_cameraState);
  1106. m_actionDispatcher->CameraState(m_cameraState)
  1107. ->MousePosition(translationManipulatorStartHoldScreenPosition1)
  1108. ->KeyboardModifierDown(GetParam().m_keyboardModifier)
  1109. ->MouseLButtonDown()
  1110. ->MousePosition(translationManipulatorEndHoldScreenPosition1)
  1111. ->MouseLButtonUp()
  1112. ->MousePosition(translationManipulatorStartHoldScreenPosition2)
  1113. ->MouseLButtonDown()
  1114. ->MousePosition(translationManipulatorEndHoldScreenPosition2)
  1115. ->MouseLButtonUp();
  1116. const auto expectedEntityTransform = GetParam().m_expectedEntityTransformAfter;
  1117. const auto expectedManipulatorTransform = GetParam().m_expectedManipulatorTransformAfter;
  1118. const auto manipulatorTransform = GetManipulatorTransform();
  1119. const auto entityTransform = AzToolsFramework::GetWorldTransform(m_entityId1);
  1120. EXPECT_THAT(*manipulatorTransform, IsCloseTolerance(expectedManipulatorTransform, 0.01f));
  1121. EXPECT_THAT(entityTransform, IsCloseTolerance(expectedEntityTransform, 0.01f));
  1122. }
  1123. static const AZ::Transform ExpectedTransformAfterLocalTranslationManipulatorMotion = AZ::Transform::CreateTranslation(
  1124. EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation +
  1125. AZ::Vector3(0.0f, LinearManipulatorYAxisMovement, LinearManipulatorZAxisMovement));
  1126. // where the manipulator should end up after the input from TranslatingMultipleEntitiesWithDifferentModifierCombinations
  1127. static const AZ::Transform ExpectedManipulatorTransformAfterGroupTranslationManipulatorMotion = AZ::Transform::CreateTranslation(
  1128. AggregateManipulatorPositionWithEntity2and3Selected +
  1129. AZ::Vector3(0.0f, LinearManipulatorYAxisMovement, LinearManipulatorZAxisMovement));
  1130. INSTANTIATE_TEST_SUITE_P(
  1131. All,
  1132. EditorTransformComponentSelectionTranslationManipulatorSingleEntityTestFixtureParam,
  1133. testing::Values(
  1134. // this replicates translating an entity in local space with no modifiers held
  1135. // manipulator and entity translate
  1136. ManipulatorOptionsSingle{ AzToolsFramework::ViewportInteraction::KeyboardModifier::None,
  1137. ExpectedTransformAfterLocalTranslationManipulatorMotion,
  1138. ExpectedTransformAfterLocalTranslationManipulatorMotion },
  1139. // this replicates translating an entity in local space with the alt modifier held
  1140. // manipulator and entity translate (to the user, equivalent to no modifiers with one entity selected)
  1141. ManipulatorOptionsSingle{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt,
  1142. ExpectedTransformAfterLocalTranslationManipulatorMotion,
  1143. ExpectedTransformAfterLocalTranslationManipulatorMotion },
  1144. // this replicates translating an entity in world space with the shift modifier held
  1145. // manipulator and entity translate
  1146. ManipulatorOptionsSingle{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift,
  1147. ExpectedTransformAfterLocalTranslationManipulatorMotion,
  1148. ExpectedTransformAfterLocalTranslationManipulatorMotion },
  1149. // this replicates translating the manipulator in local space with the ctrl modifier held
  1150. // entity is unchanged, manipulator moves
  1151. ManipulatorOptionsSingle{
  1152. AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl, ExpectedTransformAfterLocalTranslationManipulatorMotion,
  1153. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity1WorldTranslation) }));
  1154. class EditorTransformComponentSelectionTranslationManipulatorMultipleEntityTestFixtureParam
  1155. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  1156. , public ::testing::WithParamInterface<ManipulatorOptionsMultiple>
  1157. {
  1158. };
  1159. static const AZ::Transform Entity2RotationForLocalTranslation =
  1160. AZ::Transform::CreateFromQuaternion(AZ::Quaternion::CreateRotationZ(AZ::DegToRad(90.0f)));
  1161. TEST_P(
  1162. EditorTransformComponentSelectionTranslationManipulatorMultipleEntityTestFixtureParam,
  1163. TranslatingMultipleEntitiesWithDifferentModifierCombinations)
  1164. {
  1165. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1166. PositionEntities();
  1167. // move camera up and to the left so it's just above the normal row of entities
  1168. AzFramework::SetCameraTransform(
  1169. m_cameraState,
  1170. AZ::Transform::CreateFromQuaternionAndTranslation(
  1171. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 14.5, 11.0f)));
  1172. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Translation);
  1173. // give entity 2 a different orientation to entity 3 so when moving in local space their translation vectors will be different
  1174. AZ::TransformBus::Event(
  1175. m_entityId2, &AZ::TransformBus::Events::SetWorldRotationQuaternion, Entity2RotationForLocalTranslation.GetRotation());
  1176. AzToolsFramework::SelectEntities({ m_entityId2, m_entityId3 });
  1177. const auto initialManipulatorTransform = GetManipulatorTransform();
  1178. const float screenToWorldMultiplier = AzToolsFramework::CalculateScreenToWorldMultiplier(
  1179. AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation(), m_cameraState);
  1180. // calculate positions for two click and drag motions (moving a linear manipulator)
  1181. // begin each click in the center of the line of the linear manipulators
  1182. const auto translationManipulatorStartHoldWorldPosition1 = AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() +
  1183. initialManipulatorTransform->GetBasisZ() * screenToWorldMultiplier;
  1184. const auto translationManipulatorEndHoldWorldPosition1 =
  1185. translationManipulatorStartHoldWorldPosition1 + AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovement);
  1186. const auto translationManipulatorStartHoldWorldPosition2 = AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() +
  1187. AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovement) - initialManipulatorTransform->GetBasisY() * screenToWorldMultiplier;
  1188. const auto translationManipulatorEndHoldWorldPosition2 =
  1189. translationManipulatorStartHoldWorldPosition2 + AZ::Vector3::CreateAxisY(LinearManipulatorYAxisMovement);
  1190. // transform to screen space
  1191. const auto translationManipulatorStartHoldScreenPosition1 =
  1192. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition1, m_cameraState);
  1193. const auto translationManipulatorEndHoldScreenPosition1 =
  1194. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition1, m_cameraState);
  1195. const auto translationManipulatorStartHoldScreenPosition2 =
  1196. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition2, m_cameraState);
  1197. const auto translationManipulatorEndHoldScreenPosition2 =
  1198. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition2, m_cameraState);
  1199. m_actionDispatcher->CameraState(m_cameraState)
  1200. ->MousePosition(translationManipulatorStartHoldScreenPosition1)
  1201. ->KeyboardModifierDown(GetParam().m_keyboardModifier)
  1202. ->MouseLButtonDown()
  1203. ->MousePosition(translationManipulatorEndHoldScreenPosition1)
  1204. ->MouseLButtonUp()
  1205. ->MousePosition(translationManipulatorStartHoldScreenPosition2)
  1206. ->MouseLButtonDown()
  1207. ->MousePosition(translationManipulatorEndHoldScreenPosition2)
  1208. ->MouseLButtonUp();
  1209. const auto expectedEntity2Transform = GetParam().m_firstExpectedEntityTransformAfter;
  1210. const auto expectedEntity3Transform = GetParam().m_secondExpectedEntityTransformAfter;
  1211. const auto expectedManipulatorTransform = GetParam().m_expectedManipulatorTransformAfter;
  1212. const auto manipulatorTransformAfter = GetManipulatorTransform();
  1213. const auto entity2Transform = AzToolsFramework::GetWorldTransform(m_entityId2);
  1214. const auto entity3Transform = AzToolsFramework::GetWorldTransform(m_entityId3);
  1215. EXPECT_THAT(*manipulatorTransformAfter, IsCloseTolerance(expectedManipulatorTransform, 0.01f));
  1216. EXPECT_THAT(entity2Transform, IsCloseTolerance(expectedEntity2Transform, 0.01f));
  1217. EXPECT_THAT(entity3Transform, IsCloseTolerance(expectedEntity3Transform, 0.01f));
  1218. }
  1219. static const AZ::Transform ExpectedEntity2TransformAfterLocalGroupTranslationManipulatorMotion =
  1220. AZ::Transform::CreateTranslation(
  1221. EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation +
  1222. AZ::Vector3(0.0f, LinearManipulatorYAxisMovement, LinearManipulatorZAxisMovement)) *
  1223. Entity2RotationForLocalTranslation;
  1224. static const AZ::Transform ExpectedEntity3TransformAfterLocalGroupTranslationManipulatorMotion = AZ::Transform::CreateTranslation(
  1225. EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation +
  1226. AZ::Vector3(0.0f, LinearManipulatorYAxisMovement, LinearManipulatorZAxisMovement));
  1227. // note: as entity has been rotated by 90 degrees about Z in TranslatingMultipleEntitiesWithDifferentModifierCombinations then
  1228. // LinearManipulatorYAxisMovement is now aligned to the world x-axis
  1229. static const AZ::Transform ExpectedEntity2TransformAfterLocalIndividualTranslationManipulatorMotion =
  1230. AZ::Transform::CreateTranslation(
  1231. EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation +
  1232. AZ::Vector3(-LinearManipulatorYAxisMovement, 0.0f, LinearManipulatorZAxisMovement)) *
  1233. Entity2RotationForLocalTranslation;
  1234. static const AZ::Transform ExpectedEntity3TransformAfterLocalIndividualTranslationManipulatorMotion = AZ::Transform::CreateTranslation(
  1235. EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation +
  1236. AZ::Vector3(0.0f, LinearManipulatorYAxisMovement, LinearManipulatorZAxisMovement));
  1237. INSTANTIATE_TEST_SUITE_P(
  1238. All,
  1239. EditorTransformComponentSelectionTranslationManipulatorMultipleEntityTestFixtureParam,
  1240. testing::Values(
  1241. // this replicates translating a group of entities in local space with no modifiers held (group influence)
  1242. // manipulator and entity translate
  1243. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::None,
  1244. ExpectedManipulatorTransformAfterGroupTranslationManipulatorMotion,
  1245. ExpectedEntity2TransformAfterLocalGroupTranslationManipulatorMotion,
  1246. ExpectedEntity3TransformAfterLocalGroupTranslationManipulatorMotion },
  1247. // this replicates translating a group of entities in local space with the alt modifier held
  1248. // entities move in their own local space (individual influence)
  1249. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt,
  1250. ExpectedManipulatorTransformAfterGroupTranslationManipulatorMotion,
  1251. ExpectedEntity2TransformAfterLocalIndividualTranslationManipulatorMotion,
  1252. ExpectedEntity3TransformAfterLocalIndividualTranslationManipulatorMotion },
  1253. // this replicates translating a group of entities in world space with the shift modifier held
  1254. // entities and manipulator move in world space
  1255. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift,
  1256. ExpectedManipulatorTransformAfterGroupTranslationManipulatorMotion,
  1257. ExpectedEntity2TransformAfterLocalGroupTranslationManipulatorMotion,
  1258. ExpectedEntity3TransformAfterLocalGroupTranslationManipulatorMotion },
  1259. // this replicates translating the manipulator in local space with the ctrl modifier held (entities are unchanged)
  1260. ManipulatorOptionsMultiple{
  1261. AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl,
  1262. ExpectedManipulatorTransformAfterGroupTranslationManipulatorMotion,
  1263. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation) *
  1264. Entity2RotationForLocalTranslation,
  1265. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) }));
  1266. class EditorTransformComponentSelectionScaleManipulatorMultipleEntityTestFixtureParam
  1267. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  1268. , public ::testing::WithParamInterface<ManipulatorOptionsMultiple>
  1269. {
  1270. };
  1271. static const float LinearManipulatorZAxisMovementScale = 0.5f;
  1272. TEST_P(
  1273. EditorTransformComponentSelectionScaleManipulatorMultipleEntityTestFixtureParam,
  1274. ScalingMultipleEntitiesWithDifferentModifierCombinations)
  1275. {
  1276. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1277. PositionEntities();
  1278. // move camera up and to the left so it's just above the normal row of entities
  1279. AzFramework::SetCameraTransform(
  1280. m_cameraState,
  1281. AZ::Transform::CreateFromQuaternionAndTranslation(
  1282. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 15.0f, 10.1f)));
  1283. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Scale);
  1284. AzToolsFramework::SelectEntities({ m_entityId2, m_entityId3 });
  1285. // manipulator should be centered between the two entities
  1286. const auto initialManipulatorTransform = GetManipulatorTransform();
  1287. const float screenToWorldMultiplier =
  1288. AzToolsFramework::CalculateScreenToWorldMultiplier(initialManipulatorTransform->GetTranslation(), m_cameraState);
  1289. const auto translationManipulatorStartHoldWorldPosition1 = AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() +
  1290. initialManipulatorTransform->GetBasisZ() * screenToWorldMultiplier;
  1291. const auto translationManipulatorEndHoldWorldPosition1 =
  1292. translationManipulatorStartHoldWorldPosition1 + AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovementScale);
  1293. // calculate screen space positions
  1294. const auto scaleManipulatorHoldScreenPosition =
  1295. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition1, m_cameraState);
  1296. const auto scaleManipulatorEndHoldScreenPosition =
  1297. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition1, m_cameraState);
  1298. m_actionDispatcher->CameraState(m_cameraState)
  1299. ->MousePosition(scaleManipulatorHoldScreenPosition)
  1300. ->KeyboardModifierDown(GetParam().m_keyboardModifier)
  1301. ->MouseLButtonDown()
  1302. ->MousePosition(scaleManipulatorEndHoldScreenPosition)
  1303. ->MouseLButtonUp();
  1304. const auto expectedEntity2Transform = GetParam().m_firstExpectedEntityTransformAfter;
  1305. const auto expectedEntity3Transform = GetParam().m_secondExpectedEntityTransformAfter;
  1306. const auto expectedManipulatorTransform = GetParam().m_expectedManipulatorTransformAfter;
  1307. const auto manipulatorTransformAfter = GetManipulatorTransform();
  1308. const auto entity2Transform = AzToolsFramework::GetWorldTransform(m_entityId2);
  1309. const auto entity3Transform = AzToolsFramework::GetWorldTransform(m_entityId3);
  1310. EXPECT_THAT(*manipulatorTransformAfter, IsCloseTolerance(expectedManipulatorTransform, 0.01f));
  1311. EXPECT_THAT(entity2Transform, IsCloseTolerance(expectedEntity2Transform, 0.01f));
  1312. EXPECT_THAT(entity3Transform, IsCloseTolerance(expectedEntity3Transform, 0.01f));
  1313. }
  1314. static const AZ::Transform ExpectedEntity2TransformAfterLocalGroupScaleManipulatorMotion =
  1315. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation) *
  1316. AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, -1.0f, 0.0f)) *
  1317. AZ::Transform::CreateUniformScale(LinearManipulatorZAxisMovement);
  1318. static const AZ::Transform ExpectedEntity3TransformAfterLocalGroupScaleManipulatorMotion =
  1319. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) *
  1320. AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 1.0f, 0.0f)) * AZ::Transform::CreateUniformScale(LinearManipulatorZAxisMovement);
  1321. static const AZ::Transform ExpectedEntity2TransformAfterLocalIndividualScaleManipulatorMotion =
  1322. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation) *
  1323. AZ::Transform::CreateUniformScale(LinearManipulatorZAxisMovement);
  1324. static const AZ::Transform ExpectedEntity3TransformAfterLocalIndividualScaleManipulatorMotion =
  1325. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) *
  1326. AZ::Transform::CreateUniformScale(LinearManipulatorZAxisMovement);
  1327. INSTANTIATE_TEST_SUITE_P(
  1328. All,
  1329. EditorTransformComponentSelectionScaleManipulatorMultipleEntityTestFixtureParam,
  1330. testing::Values(
  1331. // this replicates scaling a group of entities in local space with no modifiers held
  1332. // entities scale relative to manipulator pivot
  1333. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::None,
  1334. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected),
  1335. ExpectedEntity2TransformAfterLocalGroupScaleManipulatorMotion,
  1336. ExpectedEntity3TransformAfterLocalGroupScaleManipulatorMotion },
  1337. // this replicates scaling a group of entities in local space with the alt modifier held
  1338. // entities scale about their own pivot
  1339. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt,
  1340. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected),
  1341. ExpectedEntity2TransformAfterLocalIndividualScaleManipulatorMotion,
  1342. ExpectedEntity3TransformAfterLocalIndividualScaleManipulatorMotion },
  1343. // this replicates scaling a group of entities in world space with the shift modifier held
  1344. // entities scale relative to manipulator pivot in world space
  1345. ManipulatorOptionsMultiple{ AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift,
  1346. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected),
  1347. ExpectedEntity2TransformAfterLocalGroupScaleManipulatorMotion,
  1348. ExpectedEntity3TransformAfterLocalGroupScaleManipulatorMotion },
  1349. // this has no effect (entities and manipulator are unchanged)
  1350. ManipulatorOptionsMultiple{
  1351. AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl,
  1352. AZ::Transform::CreateTranslation(AggregateManipulatorPositionWithEntity2and3Selected),
  1353. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation),
  1354. AZ::Transform::CreateTranslation(EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation) }));
  1355. struct ManipulatorPick
  1356. {
  1357. AZStd::array<AzToolsFramework::ViewportInteraction::KeyboardModifier, 3> m_keyboardModifiers{};
  1358. AZ::Vector3 m_pickPosition;
  1359. AZ::Vector3 m_expectedManipulatorPosition;
  1360. };
  1361. class EditorTransformComponentSelectionTranslationManipulatorPickingEntityTestFixtureParam
  1362. : public EditorTransformComponentSelectionManipulatorInteractionTestFixture
  1363. , public ::testing::WithParamInterface<ManipulatorPick>
  1364. {
  1365. };
  1366. static constexpr float ManipulatorPickBoxHalfSize = 0.5f;
  1367. static constexpr float ManipulatorPickOffsetTolerance = 0.1f;
  1368. TEST_P(
  1369. EditorTransformComponentSelectionTranslationManipulatorPickingEntityTestFixtureParam,
  1370. DittoManipulatorOnEntityChangesManipulatorToEntityTransformOrPickIntersectionBasedOnModifiers)
  1371. {
  1372. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1373. PositionEntities();
  1374. // camera (go to position format) - 10.00, 15.00, 12.00, 0.00, 90.00
  1375. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  1376. AzFramework::SetCameraTransform(
  1377. m_cameraState,
  1378. AZ::Transform::CreateFromQuaternionAndTranslation(
  1379. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 15.0f, 12.0f)));
  1380. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Translation);
  1381. // at position 5.0, 16.0, 10.0 (right most entity)
  1382. AzToolsFramework::SelectEntities({ m_entityId3 });
  1383. const auto clickPositionWorld = GetParam().m_pickPosition;
  1384. const auto clickPositionScreen = AzFramework::WorldToScreen(clickPositionWorld, m_cameraState);
  1385. for (auto modifier : GetParam().m_keyboardModifiers)
  1386. {
  1387. m_actionDispatcher->KeyboardModifierDown(modifier);
  1388. }
  1389. // click the corner of the box
  1390. m_actionDispatcher->CameraState(m_cameraState)->MousePosition(clickPositionScreen)->MouseLButtonDown()->MouseLButtonUp();
  1391. const auto manipulatorPosition = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity()).GetTranslation();
  1392. EXPECT_THAT(manipulatorPosition, IsCloseTolerance(GetParam().m_expectedManipulatorPosition, 0.01f));
  1393. }
  1394. static const AZ::Vector3 ManipulatorPickBoxCorner = EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation +
  1395. AZ::Vector3(ManipulatorPickBoxHalfSize,
  1396. -ManipulatorPickBoxHalfSize + ManipulatorPickOffsetTolerance,
  1397. -ManipulatorPickBoxHalfSize + ManipulatorPickOffsetTolerance);
  1398. INSTANTIATE_TEST_SUITE_P(
  1399. All,
  1400. EditorTransformComponentSelectionTranslationManipulatorPickingEntityTestFixtureParam,
  1401. testing::Values(
  1402. // manipulator should move to exact pick position when ctrl and shift are held
  1403. ManipulatorPick{ { AzToolsFramework::ViewportInteraction::KeyboardModifier::Control,
  1404. AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift },
  1405. ManipulatorPickBoxCorner,
  1406. ManipulatorPickBoxCorner },
  1407. // manipulator should move to picked entity position when ctrl and alt is held
  1408. ManipulatorPick{ { AzToolsFramework::ViewportInteraction::KeyboardModifier::Control,
  1409. AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt },
  1410. ManipulatorPickBoxCorner,
  1411. EditorTransformComponentSelectionViewportPickingFixture::Entity2WorldTranslation },
  1412. ManipulatorPick{ { AzToolsFramework::ViewportInteraction::KeyboardModifier::Control,
  1413. AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift },
  1414. // click position above boxes/entities
  1415. AZ::Vector3(5.0f, 15.0f, 12.0f),
  1416. // position in front of camera when there was no pick intersection (uses GetDefaultEntityPlacementDistance,
  1417. // which has a default value of 10) note: the camera is positioned 10 units along the x-axis looking down it
  1418. // (negative) and the near clip plane is set to 0.1, so the absolute position is -0.1 on the x-axis
  1419. AZ::Vector3(-0.1f, 15.0f, 12.0f) },
  1420. ManipulatorPick{ { AzToolsFramework::ViewportInteraction::KeyboardModifier::Control,
  1421. AzToolsFramework::ViewportInteraction::KeyboardModifier::Alt },
  1422. // click position above boxes/entities
  1423. AZ::Vector3(5.0f, 15.0f, 12.0f),
  1424. // position remains unchanged (manipulator won't move as an entity wasn't picked)
  1425. EditorTransformComponentSelectionViewportPickingFixture::Entity3WorldTranslation }));
  1426. using EditorTransformComponentSelectionScaleManipulatorInteractionTestFixture =
  1427. EditorTransformComponentSelectionManipulatorInteractionTestFixture;
  1428. TEST_F(
  1429. EditorTransformComponentSelectionScaleManipulatorInteractionTestFixture,
  1430. UsingScaleManipulatorWithCtrlHeldAdjustsManipulatorBaseViewScale)
  1431. {
  1432. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1433. PositionEntities();
  1434. // move camera up and to the left so it's just above the normal row of entities
  1435. AzFramework::SetCameraTransform(
  1436. m_cameraState,
  1437. AZ::Transform::CreateFromQuaternionAndTranslation(
  1438. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 90.0f)), AZ::Vector3(10.0f, 15.0f, 10.1f)));
  1439. SetTransformMode(EditorTransformComponentSelectionRequestBus::Events::Mode::Scale);
  1440. AzToolsFramework::SelectEntities({ m_entityId1 });
  1441. // manipulator should be centered between the two entities
  1442. const auto initialManipulatorTransform = GetManipulatorTransform();
  1443. const float screenToWorldMultiplier =
  1444. AzToolsFramework::CalculateScreenToWorldMultiplier(initialManipulatorTransform->GetTranslation(), m_cameraState);
  1445. const auto translationManipulatorStartHoldWorldPosition1 = AzToolsFramework::GetWorldTransform(m_entityId1).GetTranslation() +
  1446. initialManipulatorTransform->GetBasisZ() * screenToWorldMultiplier;
  1447. const auto translationManipulatorEndHoldWorldPosition1 =
  1448. translationManipulatorStartHoldWorldPosition1 + AZ::Vector3::CreateAxisZ(LinearManipulatorZAxisMovementScale);
  1449. // calculate screen space positions
  1450. const auto scaleManipulatorHoldScreenPosition =
  1451. AzFramework::WorldToScreen(translationManipulatorStartHoldWorldPosition1, m_cameraState);
  1452. const auto scaleManipulatorEndHoldScreenPosition =
  1453. AzFramework::WorldToScreen(translationManipulatorEndHoldWorldPosition1, m_cameraState);
  1454. m_actionDispatcher->CameraState(m_cameraState)
  1455. ->MousePosition(scaleManipulatorHoldScreenPosition)
  1456. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  1457. ->MouseLButtonDown()
  1458. ->MousePosition(scaleManipulatorEndHoldScreenPosition)
  1459. ->MouseLButtonUp();
  1460. // verify the view base scale as changed the expected amount based on the adjustment made to the manipulator
  1461. const auto expectedManipulatorViewBaseScale = AzToolsFramework::ManipulatorViewBaseScale();
  1462. EXPECT_NEAR(expectedManipulatorViewBaseScale, 2.0f, 0.01f);
  1463. }
  1464. using EditorTransformComponentSelectionManipulatorTestFixture =
  1465. IndirectCallManipulatorViewportInteractionFixtureMixin<EditorTransformComponentSelectionFixture>;
  1466. TEST_F(EditorTransformComponentSelectionManipulatorTestFixture, CanMoveEntityUsingManipulatorMouseMovement)
  1467. {
  1468. // the initial starting position of the entity (in front and to the left of the camera)
  1469. const auto initialTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(-10.0f, 10.0f, 0.0f));
  1470. // where the entity should end up (in front and to the right of the camera)
  1471. const auto finalTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 0.0f));
  1472. // calculate the position in screen space of the initial position of the entity
  1473. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  1474. // calculate the position in screen space of the final position of the entity
  1475. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  1476. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  1477. AzToolsFramework::SelectEntity(m_entityId1);
  1478. // move the entity to its starting position
  1479. AzToolsFramework::SetWorldTransform(m_entityId1, initialTransformWorld);
  1480. // refresh the manipulators so that they update to the position of the entity
  1481. // note: could skip this by selecting the entity after moving it but its useful to have this for reference
  1482. RefreshManipulators(AzToolsFramework::EditorTransformComponentSelectionRequestBus::Events::RefreshType::All);
  1483. // create an offset along the linear manipulator pointing along the x-axis (perpendicular to the camera view)
  1484. const auto mouseOffsetOnManipulator = AzFramework::ScreenVector(10, 0);
  1485. // store the mouse down position on the manipulator
  1486. const auto mouseDownPosition = initialPositionScreen + mouseOffsetOnManipulator;
  1487. // final position in screen space of the mouse
  1488. const auto mouseMovePosition = finalPositionScreen + mouseOffsetOnManipulator;
  1489. m_actionDispatcher->CameraState(m_cameraState)
  1490. ->MousePosition(mouseDownPosition)
  1491. ->MouseLButtonDown()
  1492. ->MousePosition(mouseMovePosition)
  1493. ->MouseLButtonUp();
  1494. // read back the position of the entity now
  1495. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityId1);
  1496. // ensure final world positions match
  1497. EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, 0.01f));
  1498. }
  1499. TEST_F(EditorTransformComponentSelectionManipulatorTestFixture, TranslatingEntityWithLinearManipulatorNotifiesOnEntityTransformChanged)
  1500. {
  1501. EditorEntityComponentChangeDetector editorEntityChangeDetector(m_entityId1);
  1502. // the initial starting position of the entity (in front and to the left of the camera)
  1503. const auto initialTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(-10.0f, 10.0f, 0.0f));
  1504. // where the entity should end up (in front and to the right of the camera)
  1505. const auto finalTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 10.0f, 0.0f));
  1506. // calculate the position in screen space of the initial position of the entity
  1507. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  1508. // calculate the position in screen space of the final position of the entity
  1509. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  1510. // move the entity to its starting position
  1511. AzToolsFramework::SetWorldTransform(m_entityId1, initialTransformWorld);
  1512. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  1513. AzToolsFramework::SelectEntity(m_entityId1);
  1514. // create an offset along the linear manipulator pointing along the x-axis (perpendicular to the camera view)
  1515. const auto mouseOffsetOnManipulator = AzFramework::ScreenVector(10, 0);
  1516. // store the mouse down position on the manipulator
  1517. const auto mouseDownPosition = initialPositionScreen + mouseOffsetOnManipulator;
  1518. // final position in screen space of the mouse
  1519. const auto mouseMovePosition = finalPositionScreen + mouseOffsetOnManipulator;
  1520. m_actionDispatcher->CameraState(m_cameraState)
  1521. ->MousePosition(mouseDownPosition)
  1522. ->MouseLButtonDown()
  1523. ->MousePosition(mouseMovePosition)
  1524. ->MouseLButtonUp();
  1525. // verify a EditorTransformChangeNotificationBus::OnEntityTransformChanged occurred
  1526. using ::testing::UnorderedElementsAreArray;
  1527. EXPECT_THAT(editorEntityChangeDetector.m_entityIds, UnorderedElementsAreArray(m_entityIds));
  1528. }
  1529. // simple widget to listen for a mouse wheel event and then forward it on to the ViewportSelectionRequestBus
  1530. class WheelEventWidget : public QWidget
  1531. {
  1532. using MouseInteractionResult = AzToolsFramework::ViewportInteraction::MouseInteractionResult;
  1533. public:
  1534. WheelEventWidget(const AzFramework::ViewportId viewportId, QWidget* parent = nullptr)
  1535. : QWidget(parent)
  1536. , m_viewportId(viewportId)
  1537. {
  1538. }
  1539. void wheelEvent(QWheelEvent* ev) override
  1540. {
  1541. namespace vi = AzToolsFramework::ViewportInteraction;
  1542. vi::MouseInteraction mouseInteraction;
  1543. mouseInteraction.m_interactionId.m_cameraId = AZ::EntityId();
  1544. mouseInteraction.m_interactionId.m_viewportId = m_viewportId;
  1545. mouseInteraction.m_mouseButtons = vi::BuildMouseButtons(ev->buttons());
  1546. mouseInteraction.m_mousePick = vi::MousePick();
  1547. mouseInteraction.m_keyboardModifiers = vi::BuildKeyboardModifiers(ev->modifiers());
  1548. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::EventResult(
  1549. m_mouseInteractionResult, AzToolsFramework::GetEntityContextId(),
  1550. &AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Events::InternalHandleAllMouseInteractions,
  1551. vi::MouseInteractionEvent(mouseInteraction, static_cast<float>(ev->angleDelta().y())));
  1552. }
  1553. MouseInteractionResult m_mouseInteractionResult;
  1554. AzFramework::ViewportId m_viewportId;
  1555. };
  1556. TEST_F(EditorTransformComponentSelectionManipulatorTestFixture, MouseScrollWheelSwitchesTransformMode)
  1557. {
  1558. namespace vi = AzToolsFramework::ViewportInteraction;
  1559. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1560. const auto transformMode = []
  1561. {
  1562. EditorTransformComponentSelectionRequestBus::Events::Mode transformMode;
  1563. EditorTransformComponentSelectionRequestBus::EventResult(
  1564. transformMode, AzToolsFramework::GetEntityContextId(),
  1565. &EditorTransformComponentSelectionRequestBus::Events::GetTransformMode);
  1566. return transformMode;
  1567. };
  1568. // given
  1569. // preconditions
  1570. EXPECT_THAT(transformMode(), EditorTransformComponentSelectionRequestBus::Events::Mode::Translation);
  1571. auto wheelEventWidget = WheelEventWidget(m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId());
  1572. // attach the global event filter to the placeholder widget
  1573. AzQtComponents::GlobalEventFilter globalEventFilter(QApplication::instance());
  1574. wheelEventWidget.installEventFilter(&globalEventFilter);
  1575. // example mouse wheel event (does not yet factor in position of mouse in relation to widget)
  1576. auto wheelEvent = QWheelEvent(
  1577. QPointF(0.0f, 0.0f), QPointF(0.0f, 0.0f), QPoint(0, 1), QPoint(0, 0), Qt::MouseButton::NoButton,
  1578. Qt::KeyboardModifier::ControlModifier, Qt::ScrollPhase::ScrollBegin, false,
  1579. Qt::MouseEventSource::MouseEventSynthesizedBySystem);
  1580. // when (trigger mouse wheel event)
  1581. QApplication::sendEvent(&wheelEventWidget, &wheelEvent);
  1582. // then
  1583. // transform mode has changed and mouse event was handled
  1584. using ::testing::Eq;
  1585. EXPECT_THAT(transformMode(), Eq(EditorTransformComponentSelectionRequestBus::Events::Mode::Rotation));
  1586. EXPECT_THAT(wheelEventWidget.m_mouseInteractionResult, Eq(vi::MouseInteractionResult::Viewport));
  1587. }
  1588. TEST_F(EditorTransformComponentSelectionFixture, EntityPositionsCanBeSnappedToGrid)
  1589. {
  1590. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1591. using ::testing::Pointwise;
  1592. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1593. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1594. const AZStd::vector<AZ::Vector3> initialUnsnappedPositions = { AZ::Vector3(1.2f, 3.5f, 6.7f), AZ::Vector3(13.2f, 15.6f, 11.4f),
  1595. AZ::Vector3(4.2f, 103.2f, 16.6f) };
  1596. AZ::TransformBus::Event(m_entityIds[0], &AZ::TransformBus::Events::SetWorldTranslation, initialUnsnappedPositions[0]);
  1597. AZ::TransformBus::Event(m_entityIds[1], &AZ::TransformBus::Events::SetWorldTranslation, initialUnsnappedPositions[1]);
  1598. AZ::TransformBus::Event(m_entityIds[2], &AZ::TransformBus::Events::SetWorldTranslation, initialUnsnappedPositions[2]);
  1599. AzToolsFramework::SelectEntities(m_entityIds);
  1600. EditorTransformComponentSelectionRequestBus::Event(
  1601. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::SnapSelectedEntitiesToWorldGrid,
  1602. 2.0f);
  1603. AZStd::vector<AZ::Vector3> entityPositionsAfterSnap;
  1604. AZStd::transform(
  1605. m_entityIds.cbegin(), m_entityIds.cend(), AZStd::back_inserter(entityPositionsAfterSnap),
  1606. [](const AZ::EntityId& entityId)
  1607. {
  1608. return AzToolsFramework::GetWorldTranslation(entityId);
  1609. });
  1610. const AZStd::vector<AZ::Vector3> expectedSnappedPositions = { AZ::Vector3(2.0f, 4.0f, 6.0f), AZ::Vector3(14.0f, 16.0f, 12.0f),
  1611. AZ::Vector3(4.0f, 104.0f, 16.0f) };
  1612. EXPECT_THAT(entityPositionsAfterSnap, Pointwise(ContainerIsClose(), expectedSnappedPositions));
  1613. }
  1614. TEST_F(EditorTransformComponentSelectionFixture, ManipulatorStaysAlignedToEntityTranslationAfterSnap)
  1615. {
  1616. using AzToolsFramework::EditorTransformComponentSelectionRequestBus;
  1617. const auto initialUnsnappedPosition = AZ::Vector3(1.2f, 3.5f, 6.7f);
  1618. AZ::TransformBus::Event(m_entityIds[0], &AZ::TransformBus::Events::SetWorldTranslation, initialUnsnappedPosition);
  1619. AzToolsFramework::SelectEntities(m_entityIds);
  1620. EditorTransformComponentSelectionRequestBus::Event(
  1621. AzToolsFramework::GetEntityContextId(), &EditorTransformComponentSelectionRequestBus::Events::SnapSelectedEntitiesToWorldGrid,
  1622. 1.0f);
  1623. const auto entityPositionAfterSnap = AzToolsFramework::GetWorldTranslation(m_entityId1);
  1624. const AZ::Vector3 manipulatorPositionAfterSnap =
  1625. GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity()).GetTranslation();
  1626. const auto expectedSnappedPosition = AZ::Vector3(1.0f, 4.0f, 7.0f);
  1627. EXPECT_THAT(entityPositionAfterSnap, IsClose(expectedSnappedPosition));
  1628. EXPECT_THAT(expectedSnappedPosition, IsClose(manipulatorPositionAfterSnap));
  1629. }
  1630. // struct to contain input reference frame and expected orientation outcome based on
  1631. // the reference frame, selection and entity hierarchy
  1632. struct ReferenceFrameWithOrientation
  1633. {
  1634. AzToolsFramework::ReferenceFrame m_referenceFrame; // the input reference frame (Local/Parent/World)
  1635. AZ::Quaternion m_orientation; // the orientation of the manipulator transform
  1636. };
  1637. // custom orientation to compare against for leaf/child entities (when ReferenceFrame is Local)
  1638. static const AZ::Quaternion ChildExpectedPivotLocalOrientationInWorldSpace =
  1639. AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisZ(), AZ::DegToRad(45.0f));
  1640. // custom orientation to compare against for branch/parent entities (when ReferenceFrame is Parent)
  1641. static const AZ::Quaternion ParentExpectedPivotLocalOrientationInWorldSpace =
  1642. AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisX(), AZ::DegToRad(45.0f));
  1643. // custom orientation to compare against for orientation/pivot override
  1644. static const AZ::Quaternion PivotOverrideLocalOrientationInWorldSpace =
  1645. AZ::Quaternion::CreateFromAxisAngle(AZ::Vector3::CreateAxisY(), AZ::DegToRad(90.0f));
  1646. class EditorTransformComponentSelectionSingleEntityPivotFixture
  1647. : public EditorTransformComponentSelectionFixture
  1648. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1649. {
  1650. };
  1651. TEST_P(EditorTransformComponentSelectionSingleEntityPivotFixture, PivotOrientationMatchesReferenceFrameSingleEntity)
  1652. {
  1653. using AzToolsFramework::Etcs::CalculatePivotOrientation;
  1654. using AzToolsFramework::Etcs::PivotOrientationResult;
  1655. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1656. // Given
  1657. AZ::TransformBus::Event(
  1658. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  1659. AZ::Transform::CreateFromQuaternionAndTranslation(ChildExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateZero()));
  1660. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1661. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1662. // When
  1663. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1664. const PivotOrientationResult pivotResult =
  1665. CalculatePivotOrientation(m_entityIds[0], referenceFrameWithOrientation.m_referenceFrame);
  1666. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1667. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1668. // Then
  1669. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1670. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1671. }
  1672. INSTANTIATE_TEST_SUITE_P(
  1673. All,
  1674. EditorTransformComponentSelectionSingleEntityPivotFixture,
  1675. testing::Values(
  1676. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, ChildExpectedPivotLocalOrientationInWorldSpace },
  1677. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, AZ::Quaternion::CreateIdentity() },
  1678. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1679. class EditorTransformComponentSelectionSingleEntityWithParentPivotFixture
  1680. : public EditorTransformComponentSelectionFixture
  1681. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1682. {
  1683. };
  1684. TEST_P(EditorTransformComponentSelectionSingleEntityWithParentPivotFixture, PivotOrientationMatchesReferenceFrameEntityWithParent)
  1685. {
  1686. using AzToolsFramework::Etcs::CalculatePivotOrientation;
  1687. using AzToolsFramework::Etcs::PivotOrientationResult;
  1688. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1689. // Given
  1690. const AZ::EntityId parentEntityId = CreateDefaultEditorEntity("Parent");
  1691. AZ::TransformBus::Event(m_entityIds[0], &AZ::TransformBus::Events::SetParent, parentEntityId);
  1692. AZ::TransformBus::Event(
  1693. parentEntityId, &AZ::TransformBus::Events::SetWorldTM,
  1694. AZ::Transform::CreateFromQuaternionAndTranslation(ParentExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateZero()));
  1695. AZ::TransformBus::Event(
  1696. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  1697. AZ::Transform::CreateFromQuaternionAndTranslation(
  1698. ChildExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateAxisZ(-5.0f)));
  1699. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1700. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1701. // When
  1702. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1703. const PivotOrientationResult pivotResult =
  1704. CalculatePivotOrientation(m_entityIds[0], referenceFrameWithOrientation.m_referenceFrame);
  1705. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1706. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1707. // Then
  1708. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1709. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1710. }
  1711. // with a single entity selected with a parent the orientation reference frames follow as you'd expect
  1712. INSTANTIATE_TEST_SUITE_P(
  1713. All,
  1714. EditorTransformComponentSelectionSingleEntityWithParentPivotFixture,
  1715. testing::Values(
  1716. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, ChildExpectedPivotLocalOrientationInWorldSpace },
  1717. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, ParentExpectedPivotLocalOrientationInWorldSpace },
  1718. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1719. class EditorTransformComponentSelectionMultipleEntitiesPivotFixture
  1720. : public EditorTransformComponentSelectionFixture
  1721. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1722. {
  1723. };
  1724. TEST_P(EditorTransformComponentSelectionMultipleEntitiesPivotFixture, PivotOrientationMatchesReferenceFrameMultipleEntities)
  1725. {
  1726. using AzToolsFramework::Etcs::CalculatePivotOrientationForEntityIds;
  1727. using AzToolsFramework::Etcs::PivotOrientationResult;
  1728. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1729. // Given
  1730. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1731. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1732. // setup entities in arbitrary triangle arrangement
  1733. AZ::TransformBus::Event(
  1734. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(-10.0f)));
  1735. AZ::TransformBus::Event(
  1736. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  1737. AZ::TransformBus::Event(
  1738. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  1739. using AzToolsFramework::EntityIdManipulatorLookup;
  1740. // note: EntityIdManipulatorLookup{} is unused during this test
  1741. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[0], EntityIdManipulatorLookup{} },
  1742. { m_entityIds[1], EntityIdManipulatorLookup{} },
  1743. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  1744. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1745. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1746. // When
  1747. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1748. const PivotOrientationResult pivotResult =
  1749. CalculatePivotOrientationForEntityIds(lookups, referenceFrameWithOrientation.m_referenceFrame);
  1750. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1751. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1752. // Then
  1753. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1754. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1755. }
  1756. // with a group selection, when the entities are not in a hierarchy, no matter what reference frame,
  1757. // we will always get an orientation aligned to the world
  1758. INSTANTIATE_TEST_SUITE_P(
  1759. All,
  1760. EditorTransformComponentSelectionMultipleEntitiesPivotFixture,
  1761. testing::Values(
  1762. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, AZ::Quaternion::CreateIdentity() },
  1763. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, AZ::Quaternion::CreateIdentity() },
  1764. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1765. class EditorTransformComponentSelectionMultipleEntitiesWithSameParentPivotFixture
  1766. : public EditorTransformComponentSelectionFixture
  1767. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1768. {
  1769. };
  1770. TEST_P(
  1771. EditorTransformComponentSelectionMultipleEntitiesWithSameParentPivotFixture,
  1772. PivotOrientationMatchesReferenceFrameMultipleEntitiesSameParent)
  1773. {
  1774. using AzToolsFramework::Etcs::CalculatePivotOrientationForEntityIds;
  1775. using AzToolsFramework::Etcs::PivotOrientationResult;
  1776. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1777. // Given
  1778. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1779. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1780. AZ::TransformBus::Event(
  1781. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  1782. AZ::Transform::CreateFromQuaternionAndTranslation(
  1783. ParentExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateAxisZ(-5.0f)));
  1784. AZ::TransformBus::Event(
  1785. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  1786. AZ::TransformBus::Event(
  1787. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  1788. AZ::TransformBus::Event(m_entityIds[1], &AZ::TransformBus::Events::SetParent, m_entityIds[0]);
  1789. AZ::TransformBus::Event(m_entityIds[2], &AZ::TransformBus::Events::SetParent, m_entityIds[0]);
  1790. using AzToolsFramework::EntityIdManipulatorLookup;
  1791. // note: EntityIdManipulatorLookup{} is unused during this test
  1792. // only select second two entities that are children of m_entityIds[0]
  1793. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[1], EntityIdManipulatorLookup{} },
  1794. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  1795. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1796. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1797. // When
  1798. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1799. const PivotOrientationResult pivotResult =
  1800. CalculatePivotOrientationForEntityIds(lookups, referenceFrameWithOrientation.m_referenceFrame);
  1801. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1802. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1803. // Then
  1804. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1805. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1806. }
  1807. // here two entities are selected with the same parent - local and parent will match parent space, with world
  1808. // giving the identity (aligned to world axes)
  1809. INSTANTIATE_TEST_SUITE_P(
  1810. All,
  1811. EditorTransformComponentSelectionMultipleEntitiesWithSameParentPivotFixture,
  1812. testing::Values(
  1813. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, ParentExpectedPivotLocalOrientationInWorldSpace },
  1814. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, ParentExpectedPivotLocalOrientationInWorldSpace },
  1815. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1816. class EditorTransformComponentSelectionMultipleEntitiesWithDifferentParentPivotFixture
  1817. : public EditorTransformComponentSelectionFixture
  1818. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1819. {
  1820. };
  1821. TEST_P(
  1822. EditorTransformComponentSelectionMultipleEntitiesWithDifferentParentPivotFixture,
  1823. PivotOrientationMatchesReferenceFrameMultipleEntitiesDifferentParent)
  1824. {
  1825. using AzToolsFramework::Etcs::CalculatePivotOrientationForEntityIds;
  1826. using AzToolsFramework::Etcs::PivotOrientationResult;
  1827. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1828. // Given
  1829. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1830. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1831. m_entityIds.push_back(CreateDefaultEditorEntity("Entity4"));
  1832. AZ::TransformBus::Event(
  1833. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  1834. AZ::Transform::CreateFromQuaternionAndTranslation(
  1835. ParentExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateAxisZ(-5.0f)));
  1836. AZ::TransformBus::Event(
  1837. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  1838. AZ::TransformBus::Event(
  1839. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  1840. AZ::TransformBus::Event(m_entityIds[1], &AZ::TransformBus::Events::SetParent, m_entityIds[0]);
  1841. AZ::TransformBus::Event(m_entityIds[2], &AZ::TransformBus::Events::SetParent, m_entityIds[3]);
  1842. using AzToolsFramework::EntityIdManipulatorLookup;
  1843. // note: EntityIdManipulatorLookup{} is unused during this test
  1844. // only select second two entities that are children of different m_entities
  1845. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[1], EntityIdManipulatorLookup{} },
  1846. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  1847. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1848. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1849. // When
  1850. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1851. const PivotOrientationResult pivotResult =
  1852. CalculatePivotOrientationForEntityIds(lookups, referenceFrameWithOrientation.m_referenceFrame);
  1853. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1854. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1855. // Then
  1856. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1857. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1858. }
  1859. // if multiple entities are selected without a parent in common, orientation will always be world again
  1860. INSTANTIATE_TEST_SUITE_P(
  1861. All,
  1862. EditorTransformComponentSelectionMultipleEntitiesWithDifferentParentPivotFixture,
  1863. testing::Values(
  1864. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, AZ::Quaternion::CreateIdentity() },
  1865. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, AZ::Quaternion::CreateIdentity() },
  1866. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1867. class EditorTransformComponentSelectionSingleEntityPivotAndOverrideFixture
  1868. : public EditorTransformComponentSelectionFixture
  1869. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1870. {
  1871. };
  1872. TEST_P(
  1873. EditorTransformComponentSelectionSingleEntityPivotAndOverrideFixture,
  1874. PivotOrientationMatchesReferenceFrameSingleEntityOptionalOverride)
  1875. {
  1876. using AzToolsFramework::Etcs::CalculateSelectionPivotOrientation;
  1877. using AzToolsFramework::Etcs::PivotOrientationResult;
  1878. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1879. // Given
  1880. AZ::TransformBus::Event(
  1881. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  1882. AZ::Transform::CreateFromQuaternionAndTranslation(ChildExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateZero()));
  1883. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1884. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1885. // When
  1886. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1887. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[0], AzToolsFramework::EntityIdManipulatorLookup{} } };
  1888. // set override frame (orientation only)
  1889. AzToolsFramework::OptionalFrame optionalFrame;
  1890. optionalFrame.m_orientationOverride = PivotOverrideLocalOrientationInWorldSpace;
  1891. const PivotOrientationResult pivotResult =
  1892. CalculateSelectionPivotOrientation(lookups, optionalFrame, referenceFrameWithOrientation.m_referenceFrame);
  1893. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1894. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1895. // Then
  1896. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1897. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1898. }
  1899. // local reference frame will still return local orientation for entity, but pivot override will trump parent
  1900. // space (world will still give identity alignment for axes)
  1901. INSTANTIATE_TEST_SUITE_P(
  1902. All,
  1903. EditorTransformComponentSelectionSingleEntityPivotAndOverrideFixture,
  1904. testing::Values(
  1905. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, PivotOverrideLocalOrientationInWorldSpace },
  1906. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, PivotOverrideLocalOrientationInWorldSpace },
  1907. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1908. class EditorTransformComponentSelectionMultipleEntitiesPivotAndOverrideFixture
  1909. : public EditorTransformComponentSelectionFixture
  1910. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1911. {
  1912. };
  1913. TEST_P(
  1914. EditorTransformComponentSelectionMultipleEntitiesPivotAndOverrideFixture,
  1915. PivotOrientationMatchesReferenceFrameMultipleEntitiesOptionalOverride)
  1916. {
  1917. using AzToolsFramework::Etcs::CalculateSelectionPivotOrientation;
  1918. using AzToolsFramework::Etcs::PivotOrientationResult;
  1919. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1920. // Given
  1921. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1922. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1923. AZ::TransformBus::Event(
  1924. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(-10.0f)));
  1925. AZ::TransformBus::Event(
  1926. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  1927. AZ::TransformBus::Event(
  1928. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  1929. using AzToolsFramework::EntityIdManipulatorLookup;
  1930. // note: EntityIdManipulatorLookup{} is unused during this test
  1931. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[0], EntityIdManipulatorLookup{} },
  1932. { m_entityIds[1], EntityIdManipulatorLookup{} },
  1933. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  1934. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1935. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1936. // When
  1937. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1938. AzToolsFramework::OptionalFrame optionalFrame;
  1939. optionalFrame.m_orientationOverride = PivotOverrideLocalOrientationInWorldSpace;
  1940. const PivotOrientationResult pivotResult =
  1941. CalculateSelectionPivotOrientation(lookups, optionalFrame, referenceFrameWithOrientation.m_referenceFrame);
  1942. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1943. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1944. // Then
  1945. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1946. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1947. }
  1948. // with multiple entities selected, override frame wins in both local and parent reference frames
  1949. INSTANTIATE_TEST_SUITE_P(
  1950. All,
  1951. EditorTransformComponentSelectionMultipleEntitiesPivotAndOverrideFixture,
  1952. testing::Values(
  1953. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, PivotOverrideLocalOrientationInWorldSpace },
  1954. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, PivotOverrideLocalOrientationInWorldSpace },
  1955. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  1956. class EditorTransformComponentSelectionMultipleEntitiesPivotAndNoOverrideFixture
  1957. : public EditorTransformComponentSelectionFixture
  1958. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  1959. {
  1960. };
  1961. TEST_P(
  1962. EditorTransformComponentSelectionMultipleEntitiesPivotAndNoOverrideFixture,
  1963. PivotOrientationMatchesReferenceFrameMultipleEntitiesNoOptionalOverride)
  1964. {
  1965. using AzToolsFramework::Etcs::CalculateSelectionPivotOrientation;
  1966. using AzToolsFramework::Etcs::PivotOrientationResult;
  1967. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1968. // Given
  1969. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  1970. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  1971. AZ::TransformBus::Event(
  1972. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(-10.0f)));
  1973. AZ::TransformBus::Event(
  1974. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  1975. AZ::TransformBus::Event(
  1976. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  1977. using AzToolsFramework::EntityIdManipulatorLookup;
  1978. // note: EntityIdManipulatorLookup{} is unused during this test
  1979. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[0], EntityIdManipulatorLookup{} },
  1980. { m_entityIds[1], EntityIdManipulatorLookup{} },
  1981. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  1982. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1983. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1984. // When
  1985. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  1986. AzToolsFramework::OptionalFrame optionalFrame;
  1987. const PivotOrientationResult pivotResult =
  1988. CalculateSelectionPivotOrientation(lookups, optionalFrame, referenceFrameWithOrientation.m_referenceFrame);
  1989. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1990. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1991. // Then
  1992. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  1993. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1994. }
  1995. // multiple entities selected (no hierarchy) always get world aligned axes (identity)
  1996. INSTANTIATE_TEST_SUITE_P(
  1997. All,
  1998. EditorTransformComponentSelectionMultipleEntitiesPivotAndNoOverrideFixture,
  1999. testing::Values(
  2000. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, AZ::Quaternion::CreateIdentity() },
  2001. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, AZ::Quaternion::CreateIdentity() },
  2002. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  2003. class EditorTransformComponentSelectionMultipleEntitiesSameParentPivotAndNoOverrideFixture
  2004. : public EditorTransformComponentSelectionFixture
  2005. , public ::testing::WithParamInterface<ReferenceFrameWithOrientation>
  2006. {
  2007. };
  2008. TEST_P(
  2009. EditorTransformComponentSelectionMultipleEntitiesSameParentPivotAndNoOverrideFixture,
  2010. PivotOrientationMatchesReferenceFrameMultipleEntitiesSameParentNoOptionalOverride)
  2011. {
  2012. using AzToolsFramework::Etcs::CalculateSelectionPivotOrientation;
  2013. using AzToolsFramework::Etcs::PivotOrientationResult;
  2014. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2015. // Given
  2016. m_entityIds.push_back(CreateDefaultEditorEntity("Entity2"));
  2017. m_entityIds.push_back(CreateDefaultEditorEntity("Entity3"));
  2018. AZ::TransformBus::Event(
  2019. m_entityIds[0], &AZ::TransformBus::Events::SetWorldTM,
  2020. AZ::Transform::CreateFromQuaternionAndTranslation(
  2021. ParentExpectedPivotLocalOrientationInWorldSpace, AZ::Vector3::CreateAxisZ(-5.0f)));
  2022. AZ::TransformBus::Event(
  2023. m_entityIds[1], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisX(10.0f)));
  2024. AZ::TransformBus::Event(
  2025. m_entityIds[2], &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3::CreateAxisY(10.0f)));
  2026. AZ::TransformBus::Event(m_entityIds[1], &AZ::TransformBus::Events::SetParent, m_entityIds[0]);
  2027. AZ::TransformBus::Event(m_entityIds[2], &AZ::TransformBus::Events::SetParent, m_entityIds[0]);
  2028. using AzToolsFramework::EntityIdManipulatorLookup;
  2029. // note: EntityIdManipulatorLookup{} is unused during this test
  2030. AzToolsFramework::EntityIdManipulatorLookups lookups{ { m_entityIds[1], EntityIdManipulatorLookup{} },
  2031. { m_entityIds[2], EntityIdManipulatorLookup{} } };
  2032. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2033. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2034. // When
  2035. const ReferenceFrameWithOrientation referenceFrameWithOrientation = GetParam();
  2036. AzToolsFramework::OptionalFrame optionalFrame;
  2037. const PivotOrientationResult pivotResult =
  2038. CalculateSelectionPivotOrientation(lookups, optionalFrame, referenceFrameWithOrientation.m_referenceFrame);
  2039. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2040. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2041. // Then
  2042. EXPECT_THAT(pivotResult.m_worldOrientation, IsClose(referenceFrameWithOrientation.m_orientation));
  2043. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2044. }
  2045. // no optional frame, same parent, local and parent both get parent alignment (world reference frame
  2046. // gives world alignment (identity))
  2047. INSTANTIATE_TEST_SUITE_P(
  2048. All,
  2049. EditorTransformComponentSelectionMultipleEntitiesSameParentPivotAndNoOverrideFixture,
  2050. testing::Values(
  2051. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Local, ParentExpectedPivotLocalOrientationInWorldSpace },
  2052. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::Parent, ParentExpectedPivotLocalOrientationInWorldSpace },
  2053. ReferenceFrameWithOrientation{ AzToolsFramework::ReferenceFrame::World, AZ::Quaternion::CreateIdentity() }));
  2054. class EditorEntityModelVisibilityFixture
  2055. : public ToolsApplicationFixture<>
  2056. , private AzToolsFramework::EditorEntityVisibilityNotificationBus::Router
  2057. , private AzToolsFramework::EditorEntityInfoNotificationBus::Handler
  2058. {
  2059. public:
  2060. void SetUpEditorFixtureImpl() override
  2061. {
  2062. AzToolsFramework::EditorEntityVisibilityNotificationBus::Router::BusRouterConnect();
  2063. AzToolsFramework::EditorEntityInfoNotificationBus::Handler::BusConnect();
  2064. }
  2065. void TearDownEditorFixtureImpl() override
  2066. {
  2067. AzToolsFramework::EditorEntityInfoNotificationBus::Handler::BusDisconnect();
  2068. AzToolsFramework::EditorEntityVisibilityNotificationBus::Router::BusRouterDisconnect();
  2069. }
  2070. };
  2071. class EditorEntityInfoRequestActivateTestComponent : public AzToolsFramework::Components::EditorComponentBase
  2072. {
  2073. public:
  2074. AZ_EDITOR_COMPONENT(
  2075. EditorEntityInfoRequestActivateTestComponent,
  2076. "{849DA1FC-6A0C-4CB8-A0BB-D90DEE7FF7F7}",
  2077. AzToolsFramework::Components::EditorComponentBase);
  2078. static void Reflect(AZ::ReflectContext* context);
  2079. // AZ::Component overrides ...
  2080. void Activate() override
  2081. {
  2082. // ensure we can successfully read IsVisible and IsLocked (bus will be connected to in entity Init)
  2083. AzToolsFramework::EditorEntityInfoRequestBus::EventResult(
  2084. m_visible, GetEntityId(), &AzToolsFramework::EditorEntityInfoRequestBus::Events::IsVisible);
  2085. AzToolsFramework::EditorEntityInfoRequestBus::EventResult(
  2086. m_locked, GetEntityId(), &AzToolsFramework::EditorEntityInfoRequestBus::Events::IsLocked);
  2087. }
  2088. void Deactivate() override
  2089. {
  2090. }
  2091. bool m_visible = false;
  2092. bool m_locked = true;
  2093. };
  2094. void EditorEntityInfoRequestActivateTestComponent::Reflect(AZ::ReflectContext* context)
  2095. {
  2096. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  2097. {
  2098. serializeContext->Class<EditorEntityInfoRequestActivateTestComponent>()->Version(0);
  2099. }
  2100. }
  2101. class EditorEntityModelEntityInfoRequestFixture : public ToolsApplicationFixture<>
  2102. {
  2103. public:
  2104. void SetUpEditorFixtureImpl() override
  2105. {
  2106. GetApplication()->RegisterComponentDescriptor(EditorEntityInfoRequestActivateTestComponent::CreateDescriptor());
  2107. }
  2108. };
  2109. TEST_F(EditorEntityModelEntityInfoRequestFixture, EditorEntityInfoRequestBusRespondsInComponentActivate)
  2110. {
  2111. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2112. // Given
  2113. AZ::Entity* entity = nullptr;
  2114. CreateDefaultEditorEntity("Entity", &entity);
  2115. entity->Deactivate();
  2116. const auto* entityInfoComponent = entity->CreateComponent<EditorEntityInfoRequestActivateTestComponent>();
  2117. // This is necessary to prevent a warning in the undo system.
  2118. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(
  2119. &AzToolsFramework::ToolsApplicationRequests::Bus::Events::AddDirtyEntity, entity->GetId());
  2120. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2121. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2122. // When
  2123. entity->Activate();
  2124. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2125. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2126. // Then
  2127. EXPECT_TRUE(entityInfoComponent->m_visible);
  2128. EXPECT_FALSE(entityInfoComponent->m_locked);
  2129. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2130. }
  2131. TEST(HandleAccents, CurrentValidEntityIdBecomesHoveredWithNoSelectionAndUnstickySelect)
  2132. {
  2133. namespace azvi = AzToolsFramework::ViewportInteraction;
  2134. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2135. AZ::EntityId hoveredEntityEntityId;
  2136. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2137. handleAccentsContext.m_ctrlHeld = false;
  2138. handleAccentsContext.m_hasSelectedEntities = false;
  2139. handleAccentsContext.m_usingBoxSelect = false;
  2140. handleAccentsContext.m_usingStickySelect = false;
  2141. bool currentEntityIdAccentAdded = false;
  2142. AzToolsFramework::HandleAccents(
  2143. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::None),
  2144. [&currentEntityIdAccentAdded, currentEntityId](const AZ::EntityId entityId, const bool accent)
  2145. {
  2146. if (entityId == currentEntityId && accent)
  2147. {
  2148. currentEntityIdAccentAdded = true;
  2149. }
  2150. });
  2151. using ::testing::Eq;
  2152. using ::testing::IsTrue;
  2153. EXPECT_THAT(currentEntityId, Eq(hoveredEntityEntityId));
  2154. EXPECT_THAT(currentEntityIdAccentAdded, IsTrue());
  2155. }
  2156. TEST(HandleAccents, CurrentValidEntityIdBecomesHoveredWithSelectionAndUnstickySelect)
  2157. {
  2158. namespace azvi = AzToolsFramework::ViewportInteraction;
  2159. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2160. AZ::EntityId hoveredEntityEntityId;
  2161. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2162. handleAccentsContext.m_ctrlHeld = false;
  2163. handleAccentsContext.m_hasSelectedEntities = true;
  2164. handleAccentsContext.m_usingBoxSelect = false;
  2165. handleAccentsContext.m_usingStickySelect = false;
  2166. bool currentEntityIdAccentAdded = false;
  2167. AzToolsFramework::HandleAccents(
  2168. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::None),
  2169. [&currentEntityIdAccentAdded, currentEntityId](const AZ::EntityId entityId, const bool accent)
  2170. {
  2171. if (entityId == currentEntityId && accent)
  2172. {
  2173. currentEntityIdAccentAdded = true;
  2174. }
  2175. });
  2176. using ::testing::Eq;
  2177. using ::testing::IsTrue;
  2178. EXPECT_THAT(currentEntityId, Eq(hoveredEntityEntityId));
  2179. EXPECT_THAT(currentEntityIdAccentAdded, IsTrue());
  2180. }
  2181. TEST(HandleAccents, CurrentValidEntityIdDoesNotBecomeHoveredWithSelectionUnstickySelectAndInvalidButton)
  2182. {
  2183. namespace azvi = AzToolsFramework::ViewportInteraction;
  2184. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2185. AZ::EntityId hoveredEntityEntityId = AZ::EntityId(54321);
  2186. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2187. handleAccentsContext.m_ctrlHeld = false;
  2188. handleAccentsContext.m_hasSelectedEntities = false;
  2189. handleAccentsContext.m_usingBoxSelect = false;
  2190. handleAccentsContext.m_usingStickySelect = false;
  2191. bool hoveredEntityIdAccentRemoved = false;
  2192. AzToolsFramework::HandleAccents(
  2193. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::Middle),
  2194. [&hoveredEntityIdAccentRemoved, hoveredEntityEntityId](const AZ::EntityId entityId, const bool accent)
  2195. {
  2196. if (entityId == hoveredEntityEntityId && !accent)
  2197. {
  2198. hoveredEntityIdAccentRemoved = true;
  2199. }
  2200. });
  2201. using ::testing::Eq;
  2202. using ::testing::IsFalse;
  2203. using ::testing::IsTrue;
  2204. EXPECT_THAT(hoveredEntityEntityId.IsValid(), IsFalse());
  2205. EXPECT_THAT(hoveredEntityIdAccentRemoved, IsTrue());
  2206. }
  2207. TEST(HandleAccents, CurrentValidEntityIdDoesNotBecomeHoveredWithSelectionUnstickySelectAndDoingBoxSelect)
  2208. {
  2209. namespace azvi = AzToolsFramework::ViewportInteraction;
  2210. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2211. AZ::EntityId hoveredEntityEntityId = AZ::EntityId(54321);
  2212. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2213. handleAccentsContext.m_ctrlHeld = false;
  2214. handleAccentsContext.m_hasSelectedEntities = false;
  2215. handleAccentsContext.m_usingBoxSelect = true;
  2216. handleAccentsContext.m_usingStickySelect = false;
  2217. bool hoveredEntityIdAccentRemoved = false;
  2218. AzToolsFramework::HandleAccents(
  2219. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::None),
  2220. [&hoveredEntityIdAccentRemoved, hoveredEntityEntityId](const AZ::EntityId entityId, const bool accent)
  2221. {
  2222. if (entityId == hoveredEntityEntityId && !accent)
  2223. {
  2224. hoveredEntityIdAccentRemoved = true;
  2225. }
  2226. });
  2227. using ::testing::Eq;
  2228. using ::testing::IsFalse;
  2229. using ::testing::IsTrue;
  2230. EXPECT_THAT(hoveredEntityEntityId.IsValid(), IsFalse());
  2231. EXPECT_THAT(hoveredEntityIdAccentRemoved, IsTrue());
  2232. }
  2233. // mimics the mouse moving off of hovered entity onto a new entity with sticky select enabled
  2234. TEST(HandleAccents, CurrentValidEntityIdDoesNotBecomeHoveredWithSelectionAndStickySelect)
  2235. {
  2236. namespace azvi = AzToolsFramework::ViewportInteraction;
  2237. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2238. AZ::EntityId hoveredEntityEntityId = AZ::EntityId(54321);
  2239. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2240. handleAccentsContext.m_ctrlHeld = false;
  2241. handleAccentsContext.m_hasSelectedEntities = true;
  2242. handleAccentsContext.m_usingBoxSelect = false;
  2243. handleAccentsContext.m_usingStickySelect = true;
  2244. bool hoveredEntityIdAccentRemoved = false;
  2245. AzToolsFramework::HandleAccents(
  2246. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::None),
  2247. [&hoveredEntityIdAccentRemoved, hoveredEntityEntityId](const AZ::EntityId entityId, const bool accent)
  2248. {
  2249. if (entityId == hoveredEntityEntityId && !accent)
  2250. {
  2251. hoveredEntityIdAccentRemoved = true;
  2252. }
  2253. });
  2254. using ::testing::Eq;
  2255. using ::testing::IsFalse;
  2256. using ::testing::IsTrue;
  2257. EXPECT_THAT(hoveredEntityIdAccentRemoved, IsTrue());
  2258. EXPECT_THAT(hoveredEntityEntityId.IsValid(), IsFalse());
  2259. }
  2260. TEST(HandleAccents, CurrentValidEntityIdDoesBecomeHoveredWithSelectionAndStickySelectAndCtrl)
  2261. {
  2262. namespace azvi = AzToolsFramework::ViewportInteraction;
  2263. const AZ::EntityId currentEntityId = AZ::EntityId(12345);
  2264. AZ::EntityId hoveredEntityEntityId = AZ::EntityId(54321);
  2265. AzToolsFramework::HandleAccentsContext handleAccentsContext;
  2266. handleAccentsContext.m_ctrlHeld = true;
  2267. handleAccentsContext.m_hasSelectedEntities = true;
  2268. handleAccentsContext.m_usingBoxSelect = false;
  2269. handleAccentsContext.m_usingStickySelect = true;
  2270. bool currentEntityIdAccentAdded = false;
  2271. bool hoveredEntityIdAccentRemoved = false;
  2272. AzToolsFramework::HandleAccents(
  2273. currentEntityId, hoveredEntityEntityId, handleAccentsContext, azvi::MouseButtonsFromButton(azvi::MouseButton::None),
  2274. [&hoveredEntityIdAccentRemoved, &currentEntityIdAccentAdded, currentEntityId,
  2275. hoveredEntityEntityId](const AZ::EntityId entityId, const bool accent)
  2276. {
  2277. if (entityId == currentEntityId && accent)
  2278. {
  2279. currentEntityIdAccentAdded = true;
  2280. }
  2281. if (entityId == hoveredEntityEntityId && !accent)
  2282. {
  2283. hoveredEntityIdAccentRemoved = true;
  2284. }
  2285. });
  2286. using ::testing::Eq;
  2287. using ::testing::IsFalse;
  2288. using ::testing::IsTrue;
  2289. EXPECT_THAT(currentEntityIdAccentAdded, IsTrue());
  2290. EXPECT_THAT(hoveredEntityIdAccentRemoved, IsTrue());
  2291. EXPECT_THAT(hoveredEntityEntityId, Eq(AZ::EntityId(12345)));
  2292. }
  2293. class EditorTransformComponentSelectionRenderGeometryIntersectionFixture : public ToolsApplicationFixture<>
  2294. {
  2295. public:
  2296. void SetUpEditorFixtureImpl() override
  2297. {
  2298. auto* app = GetApplication();
  2299. // register a simple component implementing BoundsRequestBus and EditorComponentSelectionRequestsBus
  2300. app->RegisterComponentDescriptor(BoundsTestComponent::CreateDescriptor());
  2301. // register a component implementing RenderGeometry::IntersectionRequestBus
  2302. app->RegisterComponentDescriptor(RenderGeometryIntersectionTestComponent::CreateDescriptor());
  2303. auto createEntityWithGeometryIntersectionFn = [](const char* entityName)
  2304. {
  2305. AZ::Entity* entity = nullptr;
  2306. AZ::EntityId entityId = CreateDefaultEditorEntity(entityName, &entity);
  2307. entity->Deactivate();
  2308. entity->CreateComponent<RenderGeometryIntersectionTestComponent>();
  2309. entity->Activate();
  2310. return entityId;
  2311. };
  2312. m_entityIdGround = createEntityWithGeometryIntersectionFn("Entity1");
  2313. m_entityIdBox = createEntityWithGeometryIntersectionFn("Entity2");
  2314. if (auto* ground = AzToolsFramework::GetEntityById(m_entityIdGround)->FindComponent<RenderGeometryIntersectionTestComponent>())
  2315. {
  2316. ground->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-10.0f, -10.0f, -0.5f), AZ::Vector3(10.0f, 10.0f, 0.5f));
  2317. }
  2318. AzToolsFramework::SetWorldTransform(m_entityIdGround, AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 10.0f, 5.0f)));
  2319. if (auto* box = AzToolsFramework::GetEntityById(m_entityIdBox)->FindComponent<RenderGeometryIntersectionTestComponent>())
  2320. {
  2321. box->m_localBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(-0.5f), AZ::Vector3(0.5f));
  2322. }
  2323. AzToolsFramework::SetWorldTransform(
  2324. m_entityIdBox,
  2325. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2326. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(45.0f)), AZ::Vector3(0.0f, 10.0f, 7.0f)));
  2327. }
  2328. AZ::EntityId m_entityIdGround;
  2329. AZ::EntityId m_entityIdBox;
  2330. };
  2331. using EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture =
  2332. IndirectCallManipulatorViewportInteractionFixtureMixin<EditorTransformComponentSelectionRenderGeometryIntersectionFixture>;
  2333. TEST_F(
  2334. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture, BoxCanBePlacedOnMeshSurfaceUsingSurfaceManipulator)
  2335. {
  2336. // camera (go to position format) - 0.00, 20.00, 12.00, -35.00, -180.00
  2337. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2338. AzFramework::SetCameraTransform(
  2339. m_cameraState,
  2340. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2341. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-180.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-35.0f)),
  2342. AZ::Vector3(0.0f, 20.0f, 12.0f)));
  2343. // the initial starting position of the entity
  2344. const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2345. // where the entity should end up (snapped to the larger ground surface)
  2346. const auto finalTransformWorld =
  2347. AZ::Transform::CreateFromQuaternionAndTranslation(initialTransformWorld.GetRotation(), AZ::Vector3(2.5f, 12.5f, 5.5f));
  2348. // calculate the position in screen space of the initial position of the entity
  2349. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  2350. // calculate the position in screen space of the final position of the entity
  2351. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  2352. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  2353. AzToolsFramework::SelectEntity(m_entityIdBox);
  2354. // press and drag the mouse (starting where the surface manipulator is)
  2355. m_actionDispatcher->CameraState(m_cameraState)
  2356. ->MousePosition(initialPositionScreen)
  2357. ->MouseLButtonDown()
  2358. ->MousePosition(finalPositionScreen)
  2359. ->MouseLButtonUp();
  2360. // read back the position of the entity now
  2361. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2362. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2363. // Expected: translation: (X: 2.5, Y: 12.5, Z: 5.5) rotation: (X: 0, Y: 0, Z: 0.382683, W: 0.92388) scale: 1)
  2364. // Actual: translation: (X: 2.48677, Y: 12.4926, Z: 5.5) rotation: (X: 0, Y: 0, Z: 0.382683, W: 0.92388) scale: 1)
  2365. // Delta: 0.01323 0.0074 0.0
  2366. constexpr float finalTransformWorldTolerance = 0.014f; // Max (0.01323, 0.0074, 0.0)
  2367. #else
  2368. constexpr float finalTransformWorldTolerance = 0.01f;
  2369. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2370. // ensure final world positions match
  2371. EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, finalTransformWorldTolerance));
  2372. }
  2373. TEST_F(
  2374. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
  2375. SurfaceManipulatorFollowsMouseAtDefaultEditorDistanceFromCameraWhenNoMeshIntersection)
  2376. {
  2377. // camera (go to position format) - 0.00, 25.00, 12.00, 0.00, -180.00
  2378. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2379. AzFramework::SetCameraTransform(
  2380. m_cameraState,
  2381. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2382. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-180.0f)), AZ::Vector3(0.0f, 25.0f, 12.0f)));
  2383. // the initial starting position of the entity
  2384. const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2385. // where the entity should end up (default distance away from the camera/near clip under where the mouse is)
  2386. const auto finalTransformWorld =
  2387. AZ::Transform::CreateFromQuaternionAndTranslation(initialTransformWorld.GetRotation(), AZ::Vector3(0.0f, 14.9f, 12.0f));
  2388. // calculate the position in screen space of the initial position of the entity
  2389. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  2390. // calculate the position in screen space of the final position of the entity
  2391. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  2392. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  2393. AzToolsFramework::SelectEntity(m_entityIdBox);
  2394. // press and drag the mouse (starting where the surface manipulator is)
  2395. m_actionDispatcher->CameraState(m_cameraState)
  2396. ->MousePosition(initialPositionScreen)
  2397. ->MouseLButtonDown()
  2398. ->MousePosition(finalPositionScreen)
  2399. ->MouseLButtonUp();
  2400. // read back the position of the entity now
  2401. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2402. const auto viewportRay = AzToolsFramework::ViewportInteraction::ViewportScreenToWorldRay(m_cameraState, initialPositionScreen);
  2403. const auto distanceAway = (finalEntityTransform.GetTranslation() - viewportRay.m_origin).GetLength();
  2404. // ensure final world positions match
  2405. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2406. constexpr float finalTransformWorldTolerance = 0.028f;
  2407. #else
  2408. constexpr float finalTransformWorldTolerance = 0.01f;
  2409. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2410. EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, finalTransformWorldTolerance));
  2411. // ensure distance away is what we expect
  2412. EXPECT_NEAR(distanceAway, AzToolsFramework::GetDefaultEntityPlacementDistance(), 0.001f);
  2413. }
  2414. TEST_F(
  2415. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
  2416. MiddleMouseButtonWithShiftAndCtrlHeldOnMeshSurfaceWillSnapSelectedEntityToIntersectionPoint)
  2417. {
  2418. // camera (go to position format) - 21.00, 8.00, 11.00, -22.00, 150.00
  2419. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2420. AzFramework::SetCameraTransform(
  2421. m_cameraState,
  2422. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2423. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(150.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-22.0f)),
  2424. AZ::Vector3(21.0f, 8.0f, 11.0f)));
  2425. // position the ground entity
  2426. AzToolsFramework::SetWorldTransform(
  2427. m_entityIdGround,
  2428. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2429. AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(40.0f)) * AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(60.0f)),
  2430. AZ::Vector3(14.0f, -6.0f, 5.0f)));
  2431. // select the other entity (a 1x1x1 box)
  2432. AzToolsFramework::SelectEntity(m_entityIdBox);
  2433. // expected world position (value taken from editor scenario)
  2434. const auto expectedWorldPosition = AZ::Vector3(13.606657f, -2.6753534f, 5.9827675f);
  2435. const auto screenPosition = AzFramework::WorldToScreen(expectedWorldPosition, m_cameraState);
  2436. // perform snap action
  2437. m_actionDispatcher->CameraState(m_cameraState)
  2438. ->MousePosition(screenPosition)
  2439. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  2440. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift)
  2441. ->MouseMButtonDown();
  2442. // read back the current entity transform after placement
  2443. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2444. EXPECT_THAT(finalEntityTransform.GetTranslation(), IsCloseTolerance(expectedWorldPosition, 0.01f));
  2445. }
  2446. TEST_F(
  2447. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture, SurfaceManipulatorSelfIntersectsMeshWhenCtrlIsHeld)
  2448. {
  2449. // camera (go to position format) - 47.00, -52.00, 20.00, 0.00, -60.00
  2450. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2451. // position camera
  2452. AzFramework::SetCameraTransform(
  2453. m_cameraState,
  2454. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2455. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-60.0f)), AZ::Vector3(47.0f, -52.0f, 20.0f)));
  2456. // position box
  2457. AzToolsFramework::SetWorldTransform(m_entityIdBox, AZ::Transform::CreateTranslation(AZ::Vector3(50.0f, -50.0f, 20.0f)));
  2458. // the initial starting position of the entity
  2459. const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2460. // where the surface manipulator should end up (surface of the box)
  2461. const auto finalTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(49.5f, -49.6337357f, 19.5793953f));
  2462. // calculate the position in screen space of the initial position of the entity
  2463. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  2464. // calculate the position in screen space of the final position of the entity
  2465. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  2466. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  2467. AzToolsFramework::SelectEntity(m_entityIdBox);
  2468. // press and drag the mouse (starting where the surface manipulator is)
  2469. m_actionDispatcher->CameraState(m_cameraState)
  2470. ->MousePosition(initialPositionScreen)
  2471. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  2472. ->MouseLButtonDown()
  2473. ->MousePosition(finalPositionScreen)
  2474. ->MouseLButtonUp();
  2475. // read back the position of the entity now
  2476. const AZ::Transform finalManipulatorTransform = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  2477. // ensure final world positions match
  2478. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2479. // Expected: translation: (X: 49.5, Y: -49.6337, Z: 19.5794) rotation: (X: 0, Y: 0, Z: 0, W: 1) scale: 1)
  2480. // Actual: translation: (X: 49.1415, Y: -50, Z: 20) rotation: (X: 0, Y: 0, Z: 0, W: 1) scale: 1)
  2481. // Delta: 0.3585 Y: 0.3663 Z: 0.4206
  2482. constexpr float finalManipulatorTransformTolerance = 0.43f; // Max(0.3585, 0.3663, 0.4206)
  2483. #else
  2484. constexpr float finalManipulatorTransformTolerance = 0.01f;
  2485. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2486. EXPECT_THAT(finalManipulatorTransform, IsCloseTolerance(finalTransformWorld, finalManipulatorTransformTolerance));
  2487. }
  2488. } // namespace UnitTest