Beispiel #1
0
        /// <summary>
        /// Recursively instantiates components specified by descriptors in the hierarchy
        /// </summary>
        /// <param name="assignToDescriptor">Register to transform, and put into registry so it is available by clicking</param>
        public virtual void InstantiateChildren(bool assignToDescriptor)
        {
            // getting references to collections
            var childGroupDescriptors = DesignerReflection.GetChildGroupsReferences(this);

            foreach (ChildGroupDescriptor groupDescriptor in childGroupDescriptors)
            {
                List <ComponentAdapter> childAdapters = groupDescriptor.GetChildAdaptersCollection(this);

                if (null == childAdapters)
                {
                    continue;
                }

                if (null == Component) // not instantiated
                {
                    return;
                }

                Group targetContainer;
                if (null != groupDescriptor.TargetContainerMemberInfo)
                {
                    /**
                     * 1. Try reading the member as a Group
                     * */
                    targetContainer = (Group)CoreReflector.GetMemberValue(
                        groupDescriptor.TargetContainerMemberInfo,
                        Component
                        );
                }
                else
                {
                    /**
                     * 2. Try casting this to Group (if this is a Stage class for instance)
                     * */
                    targetContainer = Component as Group;
                }

                ComponentAdapterUtil.PopulateContainer(Component, targetContainer, childAdapters.ToArray(), assignToDescriptor, false, true);
            }
        }
        /// <summary>
        /// Checks if there are any changes<br/>
        /// This method is being called when play mode stopped, giving the user a chance to cancel the persistence
        /// </summary>
        /// <returns></returns>
        internal void ProcessChanges()
        {
            //Debug.Log("HasChanges? " + _target);
            _sb = new StringBuilder();
            _changes.Clear();
            _hasChanges = false;

            foreach (MemberInfo memberInfo in _savedValues.Keys)
            {
                /**
                 * 1. Read both the old and new value (i.e. the original and changed value)
                 * */
                var oldValue = _originalValues[memberInfo];
                var newValue = _savedValues[memberInfo];
#if DEBUG
                if (DebugMode)
                {
                    //Debug.Log(string.Format("   {0} [{1}, {2}]", memberInfo, oldValue, newValue));
                }
#endif

                /**
                 * 2. If one of them is null, and the other is, then values are different
                 * */
                if (oldValue == null ^ newValue == null)
                {
                    //Debug.Log("Value differs: " + newValue);
                    RegisterChanges(memberInfo, oldValue, newValue);
                    continue;                     // no need to compare actual values
                }

                /**
                 * 3. If old value isn't null, then the new value isn't null neither (because of the previous check)
                 * Doing the specific check for each type using its own Equals implementation
                 * */
                if (oldValue != null)
                {
                    List <ComponentAdapter> oldList = oldValue as List <ComponentAdapter>;

                    if (null != oldList)
                    {
                        // we have a List value

                        List <ComponentAdapter> newList = newValue as List <ComponentAdapter>;

//                        Debug.Log(string.Format(@"***** Comparing 2 lists:
//--- old ---
//{0}
//--- new ---
//{1}",
//    ComponentAdapterUtil.DescribeAdapterList(oldList),
//    ComponentAdapterUtil.DescribeAdapterList(newList)));

                        bool hasChanges = null == newList;
                        if (!hasChanges)
                        {
                            // Note: if new element added to list, it's adapter is being destroyed by now
                            hasChanges = ComponentAdapterUtil.ListContainsNullReference(oldList);
                        }
                        if (!hasChanges)
                        {
                            hasChanges = ComponentAdapterUtil.ListContainsNullReference(newList);
                        }
                        if (!hasChanges)
                        {
                            hasChanges = !ListUtil <ComponentAdapter> .Equals(oldList, newList);
                        }

                        if (hasChanges)
                        {
//                            Debug.Log(string.Format(@"  -> List changed: {0}.{1}", null == _target ? "_": _target.ToString(), memberInfo.Name));
//                            Debug.Log(@"+++++ newList:
//" + ComponentAdapterUtil.DescribeAdapterList(newList, true));
                            RegisterChanges(memberInfo, oldValue, newValue);
// ReSharper disable once RedundantJumpStatement
                            continue;
                        }
                    }

                    else if (!oldValue.Equals(newValue))
                    {
#if DEBUG
                        if (DebugMode)
                        {
                            Debug.Log("Changes: " + memberInfo.Name + ": " + newValue);
                        }
#endif
                        RegisterChanges(memberInfo, oldValue, newValue);
// ReSharper disable once RedundantJumpStatement
                        continue;
                    }
                }
            }

#if DEBUG
            if (DebugMode)
            {
                if (_hasChanges)
                {
                    Debug.Log(string.Format(@"{0}: {1} changes detected:
			
{2}", null == _target ? "-" : _target.ToString(), _changes.Count, _sb), _target);
                }
                else
                {
                    Debug.Log("NO CHANGES detected.");
                }
            }
#endif
            //return _hasChanges;
        }