ParentNode.cs 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace AmplifyShaderEditor
  9. {
  10. public enum PreviewLocation
  11. {
  12. Auto,
  13. TopCenter,
  14. BottomCenter,
  15. Left,
  16. Right
  17. }
  18. public enum NodeMessageType
  19. {
  20. Error,
  21. Warning,
  22. Info
  23. }
  24. [Serializable]
  25. public class ParentNode : UndoParentNode, ISerializationCallbackReceiver
  26. {
  27. protected readonly string[] PrecisionLabels = { "Float", "Half" };
  28. protected readonly string[] PrecisionLabelsExtra = { "Float", "Half", "Inherit" };
  29. private const double NodeClickTime = 0.2;
  30. protected GUIContent PrecisionContent = new GUIContent( "Precision", "Changes the precision of internal calculations, using lower types saves some performance\nDefault: Float" );
  31. private const int MoveCountBuffer = 3;// When testing for stopped movement we need to take Layout and Repaint into account for them not to interfere with tests
  32. private const float MinInsideBoxWidth = 20;
  33. private const float MinInsideBoxHeight = 10;
  34. private const string WikiLinkStr = "online reference";
  35. public delegate void OnNodeEvent( ParentNode node, bool testOnlySelected, InteractionMode interactionMode );
  36. public delegate void OnNodeGenericEvent( ParentNode node );
  37. public delegate void OnNodeReOrder( ParentNode node, int index );
  38. public delegate void DrawPropertySection();
  39. public delegate void OnSRPAction( int outputId, ref MasterNodeDataCollector dataCollector );
  40. [SerializeField]
  41. protected PrecisionType m_currentPrecisionType = PrecisionType.Inherit;
  42. [SerializeField]
  43. protected bool m_customPrecision = false;
  44. [SerializeField]
  45. protected InteractionMode m_defaultInteractionMode = InteractionMode.Other;
  46. public event OnNodeEvent OnNodeStoppedMovingEvent;
  47. public OnNodeGenericEvent OnNodeChangeSizeEvent;
  48. public OnNodeGenericEvent OnNodeDestroyedEvent;
  49. public event OnNodeReOrder OnNodeReOrderEvent;
  50. public OnSRPAction OnLightweightAction;
  51. public OnSRPAction OnHDAction;
  52. [SerializeField]
  53. private int m_uniqueId;
  54. [SerializeField]
  55. protected Rect m_position;
  56. [SerializeField]
  57. protected Rect m_unpreviewedPosition;
  58. [SerializeField]
  59. protected GUIContent m_content;
  60. [SerializeField]
  61. protected GUIContent m_additionalContent;
  62. [SerializeField]
  63. protected bool m_initialized;
  64. [SerializeField]
  65. protected NodeConnectionStatus m_connStatus;
  66. protected bool m_selfPowered = false;
  67. [SerializeField]
  68. protected int m_activeConnections;
  69. [SerializeField]
  70. protected System.Type m_activeType;
  71. [SerializeField]
  72. protected int m_activePort;
  73. [SerializeField]
  74. protected int m_activeNode;
  75. protected NodeRestrictions m_restrictions;
  76. [SerializeField]
  77. protected Color m_statusColor;
  78. [SerializeField]
  79. protected Rect m_propertyDrawPos;
  80. // Ports
  81. [SerializeField]
  82. protected List<InputPort> m_inputPorts = new List<InputPort>();
  83. protected Dictionary<int, InputPort> m_inputPortsDict = new Dictionary<int, InputPort>();
  84. [SerializeField]
  85. protected bool m_sortInputPorts = false;
  86. private List<InputPort> m_sortedInputPorts = new List<InputPort>();
  87. [SerializeField]
  88. protected List<OutputPort> m_outputPorts = new List<OutputPort>();
  89. protected Dictionary<int, OutputPort> m_outputPortsDict = new Dictionary<int, OutputPort>();
  90. [SerializeField]
  91. protected bool m_sortOutputPorts = false;
  92. private List<OutputPort> m_sortedOutputPorts = new List<OutputPort>();
  93. // Positions
  94. [SerializeField]
  95. protected Rect m_globalPosition;
  96. [SerializeField]
  97. protected Rect m_headerPosition;
  98. //private Vector2 m_tooltipOffset;
  99. [SerializeField]
  100. protected bool m_sizeIsDirty = false;
  101. [SerializeField]
  102. protected Vector2 m_extraSize;
  103. [SerializeField]
  104. protected Vector2 m_insideSize;
  105. [SerializeField]
  106. protected float m_fontHeight;
  107. // Editor State save on Play Button
  108. [SerializeField]
  109. protected bool m_isDirty;
  110. [SerializeField]
  111. private int m_isMoving = 0;
  112. [SerializeField]
  113. private Rect m_lastPosition;
  114. // Live Shader Gen
  115. [SerializeField]
  116. private bool m_saveIsDirty;
  117. [SerializeField]
  118. protected bool m_requireMaterialUpdate = false;
  119. [SerializeField]
  120. protected int m_commentaryParent = -1;
  121. [SerializeField]
  122. protected int m_depth = -1;
  123. [SerializeField]
  124. protected bool m_materialMode = false;
  125. [SerializeField]
  126. protected bool m_showPreview = false;
  127. [SerializeField]
  128. protected int m_previewMaterialPassId = -1;
  129. protected bool m_useSquareNodeTitle = false;
  130. [SerializeField]
  131. protected bool m_continuousPreviewRefresh = false;
  132. private bool m_previewIsDirty = true;
  133. // Error Box Messages
  134. private Rect m_errorBox;
  135. private bool m_previousErrorMessage = false;
  136. protected bool m_showErrorMessage = false;
  137. protected NodeMessageType m_errorMessageTypeIsError = NodeMessageType.Error;
  138. protected string m_errorMessageTooltip = string.Empty;
  139. private GUIContent m_errorIcon = new GUIContent();
  140. private GUIContent m_errorMessage = new GUIContent();
  141. private GUIStyle m_errorCurrentStyle;
  142. private const string ErrorTitle = "ERROR";
  143. private const string WarningTitle = "WARNING";
  144. private const string InfoTitle = "INFO";
  145. // Drawing Node
  146. protected PreviewLocation m_selectedLocation = PreviewLocation.Auto;
  147. private int m_extraHeaderHeight = 0;
  148. protected bool m_isVisible;
  149. protected bool m_selected = false;
  150. protected bool m_rmbIgnore;
  151. protected GUIContent m_sizeContentAux;
  152. protected uint m_currentReadParamIdx = 1;
  153. protected bool m_reorderLocked = false;
  154. protected Rect m_cachedPos;
  155. protected Vector2 m_accumDelta = Vector2.zero;
  156. private bool m_isOnGrid = false;
  157. protected bool m_useInternalPortData = false;
  158. protected bool m_autoDrawInternalPortData = true;
  159. protected DrawOrder m_drawOrder = DrawOrder.Default;
  160. protected bool m_movingInFrame = false;
  161. protected float m_anchorAdjust = -1;
  162. protected Color m_headerColor;
  163. [SerializeField] // needs to be serialized because of Undo
  164. protected Color m_headerColorModifier = Color.white;
  165. protected bool m_infiniteLoopDetected = false;
  166. protected int m_textLabelWidth = -1;
  167. private bool m_linkVisibility = false;
  168. [SerializeField]
  169. protected bool m_hasTooltipLink = true;
  170. protected int m_category = 0;
  171. protected double m_lastTimeSelected;
  172. private double m_tooltipTimestamp;
  173. protected string m_tooltipText;
  174. protected Rect m_unscaledRemainingBox;
  175. protected Rect m_remainingBox;
  176. private int m_visibleInputs = 0;
  177. private int m_visibleOutputs = 0;
  178. private double m_doubleClickTimestamp;
  179. private const double DoubleClickTime = 0.25;
  180. protected bool m_canExpand = true;
  181. protected bool m_firstDraw = true;
  182. protected int m_matrixId = -1;
  183. private float m_paddingTitleLeft = 0;
  184. private float m_paddingTitleRight = 0;
  185. // Preview Fields
  186. private Material m_previewMaterial = null;
  187. private Shader m_previewShader = null;
  188. protected string m_previewShaderGUID = string.Empty;
  189. protected float m_marginPreviewLeft = 0;
  190. protected bool m_globalShowPreview = false;
  191. protected Rect m_unscaledPreviewRect;
  192. protected Rect m_previewRect;
  193. protected bool m_drawPreviewMaskButtons = true;
  194. private int m_channelNumber = 0;
  195. protected bool m_firstPreviewDraw = true;
  196. [SerializeField]
  197. protected bool m_drawPreview = true;
  198. protected bool m_drawPreviewExpander = true;
  199. private bool m_spherePreview = false;
  200. protected bool m_drawPreviewAsSphere = false;
  201. protected bool m_forceDrawPreviewAsPlane = false;
  202. private bool m_finishPreviewRender = false;
  203. private int m_cachedMainTexId = -1;
  204. private int m_cachedMaskTexId = -1;
  205. private int m_cachedPortsId = -1;
  206. private int m_cachedPortId = -1;
  207. private int m_cachedDrawSphereId = -1;
  208. private int m_cachedInvertedZoomId = -1;
  209. //private int m_cachedIsLinearId = -1;
  210. private bool[] m_previewChannels = { true, true, true, false };
  211. // Others
  212. protected bool m_hasSubtitle = false;
  213. protected bool m_showSubtitle = true;
  214. protected bool m_hasLeftDropdown = false;
  215. protected bool m_autoWrapProperties = false;
  216. protected bool m_internalDataFoldout = true;
  217. protected bool m_propertiesFoldout = true;
  218. protected bool m_repopulateDictionaries = true;
  219. protected Vector2 m_lastInputBottomRight = Vector2.zero;
  220. protected Vector2 m_lastOutputBottomLeft = Vector2.zero;
  221. private Vector4 m_portMask = Vector4.zero;
  222. private Vector2 m_auxVector2 = Vector4.zero;
  223. protected Rect m_auxRect;
  224. protected PreviewLocation m_autoLocation;
  225. protected Rect m_titlePos;
  226. protected Rect m_addTitlePos;
  227. protected Rect m_expandRect;
  228. protected Rect m_dropdownRect;
  229. protected Rect m_currInputPortPos;
  230. protected Rect m_currOutputPortPos;
  231. protected Color m_colorBuffer;
  232. [SerializeField]
  233. protected bool m_docking = false;
  234. [SerializeField]
  235. protected int m_visiblePorts = 0;
  236. protected int m_graphDepth = 0;
  237. protected int m_oldInputCount = -1;
  238. protected bool m_dropdownEditing = false;
  239. protected bool m_isNodeBeingCopied = false;
  240. protected string m_previousTitle = string.Empty;
  241. protected string m_previousAdditonalTitle = string.Empty;
  242. protected string m_previousAdditonalTitleFormat = string.Empty;
  243. private bool m_alive = true;
  244. private bool m_wasDeprecated = false;
  245. private double m_timedUpdateInitialValue;
  246. private double m_timedUpdateInterval;
  247. private bool m_fireTimedUpdateRequest = false;
  248. public ParentNode()
  249. {
  250. m_position = new Rect( 0, 0, 0, 0 );
  251. m_content = new GUIContent( GUIContent.none );
  252. m_additionalContent = new GUIContent( GUIContent.none );
  253. CommonInit( -1 );
  254. }
  255. public ParentNode( int uniqueId, float x, float y, float width, float height )
  256. {
  257. m_position = new Rect( x, y, width, height );
  258. m_content = new GUIContent( GUIContent.none );
  259. m_additionalContent = new GUIContent( GUIContent.none );
  260. CommonInit( uniqueId );
  261. }
  262. public virtual void OnEnable()
  263. {
  264. hideFlags = HideFlags.HideAndDontSave;
  265. RefreshUIHeaderColor();
  266. m_tooltipTimestamp = Time.realtimeSinceStartup;
  267. hideFlags = HideFlags.DontSave;
  268. }
  269. public void RefreshUIHeaderColor()
  270. {
  271. // @diogo: handle special shader function case; this will have to do for now...
  272. var functionNode = this as FunctionNode;
  273. if ( functionNode != null && functionNode.Function != null )
  274. {
  275. if ( functionNode.Function.Header == AmplifyShaderFunction.HeaderStyle.Custom )
  276. {
  277. m_headerColor = functionNode.Function.HeaderColor;
  278. }
  279. else if ( functionNode.Function.Header == AmplifyShaderFunction.HeaderStyle.Category )
  280. {
  281. m_headerColor = UIUtils.GetColorFromCategory( functionNode.Function.CustomNodeCategory );
  282. }
  283. else
  284. {
  285. m_headerColor = UIUtils.GetColorFromCategory( "Functions" );
  286. }
  287. }
  288. else if ( m_nodeAttribs != null )
  289. {
  290. if( UIUtils.HasColorCategory( m_nodeAttribs.Category ) )
  291. {
  292. m_headerColor = UIUtils.GetColorFromCategory( m_nodeAttribs.Category );
  293. }
  294. else
  295. {
  296. if( !string.IsNullOrEmpty( m_nodeAttribs.CustomCategoryColor ) )
  297. {
  298. m_headerColor = UIUtils.AddColorCategory( m_nodeAttribs.Category, m_nodeAttribs.CustomCategoryColor );
  299. }
  300. }
  301. }
  302. }
  303. protected virtual void CommonInit( int uniqueId )
  304. {
  305. m_uniqueId = uniqueId;
  306. m_isOnGrid = false;
  307. ConnStatus = NodeConnectionStatus.Not_Connected;
  308. m_inputPorts = new List<InputPort>();
  309. m_inputPortsDict = new Dictionary<int, InputPort>();
  310. m_outputPorts = new List<OutputPort>();
  311. m_outputPortsDict = new Dictionary<int, OutputPort>();
  312. System.Reflection.MemberInfo info = this.GetType();
  313. m_nodeAttribs = info.GetCustomAttributes( typeof( NodeAttributes ), true ).FirstOrDefault() as NodeAttributes;
  314. if( m_nodeAttribs != null )
  315. {
  316. m_content.text = m_nodeAttribs.Name;
  317. //m_content.tooltip = m_nodeAttribs.Description;
  318. m_tooltipText = m_nodeAttribs.Description;
  319. m_selected = false;
  320. }
  321. m_sizeContentAux = new GUIContent();
  322. m_extraSize = new Vector2( 0, 0 );
  323. m_insideSize = new Vector2( 0, 0 );
  324. m_sizeIsDirty = true;
  325. m_initialized = true;
  326. m_restrictions = new NodeRestrictions();
  327. m_propertyDrawPos = new Rect();
  328. }
  329. public virtual void AfterCommonInit()
  330. {
  331. if( PreviewShader && !HasPreviewShader )
  332. {
  333. m_drawPreview = false;
  334. m_drawPreviewExpander = false;
  335. m_canExpand = false;
  336. }
  337. if( m_drawPreviewExpander || m_hasLeftDropdown )
  338. {
  339. m_paddingTitleRight += Constants.PreviewExpanderWidth + Constants.IconsLeftRightMargin;
  340. m_paddingTitleLeft = Constants.PreviewExpanderWidth + Constants.IconsLeftRightMargin;
  341. }
  342. }
  343. public virtual void Destroy()
  344. {
  345. m_alive = false;
  346. if( OnNodeDestroyedEvent != null )
  347. {
  348. OnNodeDestroyedEvent( this );
  349. OnNodeDestroyedEvent = null;
  350. }
  351. OnLightweightAction = null;
  352. OnHDAction = null;
  353. OnNodeStoppedMovingEvent = null;
  354. OnNodeChangeSizeEvent = null;
  355. OnNodeReOrderEvent = null;
  356. if( m_restrictions != null )
  357. m_restrictions.Destroy();
  358. m_restrictions = null;
  359. if( m_inputPorts != null )
  360. {
  361. int inputCount = m_inputPorts.Count;
  362. for( int i = 0; i < inputCount; i++ )
  363. {
  364. m_inputPorts[ i ].Destroy();
  365. }
  366. m_inputPorts.Clear();
  367. m_inputPorts = null;
  368. }
  369. if( m_outputPorts != null )
  370. {
  371. int outputCount = m_outputPorts.Count;
  372. for( int i = 0; i < outputCount; i++ )
  373. {
  374. m_outputPorts[ i ].Destroy();
  375. }
  376. m_outputPorts.Clear();
  377. m_outputPorts = null;
  378. }
  379. if( m_inputPortsDict != null )
  380. m_inputPortsDict.Clear();
  381. m_inputPortsDict = null;
  382. if( m_outputPortsDict != null )
  383. m_outputPortsDict.Clear();
  384. m_outputPortsDict = null;
  385. if( m_previewMaterial != null )
  386. DestroyImmediate( m_previewMaterial );
  387. m_previewMaterial = null;
  388. m_previewShader = null;
  389. //m_containerGraph = null;
  390. }
  391. public virtual void Move( Vector2 delta )
  392. {
  393. if( m_docking )
  394. return;
  395. Move( delta, false );
  396. }
  397. public virtual void Move( Vector2 delta, bool snap )
  398. {
  399. if( m_docking )
  400. return;
  401. if( m_isMoving == 0 )
  402. {
  403. m_cachedPos = m_position;
  404. m_accumDelta = Vector2.zero;
  405. }
  406. m_isMoving = MoveCountBuffer;
  407. m_accumDelta += delta;
  408. if( snap )
  409. {
  410. m_position.x = Mathf.Round( ( m_cachedPos.x + m_accumDelta.x ) / 16 ) * 16;
  411. m_position.y = Mathf.Round( ( m_cachedPos.y + m_accumDelta.y ) / 16 ) * 16;
  412. }
  413. else
  414. {
  415. m_position.x += delta.x;
  416. m_position.y += delta.y;
  417. }
  418. //if(Event.current.type == EventType.Layout)
  419. m_movingInFrame = true;
  420. }
  421. public virtual void UpdateMaterial( Material mat )
  422. {
  423. m_requireMaterialUpdate = false;
  424. }
  425. public virtual void SetMaterialMode( Material mat, bool fetchMaterialValues )
  426. {
  427. m_materialMode = ( mat != null );
  428. }
  429. public virtual bool UpdateShaderDefaults( ref Shader shader, ref TextureDefaultsDataColector defaultCol ) { return false; }
  430. public virtual void ForceUpdateFromMaterial( Material material ) { }
  431. public void SetSaveIsDirty()
  432. {
  433. if( m_connStatus == NodeConnectionStatus.Connected )
  434. {
  435. SaveIsDirty = true;
  436. }
  437. }
  438. public void ActivateNodeReordering( int index )
  439. {
  440. if( OnNodeReOrderEvent != null )
  441. OnNodeReOrderEvent( this, index );
  442. }
  443. void RecalculateInputPortIdx()
  444. {
  445. m_inputPortsDict.Clear();
  446. int count = m_inputPorts.Count;
  447. for( int i = 0; i < count; i++ )
  448. {
  449. if( m_inputPorts[ i ].IsConnected )
  450. {
  451. int nodeId = m_inputPorts[ i ].ExternalReferences[ 0 ].NodeId;
  452. int portId = m_inputPorts[ i ].ExternalReferences[ 0 ].PortId;
  453. ParentNode node = UIUtils.GetNode( nodeId );
  454. if( node != null )
  455. {
  456. OutputPort outputPort = node.GetOutputPortByUniqueId( portId );
  457. int outputCount = outputPort.ExternalReferences.Count;
  458. for( int j = 0; j < outputCount; j++ )
  459. {
  460. if( outputPort.ExternalReferences[ j ].NodeId == m_uniqueId &&
  461. outputPort.ExternalReferences[ j ].PortId == m_inputPorts[ i ].PortId )
  462. {
  463. outputPort.ExternalReferences[ j ].PortId = i;
  464. }
  465. }
  466. }
  467. }
  468. m_inputPorts[ i ].PortId = i;
  469. m_inputPortsDict.Add( i, m_inputPorts[ i ] );
  470. }
  471. }
  472. public void SwapInputPorts( int fromIdx, int toIdx )
  473. {
  474. InputPort port = m_inputPorts[ fromIdx ];
  475. //if( toIdx > fromIdx )
  476. // toIdx--;
  477. m_inputPorts.Remove( port );
  478. m_inputPorts.Insert( toIdx, port );
  479. RecalculateInputPortIdx();
  480. SetSaveIsDirty();
  481. }
  482. public void RemoveInputPort( int idx )
  483. {
  484. if( idx < m_inputPorts.Count )
  485. {
  486. m_inputPortsDict.Remove( m_inputPorts[ idx ].PortId );
  487. m_inputPorts.RemoveAt( idx );
  488. SetSaveIsDirty();
  489. m_sizeIsDirty = true;
  490. }
  491. }
  492. public void RemoveOutputPort( string name )
  493. {
  494. int count = m_outputPorts.Count;
  495. for( int i = 0; i < count; i++ )
  496. {
  497. if( m_outputPorts[ i ].Name.Equals( name ) )
  498. {
  499. if( m_outputPorts[ i ].IsConnected )
  500. {
  501. m_containerGraph.DeleteConnection( false, m_uniqueId, m_outputPorts[ i ].PortId, false, true );
  502. m_outputPortsDict.Remove( m_outputPorts[ i ].PortId );
  503. m_outputPorts.RemoveAt( i );
  504. SetSaveIsDirty();
  505. m_sizeIsDirty = true;
  506. }
  507. }
  508. }
  509. }
  510. public void RemoveOutputPort( int idx, bool isArrayIndex = true )
  511. {
  512. if( isArrayIndex )
  513. {
  514. // idx represents a position on the output port array
  515. if( idx < m_outputPorts.Count )
  516. {
  517. if( m_outputPorts[ idx ].IsConnected )
  518. {
  519. m_containerGraph.DeleteConnection( false, m_uniqueId, m_outputPorts[ idx ].PortId, false, true );
  520. }
  521. m_outputPortsDict.Remove( m_outputPorts[ idx ].PortId );
  522. m_outputPorts.RemoveAt( idx );
  523. SetSaveIsDirty();
  524. m_sizeIsDirty = true;
  525. }
  526. }
  527. else
  528. {
  529. // idx represents a port unique id
  530. int count = m_outputPorts.Count;
  531. int arrIdx = -1;
  532. for( int i = 0; i < count; i++ )
  533. {
  534. if( m_outputPorts[ i ].PortId == idx )
  535. {
  536. arrIdx = i;
  537. break;
  538. }
  539. }
  540. if( arrIdx >= 0 )
  541. {
  542. if( m_outputPorts[ arrIdx ].IsConnected )
  543. {
  544. m_containerGraph.DeleteConnection( false, m_uniqueId, idx, false, true );
  545. }
  546. m_outputPortsDict.Remove( idx );
  547. m_outputPorts.RemoveAt( arrIdx );
  548. SetSaveIsDirty();
  549. m_sizeIsDirty = true;
  550. }
  551. }
  552. }
  553. // Manually add Ports
  554. public InputPort AddInputPort( WirePortDataType type, bool typeLocked, string name, int orderId = -1, MasterNodePortCategory category = MasterNodePortCategory.Fragment, int uniquePortId = -1 )
  555. {
  556. InputPort port = new InputPort( m_uniqueId, ( uniquePortId < 0 ? m_inputPorts.Count : uniquePortId ), type, name, typeLocked, ( orderId >= 0 ? orderId : m_inputPorts.Count ), category );
  557. m_inputPorts.Add( port );
  558. m_inputPortsDict.Add( port.PortId, port );
  559. SetSaveIsDirty();
  560. m_sizeIsDirty = true;
  561. return port;
  562. }
  563. public InputPort AddInputPort( WirePortDataType type, bool typeLocked, string name, string dataName, int orderId = -1, MasterNodePortCategory category = MasterNodePortCategory.Fragment, int uniquePortId = -1 )
  564. {
  565. InputPort port = new InputPort( m_uniqueId, ( uniquePortId < 0 ? m_inputPorts.Count : uniquePortId ), type, name, dataName, typeLocked, ( orderId >= 0 ? orderId : m_inputPorts.Count ), category );
  566. m_inputPorts.Add( port );
  567. m_inputPortsDict.Add( port.PortId, port );
  568. SetSaveIsDirty();
  569. m_sizeIsDirty = true;
  570. return port;
  571. }
  572. public InputPort AddInputPortAt( int idx, WirePortDataType type, bool typeLocked, string name, int orderId = -1, MasterNodePortCategory category = MasterNodePortCategory.Fragment, int uniquePortId = -1 )
  573. {
  574. InputPort port = new InputPort( m_uniqueId, ( uniquePortId < 0 ? m_inputPorts.Count : uniquePortId ), type, name, typeLocked, ( orderId >= 0 ? orderId : m_inputPorts.Count ), category );
  575. m_inputPorts.Insert( idx, port );
  576. m_inputPortsDict.Add( port.PortId, port );
  577. SetSaveIsDirty();
  578. m_sizeIsDirty = true;
  579. RecalculateInputPortIdx();
  580. return port;
  581. }
  582. public void AddOutputPort( WirePortDataType type, string name, int uniquePortId = -1 )
  583. {
  584. m_outputPorts.Add( new OutputPort( this, m_uniqueId, ( uniquePortId < 0 ? m_outputPorts.Count : uniquePortId ), type, name ) );
  585. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  586. SetSaveIsDirty();
  587. m_sizeIsDirty = true;
  588. }
  589. public void AddOutputPortAt( int idx, WirePortDataType type, string name, int uniquePortId = -1 )
  590. {
  591. OutputPort port = new OutputPort( this, m_uniqueId, ( uniquePortId < 0 ? m_outputPorts.Count : uniquePortId ), type, name );
  592. m_outputPorts.Insert( idx, port );
  593. m_outputPortsDict.Add( port.PortId, port );
  594. SetSaveIsDirty();
  595. m_sizeIsDirty = true;
  596. }
  597. public void AddOutputVectorPorts( WirePortDataType type, string name )
  598. {
  599. m_sizeIsDirty = true;
  600. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, type, name ) );
  601. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  602. switch( type )
  603. {
  604. case WirePortDataType.FLOAT2:
  605. {
  606. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "X" ) );
  607. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  608. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "Y" ) );
  609. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  610. }
  611. break;
  612. case WirePortDataType.FLOAT3:
  613. {
  614. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "X" ) );
  615. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  616. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "Y" ) );
  617. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  618. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "Z" ) );
  619. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  620. }
  621. break;
  622. case WirePortDataType.FLOAT4:
  623. {
  624. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "X" ) );
  625. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  626. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "Y" ) );
  627. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  628. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "Z" ) );
  629. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  630. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "W" ) );
  631. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  632. }
  633. break;
  634. }
  635. SetSaveIsDirty();
  636. }
  637. public void SetPreviewDirtyFromOutputs()
  638. {
  639. PreviewIsDirty = true;
  640. }
  641. public string GetOutputVectorItem( int vectorPortId, int currentPortId, string result )
  642. {
  643. if( m_outputPorts[ 0 ].DataType == WirePortDataType.COLOR )
  644. {
  645. switch( currentPortId - vectorPortId )
  646. {
  647. case 1: result += ".r"; break;
  648. case 2: result += ".g"; break;
  649. case 3: result += ".b"; break;
  650. case 4: result += ".a"; break;
  651. case 5: result += ".rgb"; break;
  652. }
  653. }
  654. else
  655. {
  656. switch( currentPortId - vectorPortId )
  657. {
  658. case 1: result += ".x"; break;
  659. case 2: result += ".y"; break;
  660. case 3: result += ".z"; break;
  661. case 4: result += ".w"; break;
  662. case 5: result += ".xyz"; break;
  663. }
  664. }
  665. return result;
  666. }
  667. public void AddOutputColorPorts( string name = "RGBA", bool addAlpha = true, bool addRGB = false )
  668. {
  669. m_sizeIsDirty = true;
  670. // RGBA port
  671. int startOrderId = ( m_outputPorts.Count > 0 ) ? m_outputPorts[ m_outputPorts.Count - 1 ].OrderId : 0;
  672. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, addAlpha ? WirePortDataType.COLOR : WirePortDataType.FLOAT3, name ) );
  673. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  674. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId;
  675. // Red port
  676. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "R" ) );
  677. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  678. m_outputPorts[ m_outputPorts.Count - 1 ].CustomColor = Color.red;
  679. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId + 2;
  680. // Green port
  681. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "G" ) );
  682. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  683. m_outputPorts[ m_outputPorts.Count - 1 ].CustomColor = Color.green;
  684. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId + 3;
  685. // Blue port
  686. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "B" ) );
  687. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  688. m_outputPorts[ m_outputPorts.Count - 1 ].CustomColor = Color.blue;
  689. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId + 4;
  690. if ( addAlpha )
  691. {
  692. // Alpha port
  693. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT, "A" ) );
  694. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  695. m_outputPorts[ m_outputPorts.Count - 1 ].CustomColor = Color.white;
  696. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId + 5;
  697. }
  698. if ( addRGB && addAlpha )
  699. {
  700. // RGB port (placed below RGBA)
  701. m_outputPorts.Add( new OutputPort( this, m_uniqueId, m_outputPorts.Count, WirePortDataType.FLOAT3, "RGB" ) );
  702. m_outputPortsDict.Add( m_outputPorts[ m_outputPorts.Count - 1 ].PortId, m_outputPorts[ m_outputPorts.Count - 1 ] );
  703. m_outputPorts[ m_outputPorts.Count - 1 ].OrderId = startOrderId + 1;
  704. }
  705. }
  706. public void ConvertFromVectorToColorPorts()
  707. {
  708. m_outputPorts[ 0 ].ChangeType( WirePortDataType.COLOR, false );
  709. m_outputPorts[ 1 ].Name = "R";
  710. m_outputPorts[ 1 ].CustomColor = Color.red;
  711. m_outputPorts[ 2 ].Name = "G";
  712. m_outputPorts[ 2 ].CustomColor = Color.green;
  713. m_outputPorts[ 3 ].Name = "B";
  714. m_outputPorts[ 3 ].CustomColor = Color.blue;
  715. m_outputPorts[ 4 ].Name = "A";
  716. m_outputPorts[ 4 ].CustomColor = Color.white;
  717. }
  718. public string GetOutputColorItem( int vectorPortId, int currentPortId, string result )
  719. {
  720. switch( currentPortId - vectorPortId )
  721. {
  722. case 1: result += ".r"; break;
  723. case 2: result += ".g"; break;
  724. case 3: result += ".b"; break;
  725. case 4: result += ".a"; break;
  726. case 5: result += ".rgb"; break;
  727. }
  728. return result;
  729. }
  730. public void ChangeOutputType( WirePortDataType type, bool invalidateConnections )
  731. {
  732. int outputCount = m_outputPorts.Count;
  733. for( int i = 0; i < outputCount; i++ )
  734. {
  735. m_outputPorts[ i ].ChangeType( type, invalidateConnections );
  736. }
  737. }
  738. public void ChangeInputType( WirePortDataType type, bool invalidateConnections )
  739. {
  740. int inputCount = m_inputPorts.Count;
  741. for( int i = 0; i < inputCount; i++ )
  742. {
  743. m_inputPorts[ i ].ChangeType( type, invalidateConnections );
  744. }
  745. }
  746. public void ChangeOutputProperties( int outputID, string newName, WirePortDataType newType, bool invalidateConnections = true )
  747. {
  748. if( outputID < m_outputPorts.Count )
  749. {
  750. m_outputPorts[ outputID ].ChangeProperties( newName, newType, invalidateConnections );
  751. IsDirty = true;
  752. m_sizeIsDirty = true;
  753. SetSaveIsDirty();
  754. }
  755. }
  756. public void ChangeOutputName( int outputArrayIdx, string newName )
  757. {
  758. if( outputArrayIdx < m_outputPorts.Count )
  759. {
  760. m_outputPorts[ outputArrayIdx ].Name = newName;
  761. IsDirty = true;
  762. m_sizeIsDirty = true;
  763. }
  764. }
  765. public InputPort CheckInputPortAt( Vector3 pos )
  766. {
  767. int count = m_inputPorts.Count;
  768. for( int i = 0; i < count; i++ )
  769. {
  770. if( m_inputPorts[ i ].InsideActiveArea( pos ) )
  771. return m_inputPorts[ i ];
  772. }
  773. return null;
  774. }
  775. public InputPort GetFirstInputPortOfType( WirePortDataType dataType, bool countObjectTypeAsValid )
  776. {
  777. int count = m_inputPorts.Count;
  778. for( int i = 0; i < count; i++ )
  779. {
  780. if( ( m_inputPorts[ i ].CheckValidType( dataType ) ) || ( countObjectTypeAsValid && m_inputPorts[ i ].DataType == WirePortDataType.OBJECT ) )
  781. return m_inputPorts[ i ];
  782. }
  783. return null;
  784. }
  785. public OutputPort CheckOutputPortAt( Vector3 pos )
  786. {
  787. int count = m_outputPorts.Count;
  788. for( int i = 0; i < count; i++ )
  789. {
  790. if( m_outputPorts[ i ].InsideActiveArea( pos ) )
  791. return m_outputPorts[ i ];
  792. }
  793. return null;
  794. }
  795. public OutputPort GetFirstOutputPortOfType( WirePortDataType dataType, bool checkForCasts )
  796. {
  797. int count = m_outputPorts.Count;
  798. for( int i = 0; i < count; i++ )
  799. {
  800. if( ( m_outputPorts[ i ].CheckValidType( dataType ) ) || ( checkForCasts && UIUtils.CanCast( dataType, m_outputPorts[ i ].DataType ) ) )
  801. return m_outputPorts[ i ];
  802. }
  803. return null;
  804. }
  805. virtual protected void ChangeSizeFinished() { m_firstPreviewDraw = true; /*MarkForPreviewUpdate();*/ }
  806. protected void ChangeSize()
  807. {
  808. m_cachedPos = m_position;
  809. //UIUtils.ResetMainSkin();
  810. Vector2 inSize = Vector2.zero;
  811. int inputCount = 0;
  812. int inputSize = m_inputPorts.Count;
  813. for( int i = 0; i < inputSize; i++ )
  814. {
  815. if( m_inputPorts[ i ].Visible )
  816. {
  817. if( m_inputPorts[ i ].DirtyLabelSize || m_inputPorts[ i ].LabelSize == Vector2.zero )
  818. {
  819. m_inputPorts[ i ].DirtyLabelSize = false;
  820. m_sizeContentAux.text = m_inputPorts[ i ].Name;
  821. m_inputPorts[ i ].UnscaledLabelSize = UIUtils.UnZoomedInputPortStyle.CalcSize( m_sizeContentAux );
  822. }
  823. inSize.x = Mathf.Max( inSize.x, m_inputPorts[ i ].UnscaledLabelSize.x );
  824. inSize.y = Mathf.Max( inSize.y, m_inputPorts[ i ].UnscaledLabelSize.y );
  825. inputCount += 1;
  826. }
  827. }
  828. if( inSize.x > 0 )
  829. inSize.x += UIUtils.PortsSize.x + Constants.PORT_TO_LABEL_SPACE_X * 2;
  830. inSize.x += m_marginPreviewLeft;
  831. inSize.y = Mathf.Max( inSize.y, UIUtils.PortsSize.y );
  832. Vector2 outSize = Vector2.zero;
  833. int outputCount = 0;
  834. int outputSize = m_outputPorts.Count;
  835. for( int i = 0; i < outputSize; i++ )
  836. {
  837. if( m_outputPorts[ i ].Visible )
  838. {
  839. if( m_outputPorts[ i ].DirtyLabelSize || m_outputPorts[ i ].LabelSize == Vector2.zero )
  840. {
  841. m_outputPorts[ i ].DirtyLabelSize = false;
  842. m_sizeContentAux.text = m_outputPorts[ i ].Name;
  843. m_outputPorts[ i ].UnscaledLabelSize = UIUtils.UnZoomedOutputPortPortStyle.CalcSize( m_sizeContentAux );
  844. }
  845. outSize.x = Mathf.Max( outSize.x, m_outputPorts[ i ].UnscaledLabelSize.x );
  846. outSize.y = Mathf.Max( outSize.y, m_outputPorts[ i ].UnscaledLabelSize.y );
  847. outputCount += 1;
  848. }
  849. }
  850. if( outSize.x > 0 )
  851. outSize.x += UIUtils.PortsSize.x + Constants.PORT_TO_LABEL_SPACE_X * 2;
  852. outSize.y = Mathf.Max( outSize.y, UIUtils.PortsSize.y );
  853. if( m_additionalContent.text.Length > 0 )
  854. {
  855. m_extraHeaderHeight = (int)Constants.NODE_HEADER_EXTRA_HEIGHT;
  856. m_hasSubtitle = true && m_showSubtitle;
  857. }
  858. else
  859. {
  860. m_extraHeaderHeight = 0;
  861. m_hasSubtitle = false;
  862. }
  863. float headerWidth = Mathf.Max( UIUtils.UnZoomedNodeTitleStyle.CalcSize( m_content ).x + m_paddingTitleLeft + m_paddingTitleRight, UIUtils.UnZoomedPropertyValuesTitleStyle.CalcSize( m_additionalContent ).x + m_paddingTitleLeft + m_paddingTitleRight );
  864. m_position.width = Mathf.Max(headerWidth, Mathf.Max(MinInsideBoxWidth, m_insideSize.x) + inSize.x + outSize.x);/* + Constants.NODE_HEADER_LEFTRIGHT_MARGIN * 2;*/
  865. m_position.width = Mathf.Round(m_position.width / 32) * 32 + 32;
  866. //m_position.width += m_extraSize.x;
  867. m_fontHeight = Mathf.Max( inSize.y, outSize.y );
  868. m_position.height = Mathf.Max( inputCount, outputCount ) * ( m_fontHeight + Constants.INPUT_PORT_DELTA_Y );// + Constants.INPUT_PORT_DELTA_Y;
  869. m_position.height = Mathf.Max( m_position.height, Mathf.Max( MinInsideBoxHeight, m_insideSize.y ) );
  870. m_position.height += UIUtils.HeaderMaxHeight + /*m_extraHeaderHeight +*/ Constants.INPUT_PORT_DELTA_Y;// + m_extraSize.y;
  871. if( m_showErrorMessage )
  872. m_position.height += 24;
  873. m_unpreviewedPosition = m_position;
  874. //UIUtils.CurrentWindow.CameraDrawInfo.InvertedZoom = cachedZoom;
  875. if( OnNodeChangeSizeEvent != null )
  876. {
  877. OnNodeChangeSizeEvent( this );
  878. }
  879. ChangeSizeFinished();
  880. }
  881. public virtual void Reset() { }
  882. public virtual void OnOutputPortConnected( int portId, int otherNodeId, int otherPortId ) { }
  883. public virtual void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  884. {
  885. InputPort port = GetInputPortByUniqueId( portId );
  886. if( activateNode && m_connStatus == NodeConnectionStatus.Connected )
  887. {
  888. port.GetOutputNode().ActivateNode( m_activeNode, m_activePort, m_activeType );
  889. }
  890. PreviewIsDirty = true;
  891. OnNodeChange();
  892. SetSaveIsDirty();
  893. }
  894. public virtual void OnInputPortDisconnected( int portId ) { PreviewIsDirty = true; OnNodeChange(); }
  895. public virtual void OnOutputPortDisconnected( int portId ) { }
  896. public virtual void OnNodeChange()
  897. {
  898. CheckSpherePreview();
  899. int count = m_outputPorts.Count;
  900. for( int i = 0; i < count; i++ )
  901. {
  902. if( m_outputPorts[ i ].IsConnected )
  903. {
  904. for( int f = 0; f < m_outputPorts[ i ].ExternalReferences.Count; f++ )
  905. {
  906. ContainerGraph.GetNode( m_outputPorts[ i ].ExternalReferences[ f ].NodeId ).OnNodeChange();
  907. }
  908. }
  909. }
  910. }
  911. public virtual void ActivateNode( int signalGenNodeId, int signalGenPortId, System.Type signalGenNodeType )
  912. {
  913. if( m_selfPowered )
  914. return;
  915. ConnStatus = m_restrictions.GetRestiction( signalGenNodeType, signalGenPortId ) ? NodeConnectionStatus.Error : NodeConnectionStatus.Connected;
  916. m_activeConnections += 1;
  917. if( m_activeConnections == 1 )
  918. {
  919. m_activeType = signalGenNodeType;
  920. m_activeNode = signalGenNodeId;
  921. m_activePort = signalGenPortId;
  922. for( int i = 0; i < m_inputPorts.Count; i++ )
  923. {
  924. if( m_inputPorts[ i ].IsConnected )
  925. {
  926. m_inputPorts[ i ].GetOutputNode().ActivateNode( signalGenNodeId, signalGenPortId, signalGenNodeType );
  927. }
  928. }
  929. }
  930. // saveisdirty might be needed, gonna leave this here for now
  931. // SetSaveIsDirty();
  932. }
  933. public virtual void DeactivateInputPortNode( int deactivatedPort, bool forceComplete )
  934. {
  935. GetInputPortByUniqueId( deactivatedPort ).GetOutputNode().DeactivateNode( deactivatedPort, false );
  936. }
  937. public virtual void DeactivateNode( int deactivatedPort, bool forceComplete )
  938. {
  939. if( m_selfPowered )
  940. return;
  941. // saveisdirty might be needed, gonna leave this here for now
  942. // SetSaveIsDirty();
  943. m_activeConnections -= 1;
  944. if( forceComplete || m_activeConnections <= 0 )
  945. {
  946. m_activeConnections = 0;
  947. ConnStatus = NodeConnectionStatus.Not_Connected;
  948. for( int i = 0; i < m_inputPorts.Count; i++ )
  949. {
  950. if( m_inputPorts[ i ].IsConnected )
  951. {
  952. ParentNode node = m_inputPorts[ i ].GetOutputNode();
  953. if( node != null )
  954. node.DeactivateNode( deactivatedPort == -1 ? m_inputPorts[ i ].PortId : deactivatedPort, false );
  955. }
  956. }
  957. }
  958. }
  959. public Rect GlobalToLocalPosition( DrawInfo drawInfo )
  960. {
  961. float width = m_globalPosition.width / drawInfo.InvertedZoom;
  962. float height = m_globalPosition.height / drawInfo.InvertedZoom;
  963. float x = m_globalPosition.x / drawInfo.InvertedZoom - drawInfo.CameraOffset.x;
  964. float y = m_globalPosition.y / drawInfo.InvertedZoom - drawInfo.CameraOffset.y;
  965. return new Rect( x, y, width, height );
  966. }
  967. protected void CalculatePositionAndVisibility( DrawInfo drawInfo )
  968. {
  969. //m_movingInFrame = false;
  970. m_globalPosition = m_position;
  971. m_globalPosition.x = drawInfo.InvertedZoom * ( m_globalPosition.x + drawInfo.CameraOffset.x );
  972. m_globalPosition.y = drawInfo.InvertedZoom * ( m_globalPosition.y + drawInfo.CameraOffset.y );
  973. m_globalPosition.width *= drawInfo.InvertedZoom;
  974. m_globalPosition.height *= drawInfo.InvertedZoom;
  975. m_isVisible = ( m_globalPosition.x + m_globalPosition.width > 0 ) &&
  976. ( m_globalPosition.x < drawInfo.CameraArea.width ) &&
  977. ( m_globalPosition.y + m_globalPosition.height > 0 ) &&
  978. ( m_globalPosition.y < drawInfo.CameraArea.height );
  979. if( m_isMoving > 0 && drawInfo.CurrentEventType != EventType.MouseDrag )
  980. {
  981. float deltaX = Mathf.Abs( m_lastPosition.x - m_position.x );
  982. float deltaY = Mathf.Abs( m_lastPosition.y - m_position.y );
  983. if( deltaX < 0.01f && deltaY < 0.01f )
  984. {
  985. m_isMoving -= 1;
  986. if( m_isMoving == 0 )
  987. {
  988. OnSelfStoppedMovingEvent();
  989. }
  990. }
  991. else
  992. {
  993. m_isMoving = MoveCountBuffer;
  994. }
  995. m_lastPosition = m_position;
  996. }
  997. }
  998. public void FireStoppedMovingEvent( bool testOnlySelected, InteractionMode interactionMode )
  999. {
  1000. if( OnNodeStoppedMovingEvent != null )
  1001. OnNodeStoppedMovingEvent( this, testOnlySelected, interactionMode );
  1002. }
  1003. public virtual void OnSelfStoppedMovingEvent()
  1004. {
  1005. FireStoppedMovingEvent( true, m_defaultInteractionMode );
  1006. }
  1007. protected void DrawPrecisionProperty( bool withInherit = true )
  1008. {
  1009. if( withInherit )
  1010. m_currentPrecisionType = (PrecisionType)EditorGUILayoutPopup( PrecisionContent.text, (int)m_currentPrecisionType, PrecisionLabelsExtra );
  1011. else
  1012. m_currentPrecisionType = (PrecisionType)EditorGUILayoutPopup( PrecisionContent.text, (int)m_currentPrecisionType, PrecisionLabels );
  1013. }
  1014. public virtual void DrawTitle( Rect titlePos )
  1015. {
  1016. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  1017. {
  1018. GUI.Label( titlePos, m_content, UIUtils.GetCustomStyle( CustomStyle.NodeTitle ) );
  1019. }
  1020. }
  1021. public virtual void DrawPreview( DrawInfo drawInfo, Rect rect )
  1022. {
  1023. //if ( !m_drawPreview )
  1024. // return;
  1025. if( m_cachedDrawSphereId == -1 )
  1026. m_cachedDrawSphereId = Shader.PropertyToID( "_DrawSphere" );
  1027. if( m_cachedInvertedZoomId == -1 )
  1028. m_cachedInvertedZoomId = Shader.PropertyToID( "_InvertedZoom" );
  1029. m_channelNumber = 0;
  1030. Vector4 mask = Vector4.one;
  1031. if( m_outputPorts.Count > 0 )
  1032. {
  1033. switch( m_outputPorts[ 0 ].DataType )
  1034. {
  1035. case WirePortDataType.FLOAT:
  1036. m_channelNumber = 1;
  1037. mask.Set( 1, 1, 1, 0 );
  1038. break;
  1039. case WirePortDataType.FLOAT2:
  1040. m_channelNumber = 2;
  1041. mask.Set( m_previewChannels[ 0 ] ? 1 : 0, m_previewChannels[ 1 ] ? 1 : 0, 1, 0 );
  1042. break;
  1043. case WirePortDataType.COLOR:
  1044. case WirePortDataType.FLOAT4:
  1045. case WirePortDataType.SAMPLER1D:
  1046. case WirePortDataType.SAMPLER2D:
  1047. case WirePortDataType.SAMPLER3D:
  1048. case WirePortDataType.SAMPLERCUBE:
  1049. case WirePortDataType.SAMPLER2DARRAY:
  1050. m_channelNumber = 4;
  1051. mask.Set( m_previewChannels[ 0 ] ? 1 : 0, m_previewChannels[ 1 ] ? 1 : 0, m_previewChannels[ 2 ] ? 1 : 0, m_previewChannels[ 3 ] ? 1 : 0 );
  1052. break;
  1053. default:
  1054. m_channelNumber = 3;
  1055. mask.Set( m_previewChannels[ 0 ] ? 1 : 0, m_previewChannels[ 1 ] ? 1 : 0, m_previewChannels[ 2 ] ? 1 : 0, 0 );
  1056. break;
  1057. }
  1058. }
  1059. UIUtils.LinearMaterial.SetFloat( m_cachedDrawSphereId, ( SpherePreview ? 1 : 0 ) );
  1060. UIUtils.LinearMaterial.SetFloat( m_cachedInvertedZoomId, drawInfo.InvertedZoom );
  1061. UIUtils.LinearMaterial.SetVector( "_Mask", mask );
  1062. bool cached = GL.sRGBWrite;
  1063. GL.sRGBWrite = true;
  1064. //EditorGUI.DrawPreviewTexture( rect, PreviewTexture, UIUtils.LinearMaterial );
  1065. int pass = 0;
  1066. if( SpherePreview )
  1067. {
  1068. if( mask.w == 1 )
  1069. pass = 3;
  1070. else
  1071. pass = 1;
  1072. }
  1073. else if( mask.w == 1 )
  1074. pass = 2;
  1075. Graphics.DrawTexture( rect, PreviewTexture, UIUtils.LinearMaterial, pass );
  1076. GL.sRGBWrite = cached;
  1077. //Preview buttons
  1078. if( m_drawPreviewMaskButtons )
  1079. DrawPreviewMaskButtonsRepaint( drawInfo, rect );
  1080. }
  1081. protected void DrawPreviewMaskButtonsLayout( DrawInfo drawInfo, Rect rect )
  1082. {
  1083. if( rect.Contains( drawInfo.MousePosition ) && m_channelNumber > 1 && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 )
  1084. {
  1085. Rect buttonRect = rect;
  1086. buttonRect.height = 14 * drawInfo.InvertedZoom;
  1087. buttonRect.y = rect.yMax - buttonRect.height;
  1088. buttonRect.width = 14 * drawInfo.InvertedZoom;
  1089. if( m_channelNumber == 2 )
  1090. {
  1091. m_previewChannels[ 0 ] = GUI.Toggle( buttonRect, m_previewChannels[ 0 ], string.Empty, GUIStyle.none );
  1092. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1093. m_previewChannels[ 1 ] = GUI.Toggle( buttonRect, m_previewChannels[ 1 ], string.Empty, GUIStyle.none );
  1094. }
  1095. else if( m_channelNumber == 3 )
  1096. {
  1097. m_previewChannels[ 0 ] = GUI.Toggle( buttonRect, m_previewChannels[ 0 ], string.Empty, GUIStyle.none );
  1098. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1099. m_previewChannels[ 1 ] = GUI.Toggle( buttonRect, m_previewChannels[ 1 ], string.Empty, GUIStyle.none );
  1100. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1101. m_previewChannels[ 2 ] = GUI.Toggle( buttonRect, m_previewChannels[ 2 ], string.Empty, GUIStyle.none );
  1102. }
  1103. else if( m_channelNumber == 4 )
  1104. {
  1105. m_previewChannels[ 0 ] = GUI.Toggle( buttonRect, m_previewChannels[ 0 ], string.Empty, GUIStyle.none );
  1106. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1107. m_previewChannels[ 1 ] = GUI.Toggle( buttonRect, m_previewChannels[ 1 ], string.Empty, GUIStyle.none );
  1108. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1109. m_previewChannels[ 2 ] = GUI.Toggle( buttonRect, m_previewChannels[ 2 ], string.Empty, GUIStyle.none );
  1110. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1111. m_previewChannels[ 3 ] = GUI.Toggle( buttonRect, m_previewChannels[ 3 ], string.Empty, GUIStyle.none );
  1112. }
  1113. }
  1114. }
  1115. protected void DrawPreviewMaskButtonsRepaint( DrawInfo drawInfo, Rect rect )
  1116. {
  1117. if( drawInfo.CurrentEventType == EventType.Repaint && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 && rect.Contains( drawInfo.MousePosition ) && m_channelNumber > 1 )
  1118. {
  1119. Rect buttonRect = rect;
  1120. buttonRect.height = 14 * drawInfo.InvertedZoom;
  1121. buttonRect.y = rect.yMax - buttonRect.height;
  1122. buttonRect.width = 14 * drawInfo.InvertedZoom;
  1123. if( m_channelNumber == 2 )
  1124. {
  1125. UIUtils.MiniButtonTopMid.Draw( buttonRect, "R", false, false, m_previewChannels[ 0 ], false );
  1126. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1127. UIUtils.MiniButtonTopRight.Draw( buttonRect, "G", false, false, m_previewChannels[ 1 ], false );
  1128. }
  1129. else if( m_channelNumber == 3 )
  1130. {
  1131. UIUtils.MiniButtonTopMid.Draw( buttonRect, "R", false, false, m_previewChannels[ 0 ], false );
  1132. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1133. UIUtils.MiniButtonTopMid.Draw( buttonRect, "G", false, false, m_previewChannels[ 1 ], false );
  1134. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1135. UIUtils.MiniButtonTopRight.Draw( buttonRect, "B", false, false, m_previewChannels[ 2 ], false );
  1136. }
  1137. else if( m_channelNumber == 4 )
  1138. {
  1139. UIUtils.MiniButtonTopMid.Draw( buttonRect, "R", false, false, m_previewChannels[ 0 ], false );
  1140. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1141. UIUtils.MiniButtonTopMid.Draw( buttonRect, "G", false, false, m_previewChannels[ 1 ], false );
  1142. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1143. UIUtils.MiniButtonTopMid.Draw( buttonRect, "B", false, false, m_previewChannels[ 2 ], false );
  1144. buttonRect.x += 14 * drawInfo.InvertedZoom;
  1145. UIUtils.MiniButtonTopRight.Draw( buttonRect, "A", false, false, m_previewChannels[ 3 ], false );
  1146. }
  1147. }
  1148. }
  1149. public void SetTimedUpdate( double timerInterval )
  1150. {
  1151. m_timedUpdateInitialValue = EditorApplication.timeSinceStartup;
  1152. m_timedUpdateInterval = timerInterval;
  1153. m_fireTimedUpdateRequest = true;
  1154. }
  1155. public virtual void FireTimedUpdate() { }
  1156. /// <summary>
  1157. ///
  1158. /// </summary>
  1159. /// <param name="drawInfo"></param>
  1160. public virtual void OnNodeLogicUpdate( DrawInfo drawInfo )
  1161. {
  1162. if( m_fireTimedUpdateRequest && ( EditorApplication.timeSinceStartup - m_timedUpdateInitialValue ) > m_timedUpdateInterval )
  1163. {
  1164. m_fireTimedUpdateRequest = false;
  1165. FireTimedUpdate();
  1166. }
  1167. if( m_repopulateDictionaries )
  1168. {
  1169. m_repopulateDictionaries = false;
  1170. m_inputPortsDict.Clear();
  1171. int inputCount = m_inputPorts.Count;
  1172. for( int i = 0; i < inputCount; i++ )
  1173. {
  1174. m_inputPortsDict.Add( m_inputPorts[ i ].PortId, m_inputPorts[ i ] );
  1175. }
  1176. m_outputPortsDict.Clear();
  1177. int outputCount = m_outputPorts.Count;
  1178. for( int i = 0; i < outputCount; i++ )
  1179. {
  1180. m_outputPortsDict.Add( m_outputPorts[ i ].PortId, m_outputPorts[ i ] );
  1181. }
  1182. }
  1183. }
  1184. private void UpdateSortedInputPorts()
  1185. {
  1186. if ( m_sortInputPorts )
  1187. {
  1188. m_sortedInputPorts.Clear();
  1189. m_sortedInputPorts.AddRange( m_inputPorts );
  1190. m_sortedInputPorts.Sort( ( x, y ) => x.OrderId.CompareTo( y.OrderId ) );
  1191. }
  1192. else
  1193. {
  1194. m_sortedInputPorts = m_inputPorts;
  1195. }
  1196. }
  1197. private void UpdateSortedOutputPorts()
  1198. {
  1199. if ( m_sortOutputPorts )
  1200. {
  1201. m_sortedOutputPorts.Clear();
  1202. m_sortedOutputPorts.AddRange( m_outputPorts );
  1203. m_sortedOutputPorts.Sort( ( x, y ) => x.OrderId.CompareTo( y.OrderId ) );
  1204. }
  1205. else
  1206. {
  1207. m_sortedOutputPorts = m_outputPorts;
  1208. }
  1209. }
  1210. /// <summary>
  1211. /// This method should only be called to calculate layouts of elements to be draw later, only runs once per frame and before wires are drawn
  1212. /// </summary>
  1213. /// <param name="drawInfo"></param>
  1214. public virtual void OnNodeLayout( DrawInfo drawInfo )
  1215. {
  1216. if( ContainerGraph.ChangedLightingModel )
  1217. {
  1218. m_sizeIsDirty = true;
  1219. m_firstPreviewDraw = true;
  1220. }
  1221. if( m_firstDraw )
  1222. {
  1223. m_firstDraw = false;
  1224. AfterCommonInit();
  1225. OnNodeChange();
  1226. }
  1227. if( m_previousErrorMessage != m_showErrorMessage )
  1228. {
  1229. m_sizeIsDirty = true;
  1230. }
  1231. if( m_sizeIsDirty )
  1232. {
  1233. m_sizeIsDirty = false;
  1234. ChangeSize();
  1235. UpdateSortedInputPorts();
  1236. UpdateSortedOutputPorts();
  1237. }
  1238. CalculatePositionAndVisibility( drawInfo );
  1239. m_unscaledRemainingBox = m_position;
  1240. m_remainingBox = m_globalPosition;
  1241. m_lastInputBottomRight = m_position.position;
  1242. m_lastOutputBottomLeft = m_position.position;
  1243. m_lastOutputBottomLeft.x += m_position.width;
  1244. m_visibleInputs = 0;
  1245. m_visibleOutputs = 0;
  1246. if( m_hasSubtitle )
  1247. m_extraHeaderHeight = (int)Constants.NODE_HEADER_EXTRA_HEIGHT;
  1248. else
  1249. m_extraHeaderHeight = 0;
  1250. m_lastInputBottomRight.y += UIUtils.HeaderMaxHeight + m_extraHeaderHeight;
  1251. m_lastOutputBottomLeft.y += UIUtils.HeaderMaxHeight + m_extraHeaderHeight;
  1252. m_unscaledRemainingBox.y += UIUtils.HeaderMaxHeight + m_extraHeaderHeight;
  1253. if( m_isVisible )
  1254. {
  1255. // Header
  1256. m_headerPosition = m_globalPosition;
  1257. m_headerPosition.height = UIUtils.CurrentHeaderHeight + m_extraHeaderHeight * drawInfo.InvertedZoom;
  1258. // Title
  1259. m_titlePos = m_globalPosition;
  1260. m_titlePos.height = m_headerPosition.height;
  1261. if( m_hasSubtitle )
  1262. m_titlePos.yMin += ( 2 * drawInfo.InvertedZoom );
  1263. else
  1264. m_titlePos.yMin += ( 8 * drawInfo.InvertedZoom );
  1265. m_titlePos.width -= ( m_paddingTitleLeft + m_paddingTitleRight ) * drawInfo.InvertedZoom;
  1266. m_titlePos.x += m_paddingTitleLeft * drawInfo.InvertedZoom;
  1267. // Additional Title
  1268. if( m_hasSubtitle )
  1269. {
  1270. m_addTitlePos = m_titlePos;
  1271. m_addTitlePos.y = m_globalPosition.y;
  1272. m_addTitlePos.yMin += ( 15 * drawInfo.InvertedZoom );
  1273. }
  1274. // Left Dropdown
  1275. if( m_hasLeftDropdown && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1276. {
  1277. m_dropdownRect = m_headerPosition;
  1278. m_dropdownRect.width = Constants.NodeButtonSizeX * drawInfo.InvertedZoom;
  1279. m_dropdownRect.x = m_globalPosition.x + ( Constants.IconsLeftRightMargin + 1 ) * drawInfo.InvertedZoom;
  1280. m_dropdownRect.height = Constants.NodeButtonSizeY * drawInfo.InvertedZoom;
  1281. m_dropdownRect.y = m_globalPosition.y + m_headerPosition.height * 0.5f - 14 * drawInfo.InvertedZoom * 0.5f;
  1282. }
  1283. // Expander
  1284. if( m_drawPreviewExpander && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1285. {
  1286. m_expandRect = m_globalPosition;
  1287. m_expandRect.width = Constants.PreviewExpanderWidth * drawInfo.InvertedZoom;
  1288. m_expandRect.x = m_globalPosition.x + m_globalPosition.width - ( Constants.IconsLeftRightMargin + Constants.PreviewExpanderWidth ) * drawInfo.InvertedZoom; //titlePos.x + titlePos.width;
  1289. m_expandRect.height = Constants.PreviewExpanderHeight * drawInfo.InvertedZoom;
  1290. m_expandRect.y = m_globalPosition.y + m_headerPosition.height * 0.5f - Constants.PreviewExpanderHeight * drawInfo.InvertedZoom * 0.5f;
  1291. }
  1292. }
  1293. if( m_anchorAdjust < 0 )
  1294. {
  1295. m_anchorAdjust = UIUtils.GetCustomStyle( CustomStyle.PortEmptyIcon ).normal.background.width;
  1296. }
  1297. m_unscaledRemainingBox.y += Constants.INPUT_PORT_DELTA_Y;
  1298. m_lastOutputBottomLeft.y += Constants.INPUT_PORT_DELTA_Y;
  1299. m_lastInputBottomRight.y += Constants.INPUT_PORT_DELTA_Y;
  1300. // Input Ports
  1301. {
  1302. m_currInputPortPos = m_globalPosition;
  1303. m_currInputPortPos.width = drawInfo.InvertedZoom * UIUtils.PortsSize.x;
  1304. m_currInputPortPos.height = drawInfo.InvertedZoom * UIUtils.PortsSize.y;
  1305. m_currInputPortPos.x += drawInfo.InvertedZoom * Constants.PORT_INITIAL_X;
  1306. m_currInputPortPos.y += drawInfo.InvertedZoom * Constants.PORT_INITIAL_Y + m_extraHeaderHeight * drawInfo.InvertedZoom;
  1307. int inputCount = m_sortedInputPorts.Count;
  1308. float initialX = m_lastInputBottomRight.x;
  1309. for ( int i = 0; i < inputCount; i++ )
  1310. {
  1311. if( m_sortedInputPorts[ i ].Visible )
  1312. {
  1313. m_visibleInputs++;
  1314. // Button
  1315. m_sortedInputPorts[ i ].Position = m_currInputPortPos;
  1316. // Label
  1317. m_sortedInputPorts[ i ].LabelPosition = m_currInputPortPos;
  1318. float deltaX = 1f * drawInfo.InvertedZoom * ( UIUtils.PortsSize.x + Constants.PORT_TO_LABEL_SPACE_X );
  1319. m_auxRect = m_sortedInputPorts[ i ].LabelPosition;
  1320. m_auxRect.x += deltaX;
  1321. m_sortedInputPorts[ i ].LabelPosition = m_auxRect;
  1322. //if( m_sortedInputPorts[ i ].DirtyLabelSize || m_sortedInputPorts[ i ].LabelSize == Vector2.zero )
  1323. //{
  1324. // m_sortedInputPorts[ i ].DirtyLabelSize = false;
  1325. // m_sizeContentAux.text = m_sortedInputPorts[ i ].Name;
  1326. // m_sortedInputPorts[ i ].UnscaledLabelSize = UIUtils.UnZoomedInputPortStyle.CalcSize( m_sizeContentAux );
  1327. //}
  1328. m_sortedInputPorts[ i ].LabelSize = m_sortedInputPorts[ i ].UnscaledLabelSize * drawInfo.InvertedZoom;
  1329. m_lastInputBottomRight.x = Mathf.Max( m_lastInputBottomRight.x, initialX + m_sortedInputPorts[ i ].UnscaledLabelSize.x + Constants.PORT_INITIAL_X + Constants.PORT_TO_LABEL_SPACE_X + UIUtils.PortsSize.x );
  1330. if( !m_sortedInputPorts[ i ].Locked )
  1331. {
  1332. float overflow = 2;
  1333. float scaledOverflow = 4 * drawInfo.InvertedZoom;
  1334. m_auxRect = m_currInputPortPos;
  1335. m_auxRect.yMin -= scaledOverflow + overflow;
  1336. m_auxRect.yMax += scaledOverflow + overflow;
  1337. m_auxRect.xMin -= Constants.PORT_INITIAL_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1338. if( m_containerGraph.ParentWindow.WireReferenceUtils.OutputPortReference.IsValid )
  1339. m_auxRect.xMax += m_sortedInputPorts[ i ].LabelSize.x + Constants.PORT_TO_LABEL_SPACE_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1340. else
  1341. m_auxRect.xMax += Constants.PORT_TO_LABEL_SPACE_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1342. m_sortedInputPorts[ i ].ActivePortArea = m_auxRect;
  1343. }
  1344. m_currInputPortPos.y += drawInfo.InvertedZoom * ( m_fontHeight + Constants.INPUT_PORT_DELTA_Y );
  1345. //GUI.Label( m_sortedInputPorts[ i ].ActivePortArea, string.Empty, UIUtils.Box );
  1346. }
  1347. }
  1348. if( m_visibleInputs > 0 )
  1349. m_lastInputBottomRight.y += m_fontHeight * m_visibleInputs + Constants.INPUT_PORT_DELTA_Y * ( m_visibleInputs - 1 );
  1350. }
  1351. // Output Ports
  1352. {
  1353. m_currOutputPortPos = m_globalPosition;
  1354. m_currOutputPortPos.width = drawInfo.InvertedZoom * UIUtils.PortsSize.x;
  1355. m_currOutputPortPos.height = drawInfo.InvertedZoom * UIUtils.PortsSize.y;
  1356. m_currOutputPortPos.x += ( m_globalPosition.width - drawInfo.InvertedZoom * ( Constants.PORT_INITIAL_X + m_anchorAdjust ) );
  1357. m_currOutputPortPos.y += drawInfo.InvertedZoom * Constants.PORT_INITIAL_Y + m_extraHeaderHeight * drawInfo.InvertedZoom;
  1358. float initialX = m_lastOutputBottomLeft.x;
  1359. int outputCount = m_sortedOutputPorts.Count;
  1360. for ( int i = 0; i < outputCount; i++ )
  1361. {
  1362. if( m_sortedOutputPorts[ i ].Visible )
  1363. {
  1364. m_visibleOutputs++;
  1365. //Button
  1366. m_sortedOutputPorts[ i ].Position = m_currOutputPortPos;
  1367. // Label
  1368. m_sortedOutputPorts[ i ].LabelPosition = m_currOutputPortPos;
  1369. float deltaX = 1f * drawInfo.InvertedZoom * ( UIUtils.PortsSize.x + Constants.PORT_TO_LABEL_SPACE_X );
  1370. m_auxRect = m_sortedOutputPorts[ i ].LabelPosition;
  1371. m_auxRect.x -= deltaX;
  1372. m_sortedOutputPorts[ i ].LabelPosition = m_auxRect;
  1373. m_sortedOutputPorts[ i ].LabelSize = m_sortedOutputPorts[ i ].UnscaledLabelSize * drawInfo.InvertedZoom;
  1374. m_lastOutputBottomLeft.x = Mathf.Min( m_lastOutputBottomLeft.x, initialX - m_sortedOutputPorts[ i ].UnscaledLabelSize.x - Constants.PORT_INITIAL_X - Constants.PORT_TO_LABEL_SPACE_X - UIUtils.PortsSize.x );
  1375. if( !m_sortedOutputPorts[ i ].Locked )
  1376. {
  1377. float overflow = 2;
  1378. float scaledOverflow = 4 * drawInfo.InvertedZoom;
  1379. m_auxRect = m_currOutputPortPos;
  1380. m_auxRect.yMin -= scaledOverflow + overflow;
  1381. m_auxRect.yMax += scaledOverflow + overflow;
  1382. if( m_containerGraph.ParentWindow.WireReferenceUtils.InputPortReference.IsValid )
  1383. m_auxRect.xMin -= m_sortedOutputPorts[ i ].LabelSize.x + Constants.PORT_TO_LABEL_SPACE_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1384. else
  1385. m_auxRect.xMin -= Constants.PORT_TO_LABEL_SPACE_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1386. m_auxRect.xMax += Constants.PORT_INITIAL_X * drawInfo.InvertedZoom + scaledOverflow + overflow;
  1387. m_sortedOutputPorts[ i ].ActivePortArea = m_auxRect;
  1388. }
  1389. m_currOutputPortPos.y += drawInfo.InvertedZoom * ( m_fontHeight + Constants.INPUT_PORT_DELTA_Y );
  1390. //GUI.Label( m_sortedOutputPorts[ i ].ActivePortArea, string.Empty, UIUtils.Box );
  1391. }
  1392. }
  1393. if( m_visibleOutputs > 0 )
  1394. m_lastOutputBottomLeft.y += m_fontHeight * m_visibleOutputs + Constants.INPUT_PORT_DELTA_Y * ( m_visibleOutputs - 1 );
  1395. }
  1396. m_lastInputBottomRight.x += m_marginPreviewLeft;
  1397. //Vector2 scaledLastOutputBottomLeft = ( m_lastOutputBottomLeft + drawInfo.CameraOffset ) * drawInfo.InvertedZoom;
  1398. //GUI.Label( new Rect( scaledLastOutputBottomLeft, Vector2.one * 2 ), string.Empty, UIUtils.CurrentWindow.CustomStylesInstance.Box );
  1399. m_unscaledRemainingBox.xMin = m_lastInputBottomRight.x;
  1400. //m_unscaledRemainingBox.yMin = m_lastInputBottomRight.y;
  1401. m_unscaledRemainingBox.xMax = m_lastOutputBottomLeft.x;
  1402. m_unscaledRemainingBox.yMax = Mathf.Max( m_lastOutputBottomLeft.y, m_lastInputBottomRight.y );
  1403. m_remainingBox.position = ( m_unscaledRemainingBox.position + drawInfo.CameraOffset ) * drawInfo.InvertedZoom;
  1404. m_remainingBox.size = m_unscaledRemainingBox.size * drawInfo.InvertedZoom;
  1405. //GUI.Label( m_remainingBox, string.Empty, UIUtils.Box );
  1406. if( m_visibleInputs == 0 )
  1407. {
  1408. m_remainingBox.x += Constants.PORT_INITIAL_X * drawInfo.InvertedZoom;
  1409. m_remainingBox.width -= Constants.PORT_INITIAL_X * drawInfo.InvertedZoom;
  1410. }
  1411. if( m_visibleOutputs == 0 )
  1412. {
  1413. m_remainingBox.width -= Constants.PORT_INITIAL_X * drawInfo.InvertedZoom;
  1414. }
  1415. if( ContainerGraph.ParentWindow.GlobalPreview != m_globalShowPreview )
  1416. {
  1417. m_globalShowPreview = ContainerGraph.ParentWindow.GlobalPreview;
  1418. m_sizeIsDirty = true;
  1419. ContainerGraph.ParentWindow.RequestRepaint();
  1420. }
  1421. // Generate Proper Preview Rect
  1422. float marginAround = 10;
  1423. float scaledMarginAround = marginAround * drawInfo.InvertedZoom;
  1424. float previewSize = 128;
  1425. PreviewLocation m_autoLocation = m_selectedLocation;
  1426. if( m_selectedLocation == PreviewLocation.Auto )
  1427. {
  1428. if( m_visibleOutputs > m_visibleInputs )
  1429. {
  1430. m_autoLocation = PreviewLocation.Left;
  1431. }
  1432. else if( m_visibleOutputs < m_visibleInputs )
  1433. {
  1434. m_autoLocation = PreviewLocation.Right;
  1435. }
  1436. else if( m_unscaledRemainingBox.width > previewSize )
  1437. {
  1438. m_autoLocation = PreviewLocation.TopCenter;
  1439. }
  1440. else
  1441. {
  1442. m_autoLocation = PreviewLocation.BottomCenter;
  1443. }
  1444. }
  1445. if( m_canExpand && ( m_showPreview || m_globalShowPreview ) )
  1446. {
  1447. if( m_autoLocation == PreviewLocation.TopCenter )
  1448. {
  1449. m_unscaledPreviewRect.y = m_unscaledRemainingBox.y;
  1450. m_unscaledPreviewRect.x = m_unscaledRemainingBox.center.x - 0.5f * ( previewSize + 2 * marginAround );
  1451. }
  1452. else if( m_autoLocation == PreviewLocation.BottomCenter )
  1453. {
  1454. m_unscaledPreviewRect.y = Mathf.Max( m_lastOutputBottomLeft.y, m_lastInputBottomRight.y );
  1455. m_unscaledPreviewRect.x = m_position.x + 0.5f * m_position.width - 0.5f * ( previewSize + 2 * marginAround );
  1456. }
  1457. else if( m_autoLocation == PreviewLocation.Left )
  1458. {
  1459. m_unscaledPreviewRect.y = m_lastInputBottomRight.y;
  1460. m_unscaledPreviewRect.x = m_position.x;
  1461. }
  1462. else if( m_autoLocation == PreviewLocation.Right )
  1463. {
  1464. m_unscaledPreviewRect.y = m_lastOutputBottomLeft.y;
  1465. m_unscaledPreviewRect.x = m_lastInputBottomRight.x;
  1466. }
  1467. if( m_autoLocation == PreviewLocation.BottomCenter )
  1468. m_unscaledPreviewRect.height = previewSize + 2 * marginAround;
  1469. else if( m_autoLocation == PreviewLocation.TopCenter )
  1470. m_unscaledPreviewRect.height = previewSize + marginAround;
  1471. else
  1472. m_unscaledPreviewRect.height = previewSize + ( m_visibleInputs > 0 && m_visibleOutputs > 0 ? 2 * marginAround : marginAround );
  1473. m_unscaledPreviewRect.width = previewSize + 2 * marginAround;
  1474. m_previewRect = m_unscaledPreviewRect;
  1475. m_previewRect.position = ( m_previewRect.position + drawInfo.CameraOffset ) * drawInfo.InvertedZoom;
  1476. m_auxVector2.Set( previewSize * drawInfo.InvertedZoom, previewSize * drawInfo.InvertedZoom );
  1477. m_previewRect.size = m_auxVector2;
  1478. if( m_autoLocation == PreviewLocation.BottomCenter )
  1479. {
  1480. m_auxVector2.Set( m_previewRect.position.x + scaledMarginAround, m_previewRect.position.y + scaledMarginAround );
  1481. m_previewRect.position = m_auxVector2;
  1482. }
  1483. else if( m_autoLocation == PreviewLocation.TopCenter )
  1484. {
  1485. m_auxVector2.Set( m_previewRect.position.x + scaledMarginAround, m_previewRect.position.y );
  1486. m_previewRect.position = m_auxVector2;
  1487. }
  1488. else
  1489. {
  1490. m_previewRect.position += new Vector2( scaledMarginAround, ( m_visibleInputs > 0 && m_visibleOutputs > 0 ? scaledMarginAround : 0 ) );
  1491. }
  1492. }
  1493. // Adjust node rect after preview
  1494. if( m_firstPreviewDraw )
  1495. {
  1496. m_firstPreviewDraw = false;
  1497. ContainerGraph.ParentWindow.RequestRepaint();
  1498. if( m_canExpand && ( m_showPreview || m_globalShowPreview ) )
  1499. {
  1500. if( m_autoLocation == PreviewLocation.TopCenter )
  1501. {
  1502. float fillWidth = m_unscaledRemainingBox.width - m_unscaledPreviewRect.width;
  1503. m_extraSize.x = Mathf.Max( -fillWidth, 0 );
  1504. float fillHeight = m_position.yMax - m_unscaledPreviewRect.yMax;
  1505. m_extraSize.y = Mathf.Max( -fillHeight, 0 );
  1506. }
  1507. if( m_autoLocation == PreviewLocation.BottomCenter )
  1508. {
  1509. float fillWidth = m_position.width - m_unscaledPreviewRect.width;
  1510. m_extraSize.x = Mathf.Max( -fillWidth, 0 );
  1511. float fillHeight = m_position.yMax - m_unscaledPreviewRect.yMax;
  1512. m_extraSize.y = Mathf.Max( -fillHeight, 0 );
  1513. }
  1514. else if( m_autoLocation == PreviewLocation.Left )
  1515. {
  1516. float fillWidth = m_lastOutputBottomLeft.x - m_unscaledPreviewRect.xMax;
  1517. m_extraSize.x = Mathf.Max( -fillWidth, 0 );
  1518. float fillHeight = m_position.yMax - m_unscaledPreviewRect.yMax;
  1519. m_extraSize.y = Mathf.Max( -fillHeight, 0 );
  1520. }
  1521. else if( m_autoLocation == PreviewLocation.Right )
  1522. {
  1523. float fillWidth = m_position.xMax - m_unscaledPreviewRect.xMax;
  1524. m_extraSize.x = Mathf.Max( -fillWidth, 0 );
  1525. float fillHeight = m_position.yMax - m_unscaledPreviewRect.yMax;
  1526. m_extraSize.y = Mathf.Max( -fillHeight, 0 );
  1527. }
  1528. if( m_showErrorMessage )
  1529. m_extraSize.y += 24;
  1530. }
  1531. else if( m_canExpand )
  1532. {
  1533. m_extraSize.y = 0;
  1534. m_extraSize.x = 0;
  1535. }
  1536. m_position.width = m_unpreviewedPosition.width + m_extraSize.x;
  1537. m_position.height = m_unpreviewedPosition.height + m_extraSize.y;
  1538. }
  1539. if( m_showErrorMessage )
  1540. {
  1541. m_errorBox = m_globalPosition;
  1542. m_errorBox.y = ( m_globalPosition.yMax - 28 * drawInfo.InvertedZoom ) + 3 * drawInfo.InvertedZoom;
  1543. m_errorBox.height = 25 * drawInfo.InvertedZoom;
  1544. }
  1545. m_previousErrorMessage = m_showErrorMessage;
  1546. }
  1547. /// <summary>
  1548. /// This method should only be called to draw elements, runs once per frame and after wires are drawn
  1549. /// </summary>
  1550. /// <param name="drawInfo"></param>
  1551. public virtual void OnNodeRepaint( DrawInfo drawInfo )
  1552. {
  1553. if( !m_isVisible )
  1554. return;
  1555. m_colorBuffer = GUI.color;
  1556. // Background
  1557. GUI.color = m_infiniteLoopDetected ? Constants.InfiniteLoopColor : Constants.NodeBodyColor;
  1558. if( m_useSquareNodeTitle || ContainerGraph.LodLevel >= ParentGraph.NodeLOD.LOD2 )
  1559. GUI.Label( m_globalPosition, string.Empty, UIUtils.NodeWindowOffSquare );
  1560. else
  1561. GUI.Label( m_globalPosition, string.Empty, UIUtils.GetCustomStyle( CustomStyle.NodeWindowOff ) );
  1562. // Header
  1563. //GUI
  1564. GUI.color = m_headerColor * m_headerColorModifier;
  1565. if( m_useSquareNodeTitle || ContainerGraph.LodLevel >= ParentGraph.NodeLOD.LOD2 )
  1566. GUI.Label( m_headerPosition, string.Empty, UIUtils.NodeHeaderSquare );
  1567. else
  1568. GUI.Label( m_headerPosition, string.Empty, UIUtils.GetCustomStyle( CustomStyle.NodeHeader ) );
  1569. GUI.color = m_colorBuffer;
  1570. // @diogo: shader function icon
  1571. var functionNode = this as FunctionNode;
  1572. if ( functionNode != null )
  1573. {
  1574. float scaledSize = FunctionNode.HeaderIconSize * drawInfo.InvertedZoom;
  1575. GUI.DrawTexture( new Rect( m_globalPosition.x, m_globalPosition.y, scaledSize, scaledSize ), functionNode.HeaderIcon, ScaleMode.ScaleAndCrop, true );
  1576. }
  1577. // Title
  1578. DrawTitle( m_titlePos );
  1579. // Additional Tile
  1580. if( m_hasSubtitle && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  1581. GUI.Label( m_addTitlePos, m_additionalContent, UIUtils.GetCustomStyle( CustomStyle.PropertyValuesTitle ) );
  1582. // Dropdown
  1583. if( m_hasLeftDropdown && !m_dropdownEditing && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1584. GUI.Label( m_dropdownRect, string.Empty, UIUtils.PropertyPopUp );
  1585. // Expander
  1586. if( m_drawPreviewExpander && ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1587. GUI.Label( m_expandRect, string.Empty, ( m_showPreview ? UIUtils.PreviewCollapser : UIUtils.PreviewExpander ) );
  1588. // Input Ports
  1589. int inputCount = m_inputPorts.Count;
  1590. for( int i = 0; i < inputCount; i++ )
  1591. {
  1592. if( m_inputPorts[ i ].Visible )
  1593. {
  1594. // Input Port Icon
  1595. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1596. {
  1597. if( m_inputPorts[ i ].Locked )
  1598. GUI.color = Constants.LockedPortColor;
  1599. else if( ContainerGraph.ParentWindow.Options.ColoredPorts )
  1600. GUI.color = UIUtils.GetColorForDataType( m_inputPorts[ i ].DataType, false, true );
  1601. else
  1602. GUI.color = m_inputPorts[ i ].HasCustomColor ? m_inputPorts[ i ].CustomColor : UIUtils.GetColorForDataType( m_inputPorts[ i ].DataType, true, true );
  1603. GUIStyle style = m_inputPorts[ i ].IsConnected ? UIUtils.GetCustomStyle( CustomStyle.PortFullIcon ) : UIUtils.GetCustomStyle( CustomStyle.PortEmptyIcon );
  1604. GUI.Label( m_inputPorts[ i ].Position, string.Empty, style );
  1605. GUI.color = m_colorBuffer;
  1606. }
  1607. // Input Port Label
  1608. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  1609. {
  1610. if( m_inputPorts[ i ].Locked )
  1611. {
  1612. GUI.color = Constants.PortLockedTextColor;
  1613. GUI.Label( m_inputPorts[ i ].LabelPosition, m_inputPorts[ i ].Name, UIUtils.InputPortLabel );
  1614. GUI.color = m_colorBuffer;
  1615. }
  1616. else
  1617. {
  1618. if( m_containerGraph.ParentWindow.GlobalShowInternalData && !m_inputPorts[ i ].IsConnected && UIUtils.InternalDataOnPort.fontSize > 1f && ( m_inputPorts[ i ].AutoDrawInternalData || ( m_autoDrawInternalPortData && m_useInternalPortData ) ) && m_inputPorts[ i ].DisplayInternalData.Length > 4 && ( ( m_inputPorts[ i ].DataType >= WirePortDataType.FLOAT && m_inputPorts[ i ].DataType <= WirePortDataType.INT ) || m_inputPorts[ i ].DataType == WirePortDataType.UINT ) )
  1619. {
  1620. GUI.color = Constants.NodeBodyColor/* * new Color( 1f, 1f, 1f, 0.75f )*/;
  1621. Rect internalBox = m_inputPorts[ i ].LabelPosition;
  1622. m_sizeContentAux.text = m_inputPorts[ i ].DisplayInternalData;
  1623. Vector2 portText = UIUtils.InternalDataOnPort.CalcSize( m_sizeContentAux );
  1624. internalBox.width = portText.x;
  1625. internalBox.height = portText.y;
  1626. internalBox.y = m_inputPorts[ i ].LabelPosition.center.y - internalBox.height * 0.5f;
  1627. internalBox.x = GlobalPosition.x - internalBox.width - 4 * drawInfo.InvertedZoom - 1;
  1628. Rect backBox = new Rect( internalBox );
  1629. backBox.xMin -= 4 * drawInfo.InvertedZoom;
  1630. backBox.xMax += 4 * drawInfo.InvertedZoom;
  1631. backBox.yMin -= 2 * drawInfo.InvertedZoom;
  1632. backBox.yMax += 2 * drawInfo.InvertedZoom;
  1633. GUI.Label( backBox, string.Empty, UIUtils.InternalDataBackground );
  1634. GUI.color *= new Color( 1f, 1f, 1f, 0.5f );
  1635. GUI.Label( internalBox, m_sizeContentAux, UIUtils.InternalDataOnPort );
  1636. GUI.color = m_colorBuffer;
  1637. }
  1638. GUI.Label( m_inputPorts[ i ].LabelPosition, m_inputPorts[ i ].Name, UIUtils.InputPortLabel );
  1639. }
  1640. }
  1641. }
  1642. }
  1643. // Output Ports
  1644. int outputCount = m_outputPorts.Count;
  1645. for( int i = 0; i < outputCount; i++ )
  1646. {
  1647. if( m_outputPorts[ i ].Visible )
  1648. {
  1649. // Output Port Icon
  1650. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1651. {
  1652. if( m_outputPorts[ i ].Locked )
  1653. GUI.color = Constants.LockedPortColor;
  1654. else if( ContainerGraph.ParentWindow.Options.ColoredPorts )
  1655. GUI.color = UIUtils.GetColorForDataType( m_outputPorts[ i ].DataType, false, false );
  1656. else
  1657. GUI.color = m_outputPorts[ i ].HasCustomColor ? m_outputPorts[ i ].CustomColor : UIUtils.GetColorForDataType( m_outputPorts[ i ].DataType, true, false );
  1658. GUIStyle style = m_outputPorts[ i ].IsConnected ? UIUtils.GetCustomStyle( CustomStyle.PortFullIcon ) : UIUtils.GetCustomStyle( CustomStyle.PortEmptyIcon );
  1659. GUI.Label( m_outputPorts[ i ].Position, string.Empty, style );
  1660. GUI.color = m_colorBuffer;
  1661. }
  1662. // Output Port Label
  1663. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD3 )
  1664. {
  1665. if( m_outputPorts[ i ].Locked )
  1666. {
  1667. GUI.color = Constants.PortLockedTextColor;
  1668. GUI.Label( m_outputPorts[ i ].LabelPosition, m_outputPorts[ i ].Name, UIUtils.OutputPortLabel );
  1669. GUI.color = m_colorBuffer;
  1670. }
  1671. else
  1672. {
  1673. GUI.Label( m_outputPorts[ i ].LabelPosition, m_outputPorts[ i ].Name, UIUtils.OutputPortLabel );
  1674. }
  1675. }
  1676. }
  1677. }
  1678. // Preview
  1679. if( ( m_showPreview || m_globalShowPreview ) && m_drawPreview )
  1680. DrawPreview( drawInfo, m_previewRect );
  1681. // Error and Warning bottom message
  1682. if( m_showErrorMessage )
  1683. {
  1684. GUI.color = new Color( 0.0f, 0.0f, 0.0f, 0.5f );
  1685. GUI.Label( m_errorBox, string.Empty, UIUtils.Separator );
  1686. GUI.color = m_colorBuffer;
  1687. switch( m_errorMessageTypeIsError )
  1688. {
  1689. default:
  1690. case NodeMessageType.Error:
  1691. {
  1692. m_errorMessage.text = ErrorTitle;
  1693. m_errorIcon.image = UIUtils.SmallErrorIcon;
  1694. m_errorCurrentStyle = UIUtils.BoldErrorStyle;
  1695. }
  1696. break;
  1697. case NodeMessageType.Warning:
  1698. {
  1699. m_errorMessage.text = WarningTitle;
  1700. m_errorIcon.image = UIUtils.SmallWarningIcon;
  1701. m_errorCurrentStyle = UIUtils.BoldWarningStyle;
  1702. }
  1703. break;
  1704. case NodeMessageType.Info:
  1705. {
  1706. m_errorMessage.text = InfoTitle;
  1707. m_errorIcon.image = UIUtils.SmallInfoIcon;
  1708. m_errorCurrentStyle = UIUtils.BoldInfoStyle;
  1709. }
  1710. break;
  1711. }
  1712. Rect textBox = m_errorBox;
  1713. textBox.y += 1 * drawInfo.InvertedZoom;
  1714. textBox.height = 24 * drawInfo.InvertedZoom;
  1715. float textWidth = m_errorCurrentStyle.CalcSize( m_errorMessage ).x;
  1716. GUI.Label( textBox, m_errorMessage, m_errorCurrentStyle );
  1717. textBox.x -= textWidth * 0.5f + 12 * drawInfo.InvertedZoom;
  1718. GUI.Label( textBox, m_errorIcon, m_errorCurrentStyle );
  1719. textBox.x += textWidth + 24 * drawInfo.InvertedZoom;
  1720. GUI.Label( textBox, m_errorIcon, m_errorCurrentStyle );
  1721. }
  1722. // Selection Box
  1723. if( m_selected )
  1724. {
  1725. GUI.color = Constants.NodeSelectedColor;
  1726. if( m_useSquareNodeTitle || ContainerGraph.LodLevel >= ParentGraph.NodeLOD.LOD2 )
  1727. GUI.Label( m_globalPosition, string.Empty, UIUtils.NodeWindowOnSquare );
  1728. else
  1729. GUI.Label( m_globalPosition, string.Empty, UIUtils.GetCustomStyle( CustomStyle.NodeWindowOn ) );
  1730. GUI.color = m_colorBuffer;
  1731. }
  1732. // Debug Visualizers
  1733. //if( FinishPreviewRender || m_continuousPreviewRefresh )
  1734. //{
  1735. // GUI.color = new Color( 0, 1, 0.5f, 1f );
  1736. // Rect r = m_globalPosition;
  1737. // r.width = 8;
  1738. // r.height = 8;
  1739. // r.x -= 5 * drawInfo.InvertedZoom;
  1740. // r.y -= 5 * drawInfo.InvertedZoom;
  1741. // GUI.Label( r, string.Empty, UIUtils.GetCustomStyle( CustomStyle.PortFullIcon ) );
  1742. // GUI.color = m_colorBuffer;
  1743. // FinishPreviewRender = false;
  1744. //}
  1745. //GUI.Label( m_remainingBox, string.Empty, UIUtils.Box );
  1746. }
  1747. public bool DropdownEditing { get { return m_dropdownEditing; } set { m_dropdownEditing = value; PreviewIsDirty = true; } }
  1748. public void DisablePreview()
  1749. {
  1750. m_showPreview = false;
  1751. m_sizeIsDirty = true;
  1752. }
  1753. /// <summary>
  1754. /// Handles gui controls, runs before node layout
  1755. /// </summary>
  1756. /// <param name="drawInfo"></param>
  1757. public virtual void DrawGUIControls( DrawInfo drawInfo )
  1758. {
  1759. if( !m_initialized )
  1760. return;
  1761. if( !m_isVisible )
  1762. return;
  1763. if( drawInfo.CurrentEventType == EventType.MouseDown && drawInfo.LeftMouseButtonPressed )
  1764. {
  1765. if( m_expandRect.Contains( drawInfo.MousePosition ) )
  1766. {
  1767. m_showPreview = !m_showPreview;
  1768. m_sizeIsDirty = true;
  1769. ContainerGraph.ParentWindow.MouseInteracted = true;
  1770. }
  1771. if( m_hasLeftDropdown && m_dropdownRect.Contains( drawInfo.MousePosition ) )
  1772. {
  1773. m_dropdownEditing = true;
  1774. }
  1775. else if( m_dropdownEditing )
  1776. {
  1777. DropdownEditing = false;
  1778. }
  1779. }
  1780. DrawGuiPorts( drawInfo );
  1781. }
  1782. //public static bool MyRepeatButton( DrawInfo drawInfo, Rect position, string text, GUIStyle style )
  1783. //{
  1784. // if(/* drawInfo.CurrentEventType == EventType.MouseDown &&*/ position.Contains( drawInfo.MousePosition ) )
  1785. // {
  1786. // UIUtils.CurrentWindow.MouseInteracted = true;
  1787. // return true;
  1788. // }
  1789. // return false;
  1790. //}
  1791. public void DrawGuiPorts( DrawInfo drawInfo )
  1792. {
  1793. if( !m_initialized )
  1794. return;
  1795. if( !m_isVisible )
  1796. return;
  1797. if( drawInfo.CurrentEventType == EventType.MouseDown )
  1798. {
  1799. int inputCount = m_inputPorts.Count;
  1800. int outputCount = m_outputPorts.Count;
  1801. for( int i = 0; i < inputCount; i++ )
  1802. {
  1803. if( m_inputPorts[ i ].Visible && !m_inputPorts[ i ].Locked && m_isVisible && m_inputPorts[ i ].ActivePortArea.Contains( drawInfo.MousePosition ) && drawInfo.LeftMouseButtonPressed )
  1804. {
  1805. UIUtils.CurrentWindow.MouseInteracted = true;
  1806. m_inputPorts[ i ].Click();
  1807. // need to put the mouse button on a hot state so it will detect the Mouse Up event correctly on the Editor Window
  1808. int controlID = GUIUtility.GetControlID( FocusType.Passive );
  1809. //int controlID = GUIUtility.GetControlID( "repeatButton".GetHashCode(), FocusType.Passive, m_inputPorts[ i ].ActivePortArea );
  1810. GUIUtility.hotControl = controlID;
  1811. bool saveReference = true;
  1812. if( m_inputPorts[ i ].IsConnected )
  1813. {
  1814. double doubleTapTime = EditorApplication.timeSinceStartup;
  1815. bool doubleTap = ( doubleTapTime - m_doubleClickTimestamp ) < DoubleClickTime;
  1816. m_doubleClickTimestamp = doubleTapTime;
  1817. if( doubleTap )
  1818. {
  1819. m_containerGraph.DeleteConnection( true, UniqueId, m_inputPorts[ i ].PortId, true, true );
  1820. Event.current.Use();
  1821. }
  1822. else
  1823. //if ( AppyModifierToPort( _inputPorts[ i ], true ) )
  1824. //{
  1825. //saveReference = false;
  1826. //}
  1827. if( !ApplyModifierToPort( m_inputPorts[ i ], true ) )
  1828. {
  1829. UIUtils.ShowContextOnPick = false;
  1830. PickInput( m_inputPorts[ i ] );
  1831. }
  1832. saveReference = false;
  1833. }
  1834. if( saveReference && !m_containerGraph.ParentWindow.WireReferenceUtils.InputPortReference.IsValid )
  1835. //if ( !modifierApplied && !UIUtils.InputPortReference.IsValid )
  1836. {
  1837. m_containerGraph.ParentWindow.WireReferenceUtils.SetInputReference( m_uniqueId, m_inputPorts[ i ].PortId, m_inputPorts[ i ].DataType, m_inputPorts[ i ].TypeLocked );
  1838. }
  1839. IsDirty = true;
  1840. inputCount = m_inputPorts.Count;
  1841. }
  1842. }
  1843. for( int i = 0; i < outputCount; i++ )
  1844. {
  1845. if( m_outputPorts[ i ].Visible && m_outputPorts[ i ].ActivePortArea.Contains( drawInfo.MousePosition ) && drawInfo.LeftMouseButtonPressed )
  1846. {
  1847. UIUtils.CurrentWindow.MouseInteracted = true;
  1848. m_outputPorts[ i ].Click();
  1849. // need to put the mouse button on a hot state so it will detect the Mouse Up event correctly on the Editor Window
  1850. int controlID = GUIUtility.GetControlID( FocusType.Passive );
  1851. //int controlID = GUIUtility.GetControlID( "aseRepeatButton".GetHashCode(), FocusType.Passive, m_outputPorts[ i ].ActivePortArea );
  1852. GUIUtility.hotControl = controlID;
  1853. bool saveReference = true;
  1854. if( m_outputPorts[ i ].IsConnected )
  1855. {
  1856. if( ApplyModifierToPort( m_outputPorts[ i ], false ) )
  1857. {
  1858. saveReference = false;
  1859. }
  1860. }
  1861. if( saveReference && !m_containerGraph.ParentWindow.WireReferenceUtils.OutputPortReference.IsValid )
  1862. {
  1863. m_containerGraph.ParentWindow.WireReferenceUtils.SetOutputReference( m_uniqueId, m_outputPorts[ i ].PortId, m_outputPorts[ i ].DataType, false );
  1864. }
  1865. IsDirty = true;
  1866. outputCount = m_outputPorts.Count;
  1867. }
  1868. }
  1869. }
  1870. //Preview buttons
  1871. if( m_drawPreviewMaskButtons && ( drawInfo.CurrentEventType == EventType.MouseDown || drawInfo.CurrentEventType == EventType.MouseUp ) )
  1872. DrawPreviewMaskButtonsLayout( drawInfo, m_previewRect );
  1873. }
  1874. /// <summary>
  1875. /// Can be used to draw an entire node, runs after wires
  1876. /// </summary>
  1877. /// <param name="drawInfo"></param>
  1878. public virtual void Draw( DrawInfo drawInfo )
  1879. {
  1880. if( !m_initialized )
  1881. return;
  1882. if( drawInfo.CurrentEventType == EventType.Repaint )
  1883. OnNodeRepaint( drawInfo );
  1884. }
  1885. public virtual void SetPreviewInputs()
  1886. {
  1887. if( !HasPreviewShader || !m_initialized )
  1888. return;
  1889. int count = m_inputPorts.Count;
  1890. for( int i = 0; i < count; i++ )
  1891. {
  1892. if( m_inputPorts[ i ].IsConnected && m_inputPorts[ i ].InputNodeHasPreview( ContainerGraph ) )
  1893. {
  1894. m_inputPorts[ i ].SetPreviewInputTexture( ContainerGraph );
  1895. }
  1896. else
  1897. {
  1898. m_inputPorts[ i ].SetPreviewInputValue( ContainerGraph );
  1899. }
  1900. }
  1901. }
  1902. public bool SafeDraw( DrawInfo drawInfo )
  1903. {
  1904. EditorGUI.BeginChangeCheck();
  1905. Draw( drawInfo );
  1906. if( EditorGUI.EndChangeCheck() )
  1907. {
  1908. SaveIsDirty = true;
  1909. return true;
  1910. }
  1911. return false;
  1912. }
  1913. public bool ShowTooltip( DrawInfo drawInfo )
  1914. {
  1915. if( string.IsNullOrEmpty( m_tooltipText ) )
  1916. return false;
  1917. if( m_globalPosition.Contains( drawInfo.MousePosition ) || m_linkVisibility )
  1918. {
  1919. if( m_tooltipTimestamp + 0.6f < Time.realtimeSinceStartup || m_linkVisibility )
  1920. {
  1921. bool errorTooltip = false;
  1922. if( m_showErrorMessage && m_errorBox.Contains( drawInfo.MousePosition ) && !string.IsNullOrEmpty( m_errorMessageTooltip ) )
  1923. errorTooltip = true;
  1924. Rect globalTooltipPos = m_globalPosition;
  1925. GUIContent temp = new GUIContent( errorTooltip ? m_errorMessageTooltip : m_tooltipText );
  1926. UIUtils.TooltipBox.wordWrap = false;
  1927. Vector2 optimal = UIUtils.TooltipBox.CalcSize( temp );
  1928. if( optimal.x > 300f )
  1929. {
  1930. UIUtils.TooltipBox.wordWrap = true;
  1931. optimal.x = 300f;
  1932. optimal.y = UIUtils.TooltipBox.CalcHeight( temp, 300f );
  1933. }
  1934. globalTooltipPos.width = Mathf.Max( 120, optimal.x );
  1935. globalTooltipPos.height = optimal.y;
  1936. globalTooltipPos.center = m_globalPosition.center;
  1937. if( !errorTooltip && m_hasTooltipLink )
  1938. globalTooltipPos.height += 16;
  1939. if( errorTooltip )
  1940. globalTooltipPos.y = 10 + m_globalPosition.yMax;
  1941. else
  1942. globalTooltipPos.y = m_globalPosition.yMin - 10 - globalTooltipPos.height;
  1943. if ( globalTooltipPos.x < 10 )
  1944. globalTooltipPos.x = 10;
  1945. if( globalTooltipPos.x + globalTooltipPos.width > Screen.width - 10 )
  1946. globalTooltipPos.x = Screen.width - globalTooltipPos.width - 10;
  1947. //UNCOMMENT this for auto adjust tooltip to the top window box
  1948. //if( globalTooltipPos.y < 40 )
  1949. // globalTooltipPos.y = 40;
  1950. if( errorTooltip && globalTooltipPos.y + globalTooltipPos.height > Screen.height - 32 )
  1951. globalTooltipPos.y = Screen.height - 32 - globalTooltipPos.height;
  1952. GUI.Label( globalTooltipPos, temp, UIUtils.TooltipBox );
  1953. if( !errorTooltip && m_hasTooltipLink )
  1954. {
  1955. Rect link = globalTooltipPos;
  1956. link.y = globalTooltipPos.yMax - 16;
  1957. link.height = 16;
  1958. link.width = 86;
  1959. link.x = globalTooltipPos.center.x - 43;
  1960. Rect hover = globalTooltipPos;
  1961. hover.yMax += 15;// m_globalPosition.yMax;
  1962. m_linkVisibility = hover.Contains( drawInfo.MousePosition );
  1963. if( link.Contains( drawInfo.MousePosition ) )
  1964. {
  1965. if( drawInfo.CurrentEventType == EventType.MouseDown )
  1966. {
  1967. if( m_tooltipTimestamp + 1.25f < Time.realtimeSinceStartup )
  1968. {
  1969. Application.OpenURL( Attributes.NodeUrl );
  1970. }
  1971. }
  1972. else
  1973. {
  1974. UIUtils.MainSkin.customStyles[ 52 ].Draw( link, WikiLinkStr, true, false, false, false );
  1975. }
  1976. }
  1977. else
  1978. {
  1979. GUI.Label( link, WikiLinkStr, UIUtils.MainSkin.customStyles[ 52 ] );
  1980. }
  1981. }
  1982. ContainerGraph.ParentWindow.RequestRepaint();
  1983. return true;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. if( !m_linkVisibility )
  1989. m_tooltipTimestamp = Time.realtimeSinceStartup;
  1990. }
  1991. return false;
  1992. }
  1993. public virtual bool SafeDrawProperties()
  1994. {
  1995. EditorGUI.BeginChangeCheck();
  1996. PreDrawProperties();
  1997. if( m_autoWrapProperties )
  1998. {
  1999. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, DrawProperties );
  2000. }
  2001. else
  2002. {
  2003. DrawProperties();
  2004. }
  2005. if( EditorGUI.EndChangeCheck() )
  2006. {
  2007. PreviewIsDirty = true;
  2008. //UIUtils.RecordObject(this);
  2009. //MarkForPreviewUpdate();
  2010. return true;
  2011. }
  2012. return false;
  2013. }
  2014. public void PreDrawProperties()
  2015. {
  2016. if( m_useInternalPortData && m_autoDrawInternalPortData )
  2017. {
  2018. DrawInternalDataGroup();
  2019. }
  2020. }
  2021. virtual public void DrawProperties() { }
  2022. protected void DrawInternalDataGroup()
  2023. {
  2024. bool drawInternalDataUI = false;
  2025. int inputCount = m_inputPorts.Count;
  2026. if( inputCount > 0 )
  2027. {
  2028. for( int i = 0; i < inputCount; i++ )
  2029. {
  2030. if( m_inputPorts[ i ].Available && m_inputPorts[ i ].ValidInternalData && !m_inputPorts[ i ].IsConnected /*&& ( m_inputPorts[ i ].AutoDrawInternalData || ( m_autoDrawInternalPortData && m_useInternalPortData ) )*/ /*&& m_inputPorts[ i ].AutoDrawInternalData*/ )
  2031. {
  2032. drawInternalDataUI = true;
  2033. break;
  2034. }
  2035. }
  2036. }
  2037. if( drawInternalDataUI )
  2038. NodeUtils.DrawPropertyGroup( ref m_internalDataFoldout, Constants.InternalDataLabelStr, () =>
  2039. {
  2040. for( int i = 0; i < m_inputPorts.Count; i++ )
  2041. {
  2042. if( m_inputPorts[ i ].ValidInternalData && !m_inputPorts[ i ].IsConnected && m_inputPorts[ i ].Visible /*&& m_inputPorts[ i ].AutoDrawInternalData*/ )
  2043. {
  2044. m_inputPorts[ i ].ShowInternalData( this );
  2045. }
  2046. }
  2047. } );
  2048. }
  2049. protected void PickInput( InputPort port )
  2050. {
  2051. WireReference connection = port.GetConnection( 0 );
  2052. OutputPort from = port.GetOutputConnection( 0 );
  2053. m_containerGraph.ParentWindow.WireReferenceUtils.OutputPortReference.SetReference( from.NodeId, from.PortId, from.DataType, connection.TypeLocked );
  2054. m_containerGraph.DeleteConnection( true, UniqueId, port.PortId, true, true );
  2055. //TODO: check if not necessary
  2056. Event.current.Use();
  2057. IsDirty = true;
  2058. SetSaveIsDirty();
  2059. }
  2060. protected bool ApplyModifierToPort( WirePort port, bool isInput )
  2061. {
  2062. bool modifierApplied = false;
  2063. switch( Event.current.modifiers )
  2064. {
  2065. case EventModifiers.Alt:
  2066. {
  2067. m_containerGraph.DeleteConnection( isInput, UniqueId, port.PortId, true, true );
  2068. modifierApplied = true;
  2069. m_containerGraph.ParentWindow.InvalidateAlt();
  2070. }
  2071. break;
  2072. case EventModifiers.Control:
  2073. {
  2074. //WireReference connection = port.GetConnection( 0 );
  2075. //if ( isInput )
  2076. //{
  2077. // UIUtils.OutputPortReference.SetReference( connection.NodeId, connection.PortId, connection.DataType, connection.TypeLocked );
  2078. //}
  2079. //else
  2080. //{
  2081. // UIUtils.InputPortReference.SetReference( connection.NodeId, connection.PortId, connection.DataType, connection.TypeLocked );
  2082. //}
  2083. //UIUtils.DeleteConnection( isInput, UniqueId, port.PortId, true );
  2084. //modifierApplied = true;
  2085. if( !isInput )
  2086. {
  2087. WireReference connection = port.GetConnection( 0 );
  2088. m_containerGraph.ParentWindow.WireReferenceUtils.InputPortReference.SetReference( connection.NodeId, connection.PortId, connection.DataType, connection.TypeLocked );
  2089. m_containerGraph.DeleteConnection( isInput, UniqueId, port.PortId, true, true );
  2090. modifierApplied = true;
  2091. }
  2092. }
  2093. break;
  2094. }
  2095. if( isInput )
  2096. m_containerGraph.ParentWindow.WireReferenceUtils.SwitchPortReference.SetReference( port.NodeId, port.PortId, port.DataType, false ); //always save last connection
  2097. else
  2098. m_containerGraph.ParentWindow.WireReferenceUtils.SwitchPortReference.SetReference( -1, -1, WirePortDataType.OBJECT, false ); //invalidate connection
  2099. if( modifierApplied )
  2100. {
  2101. Event.current.Use();
  2102. IsDirty = true;
  2103. SetSaveIsDirty();
  2104. }
  2105. return modifierApplied;
  2106. }
  2107. public void DeleteAllInputConnections( bool alsoDeletePorts , bool inhibitWireNodeAutoDel = false )
  2108. {
  2109. int count = m_inputPorts.Count;
  2110. for( int i = 0; i < count; i++ )
  2111. {
  2112. if( m_inputPorts[ i ].IsConnected )
  2113. {
  2114. ParentNode connNode = null;
  2115. if( inhibitWireNodeAutoDel )
  2116. {
  2117. connNode = m_inputPorts[ i ].GetOutputNode();
  2118. connNode.Alive = false;
  2119. }
  2120. m_containerGraph.DeleteConnection( true, UniqueId, m_inputPorts[ i ].PortId, false, true );
  2121. if( inhibitWireNodeAutoDel )
  2122. {
  2123. connNode.Alive = true;
  2124. }
  2125. }
  2126. }
  2127. if( alsoDeletePorts )
  2128. {
  2129. m_inputPorts.Clear();
  2130. m_inputPortsDict.Clear();
  2131. }
  2132. SetSaveIsDirty();
  2133. }
  2134. public void DeleteAllOutputConnections( bool alsoDeletePorts )
  2135. {
  2136. int count = m_outputPorts.Count;
  2137. for( int i = 0; i < count; i++ )
  2138. {
  2139. if( m_outputPorts[ i ].IsConnected )
  2140. m_containerGraph.DeleteConnection( false, UniqueId, m_outputPorts[ i ].PortId, false, true );
  2141. }
  2142. if( alsoDeletePorts )
  2143. {
  2144. m_outputPorts.Clear();
  2145. m_outputPortsDict.Clear();
  2146. }
  2147. SetSaveIsDirty();
  2148. }
  2149. public void DeleteInputPortByArrayIdx( int arrayIdx )
  2150. {
  2151. if( arrayIdx >= m_inputPorts.Count )
  2152. return;
  2153. m_containerGraph.DeleteConnection( true, UniqueId, m_inputPorts[ arrayIdx ].PortId, false, true );
  2154. m_inputPortsDict.Remove( m_inputPorts[ arrayIdx ].PortId );
  2155. m_inputPorts.RemoveAt( arrayIdx );
  2156. m_sizeIsDirty = true;
  2157. SetSaveIsDirty();
  2158. RecalculateInputPortIdx();
  2159. }
  2160. public void DeleteOutputPortByArrayIdx( int portIdx )
  2161. {
  2162. if( portIdx >= m_outputPorts.Count )
  2163. return;
  2164. m_containerGraph.DeleteConnection( false, UniqueId, m_outputPorts[ portIdx ].PortId, false, true );
  2165. m_outputPortsDict.Remove( m_outputPorts[ portIdx ].PortId );
  2166. m_outputPorts.RemoveAt( portIdx );
  2167. m_sizeIsDirty = true;
  2168. }
  2169. public InputPort GetInputPortByArrayId( int id )
  2170. {
  2171. if( id < m_inputPorts.Count )
  2172. return m_inputPorts[ id ];
  2173. return null;
  2174. }
  2175. public OutputPort GetOutputPortByArrayId( int id )
  2176. {
  2177. if( id < m_outputPorts.Count )
  2178. return m_outputPorts[ id ];
  2179. return null;
  2180. }
  2181. public InputPort GetInputPortByUniqueId( int id )
  2182. {
  2183. if( m_inputPortsDict.ContainsKey( id ) )
  2184. return m_inputPortsDict[ id ];
  2185. if( m_inputPortsDict.Count != m_inputPorts.Count )
  2186. m_repopulateDictionaries = true;
  2187. int inputCount = m_inputPorts.Count;
  2188. for( int i = 0; i < inputCount; i++ )
  2189. {
  2190. if( m_inputPorts[ i ].PortId == id )
  2191. {
  2192. return m_inputPorts[ i ];
  2193. }
  2194. }
  2195. return null;
  2196. }
  2197. public InputPort GetInputPortByName( string name )
  2198. {
  2199. return InputPorts.Find( x => x.Name.Equals( name ) );
  2200. }
  2201. public InputPort GetInputPortByExternalLinkId( string internalDataName )
  2202. {
  2203. return InputPorts.Find( x => x.ExternalLinkId.Equals( internalDataName ) );
  2204. }
  2205. public OutputPort GetOutputPortByUniqueId( int id )
  2206. {
  2207. if( m_outputPortsDict.ContainsKey( id ) )
  2208. return m_outputPortsDict[ id ];
  2209. if( m_outputPortsDict.Count != m_outputPorts.Count )
  2210. m_repopulateDictionaries = true;
  2211. int outputCount = m_outputPorts.Count;
  2212. for( int i = 0; i < outputCount; i++ )
  2213. {
  2214. if( m_outputPorts[ i ].PortId == id )
  2215. return m_outputPorts[ i ];
  2216. }
  2217. return null;
  2218. }
  2219. public virtual void AfterDuplication(){}
  2220. public override string ToString()
  2221. {
  2222. string dump = "";
  2223. dump += ( "Type: " + GetType() );
  2224. dump += ( " Unique Id: " + UniqueId + "\n" );
  2225. dump += ( " Inputs: \n" );
  2226. int inputCount = m_inputPorts.Count;
  2227. int outputCount = m_outputPorts.Count;
  2228. for( int inputIdx = 0; inputIdx < inputCount; inputIdx++ )
  2229. {
  2230. dump += ( m_inputPorts[ inputIdx ] + "\n" );
  2231. }
  2232. dump += ( "Outputs: \n" );
  2233. for( int outputIdx = 0; outputIdx < outputCount; outputIdx++ )
  2234. {
  2235. dump += ( m_outputPorts[ outputIdx ] + "\n" );
  2236. }
  2237. return dump;
  2238. }
  2239. public string GetValueFromOutputStr( int outputId, WirePortDataType inputPortType, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  2240. {
  2241. if( ignoreLocalvar )
  2242. {
  2243. return GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  2244. }
  2245. OutputPort outPort = GetOutputPortByUniqueId( outputId );
  2246. if( outPort.IsLocalValue( dataCollector.PortCategory ) )
  2247. {
  2248. if( outPort.DataType != WirePortDataType.OBJECT && outPort.DataType != inputPortType )
  2249. {
  2250. return UIUtils.CastPortType( ref dataCollector, CurrentPrecisionType, null, outPort.DataType, inputPortType, outPort.LocalValue( dataCollector.PortCategory ) );
  2251. }
  2252. else
  2253. {
  2254. return outPort.LocalValue( dataCollector.PortCategory );
  2255. }
  2256. }
  2257. string result = GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  2258. result = CreateOutputLocalVariable( outputId, result, ref dataCollector );
  2259. if( outPort.DataType != WirePortDataType.OBJECT && outPort.DataType != inputPortType )
  2260. {
  2261. result = UIUtils.CastPortType( ref dataCollector, CurrentPrecisionType, null, outPort.DataType, inputPortType, result );
  2262. }
  2263. return result;
  2264. }
  2265. public virtual string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  2266. {
  2267. if( dataCollector.IsSRP )
  2268. {
  2269. switch( dataCollector.CurrentSRPType )
  2270. {
  2271. case TemplateSRPType.HDRP: if(OnHDAction!=null) OnHDAction( outputId, ref dataCollector ); break;
  2272. case TemplateSRPType.URP:if(OnLightweightAction != null) OnLightweightAction( outputId, ref dataCollector ); break;
  2273. }
  2274. }
  2275. return string.Empty;
  2276. }
  2277. public string GenerateValueInVertex( ref MasterNodeDataCollector dataCollector, WirePortDataType dataType, string dataValue, string dataName, bool createInterpolator )
  2278. {
  2279. if( !dataCollector.IsFragmentCategory )
  2280. return dataValue;
  2281. //TEMPLATES
  2282. if( dataCollector.IsTemplate )
  2283. {
  2284. if( createInterpolator && dataCollector.TemplateDataCollectorInstance.HasCustomInterpolatedData( dataName ) )
  2285. return dataName;
  2286. MasterNodePortCategory category = dataCollector.PortCategory;
  2287. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  2288. dataCollector.PortCategory = category;
  2289. if( createInterpolator )
  2290. {
  2291. dataCollector.TemplateDataCollectorInstance.RegisterCustomInterpolatedData( dataName, dataType, CurrentPrecisionType, dataValue );
  2292. }
  2293. else
  2294. {
  2295. dataCollector.AddToVertexLocalVariables( -1, CurrentPrecisionType, dataType, dataName, dataValue );
  2296. }
  2297. return dataName;
  2298. }
  2299. //SURFACE
  2300. {
  2301. if( dataCollector.TesselationActive )
  2302. {
  2303. UIUtils.ShowMessage( UniqueId, "Unable to use Vertex to Frag when Tessellation is active" );
  2304. return m_outputPorts[ 0 ].ErrorValue;
  2305. }
  2306. if( createInterpolator )
  2307. dataCollector.AddToInput( UniqueId, dataName, dataType, CurrentPrecisionType );
  2308. MasterNodePortCategory portCategory = dataCollector.PortCategory;
  2309. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  2310. if( createInterpolator )
  2311. {
  2312. dataCollector.AddLocalVariable( UniqueId, Constants.VertexShaderOutputStr + "." + dataName, dataValue + ";" );
  2313. }
  2314. else
  2315. {
  2316. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, dataType, dataName, dataValue );
  2317. }
  2318. dataCollector.PortCategory = portCategory;
  2319. return createInterpolator ? Constants.InputVarStr + "." + dataName : dataName;
  2320. }
  2321. }
  2322. public string GenerateInputInVertex( ref MasterNodeDataCollector dataCollector , int inputPortUniqueId , string varName , bool createInterpolator , bool noInterpolationFlag = false , bool sampleFlag = false )
  2323. {
  2324. InputPort inputPort = GetInputPortByUniqueId( inputPortUniqueId );
  2325. if( !dataCollector.IsFragmentCategory )
  2326. {
  2327. string value = inputPort.GeneratePortInstructions( ref dataCollector );
  2328. dataCollector.AddLocalVariable( -1 , CurrentPrecisionType , inputPort.DataType , varName , value );
  2329. return varName;
  2330. }
  2331. //TEMPLATES
  2332. if( dataCollector.IsTemplate )
  2333. {
  2334. if( createInterpolator && dataCollector.TemplateDataCollectorInstance.HasCustomInterpolatedData( varName ) )
  2335. return varName;
  2336. MasterNodePortCategory category = dataCollector.PortCategory;
  2337. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  2338. //bool dirtyVertexVarsBefore = dataCollector.DirtyVertexVariables;
  2339. //ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );
  2340. string data = inputPort.GeneratePortInstructions( ref dataCollector );
  2341. dataCollector.PortCategory = category;
  2342. //if( !dirtyVertexVarsBefore && dataCollector.DirtyVertexVariables )
  2343. //{
  2344. // dataCollector.AddVertexInstruction( dataCollector.VertexLocalVariablesFromList, UniqueId, false );
  2345. // dataCollector.ClearVertexLocalVariables();
  2346. // ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );
  2347. //}
  2348. //ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Fragment );
  2349. if( createInterpolator )
  2350. {
  2351. dataCollector.TemplateDataCollectorInstance.RegisterCustomInterpolatedData( varName, inputPort.DataType, CurrentPrecisionType, data,true,MasterNodePortCategory.Fragment,noInterpolationFlag,sampleFlag );
  2352. }
  2353. else
  2354. {
  2355. dataCollector.AddToVertexLocalVariables( -1, CurrentPrecisionType, inputPort.DataType, varName, data );
  2356. }
  2357. return varName;
  2358. }
  2359. //SURFACE
  2360. {
  2361. if( dataCollector.TesselationActive )
  2362. {
  2363. UIUtils.ShowMessage( UniqueId, "Unable to use Vertex to Frag when Tessellation is active" );
  2364. return m_outputPorts[ 0 ].ErrorValue;
  2365. }
  2366. if( createInterpolator )
  2367. dataCollector.AddToInput( UniqueId, varName, inputPort.DataType, CurrentPrecisionType );
  2368. MasterNodePortCategory portCategory = dataCollector.PortCategory;
  2369. dataCollector.PortCategory = MasterNodePortCategory.Vertex;
  2370. //bool dirtyVertexVarsBefore = dataCollector.DirtyVertexVariables;
  2371. //ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );
  2372. string vertexVarValue = inputPort.GeneratePortInstructions( ref dataCollector );
  2373. if( createInterpolator )
  2374. {
  2375. dataCollector.AddLocalVariable( UniqueId, Constants.VertexShaderOutputStr + "." + varName, vertexVarValue + ";" );
  2376. }
  2377. else
  2378. {
  2379. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, inputPort.DataType, varName, vertexVarValue );
  2380. }
  2381. dataCollector.PortCategory = portCategory;
  2382. //if( !dirtyVertexVarsBefore && dataCollector.DirtyVertexVariables )
  2383. //{
  2384. // dataCollector.AddVertexInstruction( dataCollector.VertexLocalVariables, UniqueId, false );
  2385. // dataCollector.ClearVertexLocalVariables();
  2386. // ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );
  2387. //}
  2388. //ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Fragment );
  2389. return createInterpolator ? Constants.InputVarStr + "." + varName : varName;
  2390. }
  2391. }
  2392. protected virtual void OnUniqueIDAssigned() { }
  2393. public string CreateOutputLocalVariable( int outputArrayId, string value, ref MasterNodeDataCollector dataCollector )
  2394. {
  2395. OutputPort port = GetOutputPortByUniqueId( outputArrayId );
  2396. if( port.IsLocalValue( dataCollector.PortCategory ) )
  2397. return port.LocalValue( dataCollector.PortCategory );
  2398. if( port.ConnectionCount > 1 )
  2399. {
  2400. RegisterLocalVariable( outputArrayId, value, ref dataCollector );
  2401. return port.LocalValue( dataCollector.PortCategory );
  2402. }
  2403. else
  2404. {
  2405. // revisit later (break to components case)
  2406. port.SetLocalValue( value, dataCollector.PortCategory );
  2407. }
  2408. return value;
  2409. }
  2410. public void RegisterLocalVariable( int outputArrayId, string value, ref MasterNodeDataCollector dataCollector, string customName = null )
  2411. {
  2412. OutputPort port = GetOutputPortByUniqueId( outputArrayId );
  2413. if( (int)port.DataType >= (int)( 1 << 10 ) || port.DataType == WirePortDataType.OBJECT ) //10 is the flag start of sampler types
  2414. {
  2415. port.SetLocalValue( value, dataCollector.PortCategory );
  2416. return;
  2417. }
  2418. bool vertexMode = dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation;
  2419. string localVar = port.ConfigOutputLocalValue( CurrentPrecisionType, value, customName, dataCollector.PortCategory );
  2420. if( vertexMode )
  2421. {
  2422. dataCollector.AddToVertexLocalVariables( m_uniqueId, localVar );
  2423. }
  2424. else
  2425. {
  2426. dataCollector.AddToFragmentLocalVariables( m_uniqueId, localVar );
  2427. }
  2428. }
  2429. public void InvalidateConnections()
  2430. {
  2431. int inputCount = m_inputPorts.Count;
  2432. int outputCount = m_outputPorts.Count;
  2433. for( int i = 0; i < inputCount; i++ )
  2434. {
  2435. m_inputPorts[ i ].InvalidateAllConnections();
  2436. }
  2437. for( int i = 0; i < outputCount; i++ )
  2438. {
  2439. m_outputPorts[ i ].InvalidateAllConnections();
  2440. }
  2441. }
  2442. public virtual bool OnClick( Vector2 currentMousePos2D )
  2443. {
  2444. bool singleClick = true;
  2445. if( ( EditorApplication.timeSinceStartup - m_lastTimeSelected ) < NodeClickTime )
  2446. {
  2447. OnNodeDoubleClicked( currentMousePos2D );
  2448. singleClick = false;
  2449. }
  2450. m_lastTimeSelected = EditorApplication.timeSinceStartup;
  2451. return singleClick;
  2452. }
  2453. public virtual void OnNodeDoubleClicked( Vector2 currentMousePos2D )
  2454. {
  2455. ContainerGraph.ParentWindow.ParametersWindow.IsMaximized = !ContainerGraph.ParentWindow.ParametersWindow.IsMaximized;
  2456. }
  2457. public virtual void OnNodeSelected( bool value )
  2458. {
  2459. if( !value )
  2460. {
  2461. if( m_inputPorts != null )
  2462. {
  2463. int count = m_inputPorts.Count;
  2464. for( int i = 0; i < count; i++ )
  2465. {
  2466. m_inputPorts[ i ].ResetEditing();
  2467. }
  2468. }
  2469. if( m_outputPorts != null )
  2470. {
  2471. int count = m_outputPorts.Count;
  2472. for( int i = 0; i < count; i++ )
  2473. {
  2474. m_outputPorts[ i ].ResetEditing();
  2475. }
  2476. }
  2477. }
  2478. }
  2479. public void ResetOutputLocals()
  2480. {
  2481. int outputCount = m_outputPorts.Count;
  2482. for( int i = 0; i < outputCount; i++ )
  2483. {
  2484. m_outputPorts[ i ].ResetLocalValue();
  2485. }
  2486. }
  2487. public void ResetOutputLocalsIfNot( MasterNodePortCategory category )
  2488. {
  2489. int outputCount = m_outputPorts.Count;
  2490. for( int i = 0; i < outputCount; i++ )
  2491. {
  2492. //if( !m_outputPorts[ i ].IsLocalOnCategory( category ) )
  2493. // m_outputPorts[ i ].ResetLocalValue();
  2494. m_outputPorts[ i ].ResetLocalValueIfNot( category );
  2495. }
  2496. }
  2497. public virtual void Rewire() { }
  2498. //public virtual List<int> NodeReferences { get { return null; } }
  2499. public int UniqueId
  2500. {
  2501. get { return m_uniqueId; }
  2502. set
  2503. {
  2504. m_uniqueId = value;
  2505. int inputCount = m_inputPorts.Count;
  2506. int outputCount = m_outputPorts.Count;
  2507. for( int inputIdx = 0; inputIdx < inputCount; inputIdx++ )
  2508. {
  2509. m_inputPorts[ inputIdx ].NodeId = value;
  2510. }
  2511. for( int outputIdx = 0; outputIdx < outputCount; outputIdx++ )
  2512. {
  2513. m_outputPorts[ outputIdx ].NodeId = value;
  2514. }
  2515. OnUniqueIDAssigned();
  2516. }
  2517. }
  2518. public void SetBaseUniqueId( int uniqueId, bool setOnPorts = false )
  2519. {
  2520. m_uniqueId = uniqueId;
  2521. if( setOnPorts )
  2522. {
  2523. int inputCount = m_inputPorts.Count;
  2524. int outputCount = m_outputPorts.Count;
  2525. for( int inputIdx = 0; inputIdx < inputCount; inputIdx++ )
  2526. {
  2527. m_inputPorts[ inputIdx ].NodeId = uniqueId;
  2528. }
  2529. for( int outputIdx = 0; outputIdx < outputCount; outputIdx++ )
  2530. {
  2531. m_outputPorts[ outputIdx ].NodeId = uniqueId;
  2532. }
  2533. }
  2534. }
  2535. public string OutputId
  2536. {
  2537. get
  2538. {
  2539. if( ContainerGraph.GraphId > 0 )
  2540. return UniqueId + "_g" + ContainerGraph.GraphId;
  2541. else
  2542. return UniqueId.ToString();
  2543. }
  2544. }
  2545. public virtual Rect Position { get { return m_position; } }
  2546. public Rect TruePosition { get { return m_position; } }
  2547. public Vector2 CenterPosition { get { return new Vector2( m_position.x + m_position.width * 0.5f, m_position.y + m_position.height * 0.5f ); ; } }
  2548. public Rect GlobalPosition { get { return m_globalPosition; } }
  2549. public Vector2 Corner { get { return new Vector2( m_position.x + m_position.width, m_position.y + m_position.height ); } }
  2550. public Vector2 Vec2Position
  2551. {
  2552. get { return new Vector2( m_position.x, m_position.y ); }
  2553. set
  2554. {
  2555. m_position.x = value.x;
  2556. m_position.y = value.y;
  2557. }
  2558. }
  2559. public Vector3 Vec3Position
  2560. {
  2561. get { return new Vector3( m_position.x, m_position.y, 0f ); }
  2562. set
  2563. {
  2564. m_position.x = value.x;
  2565. m_position.y = value.y;
  2566. }
  2567. }
  2568. public bool Selected
  2569. {
  2570. get { return m_selected; }
  2571. set
  2572. {
  2573. m_infiniteLoopDetected = false;
  2574. m_selected = value;
  2575. OnNodeSelected( value );
  2576. }
  2577. }
  2578. public List<InputPort> InputPorts { get { return m_inputPorts; } }
  2579. public List<OutputPort> OutputPorts
  2580. {
  2581. get { return m_outputPorts; }
  2582. }
  2583. public bool IsConnected { get { return m_connStatus == NodeConnectionStatus.Connected; } }
  2584. public NodeConnectionStatus ConnStatus
  2585. {
  2586. get { return m_connStatus; }
  2587. set
  2588. {
  2589. if( m_selfPowered )
  2590. {
  2591. m_connStatus = NodeConnectionStatus.Connected;
  2592. }
  2593. else
  2594. {
  2595. m_connStatus = value;
  2596. }
  2597. switch( m_connStatus )
  2598. {
  2599. case NodeConnectionStatus.Island:
  2600. case NodeConnectionStatus.Not_Connected: m_statusColor = Constants.NodeDefaultColor; break;
  2601. case NodeConnectionStatus.Connected: m_statusColor = Constants.NodeConnectedColor; break;
  2602. case NodeConnectionStatus.Error: m_statusColor = Constants.NodeErrorColor; break;
  2603. }
  2604. }
  2605. }
  2606. public bool SelfPowered
  2607. {
  2608. set
  2609. {
  2610. m_selfPowered = value;
  2611. if( value )
  2612. {
  2613. ConnStatus = NodeConnectionStatus.Connected;
  2614. }
  2615. }
  2616. }
  2617. // This is also called when recording on Undo
  2618. public virtual void OnBeforeSerialize() { }
  2619. public virtual void OnAfterDeserialize()
  2620. {
  2621. m_selected = false;
  2622. m_isOnGrid = false;
  2623. for( int i = 0; i < m_inputPorts.Count; i++ )
  2624. {
  2625. m_inputPorts[ i ].ResetWireReferenceStatus();
  2626. }
  2627. m_repopulateDictionaries = true;
  2628. m_sizeIsDirty = true;
  2629. m_previewIsDirty = true;
  2630. }
  2631. public virtual int InputIdFromDeprecated( int oldInputId ) { return oldInputId; }
  2632. public virtual int OutputIdFromDeprecated( int oldOutputId ) { return oldOutputId; }
  2633. public virtual void ReadFromDeprecated( ref string[] nodeParams, Type oldType = null ) { }
  2634. //Inherited classes must call this base method in order to setup id and position
  2635. public virtual void ReadFromString( ref string[] nodeParams )
  2636. {
  2637. ParentReadFromString( ref nodeParams );
  2638. }
  2639. public void ParentReadFromString( ref string[] nodeParams )
  2640. {
  2641. m_currentReadParamIdx = IOUtils.NodeTypeId + 1;
  2642. UniqueId = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] );
  2643. string[] posCoordinates = nodeParams[ m_currentReadParamIdx++ ].Split( IOUtils.VECTOR_SEPARATOR );
  2644. m_position.x = Convert.ToSingle( posCoordinates[ 0 ] );
  2645. m_position.y = Convert.ToSingle( posCoordinates[ 1 ] );
  2646. if( UIUtils.CurrentShaderVersion() > 22 )
  2647. {
  2648. string val = GetCurrentParam( ref nodeParams );
  2649. if( m_customPrecision )
  2650. {
  2651. if( val.Equals("Fixed") )
  2652. m_currentPrecisionType = PrecisionType.Half;
  2653. else
  2654. m_currentPrecisionType = (PrecisionType)Enum.Parse( typeof( PrecisionType ), val );
  2655. }
  2656. else
  2657. {
  2658. m_currentPrecisionType = PrecisionType.Inherit;
  2659. }
  2660. }
  2661. if( UIUtils.CurrentShaderVersion() > 5004 )
  2662. m_showPreview = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  2663. }
  2664. //should be called after ReadFromString
  2665. public virtual void ReadInputDataFromString( ref string[] nodeParams )
  2666. {
  2667. int count = 0;
  2668. if( UIUtils.CurrentShaderVersion() > 7003 )
  2669. {
  2670. try
  2671. {
  2672. count = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] );
  2673. }
  2674. catch( Exception e )
  2675. {
  2676. Debug.LogException( e );
  2677. }
  2678. }
  2679. else
  2680. {
  2681. count = ( m_oldInputCount < 0 ) ? m_inputPorts.Count : m_oldInputCount;
  2682. }
  2683. for( int i = 0; i < count && i < nodeParams.Length && m_currentReadParamIdx < nodeParams.Length; i++ )
  2684. {
  2685. if( UIUtils.CurrentShaderVersion() < 5003 )
  2686. {
  2687. int newId = VersionConvertInputPortId( i );
  2688. if( UIUtils.CurrentShaderVersion() > 23 )
  2689. {
  2690. m_inputPorts[ newId ].DataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  2691. }
  2692. m_inputPorts[ newId ].InternalData = nodeParams[ m_currentReadParamIdx++ ];
  2693. if( m_inputPorts[ newId ].IsEditable && UIUtils.CurrentShaderVersion() >= 3100 && m_currentReadParamIdx < nodeParams.Length )
  2694. {
  2695. m_inputPorts[ newId ].Name = nodeParams[ m_currentReadParamIdx++ ];
  2696. }
  2697. m_inputPorts[ newId ].UpdatePreviewInternalData();
  2698. }
  2699. else
  2700. {
  2701. string portIdStr = nodeParams[ m_currentReadParamIdx++ ];
  2702. int portId = -1;
  2703. try
  2704. {
  2705. portId = Convert.ToInt32( portIdStr );
  2706. }
  2707. catch( Exception e )
  2708. {
  2709. Debug.LogException( e );
  2710. }
  2711. WirePortDataType DataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  2712. string InternalData = nodeParams[ m_currentReadParamIdx++ ];
  2713. bool isEditable = Convert.ToBoolean( nodeParams[ m_currentReadParamIdx++ ] );
  2714. string Name = string.Empty;
  2715. if( isEditable && m_currentReadParamIdx < nodeParams.Length )
  2716. {
  2717. Name = nodeParams[ m_currentReadParamIdx++ ];
  2718. }
  2719. InputPort inputPort = GetInputPortByUniqueId( portId );
  2720. if( inputPort != null )
  2721. {
  2722. if( UIUtils.IsValidType( DataType ) )
  2723. inputPort.DataType = DataType;
  2724. inputPort.InternalData = InternalData;
  2725. if( !string.IsNullOrEmpty( Name ) )
  2726. {
  2727. inputPort.Name = Name;
  2728. }
  2729. inputPort.UpdatePreviewInternalData();
  2730. }
  2731. }
  2732. }
  2733. }
  2734. public virtual void ReadOutputDataFromString( ref string[] nodeParams )
  2735. {
  2736. int count = 0;
  2737. if( UIUtils.CurrentShaderVersion() > 7003 )
  2738. {
  2739. count = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] );
  2740. }
  2741. else
  2742. {
  2743. count = m_outputPorts.Count;
  2744. }
  2745. for( int i = 0; i < count && i < nodeParams.Length && m_currentReadParamIdx < nodeParams.Length; i++ )
  2746. {
  2747. try
  2748. {
  2749. WirePortDataType dataType = (WirePortDataType)Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  2750. int portId = -1;
  2751. if( UIUtils.CurrentShaderVersion() > 13903 )
  2752. {
  2753. portId = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] ); ;
  2754. }
  2755. else
  2756. {
  2757. portId = i;
  2758. }
  2759. OutputPort port = GetOutputPortByUniqueId( portId );
  2760. if( port != null && UIUtils.IsValidType( dataType ) )
  2761. {
  2762. port.DataType = dataType;
  2763. }
  2764. }
  2765. catch( Exception e )
  2766. {
  2767. Debug.LogException( e );
  2768. }
  2769. }
  2770. }
  2771. public virtual void ReadAdditionalClipboardData( ref string[] nodeParams ) { }
  2772. protected string GetCurrentParam( ref string[] nodeParams )
  2773. {
  2774. if( m_currentReadParamIdx < nodeParams.Length )
  2775. {
  2776. return nodeParams[ m_currentReadParamIdx++ ];
  2777. }
  2778. UIUtils.ShowMessage( UniqueId, "Invalid params number in node " + m_uniqueId + " of type " + GetType(), MessageSeverity.Error );
  2779. return string.Empty;
  2780. }
  2781. protected string GetCurrentParam( int index, ref string[] nodeParams )
  2782. {
  2783. if( m_currentReadParamIdx < nodeParams.Length )
  2784. {
  2785. return nodeParams[ index ];
  2786. }
  2787. UIUtils.ShowMessage( UniqueId, "Invalid params number in node " + m_uniqueId + " of type " + GetType(), MessageSeverity.Error );
  2788. return string.Empty;
  2789. }
  2790. public virtual void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  2791. {
  2792. IOUtils.AddTypeToString( ref nodeInfo, IOUtils.NodeParam );
  2793. IOUtils.AddFieldValueToString( ref nodeInfo, GetType() );
  2794. IOUtils.AddFieldValueToString( ref nodeInfo, m_uniqueId );
  2795. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_position.x.ToString() + IOUtils.VECTOR_SEPARATOR + m_position.y.ToString() ) );
  2796. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentPrecisionType );
  2797. IOUtils.AddFieldValueToString( ref nodeInfo, m_showPreview );
  2798. for( int i = 0; i < m_inputPorts.Count; i++ )
  2799. {
  2800. m_inputPorts[ i ].WriteToString( ref connectionsInfo );
  2801. }
  2802. }
  2803. public virtual void WriteInputDataToString( ref string nodeInfo )
  2804. {
  2805. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts.Count );
  2806. for( int i = 0; i < m_inputPorts.Count; i++ )
  2807. {
  2808. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts[ i ].PortId );
  2809. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts[ i ].DataType );
  2810. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts[ i ].InternalData );
  2811. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts[ i ].IsEditable );
  2812. if( m_inputPorts[ i ].IsEditable )
  2813. {
  2814. IOUtils.AddFieldValueToString( ref nodeInfo, m_inputPorts[ i ].Name );
  2815. }
  2816. }
  2817. }
  2818. public void WriteOutputDataToString( ref string nodeInfo )
  2819. {
  2820. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputPorts.Count );
  2821. for( int i = 0; i < m_outputPorts.Count; i++ )
  2822. {
  2823. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputPorts[ i ].DataType );
  2824. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputPorts[ i ].PortId );
  2825. }
  2826. }
  2827. public virtual void WriteAdditionalClipboardData( ref string nodeInfo ) { }
  2828. public virtual string GetIncludes() { return string.Empty; }
  2829. public virtual void OnObjectDropped( UnityEngine.Object obj ) { }
  2830. public virtual void SetupFromCastObject( UnityEngine.Object obj ) { }
  2831. public virtual bool OnNodeInteraction( ParentNode node ) { return false; }
  2832. public virtual void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type ) { }
  2833. public virtual void OnConnectedInputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type ) { }
  2834. public Rect CachedPos { get { return m_cachedPos; } }
  2835. public bool IsOnGrid
  2836. {
  2837. set { m_isOnGrid = value; }
  2838. get { return m_isOnGrid; }
  2839. }
  2840. public uint CurrentReadParamIdx
  2841. {
  2842. get { return m_currentReadParamIdx++; }
  2843. set { m_currentReadParamIdx = value; }
  2844. }
  2845. public Dictionary<string, InputPort> InputPortsDict
  2846. {
  2847. get
  2848. {
  2849. Dictionary<string, InputPort> dict = new Dictionary<string, InputPort>();
  2850. for( int i = 0; i < m_inputPorts.Count; i++ )
  2851. {
  2852. dict.Add( m_inputPorts[ i ].Name, m_inputPorts[ i ] );
  2853. }
  2854. return dict;
  2855. }
  2856. }
  2857. public bool IsDirty
  2858. {
  2859. set { m_isDirty = value && UIUtils.DirtyMask; }
  2860. get
  2861. {
  2862. bool value = m_isDirty;
  2863. m_isDirty = false;
  2864. return value;
  2865. }
  2866. }
  2867. public virtual void ResetNodeData()
  2868. {
  2869. m_category = 0;
  2870. m_graphDepth = 0;
  2871. }
  2872. public virtual void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  2873. {
  2874. UIUtils.SetCategoryInBitArray( ref m_category, nodeData.Category );
  2875. nodeData.GraphDepth += 1;
  2876. if( nodeData.GraphDepth > m_graphDepth )
  2877. {
  2878. m_graphDepth = nodeData.GraphDepth;
  2879. }
  2880. int count = m_inputPorts.Count;
  2881. for( int i = 0; i < count; i++ )
  2882. {
  2883. if( m_inputPorts[ i ].IsConnected )
  2884. {
  2885. m_inputPorts[ i ].GetOutputNode().PropagateNodeData( nodeData, ref dataCollector );
  2886. }
  2887. }
  2888. }
  2889. public void SetTitleTextOnCallback( string compareTitle, Action<ParentNode, string> callback )
  2890. {
  2891. if( !m_previousTitle.Equals( compareTitle ) )
  2892. {
  2893. m_previousTitle = compareTitle;
  2894. m_sizeIsDirty = true;
  2895. callback( this, compareTitle );
  2896. }
  2897. }
  2898. public void SetAdditonalTitleTextOnCallback( string compareTitle, Action<ParentNode, string> callback )
  2899. {
  2900. if( !m_previousAdditonalTitle.Equals( compareTitle ) )
  2901. {
  2902. m_previousAdditonalTitle = compareTitle;
  2903. m_sizeIsDirty = true;
  2904. callback( this, compareTitle );
  2905. }
  2906. }
  2907. public void SetAdditonalTitleTextOnCallback( string compareTitle, string compareTitleFormat, Action<ParentNode, string> callback )
  2908. {
  2909. if ( !m_previousAdditonalTitle.Equals( compareTitle ) || !m_previousAdditonalTitleFormat.Equals( compareTitleFormat ))
  2910. {
  2911. m_previousAdditonalTitle = compareTitle;
  2912. m_previousAdditonalTitleFormat = compareTitleFormat;
  2913. m_sizeIsDirty = true;
  2914. callback( this, compareTitle );
  2915. }
  2916. }
  2917. public virtual void SetClippedTitle( string newText, int maxSize = 170, string endString = "..." )
  2918. {
  2919. m_content.text = GenerateClippedTitle( newText,maxSize,endString );
  2920. m_sizeIsDirty = true;
  2921. }
  2922. public virtual void SetClippedAdditionalTitle( string newText, int maxSize = 170, string endString = "..." )
  2923. {
  2924. m_additionalContent.text = GenerateClippedTitle( newText, maxSize, endString );
  2925. m_sizeIsDirty = true;
  2926. }
  2927. public void SetTitleText( string newText )
  2928. {
  2929. if( !newText.Equals( m_content.text ) )
  2930. {
  2931. m_content.text = newText;
  2932. m_sizeIsDirty = true;
  2933. }
  2934. }
  2935. public void SetAdditonalTitleText( string newText )
  2936. {
  2937. if( !newText.Equals( m_additionalContent.text ) )
  2938. {
  2939. m_additionalContent.text = newText;
  2940. m_sizeIsDirty = true;
  2941. }
  2942. }
  2943. public string GenerateErrorValue( int outputIdx = 0 )
  2944. {
  2945. switch( m_outputPorts[ outputIdx ].DataType )
  2946. {
  2947. case WirePortDataType.FLOAT2:
  2948. {
  2949. return "(0).xx";
  2950. }
  2951. case WirePortDataType.FLOAT3:
  2952. {
  2953. return "(0).xxx";
  2954. }
  2955. case WirePortDataType.FLOAT4:
  2956. case WirePortDataType.COLOR:
  2957. {
  2958. return "(0).xxxx";
  2959. }
  2960. }
  2961. return "0";
  2962. }
  2963. //Methods created to take into account new ports added on nodes newer versions
  2964. //This way we can convert connections from previous versions to newer ones and not brake shader graph
  2965. public virtual int VersionConvertInputPortId( int portId ) { return portId; }
  2966. public virtual int VersionConvertOutputPortId( int portId ) { return portId; }
  2967. public virtual string DataToArray { get { return string.Empty; } }
  2968. public bool SaveIsDirty
  2969. {
  2970. set { m_saveIsDirty = value && UIUtils.DirtyMask; }
  2971. get
  2972. {
  2973. bool value = m_saveIsDirty;
  2974. m_saveIsDirty = false;
  2975. return value;
  2976. }
  2977. }
  2978. public GUIContent TitleContent { get { return m_content; } }
  2979. public GUIContent AdditonalTitleContent { get { return m_additionalContent; } }
  2980. public bool IsVisible { get { return m_isVisible; } }
  2981. public NodeAttributes Attributes { get { return m_nodeAttribs; } }
  2982. public bool ReorderLocked { get { return m_reorderLocked; } }
  2983. public bool RequireMaterialUpdate { get { return m_requireMaterialUpdate; } }
  2984. public bool RMBIgnore { get { return m_rmbIgnore; } }
  2985. public float TextLabelWidth { get { return m_textLabelWidth; } }
  2986. public bool IsMoving { get { return m_isMoving > 0; } }
  2987. public bool MovingInFrame { get { return m_movingInFrame; } set { m_movingInFrame = value; } }
  2988. public bool SizeIsDirty { get { return m_sizeIsDirty; } set { m_sizeIsDirty = value; } }
  2989. public int Category { get { return m_category; } }
  2990. public int CommentaryParent
  2991. {
  2992. get { return m_commentaryParent; }
  2993. set { m_commentaryParent = value; }
  2994. }
  2995. public int Depth
  2996. {
  2997. get { return m_depth; }
  2998. set { m_depth = value; }
  2999. }
  3000. public int MatrixId
  3001. {
  3002. get { return m_matrixId; }
  3003. set { m_matrixId = value; }
  3004. }
  3005. public float PaddingTitleRight
  3006. {
  3007. get { return m_paddingTitleRight; }
  3008. set { m_paddingTitleRight += value; }
  3009. }
  3010. public float PaddingTitleLeft
  3011. {
  3012. get { return m_paddingTitleLeft; }
  3013. set { m_paddingTitleLeft += value; }
  3014. }
  3015. public int CachedPortsId
  3016. {
  3017. get
  3018. {
  3019. return m_cachedPortsId;
  3020. }
  3021. }
  3022. public virtual bool RecursivePreviewUpdate( Dictionary<string,bool> duplicatesDict = null )
  3023. {
  3024. if( duplicatesDict == null )
  3025. {
  3026. duplicatesDict = ContainerGraph.ParentWindow.VisitedChanged;
  3027. }
  3028. for( int i = 0; i < InputPorts.Count; i++ )
  3029. {
  3030. ParentNode outNode = null;
  3031. if( InputPorts[ i ].ExternalReferences.Count > 0 )
  3032. {
  3033. outNode = ContainerGraph.GetNode( InputPorts[ i ].ExternalReferences[ 0 ].NodeId );
  3034. }
  3035. if( outNode != null )
  3036. {
  3037. if( !duplicatesDict.ContainsKey( outNode.OutputId ) )
  3038. {
  3039. bool result = outNode.RecursivePreviewUpdate();
  3040. if( result )
  3041. PreviewIsDirty = true;
  3042. } else if( duplicatesDict[ outNode.OutputId ] )
  3043. {
  3044. PreviewIsDirty = true;
  3045. }
  3046. }
  3047. }
  3048. bool needsUpdate = PreviewIsDirty;
  3049. RenderNodePreview();
  3050. if( !duplicatesDict.ContainsKey( OutputId ) )
  3051. duplicatesDict.Add( OutputId, needsUpdate );
  3052. return needsUpdate;
  3053. }
  3054. public virtual void RenderNodePreview()
  3055. {
  3056. //Runs at least one time
  3057. if( !HasPreviewShader || !m_initialized )
  3058. {
  3059. // nodes with no preview don't update at all
  3060. PreviewIsDirty = false;
  3061. return;
  3062. }
  3063. if( !PreviewIsDirty && !m_continuousPreviewRefresh )
  3064. return;
  3065. //Debug.Log( "PREVIEW " + this );
  3066. SetPreviewInputs();
  3067. if( m_cachedMainTexId == -1 )
  3068. m_cachedMainTexId = Shader.PropertyToID( "_MainTex" );
  3069. if( m_cachedMaskTexId == -1 )
  3070. m_cachedMaskTexId = Shader.PropertyToID( "_MaskTex" );
  3071. if( m_cachedPortsId == -1 )
  3072. m_cachedPortsId = Shader.PropertyToID( "_Ports" );
  3073. if( m_cachedPortId == -1 )
  3074. m_cachedPortId = Shader.PropertyToID( "_Port" );
  3075. if (!Preferences.User.DisablePreviews)
  3076. {
  3077. RenderTexture temp = RenderTexture.active;
  3078. RenderTexture beforeMask = RenderTexture.GetTemporary(Constants.PreviewSize, Constants.PreviewSize, 0, Constants.PreviewFormat, RenderTextureReadWrite.Linear);
  3079. int count = m_outputPorts.Count;
  3080. for( int i = 0; i < count; i++ )
  3081. {
  3082. if ( i == 0 )
  3083. {
  3084. RenderTexture.active = beforeMask;
  3085. if ( m_previewMaterialPassId < PreviewMaterial.passCount )
  3086. {
  3087. Graphics.Blit( null, beforeMask, PreviewMaterial, m_previewMaterialPassId );
  3088. }
  3089. m_portMask.Set( 0, 0, 0, 0 );
  3090. switch( m_outputPorts[ i ].DataType )
  3091. {
  3092. case WirePortDataType.INT:
  3093. case WirePortDataType.FLOAT:
  3094. m_portMask.Set( 1, 1, 1, 1 );
  3095. break;
  3096. case WirePortDataType.FLOAT2:
  3097. m_portMask.Set( 1, 1, 0, 0 );
  3098. break;
  3099. case WirePortDataType.FLOAT3:
  3100. m_portMask.Set( 1, 1, 1, 0 );
  3101. break;
  3102. case WirePortDataType.COLOR:
  3103. case WirePortDataType.FLOAT4:
  3104. m_portMask.Set( 1, 1, 1, 1 );
  3105. break;
  3106. default:
  3107. m_portMask.Set( 1, 1, 1, 1 );
  3108. break;
  3109. }
  3110. if( m_outputPorts[ i ].DataType == WirePortDataType.FLOAT3x3 || m_outputPorts[ i ].DataType == WirePortDataType.FLOAT4x4 )
  3111. {
  3112. m_outputPorts[ i ].MaskingMaterial.SetTexture( m_cachedMainTexId, EditorGUIUtility.whiteTexture );
  3113. }
  3114. else
  3115. {
  3116. m_outputPorts[ i ].MaskingMaterial.SetTexture( m_cachedMainTexId, beforeMask );
  3117. }
  3118. m_outputPorts[ i ].MaskingMaterial.SetVector( m_cachedPortsId, m_portMask );
  3119. RenderTexture.active = m_outputPorts[ i ].OutputPreviewTexture;
  3120. Graphics.Blit( null , m_outputPorts[ i ].OutputPreviewTexture , m_outputPorts[ i ].MaskingMaterial , 0 );
  3121. }
  3122. else
  3123. {
  3124. m_outputPorts[ i ].MaskingMaterial.SetTexture( m_cachedMaskTexId , PreviewTexture );
  3125. m_outputPorts[ i ].MaskingMaterial.SetFloat( m_cachedPortId , i );
  3126. RenderTexture.active = m_outputPorts[ i ].OutputPreviewTexture;
  3127. Graphics.Blit( null , m_outputPorts[ i ].OutputPreviewTexture , m_outputPorts[ i ].MaskingMaterial , 1 );
  3128. }
  3129. }
  3130. RenderTexture.ReleaseTemporary(beforeMask);
  3131. RenderTexture.active = temp;
  3132. }
  3133. PreviewIsDirty = m_continuousPreviewRefresh;
  3134. FinishPreviewRender = true;
  3135. }
  3136. protected void ShowTab( NodeMessageType type, string tooltip )
  3137. {
  3138. m_showErrorMessage = true;
  3139. m_errorMessageTypeIsError = type;
  3140. m_errorMessageTooltip = tooltip;
  3141. }
  3142. protected void ShowTab()
  3143. {
  3144. m_showErrorMessage = true;
  3145. }
  3146. protected void HideTab()
  3147. {
  3148. m_showErrorMessage = false;
  3149. }
  3150. public virtual RenderTexture PreviewTexture
  3151. {
  3152. get
  3153. {
  3154. if( m_outputPorts.Count > 0 )
  3155. return m_outputPorts[ 0 ].OutputPreviewTexture;
  3156. else
  3157. return null;
  3158. }
  3159. }
  3160. public void FullWriteToString( ref string nodesInfo, ref string connectionsInfo )
  3161. {
  3162. WriteToString( ref nodesInfo, ref connectionsInfo );
  3163. WriteInputDataToString( ref nodesInfo );
  3164. WriteOutputDataToString( ref nodesInfo );
  3165. }
  3166. public void ClipboardFullWriteToString( ref string nodesInfo, ref string connectionsInfo )
  3167. {
  3168. FullWriteToString( ref nodesInfo, ref connectionsInfo );
  3169. WriteAdditionalClipboardData( ref nodesInfo );
  3170. }
  3171. public void FullReadFromString( ref string[] parameters )
  3172. {
  3173. try
  3174. {
  3175. ReadFromString( ref parameters );
  3176. ReadInputDataFromString( ref parameters );
  3177. ReadOutputDataFromString( ref parameters );
  3178. }
  3179. catch( Exception e )
  3180. {
  3181. Debug.LogException( e );
  3182. }
  3183. }
  3184. public void ClipboardFullReadFromString( ref string[] parameters )
  3185. {
  3186. try
  3187. {
  3188. FullReadFromString( ref parameters );
  3189. ReadAdditionalClipboardData( ref parameters );
  3190. }
  3191. catch( Exception e )
  3192. {
  3193. Debug.LogException( e );
  3194. }
  3195. }
  3196. public string GenerateClippedTitle( string original , int maxSize = 170, string endString = "..." )
  3197. {
  3198. if( UIUtils.UnZoomedNodeTitleStyle == null )
  3199. return original;
  3200. GUIContent content = new GUIContent( original );
  3201. string finalTitle = string.Empty;
  3202. bool addEllipsis = false;
  3203. for( int i = 1; i <= original.Length; i++ )
  3204. {
  3205. content.text = original.Substring( 0, i );
  3206. Vector2 titleSize = UIUtils.UnZoomedNodeTitleStyle.CalcSize( content );
  3207. if( titleSize.x > maxSize )
  3208. {
  3209. addEllipsis = true;
  3210. break;
  3211. }
  3212. else
  3213. {
  3214. finalTitle = content.text;
  3215. }
  3216. }
  3217. if( addEllipsis )
  3218. finalTitle += endString;
  3219. return finalTitle;
  3220. }
  3221. public virtual void RefreshOnUndo() { }
  3222. public virtual void CalculateCustomGraphDepth() { }
  3223. public int GraphDepth { get { return m_graphDepth; } }
  3224. public PrecisionType CurrentPrecisionType { get { return m_currentPrecisionType == PrecisionType.Inherit ? ContainerGraph.CurrentPrecision : m_currentPrecisionType; } }
  3225. public Material PreviewMaterial
  3226. {
  3227. get
  3228. {
  3229. if( m_previewMaterial == null )
  3230. {
  3231. m_previewMaterial = new Material( PreviewShader );
  3232. }
  3233. return m_previewMaterial;
  3234. }
  3235. }
  3236. public Shader PreviewShader
  3237. {
  3238. get
  3239. {
  3240. if( m_previewShader == null )
  3241. {
  3242. m_previewShader = AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( m_previewShaderGUID ) );
  3243. }
  3244. if( m_previewShader == null )
  3245. {
  3246. m_previewShader = AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "d9ca47581ac157145bff6f72ac5dd73e" ) ); //ranged float guid
  3247. }
  3248. if( m_previewShader == null )
  3249. m_previewShader = Shader.Find( "Unlit/Colored Transparent" );
  3250. return m_previewShader;
  3251. }
  3252. }
  3253. public bool HasPreviewShader
  3254. {
  3255. get { return !string.IsNullOrEmpty( m_previewShaderGUID ); }
  3256. }
  3257. public void CheckSpherePreview()
  3258. {
  3259. bool oneIsSphere = false;
  3260. if( m_drawPreviewAsSphere )
  3261. oneIsSphere = true;
  3262. int count = m_inputPorts.Count;
  3263. for( int i = 0; i < count; i++ )
  3264. {
  3265. ParentNode node = null;
  3266. if( m_inputPorts[ i ].ExternalReferences.Count > 0)
  3267. {
  3268. node = ContainerGraph.GetNode( m_inputPorts[ i ].ExternalReferences[ 0 ].NodeId );
  3269. }
  3270. if( node != null && node.SpherePreview )
  3271. oneIsSphere = true;
  3272. }
  3273. if( m_forceDrawPreviewAsPlane )
  3274. oneIsSphere = false;
  3275. SpherePreview = oneIsSphere;
  3276. }
  3277. public bool SpherePreview
  3278. {
  3279. get { return m_spherePreview; }
  3280. set { m_spherePreview = value; }
  3281. }
  3282. public bool ShowPreview
  3283. {
  3284. get { return m_showPreview; }
  3285. set { m_showPreview = value; }
  3286. }
  3287. public int VisiblePorts
  3288. {
  3289. get { return m_visiblePorts; }
  3290. set { m_visiblePorts = value; }
  3291. }
  3292. public bool Docking
  3293. {
  3294. get { return m_docking; }
  3295. set { m_docking = value; }
  3296. }
  3297. public bool UseSquareNodeTitle
  3298. {
  3299. get { return m_useSquareNodeTitle; }
  3300. set { m_useSquareNodeTitle = value; }
  3301. }
  3302. public bool InsideShaderFunction
  3303. {
  3304. get { return ContainerGraph != ContainerGraph.ParentWindow.CurrentGraph; }
  3305. }
  3306. public virtual void SetContainerGraph( ParentGraph newgraph )
  3307. {
  3308. m_containerGraph = newgraph;
  3309. }
  3310. public virtual void OnMasterNodeReplaced( MasterNode newMasterNode ) { PreviewIsDirty = true; }
  3311. public virtual void RefreshExternalReferences() { }
  3312. public Rect DropdownRect { get { return m_dropdownRect; } }
  3313. public virtual bool Contains( Vector2 pos ) { return m_globalPosition.Contains( pos ); }
  3314. public virtual bool Contains( Vector3 pos ) { return m_globalPosition.Contains( pos ); }
  3315. public bool IsNodeBeingCopied { get { return m_isNodeBeingCopied; } set { m_isNodeBeingCopied = value; } }
  3316. public virtual WirePortDataType GetInputPortVisualDataTypeByArrayIdx( int portArrayIdx )
  3317. {
  3318. return m_inputPorts[ portArrayIdx ].DataType;
  3319. }
  3320. public virtual WirePortDataType GetOutputPortVisualDataTypeById( int portId )
  3321. {
  3322. return GetOutputPortByUniqueId( portId ).DataType;
  3323. }
  3324. public virtual bool CheckFindText( string text )
  3325. {
  3326. return TitleContent.text.IndexOf( text, StringComparison.CurrentCultureIgnoreCase ) >= 0;
  3327. }
  3328. public virtual ParentNode ExecuteStubCode(){ return this; }
  3329. public virtual float HeightEstimate
  3330. {
  3331. get
  3332. {
  3333. float heightEstimate = 0;
  3334. heightEstimate = 32 + Constants.INPUT_PORT_DELTA_Y;
  3335. for( int i = 0; i < InputPorts.Count; i++ )
  3336. {
  3337. if( InputPorts[ i ].Visible )
  3338. heightEstimate += 18 + Constants.INPUT_PORT_DELTA_Y;
  3339. }
  3340. return heightEstimate;
  3341. // Magic number 18 represents m_fontHeight that might not be set yet
  3342. //return Constants.NODE_HEADER_EXTRA_HEIGHT + Mathf.Max( 18 + m_inputPorts.Count, m_outputPorts.Count ) * Constants.INPUT_PORT_DELTA_Y;
  3343. }
  3344. }
  3345. public bool WasDeprecated { get { return m_wasDeprecated; } set { m_wasDeprecated = value; } }
  3346. public bool Alive { get { return m_alive;} set { m_alive = value; } }
  3347. public string TypeName { get { if( m_nodeAttribs != null ) return m_nodeAttribs.Name;return GetType().ToString(); } }
  3348. public bool PreviewIsDirty { set { m_previewIsDirty = value; } get { return m_previewIsDirty; } }
  3349. protected bool FinishPreviewRender { get { return m_finishPreviewRender; } set { m_finishPreviewRender = value; } }
  3350. public virtual bool IsStubNode { get { return false; } }
  3351. }
  3352. }