Beispiel #1
0
  public static SpriteTemplate fromSprites(
      LinkedList<Sprite> sprites, Layer layer, Shape pivotShape, Relativity relativity) {
    SpriteTemplate template = null;
    double leftX = 0d, topY = 0d, rightX = 0d, bottomY = 0d;
    Sprite newSprite = new Sprite();

    for (Sprite sprite : sprites) {
      if (sprite.shapeType.getNum() == ShapeType.spriteTemplate.getNum()) continue;
      if (template != null) {
        leftX = Math.min(leftX, sprite.leftX());
        topY = Math.min(topY, sprite.topY());
        rightX = Math.max(rightX, sprite.rightX());
        bottomY = Math.max(bottomY, sprite.bottomY());
      } else {
        template = new SpriteTemplate();
        leftX = sprite.leftX();
        topY = sprite.topY();
        rightX = sprite.rightX();
        bottomY = sprite.bottomY();
        if (pivotShape == null) pivotShape = sprite;
      }
    }

    if (template != null) {
      newSprite.setCoords(0.5d * (leftX + rightX), 0.5d * (topY + bottomY));
      newSprite.setHeight(bottomY - topY);
      newSprite.shapeType = template;
      if (layer != null) layer.insert(newSprite, pivotShape, relativity);

      for (Sprite sprite : sprites) {
        if (sprite.shapeType.getNum() == ShapeType.spriteTemplate.getNum()) continue;
        layer.remove(sprite);
        sprite.setCoords(
            (newSprite.getX() - sprite.getX()) / newSprite.getWidth(),
            (newSprite.getY() - sprite.getY()) / newSprite.getHeight());
        sprite.setSize(
            sprite.getWidth() / newSprite.getWidth(), sprite.getHeight() / newSprite.getHeight());
        template.sprites.addLast(sprite);
      }
    }

    return template;
  }