public static FileHandle getRelativeFileHandle(FileHandle file, String path) {
   StringTokenizer tokenizer = new StringTokenizer(path, "\\/");
   FileHandle result = file.parent();
   while (tokenizer.hasMoreElements()) {
     String token = tokenizer.nextToken();
     if (token.equals("..")) result = result.parent();
     else {
       result = result.child(token);
     }
   }
   return result;
 }
Example #2
0
 @Override
 public void onTilesetParsed(Tileset parsedTileset) {
   if (loadTilesets && !parsedTileset.isTextureLoaded()) {
     parsedTileset.loadTexture(fileHandle.parent());
   }
   this.tilesets.add(parsedTileset);
 }
 @Override
 public ParticleEffect load(
     AssetManager am, String fileName, FileHandle file, ParticleEffectParameter param) {
   ParticleEffect effect = new ParticleEffect();
   if (param != null && param.atlasFile != null)
     effect.load(file, am.get(param.atlasFile, TextureAtlas.class));
   else if (param != null && param.imagesDir != null) effect.load(file, param.imagesDir);
   else effect.load(file, file.parent());
   return effect;
 }
  void loadSkeleton(final FileHandle skeletonFile) {
    if (skeletonFile == null) return;

    try {
      // Setup a texture atlas that uses a white image for images not found in the atlas.
      Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
      pixmap.setColor(new Color(1, 1, 1, 0.33f));
      pixmap.fill();
      final AtlasRegion fake = new AtlasRegion(new Texture(pixmap), 0, 0, 32, 32);
      pixmap.dispose();

      String atlasFileName = skeletonFile.nameWithoutExtension();
      if (atlasFileName.endsWith(".json"))
        atlasFileName = new FileHandle(atlasFileName).nameWithoutExtension();
      FileHandle atlasFile = skeletonFile.sibling(atlasFileName + ".atlas");
      if (!atlasFile.exists()) atlasFile = skeletonFile.sibling(atlasFileName + ".atlas.txt");
      TextureAtlasData data =
          !atlasFile.exists() ? null : new TextureAtlasData(atlasFile, atlasFile.parent(), false);
      TextureAtlas atlas =
          new TextureAtlas(data) {
            public AtlasRegion findRegion(String name) {
              AtlasRegion region = super.findRegion(name);
              if (region == null) {
                // Look for separate image file.
                FileHandle file = skeletonFile.sibling(name + ".png");
                if (file.exists()) {
                  Texture texture = new Texture(file);
                  texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
                  region = new AtlasRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
                  region.name = name;
                }
              }
              return region != null ? region : fake;
            }
          };

      // Load skeleton data.
      String extension = skeletonFile.extension();
      if (extension.equalsIgnoreCase("json") || extension.equalsIgnoreCase("txt")) {
        SkeletonJson json = new SkeletonJson(atlas);
        json.setScale(ui.scaleSlider.getValue());
        skeletonData = json.readSkeletonData(skeletonFile);
      } else {
        SkeletonBinary binary = new SkeletonBinary(atlas);
        binary.setScale(ui.scaleSlider.getValue());
        skeletonData = binary.readSkeletonData(skeletonFile);
        if (skeletonData.getBones().size == 0) throw new Exception("No bones in skeleton data.");
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      ui.toast("Error loading skeleton: " + skeletonFile.name());
      lastModifiedCheck = 5;
      return;
    }

    skeleton = new Skeleton(skeletonData);
    skeleton.setToSetupPose();
    skeleton = new Skeleton(skeleton); // Tests copy constructors.
    skeleton.updateWorldTransform();

    state = new AnimationState(new AnimationStateData(skeletonData));
    state.addListener(
        new AnimationStateAdapter() {
          public void event(TrackEntry entry, Event event) {
            ui.toast(event.getData().getName());
          }
        });

    this.skeletonFile = skeletonFile;
    prefs.putString("lastFile", skeletonFile.path());
    prefs.flush();
    lastModified = skeletonFile.lastModified();
    lastModifiedCheck = checkModifiedInterval;

    // Populate UI.

    ui.window.getTitleLabel().setText(skeletonFile.name());
    {
      Array<String> items = new Array();
      for (Skin skin : skeletonData.getSkins()) items.add(skin.getName());
      ui.skinList.setItems(items);
    }
    {
      Array<String> items = new Array();
      for (Animation animation : skeletonData.getAnimations()) items.add(animation.getName());
      ui.animationList.setItems(items);
    }
    ui.trackButtons.getButtons().first().setChecked(true);

    // Configure skeleton from UI.

    if (ui.skinList.getSelected() != null) skeleton.setSkin(ui.skinList.getSelected());
    setAnimation();

    // ui.animationList.clearListeners();
    // state.setAnimation(0, "walk", true);
  }
Example #5
0
  private void init(FileHandle fontFile, TextureRegion region, boolean flip) {
    flipped = flip;
    BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512);
    try {
      reader.readLine(); // info

      String line = reader.readLine();
      if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
      String[] common = line.split(" ", 4);
      if (common.length < 4) throw new GdxRuntimeException("Invalid font file: " + fontFile);

      if (!common[1].startsWith("lineHeight="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      lineHeight = Integer.parseInt(common[1].substring(11));

      if (!common[2].startsWith("base="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      int baseLine = Integer.parseInt(common[2].substring(5));

      if (region != null) reader.readLine(); // page
      else {
        line = reader.readLine();
        if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String[] page = line.split(" ", 4);
        if (!page[2].startsWith("file="))
          throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String imgFilename = null;
        if (page[2].endsWith("\"")) {
          imgFilename = page[2].substring(6, page[2].length() - 1);
        } else {
          imgFilename = page[2].substring(5, page[2].length());
        }
        FileHandle imageFile = fontFile.parent().child(imgFilename);
        region = new TextureRegion(new Texture(imageFile, false));
      }

      this.region = region;
      float invTexWidth = 1.0f / region.getTexture().getWidth();
      float invTexHeight = 1.0f / region.getTexture().getHeight();
      float u = region.u;
      float v = region.v;

      descent = 0;
      //			descent = g != null? baseLine + g.yoffset:0;

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (line.startsWith("kernings ")) break;
        if (!line.startsWith("char ")) continue;

        Glyph glyph = new Glyph();

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int ch = Integer.parseInt(tokens.nextToken());
        if (ch <= Character.MAX_VALUE) {
          Glyph[] page = glyphs[ch / PAGE_SIZE];
          if (page == null) glyphs[ch / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
          page[ch & PAGE_SIZE - 1] = glyph;
        } else continue;
        tokens.nextToken();
        int srcX = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int srcY = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.width = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.height = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.xoffset = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        if (flip) glyph.yoffset = Integer.parseInt(tokens.nextToken());
        else glyph.yoffset = -(glyph.height + Integer.parseInt(tokens.nextToken()));
        tokens.nextToken();
        glyph.xadvance = Integer.parseInt(tokens.nextToken());

        glyph.u = u + srcX * invTexWidth;
        glyph.u2 = u + (srcX + glyph.width) * invTexWidth;
        if (flip) {
          glyph.v = v + srcY * invTexHeight;
          glyph.v2 = v + (srcY + glyph.height) * invTexHeight;
        } else {
          glyph.v2 = v + srcY * invTexHeight;
          glyph.v = v + (srcY + glyph.height) * invTexHeight;
        }

        descent = Math.min(baseLine + glyph.yoffset, descent);
      }

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (!line.startsWith("kerning ")) break;

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int first = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int second = Integer.parseInt(tokens.nextToken());
        if (first < 0 || first > Character.MAX_VALUE || second < 0 || second > Character.MAX_VALUE)
          continue;
        Glyph glyph = getGlyph((char) first);
        tokens.nextToken();
        int amount = Integer.parseInt(tokens.nextToken());
        glyph.setKerning(second, amount);
      }

      Glyph g = getGlyph(' ');
      if (g == null) {
        g = new Glyph();
        g.xadvance = getGlyph('l').xadvance;
        Glyph[] page = glyphs[' ' / PAGE_SIZE];
        if (page == null) glyphs[' ' / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
        page[' ' & PAGE_SIZE - 1] = g;
      }
      spaceWidth = g != null ? g.xadvance + g.width : 1;

      g = getGlyph('x');
      xHeight = g != null ? g.height : 1;

      g = getGlyph('M');
      capHeight = g != null ? g.height : 1;

      ascent = baseLine - capHeight;
      down = -lineHeight;
      if (flip) {
        ascent = -ascent;
        down = -down;
      }
    } catch (Exception ex) {
      throw new GdxRuntimeException("Error loading font file: " + fontFile, ex);
    } finally {
      try {
        reader.close();
      } catch (IOException ignored) {
      }
    }
  }