EditorTransformComponentSelectionTests.cpp 188 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  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_defaultMainWindow, 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_defaultMainWindow, 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_defaultMainWindow, 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_defaultMainWindow, 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. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2715. // Expected: translation: (X: 2.5, Y: 12.5, Z: 5.5) rotation: (X: 0, Y: 0, Z: 0.382683, W: 0.92388) scale: 1)
  2716. // Actual: translation: (X: 2.48677, Y: 12.4926, Z: 5.5) rotation: (X: 0, Y: 0, Z: 0.382683, W: 0.92388) scale: 1)
  2717. // Delta: 0.01323 0.0074 0.0
  2718. constexpr float finalTransformWorldTolerance = 0.014f; // Max (0.01323, 0.0074, 0.0)
  2719. #else
  2720. constexpr float finalTransformWorldTolerance = 0.01f;
  2721. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2722. // ensure final world positions match
  2723. EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, finalTransformWorldTolerance));
  2724. }
  2725. TEST_F(
  2726. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
  2727. SurfaceManipulatorFollowsMouseAtDefaultEditorDistanceFromCameraWhenNoMeshIntersection)
  2728. {
  2729. // camera (go to position format) - 0.00, 25.00, 12.00, 0.00, -180.00
  2730. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2731. AzFramework::SetCameraTransform(
  2732. m_cameraState,
  2733. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2734. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-180.0f)), AZ::Vector3(0.0f, 25.0f, 12.0f)));
  2735. // the initial starting position of the entity
  2736. const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2737. // where the entity should end up (default distance away from the camera/near clip under where the mouse is)
  2738. const auto finalTransformWorld =
  2739. AZ::Transform::CreateFromQuaternionAndTranslation(initialTransformWorld.GetRotation(), AZ::Vector3(0.0f, 14.9f, 12.0f));
  2740. // calculate the position in screen space of the initial position of the entity
  2741. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  2742. // calculate the position in screen space of the final position of the entity
  2743. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  2744. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  2745. AzToolsFramework::SelectEntity(m_entityIdBox);
  2746. // press and drag the mouse (starting where the surface manipulator is)
  2747. m_actionDispatcher->CameraState(m_cameraState)
  2748. ->MousePosition(initialPositionScreen)
  2749. ->MouseLButtonDown()
  2750. ->MousePosition(finalPositionScreen)
  2751. ->MouseLButtonUp();
  2752. // read back the position of the entity now
  2753. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2754. const auto viewportRay = AzToolsFramework::ViewportInteraction::ViewportScreenToWorldRay(m_cameraState, initialPositionScreen);
  2755. const auto distanceAway = (finalEntityTransform.GetTranslation() - viewportRay.m_origin).GetLength();
  2756. // ensure final world positions match
  2757. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2758. constexpr float finalTransformWorldTolerance = 0.028f;
  2759. #else
  2760. constexpr float finalTransformWorldTolerance = 0.01f;
  2761. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2762. EXPECT_THAT(finalEntityTransform, IsCloseTolerance(finalTransformWorld, finalTransformWorldTolerance));
  2763. // ensure distance away is what we expect
  2764. EXPECT_NEAR(distanceAway, AzToolsFramework::GetDefaultEntityPlacementDistance(), 0.001f);
  2765. }
  2766. TEST_F(
  2767. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture,
  2768. MiddleMouseButtonWithShiftAndCtrlHeldOnMeshSurfaceWillSnapSelectedEntityToIntersectionPoint)
  2769. {
  2770. // camera (go to position format) - 21.00, 8.00, 11.00, -22.00, 150.00
  2771. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2772. AzFramework::SetCameraTransform(
  2773. m_cameraState,
  2774. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2775. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(150.0f)) * AZ::Matrix3x3::CreateRotationX(AZ::DegToRad(-22.0f)),
  2776. AZ::Vector3(21.0f, 8.0f, 11.0f)));
  2777. // position the ground entity
  2778. AzToolsFramework::SetWorldTransform(
  2779. m_entityIdGround,
  2780. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2781. AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(40.0f)) * AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(60.0f)),
  2782. AZ::Vector3(14.0f, -6.0f, 5.0f)));
  2783. // select the other entity (a 1x1x1 box)
  2784. AzToolsFramework::SelectEntity(m_entityIdBox);
  2785. // expected world position (value taken from editor scenario)
  2786. const auto expectedWorldPosition = AZ::Vector3(13.606657f, -2.6753534f, 5.9827675f);
  2787. const auto screenPosition = AzFramework::WorldToScreen(expectedWorldPosition, m_cameraState);
  2788. // perform snap action
  2789. m_actionDispatcher->CameraState(m_cameraState)
  2790. ->MousePosition(screenPosition)
  2791. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  2792. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Shift)
  2793. ->MouseMButtonDown();
  2794. // read back the current entity transform after placement
  2795. const AZ::Transform finalEntityTransform = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2796. EXPECT_THAT(finalEntityTransform.GetTranslation(), IsCloseTolerance(expectedWorldPosition, 0.01f));
  2797. }
  2798. TEST_F(
  2799. EditorTransformComponentSelectionRenderGeometryIntersectionManipulatorFixture, SurfaceManipulatorSelfIntersectsMeshWhenCtrlIsHeld)
  2800. {
  2801. // camera (go to position format) - 47.00, -52.00, 20.00, 0.00, -60.00
  2802. m_cameraState.m_viewportSize = AzFramework::ScreenSize(1280, 720);
  2803. // position camera
  2804. AzFramework::SetCameraTransform(
  2805. m_cameraState,
  2806. AZ::Transform::CreateFromMatrix3x3AndTranslation(
  2807. AZ::Matrix3x3::CreateRotationZ(AZ::DegToRad(-60.0f)), AZ::Vector3(47.0f, -52.0f, 20.0f)));
  2808. // position box
  2809. AzToolsFramework::SetWorldTransform(m_entityIdBox, AZ::Transform::CreateTranslation(AZ::Vector3(50.0f, -50.0f, 20.0f)));
  2810. // the initial starting position of the entity
  2811. const auto initialTransformWorld = AzToolsFramework::GetWorldTransform(m_entityIdBox);
  2812. // where the surface manipulator should end up (surface of the box)
  2813. const auto finalTransformWorld = AZ::Transform::CreateTranslation(AZ::Vector3(49.5f, -49.6337357f, 19.5793953f));
  2814. // calculate the position in screen space of the initial position of the entity
  2815. const auto initialPositionScreen = AzFramework::WorldToScreen(initialTransformWorld.GetTranslation(), m_cameraState);
  2816. // calculate the position in screen space of the final position of the entity
  2817. const auto finalPositionScreen = AzFramework::WorldToScreen(finalTransformWorld.GetTranslation(), m_cameraState);
  2818. // select the entity (this will cause the manipulators to appear in EditorTransformComponentSelection)
  2819. AzToolsFramework::SelectEntity(m_entityIdBox);
  2820. // press and drag the mouse (starting where the surface manipulator is)
  2821. m_actionDispatcher->CameraState(m_cameraState)
  2822. ->MousePosition(initialPositionScreen)
  2823. ->KeyboardModifierDown(AzToolsFramework::ViewportInteraction::KeyboardModifier::Control)
  2824. ->MouseLButtonDown()
  2825. ->MousePosition(finalPositionScreen)
  2826. ->MouseLButtonUp();
  2827. // read back the position of the entity now
  2828. const AZ::Transform finalManipulatorTransform = GetManipulatorTransform().value_or(AZ::Transform::CreateIdentity());
  2829. // ensure final world positions match
  2830. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2831. // Expected: translation: (X: 49.5, Y: -49.6337, Z: 19.5794) rotation: (X: 0, Y: 0, Z: 0, W: 1) scale: 1)
  2832. // Actual: translation: (X: 49.1415, Y: -50, Z: 20) rotation: (X: 0, Y: 0, Z: 0, W: 1) scale: 1)
  2833. // Delta: 0.3585 Y: 0.3663 Z: 0.4206
  2834. constexpr float finalManipulatorTransformTolerance = 0.43f; // Max(0.3585, 0.3663, 0.4206)
  2835. #else
  2836. constexpr float finalManipulatorTransformTolerance = 0.01f;
  2837. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  2838. EXPECT_THAT(finalManipulatorTransform, IsCloseTolerance(finalTransformWorld, finalManipulatorTransformTolerance));
  2839. }
  2840. } // namespace UnitTest