MainWindow.cpp 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740
  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 "MainWindow.h"
  9. // AZ
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/Utils.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. #include <AzCore/Component/TransformBus.h>
  14. #include <AzCore/std/smart_ptr/make_shared.h>
  15. #include <AzFramework/Terrain/TerrainDataRequestBus.h>
  16. #include <AzQtComponents/Buses/ShortcutDispatch.h>
  17. #include <AzToolsFramework/ActionManager/HotKey/HotKeyManagerInterface.h>
  18. #include <AzToolsFramework/API/ComponentEntityObjectBus.h>
  19. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  20. #include <AzToolsFramework/Editor/ActionManagerUtils.h>
  21. #include <AzToolsFramework/Entity/EditorEntityContextBus.h>
  22. #include <AzToolsFramework/Entity/EditorEntityInfoBus.h>
  23. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  24. #include <AzToolsFramework/Entity/ReadOnly/ReadOnlyEntityInterface.h>
  25. #include <AzToolsFramework/Prefab/PrefabFocusPublicInterface.h>
  26. #include <AzToolsFramework/Prefab/PrefabPublicInterface.h>
  27. #include <AzToolsFramework/PropertyTreeEditor/PropertyTreeEditor.h>
  28. #include <AzToolsFramework/ToolsComponents/EditorDisabledCompositionBus.h>
  29. #include <AzToolsFramework/ToolsComponents/EditorPendingCompositionBus.h>
  30. #include <AzToolsFramework/UI/ComponentPalette/ComponentPaletteUtil.hxx>
  31. #include <AzToolsFramework/UI/UICore/WidgetHelpers.h>
  32. #include <AzToolsFramework/Undo/UndoSystem.h>
  33. #include <IEditor.h>
  34. // Gradient Signal
  35. #include <GradientSignal/Ebuses/GradientPreviewContextRequestBus.h>
  36. #include <GradientSignal/Ebuses/ImageGradientRequestBus.h>
  37. #include <GradientSignal/Editor/EditorGradientImageCreatorRequestBus.h>
  38. // Qt
  39. #include <QApplication>
  40. #include <QMessageBox>
  41. #include <QStringList>
  42. #include <QTimer>
  43. #include <QVBoxLayout>
  44. // GraphCanvas
  45. #include <GraphCanvas/Components/Nodes/NodeBus.h>
  46. #include <GraphCanvas/Components/NodePropertyDisplay/NodePropertyDisplay.h>
  47. #include <GraphCanvas/Editor/EditorDockWidgetBus.h>
  48. #include <GraphCanvas/Widgets/EditorContextMenu/ContextMenus/SceneContextMenu.h>
  49. #include <GraphCanvas/Widgets/GraphCanvasEditor/GraphCanvasEditorCentralWidget.h>
  50. #include <GraphCanvas/Widgets/GraphCanvasEditor/GraphCanvasEditorDockWidget.h>
  51. // GraphModel
  52. #include <GraphModel/Integration/NodePalette/GraphCanvasNodePaletteItems.h>
  53. #include <GraphModel/Integration/NodePalette/StandardNodePaletteItem.h>
  54. #include <GraphModel/Integration/ReadOnlyDataInterface.h>
  55. #include <GraphModel/Model/Connection.h>
  56. #include <GraphModel/Model/Slot.h>
  57. // Landscape Canvas
  58. #include <Editor/Core/Core.h>
  59. #include <Editor/Core/GraphContext.h>
  60. #include <Editor/Menus/LayerExtenderContextMenu.h>
  61. #include <Editor/Menus/NodeContextMenu.h>
  62. #include <Editor/Menus/SceneContextMenuActions.h>
  63. #include <Editor/Nodes/Areas/AreaBlenderNode.h>
  64. #include <Editor/Nodes/Areas/BlockerAreaNode.h>
  65. #include <Editor/Nodes/Areas/MeshBlockerAreaNode.h>
  66. #include <Editor/Nodes/Areas/SpawnerAreaNode.h>
  67. #include <Editor/Nodes/AreaFilters/AltitudeFilterNode.h>
  68. #include <Editor/Nodes/AreaFilters/DistanceBetweenFilterNode.h>
  69. #include <Editor/Nodes/AreaFilters/DistributionFilterNode.h>
  70. #include <Editor/Nodes/AreaFilters/ShapeIntersectionFilterNode.h>
  71. #include <Editor/Nodes/AreaFilters/SlopeFilterNode.h>
  72. #include <Editor/Nodes/AreaFilters/SurfaceMaskDepthFilterNode.h>
  73. #include <Editor/Nodes/AreaFilters/SurfaceMaskFilterNode.h>
  74. #include <Editor/Nodes/AreaModifiers/PositionModifierNode.h>
  75. #include <Editor/Nodes/AreaModifiers/RotationModifierNode.h>
  76. #include <Editor/Nodes/AreaModifiers/ScaleModifierNode.h>
  77. #include <Editor/Nodes/AreaModifiers/SlopeAlignmentModifierNode.h>
  78. #include <Editor/Nodes/AreaSelectors/AssetWeightSelectorNode.h>
  79. #include <Editor/Nodes/Gradients/AltitudeGradientNode.h>
  80. #include <Editor/Nodes/Gradients/ConstantGradientNode.h>
  81. #include <Editor/Nodes/Gradients/FastNoiseGradientNode.h>
  82. #include <Editor/Nodes/Gradients/GradientBakerNode.h>
  83. #include <Editor/Nodes/Gradients/ImageGradientNode.h>
  84. #include <Editor/Nodes/Gradients/PerlinNoiseGradientNode.h>
  85. #include <Editor/Nodes/Gradients/RandomNoiseGradientNode.h>
  86. #include <Editor/Nodes/Gradients/ShapeAreaFalloffGradientNode.h>
  87. #include <Editor/Nodes/Gradients/SlopeGradientNode.h>
  88. #include <Editor/Nodes/Gradients/SurfaceMaskGradientNode.h>
  89. #include <Editor/Nodes/GradientModifiers/DitherGradientModifierNode.h>
  90. #include <Editor/Nodes/GradientModifiers/GradientMixerNode.h>
  91. #include <Editor/Nodes/GradientModifiers/InvertGradientModifierNode.h>
  92. #include <Editor/Nodes/GradientModifiers/LevelsGradientModifierNode.h>
  93. #include <Editor/Nodes/GradientModifiers/PosterizeGradientModifierNode.h>
  94. #include <Editor/Nodes/GradientModifiers/SmoothStepGradientModifierNode.h>
  95. #include <Editor/Nodes/GradientModifiers/ThresholdGradientModifierNode.h>
  96. #include <Editor/Nodes/Shapes/AxisAlignedBoxShapeNode.h>
  97. #include <Editor/Nodes/Shapes/BoxShapeNode.h>
  98. #include <Editor/Nodes/Shapes/CapsuleShapeNode.h>
  99. #include <Editor/Nodes/Shapes/CompoundShapeNode.h>
  100. #include <Editor/Nodes/Shapes/CylinderShapeNode.h>
  101. #include <Editor/Nodes/Shapes/DiskShapeNode.h>
  102. #include <Editor/Nodes/Shapes/PolygonPrismShapeNode.h>
  103. #include <Editor/Nodes/Shapes/ReferenceShapeNode.h>
  104. #include <Editor/Nodes/Shapes/SphereShapeNode.h>
  105. #include <Editor/Nodes/Shapes/TubeShapeNode.h>
  106. #include <Editor/Nodes/Terrain/PhysXHeightfieldColliderNode.h>
  107. #include <Editor/Nodes/Terrain/TerrainHeightGradientListNode.h>
  108. #include <Editor/Nodes/Terrain/TerrainLayerSpawnerNode.h>
  109. #include <Editor/Nodes/Terrain/TerrainMacroMaterialNode.h>
  110. #include <Editor/Nodes/Terrain/TerrainPhysicsHeightfieldColliderNode.h>
  111. #include <Editor/Nodes/Terrain/TerrainSurfaceGradientListNode.h>
  112. #include <Editor/Nodes/Terrain/TerrainSurfaceMaterialsListNode.h>
  113. #include <Editor/Nodes/UI/GradientPreviewThumbnailItem.h>
  114. #include <EditorLandscapeCanvasComponent.h>
  115. #include <LmbrCentral/Shape/ReferenceShapeComponentBus.h>
  116. namespace LandscapeCanvasEditor
  117. {
  118. static const int NODE_OFFSET_X_PIXELS = 350;
  119. static const int NODE_OFFSET_Y_PIXELS = 450;
  120. static constexpr int InvalidSlotIndex = -1;
  121. static const char* PreviewEntityElementName = "BoundsEntity";
  122. static const char* GradientIdElementName = "GradientId";
  123. static const char* GradientEntityIdElementName = "Gradient Entity";
  124. static const char* ShapeEntityIdElementName = "ShapeEntityId";
  125. static const char* InputBoundsEntityIdElementName = "InputBounds";
  126. static const char* EntityIdListElementName = "element";
  127. static IEditor* GetLegacyEditor()
  128. {
  129. IEditor* editor = nullptr;
  130. AzToolsFramework::EditorRequestBus::BroadcastResult(
  131. editor, &AzToolsFramework::EditorRequestBus::Events::GetEditor);
  132. return editor;
  133. }
  134. struct NodePoint
  135. {
  136. NodePoint* parent = nullptr;
  137. GraphModel::NodePtr node = nullptr;
  138. AZ::EntityId vegetationEntityId;
  139. AZStd::vector<NodePoint*> children;
  140. };
  141. NodePoint* FindNodePoint(const AZStd::vector<NodePoint*>& points, const AZStd::unordered_map<AZ::EntityId, GraphModel::NodePtrList>& nodeWrappings, GraphModel::NodePtr node)
  142. {
  143. for (auto it : points)
  144. {
  145. if (it->node == node)
  146. {
  147. return it;
  148. }
  149. // Wrapped nodes don't get their own NodePoint, so if we find a wrapper node
  150. // we need to check if any of its wrapped nodes match the node we are
  151. // looking for as well, since they will be in the same position as
  152. // their wrapper node parent
  153. else if (it->node->GetNodeType() == GraphModel::NodeType::WrapperNode)
  154. {
  155. const AZ::EntityId& entityId = it->vegetationEntityId;
  156. auto nodeWrapIt = nodeWrappings.find(entityId);
  157. if (nodeWrapIt != nodeWrappings.end())
  158. {
  159. const auto& wrappedNodes = nodeWrapIt->second;
  160. for (auto wrappedNode : wrappedNodes)
  161. {
  162. if (wrappedNode == node)
  163. {
  164. return it;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. return nullptr;
  171. }
  172. AZ::Vector2 PlaceNodes(const AZ::EntityId& sceneId, NodePoint* point, AZ::Vector2 offset)
  173. {
  174. if (!point)
  175. {
  176. return offset;
  177. }
  178. if (point->node)
  179. {
  180. GraphModelIntegration::GraphControllerRequestBus::Event(sceneId, &GraphModelIntegration::GraphControllerRequests::AddNode, point->node, offset);
  181. offset.SetX(offset.GetX() + NODE_OFFSET_X_PIXELS);
  182. }
  183. size_t numChildren = point->children.size();
  184. if (numChildren)
  185. {
  186. for (int i = 0; i < numChildren; ++i)
  187. {
  188. // Update the y-coordinate of our offset from any nodes placed by our child so that
  189. // any subsequent nodes will be placed below them
  190. AZ::Vector2 childOffset = PlaceNodes(sceneId, point->children[i], offset);
  191. offset.SetY(childOffset.GetY());
  192. // Start a new "row" if this node has any more children that need room
  193. if (i < numChildren - 1)
  194. {
  195. offset.SetY(offset.GetY() + NODE_OFFSET_Y_PIXELS);
  196. }
  197. }
  198. }
  199. return offset;
  200. }
  201. AZ::TypeId PickComponentTypeIdToAdd(const AzToolsFramework::ComponentPaletteUtil::ComponentDataTable& componentDataTable)
  202. {
  203. using namespace AzToolsFramework;
  204. // A map of category names with preferred component names.
  205. // There may be multiple component names for a category, as long as they provide different services.
  206. const AZStd::map<QString, AZStd::vector<QString>> preferredComponentsByCategory = { { "Shape", { "Shape Reference" } } };
  207. // Scan through the preferred categories to see whether any exist in the componentDataTable.
  208. for (const auto& preferredComponentPair : preferredComponentsByCategory)
  209. {
  210. auto candidateDataTablePair = componentDataTable.find(preferredComponentPair.first);
  211. if (candidateDataTablePair != componentDataTable.end())
  212. {
  213. // Now check all the preferred components for that category, and return the first one that exists in the candidate componentDataTable.
  214. for (const auto& preferredComponentName : preferredComponentPair.second)
  215. {
  216. const auto& candidateComponent = candidateDataTablePair->second.find(preferredComponentName);
  217. if (candidateComponent != candidateDataTablePair->second.end())
  218. {
  219. return candidateComponent->second->m_typeId;
  220. }
  221. }
  222. }
  223. }
  224. // There are a couple of cases where we prefer certain categories of Components
  225. // to be added over others,
  226. // so if those there are components in those categories, then choose them first.
  227. // Otherwise, just pick the first one in the list.
  228. static const QStringList preferredCategories = { "Vegetation", "Graphics/Mesh" };
  229. ComponentPaletteUtil::ComponentDataTable::const_iterator categoryIt;
  230. for (const auto& categoryName : preferredCategories)
  231. {
  232. categoryIt = componentDataTable.find(categoryName);
  233. if (categoryIt != componentDataTable.end())
  234. {
  235. break;
  236. }
  237. }
  238. if (categoryIt == componentDataTable.end())
  239. {
  240. categoryIt = componentDataTable.begin();
  241. }
  242. AZ_Assert(categoryIt->second.size(), "No components found that satisfy the missing required service(s).");
  243. const auto& componentPair = categoryIt->second.begin();
  244. return componentPair->second->m_typeId;
  245. }
  246. CustomEntityPropertyEditor::CustomEntityPropertyEditor(QWidget* parent)
  247. : AzToolsFramework::EntityPropertyEditor(parent)
  248. {
  249. }
  250. void CustomEntityPropertyEditor::CloseInspectorWindow()
  251. {
  252. // Override this to be empty, since our custom instance of this pinned inspector
  253. // doesn't need to be closed when the context resets
  254. }
  255. QString CustomEntityPropertyEditor::GetEntityDetailsLabelText() const
  256. {
  257. return QObject::tr("Select a node to show its properties in the inspector.");
  258. }
  259. CustomNodeInspectorDockWidget::CustomNodeInspectorDockWidget(QWidget* parent)
  260. : AzQtComponents::StyledDockWidget(parent)
  261. {
  262. QVBoxLayout* layout = new QVBoxLayout();
  263. // Our custom Node Inspector is just a Pinned Inspector that by default is
  264. // pointed to an invalid EntityId, so it won't follow the Editor selection
  265. m_propertyEditor = new CustomEntityPropertyEditor(this);
  266. m_propertyEditor->SetOverrideEntityIds({ AZ::EntityId() });
  267. layout->addWidget(m_propertyEditor);
  268. QWidget* host = new QWidget(this);
  269. host->setLayout(layout);
  270. setWidget(host);
  271. setObjectName("TempNodeInspector");
  272. setWindowTitle(QObject::tr("Node Inspector"));
  273. }
  274. CustomEntityPropertyEditor* CustomNodeInspectorDockWidget::GetEntityPropertyEditor()
  275. {
  276. return m_propertyEditor;
  277. }
  278. #define REGISTER_NODE_PALETTE_ITEM(category, TYPE, editorId) \
  279. category->CreateChildNode<GraphModelIntegration::StandardNodePaletteItem<TYPE>>(TYPE::TITLE, editorId);
  280. GraphCanvas::GraphCanvasTreeItem* LandscapeCanvasConfig::CreateNodePaletteRoot()
  281. {
  282. using namespace LandscapeCanvas;
  283. const GraphCanvas::EditorId& editorId = LANDSCAPE_CANVAS_EDITOR_ID;
  284. GraphCanvas::NodePaletteTreeItem* rootItem = aznew GraphCanvas::NodePaletteTreeItem("Root", editorId);
  285. // Don't give the Vegetation options if the gem isn't present.
  286. bool vegetationGemIsPresent = AzToolsFramework::IsComponentWithServiceRegistered(AZ_CRC_CE("VegetationSystemService"));
  287. if (vegetationGemIsPresent)
  288. {
  289. GraphCanvas::IconDecoratedNodePaletteTreeItem* areaCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Vegetation Areas", editorId);
  290. areaCategory->SetTitlePalette("VegetationAreaNodeTitlePalette");
  291. REGISTER_NODE_PALETTE_ITEM(areaCategory, AreaBlenderNode, editorId);
  292. REGISTER_NODE_PALETTE_ITEM(areaCategory, BlockerAreaNode, editorId);
  293. REGISTER_NODE_PALETTE_ITEM(areaCategory, MeshBlockerAreaNode, editorId);
  294. REGISTER_NODE_PALETTE_ITEM(areaCategory, SpawnerAreaNode, editorId);
  295. }
  296. // Gradients
  297. GraphCanvas::IconDecoratedNodePaletteTreeItem* gradientCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Gradients", editorId);
  298. gradientCategory->SetTitlePalette("GradientNodeTitlePalette");
  299. REGISTER_NODE_PALETTE_ITEM(gradientCategory, AltitudeGradientNode, editorId);
  300. REGISTER_NODE_PALETTE_ITEM(gradientCategory, ConstantGradientNode, editorId);
  301. REGISTER_NODE_PALETTE_ITEM(gradientCategory, GradientBakerNode, editorId);
  302. REGISTER_NODE_PALETTE_ITEM(gradientCategory, ImageGradientNode, editorId);
  303. REGISTER_NODE_PALETTE_ITEM(gradientCategory, PerlinNoiseGradientNode, editorId);
  304. REGISTER_NODE_PALETTE_ITEM(gradientCategory, RandomNoiseGradientNode, editorId);
  305. REGISTER_NODE_PALETTE_ITEM(gradientCategory, ShapeAreaFalloffGradientNode, editorId);
  306. REGISTER_NODE_PALETTE_ITEM(gradientCategory, SlopeGradientNode, editorId);
  307. REGISTER_NODE_PALETTE_ITEM(gradientCategory, SurfaceMaskGradientNode, editorId);
  308. // Don't give the option for the Fast Noise Gradient if the gem isn't present.
  309. bool fastNoiseGemIsPresent = AzToolsFramework::IsComponentWithServiceRegistered(AZ_CRC_CE("FastNoiseService"));
  310. if (fastNoiseGemIsPresent)
  311. {
  312. REGISTER_NODE_PALETTE_ITEM(gradientCategory, FastNoiseGradientNode, editorId);
  313. }
  314. // Gradient Modifiers
  315. GraphCanvas::IconDecoratedNodePaletteTreeItem* gradientModifierCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Gradient Modifiers", editorId);
  316. gradientModifierCategory->SetTitlePalette("GradientModifierNodeTitlePalette");
  317. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, DitherGradientModifierNode, editorId);
  318. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, GradientMixerNode, editorId);
  319. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, InvertGradientModifierNode, editorId);
  320. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, LevelsGradientModifierNode, editorId);
  321. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, PosterizeGradientModifierNode, editorId);
  322. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, SmoothStepGradientModifierNode, editorId);
  323. REGISTER_NODE_PALETTE_ITEM(gradientModifierCategory, ThresholdGradientModifierNode, editorId);
  324. // Shapes
  325. GraphCanvas::IconDecoratedNodePaletteTreeItem* shapeCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Shapes", editorId);
  326. shapeCategory->SetTitlePalette("ShapeNodeTitlePalette");
  327. REGISTER_NODE_PALETTE_ITEM(shapeCategory, AxisAlignedBoxShapeNode, editorId);
  328. REGISTER_NODE_PALETTE_ITEM(shapeCategory, BoxShapeNode, editorId);
  329. REGISTER_NODE_PALETTE_ITEM(shapeCategory, CapsuleShapeNode, editorId);
  330. REGISTER_NODE_PALETTE_ITEM(shapeCategory, CompoundShapeNode, editorId);
  331. REGISTER_NODE_PALETTE_ITEM(shapeCategory, CylinderShapeNode, editorId);
  332. REGISTER_NODE_PALETTE_ITEM(shapeCategory, DiskShapeNode, editorId);
  333. REGISTER_NODE_PALETTE_ITEM(shapeCategory, PolygonPrismShapeNode, editorId);
  334. REGISTER_NODE_PALETTE_ITEM(shapeCategory, ReferenceShapeNode, editorId);
  335. REGISTER_NODE_PALETTE_ITEM(shapeCategory, SphereShapeNode, editorId);
  336. REGISTER_NODE_PALETTE_ITEM(shapeCategory, TubeShapeNode, editorId);
  337. // Don't give the Terrain options if the gem isn't present.
  338. bool terrainGemIsPresent = AzToolsFramework::IsComponentWithServiceRegistered(AZ_CRC_CE("TerrainService"));
  339. if (terrainGemIsPresent)
  340. {
  341. GraphCanvas::IconDecoratedNodePaletteTreeItem* terrainCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Terrain", editorId);
  342. terrainCategory->SetTitlePalette("TerrainNodeTitlePalette");
  343. REGISTER_NODE_PALETTE_ITEM(terrainCategory, TerrainLayerSpawnerNode, editorId);
  344. REGISTER_NODE_PALETTE_ITEM(terrainCategory, TerrainMacroMaterialNode, editorId);
  345. REGISTER_NODE_PALETTE_ITEM(terrainCategory, TerrainSurfaceMaterialsListNode, editorId);
  346. }
  347. GraphModelIntegration::AddCommonNodePaletteUtilities(rootItem, editorId);
  348. return rootItem;
  349. }
  350. #undef REGISTER_NODE_PALETTE_ITEM
  351. // Don't register nodes whose corresponding component already exists on the given Entity so that we
  352. // can prevent the user from adding extender nodes that would leave components in an incompatible state
  353. #define REGISTER_NODE_PALETTE_ITEM_UNIQUE(category, TYPE, editorId, entityId) \
  354. { \
  355. AZ::TypeId componentTypeId; \
  356. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(componentTypeId, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::GetComponentTypeId, azrtti_typeid<TYPE>()); \
  357. if (!AzToolsFramework::EntityHasComponentOfType(entityId, componentTypeId)) \
  358. { \
  359. category->CreateChildNode<GraphModelIntegration::StandardNodePaletteItem<TYPE>>(TYPE::TITLE, editorId); \
  360. } \
  361. } \
  362. GraphCanvas::GraphCanvasTreeItem* GetAreaExtendersNodePaletteRoot(const GraphCanvas::EditorId& editorId, AZ::EntityId entityId)
  363. {
  364. using namespace LandscapeCanvas;
  365. GraphCanvas::NodePaletteTreeItem* rootItem = aznew GraphCanvas::NodePaletteTreeItem("Root", editorId);
  366. // Filters
  367. GraphCanvas::IconDecoratedNodePaletteTreeItem* filtersCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Filters", editorId);
  368. filtersCategory->SetTitlePalette("VegetationAreaNodeTitlePalette");
  369. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, AltitudeFilterNode, editorId, entityId);
  370. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, DistanceBetweenFilterNode, editorId, entityId);
  371. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, DistributionFilterNode, editorId, entityId);
  372. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, ShapeIntersectionFilterNode, editorId, entityId);
  373. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, SlopeFilterNode, editorId, entityId);
  374. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, SurfaceMaskDepthFilterNode, editorId, entityId);
  375. REGISTER_NODE_PALETTE_ITEM_UNIQUE(filtersCategory, SurfaceMaskFilterNode, editorId, entityId);
  376. // Modifiers
  377. GraphCanvas::IconDecoratedNodePaletteTreeItem* modifiersCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Modifiers", editorId);
  378. modifiersCategory->SetTitlePalette("VegetationAreaNodeTitlePalette");
  379. REGISTER_NODE_PALETTE_ITEM_UNIQUE(modifiersCategory, PositionModifierNode, editorId, entityId);
  380. REGISTER_NODE_PALETTE_ITEM_UNIQUE(modifiersCategory, RotationModifierNode, editorId, entityId);
  381. REGISTER_NODE_PALETTE_ITEM_UNIQUE(modifiersCategory, ScaleModifierNode, editorId, entityId);
  382. REGISTER_NODE_PALETTE_ITEM_UNIQUE(modifiersCategory, SlopeAlignmentModifierNode, editorId, entityId);
  383. // Selectors
  384. GraphCanvas::IconDecoratedNodePaletteTreeItem* selectorsCategory = rootItem->CreateChildNode<GraphCanvas::IconDecoratedNodePaletteTreeItem>("Selectors", editorId);
  385. selectorsCategory->SetTitlePalette("VegetationAreaNodeTitlePalette");
  386. REGISTER_NODE_PALETTE_ITEM_UNIQUE(selectorsCategory, AssetWeightSelectorNode, editorId, entityId);
  387. // Remove any category entries that wind up with no sub-items
  388. for (GraphCanvas::NodePaletteTreeItem* category : { filtersCategory, modifiersCategory, selectorsCategory })
  389. {
  390. if (category && category->GetChildCount() <= 0)
  391. {
  392. category->DetachItem();
  393. }
  394. }
  395. return rootItem;
  396. }
  397. GraphCanvas::GraphCanvasTreeItem* GetTerrainExtendersNodePaletteRoot(const GraphCanvas::EditorId& editorId, AZ::EntityId entityId)
  398. {
  399. using namespace LandscapeCanvas;
  400. GraphCanvas::NodePaletteTreeItem* rootItem = aznew GraphCanvas::NodePaletteTreeItem("Root", editorId);
  401. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, PhysXHeightfieldColliderNode, editorId, entityId);
  402. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, TerrainHeightGradientListNode, editorId, entityId);
  403. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, TerrainMacroMaterialNode, editorId, entityId);
  404. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, TerrainPhysicsHeightfieldColliderNode, editorId, entityId);
  405. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, TerrainSurfaceGradientListNode, editorId, entityId);
  406. REGISTER_NODE_PALETTE_ITEM_UNIQUE(rootItem, TerrainSurfaceMaterialsListNode, editorId, entityId);
  407. return rootItem;
  408. }
  409. #undef REGISTER_NODE_PALETTE_ITEM_UNIQUE
  410. LandscapeCanvasConfig* GetDefaultConfig()
  411. {
  412. LandscapeCanvasConfig* config = new LandscapeCanvasConfig();
  413. config->m_editorId = LandscapeCanvas::LANDSCAPE_CANVAS_EDITOR_ID;
  414. config->m_baseStyleSheet = "LandscapeCanvas/StyleSheet/graphcanvas_style.json";
  415. config->m_mimeType = LandscapeCanvas::MIME_EVENT_TYPE;
  416. config->m_saveIdentifier = LandscapeCanvas::SAVE_IDENTIFIER;
  417. return config;
  418. }
  419. AzFramework::EntityContextId MainWindow::s_editorEntityContextId = AzFramework::EntityContextId::CreateNull();
  420. MainWindow::MainWindow(QWidget* parent)
  421. : GraphModelIntegration::EditorMainWindow(GetDefaultConfig(), parent)
  422. {
  423. AZ::ComponentApplicationBus::BroadcastResult(m_serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  424. AZ_Assert(m_serializeContext, "Failed to acquire application serialize context.");
  425. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  426. s_editorEntityContextId, &AzToolsFramework::EditorEntityContextRequests::GetEditorEntityContextId);
  427. m_prefabFocusPublicInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabFocusPublicInterface>::Get();
  428. AZ_Assert(m_prefabFocusPublicInterface, "LandscapeCanvas - could not get PrefabFocusPublicInterface on construction.");
  429. m_prefabPublicInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabPublicInterface>::Get();
  430. AZ_Assert(m_prefabPublicInterface, "LandscapeCanvas - could not get PrefabPublicInterface on construction.");
  431. m_readOnlyEntityPublicInterface = AZ::Interface<AzToolsFramework::ReadOnlyEntityPublicInterface>::Get();
  432. AZ_Assert(m_readOnlyEntityPublicInterface, "LandscapeCanvas - could not get ReadOnlyEntityPublicInterface on construction.");
  433. const GraphCanvas::EditorId& editorId = GetEditorId();
  434. // Register unique color palettes for our connections (data types)
  435. GraphCanvas::StyleManagerRequestBus::Event(editorId, &GraphCanvas::StyleManagerRequests::RegisterDataPaletteStyle, LandscapeCanvas::BoundsTypeId, "BoundsDataColorPalette");
  436. GraphCanvas::StyleManagerRequestBus::Event(editorId, &GraphCanvas::StyleManagerRequests::RegisterDataPaletteStyle, LandscapeCanvas::GradientTypeId, "GradientDataColorPalette");
  437. GraphCanvas::StyleManagerRequestBus::Event(editorId, &GraphCanvas::StyleManagerRequests::RegisterDataPaletteStyle, LandscapeCanvas::AreaTypeId, "VegetationAreaDataColorPalette");
  438. GraphCanvas::StyleManagerRequestBus::Event(editorId, &GraphCanvas::StyleManagerRequests::RegisterDataPaletteStyle, LandscapeCanvas::PathTypeId, "PathDataColorPalette");
  439. LandscapeCanvas::LandscapeCanvasRequestBus::Handler::BusConnect();
  440. AzToolsFramework::EditorPickModeNotificationBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
  441. AzToolsFramework::EntityCompositionNotificationBus::Handler::BusConnect();
  442. AzToolsFramework::ToolsApplicationNotificationBus::Handler::BusConnect();
  443. AzToolsFramework::Prefab::PrefabFocusNotificationBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
  444. AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler::BusConnect();
  445. CrySystemEventBus::Handler::BusConnect();
  446. AZ::EntitySystemBus::Handler::BusConnect();
  447. // Listen for Entity notifications if a level is already loaded
  448. // Otherwise, we will connect/disconnect from this bus when levels are loaded/closed
  449. if (GetLegacyEditor()->IsLevelLoaded())
  450. {
  451. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  452. }
  453. // Create our temporary Node Inspector using a Pinned Inspector
  454. m_customNodeInspector = aznew CustomNodeInspectorDockWidget(this);
  455. // Add our custom action to the scene context menu
  456. m_sceneContextMenu->AddMenuAction(aznew FindSelectedNodesAction(this));
  457. UpdateGraphEnabled();
  458. static constexpr AZStd::string_view LandscapeCanvasActionContextIdentifier = "o3de.context.editor.landscapecanvas";
  459. if(auto hotKeyManagerInterface = AZ::Interface<AzToolsFramework::HotKeyManagerInterface>::Get())
  460. {
  461. hotKeyManagerInterface->AssignWidgetToActionContext(LandscapeCanvasActionContextIdentifier, this);
  462. }
  463. }
  464. MainWindow::~MainWindow()
  465. {
  466. AZ::EntitySystemBus::Handler::BusDisconnect();
  467. CrySystemEventBus::Handler::BusDisconnect();
  468. AzToolsFramework::Prefab::PrefabPublicNotificationBus::Handler::BusDisconnect();
  469. AzToolsFramework::Prefab::PrefabFocusNotificationBus::Handler::BusDisconnect();
  470. AzToolsFramework::ToolsApplicationNotificationBus::Handler::BusDisconnect();
  471. AzToolsFramework::EditorPickModeNotificationBus::Handler::BusDisconnect();
  472. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  473. AzToolsFramework::EntityCompositionNotificationBus::Handler::BusConnect();
  474. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::MultiHandler::BusDisconnect();
  475. LandscapeCanvas::LandscapeCanvasRequestBus::Handler::BusDisconnect();
  476. }
  477. GraphModel::GraphContextPtr MainWindow::GetGraphContext() const
  478. {
  479. return LandscapeCanvas::GraphContext::GetInstance();
  480. }
  481. void MainWindow::OnGraphModelNodeAdded(GraphModel::NodePtr node)
  482. {
  483. // If we weren't graphing a scene, then this new node was dragged in from the Node Palette,
  484. // so we need to create the appropriate underlying Entity/Component(s)
  485. if (!m_ignoreGraphUpdates)
  486. {
  487. HandleNodeCreated(node);
  488. }
  489. // Handle any custom logic when a node is added to the graph (e.g. adding thumbnails)
  490. HandleNodeAdded(node);
  491. }
  492. void MainWindow::OnGraphModelNodeRemoved(GraphModel::NodePtr node)
  493. {
  494. // Remove the cached EntityId mapping for this node
  495. GraphCanvas::GraphId graphId = (*GraphModelIntegration::GraphControllerNotificationBus::GetCurrentBusId());
  496. auto nodeMap = GetEntityIdNodeMap(graphId, node);
  497. if (nodeMap)
  498. {
  499. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  500. const AZ::EntityId& entityId = baseNodePtr->GetVegetationEntityId();
  501. auto it = nodeMap->find(entityId);
  502. if (it != nodeMap->end())
  503. {
  504. nodeMap->erase(it);
  505. }
  506. }
  507. if (m_ignoreGraphUpdates)
  508. {
  509. return;
  510. }
  511. // Check if the deleted node was a wrapped node
  512. bool isNodeWrapped = false;
  513. auto it = AZStd::find(m_deletedWrappedNodes.begin(), m_deletedWrappedNodes.end(), node);
  514. if (it != m_deletedWrappedNodes.end())
  515. {
  516. isNodeWrapped = true;
  517. m_deletedWrappedNodes.erase(it);
  518. }
  519. // If a wrapped node is removed, then only delete the underlying component.
  520. // Otherwise, delete the whole underlying Entity when the node is removed.
  521. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  522. if (isNodeWrapped)
  523. {
  524. AZ::Component* component = baseNodePtr->GetComponent();
  525. if (component)
  526. {
  527. m_ignoreGraphUpdates = true;
  528. AzToolsFramework::RemoveComponents({ component });
  529. m_ignoreGraphUpdates = false;
  530. }
  531. }
  532. else
  533. {
  534. // Don't use the m_ignoreGraphUpdates guard here because we want descendant Entities that get deleted
  535. // to remove their corresponding nodes from the graph as well to stay in sync
  536. const AZ::EntityId& entityId = baseNodePtr->GetVegetationEntityId();
  537. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(&AzToolsFramework::ToolsApplicationRequests::DeleteEntityAndAllDescendants, entityId);
  538. }
  539. }
  540. void MainWindow::PreOnGraphModelNodeRemoved(GraphModel::NodePtr node)
  541. {
  542. GraphCanvas::GraphId graphId = (*GraphModelIntegration::GraphControllerNotificationBus::GetCurrentBusId());
  543. // We need to track any wrapped nodes before the actually get deleted so we can handle
  544. // their deletion properly, because once the OnGraphModelNodeRemoved is called
  545. // the wrapped information is lost.
  546. bool isNodeWrapped = false;
  547. GraphModelIntegration::GraphControllerRequestBus::EventResult(isNodeWrapped, graphId, &GraphModelIntegration::GraphControllerRequests::IsNodeWrapped, node);
  548. if (isNodeWrapped)
  549. {
  550. m_deletedWrappedNodes.push_back(node);
  551. }
  552. // Before a node gets removed from the graph, save off its position
  553. // so that we can restore it to its previous spot if it ends up
  554. // being added back via Undo
  555. auto it = m_deletedNodePositions.find(graphId);
  556. if (it != m_deletedNodePositions.end())
  557. {
  558. DeletedNodePositionsMap& deletedNodePositionMap = it->second;
  559. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  560. AZ::EntityComponentIdPair pair(baseNodePtr->GetVegetationEntityId(), baseNodePtr->GetComponentId());
  561. AZ::Vector2 position;
  562. GraphModelIntegration::GraphControllerRequestBus::EventResult(position, graphId, &GraphModelIntegration::GraphControllerRequests::GetPosition, node);
  563. deletedNodePositionMap[pair] = position;
  564. }
  565. }
  566. void MainWindow::OnGraphModelConnectionAdded(GraphModel::ConnectionPtr connection)
  567. {
  568. // Don't need to act on connections that aren't added by the user
  569. if (m_ignoreGraphUpdates)
  570. {
  571. return;
  572. }
  573. UpdateConnectionData(connection, true /* added */);
  574. }
  575. void MainWindow::OnGraphModelConnectionRemoved(GraphModel::ConnectionPtr connection)
  576. {
  577. // Don't need to act on connections that aren't removed by the user
  578. if (m_ignoreGraphUpdates)
  579. {
  580. return;
  581. }
  582. UpdateConnectionData(connection, false /* added */);
  583. }
  584. void MainWindow::PreOnGraphModelNodeWrapped(GraphModel::NodePtr wrapperNode, GraphModel::NodePtr node)
  585. {
  586. if (m_ignoreGraphUpdates)
  587. {
  588. return;
  589. }
  590. // Keep track when wrapped nodes are about to be added so we can prevent the logic that
  591. // creates new entities when nodes are added
  592. m_addedWrappedNodes.push_back(node);
  593. }
  594. void MainWindow::OnGraphModelNodeWrapped(GraphModel::NodePtr wrapperNode, GraphModel::NodePtr node)
  595. {
  596. // We only need to add components when nodes are created by the user,
  597. // not when we are parsing/graphing an existing setup
  598. if (m_ignoreGraphUpdates)
  599. {
  600. return;
  601. }
  602. auto it = AZStd::find(m_addedWrappedNodes.begin(), m_addedWrappedNodes.end(), node);
  603. if (it != m_addedWrappedNodes.end())
  604. {
  605. m_addedWrappedNodes.erase(it);
  606. }
  607. // We don't need to create a new component for nodes that already
  608. // have a component tied to them, which happens when nodes get deserialized
  609. // and OnNodeWrapped gets invoked
  610. auto wrappedNode = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  611. if (wrappedNode->GetComponentId() != AZ::InvalidComponentId)
  612. {
  613. return;
  614. }
  615. // When a node is wrapped (e.g. filter/modifier added to a layer area), then we will
  616. // add the Component to the Entity of the wrapper node
  617. auto* sourceNode = static_cast<LandscapeCanvas::BaseNode*>(wrapperNode.get());
  618. AZ::EntityId vegetationEntityId = sourceNode->GetVegetationEntityId();
  619. m_ignoreGraphUpdates = true;
  620. AddComponentForNode(node, vegetationEntityId);
  621. m_ignoreGraphUpdates = false;
  622. }
  623. void MainWindow::OnSelectionChanged()
  624. {
  625. GraphCanvas::AssetEditorMainWindow::OnSelectionChanged();
  626. if (m_ignoreGraphUpdates)
  627. {
  628. return;
  629. }
  630. GraphModel::NodePtrList nodeList;
  631. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodeList, GetActiveGraphCanvasGraphId(), &GraphModelIntegration::GraphControllerRequests::GetSelectedNodes);
  632. // Iterate through the selected nodes to find their corresponding vegetation entities
  633. AzToolsFramework::EntityIdSet vegetationEntityIdsToSelect;
  634. for (const auto& node : nodeList)
  635. {
  636. if (!node)
  637. {
  638. continue;
  639. }
  640. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  641. vegetationEntityIdsToSelect.insert(baseNodePtr->GetVegetationEntityId());
  642. }
  643. // If we don't have any nodes selected, or the entities selected in the graph aren't nodes (e.g. comments, node groups)
  644. // then show an empty Node Inspector
  645. if (vegetationEntityIdsToSelect.empty())
  646. {
  647. m_customNodeInspector->GetEntityPropertyEditor()->SetOverrideEntityIds({ AZ::EntityId() });
  648. return;
  649. }
  650. QTimer::singleShot(0, [this, vegetationEntityIdsToSelect]() {
  651. // If we are in object pick mode and have selected a single node, then use the Entity for that node
  652. // as the pick mode selection
  653. if (m_inObjectPickMode && vegetationEntityIdsToSelect.size() == 1)
  654. {
  655. AZ::EntityId selectedEntityId = *vegetationEntityIdsToSelect.begin();
  656. AzToolsFramework::EditorPickModeRequestBus::Broadcast(&AzToolsFramework::EditorPickModeRequests::PickModeSelectEntity, selectedEntityId);
  657. AzToolsFramework::EditorPickModeRequestBus::Broadcast(&AzToolsFramework::EditorPickModeRequests::StopEntityPickMode);
  658. }
  659. // Otherwise, update the selection in our node inspector
  660. else
  661. {
  662. m_customNodeInspector->GetEntityPropertyEditor()->SetOverrideEntityIds(vegetationEntityIdsToSelect);
  663. }
  664. });
  665. }
  666. void MainWindow::OnEntitiesDeserialized(const GraphCanvas::GraphSerialization& serializationTarget)
  667. {
  668. using namespace AzToolsFramework;
  669. using namespace LandscapeCanvas;
  670. m_ignoreGraphUpdates = true;
  671. GraphCanvas::GraphId graphId = GetActiveGraphCanvasGraphId();
  672. LandscapeCanvasSerialization serialization;
  673. LandscapeCanvasSerializationRequestBus::BroadcastResult(serialization, &LandscapeCanvasSerializationRequests::GetSerializedMappings);
  674. EntityIdList entitiesToDuplicate;
  675. // Look for any nodes being serialized for which we want to duplicate the Entity
  676. // corresponding to our Landscape Canvas node
  677. for (AZ::Entity* nodeEntity : serializationTarget.GetGraphData().m_nodes)
  678. {
  679. GraphCanvas::NodeId nodeUiId = nodeEntity->GetId();
  680. // Ignore any nodes serialized by GraphCanvas that aren't GraphModel nodes (e.g. comments/node groups), since they
  681. // don't have an actual Entity/Component tied to them that we'll need to duplicate
  682. GraphModel::NodePtr node;
  683. GraphModelIntegration::GraphControllerRequestBus::EventResult(node, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodeById, nodeUiId);
  684. if (!node)
  685. {
  686. continue;
  687. }
  688. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  689. AZ::EntityId entityId = baseNodePtr->GetVegetationEntityId();
  690. entitiesToDuplicate.push_back(entityId);
  691. }
  692. // Duplicate the corresponding entities
  693. auto outcome = m_prefabPublicInterface->DuplicateEntitiesInInstance(entitiesToDuplicate);
  694. if (!outcome.IsSuccess())
  695. {
  696. AZ_Error("LandscapeCanvas", false, outcome.GetError().c_str());
  697. return;
  698. }
  699. auto duplicatedEntities = outcome.GetValue();
  700. // Create a mapping of the original EntityId's corresponding to the
  701. // new EntityId's that were duplicated.
  702. int i = 0;
  703. const size_t numDuplicatedEntities = duplicatedEntities.size();
  704. for (const auto& originalEntityId : entitiesToDuplicate)
  705. {
  706. // An EntityId might already exist in the mapping for the case where a single node (Entity)
  707. // has multiple wrapped nodes on it, corresponding to multiple components on a single Entity
  708. if (serialization.m_deserializedEntities.contains(originalEntityId))
  709. {
  710. continue;
  711. }
  712. if (i >= numDuplicatedEntities)
  713. {
  714. break;
  715. }
  716. serialization.m_deserializedEntities[originalEntityId] = duplicatedEntities[i++];
  717. }
  718. LandscapeCanvas::LandscapeCanvasSerializationRequestBus::Broadcast(&LandscapeCanvas::LandscapeCanvasSerializationRequests::SetDeserializedEntities, serialization.m_deserializedEntities);
  719. m_ignoreGraphUpdates = false;
  720. }
  721. void MainWindow::OnGraphModelGraphModified(GraphModel::NodePtr node)
  722. {
  723. AZ_UNUSED(node);
  724. if (m_ignoreGraphUpdates)
  725. {
  726. return;
  727. }
  728. // Flag the level as dirty if anything in the graph changes, since some graph actions
  729. // (e.g. moving nodes around, creating bookmarks, etc...) don't trigger actual Entity/Component
  730. // changes that would flag the level as dirty.
  731. if (const auto editor = GetLegacyEditor();
  732. !editor->IsModified())
  733. {
  734. editor->SetModifiedFlag();
  735. editor->SetModifiedModule(eModifiedEntities);
  736. }
  737. }
  738. void MainWindow::OnEditorOpened(GraphCanvas::EditorDockWidget* dockWidget)
  739. {
  740. using namespace AzFramework::Terrain;
  741. // Detect if it's possible to create a new entity in the current context
  742. AZ::EntityId focusRootEntityId = m_prefabFocusPublicInterface->GetFocusedPrefabContainerEntityId(s_editorEntityContextId);
  743. if (m_readOnlyEntityPublicInterface->IsReadOnly(focusRootEntityId))
  744. {
  745. // Abort
  746. CloseEditor(dockWidget->GetDockWidgetId());
  747. QWidget* activeWindow = AzToolsFramework::GetActiveWindow();
  748. QMessageBox::warning(
  749. activeWindow,
  750. QString("Landscape Canvas Asset Creation Error"),
  751. QString("Could not create new Landscape Canvas asset under read-only entity."),
  752. QMessageBox::Ok,
  753. QMessageBox::Ok
  754. );
  755. return;
  756. }
  757. // Invoke the GraphCanvas base instead of the GraphModelIntegration::EditorMainWindow so that we
  758. // can do our own custom handling when opening an existing graph
  759. GraphCanvas::AssetEditorMainWindow::OnEditorOpened(dockWidget);
  760. // If this graph was opened by File -> New or by dragging a node from
  761. // the Node Palette onto the empty canvas, then we first need to create
  762. // a root Entity for it with a Landscape Canvas component.
  763. if (!m_ignoreGraphUpdates)
  764. {
  765. AZ::EntityId rootEntityId;
  766. AzToolsFramework::EditorRequestBus::BroadcastResult(rootEntityId, &AzToolsFramework::EditorRequests::CreateNewEntity, AZ::EntityId());
  767. AZ::Vector3 translation = AZ::Vector3::CreateZero();
  768. AZ::TransformBus::EventResult(translation, rootEntityId, &AZ::TransformBus::Events::GetWorldTranslation);
  769. // Get the terrain height at the XY world coordinate where our new Entity was created
  770. float height = translation.GetZ();
  771. TerrainDataRequestBus::BroadcastResult(height, &TerrainDataRequests::GetHeightFromFloats, translation.GetX(), translation.GetY(), TerrainDataRequests::Sampler::BILINEAR, nullptr);
  772. // Update the new Entity translation so that it is placed on the terrain so that any vegetation resulting
  773. // from it will be planted on the terrain
  774. translation.SetZ(height);
  775. AZ::TransformBus::Event(rootEntityId, &AZ::TransformBus::Events::SetWorldTranslation, translation);
  776. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::AddComponentsToEntities, AzToolsFramework::EntityIdList{ rootEntityId }, AZ::ComponentTypeList{ LandscapeCanvas::EditorLandscapeCanvasComponentTypeId });
  777. HandleGraphOpened(rootEntityId, dockWidget->GetDockWidgetId());
  778. // Update the tab name for the new graph after creating the root Entity to hold its Landscape Canvas component
  779. AZ::Entity* rootEntity = nullptr;
  780. AZ::ComponentApplicationBus::BroadcastResult(rootEntity, &AZ::ComponentApplicationRequests::FindEntity, rootEntityId);
  781. AZ_Assert(rootEntity, "No Entity found for EntityId = %s", rootEntityId.ToString().c_str());
  782. OnEntityNameChanged(rootEntityId, rootEntity->GetName());
  783. }
  784. // Initialize the EntityIdNodeMaps that will be used for parsing/creating connections later
  785. GraphCanvas::GraphId graphId = dockWidget->GetGraphId();
  786. EntityIdNodeMaps newNodeMaps;
  787. for (int i = 0; i < EntityIdNodeMapEnum::Count; ++i)
  788. {
  789. newNodeMaps.push_back(EntityIdNodeMap());
  790. }
  791. m_entityIdNodeMapsByGraph[graphId] = newNodeMaps;
  792. m_deletedNodePositions[graphId] = DeletedNodePositionsMap();
  793. }
  794. void MainWindow::OnEditorClosing(GraphCanvas::EditorDockWidget* dockWidget)
  795. {
  796. // Stop listening for changes to this Vegetation Entity when we close the graph for it.
  797. GraphCanvas::DockWidgetId dockWidgetId = dockWidget->GetDockWidgetId();
  798. auto it = AZStd::find_if(m_dockWidgetsByEntity.begin(), m_dockWidgetsByEntity.end(),
  799. [dockWidgetId](decltype(m_dockWidgetsByEntity)::const_reference pair)
  800. {
  801. return dockWidgetId == pair.second;
  802. }
  803. );
  804. if (it != m_dockWidgetsByEntity.end())
  805. {
  806. const AZ::EntityId& rootEntityId = it->first;
  807. m_dockWidgetsByEntity.erase(it);
  808. // Save our graph whenever it is closed
  809. AZ::Entity* entity = nullptr;
  810. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, rootEntityId);
  811. if (entity)
  812. {
  813. GraphCanvas::GraphId graphId = dockWidget->GetGraphId();
  814. GraphCanvas::GraphModelRequestBus::Event(graphId, &GraphCanvas::GraphModelRequests::OnSaveDataDirtied, graphId);
  815. // Serialize the graph into the Landscape Canvas component on the root Entity that
  816. // corresponds to this graph
  817. GraphModel::GraphPtr graph = GetGraphById(graphId);
  818. auto landscapeCanvasComponent = azrtti_cast<LandscapeCanvas::EditorLandscapeCanvasComponent*>(entity->FindComponent(LandscapeCanvas::EditorLandscapeCanvasComponentTypeId));
  819. if (landscapeCanvasComponent)
  820. {
  821. landscapeCanvasComponent->m_graph = *m_serializeContext->CloneObject(graph.get());
  822. // Mark the Landscape Canvas entity as dirty so the changes to the graph will be picked up on the next save
  823. AzToolsFramework::ScopedUndoBatch undo("Update Landscape Canvas Graph");
  824. AzToolsFramework::ToolsApplicationRequests::Bus::Broadcast(&AzToolsFramework::ToolsApplicationRequests::Bus::Events::AddDirtyEntity, rootEntityId);
  825. }
  826. }
  827. }
  828. // Clear out the cached EntityIdNode mapping for the graph when it is closed
  829. GraphCanvas::GraphId graphId = dockWidget->GetGraphId();
  830. auto nodeMapIt = m_entityIdNodeMapsByGraph.find(graphId);
  831. if (nodeMapIt != m_entityIdNodeMapsByGraph.end())
  832. {
  833. m_entityIdNodeMapsByGraph.erase(nodeMapIt);
  834. }
  835. auto nodePositionsMapIt = m_deletedNodePositions.find(graphId);
  836. if (nodePositionsMapIt != m_deletedNodePositions.end())
  837. {
  838. m_deletedNodePositions.erase(nodePositionsMapIt);
  839. }
  840. // Do this last so that the graph isn't closed before we get a chance to save it
  841. GraphModelIntegration::EditorMainWindow::OnEditorClosing(dockWidget);
  842. }
  843. QAction* MainWindow::AddFileNewAction(QMenu* menu)
  844. {
  845. m_fileNewAction = GraphModelIntegration::EditorMainWindow::AddFileNewAction(menu);
  846. // Disable our file menu action for creating a new graph if a level isn't loaded
  847. m_fileNewAction->setEnabled(GetLegacyEditor()->IsLevelLoaded());
  848. return m_fileNewAction;
  849. }
  850. QAction* MainWindow::AddFileOpenAction(QMenu* menu)
  851. {
  852. AZ_UNUSED(menu);
  853. return nullptr;
  854. }
  855. QAction* MainWindow::AddFileSaveAction(QMenu* menu)
  856. {
  857. AZ_UNUSED(menu);
  858. return nullptr;
  859. }
  860. QAction* MainWindow::AddFileSaveAsAction(QMenu* menu)
  861. {
  862. AZ_UNUSED(menu);
  863. return nullptr;
  864. }
  865. QMenu* MainWindow::AddEditMenu()
  866. {
  867. QMenu* menu = GraphModelIntegration::EditorMainWindow::AddEditMenu();
  868. // Temporarily add our own Undo/Redo menu actions that will just trigger the main Editor's
  869. // Undo/Redo actions, since our graphs are listening/responding to Editor Entity/Component
  870. // changes (e.g. entities/components being added/removed).
  871. // Once our generic GraphModel windowing framework supports Undo/Redo then we will extend
  872. // the GraphModel::EditorMainWindow to provide the Undo/Redo menu actions by default.
  873. if (menu && !menu->actions().empty())
  874. {
  875. auto separatorAction = menu->insertSeparator(menu->actions().first());
  876. auto redoAction = new QAction(QObject::tr("&Redo"), this);
  877. redoAction->setShortcut(AzQtComponents::RedoKeySequence);
  878. QObject::connect(redoAction, &QAction::triggered, [] {
  879. GetLegacyEditor()->Redo();
  880. });
  881. menu->insertAction(separatorAction, redoAction);
  882. auto undoAction = new QAction(QObject::tr("&Undo"), this);
  883. undoAction->setShortcut(QKeySequence::Undo);
  884. QObject::connect(undoAction, &QAction::triggered, [] {
  885. GetLegacyEditor()->Undo();
  886. });
  887. menu->insertAction(redoAction, undoAction);
  888. }
  889. return menu;
  890. }
  891. QAction* MainWindow::AddEditCutAction([[maybe_unused]] QMenu* menu)
  892. {
  893. // Disabled until we can leverage prefab API to cut/copy/paste
  894. return nullptr;
  895. }
  896. QAction* MainWindow::AddEditCopyAction([[maybe_unused]] QMenu* menu)
  897. {
  898. // Disabled until we can leverage prefab API to cut/copy/paste
  899. return nullptr;
  900. }
  901. QAction* MainWindow::AddEditPasteAction([[maybe_unused]] QMenu* menu)
  902. {
  903. // Disabled until we can leverage prefab API to cut/paste
  904. return nullptr;
  905. }
  906. void MainWindow::HandleWrapperNodeActionWidgetClicked(GraphModel::NodePtr wrapperNode, [[maybe_unused]] const QRect& actionWidgetBoundingRect, const QPointF& scenePoint, const QPoint& screenPoint)
  907. {
  908. auto baseNode = static_cast<LandscapeCanvas::BaseNode*>(wrapperNode.get());
  909. AZ::EntityId entityId = baseNode->GetVegetationEntityId();
  910. GraphCanvas::NodePaletteConfig config;
  911. config.m_editorId = GetEditorId();
  912. config.m_mimeType = LandscapeCanvas::MIME_EVENT_TYPE;
  913. config.m_isInContextMenu = true;
  914. config.m_saveIdentifier = LandscapeCanvas::CONTEXT_MENU_SAVE_IDENTIFIER;
  915. switch (baseNode->GetBaseNodeType())
  916. {
  917. case LandscapeCanvas::BaseNode::TerrainArea:
  918. config.m_rootTreeItem = GetTerrainExtendersNodePaletteRoot(GetEditorId(), entityId);
  919. break;
  920. case LandscapeCanvas::BaseNode::VegetationArea:
  921. config.m_rootTreeItem = GetAreaExtendersNodePaletteRoot(GetEditorId(), entityId);
  922. break;
  923. default:
  924. AZ_Assert(false, "Unsupported node type: %d", baseNode->GetBaseNodeType());
  925. return;
  926. }
  927. // Create the Context Menu with embedded Node Palette for adding extenders to the wrapped node
  928. // The ownership of this Node Palette is passed to the context menu
  929. LayerExtenderContextMenu menu(config, this);
  930. menu.exec(screenPoint);
  931. // Check if a node was selected in the Node Palette of our context menu.
  932. // If the menu was dismissed, then the mime event will be null.
  933. GraphCanvas::GraphCanvasMimeEvent* mimeEvent = menu.GetNodePalette()->GetContextMenuEvent();
  934. if (mimeEvent)
  935. {
  936. GraphCanvas::GraphId graphId = GetActiveGraphCanvasGraphId();
  937. AZ::Vector2 dropPos(aznumeric_cast<float>(scenePoint.x()), aznumeric_cast<float>(scenePoint.y()));
  938. m_ignoreGraphUpdates = true;
  939. // Create the node that was selected from the node palette.
  940. if (mimeEvent->ExecuteEvent(dropPos, dropPos, graphId))
  941. {
  942. GraphCanvas::NodeId nodeId = mimeEvent->GetCreatedNodeId();
  943. GraphModel::NodePtr node;
  944. GraphModelIntegration::GraphControllerRequestBus::EventResult(node, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodeById, nodeId);
  945. // Stop ignoring the graph updates once the node has been created so that the OnGraphModelNodeWrapped
  946. // will get called once we wrap the node in the next step
  947. m_ignoreGraphUpdates = false;
  948. // Wrap the extender node on its parent wrapped node.
  949. AZ::u32 layoutOrder = GetWrappedNodeLayoutOrder(node);
  950. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::WrapNodeOrdered, wrapperNode, node, layoutOrder);
  951. }
  952. else
  953. {
  954. m_ignoreGraphUpdates = false;
  955. }
  956. }
  957. }
  958. GraphCanvas::Endpoint MainWindow::CreateNodeForProposal(const AZ::EntityId& connectionId, const GraphCanvas::Endpoint& endpoint, const QPointF& scenePoint, const QPoint& screenPoint)
  959. {
  960. GraphCanvas::Endpoint createdEndpoint = GraphCanvas::AssetEditorMainWindow::CreateNodeForProposal(connectionId, endpoint, scenePoint, screenPoint);
  961. if (createdEndpoint.IsValid())
  962. {
  963. GraphModel::NodePtr sourceNode;
  964. GraphModelIntegration::GraphControllerRequestBus::EventResult(sourceNode, GetActiveGraphCanvasGraphId(), &GraphModelIntegration::GraphControllerRequests::GetNodeById, endpoint.GetNodeId());
  965. GraphModel::NodePtr createdNode;
  966. GraphModelIntegration::GraphControllerRequestBus::EventResult(createdNode, GetActiveGraphCanvasGraphId(), &GraphModelIntegration::GraphControllerRequests::GetNodeById, createdEndpoint.GetNodeId());
  967. AZ_Assert(sourceNode && createdNode, "Unable to find GraphModel::Node for associated Endpoint.");
  968. // If the source node and the created node both have preview bounds slots,
  969. // then automatically connect the preview bounds on the created node to the
  970. // same slot as the one on the source node (if it is connected to something)
  971. GraphModel::SlotPtr sourcePreviewBoundsSlot = sourceNode->GetSlot(LandscapeCanvas::PREVIEW_BOUNDS_SLOT_ID);
  972. GraphModel::SlotPtr createdPreviewBoundsSlot = createdNode->GetSlot(LandscapeCanvas::PREVIEW_BOUNDS_SLOT_ID);
  973. if (sourcePreviewBoundsSlot && createdPreviewBoundsSlot)
  974. {
  975. // The preview bounds is an input slot, so it will only have 1 connection (if any)
  976. GraphModel::Slot::ConnectionList connections = sourcePreviewBoundsSlot->GetConnections();
  977. if (connections.size() == 1)
  978. {
  979. GraphModel::ConnectionPtr connection = *connections.begin();
  980. GraphModel::SlotPtr previewBoundsSourceSlot = connection->GetSourceSlot();
  981. GraphModelIntegration::GraphControllerRequestBus::Event(GetActiveGraphCanvasGraphId(), &GraphModelIntegration::GraphControllerRequests::AddConnection, previewBoundsSourceSlot, createdPreviewBoundsSlot);
  982. }
  983. }
  984. }
  985. return createdEndpoint;
  986. }
  987. void MainWindow::OnEntityActivated(const AZ::EntityId& entityId)
  988. {
  989. if (m_ignoreGraphUpdates)
  990. {
  991. return;
  992. }
  993. // We already handle when components are explicitly enabled/disabled, but if they are enabled/disabled
  994. // as a result of their dependencies being enabled/disabled, then we don't get an explicit
  995. // notification for that action. The Entity Inspector also uses this EntitySystemBus::OnEntityActivated
  996. // to determine when to re-check component state, since the Entity gets deactivated/re-activated when
  997. // making component changes, so this is when we should update the enabled/disabled state of any nodes
  998. // associated with this Entity.
  999. GraphModel::NodePtrList matchingNodes = GetAllNodesMatchingEntity(entityId);
  1000. for (auto node : matchingNodes)
  1001. {
  1002. GraphCanvas::GraphId graphId = GetGraphId(node->GetGraph());
  1003. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  1004. if (baseNodePtr->GetComponent())
  1005. {
  1006. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::EnableNode, node);
  1007. }
  1008. else
  1009. {
  1010. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  1011. }
  1012. }
  1013. }
  1014. void MainWindow::OnEntityNameChanged(const AZ::EntityId& entityId, const AZStd::string& name)
  1015. {
  1016. // Update the entity name slot on any nodes for this entity across all graphs.
  1017. for (GraphCanvas::GraphId graphId : GetOpenGraphIds())
  1018. {
  1019. GraphModel::NodePtrList nodes = GetAllNodesMatchingEntityInGraph(graphId, entityId);
  1020. for (auto& node : nodes)
  1021. {
  1022. if (auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get()); baseNodePtr)
  1023. {
  1024. // Refresh the entity name on this node.
  1025. baseNodePtr->RefreshEntityName();
  1026. // Refresh the display for the entity name on this node.
  1027. if (GraphModel::SlotPtr slot = node->GetSlot(LandscapeCanvas::ENTITY_NAME_SLOT_ID); slot)
  1028. {
  1029. GraphCanvas::SlotId slotId;
  1030. GraphModelIntegration::GraphControllerRequestBus::EventResult(
  1031. slotId, graphId, &GraphModelIntegration::GraphControllerRequests::GetSlotIdBySlot, slot);
  1032. AZ::EBusAggregateResults<GraphCanvas::NodePropertyDisplay*> nodePropertyDisplays;
  1033. GraphCanvas::NodePropertyRequestBus::EventResult(
  1034. nodePropertyDisplays, slotId, &GraphCanvas::NodePropertyRequests::GetNodePropertyDisplay);
  1035. for (auto& nodePropertyDisplay : nodePropertyDisplays.values)
  1036. {
  1037. if (nodePropertyDisplay)
  1038. {
  1039. nodePropertyDisplay->UpdateDisplay();
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. // If this entity is also the root entity for a graph, update the graph's tab name.
  1047. auto it = m_dockWidgetsByEntity.find(entityId);
  1048. if (it != m_dockWidgetsByEntity.end())
  1049. {
  1050. GraphCanvas::DockWidgetId dockWidgetId = it->second;
  1051. GraphCanvas::EditorDockWidgetRequestBus::Event(dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::SetTitle, name);
  1052. }
  1053. }
  1054. bool MainWindow::HandleGraphOpened(const AZ::EntityId& rootEntityId, const GraphCanvas::DockWidgetId& dockWidgetId)
  1055. {
  1056. // Keep track of the dock widget created for this root Vegetation Entity, and
  1057. // listen for any changes to the entity
  1058. m_dockWidgetsByEntity[rootEntityId] = dockWidgetId;
  1059. GraphCanvas::GraphId graphId;
  1060. GraphCanvas::EditorDockWidgetRequestBus::EventResult(graphId, dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::GetGraphId);
  1061. AZ::Entity* entity = nullptr;
  1062. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, rootEntityId);
  1063. AZ_Assert(entity, "No Entity found for EntityId = %s", rootEntityId.ToString().c_str());
  1064. auto landscapeCanvasComponent = azrtti_cast<LandscapeCanvas::EditorLandscapeCanvasComponent*>(entity->FindComponent(LandscapeCanvas::EditorLandscapeCanvasComponentTypeId));
  1065. AZ_Assert(landscapeCanvasComponent, "Missing Landscape Canvas component on EntityId = %s", rootEntityId.ToString().c_str());
  1066. bool isNewGraph = false;
  1067. GraphModel::GraphPtr graph = AZStd::make_shared<GraphModel::Graph>(GetGraphContext());
  1068. GraphModel::Graph& savedGraph = landscapeCanvasComponent->m_graph;
  1069. if (savedGraph.GetNodes().empty())
  1070. {
  1071. // If this graph has never been saved before, then there won't be any nodes in
  1072. // the serialized graph from our component, so we don't need to load anything
  1073. isNewGraph = true;
  1074. }
  1075. else
  1076. {
  1077. // Load the serialized graph and invoke the PostLoadSetup so that all the metadata
  1078. // for the graph/nodes/slots gets setup properly before we call CreateGraphController
  1079. // that will actually recreate the full graph in the scene
  1080. graph.reset(m_serializeContext->CloneObject(&savedGraph));
  1081. graph->PostLoadSetup(GetGraphContext());
  1082. }
  1083. // Keep track of our new graph.
  1084. m_graphs[graphId] = graph;
  1085. // Listen for GraphController notifications on the new graph.
  1086. GraphModelIntegration::GraphControllerNotificationBus::MultiHandler::BusConnect(graphId);
  1087. // Create the controller for the new graph.
  1088. GraphModelIntegration::GraphManagerRequestBus::Broadcast(&GraphModelIntegration::GraphManagerRequests::CreateGraphController, graphId, graph);
  1089. // If we loaded a saved graph, we need to make sure all the loaded nodes Entity/Components still exist,
  1090. // and also look for any new components that have been added that need new nodes created for them
  1091. if (!isNewGraph)
  1092. {
  1093. RefreshEntityComponentNodes(rootEntityId, graphId);
  1094. }
  1095. return isNewGraph;
  1096. }
  1097. GraphCanvas::ContextMenuAction::SceneReaction MainWindow::ShowNodeContextMenu(const AZ::EntityId& nodeId, const QPoint& screenPoint, const QPointF& scenePoint)
  1098. {
  1099. NodeContextMenu contextMenu(GetActiveGraphCanvasGraphId());
  1100. return AssetEditorMainWindow::HandleContextMenu(contextMenu, nodeId, screenPoint, scenePoint);
  1101. }
  1102. void MainWindow::GetChildrenTree(const AZ::EntityId& rootEntityId, AzToolsFramework::EntityIdList& childrenList)
  1103. {
  1104. AzToolsFramework::EntityIdList children;
  1105. AzToolsFramework::EditorEntityInfoRequestBus::EventResult(children, rootEntityId, &AzToolsFramework::EditorEntityInfoRequestBus::Events::GetChildren);
  1106. for (auto childId : children)
  1107. {
  1108. childrenList.push_back(childId);
  1109. GetChildrenTree(childId, childrenList);
  1110. }
  1111. }
  1112. QString MainWindow::GetPropertyPathForSlot(GraphModel::SlotPtr slot, GraphModel::DataType::Enum dataType, int elementIndex)
  1113. {
  1114. static const char* ConfigurationPropertyPrefix = "Configuration|";
  1115. static const char* PreviewEntityIdPropertyPath = "Previewer|Preview Settings|Pin Preview to Shape";
  1116. static const char* GradientEntityIdPropertyPath = "Gradient|Gradient Entity Id";
  1117. static const char* ShapeEntityIdPropertyPath = "Shape Entity Id";
  1118. static const char* InputBoundsEntityIdPropertyPath = "Input Bounds";
  1119. static const char* PinToShapeEntityIdPropertyPath = "Pin To Shape Entity Id";
  1120. static const char* VegetationAreasPropertyPath = "Vegetation Areas";
  1121. static const char* TerrainSurfaceEntityIdPropertyPath = "Gradient Entity";
  1122. const GraphModel::SlotName& slotName = slot->GetName();
  1123. QString propertyPath;
  1124. bool useConfigurationPrefix = true;
  1125. switch (dataType)
  1126. {
  1127. case LandscapeCanvas::LandscapeCanvasDataTypeEnum::Bounds:
  1128. {
  1129. if (slotName == LandscapeCanvas::PREVIEW_BOUNDS_SLOT_ID)
  1130. {
  1131. propertyPath = PreviewEntityIdPropertyPath;
  1132. useConfigurationPrefix = false;
  1133. }
  1134. else if (slotName == LandscapeCanvas::INBOUND_SHAPE_SLOT_ID
  1135. || slotName == LandscapeCanvas::PLACEMENT_BOUNDS_SLOT_ID)
  1136. {
  1137. propertyPath = ShapeEntityIdPropertyPath;
  1138. }
  1139. else if (slotName == LandscapeCanvas::PIN_TO_SHAPE_SLOT_ID)
  1140. {
  1141. propertyPath = PinToShapeEntityIdPropertyPath;
  1142. }
  1143. else if (slotName == LandscapeCanvas::INPUT_BOUNDS_SLOT_ID)
  1144. {
  1145. propertyPath = InputBoundsEntityIdPropertyPath;
  1146. }
  1147. } break;
  1148. case LandscapeCanvas::LandscapeCanvasDataTypeEnum::Gradient:
  1149. {
  1150. GraphModel::NodePtr targetNode = slot->GetParentNode();
  1151. auto targetBaseNode = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  1152. auto targetBaseNodeType = targetBaseNode->GetBaseNodeType();
  1153. if (targetBaseNodeType == LandscapeCanvas::BaseNode::TerrainSurfaceExtender)
  1154. {
  1155. propertyPath = TerrainSurfaceEntityIdPropertyPath;
  1156. }
  1157. else if (targetBaseNodeType != LandscapeCanvas::BaseNode::TerrainExtender)
  1158. {
  1159. propertyPath = GradientEntityIdPropertyPath;
  1160. }
  1161. // Special case handling of some gradient properties for extendable gradient mixers
  1162. // and the position modifier which are nested under group elements
  1163. if (slot->SupportsExtendability())
  1164. {
  1165. QString gradientListName;
  1166. if (targetBaseNodeType == LandscapeCanvas::BaseNode::TerrainExtender)
  1167. {
  1168. gradientListName = "Gradient Entities|[%1]";
  1169. }
  1170. else if (targetBaseNodeType == LandscapeCanvas::BaseNode::TerrainSurfaceExtender)
  1171. {
  1172. gradientListName = "Gradient to Surface Mappings|[%1]|";
  1173. }
  1174. else
  1175. {
  1176. gradientListName = "Layers|[%1]|";
  1177. }
  1178. propertyPath.prepend(gradientListName.arg(elementIndex));
  1179. }
  1180. else if (slotName == LandscapeCanvas::BaseAreaModifierNode::INBOUND_GRADIENT_X_SLOT_ID
  1181. || slotName == LandscapeCanvas::BaseAreaModifierNode::INBOUND_GRADIENT_Y_SLOT_ID
  1182. || slotName == LandscapeCanvas::BaseAreaModifierNode::INBOUND_GRADIENT_Z_SLOT_ID)
  1183. {
  1184. // The X/Y/Z supported nodes are Position/Rotation modifiers, so we need
  1185. // to figure out which one this is to get the right property path
  1186. if (targetNode)
  1187. {
  1188. // The node titles are "Position Modifier" or "Rotation Modifier", and
  1189. // the property path is expecting Position/Rotation|Gradient|Gradient Entity Id
  1190. // so we need to parse the "Position"/"Rotation" out of the title to use
  1191. // in the property path
  1192. QStringList parts = QString(targetNode->GetTitle()).split(' ');
  1193. AZ_Assert(!parts.empty(), "Unrecognized node title");
  1194. propertyPath.prepend(QString("%1 %2|").arg(parts[0]).arg(slotName.back()));
  1195. }
  1196. }
  1197. } break;
  1198. case LandscapeCanvas::LandscapeCanvasDataTypeEnum::Area:
  1199. {
  1200. propertyPath = QString("%1|[%2]").arg(VegetationAreasPropertyPath).arg(elementIndex);
  1201. } break;
  1202. }
  1203. // Most of our supported properties are nested under a top-level configuration path
  1204. if (!propertyPath.isEmpty() && useConfigurationPrefix)
  1205. {
  1206. propertyPath.prepend(ConfigurationPropertyPrefix);
  1207. }
  1208. return propertyPath;
  1209. }
  1210. void MainWindow::UpdateConnectionData(GraphModel::ConnectionPtr connection, bool added)
  1211. {
  1212. if (!connection)
  1213. {
  1214. return;
  1215. }
  1216. GraphCanvas::GraphId graphId = (*GraphModelIntegration::GraphControllerNotificationBus::GetCurrentBusId());
  1217. // Similarly as below, this protects against the edge case where this logic gets hit if the node and/or
  1218. // slot belonging to this connection got deleted before this was executed.
  1219. if (!connection->GetSourceNode() || !connection->GetTargetNode() || !connection->GetSourceSlot() || !connection->GetTargetSlot())
  1220. {
  1221. return;
  1222. }
  1223. // Figure out the element index we need to update based on the index of the
  1224. // target slot on the target node that have the same data type
  1225. GraphModel::NodePtr targetNode = connection->GetTargetNode();
  1226. GraphModel::SlotPtr targetSlot = connection->GetTargetSlot();
  1227. GraphModel::DataTypePtr dataType = connection->GetSourceSlot()->GetDataType();
  1228. int elementIndexToModify = GetInboundDataSlotIndex(targetNode, dataType, targetSlot);
  1229. if (elementIndexToModify == InvalidSlotIndex)
  1230. {
  1231. // Typically this shouldn't be reached, but there are cases where the slot index might
  1232. // be invalid, such as the target node being deleted before the connection is triggered
  1233. // to be removed, which could happen if the node was deleted while it was in a collapsed group.
  1234. return;
  1235. }
  1236. // If the connection was removed, the target will be set to an invalid EntityId
  1237. // If the connection was added, the target will be updated with the appropriate EntityId from the source
  1238. AZ::EntityId newEntityId;
  1239. if (added)
  1240. {
  1241. auto sourceNode = static_cast<LandscapeCanvas::BaseNode*>(connection->GetSourceNode().get());
  1242. newEntityId = sourceNode->GetVegetationEntityId();
  1243. }
  1244. // Figure out the property path we are looking for based on the data type of the slot
  1245. GraphModel::DataType::Enum dataTypeEnum = dataType->GetTypeEnum();
  1246. QString propertyPath = GetPropertyPathForSlot(targetSlot, dataTypeEnum, elementIndexToModify);
  1247. if (propertyPath.isEmpty())
  1248. {
  1249. // Special-case to handle setting an image asset path.
  1250. // This needs separate logic because all our other data types (Bounds/Gradient/Area)
  1251. // are just AZ::EntityId under the hood and can be set directly on the property,
  1252. // whereas the output asset comes as an AZ::IO::Path and the input is an actual
  1253. // AZ::RPI::StreamingImageAsset, so we need to use the helper buses to get/set
  1254. if (added && dataTypeEnum == LandscapeCanvas::LandscapeCanvasDataTypeEnum::Path)
  1255. {
  1256. auto targetBaseNode = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  1257. HandleSetImageAssetPath(newEntityId, targetBaseNode->GetVegetationEntityId());
  1258. }
  1259. return;
  1260. }
  1261. // Calling UpdateConnectionData will result in a component property being modified,
  1262. // which in turn will result in prefab propagation. Because that is delayed until the next
  1263. // tick, there is a point in time where the OnEntityComponentPropertyChanged event will
  1264. // be triggered but the property won't be set yet, so when UpdateConnections gets called,
  1265. // it will think the connection corresponding to that property needs to be removed. So
  1266. // we need to handle this case by ignoring the next component property change for this entity
  1267. // since it will already be up-to-date by UpdateConnectionData being invoked
  1268. if (targetNode)
  1269. {
  1270. auto targetBaseNode = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  1271. m_ignoreEntityComponentPropertyChanges.push_back(targetBaseNode->GetVegetationEntityId());
  1272. }
  1273. // If our target is an extendable slot (e.g. gradient mixer, area blender, etc...) then the element that needs
  1274. // to be set is actually in a container, and might need to be added
  1275. bool elementInContainer = targetSlot->SupportsExtendability();
  1276. // Queue this event since it occurs when attaching/detaching connections
  1277. // in the UI, otherwise the attach/detach will appear to stall momentarily
  1278. QTimer::singleShot(0, [this, graphId, targetNode, targetSlot, newEntityId, propertyPath, elementIndexToModify, elementInContainer]() {
  1279. if (!targetNode)
  1280. {
  1281. return;
  1282. }
  1283. // Special case for the Vegetation Area Placement Bounds, the slot actually represents a separate
  1284. // Reference Shape or actual Shape component on the same Entity
  1285. AZ::Component* component = nullptr;
  1286. auto targetBaseNode = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  1287. if (targetBaseNode->GetBaseNodeType() == LandscapeCanvas::BaseNode::BaseNodeType::VegetationArea && targetSlot->GetName() == LandscapeCanvas::PLACEMENT_BOUNDS_SLOT_ID)
  1288. {
  1289. // Make sure the target entity still exists before we do all this special-case logic, because it
  1290. // might have been deleted and UpdateConnectionData was only executed because GraphModel was removing
  1291. // the connections associated with a node being deleted
  1292. const AZ::EntityId& targetEntityId = targetBaseNode->GetVegetationEntityId();
  1293. AZ::Entity* targetEntity = nullptr;
  1294. AZ::ComponentApplicationBus::BroadcastResult(targetEntity, &AZ::ComponentApplicationRequests::FindEntity, targetEntityId);
  1295. if (!targetEntity)
  1296. {
  1297. return;
  1298. }
  1299. // Special case handling when connecting the Placement Bounds to a Shape that exists
  1300. // on the same Entity by re-enabling that disabled Shape component. This is mainly for
  1301. // handling existing Vegetation data that wasn't authored in a graph originally.
  1302. if (newEntityId == targetEntityId)
  1303. {
  1304. auto nodeMapsIt = m_entityIdNodeMapsByGraph.find(graphId);
  1305. if (nodeMapsIt == m_entityIdNodeMapsByGraph.end())
  1306. {
  1307. return;
  1308. }
  1309. const EntityIdNodeMaps& nodeMaps = nodeMapsIt->second;
  1310. const auto& shapeNodeMap = nodeMaps[EntityIdNodeMapEnum::Shapes];
  1311. auto shapeIt = shapeNodeMap.find(targetEntityId);
  1312. if (shapeIt != shapeNodeMap.end())
  1313. {
  1314. AzToolsFramework::ScopedUndoBatch undoBatch("Enable Embedded Shape");
  1315. auto shapeNode = static_cast<LandscapeCanvas::BaseNode*>(shapeIt->second.get());
  1316. AZ::Entity::ComponentArrayType disabledComponents;
  1317. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(targetEntityId, &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  1318. for (auto disabledComponent : disabledComponents)
  1319. {
  1320. // Look through the disabled components on our Entity for our disabled Shape component
  1321. if (disabledComponent->GetId() == shapeNode->GetComponentId())
  1322. {
  1323. // Re-enable our Shape component
  1324. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::EnableComponents, AZ::Entity::ComponentArrayType{ disabledComponent });
  1325. // Disable any incompatible components (e.g. an existing Reference Shape component on the Entity)
  1326. AzToolsFramework::EntityCompositionRequests::PendingComponentInfo pendingComponentInfo;
  1327. AzToolsFramework::EntityCompositionRequestBus::BroadcastResult(pendingComponentInfo, &AzToolsFramework::EntityCompositionRequests::GetPendingComponentInfo, disabledComponent);
  1328. if (!pendingComponentInfo.m_validComponentsThatAreIncompatible.empty())
  1329. {
  1330. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::DisableComponents, pendingComponentInfo.m_validComponentsThatAreIncompatible);
  1331. }
  1332. break;
  1333. }
  1334. }
  1335. undoBatch.MarkEntityDirty(targetEntityId);
  1336. return;
  1337. }
  1338. }
  1339. // For the common case, we just need to use the Reference Shape component on this Entity if it is enabled
  1340. auto baseAreaNodePtr = static_cast<LandscapeCanvas::BaseAreaNode*>(targetNode.get());
  1341. component = baseAreaNodePtr->GetReferenceShapeComponent();
  1342. // If GetReferenceShapeComponent() fails, then that means either there is no Reference Shape component
  1343. // on our Entity, or there is but it is disabled
  1344. if (!component)
  1345. {
  1346. // Look for a disabled Reference Shape component on this Entity and re-enable it if we find it
  1347. AZ::Entity::ComponentArrayType disabledComponents;
  1348. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(targetEntityId, &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  1349. for (auto disabledComponent : disabledComponents)
  1350. {
  1351. if (disabledComponent->RTTI_GetType() == LmbrCentral::EditorReferenceShapeComponentTypeId)
  1352. {
  1353. component = disabledComponent;
  1354. // Re-enable our Reference Shape component
  1355. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::EnableComponents, AZ::Entity::ComponentArrayType{ component });
  1356. // Disable any incompatible components (e.g. a previous Shape Component)
  1357. AzToolsFramework::EntityCompositionRequests::PendingComponentInfo pendingComponentInfo;
  1358. AzToolsFramework::EntityCompositionRequestBus::BroadcastResult(pendingComponentInfo, &AzToolsFramework::EntityCompositionRequests::GetPendingComponentInfo, component);
  1359. if (!pendingComponentInfo.m_validComponentsThatAreIncompatible.empty())
  1360. {
  1361. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::DisableComponents, pendingComponentInfo.m_validComponentsThatAreIncompatible);
  1362. }
  1363. break;
  1364. }
  1365. }
  1366. // If 'component' is still null then that means there is no Reference Shape component on our Entity, so we need to add one
  1367. if (!component)
  1368. {
  1369. AZ::ComponentId componentId = AddComponentTypeIdToEntity(targetEntityId, LmbrCentral::EditorReferenceShapeComponentTypeId);
  1370. component = targetEntity->FindComponent(componentId);
  1371. }
  1372. }
  1373. }
  1374. // Otherwise, just retrieve the main component that this node represents
  1375. else
  1376. {
  1377. component = targetBaseNode->GetComponent();
  1378. }
  1379. // Check this here because the target node might have been deleted before
  1380. // this gets invoked (e.g. a connection being removed because a node was deleted)
  1381. if (!component)
  1382. {
  1383. return;
  1384. }
  1385. // Iterate through the component class element edit context to expand the elements container
  1386. // size (if necessary)
  1387. m_serializeContext->EnumerateObject(component,
  1388. // beginElemCB (this is called at the beginning of processing a new element)
  1389. [this, propertyPath, elementIndexToModify, elementInContainer](void *instance, const AZ::SerializeContext::ClassData *classData, [[maybe_unused]] const AZ::SerializeContext::ClassElement *classElement) -> bool
  1390. {
  1391. // If the element we are trying to set is in a container, we might need to add some more elements
  1392. // to the container to hold it
  1393. if (elementInContainer && classData && classData->m_container)
  1394. {
  1395. AZ::SerializeContext::IDataContainer* container = classData->m_container;
  1396. const AZ::SerializeContext::ClassElement* containerClassElement = container->GetElement(container->GetDefaultElementNameCrc());
  1397. // If the container already has enough elements, then we don't need to do anything with the container
  1398. size_t containerSize = container->Size(instance);
  1399. size_t requiredSize = elementIndexToModify + 1;
  1400. if (containerSize >= requiredSize)
  1401. {
  1402. return true;
  1403. }
  1404. if (container->IsFixedCapacity() && !container->IsSmartPointer() && requiredSize >= container->Capacity(instance))
  1405. {
  1406. GraphModel::GraphPtr graph = GetGraphById(GetActiveGraphCanvasGraphId());
  1407. AZ_Warning(graph->GetSystemName(), false, "Cannot add additional entries to the container as it is at its capacity of %zu", container->Capacity(instance));
  1408. return true;
  1409. }
  1410. // Add more elements to the container to reach the necessary size
  1411. while (containerSize < requiredSize)
  1412. {
  1413. // Reserve entry in the container
  1414. void* dataAddress = container->ReserveElement(instance, containerClassElement);
  1415. // Store the new element in the container
  1416. container->StoreElement(instance, dataAddress);
  1417. ++containerSize;
  1418. }
  1419. }
  1420. return true;
  1421. }, {},
  1422. AZ::SerializeContext::ENUM_ACCESS_FOR_WRITE, nullptr/* errorHandler */);
  1423. {
  1424. // Update the property with the new EntityId
  1425. AzToolsFramework::ScopedUndoBatch undoBatch("Update Component Property");
  1426. AzToolsFramework::PropertyTreeEditor pte = AzToolsFramework::PropertyTreeEditor(reinterpret_cast<void*>(component), component->RTTI_GetType());
  1427. pte.SetProperty(propertyPath.toUtf8().constData(), AZStd::any(newEntityId));
  1428. undoBatch.MarkEntityDirty(targetBaseNode->GetVegetationEntityId());
  1429. }
  1430. // Trigger property editors to update attributes/values or else they might be showing stale data
  1431. // since we are updating the property value directly.
  1432. AzToolsFramework::ToolsApplicationEvents::Bus::Broadcast(
  1433. &AzToolsFramework::ToolsApplicationEvents::InvalidatePropertyDisplay,
  1434. AzToolsFramework::Refresh_AttributesAndValues);
  1435. });
  1436. }
  1437. void MainWindow::HandleSetImageAssetPath(const AZ::EntityId& sourceEntityId, const AZ::EntityId& targetEntityId)
  1438. {
  1439. // This only gets called when a valid connection is made between a Gradient Baker output image slot
  1440. // (sourceEntityId) and an Image Gradient input image asset slot (targetEntityId)
  1441. // So we need to use the corresponding request bus APIs to update the image asset path on
  1442. // the Image Gradient
  1443. AZ::IO::Path outputImagePath;
  1444. GradientSignal::GradientImageCreatorRequestBus::EventResult(
  1445. outputImagePath, sourceEntityId, &GradientSignal::GradientImageCreatorRequests::GetOutputImagePath);
  1446. if (!outputImagePath.empty())
  1447. {
  1448. AzToolsFramework::ScopedUndoBatch undo("Update Image Gradient Asset");
  1449. // The ImageGradientRequests::SetImageAssetPath only takes a product path, but we are given
  1450. // a source asset path, so need to append the product extension
  1451. QString imageAssetPath = QString::fromUtf8(
  1452. outputImagePath.c_str(), static_cast<int>(outputImagePath.Native().size()));
  1453. imageAssetPath += ".streamingimage";
  1454. GradientSignal::ImageGradientRequestBus::Event(
  1455. targetEntityId, &GradientSignal::ImageGradientRequests::SetImageAssetPath, imageAssetPath.toUtf8().constData());
  1456. undo.MarkEntityDirty(targetEntityId);
  1457. }
  1458. }
  1459. GraphCanvas::GraphId MainWindow::OnGraphEntity(const AZ::EntityId& entityId)
  1460. {
  1461. GraphCanvas::GraphId graphId;
  1462. // If we already have a graph open for this Entity, then just focus it
  1463. // instead of creating a new graph
  1464. auto it = m_dockWidgetsByEntity.find(entityId);
  1465. if (it != m_dockWidgetsByEntity.end())
  1466. {
  1467. GraphCanvas::DockWidgetId dockWidgetId = it->second;
  1468. if (FocusDockWidget(dockWidgetId))
  1469. {
  1470. GraphCanvas::EditorDockWidgetRequestBus::EventResult(graphId, dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::GetGraphId);
  1471. return graphId;
  1472. }
  1473. }
  1474. m_ignoreGraphUpdates = true;
  1475. // Retrieve the entity being graphed so we can use the name for the graph title
  1476. AZ::Entity* rootEntity = nullptr;
  1477. AZ::ComponentApplicationBus::BroadcastResult(rootEntity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  1478. AZ_Assert(rootEntity, "No Entity found for EntityId = %s", entityId.ToString().c_str());
  1479. // Create a new scene
  1480. GraphCanvas::DockWidgetId dockWidgetId = CreateEditorDockWidget(rootEntity->GetName().c_str());
  1481. GraphCanvas::EditorDockWidgetRequestBus::EventResult(graphId, dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::GetGraphId);
  1482. // If HandleGraphOpened returns true, then it means there was no previously saved graph loaded,
  1483. // so we need to do the first time parsing/creating of nodes/connections + default node layout
  1484. if (HandleGraphOpened(entityId, dockWidgetId))
  1485. {
  1486. InitialEntityGraph(entityId, graphId);
  1487. }
  1488. // Otherwise, we were able to load a previously saved graph so we just need to update the
  1489. // connections
  1490. else
  1491. {
  1492. GraphModel::NodePtrList nodes;
  1493. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  1494. for (auto node : nodes)
  1495. {
  1496. UpdateConnections(node);
  1497. }
  1498. }
  1499. m_ignoreGraphUpdates = false;
  1500. // Clear the selection once we have added all the nodes, because by default nodes get
  1501. // selected when they are added to the graph
  1502. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::ClearSelection);
  1503. return graphId;
  1504. }
  1505. bool MainWindow::ConfigureDefaultLayout()
  1506. {
  1507. if (!GraphCanvas::AssetEditorMainWindow::ConfigureDefaultLayout())
  1508. {
  1509. return false;
  1510. }
  1511. // First try to close our node inspector
  1512. if (!m_customNodeInspector->close())
  1513. {
  1514. return false;
  1515. }
  1516. // Add our custom Node Inspector to the default layout
  1517. addDockWidget(Qt::RightDockWidgetArea, m_customNodeInspector);
  1518. m_customNodeInspector->setFloating(false);
  1519. m_customNodeInspector->show();
  1520. return true;
  1521. }
  1522. void MainWindow::OnEditorEntityCreated(const AZ::EntityId& entityId)
  1523. {
  1524. // If the user has deleted an Entity and then invokes Undo, its parent
  1525. // Entity may be deleted and then re-created as part of the restore
  1526. // operation, so we need to queue our deletes and detect this case
  1527. // in order to safely ignore the Entity deletion
  1528. auto queuedIt = AZStd::find(m_queuedEntityDeletes.begin(), m_queuedEntityDeletes.end(), entityId);
  1529. if (queuedIt != m_queuedEntityDeletes.end())
  1530. {
  1531. // Deleting this from the queue signifies the delete being ignored
  1532. // when it gets invoked after the singleShot
  1533. m_queuedEntityDeletes.erase(queuedIt);
  1534. // If this is any other Entity besides one of our root Entities, then
  1535. // we should still do the refresh (RefreshEntityComponentNodes) to make
  1536. // sure any components that may have been added/removed are parsed
  1537. auto it = m_dockWidgetsByEntity.find(entityId);
  1538. if (it != m_dockWidgetsByEntity.end())
  1539. {
  1540. return;
  1541. }
  1542. }
  1543. HandleEditorEntityCreated(entityId);
  1544. }
  1545. void MainWindow::HandleEditorEntityCreated(const AZ::EntityId& entityId, GraphCanvas::GraphId graphId)
  1546. {
  1547. if (m_ignoreGraphUpdates || m_prefabPropagationInProgress)
  1548. {
  1549. return;
  1550. }
  1551. // Try to find an open graph whose root Entity contains the Entity which this component was added to
  1552. if (!graphId.IsValid())
  1553. {
  1554. graphId = FindGraphContainingEntity(entityId);
  1555. }
  1556. // If we still couldn't find a graph for this Entity, then bail out
  1557. if (!graphId.IsValid())
  1558. {
  1559. return;
  1560. }
  1561. m_ignoreGraphUpdates = true;
  1562. // Refresh the Entity/Component tree for this entity to create any nodes that may
  1563. // have been added by this change. We only need to update all connections if node(s)
  1564. // were actually created.
  1565. GraphModel::NodePtrList createdNodes = RefreshEntityComponentNodes(entityId, graphId);
  1566. GraphModel::NodePtrList nodes;
  1567. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  1568. if (!createdNodes.empty())
  1569. {
  1570. for (auto node : nodes)
  1571. {
  1572. UpdateConnections(node);
  1573. }
  1574. }
  1575. // Otherwise, we only need to update connections for nodes corresponding to this Entity
  1576. else
  1577. {
  1578. for (auto node : nodes)
  1579. {
  1580. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  1581. if (baseNodePtr->GetVegetationEntityId() == entityId)
  1582. {
  1583. UpdateConnections(node);
  1584. }
  1585. }
  1586. }
  1587. m_ignoreGraphUpdates = false;
  1588. }
  1589. void MainWindow::OnEditorEntityDeleted(const AZ::EntityId& entityId)
  1590. {
  1591. if (m_prefabPropagationInProgress)
  1592. {
  1593. // If we get the entity deleted event while prefab propagation is in progress,
  1594. // it means there was some kind of change that caused that entity to be rebuilt
  1595. // that we can't track by other notification APIs (e.g. entity was added/removed
  1596. // by undo/redo), so we will queue this entity to be refreshed after the
  1597. // propagation is complete.
  1598. m_queuedEntityRefresh.push_back(entityId);
  1599. return;
  1600. }
  1601. m_queuedEntityDeletes.push_back(entityId);
  1602. QTimer::singleShot(0, [this, entityId]() {
  1603. QueuedEditorEntityDeleted(entityId);
  1604. });
  1605. }
  1606. void MainWindow::QueuedEditorEntityDeleted(const AZ::EntityId& entityId)
  1607. {
  1608. // Check if this was a legitimate Entity deletion, or if it was just a result
  1609. // of an undo/redo restoration
  1610. auto queuedIt = AZStd::find(m_queuedEntityDeletes.begin(), m_queuedEntityDeletes.end(), entityId);
  1611. if (queuedIt != m_queuedEntityDeletes.end())
  1612. {
  1613. m_queuedEntityDeletes.erase(queuedIt);
  1614. }
  1615. else
  1616. {
  1617. return;
  1618. }
  1619. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::MultiHandler::BusDisconnect(entityId);
  1620. HandleEditorEntityDeleted(entityId);
  1621. }
  1622. void MainWindow::HandleEditorEntityDeleted(const AZ::EntityId& entityId)
  1623. {
  1624. if (m_ignoreGraphUpdates)
  1625. {
  1626. return;
  1627. }
  1628. m_ignoreGraphUpdates = true;
  1629. // If the Entity deleted corresponds to one of our graphs, then close it
  1630. auto it = m_dockWidgetsByEntity.find(entityId);
  1631. if (it != m_dockWidgetsByEntity.end())
  1632. {
  1633. CloseEditor(it->second);
  1634. }
  1635. // Otherwise check if there are any nodes matching that Entity that need
  1636. // to be removed
  1637. else
  1638. {
  1639. for (GraphCanvas::GraphId graphId : GetOpenGraphIds())
  1640. {
  1641. GraphModel::NodePtrList nodes;
  1642. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  1643. for (auto node : nodes)
  1644. {
  1645. // Ignore area extenders since those nodes will end up being removed when their wrapper node (parent) is deleted
  1646. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  1647. if (baseNodePtr->GetVegetationEntityId() == entityId && !baseNodePtr->IsAreaExtender())
  1648. {
  1649. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::RemoveNode, node);
  1650. }
  1651. }
  1652. }
  1653. }
  1654. m_ignoreGraphUpdates = false;
  1655. }
  1656. void MainWindow::OnEntityPickModeStarted()
  1657. {
  1658. m_inObjectPickMode = true;
  1659. }
  1660. void MainWindow::OnEntityPickModeStopped()
  1661. {
  1662. m_inObjectPickMode = false;
  1663. }
  1664. GraphModel::NodePtrList MainWindow::GetAllNodesMatchingEntityInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityId& entityId)
  1665. {
  1666. GraphModel::NodePtrList nodes;
  1667. GraphModelIntegration::GraphControllerRequestBus::EventResult(
  1668. nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  1669. nodes.erase(AZStd::remove_if(
  1670. nodes.begin(),
  1671. nodes.end(),
  1672. [entityId](const GraphModel::NodePtr& nodePtr)
  1673. {
  1674. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(nodePtr.get());
  1675. return (!baseNodePtr) || (entityId != baseNodePtr->GetVegetationEntityId());
  1676. }), nodes.end());
  1677. return nodes;
  1678. }
  1679. GraphModel::NodePtrList MainWindow::GetAllNodesMatchingEntityComponentInGraph(
  1680. const GraphCanvas::GraphId& graphId, const AZ::EntityComponentIdPair& entityComponentId)
  1681. {
  1682. GraphModel::NodePtrList nodes;
  1683. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  1684. const AZ::EntityId& entityId = entityComponentId.GetEntityId();
  1685. const AZ::ComponentId& componentId = entityComponentId.GetComponentId();
  1686. nodes.erase(AZStd::remove_if(
  1687. nodes.begin(),
  1688. nodes.end(),
  1689. [entityId, componentId](const GraphModel::NodePtr& nodePtr)
  1690. {
  1691. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(nodePtr.get());
  1692. return (!baseNodePtr)
  1693. || (entityId != baseNodePtr->GetVegetationEntityId())
  1694. || (componentId != baseNodePtr->GetComponentId());
  1695. }), nodes.end());
  1696. return nodes;
  1697. }
  1698. GraphModel::NodePtr MainWindow::GetNodeMatchingEntityInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityId& entityId)
  1699. {
  1700. GraphModel::NodePtrList nodes = GetAllNodesMatchingEntityInGraph(graphId, entityId);
  1701. return nodes.empty() ? nullptr : nodes.front();
  1702. }
  1703. GraphModel::NodePtr MainWindow::GetNodeMatchingEntityComponentInGraph(const GraphCanvas::GraphId& graphId, const AZ::EntityComponentIdPair& entityComponentId)
  1704. {
  1705. GraphModel::NodePtrList nodes = GetAllNodesMatchingEntityComponentInGraph(graphId, entityComponentId);
  1706. return nodes.empty() ? nullptr : nodes.front();
  1707. }
  1708. GraphModel::NodePtrList MainWindow::GetAllNodesMatchingEntity(const AZ::EntityId& entityId)
  1709. {
  1710. GraphModel::NodePtrList matchingNodes;
  1711. for (GraphCanvas::GraphId graphId : GetOpenGraphIds())
  1712. {
  1713. GraphModel::NodePtrList nodes = GetAllNodesMatchingEntityInGraph(graphId, entityId);
  1714. matchingNodes.insert(matchingNodes.end(), nodes.begin(), nodes.end());
  1715. }
  1716. return matchingNodes;
  1717. }
  1718. GraphModel::NodePtrList MainWindow::GetAllNodesMatchingEntityComponent(const AZ::EntityComponentIdPair& entityComponentId)
  1719. {
  1720. GraphModel::NodePtrList matchingNodes;
  1721. for (GraphCanvas::GraphId graphId : GetOpenGraphIds())
  1722. {
  1723. GraphModel::NodePtrList nodes = GetAllNodesMatchingEntityComponentInGraph(graphId, entityComponentId);
  1724. matchingNodes.insert(matchingNodes.end(), nodes.begin(), nodes.end());
  1725. }
  1726. return matchingNodes;
  1727. }
  1728. void MainWindow::UpdateConnections(GraphModel::NodePtr node)
  1729. {
  1730. // Retrieve all the input data connections for this node that would be expected
  1731. // based on the component property fields. If this differs from what is actually
  1732. // connected for the slots on this node, then we will need to update (add/remove)
  1733. // the connections so that they match.
  1734. ConnectionsList expectedConnections;
  1735. GraphCanvas::GraphId graphId = GetGraphId(node->GetGraph());
  1736. ParseNodeConnections(graphId, node, expectedConnections);
  1737. // Iterate through the input data slots on this node to check for
  1738. // existing connections that satisfy our expected connections, and to
  1739. // remove any current connections that aren't in our expected list.
  1740. for (auto slotPair : node->GetSlots())
  1741. {
  1742. GraphModel::SlotPtr slot = slotPair.second;
  1743. // We only care about input data slots because those are the only slots
  1744. // that could be modified when a Component on an Entity is changed,
  1745. // which is what triggers OnEntityComponentPropertyChanged
  1746. if (!slot->Is(GraphModel::SlotDirection::Input, GraphModel::SlotType::Data))
  1747. {
  1748. continue;
  1749. }
  1750. // If there aren't any connections to this slot, we can skip it
  1751. auto slotConnections = slot->GetConnections();
  1752. if (slotConnections.empty())
  1753. {
  1754. continue;
  1755. }
  1756. // Input data slots will only have one connection
  1757. GraphModel::ConnectionPtr connection = *slotConnections.begin();
  1758. // Check if this connection matches one in our list of expected connections
  1759. bool matchesExisting = false;
  1760. for (auto it = expectedConnections.begin(); it != expectedConnections.end(); ++it)
  1761. {
  1762. auto sourceNode = it->first.first;
  1763. auto sourceSlot = it->first.second;
  1764. auto targetNode = it->second.first;
  1765. auto targetSlot = it->second.second;
  1766. // If we found a matching connection, then remove it from our list of expected
  1767. // so we don't have to process it after we are done checking all the slots
  1768. // on the node
  1769. if (sourceNode == connection->GetSourceNode() && sourceSlot == connection->GetSourceSlot() &&
  1770. targetNode == connection->GetTargetNode() && targetSlot == connection->GetTargetSlot())
  1771. {
  1772. matchesExisting = true;
  1773. expectedConnections.erase(it);
  1774. break;
  1775. }
  1776. }
  1777. // If this connection doesn't match an expected connection, then it needs to be removed
  1778. if (!matchesExisting)
  1779. {
  1780. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::RemoveConnection, connection);
  1781. }
  1782. }
  1783. // For the remaining expected connections, this means they didn't exist already,
  1784. // so we need to create them
  1785. for (auto it : expectedConnections)
  1786. {
  1787. GraphModel::SlotPtr sourceSlot = it.first.second;
  1788. GraphModel::SlotPtr targetSlot = it.second.second;
  1789. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::AddConnection, sourceSlot, targetSlot);
  1790. }
  1791. }
  1792. GraphCanvas::GraphId MainWindow::FindGraphContainingEntity(const AZ::EntityId& entityId)
  1793. {
  1794. GraphCanvas::GraphId graphId;
  1795. AZ::Entity* entity = nullptr;
  1796. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  1797. if (!entity)
  1798. {
  1799. return graphId;
  1800. }
  1801. AZ::EntityId parentEntityId = entityId;
  1802. AZ::EntityId levelEntityId;
  1803. AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult(levelEntityId, &AzToolsFramework::ToolsApplicationRequests::GetCurrentLevelEntityId);
  1804. // Crawl up the Entity hierarchy looking for a matching open graph.
  1805. // Stop the loop if we encounter the Level Entity, which can be hit here when
  1806. // components are added/removed via the Level Inspector.
  1807. while (parentEntityId.IsValid() && parentEntityId != levelEntityId)
  1808. {
  1809. auto it = m_dockWidgetsByEntity.find(parentEntityId);
  1810. if (it != m_dockWidgetsByEntity.end())
  1811. {
  1812. const GraphCanvas::DockWidgetId& dockWidgetId = it->second;
  1813. GraphCanvas::EditorDockWidgetRequestBus::EventResult(graphId, dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::GetGraphId);
  1814. break;
  1815. }
  1816. else
  1817. {
  1818. AZ::EntityId previousParentEntityId = parentEntityId;
  1819. AzToolsFramework::EditorEntityInfoRequestBus::EventResult(parentEntityId, parentEntityId, &AzToolsFramework::EditorEntityInfoRequestBus::Events::GetParent);
  1820. // Prevent infinite loop if the GetParent ends up returning itself, which could happen in a case where a slice is in
  1821. // the process of being restored and this logic gets invoked.
  1822. if (previousParentEntityId == parentEntityId)
  1823. {
  1824. AZ_Assert(false, "Corrupt parent hierarchy - entity parent ID is set to itself, breaking here to prevent infinite loop.");
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. return graphId;
  1830. }
  1831. void MainWindow::EnumerateEntityComponentTree(const AZ::EntityId& rootEntityId, EntityComponentCallback callback)
  1832. {
  1833. // Retrieve the entity hierarchy for our root entity
  1834. AzToolsFramework::EntityIdList children;
  1835. children.push_back(rootEntityId);
  1836. GetChildrenTree(rootEntityId, children);
  1837. // Iterate through our entity hierarchy and invoke our callback on all
  1838. // components that are found (both enabled and disabled)
  1839. for (auto entityId : children)
  1840. {
  1841. AZ::Entity* entity = nullptr;
  1842. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  1843. if (!entity)
  1844. {
  1845. continue;
  1846. }
  1847. // Retrieve the enabled components on our Entity
  1848. AZ::Entity::ComponentArrayType components = entity->GetComponents();
  1849. for (AZ::Component* component : components)
  1850. {
  1851. callback(entityId, component, false);
  1852. }
  1853. // If there are any disabled components on our Entity, we need to
  1854. // retrieve them separately because they won't show up with Entity::GetComponents()
  1855. AZ::Entity::ComponentArrayType disabledComponents;
  1856. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(entityId, &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  1857. for (AZ::Component* disabledComponent : disabledComponents)
  1858. {
  1859. callback(entityId, disabledComponent, true);
  1860. }
  1861. }
  1862. }
  1863. void MainWindow::InitialEntityGraph(const AZ::EntityId& entityId, GraphCanvas::GraphId graphId)
  1864. {
  1865. // Keep track of our node points for creating a better default node layout
  1866. NodePoint* rootPoint = new NodePoint();
  1867. AZStd::unordered_map<AZ::EntityId, AZStd::vector<NodePoint*>> nodePointMap;
  1868. // Keep track of any node wrappings we will need to setup after the nodes
  1869. // have been added to the graph
  1870. AZStd::unordered_map<AZ::EntityId, GraphModel::NodePtrList> nodeWrappings;
  1871. // We don't need to cache a mapping of the area extenders since they don't have
  1872. // output slots that connect to other nodes
  1873. AZStd::vector<LandscapeCanvas::BaseNode::BaseNodePtr> areaExtenders;
  1874. // Iterate through our entity hierarchy to look for components that
  1875. // correspond with nodes we know how to graph
  1876. GraphModel::NodePtrList disabledNodes;
  1877. GraphModel::GraphPtr graph = GetGraphById(graphId);
  1878. EnumerateEntityComponentTree(entityId, [this, graph, graphId, rootPoint, &nodePointMap, &nodeWrappings, &areaExtenders, &disabledNodes](const AZ::EntityId& entityId, AZ::Component* component, bool isDisabled) {
  1879. const AZ::TypeId& componentTypeId = component->RTTI_GetType();
  1880. // Create the node for the given component type.
  1881. // If we don't support a node for this component type, it will just return nullptr.
  1882. LandscapeCanvas::BaseNode::BaseNodePtr node;
  1883. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(node, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::CreateNodeForType, graph, componentTypeId);
  1884. // Set the EntityId for the vegetation entity corresponding to this node (if we found one)
  1885. if (node)
  1886. {
  1887. node->SetVegetationEntityId(entityId);
  1888. node->SetComponentId(component->GetId());
  1889. // Update the node mappings we need to cache for this node
  1890. UpdateEntityIdNodeMap(graphId, node);
  1891. // Keep track of which nodes came from disabled components so that we can disable
  1892. // those nodes once they are added to the graph
  1893. if (isDisabled)
  1894. {
  1895. disabledNodes.push_back(node);
  1896. }
  1897. // Keep track locally of our area extenders so we can parse them later
  1898. LandscapeCanvas::BaseNode::BaseNodeType baseNodeType = node->GetBaseNodeType();
  1899. switch (baseNodeType)
  1900. {
  1901. case LandscapeCanvas::BaseNode::TerrainExtender:
  1902. case LandscapeCanvas::BaseNode::VegetationAreaFilter:
  1903. case LandscapeCanvas::BaseNode::VegetationAreaModifier:
  1904. case LandscapeCanvas::BaseNode::VegetationAreaSelector:
  1905. areaExtenders.push_back(node);
  1906. break;
  1907. }
  1908. // If this node is meant to be wrapped on a WrapperNode, then
  1909. // add it to the node wrappings so we can wrap it later after
  1910. // the nodes have been added to the graph
  1911. if (node->IsAreaExtender())
  1912. {
  1913. auto nodeWrapIt = nodeWrappings.find(entityId);
  1914. if (nodeWrapIt == nodeWrappings.end())
  1915. {
  1916. nodeWrappings[entityId] = { node };
  1917. }
  1918. else
  1919. {
  1920. nodeWrappings[entityId].push_back(node);
  1921. }
  1922. }
  1923. // Otherwise, create a new node point for this general node and just place it as a child on our root
  1924. else
  1925. {
  1926. NodePoint* point = new NodePoint();
  1927. point->node = node;
  1928. point->vegetationEntityId = entityId;
  1929. point->parent = rootPoint;
  1930. rootPoint->children.push_back(point);
  1931. nodePointMap[entityId].push_back(point);
  1932. }
  1933. }
  1934. });
  1935. // Find connections between nodes. Save the corresponding node for the slot in a pair, because
  1936. // we can't retrieve the parent node from the Slot until the node has been added to the graph, but we need
  1937. // to match based on that data to place nodes near eachother that have slots connected.
  1938. ConnectionsList connections;
  1939. const EntityIdNodeMaps& nodeMaps = m_entityIdNodeMapsByGraph[graphId];
  1940. for (auto nodeType : { EntityIdNodeMapEnum::Gradients, EntityIdNodeMapEnum::WrapperNodes })
  1941. {
  1942. const EntityIdNodeMap& nodeMap = nodeMaps[nodeType];
  1943. for (const auto& it : nodeMap)
  1944. {
  1945. GraphModel::NodePtr node = it.second;
  1946. ParseNodeConnections(graphId, node, connections);
  1947. }
  1948. }
  1949. for (const auto& it : areaExtenders)
  1950. {
  1951. ParseNodeConnections(graphId, it, connections);
  1952. }
  1953. // Use the connections between nodes to setup the node point tree so
  1954. // that nodes that are connected together are:
  1955. // 1. Placed near eachother
  1956. // 2. Target nodes are placed to the right of the source node
  1957. // When the node points are created, they are all placed as children on
  1958. // a dummy root node point, so any nodes that don't have connections will
  1959. // be placed at the bottom in a vertical column. The tree is connection type
  1960. // agnostic, so it doesn't matter whether a Shape is connected to a Gradient,
  1961. // or a Gradient is connecte to a Gradient Modifier, any nodes that are connected
  1962. // will be placed in a left to right flow, and also handles if one node has multiple
  1963. // output slots connected to multiple nodes. As we continue to add support for more
  1964. // connections, they will automatically be handled by this logic.
  1965. for (auto it : connections)
  1966. {
  1967. GraphModel::NodePtr sourceNode = it.first.first;
  1968. GraphModel::NodePtr targetNode = it.second.first;
  1969. auto sourceBaseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(sourceNode.get());
  1970. auto targetBaseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  1971. AZ::EntityId sourceEntityId = sourceBaseNodePtr->GetVegetationEntityId();
  1972. AZ::EntityId targetEntityId = targetBaseNodePtr->GetVegetationEntityId();
  1973. // Find the source and target NodePoints from the map. There may be multiple
  1974. // NodePoints for a single Vegetation EntityId in the case where multiple
  1975. // components are on the same Entity, so if there's more than one entry
  1976. // we need to search and match based on the NodePtr.
  1977. auto sourcePoints = nodePointMap[sourceEntityId];
  1978. auto targetPoints = nodePointMap[targetEntityId];
  1979. NodePoint* sourcePoint = sourcePoints.size() == 1 ? sourcePoints[0] : FindNodePoint(sourcePoints, nodeWrappings, sourceNode);
  1980. NodePoint* targetPoint = targetPoints.size() == 1 ? targetPoints[0] : FindNodePoint(targetPoints, nodeWrappings, targetNode);
  1981. if (!sourcePoint || !targetPoint)
  1982. {
  1983. AZ_Error(graph->GetSystemName(), false, "Invalid source or target point connection");
  1984. continue;
  1985. }
  1986. // Add this target node as one of the children from the source node
  1987. sourcePoint->children.push_back(targetPoint);
  1988. // If the target already had a parent, remove it as a child
  1989. if (targetPoint->parent)
  1990. {
  1991. NodePoint* parentPoint = targetPoint->parent;
  1992. auto iter = AZStd::find(parentPoint->children.begin(), parentPoint->children.end(), targetPoint);
  1993. if (iter != parentPoint->children.end())
  1994. {
  1995. parentPoint->children.erase(iter);
  1996. }
  1997. }
  1998. // Then set the new parent for our target
  1999. targetPoint->parent = sourcePoint;
  2000. }
  2001. // Place the nodes in a tree layout grouped by their connections
  2002. AZ::Vector2 gridMajorPitch;
  2003. GraphModelIntegration::GraphControllerRequestBus::EventResult(gridMajorPitch, graphId, &GraphModelIntegration::GraphControllerRequests::GetMajorPitch);
  2004. PlaceNodes(graphId, rootPoint, gridMajorPitch);
  2005. // Setup the node wrappings now that the nodes have been placed in the graph
  2006. for (auto it : nodeWrappings)
  2007. {
  2008. const AZ::EntityId& wrapperNodeEntityId = it.first;
  2009. auto nodePointIt = nodePointMap.find(wrapperNodeEntityId);
  2010. if (nodePointIt == nodePointMap.end())
  2011. {
  2012. continue;
  2013. }
  2014. // Find the wrapper node for this EntityId. There could be multiple nodes with the same
  2015. // EntityId (e.g. box shapes), but there can't be multiple wrapper nodes on the same Entity.
  2016. GraphModel::NodePtr wrapperNode = nullptr;
  2017. for (auto nodePoint : nodePointIt->second)
  2018. {
  2019. if (nodePoint->node->GetNodeType() == GraphModel::NodeType::WrapperNode)
  2020. {
  2021. wrapperNode = nodePoint->node;
  2022. break;
  2023. }
  2024. }
  2025. GraphModel::NodePtrList wrappedNodes = it.second;
  2026. for (GraphModel::NodePtr node : wrappedNodes)
  2027. {
  2028. // Wrap the node using its preferred layout order (if it has one)
  2029. AZ::u32 layoutOrder = GetWrappedNodeLayoutOrder(node);
  2030. if (layoutOrder != GraphModel::DefaultWrappedNodeLayoutOrder)
  2031. {
  2032. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::WrapNodeOrdered, wrapperNode, node, layoutOrder);
  2033. }
  2034. else
  2035. {
  2036. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::WrapNode, wrapperNode, node);
  2037. }
  2038. }
  2039. }
  2040. // Delete the node points now that we've completed placing the nodes
  2041. delete rootPoint;
  2042. for (auto it : nodePointMap)
  2043. {
  2044. for (auto pointIt : it.second)
  2045. {
  2046. delete pointIt;
  2047. }
  2048. }
  2049. // Disable any nodes that came from disabled components now that they've all been added to the graph
  2050. for (auto node : disabledNodes)
  2051. {
  2052. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  2053. }
  2054. // Create the connections now, after placing the nodes, since the
  2055. // connection data is used for appropriate node placement
  2056. for (auto it : connections)
  2057. {
  2058. GraphModel::SlotPtr sourceSlot = it.first.second;
  2059. GraphModel::SlotPtr targetSlot = it.second.second;
  2060. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::AddConnection, sourceSlot, targetSlot);
  2061. }
  2062. }
  2063. GraphModel::NodePtrList MainWindow::RefreshEntityComponentNodes(const AZ::EntityId& targetEntityId, GraphCanvas::GraphId graphId)
  2064. {
  2065. GraphModel::GraphPtr graph = GetGraphById(graphId);
  2066. GraphModel::NodePtrList loadedNodes, disabledNodes, createdNodes;
  2067. GraphModelIntegration::GraphControllerRequestBus::EventResult(loadedNodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  2068. EnumerateEntityComponentTree(
  2069. targetEntityId,
  2070. [this, graph, graphId, &loadedNodes, &disabledNodes, &createdNodes](
  2071. const AZ::EntityId& entityId, AZ::Component* component, bool isDisabled)
  2072. {
  2073. bool foundMatch = false;
  2074. GraphModel::NodePtr validNode = nullptr;
  2075. // Check if this component matches a node that was already loaded in the graph
  2076. for (auto it = loadedNodes.begin(); it != loadedNodes.end(); ++it)
  2077. {
  2078. GraphModel::NodePtr node = *it;
  2079. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2080. AZ::ComponentId componentId = component->GetId();
  2081. if (entityId == baseNodePtr->GetVegetationEntityId() && componentId == baseNodePtr->GetComponentId())
  2082. {
  2083. foundMatch = true;
  2084. validNode = node;
  2085. // Erase this from our list of loaded nodes so that we know we found its match
  2086. // After we iterate through the Entity/Component tree, anything left in loadedNodes
  2087. // will represent saved nodes that no longer have a corresponding Entity/Component in the level
  2088. loadedNodes.erase(it);
  2089. break;
  2090. }
  2091. }
  2092. // If we didn't find a match for this component, check if this is a newly added component we need to
  2093. // create a node for
  2094. if (!foundMatch)
  2095. {
  2096. const AZ::TypeId& componentTypeId = component->RTTI_GetType();
  2097. // Try to create the node for the given component type.
  2098. // If we don't support a node for this component type, it will just return nullptr.
  2099. LandscapeCanvas::BaseNode::BaseNodePtr node;
  2100. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(node, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::CreateNodeForType, graph, componentTypeId);
  2101. if (node)
  2102. {
  2103. validNode = node;
  2104. createdNodes.push_back(node);
  2105. node->SetVegetationEntityId(entityId);
  2106. node->SetComponentId(component->GetId());
  2107. PlaceNewNode(graphId, node);
  2108. }
  2109. }
  2110. if (validNode)
  2111. {
  2112. if (isDisabled)
  2113. {
  2114. disabledNodes.push_back(validNode);
  2115. }
  2116. // Update the node mappings we need to cache for this node
  2117. UpdateEntityIdNodeMap(graphId, validNode);
  2118. }
  2119. });
  2120. // Disable any nodes that came from disabled components now that they've all been added to the graph
  2121. for (auto node : disabledNodes)
  2122. {
  2123. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  2124. }
  2125. // Anything left in 'loadedNodes' at this point after the enumerate is done can be
  2126. // deleted if we were refreshing the the root Entity for this graph, since that means
  2127. // there's no longer an existing component matching it
  2128. if (targetEntityId == GetRootEntityIdForGraphId(graphId))
  2129. {
  2130. for (auto node : loadedNodes)
  2131. {
  2132. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::RemoveNode, node);
  2133. }
  2134. }
  2135. return createdNodes;
  2136. }
  2137. void MainWindow::OnEntityComponentAdded(const AZ::EntityId& entityId, const AZ::ComponentId& componentId)
  2138. {
  2139. if (m_ignoreGraphUpdates)
  2140. {
  2141. return;
  2142. }
  2143. // Try to find an open graph whose root Entity contains the Entity which this component was added to
  2144. GraphCanvas::GraphId graphId = FindGraphContainingEntity(entityId);
  2145. if (!graphId.IsValid())
  2146. {
  2147. return;
  2148. }
  2149. // When OnEntityComponentAdded is called, the component won't be accessible by Entity::FindComponent yet, it will still
  2150. // be pending even whether it is disabled or not
  2151. AZ::Component* component = nullptr;
  2152. AZ::Entity::ComponentArrayType pendingComponents;
  2153. AzToolsFramework::EditorPendingCompositionRequestBus::Event(entityId, &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  2154. for (AZ::Component* pendingComponent : pendingComponents)
  2155. {
  2156. if (pendingComponent->GetId() == componentId)
  2157. {
  2158. component = pendingComponent;
  2159. break;
  2160. }
  2161. }
  2162. if (!component)
  2163. {
  2164. return;
  2165. }
  2166. // Create the node for the given component type.
  2167. // If we don't support a node for this component type, it will just return nullptr.
  2168. LandscapeCanvas::BaseNode::BaseNodePtr node;
  2169. GraphModel::GraphPtr graph = GetGraphById(graphId);
  2170. const AZ::TypeId& componentTypeId = component->RTTI_GetType();
  2171. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(node, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::CreateNodeForType, graph, componentTypeId);
  2172. if (!node)
  2173. {
  2174. return;
  2175. }
  2176. // Set the EntityId for the vegetation entity corresponding to this node (if we found one)
  2177. node->SetVegetationEntityId(entityId);
  2178. node->SetComponentId(componentId);
  2179. // Update the node mappings we need to cache for this node and parse any connections that it may have setup already
  2180. UpdateEntityIdNodeMap(graphId, node);
  2181. ConnectionsList connections;
  2182. ParseNodeConnections(graphId, node, connections);
  2183. m_ignoreGraphUpdates = true;
  2184. // Add the node to the graph, either wrapped on its parent or just in the scene if it's standalone
  2185. PlaceNewNode(graphId, node);
  2186. // Disable this node for now since it's pending when OnEntityComponentAdded is called, it will be enabled
  2187. // after if it becomes enabled
  2188. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  2189. // Create connections if any exist (e.g. if a component was copied/pasted with existing configuration)
  2190. for (auto it : connections)
  2191. {
  2192. GraphModel::SlotPtr sourceSlot = it.first.second;
  2193. GraphModel::SlotPtr targetSlot = it.second.second;
  2194. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::AddConnection, sourceSlot, targetSlot);
  2195. }
  2196. m_ignoreGraphUpdates = false;
  2197. // As mentioned earlier, the component added when OnEntityComponentAdded is called is still pending currently,
  2198. // so we need to delay checking until after this event is invoked to see if the component was enabled
  2199. QTimer::singleShot(0, [this, entityId, componentId, graphId, node]() {
  2200. AZ::Entity* entity = nullptr;
  2201. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  2202. if (!entity)
  2203. {
  2204. return;
  2205. }
  2206. AZ::Component* component = entity->FindComponent(componentId);
  2207. if (component)
  2208. {
  2209. // If FindComponent succeeds, then the component has been enabled
  2210. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::EnableNode, node);
  2211. // Also check if any other previously deactivated (pending) components on this same Entity were activated
  2212. // when this new component was added (e.g. a random noise gradient component being activated once the
  2213. // gradient transform modifier and shape are added)
  2214. auto nodeMapsIt = m_entityIdNodeMapsByGraph.find(graphId);
  2215. if (nodeMapsIt != m_entityIdNodeMapsByGraph.end())
  2216. {
  2217. const EntityIdNodeMaps& nodeMaps = nodeMapsIt->second;
  2218. for (int i = 0; i < EntityIdNodeMapEnum::Count; ++i)
  2219. {
  2220. const auto& nodeMap = nodeMaps[i];
  2221. auto it = nodeMap.find(entityId);
  2222. if (it != nodeMap.end())
  2223. {
  2224. GraphModel::NodePtr cachedNode = it->second;
  2225. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(cachedNode.get());
  2226. // Ignore node matching the same componentId as the component that was directly added
  2227. // If the GetComponent() method returns a valid pointer, it means the component is enabled now
  2228. if ((baseNodePtr->GetComponentId() != componentId) && baseNodePtr->GetComponent())
  2229. {
  2230. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::EnableNode, cachedNode);
  2231. }
  2232. }
  2233. }
  2234. }
  2235. }
  2236. });
  2237. }
  2238. void MainWindow::PlaceNewNode(GraphCanvas::GraphId graphId, LandscapeCanvas::BaseNode::BaseNodePtr node)
  2239. {
  2240. // If this is an extender node, then we need to wrap it to its parent node
  2241. if (node->IsAreaExtender())
  2242. {
  2243. auto nodeMapsIt = m_entityIdNodeMapsByGraph.find(graphId);
  2244. if (nodeMapsIt == m_entityIdNodeMapsByGraph.end())
  2245. {
  2246. return;
  2247. }
  2248. const EntityIdNodeMaps& nodeMaps = nodeMapsIt->second;
  2249. const auto& wrapperNodeMap = nodeMaps[EntityIdNodeMapEnum::WrapperNodes];
  2250. auto it = wrapperNodeMap.find(node->GetVegetationEntityId());
  2251. if (it != wrapperNodeMap.end())
  2252. {
  2253. GraphModel::NodePtr wrapperNode = it->second;
  2254. AZ::u32 layoutOrder = GetWrappedNodeLayoutOrder(node);
  2255. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::WrapNodeOrdered, wrapperNode, node, layoutOrder);
  2256. // Some nodes could be wrapped or free floating, so if this was a wrapped node, we can stop now
  2257. // Otherwise, we need to fall-through and just place it in the graph
  2258. return;
  2259. }
  2260. }
  2261. // If we aren't placing a wrapped node, then just add it to the graph
  2262. AZ::Vector2 nodePosition = AZ::Vector2::CreateZero();
  2263. auto it = m_deletedNodePositions.find(graphId);
  2264. if (it != m_deletedNodePositions.end())
  2265. {
  2266. // Check if there was a saved position from a previous node with matching Entity/Component pair
  2267. // that had been previously deleted, so that we can handle Undo/Redo placing the re-created
  2268. // node back in the same position
  2269. const DeletedNodePositionsMap& deletedNodePositionMap = it->second;
  2270. AZ::EntityComponentIdPair pair(node->GetVegetationEntityId(), node->GetComponentId());
  2271. auto deletedPositionIt = deletedNodePositionMap.find(pair);
  2272. if (deletedPositionIt != deletedNodePositionMap.end())
  2273. {
  2274. nodePosition = deletedPositionIt->second;
  2275. }
  2276. // Otherwise, this really is a new node, so place it outside the top-left edge of the bounds of all nodes in the scene
  2277. else
  2278. {
  2279. QRectF sceneArea;
  2280. GraphCanvas::SceneRequestBus::EventResult(sceneArea, graphId, &GraphCanvas::SceneRequests::GetSceneBoundingArea);
  2281. nodePosition = AZ::Vector2(aznumeric_cast<float>(sceneArea.right()) + NODE_OFFSET_X_PIXELS, aznumeric_cast<float>(sceneArea.top()));
  2282. }
  2283. }
  2284. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::AddNode, node, nodePosition);
  2285. }
  2286. void MainWindow::OnEntityComponentRemoved(const AZ::EntityId& entityId, const AZ::ComponentId& componentId)
  2287. {
  2288. if (m_ignoreGraphUpdates)
  2289. {
  2290. return;
  2291. }
  2292. m_ignoreGraphUpdates = true;
  2293. for (GraphCanvas::GraphId graphId : GetOpenGraphIds())
  2294. {
  2295. GraphModel::NodePtrList nodes;
  2296. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodes, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodes);
  2297. for (auto node : nodes)
  2298. {
  2299. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2300. if (baseNodePtr->GetVegetationEntityId() == entityId && baseNodePtr->GetComponentId() == componentId)
  2301. {
  2302. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::RemoveNode, node);
  2303. break;
  2304. }
  2305. }
  2306. }
  2307. m_ignoreGraphUpdates = false;
  2308. }
  2309. void MainWindow::OnEntityComponentEnabled(const AZ::EntityId& entityId, const AZ::ComponentId& componentId)
  2310. {
  2311. AZ::EntityComponentIdPair entityComponentId(entityId, componentId);
  2312. GraphModel::NodePtrList matchingNodes = GetAllNodesMatchingEntityComponent(entityComponentId);
  2313. for (auto node : matchingNodes)
  2314. {
  2315. GraphCanvas::GraphId graphId = GetGraphId(node->GetGraph());
  2316. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::EnableNode, node);
  2317. }
  2318. }
  2319. void MainWindow::OnEntityComponentDisabled(const AZ::EntityId& entityId, const AZ::ComponentId& componentId)
  2320. {
  2321. AZ::EntityComponentIdPair entityComponentId(entityId, componentId);
  2322. GraphModel::NodePtrList matchingNodes = GetAllNodesMatchingEntityComponent(entityComponentId);
  2323. for (auto node : matchingNodes)
  2324. {
  2325. GraphCanvas::GraphId graphId = GetGraphId(node->GetGraph());
  2326. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  2327. }
  2328. }
  2329. void MainWindow::OnEntityComponentPropertyChanged(AZ::ComponentId changedComponentId)
  2330. {
  2331. AZ_UNUSED(changedComponentId);
  2332. const AZ::EntityId changedEntityId = *AzToolsFramework::PropertyEditorEntityChangeNotificationBus::GetCurrentBusId();
  2333. auto ignoreIt = AZStd::find(m_ignoreEntityComponentPropertyChanges.begin(), m_ignoreEntityComponentPropertyChanges.end(), changedEntityId);
  2334. if (ignoreIt != m_ignoreEntityComponentPropertyChanges.end())
  2335. {
  2336. return;
  2337. }
  2338. GraphModel::NodePtrList matchingNodes = GetAllNodesMatchingEntity(changedEntityId);
  2339. for (auto node : matchingNodes)
  2340. {
  2341. // Re-parse any input connections for this node to add/remove any connections
  2342. // that might've been modified when the component/property was changed
  2343. UpdateConnections(node);
  2344. }
  2345. }
  2346. void MainWindow::EntityParentChanged(AZ::EntityId entityId, AZ::EntityId newParentId, AZ::EntityId oldParentId)
  2347. {
  2348. if (m_prefabPropagationInProgress)
  2349. {
  2350. return;
  2351. }
  2352. GraphCanvas::GraphId oldGraphId = FindGraphContainingEntity(oldParentId);
  2353. GraphCanvas::GraphId newGraphId = FindGraphContainingEntity(newParentId);
  2354. // If the Entity is being re-parented but still inside the same graph, then we don't need to do anything
  2355. // This will also trigger if the Entity isn't in a currently open graph, in which case we can also ignore
  2356. if (newGraphId == oldGraphId)
  2357. {
  2358. return;
  2359. }
  2360. // If there is an open graph for the previous parent, then treat this like the Entity being deleted
  2361. if (oldGraphId.IsValid())
  2362. {
  2363. HandleEditorEntityDeleted(entityId);
  2364. }
  2365. // If there is an open graph for the new parent, then treat this like an Entity being created
  2366. if (newGraphId.IsValid())
  2367. {
  2368. // We need to pass in the new graphId for the new parentEntity because when EntityParentChanged
  2369. // is invoked, the EditorEntityInfoRequestBus::Events::GetParent (that is used by FindGraphContainingEntity)
  2370. // will still return the old parentId
  2371. HandleEditorEntityCreated(entityId, newGraphId);
  2372. }
  2373. }
  2374. void MainWindow::OnPrefabFocusChanged(
  2375. [[maybe_unused]] AZ::EntityId previousContainerEntityId, [[maybe_unused]] AZ::EntityId newContainerEntityId)
  2376. {
  2377. // Make sure to close any open graphs that aren't currently in prefab focus
  2378. // to prevent the user from making modifications outside of the allowed focus scope
  2379. AZStd::vector<GraphCanvas::DockWidgetId> dockWidgetsToClose;
  2380. for (auto [entityId, dockWidgetId] : m_dockWidgetsByEntity)
  2381. {
  2382. if (!m_prefabFocusPublicInterface->IsOwningPrefabBeingFocused(entityId))
  2383. {
  2384. dockWidgetsToClose.push_back(dockWidgetId);
  2385. }
  2386. }
  2387. for (auto dockWidgetId : dockWidgetsToClose)
  2388. {
  2389. CloseEditor(dockWidgetId);
  2390. }
  2391. }
  2392. void MainWindow::OnPrefabInstancePropagationBegin()
  2393. {
  2394. // Ignore graph updates during prefab propagation because the entities will be
  2395. // deleted and re-created, which would inadvertantly trigger our logic to close
  2396. // the graph when the corresponding entity is deleted.
  2397. m_prefabPropagationInProgress = true;
  2398. }
  2399. void MainWindow::OnPrefabInstancePropagationEnd()
  2400. {
  2401. // See comment above in OnPrefabInstancePropagationBegin
  2402. m_prefabPropagationInProgress = false;
  2403. // Clear our list of EntityIds to ignore component property change notifications
  2404. // from since the prefab propagation has completed
  2405. m_ignoreEntityComponentPropertyChanges.clear();
  2406. // After prefab propagation is complete, the entity tied to one of our open
  2407. // graphs might have been deleted (e.g. if a prefab was created from that entity).
  2408. // Any open graphs tied to an entity that no longer exists will need to be closed.
  2409. // We need to close them in a separate iterator because the CloseEditor API will
  2410. // end up modifying m_dockWidgetsByEntity.
  2411. AZStd::vector<GraphCanvas::DockWidgetId> dockWidgetsToDelete;
  2412. for (auto [entityId, dockWidgetId] : m_dockWidgetsByEntity)
  2413. {
  2414. AZ::Entity* entity = nullptr;
  2415. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  2416. if (!entity)
  2417. {
  2418. dockWidgetsToDelete.push_back(dockWidgetId);
  2419. }
  2420. }
  2421. for (auto dockWidgetId : dockWidgetsToDelete)
  2422. {
  2423. CloseEditor(dockWidgetId);
  2424. }
  2425. // Handle any nodes that might've been created by duplicated/pasted entities
  2426. // once the prefab propagation has finished
  2427. HandleDeserializedNodes();
  2428. // Handle any queued entities that we need to refresh by calling
  2429. // HandleEditorEntityCreated, which will handle if there is anything
  2430. // out of sync in the graph based on the corresponding entity.
  2431. for (const auto& entityId : m_queuedEntityRefresh)
  2432. {
  2433. HandleEditorEntityCreated(entityId);
  2434. }
  2435. m_queuedEntityRefresh.clear();
  2436. }
  2437. void MainWindow::OnCryEditorEndCreate()
  2438. {
  2439. UpdateGraphEnabled();
  2440. }
  2441. void MainWindow::OnCryEditorEndLoad()
  2442. {
  2443. UpdateGraphEnabled();
  2444. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusConnect();
  2445. }
  2446. void MainWindow::OnCryEditorCloseScene()
  2447. {
  2448. UpdateGraphEnabled();
  2449. AzToolsFramework::EditorEntityContextNotificationBus::Handler::BusDisconnect();
  2450. }
  2451. void MainWindow::OnCryEditorSceneClosed()
  2452. {
  2453. UpdateGraphEnabled();
  2454. // Close all the open editor graphs when the level is closed, and stop listening
  2455. // for Editor Entity property changes since our graphs are tied to the level data
  2456. CloseAllEditors();
  2457. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::MultiHandler::BusDisconnect();
  2458. }
  2459. void MainWindow::UpdateGraphEnabled()
  2460. {
  2461. bool isLevelLoaded = GetLegacyEditor()->IsLevelLoaded();
  2462. // Disable being able to drag from the node palette to the empty dock window
  2463. // to create a new graph when a level isn't loaded
  2464. GetCentralDockWindow()->GetEmptyDockWidget()->setAcceptDrops(isLevelLoaded);
  2465. // Disable the new graph menu action when no level is loaded
  2466. if (m_fileNewAction)
  2467. {
  2468. m_fileNewAction->setEnabled(isLevelLoaded);
  2469. }
  2470. // Extra safety check to prevent our tool from creating Entities if a node is added to a graph
  2471. // This in theory shouldn't be hit since we are preventing new graphs from being created
  2472. // in the first place, but is just an extra precaution
  2473. m_ignoreGraphUpdates = !isLevelLoaded;
  2474. }
  2475. void MainWindow::PostOnActiveGraphChanged()
  2476. {
  2477. // Update our selection in our custom Node Inspector when the active graph changes
  2478. OnSelectionChanged();
  2479. }
  2480. AZ::u32 MainWindow::GetWrappedNodeLayoutOrder(GraphModel::NodePtr node)
  2481. {
  2482. AZ::u32 layoutOrder = GraphModel::DefaultWrappedNodeLayoutOrder;
  2483. if (!node)
  2484. {
  2485. return layoutOrder;
  2486. }
  2487. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2488. if (!baseNodePtr)
  2489. {
  2490. return layoutOrder;
  2491. }
  2492. // Find the layout order for the wrapped node
  2493. int index = -1;
  2494. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(index, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::GetNodeRegisteredIndex, baseNodePtr->RTTI_GetType());
  2495. if (index != -1)
  2496. {
  2497. return index;
  2498. }
  2499. return layoutOrder;
  2500. }
  2501. AZ::EntityId MainWindow::GetRootEntityIdForGraphId(const GraphCanvas::GraphId& graphId)
  2502. {
  2503. for (const auto& pair : m_dockWidgetsByEntity)
  2504. {
  2505. const GraphCanvas::DockWidgetId& dockWidgetId = pair.second;
  2506. GraphCanvas::GraphId dockGraphId;
  2507. GraphCanvas::EditorDockWidgetRequestBus::EventResult(dockGraphId, dockWidgetId, &GraphCanvas::EditorDockWidgetRequests::GetGraphId);
  2508. if (dockGraphId == graphId)
  2509. {
  2510. return pair.first;
  2511. }
  2512. }
  2513. return AZ::EntityId();
  2514. }
  2515. AZ::ComponentId MainWindow::AddComponentTypeIdToEntity(
  2516. const AZ::EntityId& entityId, AZ::TypeId componentToAddTypeId, AZStd::span<const AZ::ComponentServiceType> optionalServices)
  2517. {
  2518. using namespace AzToolsFramework;
  2519. // Cache the original m_ignoreGraphUpdates so we can restore it later
  2520. bool originalIgnoreGraphUpdates = m_ignoreGraphUpdates;
  2521. // Add the corresponding Component for this node to its representative Entity,
  2522. // and any required Components it may need by keeping track of any missing required
  2523. // services that are reported when the Component(s) are added
  2524. // Initialize our list of missing required services with any optional services this component needs
  2525. AZ::ComponentDescriptor::DependencyArrayType missingRequiredServices(optionalServices.begin(), optionalServices.end());
  2526. AZ::ComponentId requestedComponentId = AZ::InvalidComponentId;
  2527. do
  2528. {
  2529. AZ::ComponentDescriptor* componentDescriptor = nullptr;
  2530. AZ::ComponentDescriptorBus::EventResult(componentDescriptor, componentToAddTypeId, &AZ::ComponentDescriptor::GetDescriptor);
  2531. AZ_Assert(componentDescriptor, "Unable to find ComponentDescriptor for %s.", componentToAddTypeId.ToString<AZStd::string>().c_str());
  2532. // Find what (if any) services are provided by the Component we are about to add,
  2533. // and remove them from the list of missing required services are are tracking
  2534. AZ::ComponentDescriptor::DependencyArrayType providedServices;
  2535. componentDescriptor->GetProvidedServices(providedServices, nullptr);
  2536. for (const auto& service : providedServices)
  2537. {
  2538. auto it = AZStd::find(missingRequiredServices.begin(), missingRequiredServices.end(), service);
  2539. if (it != missingRequiredServices.end())
  2540. {
  2541. missingRequiredServices.erase(it);
  2542. }
  2543. }
  2544. // Add the Component to the Vegetation Entity
  2545. EntityCompositionRequests::AddComponentsOutcome outcome = AZ::Failure(AZStd::string());
  2546. EntityCompositionRequestBus::BroadcastResult(outcome, &EntityCompositionRequests::AddComponentsToEntities, EntityIdList{ entityId }, AZ::ComponentTypeList{ componentToAddTypeId });
  2547. AZ_Assert(outcome.IsSuccess(), "Failed to add component %s", componentToAddTypeId.ToString<AZStd::string>().c_str());
  2548. // Capture the ComponentId for the original component type that was requested to be added
  2549. if (requestedComponentId == AZ::InvalidComponentId)
  2550. {
  2551. const AZ::Entity::ComponentArrayType& componentsAdded = outcome.GetValue()[entityId].m_componentsAdded;
  2552. AZ_Assert(!componentsAdded.empty(), "Failed to add component %s", componentToAddTypeId.ToString<AZStd::string>().c_str());
  2553. requestedComponentId = componentsAdded.front()->GetId();
  2554. }
  2555. // After the Component has been added, check if it is missing any required services
  2556. // by checking the m_addedPendingComponents property in the outcome, which means
  2557. // the Component was added to the Entity, but is missing one or more required services.
  2558. // If m_addedPendingComponents is empty, then that means the Component was added
  2559. // with no issues, so we can continue.
  2560. const AZ::Entity::ComponentArrayType& pendingComponents = outcome.GetValue()[entityId].m_addedPendingComponents;
  2561. if (!pendingComponents.empty())
  2562. {
  2563. AZ::Component* component = pendingComponents.front();
  2564. // Find the missing required services for the pending Component,
  2565. // and them to our list (if it wasn't in the list already).
  2566. EntityCompositionRequests::PendingComponentInfo pendingComponentInfo;
  2567. EntityCompositionRequestBus::BroadcastResult(pendingComponentInfo, &EntityCompositionRequests::GetPendingComponentInfo, component);
  2568. for (const auto& service : pendingComponentInfo.m_missingRequiredServices)
  2569. {
  2570. if (AZStd::find(missingRequiredServices.begin(), missingRequiredServices.end(), service) == missingRequiredServices.end())
  2571. {
  2572. missingRequiredServices.push_back(service);
  2573. }
  2574. }
  2575. // Disable any components that are incompatible with the component we have added
  2576. if (!pendingComponentInfo.m_validComponentsThatAreIncompatible.empty())
  2577. {
  2578. AzToolsFramework::EntityCompositionRequestBus::Broadcast(&AzToolsFramework::EntityCompositionRequests::DisableComponents, pendingComponentInfo.m_validComponentsThatAreIncompatible);
  2579. }
  2580. }
  2581. // If we are missing any required services, use the ComponentPaletteUtil::ComponentDataTable to find
  2582. // what components will satisfy them, then choose one to be added and repeat the loop so we can find
  2583. // any additional required services that Component may need
  2584. if (!missingRequiredServices.empty())
  2585. {
  2586. ComponentPaletteUtil::ComponentDataTable componentDataTable;
  2587. ComponentPaletteUtil::ComponentIconTable componentIconTable;
  2588. ComponentPaletteUtil::BuildComponentTables(m_serializeContext, AppearsInGameComponentMenu, missingRequiredServices, componentDataTable, componentIconTable);
  2589. AZ_Assert(componentDataTable.size(), "No components found that satisfy the missing required service(s).");
  2590. componentToAddTypeId = PickComponentTypeIdToAdd(componentDataTable);
  2591. }
  2592. // After adding the first component, re-enable listening to graph updates
  2593. // This handles the case where we add dependent components that have
  2594. // corresponding nodes we want to see in the graph
  2595. m_ignoreGraphUpdates = false;
  2596. } while (!missingRequiredServices.empty());
  2597. // Restore m_ignoreGraphUpdates to its original value now that we've added the intended component
  2598. // and all its dependencies
  2599. m_ignoreGraphUpdates = originalIgnoreGraphUpdates;
  2600. return requestedComponentId;
  2601. }
  2602. void MainWindow::HandleNodeCreated(GraphModel::NodePtr node)
  2603. {
  2604. using namespace LandscapeCanvas;
  2605. if (m_ignoreGraphUpdates)
  2606. {
  2607. return;
  2608. }
  2609. // Ignore for wrapped nodes that were added since we don't want to
  2610. // create a new Entity for them. Adding their component will be handled
  2611. // later when the OnGraphModelNodeWrapped event gets called.
  2612. auto wrappedNodeIt = AZStd::find(m_addedWrappedNodes.begin(), m_addedWrappedNodes.end(), node);
  2613. if (wrappedNodeIt != m_addedWrappedNodes.end())
  2614. {
  2615. return;
  2616. }
  2617. auto* baseNodePtr = static_cast<BaseNode*>(node.get());
  2618. if (!baseNodePtr)
  2619. {
  2620. return;
  2621. }
  2622. GraphCanvas::GraphId graphId = (*GraphModelIntegration::GraphControllerNotificationBus::GetCurrentBusId());
  2623. AZ::EntityId rootEntityId = GetRootEntityIdForGraphId(graphId);
  2624. if (!rootEntityId.IsValid())
  2625. {
  2626. AZ_Assert(false, "No root Entity associated with this graph.");
  2627. return;
  2628. }
  2629. m_ignoreGraphUpdates = true;
  2630. // If the new node already has a valid EntityId, then it means the node was copy/pasted, so we need
  2631. // to find the corresponding deserialized Entity and fix-up the references.
  2632. // However, the new deserialized entities/components won't be available until the propagation is
  2633. // complete, so we'll need to keep track of the deserialized nodes and then handle the fix-up after.
  2634. AZ::EntityId existingEntityId = baseNodePtr->GetVegetationEntityId();
  2635. if (existingEntityId.IsValid())
  2636. {
  2637. m_deserializedNodes.push_back(node);
  2638. }
  2639. // Otherwise, this new node was created by the user from the node palette or right-click menu,
  2640. // so create a fresh Entity/Component for the node
  2641. else
  2642. {
  2643. // Creating a node is actually two operations: creating an Entity + adding a component(s) to that Entity
  2644. // so we need to batch the operations so that undo/redo will treat it all as one operation
  2645. AzToolsFramework::ScopedUndoBatch undoBatch("Create Node");
  2646. // Create a new Entity to hold the Component for this new node
  2647. AZ::EntityId vegetationEntityId;
  2648. AzToolsFramework::EditorRequestBus::BroadcastResult(vegetationEntityId, &AzToolsFramework::EditorRequests::CreateNewEntity, rootEntityId);
  2649. // Add the Component for this node, as well as any required components
  2650. AddComponentForNode(node, vegetationEntityId);
  2651. }
  2652. m_ignoreGraphUpdates = false;
  2653. }
  2654. void MainWindow::AddComponentForNode(GraphModel::NodePtr node, const AZ::EntityId& entityId)
  2655. {
  2656. auto* baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2657. if (!baseNodePtr)
  2658. {
  2659. return;
  2660. }
  2661. AZ::TypeId componentToAddTypeId;
  2662. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(componentToAddTypeId, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::GetComponentTypeId, baseNodePtr->RTTI_GetType());
  2663. if (componentToAddTypeId.IsNull())
  2664. {
  2665. AZ_Assert(false, "Node missing a registered component TypeId.");
  2666. return;
  2667. }
  2668. AZ::ComponentId newComponentId = AddComponentTypeIdToEntity(entityId, componentToAddTypeId, baseNodePtr->GetOptionalRequiredServices());
  2669. // Tie this new node to its representative Entity and Component
  2670. baseNodePtr->SetVegetationEntityId(entityId);
  2671. baseNodePtr->SetComponentId(newComponentId);
  2672. }
  2673. void MainWindow::HandleNodeAdded(GraphModel::NodePtr node)
  2674. {
  2675. auto* baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2676. if (!baseNodePtr)
  2677. {
  2678. return;
  2679. }
  2680. // Update our EntityId/Node mappings when a new node is added
  2681. GraphCanvas::GraphId graphId = (*GraphModelIntegration::GraphControllerNotificationBus::GetCurrentBusId());
  2682. if (!m_ignoreGraphUpdates)
  2683. {
  2684. UpdateEntityIdNodeMap(graphId, node);
  2685. }
  2686. // For any node with an Entity Name slot, we need to replace the string property display with a read-only version
  2687. // instead until we have support for listening for GraphModel slot value changes. We need to delay this because
  2688. // when the node is added, the slots haven't been added to the element map yet.
  2689. QTimer::singleShot(0, [node, graphId]() {
  2690. GraphModel::SlotPtr slot = node->GetSlot(LandscapeCanvas::ENTITY_NAME_SLOT_ID);
  2691. if (slot)
  2692. {
  2693. GraphCanvas::NodeId nodeId;
  2694. GraphModelIntegration::GraphControllerRequestBus::EventResult(nodeId, graphId, &GraphModelIntegration::GraphControllerRequests::GetNodeIdByNode, node);
  2695. GraphCanvas::SlotId slotId;
  2696. GraphModelIntegration::GraphControllerRequestBus::EventResult(slotId, graphId, &GraphModelIntegration::GraphControllerRequests::GetSlotIdBySlot, slot);
  2697. // If this is a wrapped node, then remove the Entity Name property slot since the wrapper node will already have one
  2698. bool isNodeWrapped = false;
  2699. GraphModelIntegration::GraphControllerRequestBus::EventResult(isNodeWrapped, graphId, &GraphModelIntegration::GraphControllerRequests::IsNodeWrapped, node);
  2700. if (isNodeWrapped)
  2701. {
  2702. GraphCanvas::NodeRequestBus::Event(nodeId, &GraphCanvas::NodeRequests::RemoveSlot, slotId);
  2703. return;
  2704. }
  2705. // The ownership of the new data interface and property display get passed to the node property display widget
  2706. // when we call SetNodePropertyDisplay
  2707. auto dataInterface = aznew GraphModelIntegration::ReadOnlyDataInterface(slot);
  2708. GraphCanvas::NodePropertyDisplay* readOnlyPropertyDisplay = nullptr;
  2709. GraphCanvas::GraphCanvasRequestBus::BroadcastResult(readOnlyPropertyDisplay, &GraphCanvas::GraphCanvasRequests::CreateReadOnlyNodePropertyDisplay, static_cast<GraphCanvas::ReadOnlyDataInterface*>(dataInterface));
  2710. readOnlyPropertyDisplay->SetNodeId(nodeId);
  2711. readOnlyPropertyDisplay->SetSlotId(slotId);
  2712. GraphCanvas::NodePropertyRequestBus::Event(slotId, &GraphCanvas::NodePropertyRequests::SetNodePropertyDisplay, readOnlyPropertyDisplay);
  2713. }
  2714. });
  2715. // Listen for component property changes on the Entity corresponding to this node
  2716. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::MultiHandler::BusConnect(baseNodePtr->GetVegetationEntityId());
  2717. LandscapeCanvas::BaseNode::BaseNodeType nodeType = baseNodePtr->GetBaseNodeType();
  2718. if (nodeType == LandscapeCanvas::BaseNode::Shape)
  2719. {
  2720. // Add thumbnail image of the shape type to the node
  2721. AZ::TypeId componentTypeId;
  2722. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(componentTypeId, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::GetComponentTypeId, baseNodePtr->RTTI_GetType());
  2723. AZStd::string entityIconPath;
  2724. AzToolsFramework::EditorRequestBus::BroadcastResult(entityIconPath, &AzToolsFramework::EditorRequestBus::Events::GetComponentIconPath, componentTypeId, AZ::Edit::Attributes::ViewportIcon, nullptr);
  2725. if (!entityIconPath.empty())
  2726. {
  2727. QPixmap iconPixmap(entityIconPath.c_str());
  2728. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::SetThumbnailImageOnNode, node, iconPixmap);
  2729. }
  2730. }
  2731. else if (nodeType == LandscapeCanvas::BaseNode::Gradient || nodeType == LandscapeCanvas::BaseNode::GradientGenerator || nodeType == LandscapeCanvas::BaseNode::GradientModifier)
  2732. {
  2733. // Add custom gradient preview thumbnail to all gradient type nodes
  2734. // The node layout takes ownership of the thumbnail, so it will be deleted whenever the node is deleted
  2735. const AZ::EntityId& gradientEntityId = baseNodePtr->GetVegetationEntityId();
  2736. GradientPreviewThumbnailItem* previewThumbnail = new GradientPreviewThumbnailItem(gradientEntityId);
  2737. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::SetThumbnailOnNode, node, previewThumbnail);
  2738. }
  2739. else if ((nodeType == LandscapeCanvas::BaseNode::VegetationArea || nodeType == LandscapeCanvas::BaseNode::TerrainArea) && (node->GetNodeType() == GraphModel::NodeType::WrapperNode))
  2740. {
  2741. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::SetWrapperNodeActionString, node, QObject::tr("Add Extenders").toUtf8().constData());
  2742. }
  2743. }
  2744. void MainWindow::UpdateEntityIdNodeMap(GraphCanvas::GraphId graphId, GraphModel::NodePtr node)
  2745. {
  2746. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2747. const AZ::EntityId& entityId = baseNodePtr->GetVegetationEntityId();
  2748. auto nodeMap = GetEntityIdNodeMap(graphId, node);
  2749. if (nodeMap)
  2750. {
  2751. nodeMap->insert({ entityId, node });
  2752. }
  2753. }
  2754. MainWindow::EntityIdNodeMap* MainWindow::GetEntityIdNodeMap(GraphCanvas::GraphId graphId, GraphModel::NodePtr node)
  2755. {
  2756. auto nodeMapsIt = m_entityIdNodeMapsByGraph.find(graphId);
  2757. if (nodeMapsIt == m_entityIdNodeMapsByGraph.end())
  2758. {
  2759. return nullptr;
  2760. }
  2761. // Return the corresponding EntityIdNodeMap for this node type
  2762. EntityIdNodeMaps& nodeMaps = nodeMapsIt->second;
  2763. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2764. LandscapeCanvas::BaseNode::BaseNodeType baseNodeType = baseNodePtr->GetBaseNodeType();
  2765. auto nodeMapType = EntityIdNodeMapEnum::Invalid;
  2766. switch (baseNodeType)
  2767. {
  2768. case LandscapeCanvas::BaseNode::Shape:
  2769. nodeMapType = EntityIdNodeMapEnum::Shapes;
  2770. break;
  2771. case LandscapeCanvas::BaseNode::TerrainArea:
  2772. case LandscapeCanvas::BaseNode::VegetationArea:
  2773. nodeMapType = EntityIdNodeMapEnum::WrapperNodes;
  2774. break;
  2775. case LandscapeCanvas::BaseNode::Gradient:
  2776. case LandscapeCanvas::BaseNode::GradientGenerator:
  2777. case LandscapeCanvas::BaseNode::GradientModifier:
  2778. nodeMapType = EntityIdNodeMapEnum::Gradients;
  2779. break;
  2780. }
  2781. if (nodeMapType != EntityIdNodeMapEnum::Invalid)
  2782. {
  2783. return &nodeMaps[nodeMapType];
  2784. }
  2785. return nullptr;
  2786. }
  2787. void MainWindow::ParseNodeConnections(GraphCanvas::GraphId graphId, GraphModel::NodePtr node, ConnectionsList& connections)
  2788. {
  2789. auto baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  2790. AZ::Component* component = baseNodePtr->GetComponent();
  2791. if (!component)
  2792. {
  2793. return;
  2794. }
  2795. // Find the node mappings for this graph
  2796. auto nodeMapsIt = m_entityIdNodeMapsByGraph.find(graphId);
  2797. if (nodeMapsIt == m_entityIdNodeMapsByGraph.end())
  2798. {
  2799. return;
  2800. }
  2801. const EntityIdNodeMaps& nodeMaps = nodeMapsIt->second;
  2802. // Iterate through the component class elements to find any matching fields corresponding
  2803. // to input slots
  2804. AZ::EntityId previewEntityId, inboundShapeEntityId;
  2805. AzToolsFramework::EntityIdList gradientSamplerIds;
  2806. AzToolsFramework::EntityIdList vegetationAreaIds;
  2807. m_serializeContext->EnumerateObject(component,
  2808. // beginElemCB
  2809. [&previewEntityId, &inboundShapeEntityId, &gradientSamplerIds, &vegetationAreaIds, baseNodePtr](void *instance, [[maybe_unused]] const AZ::SerializeContext::ClassData *classData, const AZ::SerializeContext::ClassElement *classElement) -> bool
  2810. {
  2811. if (classElement && (classElement->m_typeId == azrtti_typeid<AZ::EntityId>()))
  2812. {
  2813. if (strcmp(classElement->m_name, PreviewEntityElementName) == 0)
  2814. {
  2815. previewEntityId = *reinterpret_cast<AZ::EntityId*>(instance);
  2816. return false;
  2817. }
  2818. else if ((strcmp(classElement->m_name, GradientIdElementName) == 0)
  2819. || (strcmp(classElement->m_name, GradientEntityIdElementName) == 0))
  2820. {
  2821. gradientSamplerIds.push_back(*reinterpret_cast<AZ::EntityId*>(instance));
  2822. return false;
  2823. }
  2824. else if (strcmp(classElement->m_name, EntityIdListElementName) == 0)
  2825. {
  2826. if (baseNodePtr->GetBaseNodeType() == LandscapeCanvas::BaseNode::BaseNodeType::VegetationArea)
  2827. {
  2828. vegetationAreaIds.push_back(*reinterpret_cast<AZ::EntityId*>(instance));
  2829. }
  2830. else
  2831. {
  2832. gradientSamplerIds.push_back(*reinterpret_cast<AZ::EntityId*>(instance));
  2833. }
  2834. return false;
  2835. }
  2836. else if (strcmp(classElement->m_name, ShapeEntityIdElementName) == 0)
  2837. {
  2838. inboundShapeEntityId = *reinterpret_cast<AZ::EntityId*>(instance);
  2839. return false;
  2840. }
  2841. else if (strcmp(classElement->m_name, InputBoundsEntityIdElementName) == 0)
  2842. {
  2843. inboundShapeEntityId = *reinterpret_cast<AZ::EntityId*>(instance);
  2844. return false;
  2845. }
  2846. }
  2847. return true;
  2848. },
  2849. // endElemCB
  2850. []() -> bool { return true; },
  2851. AZ::SerializeContext::ENUM_ACCESS_FOR_READ, nullptr/* errorHandler */);
  2852. // Connect any preview entities to the corresponding shape bounds
  2853. AZStd::vector<AZStd::pair<GraphModel::SlotId, AZ::EntityId>> shapeSlotEntityPairs;
  2854. if (previewEntityId.IsValid())
  2855. {
  2856. shapeSlotEntityPairs.push_back(AZStd::make_pair(LandscapeCanvas::PREVIEW_BOUNDS_SLOT_ID, previewEntityId));
  2857. }
  2858. // Connect any inbound shape slots to the corresponding shape bounds
  2859. if (inboundShapeEntityId.IsValid())
  2860. {
  2861. // We have multiple inbound shape slots that share the same underlying property,
  2862. // so we need to figure out which kind of inbound shape slot this node has
  2863. GraphModel::SlotId shapeSlotId(LandscapeCanvas::INBOUND_SHAPE_SLOT_ID);
  2864. if (!node->GetSlot(shapeSlotId))
  2865. {
  2866. shapeSlotId = GraphModel::SlotId(LandscapeCanvas::PIN_TO_SHAPE_SLOT_ID);
  2867. if (!node->GetSlot(shapeSlotId))
  2868. {
  2869. shapeSlotId = GraphModel::SlotId(LandscapeCanvas::INPUT_BOUNDS_SLOT_ID);
  2870. }
  2871. }
  2872. shapeSlotEntityPairs.push_back(AZStd::make_pair(shapeSlotId, inboundShapeEntityId));
  2873. }
  2874. // Look for a placement bounds on Vegetation Areas, which is a special case since it could be
  2875. // driven by a Reference Shape or actual Shape component that also exists on the same Entity
  2876. // as the Vegetation Area Component that we represent with the node, but in this case the
  2877. // component will actually be shown as a Placement Bounds slot
  2878. AZ::EntityId placementBoundsEntityId;
  2879. if (baseNodePtr->GetBaseNodeType() == LandscapeCanvas::BaseNode::BaseNodeType::VegetationArea)
  2880. {
  2881. GraphModel::SlotPtr placementBoundsSlot = node->GetSlot(LandscapeCanvas::PLACEMENT_BOUNDS_SLOT_ID);
  2882. if (placementBoundsSlot)
  2883. {
  2884. // Retrieve the Placement Bounds slot value from the Reference Shape component if it exists
  2885. auto baseAreaNodePtr = static_cast<LandscapeCanvas::BaseAreaNode*>(node.get());
  2886. AZ::Component* referenceShapeComponent = baseAreaNodePtr->GetReferenceShapeComponent();
  2887. if (referenceShapeComponent)
  2888. {
  2889. QString propertyPath = GetPropertyPathForSlot(placementBoundsSlot, LandscapeCanvas::LandscapeCanvasDataTypeEnum::Bounds);
  2890. AzToolsFramework::PropertyTreeEditor pte = AzToolsFramework::PropertyTreeEditor(reinterpret_cast<void*>(referenceShapeComponent), referenceShapeComponent->RTTI_GetType());
  2891. auto placementBounds = pte.GetProperty(propertyPath.toUtf8().constData());
  2892. if (placementBounds.IsSuccess())
  2893. {
  2894. placementBoundsEntityId = AZStd::any_cast<AZ::EntityId>(placementBounds.GetValue());
  2895. if (placementBoundsEntityId.IsValid())
  2896. {
  2897. shapeSlotEntityPairs.push_back(AZStd::make_pair(LandscapeCanvas::PLACEMENT_BOUNDS_SLOT_ID, placementBoundsEntityId));
  2898. }
  2899. }
  2900. }
  2901. // Otherwise, also check if this Entity has its own Shape component as well that will serve
  2902. // as the placement bounds
  2903. else
  2904. {
  2905. const auto& shapeNodeMap = nodeMaps[EntityIdNodeMapEnum::Shapes];
  2906. const AZ::EntityId& entityId = baseAreaNodePtr->GetVegetationEntityId();
  2907. auto shapeIt = shapeNodeMap.find(entityId);
  2908. if (shapeIt != shapeNodeMap.end())
  2909. {
  2910. GraphModel::NodePtr shapeNode = (*shapeIt).second;
  2911. auto baseShapeNodePtr = static_cast<LandscapeCanvas::BaseNode*>(shapeNode.get());
  2912. if (baseShapeNodePtr->GetComponent())
  2913. {
  2914. shapeSlotEntityPairs.push_back(AZStd::make_pair(LandscapeCanvas::PLACEMENT_BOUNDS_SLOT_ID, entityId));
  2915. }
  2916. }
  2917. }
  2918. }
  2919. }
  2920. // Connect any input bounds slots to their corresponding shape bounds
  2921. const auto& shapeNodeMap = nodeMaps[EntityIdNodeMapEnum::Shapes];
  2922. for (auto slotEntityPair : shapeSlotEntityPairs)
  2923. {
  2924. const AZ::EntityId& entityId = slotEntityPair.second;
  2925. auto shapeIt = shapeNodeMap.find(entityId);
  2926. if (shapeIt == shapeNodeMap.end())
  2927. {
  2928. continue;
  2929. }
  2930. GraphModel::NodePtr shapeNode = (*shapeIt).second;
  2931. GraphModel::SlotPtr shapeBoundsSlot = shapeNode->GetSlot(LandscapeCanvas::BaseShapeNode::BOUNDS_SLOT_ID);
  2932. GraphModel::SlotPtr shapeTargetSlot = node->GetSlot(slotEntityPair.first);
  2933. auto source = AZStd::make_pair(shapeNode, shapeBoundsSlot);
  2934. auto target = AZStd::make_pair(node, shapeTargetSlot);
  2935. connections.push_back(AZStd::make_pair(source, target));
  2936. }
  2937. // Handle if this node has an image asset slot to parse
  2938. HandleImageAssetSlot(node, nodeMaps[EntityIdNodeMapEnum::Gradients], connections);
  2939. auto handleIndexedSlots = [this, graphId, node, &connections](
  2940. const AzToolsFramework::EntityIdList& entityIds,
  2941. const EntityIdNodeMap& sourceNodeMap,
  2942. const GraphModel::SlotName& outboundSlotId,
  2943. LandscapeCanvas::LandscapeCanvasDataTypeEnum slotDataType)
  2944. {
  2945. if (entityIds.empty())
  2946. {
  2947. return;
  2948. }
  2949. size_t numEntityIds = entityIds.size();
  2950. for (int i = 0; i < numEntityIds; ++i)
  2951. {
  2952. const AZ::EntityId& entityId = entityIds[i];
  2953. if (!entityId.IsValid())
  2954. {
  2955. continue;
  2956. }
  2957. // Find the source node
  2958. GraphModel::NodePtr sourceNode;
  2959. auto nodeIt = sourceNodeMap.find(entityId);
  2960. if (nodeIt != sourceNodeMap.end())
  2961. {
  2962. sourceNode = (*nodeIt).second;
  2963. }
  2964. if (sourceNode)
  2965. {
  2966. // Don't allow a node's output to be connected to itself
  2967. if (sourceNode == node)
  2968. {
  2969. continue;
  2970. }
  2971. GraphModel::SlotPtr outboundSlot = sourceNode->GetSlot(outboundSlotId);
  2972. // Find the corresponding input slot based on the index
  2973. GraphModel::DataTypePtr dataType = GetGraphContext()->GetDataType(slotDataType);
  2974. GraphModel::SlotPtr inboundSlot = EnsureInboundDataSlotWithIndex(graphId, node, dataType, i);
  2975. if (!inboundSlot)
  2976. {
  2977. AZ_Assert(false, "Unhandled inbound slot mapping.");
  2978. continue;
  2979. }
  2980. auto source = AZStd::make_pair(sourceNode, outboundSlot);
  2981. auto target = AZStd::make_pair(node, inboundSlot);
  2982. connections.push_back(AZStd::make_pair(source, target));
  2983. }
  2984. }
  2985. };
  2986. // Connect any inbound gradient slots to the corresponding Gradient, Gradient Generator, or Gradient Modifier
  2987. handleIndexedSlots(gradientSamplerIds, nodeMaps[EntityIdNodeMapEnum::Gradients], LandscapeCanvas::OUTBOUND_GRADIENT_SLOT_ID, LandscapeCanvas::LandscapeCanvasDataTypeEnum::Gradient);
  2988. // Connect any inbound vegetation area slots to the corresponding vegetation area
  2989. handleIndexedSlots(vegetationAreaIds, nodeMaps[EntityIdNodeMapEnum::WrapperNodes], LandscapeCanvas::OUTBOUND_AREA_SLOT_ID, LandscapeCanvas::LandscapeCanvasDataTypeEnum::Area);
  2990. }
  2991. void MainWindow::HandleImageAssetSlot(GraphModel::NodePtr targetNode, const EntityIdNodeMap& gradientNodeMap, ConnectionsList& connections)
  2992. {
  2993. auto baseNode = static_cast<LandscapeCanvas::BaseNode*>(targetNode.get());
  2994. const AZ::EntityId& entityId = baseNode->GetVegetationEntityId();
  2995. AZStd::string imageSourceAsset;
  2996. GradientSignal::ImageGradientRequestBus::EventResult(
  2997. imageSourceAsset, entityId, &GradientSignal::ImageGradientRequests::GetImageAssetSourcePath);
  2998. AZ::IO::Path imageSourceAssetPath(imageSourceAsset);
  2999. // The imageSourceAssetPath will only be valid if the targetNode is an Image Gradient that has
  3000. // a valid image asset path set.
  3001. if (!imageSourceAssetPath.empty())
  3002. {
  3003. // Look through all the gradient nodes in this graph to find a Gradient Baker that
  3004. // has the same output path as the input image asset to the Image Gradient. There
  3005. // might not be one if the user is generating the image gradients themselves and
  3006. // not from a gradient baker.
  3007. for (auto it = gradientNodeMap.begin(); it != gradientNodeMap.end(); ++it)
  3008. {
  3009. const AZ::EntityId& nodeEntityId = it->first;
  3010. GraphModel::NodePtr sourceNode = it->second;
  3011. // If this node doesn't have an output image slot, it's not a Gradient Baker
  3012. // so keep looking
  3013. GraphModel::SlotPtr outputImageSlot = sourceNode->GetSlot(LandscapeCanvas::OUTPUT_IMAGE_SLOT_ID);
  3014. if (!outputImageSlot)
  3015. {
  3016. continue;
  3017. }
  3018. AZ::IO::Path outputImagePath;
  3019. GradientSignal::GradientImageCreatorRequestBus::EventResult(
  3020. outputImagePath, nodeEntityId, &GradientSignal::GradientImageCreatorRequests::GetOutputImagePath);
  3021. if (imageSourceAssetPath == outputImagePath)
  3022. {
  3023. GraphModel::SlotPtr imageAssetSlot = targetNode->GetSlot(LandscapeCanvas::IMAGE_ASSET_SLOT_ID);
  3024. auto source = AZStd::make_pair(sourceNode, outputImageSlot);
  3025. auto target = AZStd::make_pair(targetNode, imageAssetSlot);
  3026. connections.push_back(AZStd::make_pair(source, target));
  3027. }
  3028. }
  3029. }
  3030. }
  3031. void MainWindow::HandleDeserializedNodes()
  3032. {
  3033. if (m_deserializedNodes.empty())
  3034. {
  3035. return;
  3036. }
  3037. m_ignoreGraphUpdates = true;
  3038. LandscapeCanvas::LandscapeCanvasSerialization serialization;
  3039. LandscapeCanvas::LandscapeCanvasSerializationRequestBus::BroadcastResult(serialization, &LandscapeCanvas::LandscapeCanvasSerializationRequests::GetSerializedMappings);
  3040. GraphCanvas::GraphId graphId = GetActiveGraphCanvasGraphId();
  3041. // The deserialized nodes already have a valid EntityId, so we need
  3042. // to find the corresponding deserialized Entity and fix-up the references
  3043. for (auto node : m_deserializedNodes)
  3044. {
  3045. auto* baseNodePtr = static_cast<LandscapeCanvas::BaseNode*>(node.get());
  3046. if (!baseNodePtr)
  3047. {
  3048. continue;
  3049. }
  3050. AZ::EntityId existingEntityId = baseNodePtr->GetVegetationEntityId();
  3051. if (!existingEntityId.IsValid())
  3052. {
  3053. continue;
  3054. }
  3055. auto it = serialization.m_deserializedEntities.find(existingEntityId);
  3056. if (it == serialization.m_deserializedEntities.end())
  3057. {
  3058. continue;
  3059. }
  3060. AZ::EntityId newEntityId = it->second;
  3061. AZ::TypeId componentTypeId;
  3062. LandscapeCanvas::LandscapeCanvasNodeFactoryRequestBus::BroadcastResult(componentTypeId, &LandscapeCanvas::LandscapeCanvasNodeFactoryRequests::GetComponentTypeId, baseNodePtr->RTTI_GetType());
  3063. if (componentTypeId.IsNull())
  3064. {
  3065. continue;
  3066. }
  3067. AZ::Entity* newEntity = nullptr;
  3068. AZ::ComponentApplicationBus::BroadcastResult(newEntity, &AZ::ComponentApplicationRequests::FindEntity, newEntityId);
  3069. AZ_Assert(newEntity, "Unable to find deserialized Entity");
  3070. // Find the component on the Entity that corresponds to this node
  3071. AZ::Component* newComponent = newEntity->FindComponent(componentTypeId);
  3072. if (!newComponent)
  3073. {
  3074. // The FindComponent won't find a component if its disabled, so if it failed
  3075. // then look through the disabled components on this Entity
  3076. AZ::Entity::ComponentArrayType disabledComponents;
  3077. AzToolsFramework::EditorDisabledCompositionRequestBus::Event(newEntityId, &AzToolsFramework::EditorDisabledCompositionRequests::GetDisabledComponents, disabledComponents);
  3078. for (auto disabledComponent : disabledComponents)
  3079. {
  3080. if (disabledComponent->RTTI_GetType() == componentTypeId)
  3081. {
  3082. newComponent = disabledComponent;
  3083. break;
  3084. }
  3085. }
  3086. // Look through the pending components next if we didn't find it in the disabled components,
  3087. // since it may be put in the pending bucket if a dependent component is actually deleted
  3088. // instead of just being disabled
  3089. if (!newComponent)
  3090. {
  3091. AZ::Entity::ComponentArrayType pendingComponents;
  3092. AzToolsFramework::EditorPendingCompositionRequestBus::Event(newEntityId, &AzToolsFramework::EditorPendingCompositionRequests::GetPendingComponents, pendingComponents);
  3093. for (AZ::Component* pendingComponent : pendingComponents)
  3094. {
  3095. if (pendingComponent->RTTI_GetType() == componentTypeId)
  3096. {
  3097. newComponent = pendingComponent;
  3098. break;
  3099. }
  3100. }
  3101. }
  3102. // If the component for this node is disabled, then the node needs to be disabled as well
  3103. GraphModelIntegration::GraphControllerRequestBus::Event(graphId, &GraphModelIntegration::GraphControllerRequests::DisableNode, node);
  3104. }
  3105. AZ_Assert(newComponent, "Deserialized Entity missing component matching node");
  3106. // Fix-up the references on the new node to the deserialized Entity/Component
  3107. baseNodePtr->SetVegetationEntityId(newEntityId);
  3108. baseNodePtr->SetComponentId(newComponent->GetId());
  3109. }
  3110. m_deserializedNodes.clear();
  3111. m_ignoreGraphUpdates = false;
  3112. }
  3113. int MainWindow::GetInboundDataSlotIndex(GraphModel::NodePtr node, GraphModel::DataTypePtr dataType, GraphModel::SlotPtr targetSlot)
  3114. {
  3115. if (!node)
  3116. {
  3117. return InvalidSlotIndex;
  3118. }
  3119. // Return the index of the specified targetSlot based on the input data slots that match the specified data type on the given node
  3120. int index = 0;
  3121. for (auto& it : node->GetSlots())
  3122. {
  3123. GraphModel::SlotPtr slot = it.second;
  3124. if (slot->Is(GraphModel::SlotDirection::Input, GraphModel::SlotType::Data))
  3125. {
  3126. // Our Bounds and Gradient data types are both AZ::EntityId under the hood, so there is
  3127. // some magic that takes place where they each support an Invalid data type as well as
  3128. // their specific data type, so instead of comparing the current slot->GetDataType() directly
  3129. // we need to check the possible data types instead for a match.
  3130. const auto& dataTypes = slot->GetSupportedDataTypes();
  3131. auto iter = AZStd::find(dataTypes.begin(), dataTypes.end(), dataType);
  3132. if (iter != dataTypes.end())
  3133. {
  3134. if (slot == targetSlot)
  3135. {
  3136. return index;
  3137. }
  3138. else
  3139. {
  3140. ++index;
  3141. }
  3142. }
  3143. }
  3144. }
  3145. return InvalidSlotIndex;
  3146. }
  3147. GraphModel::SlotPtr MainWindow::EnsureInboundDataSlotWithIndex(GraphCanvas::GraphId graphId, GraphModel::NodePtr node, GraphModel::DataTypePtr dataType, int index)
  3148. {
  3149. // Iterate through all the slots on the node to find an input data slot that matches the specified data type for the specified index
  3150. int currentIndex = 0;
  3151. for (GraphModel::SlotDefinitionPtr slotDefinition : node->GetSlotDefinitions())
  3152. {
  3153. if (slotDefinition->Is(GraphModel::SlotDirection::Input, GraphModel::SlotType::Data))
  3154. {
  3155. const GraphModel::SlotName& slotName = slotDefinition->GetName();
  3156. const auto& dataTypes = slotDefinition->GetSupportedDataTypes();
  3157. auto iter = AZStd::find(dataTypes.begin(), dataTypes.end(), dataType);
  3158. if (iter != dataTypes.end())
  3159. {
  3160. if (slotDefinition->SupportsExtendability())
  3161. {
  3162. // The subId for the extendable slots aren't necessarily an index starting at 0, depending
  3163. // on if the user removes/re-adds slots, so we first need to check if we need to offset
  3164. // the index we are expecting based on the starting subId
  3165. int subIdOffset = 0;
  3166. auto extendableSlots = node->GetExtendableSlots(slotName);
  3167. if (!extendableSlots.empty())
  3168. {
  3169. GraphModel::SlotPtr firstSlot = *extendableSlots.begin();
  3170. subIdOffset = firstSlot->GetSlotSubId();
  3171. index += subIdOffset;
  3172. }
  3173. GraphModel::SlotId slotId(slotName, index);
  3174. // If it's an extendable slot, we need to add enough to be able to accommodate the specified index.
  3175. for (int i = node->GetExtendableSlotCount(slotName) + subIdOffset; i < index + 1; ++i)
  3176. {
  3177. // If we fail to add an extended slot at any point (e.g. reached maximum, node has custom logic overriding, etc..)
  3178. // then we need to bail out. We need to add the extended slot using a different API when we are doing an initial
  3179. // graph vs. if the graph is already loaded because in the former case the node hasn't been fully created yet so
  3180. // we are just updating the data model, whereas in the latter case the node already exists in the graph and so
  3181. // we need to use the GraphController API so that the UI gets updated properly.
  3182. bool success = false;
  3183. if (node->GetId() == GraphModel::Node::INVALID_NODE_ID)
  3184. {
  3185. success = node->AddExtendedSlot(slotName);
  3186. }
  3187. else
  3188. {
  3189. GraphModelIntegration::GraphControllerRequestBus::EventResult(slotId, graphId, &GraphModelIntegration::GraphControllerRequests::ExtendSlot, node, slotName);
  3190. success = slotId.IsValid();
  3191. }
  3192. if (!success)
  3193. {
  3194. return nullptr;
  3195. }
  3196. }
  3197. return node->GetSlot(slotId);
  3198. }
  3199. else if (currentIndex == index)
  3200. {
  3201. return node->GetSlot(slotName);
  3202. }
  3203. ++currentIndex;
  3204. }
  3205. }
  3206. }
  3207. return nullptr;
  3208. }
  3209. }
  3210. #include <Source/Editor/moc_MainWindow.cpp>