Esempio n. 1
0
  // Загрузка локализации из файла
  public boolean LoadFromInternalXML(String locale, String internalPath) {
    strings.clear();
    boolean result = false;
    XmlReader reader = new XmlReader();
    try {
      XmlReader.Element root = reader.parse(Gdx.files.internal(internalPath));
      if (root != null) {
        // Проверка на правильность формата файла локализации
        if (root.getName().equalsIgnoreCase("localization")) {
          int load_count = 0;
          // Перебор всех дочерних элементов
          for (int i = 0; i < root.getChildCount(); i++) {
            XmlReader.Element item = root.getChild(i);
            if (item != null) {
              if (item.getName().equalsIgnoreCase("string")) {
                String key = item.getAttribute("key").trim();
                String default_string;
                String value;
                if (key.length() > 0) {
                  Array<XmlReader.Element> find = item.getChildrenByName("default");
                  if (find.size > 0) default_string = find.get(0).getText();
                  else default_string = item.getAttribute("default", "");

                  find = item.getChildrenByName(locale);
                  if (find.size > 0) value = find.get(0).getText();
                  else value = default_string;

                  find.clear();
                  if (value.length() > 0) {
                    strings.put(key, value);

                    if (default_string.length() > 0) translates.put(default_string, value);

                    load_count++;
                  } else {
                    if (default_string.length() > 0) {
                      strings.put(key, default_string);
                      load_count++;
                    }
                  }
                }
              }
            }
          }
          if (load_count > 0) result = true;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      strings.clear();
      result = false;
    }
    return result;
  }
Esempio n. 2
0
  public TiledMap load(String fileName, AtlasTiledMapLoaderParameters parameter) {
    try {
      if (parameter != null) {
        yUp = parameter.yUp;
      } else {
        yUp = true;
      }

      FileHandle tmxFile = resolve(fileName);
      root = xml.parse(tmxFile);
      ObjectMap<String, TextureAtlas> atlases = new ObjectMap<String, TextureAtlas>();
      FileHandle atlasFile = loadAtlas(root, tmxFile);
      if (atlasFile == null) {
        throw new GdxRuntimeException("Couldn't load atlas");
      }

      TextureAtlas atlas = new TextureAtlas(atlasFile);
      atlases.put(atlasFile.path(), atlas);

      AtlasResolver.DirectAtlasResolver atlasResolver =
          new AtlasResolver.DirectAtlasResolver(atlases);
      TiledMap map = loadMap(root, tmxFile, atlasResolver, parameter);
      map.setOwnedResources(atlases.values().toArray());
      setTextureFilters(parameter.textureMinFilter, parameter.textureMagFilter);

      return map;
    } catch (IOException e) {
      throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
    }
  }
Esempio n. 3
0
  @Override
  public Array<AssetDescriptor> getDependencies(
      String fileName, FileHandle fileHandle, AtlasTiledMapLoaderParameters parameter) {
    Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
    FileHandle tmxFile = resolve(fileName);
    try {
      root = xml.parse(tmxFile);

      Element properties = root.getChildByName("properties");
      if (properties != null) {
        for (Element property : properties.getChildrenByName("property")) {
          String name = property.getAttribute("name");
          String value = property.getAttribute("value");
          if (name.startsWith("atlas")) {
            FileHandle atlasHandle = getRelativeFileHandle(tmxFile, value);
            atlasHandle = resolve(atlasHandle.path());
            dependencies.add(new AssetDescriptor(atlasHandle.path(), TextureAtlas.class));
          }
        }
      }
    } catch (IOException e) {
      throw new GdxRuntimeException("Unable to parse .tmx file.");
    }
    return dependencies;
  }
  public String LoadFile(String filename) {
    FileHandle xmlFile = Gdx.files.internal("scenes/" + filename);

    String script = xmlFile.readString();
    XmlReader xml = new XmlReader();
    xml_element = xml.parse(script);

    String startSceneName = xml_element.getAttribute("startScene", null);

    if (startSceneName == null) {
      Array<XmlReader.Element> scenes = xml_element.getChildrenByName("scene");

      startSceneName = scenes.get(0).get("name");
    }

    return startSceneName;
  }
Esempio n. 5
0
 public XMLReader(String file, String RootKey) {
   reader = new XmlReader();
   try {
     ROOT = reader.parse(Gdx.files.internal(file));
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
  public void LoadStage() {
    FileHandle xmlFile = Gdx.files.internal("scenes/index.xml");

    String script = xmlFile.readString();
    XmlReader xml = new XmlReader();
    xml_element = xml.parse(script);

    String startSceneName = xml_element.getAttribute("startScene", null);
    String skinPath = xml_element.getAttribute("skin", null);

    assetService.setCurrentSkinPath(skinPath);

    assetService.loadAssets();

    if (startSceneName == null) {
      Array<XmlReader.Element> scenes = xml_element.getChildrenByName("scene");

      startSceneName = scenes.get(0).get("name");
    }

    RenderScene(startSceneName);
  }
Esempio n. 7
0
 @Override
 public Array<AssetDescriptor> getDependencies(
     String fileName, FileHandle tmxFile, Parameters parameter) {
   Array<AssetDescriptor> dependencies = new Array<AssetDescriptor>();
   try {
     root = xml.parse(tmxFile);
     for (FileHandle image : loadTileSheets(root, tmxFile)) {
       dependencies.add(new AssetDescriptor(image.path(), Texture.class));
     }
     return dependencies;
   } catch (IOException e) {
     throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
   }
 }
Esempio n. 8
0
  @Override
  public void parse(Element xmlElement) {
    String path = xmlElement.getAttribute("Path");

    XmlReader importXml = new XmlReader();
    Element importXmlElement = null;

    try {
      importXmlElement = importXml.parse(Gdx.files.internal("AI/" + path));
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      Class<BehaviourTreeNode> c = ClassMap.get(importXmlElement.getName().toUpperCase());
      BehaviourTreeNode node = (BehaviourTreeNode) ClassReflection.newInstance(c);

      setNode(node);

      node.parse(importXmlElement);
    } catch (ReflectionException e) {
      e.printStackTrace();
    }
  }
Esempio n. 9
0
 public TiledMap load(String fileName) {
   try {
     FileHandle tideFile = resolve(fileName);
     root = xml.parse(tideFile);
     ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
     for (FileHandle textureFile : loadTileSheets(root, tideFile)) {
       textures.put(textureFile.path(), new Texture(textureFile));
     }
     DirectImageResolver imageResolver = new DirectImageResolver(textures);
     TiledMap map = loadMap(root, tideFile, imageResolver);
     map.setOwnedResources(textures.values().toArray());
     return map;
   } catch (IOException e) {
     throw new GdxRuntimeException("Couldn't load tilemap '" + fileName + "'", e);
   }
 }
Esempio n. 10
0
  protected void loadTileset(
      TiledMap map,
      Element element,
      FileHandle tmxFile,
      AtlasResolver resolver,
      AtlasTiledMapLoaderParameters parameter) {
    if (element.getName().equals("tileset")) {
      String name = element.get("name", null);
      int firstgid = element.getIntAttribute("firstgid", 1);
      int tilewidth = element.getIntAttribute("tilewidth", 0);
      int tileheight = element.getIntAttribute("tileheight", 0);
      int spacing = element.getIntAttribute("spacing", 0);
      int margin = element.getIntAttribute("margin", 0);
      String source = element.getAttribute("source", null);

      String imageSource = "";
      int imageWidth = 0, imageHeight = 0;

      FileHandle image = null;
      if (source != null) {
        FileHandle tsx = getRelativeFileHandle(tmxFile, source);
        try {
          element = xml.parse(tsx);
          name = element.get("name", null);
          tilewidth = element.getIntAttribute("tilewidth", 0);
          tileheight = element.getIntAttribute("tileheight", 0);
          spacing = element.getIntAttribute("spacing", 0);
          margin = element.getIntAttribute("margin", 0);
          imageSource = element.getChildByName("image").getAttribute("source");
          imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
          imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
        } catch (IOException e) {
          throw new GdxRuntimeException("Error parsing external tileset.");
        }
      } else {
        imageSource = element.getChildByName("image").getAttribute("source");
        imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
        imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
      }

      // get the TextureAtlas for this tileset
      TextureAtlas atlas = null;
      String regionsName = "";
      if (map.getProperties().containsKey("atlas")) {
        FileHandle atlasHandle =
            getRelativeFileHandle(tmxFile, map.getProperties().get("atlas", String.class));
        atlasHandle = resolve(atlasHandle.path());
        atlas = resolver.getAtlas(atlasHandle.path());
        regionsName = atlasHandle.nameWithoutExtension();

        if (parameter != null && parameter.forceTextureFilters) {
          for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
          }
        }
      }

      TiledMapTileSet tileset = new TiledMapTileSet();
      MapProperties props = tileset.getProperties();
      tileset.setName(name);
      props.put("firstgid", firstgid);
      props.put("imagesource", imageSource);
      props.put("imagewidth", imageWidth);
      props.put("imageheight", imageHeight);
      props.put("tilewidth", tilewidth);
      props.put("tileheight", tileheight);
      props.put("margin", margin);
      props.put("spacing", spacing);

      Array<AtlasRegion> regions = atlas.findRegions(regionsName);
      for (AtlasRegion region : regions) {
        // handle unused tile ids
        if (region != null) {
          StaticTiledMapTile tile = new StaticTiledMapTile(region);

          if (!yUp) {
            region.flip(false, true);
          }

          int tileid = firstgid + region.index;
          tile.setId(tileid);
          tileset.putTile(tileid, tile);
        }
      }

      Array<Element> tileElements = element.getChildrenByName("tile");

      for (Element tileElement : tileElements) {
        int localtid = tileElement.getIntAttribute("id", 0);
        TiledMapTile tile = tileset.getTile(firstgid + localtid);
        if (tile != null) {
          String terrain = tileElement.getAttribute("terrain", null);
          if (terrain != null) {
            tile.getProperties().put("terrain", terrain);
          }
          String probability = tileElement.getAttribute("probability", null);
          if (probability != null) {
            tile.getProperties().put("probability", probability);
          }
          Element properties = tileElement.getChildByName("properties");
          if (properties != null) {
            loadProperties(tile.getProperties(), properties);
          }
        }
      }

      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }
      map.getTileSets().addTileSet(tileset);
    }
  }
Esempio n. 11
0
  public Animation read(FileHandle handle) {
    XmlReader rdr = new XmlReader();
    try {

      Element root = rdr.parse(handle);
      int frameRate = Integer.parseInt(root.getChildByName("FrameRate").getText());
      int loopFrame = Integer.parseInt(root.getChildByName("LoopFrame").getText());

      Animation anim = new Animation();
      anim.FrameRate = frameRate;
      anim.LoopFrame = loopFrame;
      anim.Textures = new ArrayList<TextureEntry>();
      anim.Loop = loopFrame != -1;
      anim.Keyframes = new ArrayList<Keyframe>();

      for (int i = 0; i < root.getChildCount(); ++i) {
        Element ch = root.getChild(i);
        if (ch.getName().equals("Texture")) {
          TextureRegion reg = imgSrc.getImage(ch.getText());
          if (reg != null) {
            TextureAtlas.AtlasRegion r = (TextureAtlas.AtlasRegion) reg;
            anim.Textures.add(
                new TextureEntry(
                    reg,
                    new TextureBounds(
                        new Rectangle(0, 0, r.originalWidth, r.originalHeight),
                        new Vector2(r.originalWidth / 2, r.originalHeight / 2))));
          } else {
            throw new Exception("Unable to resolve image: " + ch.getText());
          }
        }
      }

      for (int i = 0; i < root.getChildCount(); ++i) {
        Element ch = root.getChild(i);
        if (ch.getName().equals("Keyframe")) {
          Keyframe frame = new Keyframe();
          frame.Bones = new ArrayList<Bone>();
          frame.FrameNumber = ch.getIntAttribute("frame");
          frame.Trigger = ch.getAttribute("trigger", "");
          frame.FlipHorizontally = ch.getAttribute("hflip", "False").equals("True");
          frame.FlipVertically = ch.getAttribute("vflip", "False").equals("True");

          for (int j = 0; j < ch.getChildCount(); ++j) {
            Element bone = ch.getChild(j);
            if (bone.getName().equals("Bone")) {
              Element posElem = bone.getChildByName("Position");
              Element sclElem = bone.getChildByName("Scale");
              Vector2 pos = new Vector2();
              Vector2 scl = new Vector2();

              pos.x = Float.parseFloat(posElem.getChildByName("X").getText());
              pos.y = Float.parseFloat(posElem.getChildByName("Y").getText());

              scl.x = Float.parseFloat(sclElem.getChildByName("X").getText());
              scl.y = Float.parseFloat(sclElem.getChildByName("Y").getText());

              Bone b = new Bone();
              b.Hidden = bone.getChildByName("Hidden").getText().equals("True");
              b.Name = bone.getAttribute("name");
              b.TextureFlipHorizontal =
                  bone.getChildByName("TextureFlipHorizontal").getText().equals("True");
              b.TextureFlipVertical =
                  bone.getChildByName("TextureFlipVertical").getText().equals("True");
              b.ParentIndex = Integer.parseInt(bone.getChildByName("ParentIndex").getText());
              b.TextureIndex = Integer.parseInt(bone.getChildByName("TextureIndex").getText());
              b.Rotation = Float.parseFloat(bone.getChildByName("Rotation").getText());

              b.Position = pos;
              b.Scale = scl;
              b.SelfIndex = j;

              frame.Bones.add(b);
            }
          }

          frame.SortBones();
          anim.Keyframes.add(frame);
        }

        float fr = 1.0f / anim.FrameRate;
        anim.LoopTime = anim.LoopFrame * anim.FrameRate;
        anim.Loop = anim.LoopFrame != -1;
        for (Keyframe kf : anim.Keyframes) {
          kf.FrameTime = fr * kf.FrameNumber;
        }
      }

      return anim;
    } catch (Exception ex) {
      Gdx.app.log("AnimationReader", "read", ex);
      return null;
    }
  }