Beispiel #1
0
        public JsonFace GetJsonFace()
        {
            JsonFace face = new JsonFace();

            face.CullFace    = CullFace;
            face.Orientation = _orientation;
            face.Rotation    = _rotation;
            face.TextureName = TextureName;
            face.TintIndex   = TintIndex;
            face.UV          = _uV;
            face.Visible     = _visible;
            return(face);
        }
Beispiel #2
0
        public CubeFace(JsonFace face, Vector3 from, Vector3 to, Box parentBox)
        {
            _orientation = face.Orientation;
            _textureName = face.TextureName;
            _visible     = face.Visible;
            CullFace     = face.CullFace;
            _rotation    = face.Rotation;
            TintIndex    = face.TintIndex;
            _uV          = face.UV;
            _parentBox   = parentBox;
            _treeNode    = new TreeNodeFace(this);

            SetVertices(from, to);
            SetIndices();

            SetVertexBuffer(_vertices);
            SetIndexBuffer(_indices);
        }
Beispiel #3
0
        public JsonElement GetElement()
        {
            JsonElement element = new JsonElement();

            element.From     = _from;
            element.To       = _to;
            element.Shade    = _shade;
            element.Rotation = new ElementRotation(Rotation);
            element.Name     = _name;
            List <JsonFace> faces = new List <JsonFace>();

            foreach (CubeFace cF in _faces)
            {
                JsonFace face = cF.GetJsonFace();
                if (face != null)
                {
                    faces.Add(face);
                }
            }
            element.Faces = faces;
            return(element);
        }
Beispiel #4
0
        private void AddFaces(JsonElement element)
        {
            AddTabs(4);
            _fileText += "\"faces\": {\n";
            bool addComma = false;

            for (int i = 0; i < element.Faces.Count; i++)
            {
                JsonFace face = element.Faces[i];
                if (face.Visible)
                {
                    if (addComma)
                    {
                        _fileText += ", ";
                        _fileText += "\n";
                        addComma   = false;
                    }
                    AddTabs(5);
                    _fileText += "\"" + face.Orientation.ToString().ToLower() + "\": { ";
                    AddVector("uv", face.UV);
                    _fileText += ", \"texture\": " + "\"" + face.TextureName + "\"";
                    if (face.Rotation != 0)
                    {
                        _fileText += ", \"rotation\": " + face.Rotation.ToString();
                    }
                    if (face.TintIndex >= 0)
                    {
                        _fileText += ", \"tintindex\": " + face.TintIndex.ToString();
                    }
                    if (face.CullFace != CullFace.none)
                    {
                        _fileText += ", \"cullface\": " + "\"" + face.CullFace.ToString().ToLower() + "\"";
                    }
                    _fileText += " }";
                    addComma   = true;
                }
            }
            _fileText += "\n              }\n";
        }
Beispiel #5
0
        private int ParseElements()
        {
            int    dataEnd = _fileText.LastIndexOf("]");
            string value   = _fileText.Substring(_readIndex, dataEnd - _readIndex);

            JsonElement element = new JsonElement();
            bool        done    = false;

            while (!done)
            {
                int    nameIndex  = value.IndexOf("\"") + 1;
                int    nameLenght = value.IndexOf("\"", nameIndex) - nameIndex;
                string name       = value.Substring(nameIndex, nameLenght);
                value = value.Substring(value.IndexOf(":") + 1);

                if (name == "__comment")
                {
                    int    commentIndex  = value.IndexOf("\"") + 1;
                    int    commentLenght = value.IndexOf("\"", commentIndex) - commentIndex;
                    string comment       = value.Substring(commentIndex, commentLenght);

                    element.Name = comment;
                    value        = value.Substring(commentIndex + commentLenght + 1);
                }
                if (name == "from")
                {
                    value = value.Substring(value.IndexOf("[") + 1);
                    double x = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf(",")));

                    value = value.Substring(value.IndexOf(",") + 1);
                    double y = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf(",")));

                    value = value.Substring(value.IndexOf(",") + 1);
                    double z = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf("]")));

                    element.From = new Vector3((float)x, (float)y, (float)z);

                    value = value.Substring(value.IndexOf("]") + 1);
                }
                if (name == "to")
                {
                    value = value.Substring(value.IndexOf("[") + 1);
                    double x = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf(",")));

                    value = value.Substring(value.IndexOf(",") + 1);
                    double y = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf(",")));

                    value = value.Substring(value.IndexOf(",") + 1);
                    double z = System.Xml.XmlConvert.ToDouble(value.Substring(0, value.IndexOf("]")));

                    element.To = new Vector3((float)x, (float)y, (float)z);

                    value = value.Substring(value.IndexOf("]") + 1);
                }

                if (name == "shade")
                {
                    int boolEnd;
                    int nextComma      = value.IndexOf(",");
                    int nextCloseBrack = value.IndexOf("}");
                    if (nextComma < 0 || nextComma > nextCloseBrack)
                    {
                        boolEnd = nextCloseBrack;
                    }
                    else
                    {
                        boolEnd = nextComma;
                    }

                    string boolString = value.Substring(0, boolEnd);

                    if (value.Contains("false"))
                    {
                        element.Shade = false;
                    }
                    else if (value.Contains("true"))
                    {
                        element.Shade = true;
                    }

                    value = value.Substring(boolEnd + 1);
                }
                #region Faces

                if (name == "faces")
                {
                    /////todo
                    bool facesDone = false;
                    while (!facesDone)
                    {
                        JsonFace face = new JsonFace();
                        face.Visible = true;
                        int    faceNameIndex  = value.IndexOf("\"") + 1;
                        int    faceNameLenght = value.IndexOf("\"", faceNameIndex) - faceNameIndex;
                        string faceName       = value.Substring(faceNameIndex, faceNameLenght);
                        value = value.Substring(value.IndexOf(":") + 1);

                        if (faceName == "north")
                        {
                            face.Orientation = FaceOrientation.north;
                        }
                        if (faceName == "south")
                        {
                            face.Orientation = FaceOrientation.south;
                        }
                        if (faceName == "up")
                        {
                            face.Orientation = FaceOrientation.up;
                        }
                        if (faceName == "down")
                        {
                            face.Orientation = FaceOrientation.down;
                        }
                        if (faceName == "east")
                        {
                            face.Orientation = FaceOrientation.east;
                        }
                        if (faceName == "west")
                        {
                            face.Orientation = FaceOrientation.west;
                        }


                        bool   parasDone = false;
                        string faceData  = value.Substring(0, value.IndexOf("}"));
                        while (!parasDone)
                        {
                            int    parNameIndex  = faceData.IndexOf("\"") + 1;
                            int    parNameLenght = faceData.IndexOf("\"", parNameIndex) - parNameIndex;
                            string parName       = faceData.Substring(parNameIndex, parNameLenght);
                            faceData = faceData.Substring(faceData.IndexOf(":") + 1);
                            int nextComma = faceData.IndexOf(",");
                            if (parName == "uv")
                            {
                                faceData = faceData.Substring(faceData.IndexOf("[") + 1);
                                double x1 = System.Xml.XmlConvert.ToDouble(faceData.Substring(0, faceData.IndexOf(",")));

                                faceData = faceData.Substring(faceData.IndexOf(",") + 1);
                                double y1 = System.Xml.XmlConvert.ToDouble(faceData.Substring(0, faceData.IndexOf(",")));

                                faceData = faceData.Substring(faceData.IndexOf(",") + 1);
                                double x2 = System.Xml.XmlConvert.ToDouble(faceData.Substring(0, faceData.IndexOf(",")));

                                faceData = faceData.Substring(faceData.IndexOf(",") + 1);
                                double y2 = System.Xml.XmlConvert.ToDouble(faceData.Substring(0, faceData.IndexOf("]")));

                                face.UV = new Vector4((float)x1, (float)y1, (float)x2, (float)y2);
                            }
                            if (parName == "texture")
                            {
                                int    texNameIndex  = faceData.IndexOf("\"") + 1;
                                int    texNameLenght = faceData.IndexOf("\"", texNameIndex) - texNameIndex;
                                string texName       = faceData.Substring(texNameIndex, texNameLenght);
                                face.TextureName = texName;
                            }
                            if (parName == "rotation")
                            {
                                if (nextComma < 0)
                                {
                                    face.Rotation = Convert.ToInt32(faceData);
                                }
                                else
                                {
                                    face.Rotation = Convert.ToInt32(faceData.Substring(0, nextComma));
                                }
                            }
                            if (parName == "tintindex")
                            {
                                if (nextComma < 0)
                                {
                                    face.TintIndex = Convert.ToInt32(faceData);
                                }
                                else
                                {
                                    face.TintIndex = Convert.ToInt32(faceData.Substring(0, nextComma));
                                }
                            }
                            if (parName == "cullface")
                            {
                                int    cullFaceIndex  = faceData.IndexOf("\"") + 1;
                                int    cullFaceLenght = faceData.IndexOf("\"", cullFaceIndex) - cullFaceIndex;
                                string cullFaceName   = faceData.Substring(cullFaceIndex, cullFaceLenght);

                                if (cullFaceName == "down")
                                {
                                    face.CullFace = CullFace.down;
                                }
                                if (cullFaceName == "up")
                                {
                                    face.CullFace = CullFace.up;
                                }
                                if (cullFaceName == "north")
                                {
                                    face.CullFace = CullFace.north;
                                }
                                if (cullFaceName == "south")
                                {
                                    face.CullFace = CullFace.south;
                                }
                                if (cullFaceName == "west")
                                {
                                    face.CullFace = CullFace.west;
                                }
                                if (cullFaceName == "east")
                                {
                                    face.CullFace = CullFace.east;
                                }
                            }

                            nextComma = faceData.IndexOf(",");
                            if (nextComma < 0)
                            {
                                parasDone = true;
                            }
                            else
                            {
                                faceData = faceData.Substring(nextComma + 1);
                            }
                        }
                        element.Faces.Add(face);
                        value = value.Substring(value.IndexOf("}") + 1);

                        int commaindex      = value.IndexOf(",");
                        int closeBrackindex = value.IndexOf("}");
                        if (commaindex < 0 || commaindex > closeBrackindex)
                        {
                            value     = value.Substring(value.IndexOf("}") + 1);
                            facesDone = true;
                        }
                    }
                }
                #endregion Faces
                #region Rotation
                if (name == "rotation")
                {
                    ElementRotation rotation = new ElementRotation();
                    string          rotData  = value.Substring(0, value.IndexOf("}"));

                    bool parasDone = false;
                    while (!parasDone)
                    {
                        int    parameterNameIndex  = rotData.IndexOf("\"") + 1;
                        int    parameterNameLenght = rotData.IndexOf("\"", parameterNameIndex) - parameterNameIndex;
                        string parameterName       = rotData.Substring(parameterNameIndex, parameterNameLenght);
                        rotData = rotData.Substring(rotData.IndexOf(":") + 1);
                        int nextComma = rotData.IndexOf(",");

                        if (parameterName == "origin")
                        {
                            rotData = rotData.Substring(rotData.IndexOf("[") + 1);
                            double x = System.Xml.XmlConvert.ToDouble(rotData.Substring(0, rotData.IndexOf(",")));

                            rotData = rotData.Substring(rotData.IndexOf(",") + 1);
                            double y = System.Xml.XmlConvert.ToDouble(rotData.Substring(0, rotData.IndexOf(",")));

                            rotData = rotData.Substring(rotData.IndexOf(",") + 1);
                            double z = System.Xml.XmlConvert.ToDouble(rotData.Substring(0, rotData.IndexOf("]")));

                            rotation.Origin = new Vector3((float)x, (float)y, (float)z);
                        }
                        if (parameterName == "axis")
                        {
                            int    axIndex  = rotData.IndexOf("\"") + 1;
                            int    axLenght = rotData.IndexOf("\"", axIndex) - axIndex;
                            string axName   = rotData.Substring(axIndex, axLenght);

                            if (axName == "x")
                            {
                                rotation.Axis = Axis.x;
                            }
                            if (axName == "y")
                            {
                                rotation.Axis = Axis.y;
                            }
                            if (axName == "z")
                            {
                                rotation.Axis = Axis.z;
                            }
                        }
                        if (parameterName == "angle")
                        {
                            if (nextComma < 0)
                            {
                                rotation.Angle = (float)System.Xml.XmlConvert.ToDouble(rotData);
                            }
                            else
                            {
                                rotation.Angle = (float)System.Xml.XmlConvert.ToDouble(rotData.Substring(0, nextComma));
                            }
                        }
                        if (parameterName == "rescale")
                        {
                            string boolScale;
                            if (nextComma >= 0)
                            {
                                boolScale = rotData.Substring(0, nextComma);
                            }
                            else
                            {
                                boolScale = rotData;
                            }

                            if (boolScale.Contains("true"))
                            {
                                rotation.Rescale = true;
                            }
                            if (boolScale.Contains("false"))
                            {
                                rotation.Rescale = false;
                            }
                        }
                        nextComma = rotData.IndexOf(",");
                        if (nextComma < 0)
                        {
                            parasDone = true;
                        }
                        else
                        {
                            rotData = rotData.Substring(nextComma + 1);
                        }
                    }
                    element.Rotation = rotation;
                    value            = value.Substring(value.IndexOf("}") + 1);
                }
                #endregion Rotation

                int commaIndex      = value.IndexOf(",");
                int closeBrackIndex = value.IndexOf("}");
                if (commaIndex > closeBrackIndex)
                {
                    _jsonModel.Elements.Add(element);
                    element = new JsonElement();
                }
                if (commaIndex < 0)
                {
                    _jsonModel.Elements.Add(element);
                    done = true;
                }
            }
            _readIndex = dataEnd + 1;
            return(1);
        }