예제 #1
0
 /// <summary>
 /// Creates the node.
 /// </summary>
 /// <param name="groups">The group archetypes.</param>
 /// <param name="id">The node id.</param>
 /// <param name="context">The context.</param>
 /// <param name="groupID">The group identifier.</param>
 /// <param name="typeID">The type identifier.</param>
 /// <returns>Created node or null if failed.</returns>
 public static SurfaceNode CreateNode(List <GroupArchetype> groups, uint id, VisjectSurfaceContext context, ushort groupID, ushort typeID)
 {
     for (var i = 0; i < groups.Count; i++)
     {
         var groupArchetype = groups[i];
         if (groupArchetype.GroupID == groupID && groupArchetype.Archetypes != null)
         {
             foreach (var nodeArchetype in groupArchetype.Archetypes)
             {
                 if (nodeArchetype.TypeID == typeID)
                 {
                     SurfaceNode node;
                     if (nodeArchetype.Create != null)
                     {
                         node = nodeArchetype.Create(id, context, nodeArchetype, groupArchetype);
                     }
                     else
                     {
                         node = new SurfaceNode(id, context, nodeArchetype, groupArchetype);
                     }
                     return(node);
                 }
             }
         }
     }
     return(null);
 }
예제 #2
0
        /// <summary>
        /// Creates the node.
        /// </summary>
        /// <param name="groups">The group archetypes.</param>
        /// <param name="id">The node id.</param>
        /// <param name="context">The context.</param>
        /// <param name="groupID">The group identifier.</param>
        /// <param name="typeID">The type identifier.</param>
        /// <returns>Created node or null if failed.</returns>
        public static SurfaceNode CreateNode(List <GroupArchetype> groups, uint id, VisjectSurfaceContext context, ushort groupID, ushort typeID)
        {
            // Find archetype for that node
            for (var i = 0; i < groups.Count; i++)
            {
                var groupArchetype = groups[i];
                if (groupArchetype.GroupID == groupID && groupArchetype.Archetypes != null)
                {
                    for (var j = 0; j < groupArchetype.Archetypes.Length; j++)
                    {
                        var nodeArchetype = groupArchetype.Archetypes[j];
                        if (nodeArchetype.TypeID == typeID)
                        {
                            // Create
                            SurfaceNode node;
                            if (nodeArchetype.Create != null)
                            {
                                node = nodeArchetype.Create(id, context, nodeArchetype, groupArchetype);
                            }
                            else
                            {
                                node = new SurfaceNode(id, context, nodeArchetype, groupArchetype);
                            }
                            return(node);
                        }
                    }
                }
            }

            // Error
            Editor.LogError($"Failed to create Visject Surface node with id: {groupID}:{typeID}");
            return(null);
        }
예제 #3
0
        /// <summary>
        /// Called when context gets changed. Updates current context and UI. Updates the current context based on the first element in teh stack.
        /// </summary>
        protected virtual void OnContextChanged()
        {
            var context = ContextStack.Count > 0 ? ContextStack.Peek() : null;

            _context = context;
            if (ContextStack.Count == 0)
            {
                _root = null;
            }

            // Update root control linkage
            if (_rootControl != null)
            {
                _rootControl.Parent = null;
            }
            if (context != null)
            {
                _rootControl        = _context.RootControl;
                _rootControl.Parent = this;
            }
            else
            {
                _rootControl = null;
            }

            ContextChanged?.Invoke(_context);
        }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VisjectSurfaceContext"/> class.
        /// </summary>
        /// <param name="surface">The Visject surface using this context.</param>
        /// <param name="parent">The parent context. Defines the higher key surface graph context. May be null for the top-level context.</param>
        /// <param name="context">The context.</param>
        /// <param name="rootControl">The surface root control.</param>
        public VisjectSurfaceContext(VisjectSurface surface, VisjectSurfaceContext parent, ISurfaceContext context, SurfaceRootControl rootControl)
        {
            _surface    = surface;
            Parent      = parent;
            Context     = context ?? throw new ArgumentNullException(nameof(context));
            RootControl = rootControl ?? throw new ArgumentNullException(nameof(rootControl));

            // Set initial scale to provide nice zoom in effect on startup
            RootControl.Scale = new Vector2(0.5f);
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SurfaceNode"/> class.
        /// </summary>
        /// <param name="id">The node id.</param>
        /// <param name="context">The surface context.</param>
        /// <param name="nodeArch">The node archetype.</param>
        /// <param name="groupArch">The group archetype.</param>
        public SurfaceNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch)
            : base(context, nodeArch.Size.X + Constants.NodeMarginX * 2, nodeArch.Size.Y + Constants.NodeMarginY * 2 + Constants.NodeHeaderSize + Constants.NodeFooterSize)
        {
            Title          = nodeArch.Title;
            ID             = id;
            Archetype      = nodeArch;
            GroupArchetype = groupArch;

            if (Archetype.DefaultValues != null)
            {
                Values = new object[Archetype.DefaultValues.Length];
                Array.Copy(Archetype.DefaultValues, Values, Values.Length);
            }
        }
예제 #6
0
        /// <summary>
        /// Creates the node.
        /// </summary>
        /// <param name="id">The node id.</param>
        /// <param name="context">The context.</param>
        /// <param name="groupArchetype">The group archetype.</param>
        /// <param name="nodeArchetype">The node archetype.</param>
        /// <returns>Created node or null if failed.</returns>
        public static SurfaceNode CreateNode(uint id, VisjectSurfaceContext context, GroupArchetype groupArchetype, NodeArchetype nodeArchetype)
        {
            Assert.IsTrue(groupArchetype.Archetypes.Contains(nodeArchetype));

            SurfaceNode node;

            if (nodeArchetype.Create != null)
            {
                node = nodeArchetype.Create(id, context, nodeArchetype, groupArchetype);
            }
            else
            {
                node = new SurfaceNode(id, context, nodeArchetype, groupArchetype);
            }
            return(node);
        }
예제 #7
0
        /// <summary>
        /// Opens the child context of the current context.
        /// </summary>
        /// <param name="context">The context.</param>
        public void OpenContext(ISurfaceContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (_context != null && _context.Context == context)
            {
                return;
            }

            // Get or create context
            VisjectSurfaceContext surfaceContext;

            if (!_contextCache.TryGetValue(context, out surfaceContext))
            {
                surfaceContext = CreateContext(_context, context);
                _context?.Children.Add(surfaceContext);
                _contextCache.Add(context, surfaceContext);

                context.OnContextCreated(surfaceContext);

                // Load context
                if (_root != null)
                {
                    if (surfaceContext.Load())
                    {
                        throw new Exception("Failed to load graph.");
                    }
                }
            }
            if (_root == null)
            {
                _root = surfaceContext;
            }
            else if (ContextStack.Contains(surfaceContext))
            {
                throw new ArgumentException("Context has been already added to the stack.");
            }

            // Change stack
            ContextStack.Push(surfaceContext);

            // Update
            OnContextChanged();
        }
예제 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SurfaceNode"/> class.
        /// </summary>
        /// <param name="id">The node id.</param>
        /// <param name="context">The surface context.</param>
        /// <param name="nodeArch">The node archetype.</param>
        /// <param name="groupArch">The group archetype.</param>
        public SurfaceNode(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch)
            : base(context, nodeArch.Size.X + Constants.NodeMarginX * 2, nodeArch.Size.Y + Constants.NodeMarginY * 2 + Constants.NodeHeaderSize + Constants.NodeFooterSize)
        {
            Title           = nodeArch.Title;
            ID              = id;
            Archetype       = nodeArch;
            GroupArchetype  = groupArch;
            AutoFocus       = false;
            TooltipText     = nodeArch.Description;
            CullChildren    = false;
            BackgroundColor = Style.Current.BackgroundNormal;

            if (Archetype.DefaultValues != null)
            {
                Values = new object[Archetype.DefaultValues.Length];
                Array.Copy(Archetype.DefaultValues, Values, Values.Length);
            }
        }
예제 #9
0
 /// <inheritdoc />
 public MissingNode(uint id, VisjectSurfaceContext context, ushort originalGroupId, ushort originalNodeId)
     : base(id, context, new NodeArchetype
 {
     TypeID        = originalNodeId,
     Title         = "Missing Node :(",
     Description   = ":(",
     Flags         = NodeFlags.AllGraphs,
     Size          = new Vector2(200, 70),
     Elements      = new NodeElementArchetype[0],
     DefaultValues = new object[32],
 }, new GroupArchetype
 {
     GroupID    = originalGroupId,
     Name       = string.Empty,
     Color      = Color.Black,
     Archetypes = new NodeArchetype[0]
 })
 {
 }
예제 #10
0
 /// <inheritdoc />
 public void OnContextCreated(VisjectSurfaceContext context)
 {
 }
예제 #11
0
 /// <summary>
 /// Creates the Visject surface context for the given surface data source context.
 /// </summary>
 /// <param name="parent">The parent context.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 protected virtual VisjectSurfaceContext CreateContext(VisjectSurfaceContext parent, ISurfaceContext context)
 {
     return(new VisjectSurfaceContext(this, parent, context));
 }
예제 #12
0
        internal static GraphParameterData[] InitGraphParameters(IEnumerable <MaterialParameter> parameters, Material material)
        {
            int count = parameters.Count();
            var data  = new GraphParameterData[count];
            int i     = 0;

            // Load material surface parameters meta to use it for material instance parameters editing
            SurfaceParameter[] surfaceParameters = null;
            try
            {
                Profiler.BeginEvent("Init Material Parameters UI Data");

                if (material != null && !material.WaitForLoaded())
                {
                    var surfaceData = material.LoadSurface(false);
                    if (surfaceData != null && surfaceData.Length > 0)
                    {
                        using (var memoryStream = new MemoryStream(surfaceData))
                            using (var stream = new BinaryReader(memoryStream))
                            {
                                // IMPORTANT! This must match C++ Graph format

                                // Magic Code
                                int tmp = stream.ReadInt32();
                                if (tmp != 1963542358)
                                {
                                    // Error
                                    throw new Exception("Invalid Graph format version");
                                }

                                // Version
                                var version   = stream.ReadUInt32();
                                var guidBytes = new byte[16];
                                if (version < 7000)
                                {
                                    // Time saved (not used anymore to prevent binary diffs after saving unmodified surface)
                                    stream.ReadInt64();

                                    // Nodes count
                                    int nodesCount = stream.ReadInt32();

                                    // Parameters count
                                    int parametersCount = stream.ReadInt32();

                                    // For each node
                                    for (int j = 0; j < nodesCount; j++)
                                    {
                                        // ID
                                        stream.ReadUInt32();

                                        // Type
                                        stream.ReadUInt16();
                                        stream.ReadUInt16();
                                    }

                                    // For each param
                                    surfaceParameters = new SurfaceParameter[parametersCount];
                                    for (int j = 0; j < parametersCount; j++)
                                    {
                                        // Create param
                                        var param = new SurfaceParameter();
                                        surfaceParameters[j] = param;

                                        // Properties
                                        param.Type = new ScriptType(VisjectSurfaceContext.GetGraphParameterValueType((VisjectSurfaceContext.GraphParamType_Deprecated)stream.ReadByte()));
                                        stream.Read(guidBytes, 0, 16);
                                        param.ID       = new Guid(guidBytes);
                                        param.Name     = stream.ReadStr(97);
                                        param.IsPublic = stream.ReadByte() != 0;
                                        var isStatic     = stream.ReadByte() != 0;
                                        var isUIVisible  = stream.ReadByte() != 0;
                                        var isUIEditable = stream.ReadByte() != 0;

                                        // References [Deprecated]
                                        int refsCount = stream.ReadInt32();
                                        for (int k = 0; k < refsCount; k++)
                                        {
                                            stream.ReadUInt32();
                                        }

                                        // Value
                                        stream.ReadCommonValue(ref param.Value);

                                        // Meta
                                        param.Meta.Load(stream);
                                    }
                                }
                                else if (version == 7000)
                                {
                                    // Nodes count
                                    int nodesCount = stream.ReadInt32();

                                    // Parameters count
                                    int parametersCount = stream.ReadInt32();

                                    // For each node
                                    for (int j = 0; j < nodesCount; j++)
                                    {
                                        // ID
                                        stream.ReadUInt32();

                                        // Type
                                        stream.ReadUInt16();
                                        stream.ReadUInt16();
                                    }

                                    // For each param
                                    surfaceParameters = new SurfaceParameter[parametersCount];
                                    for (int j = 0; j < parametersCount; j++)
                                    {
                                        // Create param
                                        var param = new SurfaceParameter();
                                        surfaceParameters[j] = param;

                                        // Properties
                                        param.Type = stream.ReadVariantScriptType();
                                        stream.Read(guidBytes, 0, 16);
                                        param.ID       = new Guid(guidBytes);
                                        param.Name     = stream.ReadStr(97);
                                        param.IsPublic = stream.ReadByte() != 0;

                                        // Value
                                        param.Value = stream.ReadVariant();

                                        // Meta
                                        param.Meta.Load(stream);
                                    }
                                }
                            }
                    }
                }
            }
            catch (Exception ex)
            {
                Editor.LogError("Failed to get material parameters metadata.");
                Editor.LogWarning(ex);
            }
            finally
            {
                Profiler.EndEvent();
            }

            foreach (var parameter in parameters)
            {
                var surfaceParameter = surfaceParameters?.FirstOrDefault(x => x.ID == parameter.ParameterID);
                var attributes       = surfaceParameter?.Meta.GetAttributes() ?? FlaxEngine.Utils.GetEmptyArray <Attribute>();
                data[i] = new GraphParameterData(null, parameter.Name, parameter.IsPublic, ToType(parameter.ParameterType), attributes, parameter);
                i++;
            }
            Array.Sort(data, GraphParameterData.Compare);
            return(data);
        }
예제 #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VisjectSurfaceContext"/> class.
 /// </summary>
 /// <param name="surface">The Visject surface using this context.</param>
 /// <param name="parent">The parent context. Defines the higher key surface graph context. May be null for the top-level context.</param>
 /// <param name="context">The context.</param>
 public VisjectSurfaceContext(VisjectSurface surface, VisjectSurfaceContext parent, ISurfaceContext context)
     : this(surface, parent, context, new SurfaceRootControl())
 {
 }
예제 #14
0
 /// <inheritdoc />
 public DummyCustomNode(uint id, VisjectSurfaceContext context)
     : base(id, context, new NodeArchetype(), new GroupArchetype())
 {
 }