Package tiled.core

Examples of tiled.core.MapObject


        final int x = getAttribute(t, "x", 0);
        final int y = getAttribute(t, "y", 0);
        final int width = getAttribute(t, "width", 0);
        final int height = getAttribute(t, "height", 0);

        MapObject obj = new MapObject(x, y, width, height);
        obj.setShape(obj.getBounds());
        if (name != null)
            obj.setName(name);
        if (type != null)
            obj.setType(type);

        NodeList children = t.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if ("image".equalsIgnoreCase(child.getNodeName())) {
                String source = getAttributeValue(child, "source");
                if (source != null) {
                    if (! new File(source).isAbsolute()) {
                        source = xmlPath + source;
                    }
                    obj.setImageSource(source);
                }
                break;
            } else if ("ellipse".equalsIgnoreCase(child.getNodeName())) {
                obj.setShape(new Ellipse2D.Double(x, y, width, height));
            } else if ("polygon".equalsIgnoreCase(child.getNodeName()) || "polyline".equalsIgnoreCase(child.getNodeName())) {
                Polygon shape = new Polygon();
                final String pointsAttribute = getAttributeValue(child, "points");
                StringTokenizer st = new StringTokenizer(pointsAttribute, ", ");
                while (st.hasMoreElements()) {
                    shape.addPoint(x + Integer.parseInt(st.nextToken()), y + Integer.parseInt(st.nextToken()));
                }
                obj.setShape(shape);
                obj.setBounds(shape.getBounds());
            }
        }

        Properties props = new Properties();
        readProperties(children, props);

        obj.setProperties(props);
        return obj;
    }
View Full Code Here


//        gc.translate(
//                bounds.x * tsize.x,
//                bounds.y * tsize.y);

        while (itr.hasNext()) {
            MapObject mo = itr.next();
            double ox = mo.getX() * zoom;
            double oy = mo.getY() * zoom;

            Image objectImage = mo.getImage(zoom);
            if (objectImage != null) {
                gc.drawImage(objectImage, (int) ox, (int) oy);
            }

            int objWidth = mo.getWidth();
      int objHeight = mo.getHeight();
      if (objWidth == 0 || objHeight == 0) {
                gc.setAntialias(SWT.ON);
                gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
                gc.fillOval((int) ox + 1, (int) oy + 1,
                        (int) (10 * zoom), (int) (10 * zoom));
                gc.setBackground(getDisplay().getSystemColor(OBJECT_FOREGROUND));
                gc.fillOval((int) ox, (int) oy,
                        (int) (10 * zoom), (int) (10 * zoom));
                gc.setAntialias(SWT.OFF);
            } else {
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
                gc.drawRectangle((int) ox + 1, (int) oy + 1,
                    (int) (objWidth * zoom),
                    (int) (objHeight * zoom));
                gc.setForeground(getDisplay().getSystemColor(OBJECT_FOREGROUND));
                gc.drawRectangle((int) ox, (int) oy,
                    (int) (objWidth * zoom),
                    (int) (objHeight * zoom));
            }
            if (zoom > 0.0625) {
              if (drawResizeAnchors) {
                drawAnchors(gc, ox, oy, objWidth, objHeight);
              }
                final String s = mo.getName() != null ? mo.getName() : "(null)";
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
                gc.drawString(s, (int) (ox - 5) + 1, (int) (oy - 5) + 1);
                gc.setForeground(getDisplay().getSystemColor(OBJECT_FOREGROUND));
                gc.drawString(s, (int) (ox - 5), (int) (oy - 5));
               
View Full Code Here

      return;
    MapLayer layer = map.getLayer(mapView.getCurrentLayer());
    mapView.setCursor(Display.getDefault()
        .getSystemCursor(SWT.CURSOR_ARROW));
    if (layer instanceof ObjectGroup) {
      MapObject targetObject = ((ObjectGroup) layer)
          .getObjectAt(e.x, e.y);
      if (targetObject != null) {
        Rectangle bounds = ((ObjectGroup) layer)
            .getActualObjectRectangle(targetObject);
        Rectangle[] anchorRects = AnchoringUtil.getAnchorRects(bounds);
View Full Code Here

  public boolean canStartDrag(MouseEvent e) {
    if (mapEditor.getCurrentPointerState() != MapEditor.PS_RESIZEOBJ)
      return false;
    MapLayer layer = map.getLayer(mapView.getCurrentLayer());
    if (layer instanceof ObjectGroup) {
      MapObject targetObject = ((ObjectGroup) layer)
          .getObjectAt(e.x, e.y);
      if (targetObject != null) {
        Rectangle bounds = ((ObjectGroup) layer)
            .getActualObjectRectangle(targetObject);
        Rectangle[] anchorRects = AnchoringUtil.getAnchorRects(bounds);
View Full Code Here

    if (currentMode == SELECT_MODE) {
      setSelectionBounds(e);
      selectionLayer.maskedCopyFrom(editor.getCurrentLayer(), selectionLayer.getPixelBounds());
      Iterator<MapObject> objects = selectionLayer.getObjects();
      if (!objects.hasNext()) {
        MapObject object = ((ObjectGroup)editor.getCurrentLayer()).getObjectAt(e.x,e.y);
        if (object == null) {
          editor.getMap().removeLayerSpecial(selectionLayer);
          return;
        } else {
          selectionLayer.addObject(object);
          selectionLayer.setPixelBounds(object.getBounds());
          objects = selectionLayer.getObjects(); //To avoid concurrent modification
        }
       
      }
      Rectangle origBounds = selectionLayer.getPixelBounds();
      int left = origBounds.x + origBounds.width;
      int right = origBounds.x;
      int top = origBounds.y + origBounds.height;
      int bottom = origBounds.y;
      for (; objects.hasNext();) {
        MapObject object = objects.next();
        Rectangle rect = object.getBounds();
        if (rect.x < left)
          left = rect.x;
        if (rect.y < top)
          top = rect.y;
        if (rect.x + rect.width > right)
View Full Code Here

      } else if (mouseButton == 1 && layer instanceof ObjectGroup) {
        // TODO: Fix this to use pixels in the first place
        // (with optional snap to grid)
        int w = currentMap.getTileWidth();
        int h = currentMap.getTileHeight();
        MapObject object = new MapObject(bounds.x * w, bounds.y * h,
            bounds.width * w, bounds.height * h);
        /*
         * Point pos = mapView.screenToPixelCoords( event.x, event.y);
         */
        ObjectGroup group = (ObjectGroup) layer;
View Full Code Here

      } else if (layer instanceof ObjectGroup && !bMouseIsDragging) {
        // Get the object on this location and display the relative
        // options dialog
        ObjectGroup group = (ObjectGroup) layer;
        Point pos = mapView.screenToPixelCoords(event.x, event.y);
        MapObject obj = group.getObjectNear(pos.x, pos.y,
            mapView.getZoom());
        if (obj != null) {
          int tileWidth = mapView.getMap().getTileWidth();
          int tileHeight = mapView.getMap().getTileHeight();
          ObjectPropertyDialog dialog = new ObjectPropertyDialog(
              getSite().getShell(), obj, this, tileWidth, tileHeight);
          dialog.open();
          mapView.redraw();
        }
      }
    } else if (mouseButton == 2
        || (mouseButton == 1 && (event.stateMask & SWT.ALT) != 0)) {
      // Scroll with middle mouse button
      int dx = event.x - mouseInitialScreenLocation.x;
      int dy = event.y - mouseInitialScreenLocation.y;
      Point currentPosition = mapScrollView.getOrigin();
      // JViewport mapViewPort = mapScrollPane.getViewport();
      // Point currentPosition = mapViewPort.getViewPosition();
      mouseInitialScreenLocation = new Point(event.x - dx, event.y - dy);

      Point newPosition = new Point(currentPosition.x - dx,
          currentPosition.y - dy);

      // Take into account map boundaries in order to prevent
      // scrolling past them
      Point viewSize = mapView.getSize();
      Point viewportSize = mapScrollView.getSize();
      int maxX = viewSize.x - viewportSize.x;
      int maxY = viewSize.y - viewportSize.y;
      newPosition.x = Math.min(maxX, Math.max(0, newPosition.x));
      newPosition.y = Math.min(maxY, Math.max(0, newPosition.y));

      mapScrollView.setOrigin(newPosition);
      // mapViewPort.setViewPosition(newPosition);
    } else if (mouseButton == 1) {
      switch (currentPointerState) {
      case PS_PAINT:
         paintEdit.setPresentationName(TOOL_PAINT);
        if (layer instanceof TileLayer && canPaint(currentBrush)) {
          try {
            mapView.repaintRegion(currentBrush.doPaint(tile.x,
                tile.y));
            currentMap.fireMapChanged();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case PS_ERASE:
         paintEdit.setPresentationName(TOOL_ERASE);
        if (layer instanceof TileLayer) {
          ((TileLayer) layer).setTileAt(tile.x, tile.y, null);
          mapView.repaintRegion(new Rectangle(tile.x, tile.y, 1, 1));
        }
        break;
      case PS_POUR:
         paintEdit = null;
        if (layer instanceof TileLayer) {
          TileLayer tileLayer = (TileLayer) layer;
          Tile oldTile = tileLayer.getTileAt(tile.x, tile.y);
          pour(tileLayer, tile.x, tile.y, currentTile, oldTile);
          mapView.redraw();
        }
        break;
      case PS_EYED:
        if (layer instanceof TileLayer) {
          TileLayer tileLayer = (TileLayer) layer;
          Tile newTile = tileLayer.getTileAt(tile.x, tile.y);
          setCurrentTile(newTile);
        }
        break;
      case PS_MOVE: {
        Point translation = new Point(tile.x - mousePressLocation.x,
            tile.y - mousePressLocation.y);

        layer.translate(translation.x, translation.y);
        mousePressLocation = tile;
        moveDist.x += translation.x;
        moveDist.y += translation.y;
        mapView.redraw();
        break;
      }
      case PS_MARQUEE:
        if (!(layer instanceof TileLayer)) {
          break;
        }
        if (marqueeSelection != null) {
          Point limp = mouseInitialPressLocation;
          Rectangle oldArea = marqueeSelection
              .getSelectedAreaBounds();
          int minx = Math.min(limp.x, tile.x);
          int miny = Math.min(limp.y, tile.y);

          Rectangle selRect = new Rectangle(minx, miny, (Math.max(
              limp.x, tile.x) - minx) + 1, (Math.max(limp.y,
              tile.y) - miny) + 1);

          if ((event.stateMask & SWT.SHIFT) > 0) {
            marqueeSelection.add(new Area(Converter
                .SWTRectToAWT(selRect)));
          } else if ((event.stateMask & SWT.CONTROL) > 0) {
            marqueeSelection.subtract(new Area(Converter
                .SWTRectToAWT(selRect)));
          } else {
            marqueeSelection.selectRegion(Converter
                .SWTRectToAWT(selRect));
          }
          if (oldArea != null) {
            oldArea.add(marqueeSelection.getSelectedAreaBounds());
            mapView.repaintRegion(oldArea);
          }
        }
        break;
      case PS_ADDOBJ:
        if (layer instanceof ObjectGroup) {
          if (marqueeSelection == null) {
            setMarqueeSelection(new SelectionLayer(
                currentMap.getWidth(), currentMap.getHeight()));
            currentMap.addLayerSpecial(marqueeSelection);
          }

          Point limp = mouseInitialPressLocation;
          Rectangle oldArea = marqueeSelection
              .getSelectedAreaBounds();
          int minx = Math.min(limp.x, tile.x);
          int miny = Math.min(limp.y, tile.y);

          Rectangle selRect = new Rectangle(minx, miny, (Math.max(
              limp.x, tile.x) - minx) + 1, (Math.max(limp.y,
              tile.y) - miny) + 1);

          marqueeSelection.selectRegion(Converter
              .SWTRectToAWT(selRect));
          if (oldArea != null) {
            oldArea.add(marqueeSelection.getSelectedAreaBounds());
            mapView.repaintRegion(oldArea);
          }
        }
        break;
      case PS_REMOVEOBJ:
        if (layer instanceof ObjectGroup) {
          ObjectGroup group = (ObjectGroup) layer;
          Point pos = mapView.screenToPixelCoords(event.x, event.y);
          MapObject obj = group.getObjectNear(pos.x, pos.y,
              mapView.getZoom());
          if (obj != null) {
            // addEdit(new RemoveObjectEdit(group,
            // obj));
            group.removeObject(obj);
View Full Code Here

  protected void addObjects(Point cursorLocation, Rectangle selectionBounds,
      Iterator<MapObject> objects) {
    try {
      List <MapObject> resultObjects = new ArrayList<MapObject>();
      for (;objects.hasNext();) {
        MapObject next = objects.next();
        MapObject clone = next.clone();
        Rectangle initialBounds = clone.getBounds();
        clone.setBounds(initialBounds.x - selectionBounds.x + cursorLocation.x,
                initialBounds.y - selectionBounds.y + cursorLocation.y,
                initialBounds.width,
                initialBounds.height);
        resultObjects.add(clone);
      }
View Full Code Here

TOP

Related Classes of tiled.core.MapObject

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.