EditorTransformComponentSelectionTests.cpp 186 KB

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