Beispiel #1
0
        public TMXTileGroup(XMLElement element)
        {
            name = element.GetAttribute("name", null);
            width = element.GetIntAttribute("width", 0);
            height = element.GetIntAttribute("height", 0);
            objects = new List<TMXTile>();

            XMLElement propsElement = element.GetChildrenByName("properties");
            if (propsElement != null)
            {
                List<XMLElement> properties = propsElement.List("property");
                if (properties != null)
                {
                    props = new TMXProperty();
                    for (int p = 0; p < properties.Count; p++)
                    {
                        XMLElement propElement = properties[p];
                        string name_0 = propElement.GetAttribute("name", null);
                        string value_ren = propElement.GetAttribute("value", null);
                        props.SetProperty(name_0, value_ren);
                    }
                }
            }
            List<XMLElement> objectNodes = element.List("object");
            for (int i = 0; i < objectNodes.Count; i++)
            {
                XMLElement objElement = objectNodes[i];
                TMXTile obj0 = new TMXTile(objElement);
                obj0.index = i;
                CollectionUtils.Add(objects, obj0);
            }
        }
Beispiel #2
0
        public TMXTile(XMLElement element)
        {
            name = element.GetAttribute("name", "");
            type = element.GetAttribute("type", "");
            x = element.GetIntAttribute("x", 0);
            y = element.GetIntAttribute("y", 0);
            string w = element.GetAttribute("width", null);
            string h = element.GetAttribute("height", null);
            width = System.Int32.Parse((w == null || "".Equals(w)) ? "0" : w);
            height = System.Int32.Parse((h == null || "".Equals(h)) ? "0" : h);
            XMLElement imageElement = element
                    .GetChildrenByName("image");
            if (imageElement != null)
            {
                image = imageElement.GetAttribute("source", null);
            }
            XMLElement propsElement = element
                    .GetChildrenByName("properties");
            if (propsElement != null)
            {
                props = new TMXProperty();
                List<XMLElement> property = propsElement.List("property");
                for (int i = 0; i < property.Count; i++)
                {
                    XMLElement propElement = property[i];
                    string name_0 = propElement.GetAttribute("name", null);
                    string value_ren = propElement.GetAttribute("value", null);
                    props.SetProperty(name_0, value_ren);
                }

            }
        }
Beispiel #3
0
        public List <XMLElement> Find(string n)
        {
            List <XMLElement> v = new List <XMLElement>();

            for (IIterator e = Elements(); e.HasNext();)
            {
                object o = e.Next();
                if ((!(o  is  XMLElement)))
                {
                    continue;
                }
                XMLElement ele = (XMLElement)o;
                if (!ele.Equals(ele.GetName()))
                {
                    IIterator it = ele.Elements(n);
                    for (; it.HasNext();)
                    {
                        XMLElement child = (XMLElement)it.Next();
                        child.parent = ele;
                        CollectionUtils.Add(v, child);
                    }
                    continue;
                }
                else if (ele.Equals(ele.GetName()))
                {
                    CollectionUtils.Add(v, (XMLElement)o);
                    continue;
                }
            }
            return(v);
        }
Beispiel #4
0
 public void Start_ele(XMLElement ele)
 {
     for (IIterator it = ele.Elements(); it.HasNext();)
     {
         XMLElement e = (XMLElement)it.Next();
         Start_ele(e.GetName());
     }
 }
Beispiel #5
0
		private void PopElement(int idx, XMLListener l) {
			if (l != null) {
				l.EndElement(idx, this.topElement);
			}
			this.stack.Pop();
			if (stack.Count > 0) {
				this.topElement = (this.stack.Peek());
			} else {
				this.topElement = null;
			}
		}
Beispiel #6
0
		private void PushElement(XMLElement root, int idx, XMLListener l) {
			if (this.topElement == null) {
				this.rootElement = root;
			} else {
				this.topElement.AddContents(root);
			}
			this.stack.Push(root);
			this.topElement = root;
			if (l != null) {
				l.AddElement(idx, root);
			}
		}
Beispiel #7
0
 private void PopElement(int idx, XMLListener l)
 {
     if (l != null)
     {
         l.EndElement(idx, this.topElement);
     }
     this.stack.Pop();
     if (stack.Count > 0)
     {
         this.topElement = (this.stack.Peek());
     }
     else
     {
         this.topElement = null;
     }
 }
Beispiel #8
0
 private void PushElement(XMLElement root, int idx, XMLListener l)
 {
     if (this.topElement == null)
     {
         this.rootElement = root;
     }
     else
     {
         this.topElement.AddContents(root);
     }
     this.stack.Push(root);
     this.topElement = root;
     if (l != null)
     {
         l.AddElement(idx, root);
     }
 }
Beispiel #9
0
 public void Dispose()
 {
     if (stack != null)
     {
         stack.Clear();
         stack = null;
     }
     if (topElement != null)
     {
         topElement.Dispose();
         topElement = null;
     }
     if (rootElement != null)
     {
         rootElement.Dispose();
         rootElement = null;
     }
 }
Beispiel #10
0
        private static NSObject ParseObject(XMLElement n)
        {
			String type = n.GetName();
			if (type.Equals("dict",StringComparison.InvariantCultureIgnoreCase)) {
				NSDictionary dict = new NSDictionary();
				List<XMLElement> children = n.List();
				for (int i = 0; i < children.Count; i += 2) {
                    XMLElement key = (XMLElement)children[i + 0];
					XMLElement val = (XMLElement) children[i + 1];
					dict.Put(key.GetContents(), ParseObject(val));
				}
				return dict;
			} else if (type.Equals("array",StringComparison.InvariantCultureIgnoreCase)) {
				List<XMLElement> children = n.List();
				NSArray array = new NSArray(children.Count);
				for (int i = 0; i < children.Count; i++) {
					array.SetValue(i, ParseObject((XMLElement) children[i]));
				}
				return array;
			} else if (type.Equals("true",StringComparison.InvariantCultureIgnoreCase)
					|| type.Equals("yes",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSNumber(true);
			} else if (type.Equals("false",StringComparison.InvariantCultureIgnoreCase)
					|| type.Equals("no",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSNumber(false);
			} else if (type.Equals("integer",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSNumber(n.GetContents());
			} else if (type.Equals("real",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSNumber(n.GetContents());
			} else if (type.Equals("string",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSString(n.GetContents());
			} else if (type.Equals("data",StringComparison.InvariantCultureIgnoreCase)) {
				return new NSData(n.GetContents());
			} else if (type.Equals("range",StringComparison.InvariantCultureIgnoreCase)) {
				List<XMLElement> children = n.List();
				if (children.Count == 2) {
					XMLElement key = (XMLElement) children[0];
					XMLElement val = (XMLElement) children[1];
					return new NSRange(Int32.Parse(key.GetContents()),
							Int32.Parse(val.GetContents()));
				}
			}
			return null;
		}
Beispiel #11
0
        private void NewElement(string context, XMLListener l, int index)
        {
            string o = "";
            int    i;
            string str1;

            if (context.EndsWith("/>"))
            {
                i    = 2;
                str1 = context.Substring(1, (context.Length - 2) - (1));
            }
            else if (context.StartsWith("</"))
            {
                i    = 1;
                str1 = context.Substring(2, (context.Length - 1) - (2));
            }
            else
            {
                i    = 0;
                str1 = context.Substring(1, (context.Length - 1) - (1));
            }
            try {
                if (str1.IndexOf(' ') < 0)
                {
                    o = str1;
                    switch (i)
                    {
                    case OPEN_TAG:
                        PushElement(new XMLElement(o), index, l);
                        break;

                    case CLOSE_TAG:
                        if (this.topElement.GetName().Equals(o))
                        {
                            PopElement(index, l);
                        }
                        else
                        {
                            throw new Exception("Expected close of '"
                                                + this.topElement.GetName() + "' instead of "
                                                + context);
                        }
                        break;

                    case OPEN_CLOSE_TAG:
                        PushElement(new XMLElement(o), index, l);
                        PopElement(index, l);
                        break;
                    }
                }
                else
                {
                    XMLElement el = null;
                    o = str1.Substring(0, (str1.IndexOf(' ')) - (0));
                    switch (i)
                    {
                    case OPEN_TAG:
                        el = new XMLElement(o);
                        PushElement(el, index, l);
                        break;

                    case CLOSE_TAG:
                        throw new Exception("Syntax Error: " + context);

                    case OPEN_CLOSE_TAG:
                        el = new XMLElement(o);
                        PushElement(el, index, l);
                        PopElement(index, l);
                        break;
                    }
                    string str2  = str1.Substring(str1.IndexOf(' ') + 1);
                    int    start = 0;
                    int    end   = 0;

                    StringBuilder sbr1 = new StringBuilder(128);
                    StringBuilder sbr2 = new StringBuilder(32);
                    for (int m = 0; m < str2.Length; m++)
                    {
                        switch ((int)str2[m])
                        {
                        case '"':

                            start = (start != 0) ? 0 : 1;
                            break;

                        case ' ':
                            if ((end == 1) && (start == 1))
                            {
                                sbr1.Append(str2[m]);
                            }
                            else if (sbr2.Length > 0)
                            {
                                string key       = sbr2.ToString();
                                string value_ren = sbr1.ToString();
                                if (key.Length > 0)
                                {
                                    XMLAttribute a = el.AddAttribute(key, value_ren);
                                    a.element = el;
                                    if (l != null)
                                    {
                                        l.AddAttribute(index, a);
                                    }
                                }
                                end = 0;
                                sbr1.Remove(0, sbr1.Length - (0));
                                sbr2.Remove(0, sbr2.Length - (0));
                            }
                            break;

                        case '=':

                            if (start == 0)
                            {
                                end = 1;
                            }
                            break;

                        case '!':
                        case '#':
                        case '$':
                        case '%':
                        case '&':
                        case '\'':
                        case '(':
                        case ')':
                        case '*':
                        case '+':
                        case ',':
                        case '-':
                        case '.':
                        case '/':
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                        case ':':
                        case ';':
                        case '<':
                        default:
                            if (end != 0)
                            {
                                sbr1.Append(str2[m]);
                            }
                            else
                            {
                                sbr2.Append(str2[m]);
                            }
                            break;
                        }
                    }
                    if (sbr1.Length > 0)
                    {
                        string       key_0   = sbr2.ToString();
                        string       value_1 = sbr1.ToString();
                        XMLAttribute a_2     = el.AddAttribute(key_0, value_1);
                        a_2.element = el;
                        if (l != null)
                        {
                            l.AddAttribute(index, a_2);
                        }
                    }
                }
            } catch (Exception e) {
                throw new Exception("Cannot parse element '" + context
                                    + "' - (" + e + ")");
            }
        }
 public virtual void EndElement(int line, XMLElement e)
 {
     _read.StopElement(e != null ? e.GetName() : _read._classType);
 }
Beispiel #13
0
        public TMXTileSet(TMXTiledMap map, XMLElement element, bool loadImage)
        {
            this.map = map;
            this.name = element.GetAttribute("name", null);
            this.firstGID = element.GetIntAttribute("firstgid", 0);
            string source = element.GetAttribute("source", "");
            if (!"".Equals(source))
            {
                try
                {
                    Stream ins0 = Resources.OpenStream(map.GetTilesLocation()
                            + "/" + source);
                    XMLDocument doc = XMLParser.Parse(ins0);
                    XMLElement docElement = doc.GetRoot();
                    element = docElement;
                }
                catch (Exception e)
                {
                    Loon.Utils.Debug.Log.Exception(e);
                    throw new Exception(this.map.tilesLocation + "/"
                            + source);
                }
            }
            string tileWidthString = element.GetAttribute("tilewidth", "");
            string tileHeightString = element.GetAttribute("tileheight", "");
            if (tileWidthString.Length == 0 || tileHeightString.Length == 0)
            {
                throw new Exception(
                        "tileWidthString.length == 0 || tileHeightString.length == 0");
            }
            tileWidth = Int32.Parse(tileWidthString);
            tileHeight = Int32.Parse(tileHeightString);

            string sv = element.GetAttribute("spacing", "");
            if ((sv != null) && (!"".Equals(sv)))
            {
                tileSpacing = Int32.Parse(sv);
            }

            string mv = element.GetAttribute("margin", "");
            if ((mv != null) && (!"".Equals(mv)))
            {
                tileMargin = Int32.Parse(mv);
            }

            List<XMLElement> list = element.List("image");
            XMLElement imageNode = list[0];
            string fileName = imageNode.GetAttribute("source", null);

            LColor trans = null;
            string t = imageNode.GetAttribute("trans", null);
            if ((t != null) && (t.Length > 0))
            {
                trans = new LColor(((uint)Convert.ToInt32(t, 16)));
            }

            if (loadImage)
            {
                string path = map.GetTilesLocation() + "/" + fileName;
                LTexture image;
                if (trans != null)
                {
                    image = TextureUtils.FilterColor(path, trans);
                }
                else
                {
                    image = LTextures.LoadTexture(path);
                }
                SetTileSetImage(image);
            }

            List<XMLElement> elements = element.List("tile");
            for (int i = 0; i < elements.Count; i++)
            {
                XMLElement tileElement = elements[i];

                int id = tileElement.GetIntAttribute("id", 0);
                id += firstGID;
                TMXProperty tileProps = new TMXProperty();

                XMLElement propsElement = tileElement
                        .GetChildrenByName("properties");
                List<XMLElement> properties = propsElement.List("property");
                for (int p = 0; p < properties.Count; p++)
                {
                    XMLElement propElement = properties[p];
                    string name_1 = propElement.GetAttribute("name", null);
                    string value_ren = propElement.GetAttribute("value", null);
                    tileProps.SetProperty(name_1, value_ren);
                }
                CollectionUtils.Put(props, id, tileProps);
            }
        }
Beispiel #14
0
		public LTexturePack(XMLElement p) {
			Set(p);
		}
Beispiel #15
0
 public void Dispose()
 {
     if (stack != null)
     {
         stack.Clear();
         stack = null;
     }
     if (topElement != null)
     {
         topElement.Dispose();
         topElement = null;
     }
     if (rootElement != null)
     {
         rootElement.Dispose();
         rootElement = null;
     }
 }
Beispiel #16
0
        private void NewElement(string context, XMLListener l, int index)
        {
            string o = "";
            int i;
            string str1;
            if (context.EndsWith("/>")) {
                i = 2;
                str1 = context.Substring(1,(context.Length - 2)-(1));
            } else if (context.StartsWith("</")) {
                i = 1;
                str1 = context.Substring(2,(context.Length - 1)-(2));
            } else {
                i = 0;
                str1 = context.Substring(1,(context.Length - 1)-(1));
            }
            try {
                if (str1.IndexOf(' ') < 0) {
                    o = str1;
                    switch (i) {
                    case OPEN_TAG:
                        PushElement(new XMLElement(o), index, l);
                        break;
                    case CLOSE_TAG:
                        if (this.topElement.GetName().Equals(o)) {
                            PopElement(index, l);
                        } else {
                            throw new Exception("Expected close of '"
                                    + this.topElement.GetName() + "' instead of "
                                    + context);
                        }
                        break;
                    case OPEN_CLOSE_TAG:
                        PushElement(new XMLElement(o), index, l);
                        PopElement(index, l);
                        break;
                    }
                } else {
                    XMLElement el = null;
                    o = str1.Substring(0,(str1.IndexOf(' '))-(0));
                    switch (i) {
                    case OPEN_TAG:
                        el = new XMLElement(o);
                        PushElement(el, index, l);
                        break;
                    case CLOSE_TAG:
                        throw new Exception("Syntax Error: " + context);
                    case OPEN_CLOSE_TAG:
                        el = new XMLElement(o);
                        PushElement(el, index, l);
                        PopElement(index, l);
                        break;
                    }
                    string str2 = str1.Substring(str1.IndexOf(' ') + 1);
                    int start = 0;
                    int end = 0;

                    StringBuilder sbr1 = new StringBuilder(128);
                    StringBuilder sbr2 = new StringBuilder(32);
                    for (int m = 0; m < str2.Length; m++) {

                        switch ((int) str2[m]) {
                        case '"':

                            start = (start != 0) ? 0 : 1;
                            break;
                        case ' ':
                            if ((end == 1) && (start == 1)) {
                                sbr1.Append(str2[m]);
                            } else if (sbr2.Length > 0) {
                                string key = sbr2.ToString();
                                string value_ren = sbr1.ToString();
                                if (key.Length > 0) {
                                    XMLAttribute a = el.AddAttribute(key, value_ren);
                                    a.element = el;
                                    if (l != null) {
                                        l.AddAttribute(index, a);
                                    }
                                }
                                end = 0;
                                sbr1.Remove(0,sbr1.Length-(0));
                                sbr2.Remove(0,sbr2.Length-(0));
                            }
                            break;
                        case '=':

                            if (start == 0) {
                                end = 1;
                            }
                            break;
                        case '!':
                        case '#':
                        case '$':
                        case '%':
                        case '&':
                        case '\'':
                        case '(':
                        case ')':
                        case '*':
                        case '+':
                        case ',':
                        case '-':
                        case '.':
                        case '/':
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                        case ':':
                        case ';':
                        case '<':
                        default:
                            if (end != 0) {
                                sbr1.Append(str2[m]);
                            } else {
                                sbr2.Append(str2[m]);
                            }
                            break;
                        }

                    }
                    if (sbr1.Length > 0) {
                        string key_0 = sbr2.ToString();
                        string value_1 = sbr1.ToString();
                        XMLAttribute a_2 = el.AddAttribute(key_0, value_1);
                        a_2.element = el;
                        if (l != null) {
                            l.AddAttribute(index, a_2);
                        }
                    }
                }
            } catch (Exception e) {
                throw new Exception("Cannot parse element '" + context
                        + "' - (" + e + ")");
            }
        }
 public LTexturePack(XMLElement pack)
 {
     Set(pack);
 }
Beispiel #18
0
		public void Start_ele(XMLElement ele) {
			for (IIterator it = ele.Elements(); it.HasNext();) {
				XMLElement e = (XMLElement) it.Next();
				Start_ele(e.GetName());
			}
		}
Beispiel #19
0
		private void Set(XMLElement Pack) {
			this.fileName = Pack.GetAttribute("file", null);
			this.name = Pack.GetAttribute("name", fileName);
			int r = Pack.GetIntAttribute("r", -1);
			int g = Pack.GetIntAttribute("g", -1);
			int b = Pack.GetIntAttribute("b", -1);
			int a = Pack.GetIntAttribute("a", -1);
			if (r != -1 && g != -1 && b != -1 && a != -1) {
				colorMask = new LColor(r, g, b, a);
			}
			if (fileName != null) {
				List<XMLElement> blocks = Pack.List("block");
				foreach (XMLElement e  in  blocks) {
					PackEntry entry = new PackEntry(null);
					int id = e.GetIntAttribute("id", count);
					entry.id = id;
					entry.fileName = e.GetAttribute("name", null);
					entry.bounds.left = e.GetIntAttribute("left", 0);
					entry.bounds.top = e.GetIntAttribute("top", 0);
					entry.bounds.right = e.GetIntAttribute("right", 0);
					entry.bounds.bottom = e.GetIntAttribute("bottom", 0);
					if (entry.fileName != null) {
						temps.Put(entry.fileName, entry);
					} else {
						temps.Put(Convert.ToString(id), entry);
					}
					count++;
				}
				this.packing = false;
				this.packed = true;
			}
			this.useAlpha = true;
		}
Beispiel #20
0
        /// <summary>
        /// 根据TMX地图描述创建一个新层
        /// </summary>
        ///
        /// <param name="map"></param>
        /// <param name="element"></param>
        /// <exception cref="System.Exception"></exception>
        public TMXLayer(TMXTiledMap map, XMLElement element)
        {

            this.tmx = map;
            this.name = element.GetAttribute("name", "");
            this.width = element.GetIntAttribute("width", 0);
            this.height = element.GetIntAttribute("height", 0);
            this.data = new int[width, height, 3];
            this.MaxLightSize(width, height);

            // 获得当前图层属性
            XMLElement propsElement = element.GetChildrenByName("properties");

            if (propsElement != null)
            {
                props = new TMXProperty();
                List<XMLElement> properties = propsElement.List("property");
                for (int i = 0; i < properties.Count; i++)
                {
                    XMLElement propElement = properties[i];
                    string name_0 = propElement.GetAttribute("name", null);
                    string value_ren = propElement.GetAttribute("value", null);
                    props.SetProperty(name_0, value_ren);
                }
            }

            XMLElement dataNode = element.GetChildrenByName("data");
            string encoding = dataNode.GetAttribute("encoding", null);
            string compression = dataNode.GetAttribute("compression", null);

            // 进行base64的压缩解码
            if ("base64".Equals(encoding) && "gzip".Equals(compression))
            {
                try
                {

                    byte[] sdec = Base64Coder.DecodeBase64(dataNode.GetContents().Trim().ToCharArray());

                    ByteArrayInputStream mask0 = new ByteArrayInputStream(sdec);

                    GZipInputStream dis = new GZipInputStream(mask0);

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            int tileId = 0;

                            tileId |= dis.ReadByte();
                            tileId |= dis.ReadByte() << 8;
                            tileId |= dis.ReadByte() << 16;
                            tileId |= dis.ReadByte() << 24;

                            if (tileId == 0)
                            {
                                data[x,y,0] = -1;
                                data[x,y,1] = 0;
                                data[x,y,2] = 0;
                            }
                            else
                            {

                                TMXTileSet set = map.FindTileSet(tileId);

                                if (set != null)
                                {
                               
                                    data[x,y,0] = set.index;
                                    data[x,y,1] = tileId - set.firstGID;
                                }
                                data[x,y,2] = tileId;
                            }

                        }
                    }
                }
                catch (Exception e)
                {
                    Loon.Utils.Debugging.Log.Exception(e);
                    throw new Exception("Unable to decode base64 !");
                }
            }
            else
            {
                throw new Exception("Unsupport tiled map type " + encoding
                        + "," + compression + " only gzip base64 Support !");
            }
        }