Ejemplo n.º 1
0
        /// <summary>
        /// Updates dependant/independent boxes types.
        /// </summary>
        public void UpdateBoxesTypes()
        {
            // Check there is no need to use box types dependency feature
            if (Archetype.DependentBoxes == null && Archetype.IndependentBoxes == null)
            {
                // Check if need to update update current type of the typeless boxes that use connection hints
                if ((Archetype.ConnectionsHints & ConnectionsHint.All) != 0)
                {
                    for (int j = 0; j < Elements.Count; j++)
                    {
                        if (Elements[j] is Box box && box.DefaultType == ScriptType.Null)
                        {
                            box.CurrentType = box.HasAnyConnection ? box.Connections[0].CurrentType : ScriptType.Null;
                        }
                    }
                }
                return;
            }

            // Prevent recursive loop call that might happen
            if (UpdateStack.Contains(this))
            {
                return;
            }
            UpdateStack.Add(this);

            var independentBoxesLength = Archetype.IndependentBoxes?.Length;
            var dependentBoxesLength   = Archetype.DependentBoxes?.Length;

            // Get type to assign to all dependent boxes
            var type = Archetype.DefaultType;

            for (int i = 0; i < independentBoxesLength; i++)
            {
                var b = GetBox(Archetype.IndependentBoxes[i]);
                if (b != null && b.HasAnyConnection)
                {
                    // Check if the current type is set based on connection hints (eg. null box got vector connection)
                    if (type == ScriptType.Null && b.Connections[0].CurrentType != ScriptType.Null)
                    {
                        type = b.Connections[0].CurrentType;
                        break;
                    }

                    // Check if that type if part of default type
                    if (Surface.CanUseDirectCast(type, b.Connections[0].DefaultType))
                    {
                        type = b.Connections[0].CurrentType;
                        break;
                    }
                }
            }

            // Assign connection type
            for (int i = 0; i < dependentBoxesLength; i++)
            {
                var b = GetBox(Archetype.DependentBoxes[i]);
                if (b != null)
                {
                    b.CurrentType = type;
                }
            }

            // Validate minor independent boxes to fit main one
            for (int i = 0; i < independentBoxesLength; i++)
            {
                var b = GetBox(Archetype.IndependentBoxes[i]);
                if (b != null)
                {
                    b.CurrentType = type;
                }
            }

            UpdateStack.Remove(this);
        }