Esempio n. 1
0
        /// <summary>
        /// Scans the supplied parent adapter
        /// Converts ChildGroupPacks to SaveablePacks and saves them to dictionary
        /// These packs are used later (in "Process" method) to apply the changes after the play mode is stopped
        /// </summary>
        /// <param name="parentAdapter"></param>
        /// <param name="pack"></param>
        public void Update(GroupAdapter parentAdapter, ChildGroupPack pack)
        {
            //Debug.Log("Update: " + parentAdapter);
#if DEBUG
            if (DebugMode)
            {
                //Debug.Log(string.Format("Linking {0} -> {1}", parentAdapter, childAdapter));
                Debug.Log(string.Format("ParentChildLinker: Updating {0}", parentAdapter));
            }
#endif
            //Debug.Log("parentAdapter: " + parentAdapter);
            // 1. we have to monitor parentAdapter for later (if not yet monitored)
            if (Application.isPlaying)
            {
                PersistenceManager.Instance.Watch(parentAdapter);
            }

            // 2. if no pack supplied, read it now
            if (null == pack)
            {
                //Debug.Log("ParentChildLinker: Pack not defined. Reading now.");
                pack = ChildGroupPack.Read(parentAdapter);
            }

            // 3. register adapters for later
            pack.RegisterAdapters();

            // 4. get relation object
            var guidPack = pack.ToSaveablePack();

            // 5. cache it (overwrite existing!)
            _changes[guidPack.ParentInstanceId] = guidPack;
        }
Esempio n. 2
0
        public static ChildGroupPack Read(GroupAdapter groupAdapter)
        {
#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Read"));
            }
#endif
            ChildGroupPack childGroupPack = new ChildGroupPack(groupAdapter);

            var groupDescriptors = DesignerReflection.GetChildGroupsReferences(groupAdapter);

            foreach (ChildGroupDescriptor groupDescriptor in groupDescriptors)
            {
                List <ComponentAdapter> adapters = Core.Reflection.CoreReflector.GetMemberValue(groupDescriptor.CollectionMemberInfo, groupAdapter) as List <ComponentAdapter>;

                ChildGroup childGroup = new ChildGroup(adapters)
                {
                    GroupName = groupDescriptor.CollectionMemberInfo.Name
                };
                childGroupPack.Add(childGroup);
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log("Read. childGroupPack: " + childGroupPack);
            }
#endif

            return(childGroupPack);
        }
Esempio n. 3
0
        public GroupManager(GroupAdapter adapter)
        {
            //Debug.Log("Creating GroupManager");
            _groupAdapter = adapter;
            _groupDescriptors = DesignerReflection.GetChildGroupsReferences(adapter);

            _pack = ChildGroupPack.Read(adapter);
        }
Esempio n. 4
0
        /// <summary>
        /// Note: this method actually changes the collections
        /// </summary>
        /// <param name="adapter"></param>
        /// <returns></returns>
        public ChildGroupPack Remove(ComponentAdapter adapter)
        {
            _pack = ChildGroupPack.Read(_groupAdapter);

            //Debug.Log(string.Format("Reorder: grp:{0}, item:{1}", groupIndex, itemIndex));

            // get old group index
            var oldGroupIndex = _pack.GetGroupHostingAdapterIndex(adapter);

            // remove adapter from old group
            _pack.Groups[oldGroupIndex].Remove(adapter);

            return _pack;
        }
        public static void Write(GroupAdapter parentAdapter, ChildGroupPack pack)
        {
            ChildGroupPack currentGroupPack = ChildGroupPack.Read(parentAdapter);
            Debug.Log(string.Format("ParentAdapter: {0}, {1}", parentAdapter,
                                    parentAdapter.GetInstanceID()));

            for (int i = 0; i < currentGroupPack.Groups.Count; i++)
            {
                var currentGroup = currentGroupPack.Groups[i];
                Debug.Log("newGroup.Adapters: " + currentGroup.Adapters.Count);
                currentGroup.Adapters.Clear();
                //Debug.Log("newGroup.Adapters 2: " + newGroup.Adapters.Count);
                currentGroup.Adapters.AddRange(pack.Groups[i].Adapters);
                Debug.Log("newGroup.Adapters 2: " + currentGroup.Adapters.Count);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Evaluates saved Adapter IDs to real adapters
        /// Note: DO NOT use useEditorUtility=true because it will grab the old adapter!!!
        /// </summary>
        /// <returns></returns>
        public static ChildGroupPack Apply(SaveablePack saveablePack)
        {
#if DEBUG
            if (DebugMode)
            {
                Debug.Log(@"*** WRITING PACK ***
" + saveablePack);
            }
#endif

            // Note: we have to call the override having the second argument, supplying "true"
            // This is because if this is the adapter existing prior to play mode start, the dictionary points to the "old" adapter (a stale one)
            // this way we are making sure that the system looks for the specified adapter in the scene first, and then uses the dictionary (for the "new" adapters)
            GroupAdapter parentAdapter = ComponentRegistry.Instance.Get(saveablePack.ParentInstanceId, true) as GroupAdapter;

            ChildGroupPack childGroupPack = Read(parentAdapter);

            int index = 0;
            foreach (List <int> childGroupIds in saveablePack.InstanceIds)
            {
                List <ComponentAdapter> adapters = new List <ComponentAdapter>();
                foreach (int adapterId in childGroupIds)
                {
                    ComponentAdapter childAdapter = (ComponentAdapter)ComponentRegistry.Instance.Get(adapterId, true);  // FALSE!!!

                    /*if (null == childAdapter)
                     *  throw new Exception("Cannot find child adapter in dictionary: " + adapterId);*/

                    adapters.Add(childAdapter);
                }

                var actualGroup = childGroupPack.Groups[index];
                actualGroup.Clear();
                actualGroup.AddRange(adapters);

                index++;
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log("Apply. childGroupPack: " + childGroupPack);
            }
#endif

            return(childGroupPack);
        }
Esempio n. 7
0
        public static void Write(GroupAdapter parentAdapter, ChildGroupPack pack)
        {
            ChildGroupPack currentGroupPack = ChildGroupPack.Read(parentAdapter);

            Debug.Log(string.Format("ParentAdapter: {0}, {1}", parentAdapter,
                                    parentAdapter.GetInstanceID()));

            for (int i = 0; i < currentGroupPack.Groups.Count; i++)
            {
                var currentGroup = currentGroupPack.Groups[i];
                Debug.Log("newGroup.Adapters: " + currentGroup.Adapters.Count);
                currentGroup.Adapters.Clear();
                //Debug.Log("newGroup.Adapters 2: " + newGroup.Adapters.Count);
                currentGroup.Adapters.AddRange(pack.Groups[i].Adapters);
                Debug.Log("newGroup.Adapters 2: " + currentGroup.Adapters.Count);
            }
        }
Esempio n. 8
0
        public static void DescribeChildrenWithGroups(ref StringBuilder sb, Node node, bool richText)
        {
            var childNodes = node.ChildNodes;

            var unprocessedNodes = ListUtil <Node> .Clone(node.ChildNodes);

            var adapter = node.Adapter;

            //Debug.Log(@"adapter: " + adapter);
            var containerAdapter = adapter as GroupAdapter;

            // Note: the ROOT node has no adapter nor groups defined!
            if (null != containerAdapter)
            {
                ChildGroupPack pack = ChildGroupPack.Read(containerAdapter);
                //                Debug.Log(@"pack:
                //" + pack);
                foreach (ChildGroup childGroup in pack.Groups)
                {
                    sb.AppendLine(StringUtil.Indent(node.Depth + 1, string.Format("== {0} ==", childGroup.GroupName)));
                    foreach (ComponentAdapter componentAdapter in childGroup.Adapters)
                    {
                        var childNode = GetNode(childNodes, componentAdapter);
                        if (null == childNode)
                        {
                            var line = StringUtil.Indent(node.Depth + 1, string.Format("*** Not found ***"));
                            if (richText)
                            {
                                line = (StringUtil.WrapColor(StringUtil.WrapTag(line, "b"), EditorSettings.UseDarkSkin ? "yellow" : "blue"));
                            }
                            sb.AppendLine(line);
                        }
                        else
                        {
                            unprocessedNodes.Remove(childNode);
                            string name = null == childNode.Transform ? "New" : childNode.Transform.name;

                            var    isSelected = Selection.activeGameObject == componentAdapter.gameObject;
                            string selected   = isSelected ? "*" : string.Empty;
                            string changed    = ParentChildLinker.Instance.IsChanged(childNode.AdapterId) ? "[changed]" : string.Empty;

                            var line = StringUtil.Indent(childNode.Depth /* + 1*/,
                                                         string.Format("{2}{0} [A:{1}]{3}", name, childNode.AdapterId, selected, changed));

                            if (richText && isSelected)
                            {
                                line = StringUtil.WrapTag(line, "b");
                            }

                            sb.AppendLine(line);
                            DescribeChildrenWithGroups(ref sb, childNode, richText);
                        }
                    }
                }
            }

            if (unprocessedNodes.Count > 0 && null != node.ParentTransform)
            {
                sb.AppendLine(StringUtil.Indent(node.Depth + 1, string.Format("== Orphans ==")));
            }

            // orphans
            foreach (Node childNode in unprocessedNodes)
            {
                string name       = null == childNode.Transform ? "New" : childNode.Transform.name;
                var    isSelected = (Selection.activeGameObject == childNode.Adapter.gameObject);
                string selected   = isSelected ? "*" : string.Empty;
                string changed    = ParentChildLinker.Instance.IsChanged(childNode.AdapterId) ? "[changed]" : string.Empty;

                bool   isOrphan = !(childNode.Adapter is StageAdapter);
                string orphan   = isOrphan ? "[orphan] " : string.Empty;
                //string orphan = StringUtil.WrapColor((childNode.Adapter is StageAdapter) ? string.Empty : "[orphan] ",
                //EditorSettings.UseDarkSkin ? "yellow" : "blue");

                var line = StringUtil.Indent(childNode.Depth /* + 1*/, string.Format("{4}{2}{0} [A:{1}]{3}", name, childNode.AdapterId, selected, changed, orphan));
                if (isOrphan)
                {
                    line = StringUtil.WrapColor(StringUtil.WrapTag(line, "b"), EditorSettings.UseDarkSkin ? "yellow" : "blue");
                }

                if (richText && isSelected)
                {
                    line = StringUtil.WrapTag(line, "b");
                }

                sb.AppendLine(line);
                DescribeChildrenWithGroups(ref sb, childNode, richText);
            }
        }
Esempio n. 9
0
 /// <summary>
 /// Looks for adapters in the scene specified by saved IDs
 /// Applies the values to each parent adapet (collection)
 /// </summary>
 public void Write()
 {
     ChildGroupPack.Apply(this); //.Write();
 }
Esempio n. 10
0
        /// <summary>
        /// Scans the supplied parent adapter
        /// Converts ChildGroupPacks to SaveablePacks and saves them to dictionary
        /// These packs are used later (in "Process" method) to apply the changes after the play mode is stopped
        /// </summary>
        /// <param name="parentAdapter"></param>
        /// <param name="pack"></param>
        public void Update(GroupAdapter parentAdapter, ChildGroupPack pack)
        {
            //Debug.Log("Update: " + parentAdapter);
#if DEBUG
            if (DebugMode)
            {
                //Debug.Log(string.Format("Linking {0} -> {1}", parentAdapter, childAdapter));
                Debug.Log(string.Format("ParentChildLinker: Updating {0}", parentAdapter));
            }
#endif
            //Debug.Log("parentAdapter: " + parentAdapter);
            // 1. we have to monitor parentAdapter for later (if not yet monitored)
            if (Application.isPlaying)
                PersistenceManager.Instance.Watch(parentAdapter);

            // 2. if no pack supplied, read it now
            if (null == pack)
            {
                //Debug.Log("ParentChildLinker: Pack not defined. Reading now.");
                pack = ChildGroupPack.Read(parentAdapter);
            }

            // 3. register adapters for later
            pack.RegisterAdapters();

            // 4. get relation object
            var guidPack = pack.ToSaveablePack();

            // 5. cache it (overwrite existing!)
            _changes[guidPack.ParentInstanceId] = guidPack;
        }
Esempio n. 11
0
        /// <summary>
        /// Recursive!
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="node"></param>
        private static void DoFix(ref StringBuilder sb, Node node)
        {
            bool isRoot = (null == node.Adapter);
            //if (!isRoot)
            //    Debug.Log(StringUtil.Indent(node.Depth, "Fixing " + node.Adapter));

            var childNodes = node.ChildNodes;

            var unprocessedNodes = ListUtil <Node> .Clone(node.ChildNodes);

            var adapter = node.Adapter;

            //Debug.Log(@"adapter: " + adapter);
            var containerAdapter = adapter as GroupAdapter;

            //Debug.Log(@"containerAdapter: " + containerAdapter);

            // Note: the ROOT node has no adapter no groups defined!

            if (null != containerAdapter)
            {
                ChildGroupPack pack = ChildGroupPack.Read(containerAdapter);
                // Debug.Log(@"pack:
                //" + pack);
                foreach (ChildGroup childGroup in pack.Groups)
                {
                    //sb.AppendLine(StringUtil.Indent(node.Depth + 1, string.Format("== {0} ==", childGroup.GroupName)));
                    //foreach (ComponentAdapter componentAdapter in childGroup.Adapters)
                    for (int i = childGroup.Adapters.Count - 1; i >= 0; i--)
                    {
                        ComponentAdapter componentAdapter = childGroup.Adapters[i];

                        bool doRemove = false;

                        /**
                         * 1. Handle null
                         * */
                        if (null == componentAdapter)
                        {
                            //sb.AppendLine(StringUtil.Indent(node.Depth + 1, "*** Not found ***"));
                            // adapter is not child of this container, remove it from the list
                            //toRemove.Add(componentAdapter);
                            doRemove = true;
                        }

                        /**
                         * 2. Not null. Handle the adapter
                         * */
                        else
                        {
                            var childNode = GetNode(childNodes, componentAdapter);
                            if (null == childNode)
                            {
                                //sb.AppendLine(StringUtil.Indent(node.Depth + 1, "*** Not found ***"));
                                // adapter is not child of this container, remove it from the list
                                //toRemove.Add(componentAdapter);
                                doRemove = true;
                            }
                            else
                            {
                                unprocessedNodes.Remove(childNode);
                                DoFix(ref sb, childNode);
                            }
                        }

                        if (doRemove)
                        {
                            //Debug.Log("list 1: " + ComponentAdapterUtil.DescribeAdapterList(childGroup.Adapters));
                            childGroup.Adapters.RemoveAt(i);
                            //childGroup.Adapters.Remove(adapterToRemove);
                            if (null != componentAdapter)
                            {
                                sb.AppendLine(string.Format("{0}: {1} [A:{2}] removed", GuiLookup.PathToString(containerAdapter.transform, "->"), componentAdapter, componentAdapter.GetInstanceID()));
                            }
                            else
                            {
                                sb.AppendLine(string.Format("{0}: Adapter at position {1} removed", GuiLookup.PathToString(containerAdapter.transform, "->"), i));
                            }
                            //Debug.Log("list 2: " + ComponentAdapterUtil.DescribeAdapterList(childGroup.Adapters));
                        }
                    }
                }
            }

            // orphans
            foreach (Node childNode in unprocessedNodes)
            {
                if (!isRoot)
                {
                    if (null != containerAdapter)
                    {
                        ChildGroupPack pack = ChildGroupPack.Read(containerAdapter);
                        if (pack.Groups.Count > 0)
                        {
                            pack.Groups[0].Adapters.Add(childNode.Adapter);
                            sb.AppendLine(string.Format("{0}: [{1}] added to the first group", GuiLookup.PathToString(containerAdapter.transform, "->"), childNode.Adapter));
                        }
                    }
                }
                DoFix(ref sb, childNode);
            }
        }
Esempio n. 12
0
        private void RedrawComponentsInGameView(ChildGroupPack pack)
        {
            for (int i = 0; i < pack.Groups.Count; i++)
            {
                ChildGroupDescriptor groupDescriptor = _groupManager.GroupDescriptors[i];

                List<ComponentAdapter> adaptersCollection = groupDescriptor.GetChildAdaptersCollection(GroupAdapter);
                
                if (null == adaptersCollection)
                    return;

                // TODO: move to parent child linker
                var targetContainer = groupDescriptor.GetTargetContainer((IContentChildList)GroupAdapter.Component);
                if (null == targetContainer)
                    continue; // not instantiated

                targetContainer.RemoveAllContentChildren();

                foreach (ComponentAdapter adapter in adaptersCollection)
                {
                    if (null != adapter && adapter.gameObject.activeInHierarchy && adapter.enabled) // the adapter could be disabled in many ways
                        targetContainer.AddContentChild(adapter.Component);
                }
            }
        }
Esempio n. 13
0
        public static ChildGroupPack Read(GroupAdapter groupAdapter)
        {
#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format(@"Read"));
            }
#endif
            ChildGroupPack childGroupPack = new ChildGroupPack(groupAdapter);

            var groupDescriptors = DesignerReflection.GetChildGroupsReferences(groupAdapter);

            foreach (ChildGroupDescriptor groupDescriptor in groupDescriptors)
            {
                List<ComponentAdapter> adapters = Core.Reflection.CoreReflector.GetMemberValue(groupDescriptor.CollectionMemberInfo, groupAdapter) as List<ComponentAdapter>;

                ChildGroup childGroup = new ChildGroup(adapters)
                {
                    GroupName = groupDescriptor.CollectionMemberInfo.Name
                };
                childGroupPack.Add(childGroup);
            }

#if DEBUG
            if (DebugMode)
            {
                Debug.Log("Read. childGroupPack: " + childGroupPack);
            }
#endif

            return childGroupPack;
        }
Esempio n. 14
0
        /// <summary>
        /// Note: this method actually changes the collections
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="groupIndex"></param>
        /// <param name="itemIndex"></param>
        /// <returns></returns>
        public ChildGroupPack Reorder(ComponentAdapter adapter, int groupIndex, int itemIndex)
        {
            _pack = ChildGroupPack.Read(_groupAdapter);

            //Debug.Log(string.Format("Reorder: grp:{0}, item:{1}", groupIndex, itemIndex));

            // get old group index
            var oldGroupIndex = _pack.GetGroupHostingAdapterIndex(adapter);

            // remove adapter from old group
            _pack.Groups[oldGroupIndex].Remove(adapter);

            // add adapter to a new group
            _pack.Groups[groupIndex].Insert(adapter, itemIndex);

            return _pack; // (ChildGroupPack)_pack.Clone();
            
            ////Debug.Log("_groupDescriptors.Count: " + _groupDescriptors.Count);
            ////Debug.Log("groupIndex: " + groupIndex);
            //ChildGroupDescriptor groupDescriptor = _groupDescriptors[groupIndex];
            ////Debug.Log("groupDescriptor: " + groupDescriptor);

            //List<ComponentAdapter> adaptersCollection = groupDescriptor.GetChildAdaptersCollection(_containerAdapter);
            ////var targetContainer = groupDescriptor.GetTargetContainer((Container)_containerAdapter.Component);

            ////Debug.Log("adaptersCollection: " + adaptersCollection);

            //if (null == adaptersCollection)
            //    return;

            ///**
            // * 1. Find the old collection containing the adapter and remove the adapter
            // * */
            //var oldCollection = GetGroupContainingAdapter(adapter);
            ////Debug.Log("Old collection: " + collection);
            //oldCollection.Remove(adapter);

            ///**
            // * 2. Insert it into a new collection at given index
            // * */

            //if (oldCollection == adaptersCollection)
            //    itemIndex = Math.Max(itemIndex, 0);

            ////Debug.Log(string.Format("Inserting to : [{0}] at index {1}", adaptersCollection, itemIndex));
            //adaptersCollection.Insert(itemIndex, adapter);

            ////Debug.Log("targetContainer: " + targetContainer);
            ////Debug.Log("newItemIndex: " + itemIndex);
            ////Debug.Log("childAdapters[newItemIndex]: " + adaptersCollection[itemIndex]);

            //if (Application.isPlaying)
            //    Flush();

            ////var comp = adaptersCollection[itemIndex].Component;

            ////if (null != targetContainer && null != comp)
            ////    targetContainer.AddContentChildAt(itemIndex, comp);

            ////            if (_depthMode)
            ////            {
            ////                ContainerAdapter.SetChildDepth(index, adapter);
            ////            }
            ////            else
            ////            {
            ////                ContainerAdapter.Reorder(index, adapter);
            ////            }
        }