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

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


    if (newTile == oldTile || !layer.canEdit()) {
      return;
    }

    Rectangle area;
    TileLayer before = (TileLayer) createLayerCopy(layer);
    TileLayer after;

    // Check that the copy was succesfully created
    if (before == null) {
      return;
    }

    if (marqueeSelection == null) {
      area = new Rectangle(new Point(x, y));
      Stack<Point> stack = new Stack<Point>();

      stack.push(new Point(x, y));
      while (!stack.empty()) {
        // Remove the next tile from the stack
        Point p = stack.pop();

        // If the tile it meets the requirements, set it and push its
        // neighbouring tiles on the stack.
        if (layer.contains(p.x, p.y) && layer.getTileAt(p.x, p.y) == oldTile) {
          layer.setTileAt(p.x, p.y, newTile);
          area.add(p);

          stack.push(new Point(p.x, p.y - 1));
          stack.push(new Point(p.x, p.y + 1));
          stack.push(new Point(p.x + 1, p.y));
          stack.push(new Point(p.x - 1, p.y));
        }
      }
    } else {
      if (marqueeSelection.getSelectedArea().contains(x, y)) {
        area = marqueeSelection.getSelectedAreaBounds();
        for (int i = area.y; i < area.height + area.y; i++) {
          for (int j = area.x; j < area.width + area.x; j++) {
            if (marqueeSelection.getSelectedArea().contains(j, i)) {
              layer.setTileAt(j, i, newTile);
            }
          }
        }
      } else {
        return;
      }
    }

    Rectangle bounds = new Rectangle(area.x, area.y, area.width + 1, area.height + 1);
    after = new TileLayer(bounds);
    after.copyFrom(layer);

    MapLayerEdit mle = new MapLayerEdit(layer, before, after);
    mle.setPresentationName(TOOL_FILL);
    undoSupport.postEdit(mle);
  }
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");

        if (encoding != null && "base64".equalsIgnoreCase(encoding)) {
          Node cdata = child.getFirstChild();
          if (cdata == null) {
            Log.ger.warn("layer <data> tag enclosed no data. (empty data tag)");
          } else {
            char[] enc = cdata.getNodeValue().trim().toCharArray();
            byte[] dec = Base64.decode(enc);
            ByteArrayInputStream bais = new ByteArrayInputStream(dec);
            InputStream is;

            String comp = getAttributeValue(child, "compression");

            if (comp != null && "gzip".equalsIgnoreCase(comp)) {
              is = new GZIPInputStream(bais);
            } 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;

                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++;
              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

    int numBlocksGfx = Util.readShort(in);
    Chunk c = findChunk("BKDT");
    if (c == null) {
      throw new IOException("No BKDT block found!");
    }
    MapLayer ml = new TileLayer(ret, ret.getWidth(), ret.getHeight());
    ml.setName("bg");
    ret.addLayer(ml);
    for (int i = 1; i < 7; i++) {
      // TODO: I believe this should be ObjectGroup
      ml = new ObjectGroup(ret, 0, 0);
      ml.setName("ObjectLayer " + i);
      ret.addLayer(ml);
    }
    ml = new TileLayer(ret, ret.getWidth(), ret.getHeight());
    ml.setName("fg 1");
    ret.addLayer(ml);
    ml = new TileLayer(ret, ret.getWidth(), ret.getHeight());
    ml.setName("fg 2");
    ret.addLayer(ml);
    ml = new TileLayer(ret, ret.getWidth(), ret.getHeight());
    ml.setName("fg 3");
    ret.addLayer(ml);

    readBKDTChunk(ret, c.getInputStream(), numBlocks);

    c = findChunk("BGFX");
View Full Code Here

   * @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

    public Cell(Map map, int posx, int posy, int start, int len, boolean all) {
      sandwich = new Vector<Tile>();
      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

      listener.tileSelected(event);
    }
  }

  private void fireTileRegionSelectionEvent(Rectangle selection) {
    TileLayer region = createTileLayerFromRegion(selection);
    TileRegionSelectionEvent event = new TileRegionSelectionEvent(this, region);
    for (TileSelectionListener listener : tileSelectionListeners) {
      listener.tileRegionSelected(event);
    }
  }
View Full Code Here

   * @param rect
   *            the rectangular region from which a tile layer is created
   * @return the created tile layer
   */
  private TileLayer createTileLayerFromRegion(Rectangle rect) {
    TileLayer layer = new TileLayer(rect.width + 1, rect.height + 1);

    // Copy the tiles in the region to the tile layer
    for (int y = rect.y; y <= rect.y + rect.height; y++) {
      for (int x = rect.x; x <= rect.x + rect.width; x++) {
        layer.setTileAt(x - rect.x, y - rect.y, getTileAt(x, y));
      }
    }

    return layer;
  }
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

      return;
    }

    // create data
    for (int z = 0, n = map.getLayerVector().size() > 3 ? 3 : map.getLayerVector().size(); z < n; z++) {
      TileLayer layer = (TileLayer) map.getLayer(z);
      for (int x = 0, l = layer.getWidth(); x < l; x++) {
        for (int y = 0, m = layer.getHeight(); y < m; y++) {
          Tile t = layer.getTileAt(x, y);

          // is set?
          if (t == null) {
            d[z][x][y] = 0;
            continue;
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

TOP

Related Classes of de.yaams.extensions.basemap.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.