Package tiled.core

Examples of tiled.core.TileLayer


   */
  private void translateLayer(MapLayer layer) {
    if (!(layer instanceof TileLayer)) {
      return;
    }
    TileLayer tileLayer = (TileLayer) layer;
    for (int y = 0; y < tileLayer.getHeight(); y++) {
      for (int x = 0; x < tileLayer.getWidth(); x++) {
        Tile tile = tileLayer.getTileAt(x, y);
        if (tile != null) {
          tile = translateTile(tile);
          tileLayer.setTileAt(x, y, tile);
        }
      }
    }
  }
View Full Code Here


     */
    private MapLayer readLayer(Node t) throws Exception {
        final int layerWidth = getAttribute(t, "width", map.getWidth());
        final int layerHeight = getAttribute(t, "height", map.getHeight());

        TileLayer ml = new TileLayer(layerWidth, layerHeight);

        final int offsetX = getAttribute(t, "x", 0);
        final int offsetY = getAttribute(t, "y", 0);
        final int visible = getAttribute(t, "visible", 1);
        String opacity = getAttributeValue(t, "opacity");

        ml.setName(getAttributeValue(t, "name"));

        if (opacity != null) {
            ml.setOpacity(Float.parseFloat(opacity));
        }

        readProperties(t.getChildNodes(), ml.getProperties());

        for (Node child = t.getFirstChild(); child != null;
                child = child.getNextSibling())
        {
            String nodeName = child.getNodeName();
            if ("data".equalsIgnoreCase(nodeName)) {
                String encoding = getAttributeValue(child, "encoding");
                String comp = getAttributeValue(child, "compression");

                if ("base64".equalsIgnoreCase(encoding)) {
                    Node cdata = child.getFirstChild();
                    if (cdata != null) {
                        char[] enc = cdata.getNodeValue().trim().toCharArray();
                        byte[] dec = Base64.decode(enc);
                        ByteArrayInputStream bais = new ByteArrayInputStream(dec);
                        InputStream is;

                        if ("gzip".equalsIgnoreCase(comp)) {
                            final int len = layerWidth * layerHeight * 4;
                            is = new GZIPInputStream(bais, len);
                        } else if ("zlib".equalsIgnoreCase(comp)) {
                            is = new InflaterInputStream(bais);
                        } else if (comp != null && !comp.isEmpty()) {
                            throw new IOException("Unrecognized compression method \"" + comp + "\" for map layer " + ml.getName());
                        } else {
                            is = bais;
                        }

                        for (int y = 0; y < ml.getHeight(); y++) {
                            for (int x = 0; x < ml.getWidth(); x++) {
                                int tileId = 0;
                                tileId |= is.read();
                                tileId |= is.read() <<  8;
                                tileId |= is.read() << 16;
                                tileId |= is.read() << 24;

                                setTileAtFromTileId(ml, y, x, tileId);
                            }
                        }
                    }
                } else if ("csv".equalsIgnoreCase(encoding)) {
                    String csvText = child.getTextContent();
                   
                    if (comp != null && !comp.isEmpty()) {
                        throw new IOException("Unrecognized compression method \"" + comp + "\" for map layer " + ml.getName() + " and encoding " + encoding);
                    }
                   
                    String[] csvTileIds = csvText
                            .trim()    // trim 'space', 'tab', 'newline'. pay attention to additional unicode chars like \u2028, \u2029, \u0085 if necessary
                            .split("[\\s]*,[\\s]*");
                   
                    if (csvTileIds.length != ml.getHeight() * ml.getWidth()) {
                        throw new IOException("Number of tiles does not match the layer's width and height");
                    }
                   
                    for (int y = 0; y < ml.getHeight(); y++) {
                        for (int x = 0; x < ml.getWidth(); x++) {
                            String sTileId = csvTileIds[x + y * ml.getHeight()];
                            int tileId = Integer.parseInt(sTileId);
                           
                            setTileAtFromTileId(ml, y, x, tileId);
                        }
                    }
                } else {
                    int x = 0, y = 0;
                    for (Node dataChild = child.getFirstChild();
                         dataChild != null;
                         dataChild = dataChild.getNextSibling())
                    {
                        if ("tile".equalsIgnoreCase(dataChild.getNodeName())) {
                            int tileId = getAttribute(dataChild, "gid", -1);
                            setTileAtFromTileId(ml, y, x, tileId);

                            x++;
                            if (x == ml.getWidth()) {
                                x = 0; y++;
                            }
                            if (y == ml.getHeight()) { break; }
                        }
                    }
                }
            } else if ("tileproperties".equalsIgnoreCase(nodeName)) {
                for (Node tpn = child.getFirstChild();
                     tpn != null;
                     tpn = tpn.getNextSibling())
                {
                    if ("tile".equalsIgnoreCase(tpn.getNodeName())) {
                        int x = getAttribute(tpn, "x", -1);
                        int y = getAttribute(tpn, "y", -1);

                        Properties tip = new Properties();

                        readProperties(tpn.getChildNodes(), tip);
                        ml.setTileInstancePropertiesAt(x, y, tip);
                    }
                }
            }
        }

        // This is done at the end, otherwise the offset is applied during
        // the loading of the tiles.
        ml.setOffset(offsetX, offsetY);

        // Invisible layers are automatically locked, so it is important to
        // set the layer to potentially invisible _after_ the layer data is
        // loaded.
        // todo: Shouldn't this be just a user interface feature, rather than
        // todo: something to keep in mind at this level?
        ml.setVisible(visible == 1);

        return ml;
    }
View Full Code Here

        if (ret == JOptionPane.YES_OPTION) {
            TileMergeHelper tmh = new TileMergeHelper(map);
            int len = map.getTotalLayers();
            //TODO: Add a dialog option: "Yes, visible only"
            TileLayer newLayer = tmh.merge(0, len, true);
            map.removeAllLayers();
            map.addLayer(newLayer);
            newLayer.setName("Merged Layer");
            map.addTileset(tmh.getSet());
            editor.setCurrentLayer(0);
        }
        else if (ret == JOptionPane.NO_OPTION) {
            while (map.getTotalLayers() > 1) {
View Full Code Here

        super.doPaint(x, y);

        // FIXME: This loop does not take all edges into account

        for (int layer = 0; layer < numLayers; layer++) {
            TileLayer tl = (TileLayer) affectedMp.getLayer(initLayer + layer);
            if (tl != null) {
                for (int i = 0; i <= shapeBounds.height + 1; i++) {
                    for (int j = 0; j <= shapeBounds.width + 1; j++) {
                        if (shape.contains(j, i)) {
                            tl.setTileAt(j + centerx, i + centery, paintTile);
                        }
                    }
                }
            }
        }
View Full Code Here

        Rectangle shapeBounds = shape.getBounds();
        int centerx = x - shapeBounds.width / 2;
        int centery = y - shapeBounds.height / 2;

        for (int i = 0; i < numLayers; i++) {
            TileLayer tl = (TileLayer) affectedMp.getLayer(initLayer - i);
            if (tl != null) {
                for (int cy = 0; cy <= shapeBounds.height; cy++) {
                    for (int cx = 0; cx < shapeBounds.width; cx++) {
                        if (shape.contains(cx, cy) &&
                                mt.genrand() % 101 <= 100 * ratio)
                        {
                            tl.setTileAt(
                                    cx + centerx, cy + centery, paintTile);
                        }
                    }
                }
            }
View Full Code Here

        myTs.setName("Merged Set");
    }

    public TileLayer merge(int start, int len, boolean all) {
      Rectangle r = myMap.getBounds();
        mergedLayer = new TileLayer(r);

        for (int i = 0; i < r.height; i++) {
            for (int j = 0; j < r.width; j++) {
                mergedLayer.setTileAt(j, i, createCell(j, i, start, len, all));
            }
View Full Code Here

        public Cell(Map map, int posx, int posy, int start, int len, boolean all) {
            sandwich = new Vector();
            for (int i = 0; i < len; i++) {
                MapLayer ml = map.getLayer(start+i);
                if (ml instanceof TileLayer) {
                    TileLayer l = (TileLayer)ml;
                    if (l.isVisible() || all) {
                        sandwich.add(l.getTileAt(posx, posy));
                    } else {
                        sandwich.add(null);
                    }
                }
            }
View Full Code Here

        propertiesCoordinates.clear();

        // Get all properties of all selected tiles...
        MapLayer ml = editor.getCurrentLayer();
        if (ml instanceof TileLayer) {
            TileLayer tl = (TileLayer) ml;
            Rectangle r = selection.getSelectedAreaBounds();
            int maxJ = (int) (r.getY() + r.getHeight());
            int maxI = (int) (r.getX() + r.getWidth());

            // todo: BL - Why are tiles checked on null? Surely whether a tile
            // todo: is null or not has nothing to do with whether you can place
            // todo: a property as a certain location?
            for (int j = (int) r.getY(); j < maxJ; j++) {
                for (int i = (int) r.getX(); i < maxI; i++) {
                    Tile t = selection.getTileAt(i, j);
                    if (t != null) {
                        propertiesCoordinates.add(new Point(i, j));
                    }
                }
            }

            if (!propertiesCoordinates.isEmpty()) {
                // Start with properties of first tile instance
                Point point = (Point) propertiesCoordinates.get(0);
                Properties p = tl.getTileInstancePropertiesAt(point.x, point.y);

                if (p != null) {
                    mergedProperties.putAll(p);

                    for (int i = 1; i < propertiesCoordinates.size(); i++) {
                        // Merge the other properties...
                        point = (Point) propertiesCoordinates.get(i);
                        p = tl.getTileInstancePropertiesAt(point.x, point.y);

                        if (p != null) {
                            for (Enumeration e = mergedProperties.keys(); e.hasMoreElements();) {
                                // We only care for properties that are already "known"...
                                String key = (String) e.nextElement();
View Full Code Here

        super.doPaint(x, y);

        ListIterator itr = getLayers();
        while (itr.hasNext()) {
            TileLayer tl = (TileLayer)itr.next();
            TileLayer tm = (TileLayer)affectedMp.getLayer(layer++);
            if (tm != null && tm.isVisible()) {
                tl.setOffset(centerx, centery);
                tl.mergeOnto(tm);
            }
        }
View Full Code Here

        super.doPaint(x, y);

        ListIterator<?> itr = getLayers();
        while (itr.hasNext()) {
            TileLayer tl = (TileLayer)itr.next();
            TileLayer tm = (TileLayer)affectedMp.getLayer(layer++);
            if (tm != null && tm.isVisible()) {
                tl.setOffset(centerx, centery);
                tl.mergeOnto(tm);
            }
        }
View Full Code Here

TOP

Related Classes of tiled.core.TileLayer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.