Ejemplo n.º 1
0
        public Node(Transform transform)
        {
            _transform = transform;
            if (null != _transform)
            {
                _parentTransform = _transform.parent;
            }

            if (null == transform)
            {
                _name = "ROOT";
            }
            else
            {
                _name        = transform.name;
                _transformId = transform.GetInstanceID();

                _adapter = transform.GetComponent <ComponentAdapter>();
                if (null != _adapter)
                {
                    _adapterId = _adapter.GetInstanceID();
                }
            }

            _description = string.Format("[{0}]", GuiLookup.PathToString(transform, "->"));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Important: there is a false logic in recognizing the "old" adapters by
        /// positive instance ID and "new" adapters by negative instance ID!!!!!
        /// That is because all the components instantiated in this Unity session (since UnityEditor
        /// is turned on) have the negative ID.
        /// </summary>
        internal override void Apply()
        {
            Transform parent = TransformRegistry.Instance.Get(_parentTransformId, true);

            if (null == parent)
            {
                throw new Exception(string.Format("Cannot locate parent transform [{0}]: ", _parentTransformId));
            }

            Transform transform;

            // we need to differentiate between old and new objects
            Component unityComponent = (ComponentAdapter)EditorUtility.InstanceIDToObject(_componentInstanceId);
            bool      isAddition     = null == unityComponent;

            /**
             * Note: it would be false to check if this is a new component by using "if (ComponentInstanceId less then 0)"
             * (see the explanation above)
             * */
            if (isAddition)
            {
                /**
                 * 1.a. New adapter
                 * Creating a new game object, giving it a name of component type
                 * */
                GameObject go = new GameObject(_name); // /*_componentType.Name.Replace("Adapter", string.Empty)*/
                transform = go.transform;

                // creates a new adaptrer of a recorded type
                ComponentAdapter adapter = (ComponentAdapter)go.AddComponent(_componentType);

                #region Logging

#if DEBUG
                if (DebugMode)
                {
                    Debug.Log("Rerouting " + _componentInstanceId + " to " + adapter.GetInstanceID());
                }
#endif

                #endregion

                /**
                 * 1.b. Re-routing to a new transform/adapter
                 * */
                ComponentRegistry.Instance.Register(_componentInstanceId, adapter);
                TransformRegistry.Instance.Register(_transformId, transform);
            }
            else
            {
                /**
                 * 2. Old adapter
                 * */
                ComponentAdapter component = (ComponentAdapter)unityComponent;  //EditorUtility.InstanceIDToObject(ComponentInstanceId); // GuiLookup.GetObjectWithId(Id); // component ID
                Debug.Log("component: " + component);
                transform = component.transform;
            }

            /**
             * 3. Set the parent
             * */
            transform.parent = parent;
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        public override ItemAction Render()
        {
            //Rect drawRect = new Rect(Position.x, Position.y, 100.0f, 100.0f);
            Rect drawRect = Bounds;

            GUILayout.BeginArea(drawRect, StyleCache.Instance.ListRow); //GUI.skin.GetStyle("Box"));
            GUILayout.BeginHorizontal();

            GUILayout.Label(GuiContentCache.Instance.DragHandle, StyleCache.Instance.DragHandle, GUILayout.ExpandWidth(false));

            if (null != Color)
            {
                _oldColor           = GUI.color;
                GUI.backgroundColor = (Color)Color;
            }
            GUILayout.Label(_name, IsContainer ? StyleCache.Instance.ContainerHandle : StyleCache.Instance.ComponentHandle, GUILayout.ExpandWidth(false));

            if (null != Color)
            {
                GUI.backgroundColor = _oldColor;
            }

            GUILayout.FlexibleSpace();

            if (EditorSettings.ShowDebugOptionsInInspector)
            {
                if (EditorSettings.ShowUniqueIdInInspector)
                {
                    if (null != _adapter && null != _adapter.transform)
                    {
                        EditorGUILayout.HelpBox("T:" + _adapter.transform.GetInstanceID(), MessageType.None);
                        EditorGUILayout.HelpBox("A:" + _adapter.GetInstanceID(), MessageType.None);
                    }
                }

                if (EditorSettings.ShowGuidInInspector)
                {
                    //EditorGUILayout.HelpBox(_adapter.Guid, MessageType.None);
                }
            }

            if (GUILayout.Button("Select", StyleCache.Instance.BreadcrumbFirst, GUILayout.ExpandWidth(false)))
            {
                //Debug.Log("Selecting " + Index);
                _isSelected = true;
                //Selection.activeGameObject = ((ComponentAdapter)Data).gameObject;
            }

            if (GUILayout.Button(new GUIContent(string.Empty, TextureCache.Instance.Remove), StyleCache.Instance.ImageOnlyButton, GUILayout.Width(20)))
            {
                if (EditorApplication.isPlaying || Event.current.control)
                {
                    _isRemoved = true;
                    //return new ItemAction(ItemAction.REMOVE, _adapter);
                }

                else if (EditorUtility.DisplayDialog("Remove child?", string.Format(@"Are you sure you want to remove child component:

{0}", Data), "OK", "Cancel"))
                {
                    _isRemoved = true;
                    //return new ItemAction(ItemAction.REMOVE, _adapter);
                }
            }

            GUILayout.EndHorizontal();
            GUILayout.EndArea();

            if (_isRemoved)
            {
                _isRemoved = false;
                return(new ItemAction(ItemAction.REMOVE, _adapter));
            }
            if (_isSelected)
            {
                _isSelected = false;
                Selection.activeGameObject = ((ComponentAdapter)Data).gameObject;
            }

            Drag(drawRect);

            return(null);
        }