Exemplo n.º 1
0
 private void WriteNode(StringBuilder xml, HashSet <CsgNode> added, CsgNode node, string indent)
 {
     if (!added.Add(node))
     {
         xml.Append(indent);
         xml.Append("<Link Id=\"");
         xml.Append(node.Id);
         xml.AppendLine("\" />");
         return;
     }
     if (node is CsgCube)
     {
         WriteNode(xml, added, (CsgCube)node, indent); return;
     }
     if (node is CsgTranslate)
     {
         WriteNode(xml, added, (CsgTranslate)node, indent); return;
     }
     if (node is CsgRotate)
     {
         WriteNode(xml, added, (CsgRotate)node, indent); return;
     }
     if (node is CsgScale)
     {
         WriteNode(xml, added, (CsgScale)node, indent); return;
     }
     if (node is CsgGroup)
     {
         WriteNode(xml, added, (CsgGroup)node, indent); return;
     }
 }
Exemplo n.º 2
0
 public bool MoveChild(CsgNode child, int toIndex)
 {
     if (children.Remove(child))
     {
         children.Insert(toIndex, child);
         return(true);
     }
     return(false);
 }
Exemplo n.º 3
0
 public bool RemoveChild(CsgNode node)
 {
     if (node != null && children.Remove(node))
     {
         node.referencedBy.Remove(this);
         return(true);
     }
     return(false);
 }
Exemplo n.º 4
0
        private void ReadElement(XElement root, CsgGroup owner)
        {
            foreach (XElement e in root.Elements())
            {
                CsgNode node = null;
                string  id   = e.Attribute("Id").GetValue();
                switch (e.Name.LocalName)
                {
                case "Link":
                    if (!nodeIds.TryGetValue(id, out node))
                    {
                        deferredLinks.Add(new Tuple <string, CsgGroup>(id, owner));
                    }
                    break;

                case "CsgCube": node = ReadCube(e); break;

                case "CsgScale": node = ReadScale(e); break;

                case "CsgTranslate": node = ReadTranslate(e); break;

                case "CsgRotate": node = ReadRotate(e); break;

                default:
                    Type type = Type.GetType("ConstructorEngine." + e.Name.LocalName);
                    if (type != null)
                    {
                        node = Activator.CreateInstance(type) as CsgNode;
                    }
                    break;
                }
                if (node != null)
                {
                    node.Name = e.Attribute("Name").GetValue(node.Name);
                    if (!string.IsNullOrEmpty(id))
                    {
                        nodeIds[id] = node;
                    }
                    if (owner != null)
                    {
                        owner.AddChild(node);
                    }
                    else
                    {
                        node.IsObjectRoot = true;
                    }
                    Repository.RegisterNode(node);
                    if (node is CsgGroup)
                    {
                        ReadElement(e, (CsgGroup)node);
                    }
                }
            }
        }
Exemplo n.º 5
0
        public void SetRootNode(CsgNode node)
        {
            float minX = 0, minY = 0, minZ = 0;
            float maxX = 0, maxY = 0, maxZ = 0;
            bool  first = true;

            World.Clear();
            node.PopulateWorld(World);

            World.Gravity = new JVector(0, -1, 0);
            foreach (RigidBody body in World.RigidBodies)
            {
                JBBox box = body.BoundingBox;
                if (first)
                {
                    first = false;
                    minX  = box.Min.X; minY = box.Min.Y; minZ = box.Min.Z;
                    maxX  = box.Max.X; maxY = box.Max.Y; maxZ = box.Max.Z;
                }
                else
                {
                    if (minX > box.Min.X)
                    {
                        minX = box.Min.X;
                    }
                    if (maxX < box.Max.X)
                    {
                        maxX = box.Max.X;
                    }
                    if (minY > box.Min.Y)
                    {
                        minY = box.Min.Y;
                    }
                    if (maxY < box.Max.Y)
                    {
                        maxY = box.Max.Y;
                    }
                    if (minZ > box.Min.Z)
                    {
                        minZ = box.Min.Z;
                    }
                    if (maxZ < box.Max.Z)
                    {
                        maxZ = box.Max.Z;
                    }
                }
            }
            RigidBody floor = new RigidBody(new BoxShape(new JVector((maxX - minX) * 3 + 1000000, 100, (maxZ - minZ) * 3 + 1000000)));

            floor.Position = new JVector((minX + maxX) * 0.5f, minY - 51, (minZ + maxZ) * 0.5f);
            floor.IsStatic = true;
            World.AddBody(floor);
        }
Exemplo n.º 6
0
 public void RegisterNode(CsgNode node)
 {
     if (Nodes.ContainsKey(node.Id))
     {
         return;
     }
     Nodes[node.Id] = node;
     foreach (CsgNode child in node.Children)
     {
         RegisterNode(child);
     }
 }
Exemplo n.º 7
0
 public bool AddChild(CsgNode node)
 {
     if (node == null || children.Contains(node))
     {
         return(false);
     }
     if (IsCyclic(node))
     {
         return(false);
     }
     children.Add(node);
     node.referencedBy.Add(this);
     return(true);
 }
Exemplo n.º 8
0
 public bool IsCyclic(CsgNode addedNode, HashSet <CsgNode> visited = null)
 {
     if (visited == null)
     {
         visited = new HashSet <CsgNode>();
     }
     if (addedNode != null && !visited.Add(addedNode))
     {
         return(true);
     }
     if (!visited.Add(this))
     {
         return(true);
     }
     foreach (CsgNode parent in ReferencedBy)
     {
         if (parent.IsCyclic(null, visited))
         {
             return(true);
         }
     }
     return(false);
 }