⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlmapwriter.java

📁 tiled地图编辑器是2d的,很不错的国外软件,使用起来很方便的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    private static void writeObjectGroup(ObjectGroup o, XMLWriter w)        throws IOException    {        Iterator itr = o.getObjects();        while (itr.hasNext()) {            writeObject((MapObject)itr.next(), o, w);        }    }    /**     * Writes this layer to an XMLWriter. This should be done <b>after</b> the     * first global ids for the tilesets are determined, in order for the right     * gids to be written to the layer data.     */    private static void writeMapLayer(MapLayer l, XMLWriter w) throws IOException {        Preferences prefs = TiledConfiguration.node("saving");        boolean encodeLayerData =                prefs.getBoolean("encodeLayerData", true);        boolean compressLayerData =                prefs.getBoolean("layerCompression", true) &&                        encodeLayerData;        Rectangle bounds = l.getBounds();        if (l.getClass() == SelectionLayer.class) {            w.startElement("selection");        } else if(l instanceof ObjectGroup){            w.startElement("objectgroup");        } else {            w.startElement("layer");        }        w.writeAttribute("name", l.getName());        w.writeAttribute("width", bounds.width);        w.writeAttribute("height", bounds.height);        if (bounds.x != 0) {            w.writeAttribute("xoffset", bounds.x);        }        if (bounds.y != 0) {            w.writeAttribute("yoffset", bounds.y);        }        if (!l.isVisible()) {            w.writeAttribute("visible", "0");        }        if (l.getOpacity() < 1.0f) {            w.writeAttribute("opacity", l.getOpacity());        }        writeProperties(l.getProperties(), w);        if (l instanceof ObjectGroup){            writeObjectGroup((ObjectGroup)l, w);        } else {            w.startElement("data");            if (encodeLayerData) {                ByteArrayOutputStream baos = new ByteArrayOutputStream();                OutputStream out;                w.writeAttribute("encoding", "base64");                if (compressLayerData) {                    w.writeAttribute("compression", "gzip");                    out = new GZIPOutputStream(baos);                } else {                    out = baos;                }                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = ((TileLayer)l).getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        out.write(gid       & LAST_BYTE);                        out.write(gid >> 8  & LAST_BYTE);                        out.write(gid >> 16 & LAST_BYTE);                        out.write(gid >> 24 & LAST_BYTE);                    }                }                if (compressLayerData) {                    ((GZIPOutputStream)out).finish();                }                w.writeCDATA(new String(Base64.encode(baos.toByteArray())));            } else {                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = ((TileLayer)l).getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        w.startElement("tile");                        w.writeAttribute("gid", gid);                        w.endElement();                    }                }            }            w.endElement();        }        w.endElement();    }    /**     * Used to write tile elements for tilesets not based on a tileset image.     *     * @param tile the tile instance that should be written     * @param w the writer to write to     * @throws IOException when an io error occurs     */    private static void writeTile(Tile tile, XMLWriter w) throws IOException {        w.startElement("tile");        w.writeAttribute("id", tile.getId());        //if (groundHeight != getHeight()) {        //    w.writeAttribute("groundheight", "" + groundHeight);        //}        writeProperties(tile.getProperties(), w);        Preferences prefs = TiledConfiguration.node("saving");        boolean embedImages = prefs.getBoolean("embedImages", true);        boolean tileSetImages = prefs.getBoolean("tileSetImages", false);        Image tileImage = tile.getImage();        // Write encoded data        if (tileImage != null) {            if (embedImages && !tileSetImages) {                w.startElement("image");                w.writeAttribute("format", "png");                w.startElement("data");                w.writeAttribute("encoding", "base64");                w.writeCDATA(new String(Base64.encode(                                ImageHelper.imageToPNG(tileImage))));                w.endElement();                w.endElement();            } else if (embedImages && tileSetImages) {                w.startElement("image");                w.writeAttribute("id", tile.getImageId());                w.endElement();            } else {                String prefix = prefs.get("tileImagePrefix", "tile");                String filename = prefix + tile.getId() + ".png";                String path = prefs.get("maplocation", "") + filename;                w.startElement("image");                w.writeAttribute("source", filename);                FileOutputStream fw = new FileOutputStream(new File(path));                byte[] data = ImageHelper.imageToPNG(tileImage);                fw.write(data, 0, data.length);                fw.close();                w.endElement();            }        }        if (tile instanceof AnimatedTile) {            writeAnimation(((AnimatedTile)tile).getSprite(), w);        }        w.endElement();    }    private static void writeAnimation(Sprite s, XMLWriter w) throws IOException {        w.startElement("animation");        for (int k = 0; k < s.getTotalKeys(); k++) {            Sprite.KeyFrame key = s.getKey(k);            w.startElement("keyframe");            w.writeAttribute("name", key.getName());            for (int it = 0; it < key.getTotalFrames(); it++) {                Tile stile = key.getFrame(it);                w.startElement("tile");                w.writeAttribute("gid", stile.getGid());                w.endElement();            }            w.endElement();        }        w.endElement();    }    private static void writeObject(MapObject m, ObjectGroup o, XMLWriter w)        throws IOException    {        Rectangle b = o.getBounds();        w.startElement("object");        w.writeAttribute("x", m.getX() + b.x);        w.writeAttribute("y", m.getY() + b.y);        w.writeAttribute("type", m.getType());        if (m.getSource() != null) {            w.writeAttribute("source", m.getSource());        }        writeProperties(m.getProperties(), w);        w.endElement();    }    /**     * Returns the relative path from one file to the other. The function     * expects absolute paths, relative paths will be converted to absolute     * using the working directory.     *     * @param from the path of the origin file     * @param to   the path of the destination file     * @return     the relative path from origin to destination     */    public static String getRelativePath(String from, String to) {        // Make the two paths absolute and unique        try {            from = new File(from).getCanonicalPath();            to = new File(to).getCanonicalPath();        } catch (IOException e) {        }        File fromFile = new File(from);        File toFile = new File(to);        Vector fromParents = new Vector();        Vector toParents = new Vector();        // Iterate to find both parent lists        while (fromFile != null) {            fromParents.add(0, fromFile.getName());            fromFile = fromFile.getParentFile();        }        while (toFile != null) {            toParents.add(0, toFile.getName());            toFile = toFile.getParentFile();        }        // Iterate while parents are the same        int shared = 0;        int maxShared = Math.min(fromParents.size(), toParents.size());        for (shared = 0; shared < maxShared; shared++) {            String fromParent = (String)fromParents.get(shared);            String toParent = (String)toParents.get(shared);            if (!fromParent.equals(toParent)) {                break;            }        }        // Append .. for each remaining parent in fromParents        StringBuffer relPathBuf = new StringBuffer();        for (int i = shared; i < fromParents.size() - 1; i++) {            relPathBuf.append(".." + File.separator);        }        // Add the remaining part in toParents        for (int i = shared; i < toParents.size() - 1; i++) {            relPathBuf.append(toParents.get(i) + File.separator);        }        relPathBuf.append(new File(to).getName());        String relPath = relPathBuf.toString();        // Turn around the slashes when path is relative        try {            String absPath = new File(relPath).getCanonicalPath();            if (!absPath.equals(relPath)) {                // Path is not absolute, turn slashes around                // Assumes: \ does not occur in filenames                relPath = relPath.replace('\\', '/');            }        } catch (IOException e) {        }        return relPath;    }    /**     * @see tiled.io.PluggableMapIO#getFilter()     */    public String getFilter() throws Exception {        return "*.tmx,*.tsx,*.tmx.gz";    }    public String getPluginPackage() {        return "Tiled internal TMX reader/writer";    }    public String getDescription() {        return            "The core Tiled TMX format writer\n" +            "\n" +            "Tiled Map Editor, (c) 2004-2006\n" +            "Adam Turk\n" +            "Bjorn Lindeijer";    }    public String getName() {        return "Default Tiled XML (TMX) map writer";    }    public boolean accept(File pathname) {        try {            String path = pathname.getCanonicalPath();            if (path.endsWith(".tmx") || path.endsWith(".tsx") || path.endsWith(".tmx.gz")) {                return true;            }        } catch (IOException e) {}        return false;    }    public void setLogger(PluginLogger logger) {    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -