Package de.yaams.extensions.basemap.tiled.core

Examples of de.yaams.extensions.basemap.tiled.core.TileSet


    final Map m = new Map(map.getWidth(), map.getHeight());
    m.setFilename(path);
    m.setProject(p);
    m.setMid(0);

    TileSet tileset = buildTileset(info.getTileset(), p);
    m.addTileset(tileset);
    m.setTileHeight(32);
    m.setTileWidth(32);
    // tileset.

    // def [](x,y=0,z=0)
    // @data[x+y*@xsize+z*@xsize*@ysize]

    // build layers
    // load map data

    for (int z = 0, n = map.getLayer(); z < n; z++) {
      final TileLayer l = (TileLayer) m.addLayer();
      l.setName(z == 0 ? "Base" : z == 1 ? "Objects" : Integer.toString(z));
      // set tiles
      for (int x = 0, u = m.getWidth(); x < u; x++) {
        for (int y = 0, v = m.getHeight(); y < v; y++) {
          if (map.get(x, y, z) >= 0) {
            l.setTileAt(x, y, tileset.getTile(map.get(x, y, z)));
          }
        }
      }
    }
    return m;
View Full Code Here


  protected TileSet buildTileset(int id, Project p) throws IOException {
    // load tileset
    CTileset ct = (CTileset) p.getObjects().get("tileset").getObjects().get(id);

    // build tileset
    TileSet tileset = new TileSet();

    BufferedImage b = ct.getTilesetGraphic(p);

    // add tileset
    // add tiles
    for (int i = 0, l = b.getWidth() / 32 * (b.getHeight() / 32); i < l; i++) {
      Tile t = new Tile();
      t.setId(i);
      t.setImage(b.getSubimage(i % (b.getWidth() / 32) * 32, i / (b.getWidth() / 32) * 32, 32, 32));
      t.setTileSet(tileset);
      tileset.addTile(t);
    }

    tileset.setName(ct.getTitle());

    return tileset;
  }
View Full Code Here

  private Vector<Cell> cells;

  public TileMergeHelper(Map map) {
    myMap = map;
    cells = new Vector<Cell>();
    myTs = new TileSet();
    myTs.setName("Merged Set");
  }
View Full Code Here

      BrushDialog bd = new BrushDialog(this, YaFrame.get(), currentBrush);
      bd.setVisible(true);
    } else if (command.equals(Resources.getString("menu.tilesets.new"))) {
      if (currentMap != null) {
        NewTilesetDialog dialog = new NewTilesetDialog(YaFrame.get(), currentMap);
        TileSet newSet = dialog.create();
        if (newSet != null) {
          currentMap.addTileset(newSet);
        }
      }
    } else if (command.equals(Resources.getString("menu.tilesets.import"))) {
      if (currentMap != null) {
        JFileChooser chooser = new JFileChooser(currentMap.getFilename());
        MapHelper.addExtension(chooser);

        int ret = chooser.showOpenDialog(appFrame);
        if (ret == JFileChooser.APPROVE_OPTION) {
          String filename = chooser.getSelectedFile().getAbsolutePath();
          try {
            TileSet set = MapHelper.loadTileset(filename);
            currentMap.addTileset(set);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
View Full Code Here

    return img;
  }

  private TileSet unmarshalTilesetFile(InputStream in, String filename) throws Exception {
    TileSet set = null;
    Node tsNode;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      // builder.setErrorHandler(new XMLErrorHandler());
      Document tsDoc = builder.parse(in, ".");

      String xmlPathSave = xmlPath;
      if (filename.indexOf(File.separatorChar) >= 0) {
        xmlPath = filename.substring(0, filename.lastIndexOf(File.separatorChar) + 1);
      }

      NodeList tsNodeList = tsDoc.getElementsByTagName("tileset");

      // There can be only one tileset in a .tsx file.
      tsNode = tsNodeList.item(0);
      if (tsNode != null) {
        set = unmarshalTileset(tsNode);
        if (set.getSource() != null) {
          Log.ger.warn("Recursive external Tilesets are not supported.");
        }
        set.setSource(filename);
      }

      xmlPath = xmlPathSave;
    } catch (SAXException e) {
      Log.ger.error("Failed while loading " + filename + ": " + e.getLocalizedMessage());
View Full Code Here

    return null;
  }

  private Map readMPHDChunk(InputStream in) throws IOException {
    Map ret = null;
    TileSet set = new TileSet();
    int major, minor;
    major = in.read();
    minor = in.read();
    in.skip(2); // skip lsb and reserved bytes - always msb
    ret = new Map(Util.readShort(in), Util.readShort(in));
    Properties retProps = ret.getProperties();
    ret.setOrientation(Map.MDO_ORTHO); // be sure to set the orientation!
    retProps.setProperty("(s)fmap reader", "Don't modify properties marked (s) unless you really know what you're doing.");
    retProps.setProperty("version", "" + major + "." + minor);
    in.skip(4); // reserved
    twidth = Util.readShort(in);
    theight = Util.readShort(in);
    ret.setTileWidth(twidth);
    ret.setTileHeight(theight);
    set.setName("Static tiles");
    ret.addTileset(set);
    int depth = Util.readShort(in);
    if (depth < 16) {
      throw new IOException("Tile bitdepths less than 16 are not supported!");
    }
View Full Code Here

      String filename = tilesetBaseDir + source;
      // if (Util.checkRoot(source)) {
      // filename = makeUrl(source);
      // }

      TileSet ext = null;

      try {
        // just a little check for tricky people...
        String extention = source.substring(source.lastIndexOf('.') + 1);
        if (!"tsx".equals(extention.toLowerCase())) {
          Log.ger.warn("tileset files should end in .tsx! (" + source + ")");
        }

        InputStream in = new URL(makeUrl(filename)).openStream();
        ext = unmarshalTilesetFile(in, filename);
      } catch (FileNotFoundException fnf) {
        Log.ger.error("Could not find external tileset file " + filename);
      }

      if (ext == null) {
        Log.ger.error("tileset " + source + " was not loaded correctly!");
        ext = new TileSet();
      }

      ext.setFirstGid(firstGid);
      return ext;
    } else {
      final int tileWidth = getAttribute(t, "tilewidth", map != null ? map.getTileWidth() : 0);
      final int tileHeight = getAttribute(t, "tileheight", map != null ? map.getTileHeight() : 0);
      final int tileSpacing = getAttribute(t, "spacing", 0);
      final int tileMargin = getAttribute(t, "margin", 0);

      TileSet set = new TileSet();

      set.setName(getAttributeValue(t, "name"));
      set.setBaseDir(basedir);
      set.setFirstGid(firstGid);

      boolean hasTilesetImage = false;
      NodeList children = t.getChildNodes();

      for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);

        if (child.getNodeName().equalsIgnoreCase("image")) {
          if (hasTilesetImage) {
            Log.ger.warn("Ignoring illegal image element after tileset image.");
            continue;
          }

          String imgSource = getAttributeValue(child, "source");
          String id = getAttributeValue(child, "id");
          String transStr = getAttributeValue(child, "trans");

          if (imgSource != null && id == null) {
            // Not a shared image, but an entire set in one image
            // file. There should be only one image element in this
            // case.
            hasTilesetImage = true;

            // FIXME: importTileBitmap does not fully support URLs
            String sourcePath = imgSource;
            if (!new File(imgSource).isAbsolute()) {
              sourcePath = tilesetBaseDir + imgSource;
            }

            Log.ger.info("Importing " + sourcePath + "...");

            if (transStr != null) {
              int colorInt = Integer.parseInt(transStr, 16);
              Color color = new Color(colorInt);
              set.setTransparentColor(color);
            }

            set.importTileBitmap(sourcePath, new BasicTileCutter(tileWidth, tileHeight, tileSpacing, tileMargin));
          } else {
            Image image = unmarshalImage(child, tilesetBaseDir);
            String idValue = getAttributeValue(child, "id");
            int imageId = Integer.parseInt(idValue);
            set.addImage(image, imageId);
          }
        } else if (child.getNodeName().equalsIgnoreCase("tile")) {
          Tile tile = unmarshalTile(set, child, tilesetBaseDir);
          if (!hasTilesetImage || tile.getId() > set.getMaxTileId()) {
            set.addTile(tile);
          } else {
            Tile myTile = set.getTile(tile.getId());
            myTile.setProperties(tile.getProperties());
            // TODO: there is the possibility here of overlaying
            // images,
            // which some people may want
          }
View Full Code Here

   * @param m
   * @param in
   * @throws IOException
   */
  private void readBODYChunk(Map m, InputStream in) throws IOException {
    TileSet set = m.getTilesets().get(0);
    TileLayer bg = (TileLayer) m.getLayer(0), fg0 = (TileLayer) m.getLayer(7), fg1 = (TileLayer) m.getLayer(8), fg2 = (TileLayer) m
        .getLayer(9);

    for (int i = 0; i < m.getHeight(); i++) {
      for (int j = 0; j < m.getWidth(); j++) {
        int block = (Util.readShort(in) & 0x00FF) / BLKSTR_WIDTH;
        // System.out.print("" + block);
        BlkStr blk = blocks.get(block);
        bg.setTileAt(j, i, set.getTile((int) blk.bg));
        fg0.setTileAt(j, i, set.getTile((int) blk.fg0));
        fg1.setTileAt(j, i, set.getTile((int) blk.fg1));
        fg2.setTileAt(j, i, set.getTile((int) blk.fg2));
      }
    }
  }
View Full Code Here

   * @param num
   *            Number of Tiles to read
   * @throws IOException
   */
  private void readBGFXChunk(Map m, InputStream in, int num) throws IOException {
    TileSet set = m.getTilesets().get(0);
    set.addTile(new Tile());
    Util.readRawImage(in, twidth, theight); // skip the null-tile
    for (int i = 1; i < num; i++) {
      Tile t = new Tile();
      // t.setAppearance(image_id, 0);
      set.addTile(t);
    }
  }
View Full Code Here

                tileId |= is.read();
                tileId |= is.read() << 8;
                tileId |= is.read() << 16;
                tileId |= is.read() << 24;

                TileSet ts = map.findTileSetForTileGID(tileId);
                if (ts != null) {
                  ml.setTileAt(x, y, ts.getTile(tileId - ts.getFirstGid()));
                } else {
                  ml.setTileAt(x, y, null);
                }
              }
            }
          }
        } 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);
              TileSet ts = map.findTileSetForTileGID(tileId);
              if (ts != null) {
                ml.setTileAt(x, y, ts.getTile(tileId - ts.getFirstGid()));
              } else {
                ml.setTileAt(x, y, null);
              }

              x++;
View Full Code Here

TOP

Related Classes of de.yaams.extensions.basemap.tiled.core.TileSet

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.