示例#1
0
文件: VexTree.cs 项目: yzqlwt/Swf2XNA
        private void ParseJoint(V2DJointKind jointKind, Instance inst)
        {
            Dictionary <string, Joint2D> joints = parentStack.Peek().Definition.Joints;
            string nm       = inst.Name;
            int    firstUs  = nm.IndexOf('_');
            string jName    = (firstUs > -1) ? nm.Substring(0, firstUs) : nm;
            string targName = (firstUs > -1) ? nm.Substring(firstUs + 1) : "";

            Matrix m   = inst.Transformations[0].Matrix;
            float  rot = (float)((m.GetMatrixComponents().Rotation + 90) * Math.PI / 180);

            Point p = new Point(
                inst.Transformations[0].Matrix.TranslateX,
                inst.Transformations[0].Matrix.TranslateY);

            if (joints.ContainsKey(jName))
            {
                joints[jName].AppendData(p, rot);
            }
            else
            {
                joints.Add(jName, new Joint2D(jName, targName, jointKind, p, rot));
            }

            if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
            {
                joints[jName].Transforms.Add(new Point(
                                                 inst.Transformations[1].Matrix.TranslateX,
                                                 inst.Transformations[1].Matrix.TranslateY));
            }
        }
示例#2
0
 public Joint2D(string name, string targetName, V2DJointKind kind, Point location, float rotation)
 {
     this.Name       = name;
     this.TargetName = targetName;
     this.JointKind  = kind;
     this.Locations.Add(location);
     this.Rotations.Add(rotation);
 }
示例#3
0
        public List<Point> Transforms = new List<Point>(); // for pulley joint

        #endregion Fields

        #region Constructors

        public Joint2D(string name, string targetName, V2DJointKind kind, Point location, float rotation)
        {
            this.Name = name;
            this.TargetName = targetName;
            this.JointKind = kind;
            this.Locations.Add(location);
            this.Rotations.Add(rotation);
        }
示例#4
0
        private void ParseJoint(V2DJointKind jointKind, Instance inst)
        {
            List <V2DJoint> joints   = curDef.Joints; //List<V2DJoint>
            string          nm       = inst.Name;
            int             firstUs  = nm.IndexOf('_');
            string          jName    = (firstUs > -1) ? nm.Substring(0, firstUs) : nm;
            string          targName = (firstUs > -1) ? nm.Substring(firstUs + 1) : "";

            Point p = new Point(
                inst.Transformations[0].Matrix.TranslateX,
                inst.Transformations[0].Matrix.TranslateY);

            V2DJoint joint = joints.Find(jt => jt.Name == jName);

            if (joint == null)
            {
                joint      = new V2DJoint();
                joint.Name = jName;
                joint.Type = jointKind;
                joint.X2   = p.X;
                joint.Y2   = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor1X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor1Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
            else
            {
                joint.X2 = p.X;
                joint.Y2 = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor2X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor2Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
        }
示例#5
0
        private void ParseJoint(V2DJointKind jointKind, Instance inst)
        {
            Dictionary<string, Joint2D> joints = parentStack.Peek().Definition.Joints;
            string nm = inst.Name;
            int firstUs = nm.IndexOf('_');
            string jName = (firstUs > -1) ? nm.Substring(0, firstUs) : nm;
            string targName = (firstUs > -1) ? nm.Substring(firstUs + 1) : "";

            Matrix m = inst.Transformations[0].Matrix;
            float rot = (float)((m.GetMatrixComponents().Rotation + 90) * Math.PI / 180);

            Point p = new Point(
                inst.Transformations[0].Matrix.TranslateX,
                inst.Transformations[0].Matrix.TranslateY);

            if (joints.ContainsKey(jName))
            {
                joints[jName].AppendData(p, rot);
            }
            else
            {
                joints.Add(jName, new Joint2D(jName, targName, jointKind, p, rot));
            }

            if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
            {
                joints[jName].Transforms.Add(new Point(
                    inst.Transformations[1].Matrix.TranslateX,
                    inst.Transformations[1].Matrix.TranslateY));
            }
        }
示例#6
0
        private void ParseJoint(V2DJointKind jointKind, Instance inst)
        {
            List<V2DJoint> joints = curDef.Joints; //List<V2DJoint>
            string nm = inst.Name;
            int firstUs = nm.IndexOf('_');
            string jName = (firstUs > -1) ? nm.Substring(0, firstUs) : nm;
            string targName = (firstUs > -1) ? nm.Substring(firstUs + 1) : "";

            Point p = new Point(
                inst.Transformations[0].Matrix.TranslateX,
                inst.Transformations[0].Matrix.TranslateY);

            V2DJoint joint = joints.Find(jt => jt.Name == jName);
            if (joint == null)
            {
                joint = new V2DJoint();
                joint.Name = jName;
                joint.Type = jointKind;
                joint.X2 = p.X;
                joint.Y2 = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor1X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor1Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
            else
            {
                joint.X2 = p.X;
                joint.Y2 = p.Y;
                if (inst.Transformations.Count > 1) // pulley defined by joints on frame 2
                {
                    joint.GroundAnchor2X = inst.Transformations[1].Matrix.TranslateX;
                    joint.GroundAnchor2Y = inst.Transformations[1].Matrix.TranslateY;
                }
            }
        }
示例#7
0
文件: VexTree.cs 项目: yzqlwt/Swf2XNA
        private void GenerateJointData(Definition2D def)
        {
            if (!def.IsDefined)             // todo: really really need to do this isDefined crap right.
            {
                foreach (Joint2D j in def.Joints.Values)
                {
                    string jName = j.Name;
                    EnsureBodyTags(j);
                    string     b1Name = j.data["body1"];
                    string     b2Name = j.data["body2"];
                    Instance2D b1Body = (b1Name == V2D.ROOT_NAME) ? rootInstance : parentStack.Peek().Definition.GetChildByName(b1Name);
                    Instance2D b2Body = (b2Name == V2D.ROOT_NAME) ? rootInstance : parentStack.Peek().Definition.GetChildByName(b2Name);

                    V2DJointKind jointKind = j.JointKind;

                    if (!j.data.ContainsKey("location"))
                    {
                        if (V2D.OUTPUT_TYPE == OutputType.Swf)
                        {
                            j.data.Add("location", "#" + j.Locations[0].ToString());
                        }
                        else
                        {
                            j.data.Add("X", j.Locations[0].X.ToString());
                            j.data.Add("Y", j.Locations[0].Y.ToString());
                        }
                    }
                    if ((j.Locations.Count > 1) && (jointKind == V2DJointKind.Distance || jointKind == V2DJointKind.Pully))
                    {
                        if (!j.data.ContainsKey("location2"))
                        {
                            if (V2D.OUTPUT_TYPE == OutputType.Swf)
                            {
                                j.data.Add("location2", "#" + j.Locations[1].ToString());
                            }
                            else
                            {
                                j.data.Add("X2", j.Locations[1].X.ToString());
                                j.data.Add("Y2", j.Locations[1].Y.ToString());
                            }
                        }
                        if (jointKind == V2DJointKind.Pully)
                        {
                            Point groundAnchor1;
                            Point groundAnchor2;
                            if (b1Body.Transforms.Count > 1)
                            {
                                groundAnchor1 = new Point(
                                    b1Body.Transforms[1].Matrix.TranslateX,
                                    b1Body.Transforms[1].Matrix.TranslateY);
                                groundAnchor2 = new Point(
                                    b2Body.Transforms[1].Matrix.TranslateX,
                                    b2Body.Transforms[1].Matrix.TranslateY);
                            }
                            else
                            {
                                groundAnchor1 = j.Transforms[0];
                                groundAnchor2 = j.Transforms[1];
                            }

                            float d1x        = groundAnchor1.X - j.Locations[0].X;                     // m1a.TranslateX;
                            float d1y        = groundAnchor1.Y - j.Locations[0].Y;                     //m1a.TranslateY;
                            float d2x        = groundAnchor2.X - j.Locations[1].X;                     //m2a.TranslateX;
                            float d2y        = groundAnchor2.Y - j.Locations[1].Y;                     //m2a.TranslateY;
                            float maxLength1 = (float)Math.Sqrt(d1x * d1x + d1y * d1y);
                            float maxLength2 = (float)Math.Sqrt(d2x * d2x + d2y * d2y);
                            //j.data.Add("groundAnchor1", groundAnchor1.ToString());
                            //j.data.Add("groundAnchor2", groundAnchor2.ToString());
                            j.data.Add("groundAnchor1X", groundAnchor1.X.ToString());
                            j.data.Add("groundAnchor1Y", groundAnchor1.Y.ToString());
                            j.data.Add("groundAnchor2X", groundAnchor2.X.ToString());
                            j.data.Add("groundAnchor2Y", groundAnchor2.Y.ToString());
                            j.data.Add("maxLength1", maxLength1.ToString());
                            j.data.Add("maxLength2", maxLength2.ToString());
                        }
                    }
                    else if (b1Body.Transforms.Count > 1 || b2Body.Transforms.Count > 1)
                    {
                        List <Transform> tr = (b1Body.Transforms.Count > 1) ? b1Body.Transforms : b2Body.Transforms;
                        if (jointKind == V2DJointKind.Revolute)
                        {
                            float start = (float)(tr[0].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            float rmin  = (float)(tr[1].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            float rmax  = (float)(tr[2].Matrix.GetMatrixComponents().Rotation / 180 * Math.PI);
                            rmin = rmin - start;
                            rmax = rmax - start;
                            if (rmin > 0)
                            {
                                rmin = (float)(Math.PI * -2 + rmin);
                            }
                            j.data.Add("min", rmin.ToString());
                            j.data.Add("max", rmax.ToString());
                        }
                        else if (jointKind == V2DJointKind.Prismatic)
                        {
                            Point a = new Point(
                                tr[0].Matrix.TranslateX,
                                tr[0].Matrix.TranslateY);
                            Point p0 = new Point(
                                tr[1].Matrix.TranslateX,
                                tr[1].Matrix.TranslateY);
                            Point p1 = new Point(
                                tr[2].Matrix.TranslateX,
                                tr[2].Matrix.TranslateY);

                            double len = Math.Sqrt((p1.Y - p0.Y) * (p1.Y - p0.Y) + (p1.X - p0.X) * (p1.X - p0.X));
                            double r   = ((p0.Y - a.Y) * (p0.Y - p1.Y) - (p0.X - a.X) * (p0.X - p1.X)) / (len * len);

                            float axisX   = p1.X - p0.X;
                            float axisY   = p1.Y - p0.Y;
                            float maxAxis = Math.Abs(axisX) > Math.Abs(axisY) ? axisX : axisY;
                            axisX /= maxAxis;
                            axisY /= maxAxis;

                            Point ap0 = new Point(p0.X - a.X, p0.Y - a.Y);
                            Point ap1 = new Point(p1.X - a.X, p1.Y - a.Y);
                            float min = (float)-(Math.Abs(r * len));
                            float max = (float)((1 - Math.Abs(r)) * len);
                            // Point ab = new Point(b.X - a.X, b.Y - a.Y);

                            // float r0 = (ap0.X * ab.X + ap0.Y * ab.Y) / (w * w); // dot product
                            //float r1 = (ap1.X * ab.X + ap1.Y * ab.Y) / (w * w); // dot product

                            j.data.Add("axisX", axisX.ToString());
                            j.data.Add("axisY", axisY.ToString());
                            j.data.Add("min", min.ToString());
                            j.data.Add("max", max.ToString());
                        }
                    }
                }
            }
        }
示例#8
0
文件: VexTree.cs 项目: yzqlwt/Swf2XNA
        private void EnsureDefinition(Instance inst, IDefinition def)
        {
            DefinitionKind dk      = (DefinitionKind)def.UserData;
            bool           addInst = (inst != null);
            Matrix         m       = (inst == null) ? Matrix.Identity : inst.Transformations[0].Matrix;

            if ((dk & DefinitionKind.Ignore) == 0)
            {
                if (((dk & DefinitionKind.JointMarker) != 0) && (inst != null))
                {
                    V2DJointKind jointKind = jointKindMap[jointKinds.IndexOf(def.Name)];
                    ParseJoint(jointKind, inst);
                }
                else if (((dk & DefinitionKind.ShapeMarker) != 0) && (inst != null))
                {
                    if (!parentStack.Peek().Definition.IsDefined)
                    {
                        parentStack.Peek().Definition.AddShapes(curVo, def, m);
                    }
                }

                if ((dk & DefinitionKind.TextField) != 0)
                {
                    Text txt = (Text)def;
                    if (def.Name == null)
                    {
                        def.Name = "$tx_" + def.Id;
                    }
                    Definition2D d2d = GetTextDefinition(m, txt);
                    if (txt.TextRuns.Count > 0 && !txt.TextRuns[0].isEditable)
                    {
                        AddSymbolImage(def);
                    }

                    if (addInst)
                    {
                        AddInstance(inst, def);
                    }
                }

                if ((dk & DefinitionKind.Timeline) != 0)
                {
                    Definition2D d2d = GetDefinition2D(m, def);

                    Instance2D i2d;
                    if (addInst)
                    {
                        i2d = AddInstance(inst, def);
                    }
                    else
                    {
                        i2d = CreateInstance2D(def);
                    }
                    parentStack.Push(i2d);
                    ParseTimeline((Timeline)def);
                    i2d.Definition.IsDefined = true;
                    parentStack.Pop();
                }

                if ((dk & DefinitionKind.Symbol) != 0)
                {
                    // todo: this is just adding images
                    //Body2D b2d = CreateBody2D(inst, def);
                    if (def.Name == null)
                    {
                        def.Name = "$sym_" + def.Id;
                    }
                    Definition2D d2d = GetDefinition2D(m, def);
                    AddSymbolImage(def);

                    if (addInst)
                    {
                        AddInstance(inst, def);
                    }
                }

                if ((dk & DefinitionKind.Vex2D) != 0)
                {
                    Definition2D d2d = GetDefinition2D(m, def);

                    if (!isDefined.Contains(def.Id))
                    {
                        isDefined.Add(def.Id);
                        d2d.AddShapes(curVo, def, m);
                        //parentStack.Peek().Definition.AddShapes(curVo, def, m);
                    }

                    Instance2D i2d;
                    if (addInst)
                    {
                        i2d = AddInstance(inst, def);
                    }
                    else
                    {
                        i2d = CreateInstance2D(def);
                    }

                    parentStack.Push(i2d);
                    ParseTimeline((Timeline)def);
                    i2d.Definition.IsDefined = true;
                    parentStack.Pop();
                }
            }

            if (!isDefined.Contains(def.Id))
            {
                isDefined.Add(def.Id);
            }
        }
示例#9
0
        private void ParseTimeline(Timeline t)
        {
            List <IInstance> insts = t.Instances;

            foreach (IInstance instance in insts)
            {
                if (!(instance is Instance))
                {
                    continue;
                }
                Instance inst = (Instance)instance;
                if (inst.Name == null)
                {
                    inst.Name = "$inst_" + instAutoNumber++;
                }

                IDefinition    def = curVo.Definitions[inst.DefinitionId];
                DefinitionKind dk  = (DefinitionKind)def.UserData;

                if ((dk & DefinitionKind.Ignore) != 0)
                {
                    continue;
                }

                if ((dk & DefinitionKind.JointMarker) != 0)
                {
                    V2DJointKind jointKind = Enum.Parse(V2DJointKind, def.Name, true);
                    ParseJoint(jointKind, inst);
                }

                if ((dk & DefinitionKind.ShapeMarker) != 0)
                {
                }

                if ((dk & DefinitionKind.TextField) != 0)
                {
                    Text txt = (Text)def;
                }

                if ((dk & DefinitionKind.Timeline) != 0)
                {
                    V2DDefinition vd = CreateDefinition(inst, def);
                    V2DInstance   vi = CreateInstance(inst, vd);

                    parentStack.Push(vi);
                    ParseTimeline((Timeline)def);
                    parentStack.Pop();
                }

                if ((dk & DefinitionKind.Symbol) != 0)
                {
                    V2DInstance vi = CreateInstance(inst, def);
                }

                if ((dk & DefinitionKind.Vex2D) != 0)
                {
                    // todo: this is just adding images and/or instances
                    Body2D b2d = CreateDefinition(inst, def);
                    b2d.AddShapes(curVo, def, inst);
                }
            }
            GenerateJointData();
        }