public void AddChild(ProjectItemOld item)
        {
            if (Children == null)
            {
                Children = new List <ProjectItemOld>(1);
            }

            item.Parent = this;
            Children.Add(item);
        }
 public void FindChildren(List <ProjectItemOld> items, ProjectItemType type)
 {
     if (Children != null)
     {
         for (int i = 0; i < Children.Count; ++i)
         {
             ProjectItemOld child = Children[i];
             if ((child.Type & type) != 0)
             {
                 items.Add(Children[i]);
             }
         }
     }
 }
        public void SetParent(ProjectItemOld item)
        {
            if (Parent != null)
            {
                if (Parent.Children != null)
                {
                    Parent.Children.Remove(this);
                }
            }

            Parent = item;
            if (Parent.Children == null)
            {
                Parent.Children = new List <ProjectItemOld>();
            }
            Parent.Children.Add(this);
        }
        public static void Foreach(ProjectItemOld item, System.Action <ProjectItemOld> visit)
        {
            if (item == null)
            {
                return;
            }

            visit(item);
            if (item.Children == null)
            {
                return;
            }

            for (int i = 0; i < item.Children.Count; ++i)
            {
                Foreach(item.Children[i], visit);
            }
        }
        public override string ToString()
        {
            StringBuilder  sb     = new StringBuilder();
            ProjectItemOld parent = this;

            while (parent != null)
            {
                sb.Insert(0, parent.Id);
                sb.Insert(0, "/");
                parent = parent.Parent;
            }

            if (Type == ProjectItemType.Scene)
            {
                return(sb.ToString() + ".rtsc");
            }
            else if (Type == ProjectItemType.Resource)
            {
                return(sb.ToString() + ".rtrs");
            }

            return(sb.ToString());
        }
        public ProjectItemOld(string id, ProjectItemType type, ProjectItemOld parent = null, KnownResourceType[] resourceTypes = null)
        {
            if (!IsValidId(id))
            {
                throw new System.FormatException("id has invalid characters");
            }
            if (id != null)
            {
                if (type != ProjectItemType.Resource)
                {
                    if (id[0] == '-' || char.IsDigit(id[0]))
                    {
                        throw new System.FormatException("id should start with character");
                    }
                }
            }


            Parent        = parent;
            Id            = id;
            Type          = type;
            ResourceTypes = resourceTypes;
        }
        private ProjectItemOld Find(ProjectItemOld item, System.Func <ProjectItemOld, bool> visit)
        {
            if (visit(item))
            {
                return(item);
            }

            if (item.Children == null)
            {
                return(null);
            }

            for (int i = 0; i < item.Children.Count; ++i)
            {
                ProjectItemOld result = Find(item.Children[i], visit);
                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
 public void RemoveChild(ProjectItemOld item)
 {
     Children.Remove(item);
     item.Parent = null;
 }
        public static string GetUniqueName(string baseName, ProjectItemOld parentFolder, ProjectItemType type)
        {
            if (parentFolder.Children == null || parentFolder.Children.Count == 0)
            {
                return(baseName);
            }

            while (baseName.Length > 0)
            {
                if (char.IsDigit(baseName[baseName.Length - 1]))
                {
                    baseName = baseName.Substring(0, baseName.Length - 1);
                    baseName = baseName.TrimEnd(' ');
                }
                else
                {
                    break;
                }
            }

            string[]      names = parentFolder.Children.Where(projectItem => (projectItem.Type & type) != 0 && projectItem.Id.Contains(baseName)).Select(projectItem => projectItem.Id).ToArray();
            HashSet <int> ids   = new HashSet <int>();

            for (int i = 0; i < names.Length; ++i)
            {
                string name = names[i];
                if (name == baseName)
                {
                    if (!ids.Contains(0))
                    {
                        ids.Add(0);
                        continue;
                    }
                }
                string[] parts    = name.Split(' ');
                string   lastPart = parts[parts.Length - 1];
                int      v;
                if (int.TryParse(lastPart, out v))
                {
                    if (!ids.Contains(v))
                    {
                        ids.Add(v);
                    }
                }
            }

            if (ids.Count == 0)
            {
                return(baseName);
            }

            if (ids.Count > 10000)
            {
                return(baseName + " " + System.Guid.NewGuid());
            }


            int max = ids.Max();

            do
            {
                max++;
            }while (ids.Contains(max));
            return(baseName + " " + max);
        }