Guest User

Untitled

a guest
Jun 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import javax.xml.bind.JAXB;
  2. import javax.xml.bind.JAXBContext;
  3. import javax.xml.bind.JAXBException;
  4. import javax.xml.bind.Unmarshaller;
  5. import javax.xml.bind.annotation.*;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.nio.file.*;
  9. import java.nio.file.attribute.BasicFileAttributes;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Set;
  13. import java.util.TreeSet;
  14.  
  15. /**
  16. * <p>Created on 17.12.2014</p>
  17. *
  18. * @author NooBxGockeL
  19. */
  20. public class KenneyToLibGdxAtlasConverter implements Runnable {
  21.  
  22. private final FileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {
  23.  
  24. @Override
  25. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  26. FileVisitResult fileVisitResult = super.visitFile(file, attrs);
  27. String fileName = file.getFileName().toString();
  28. if (fileName.endsWith(".xml")) {
  29. convertKennyXML(file);
  30. }
  31. return fileVisitResult;
  32. }
  33. };
  34.  
  35. private static final String NEW_LINE = "\n";
  36. private static final int MAX_DEPTH = 10;
  37. private final boolean stripEndings;
  38. private final Path rootPath;
  39. private final Unmarshaller unmarshaller;
  40.  
  41. public KenneyToLibGdxAtlasConverter(boolean stripEndings, Path rootPath) throws JAXBException {
  42. this.stripEndings = stripEndings;
  43. this.rootPath = rootPath;
  44. this.unmarshaller = JAXBContext.newInstance(TextureAtlas.class).createUnmarshaller();
  45. }
  46.  
  47. @Override
  48. public void run() {
  49. try {
  50. System.out.println("=== CONVERSION PROCESS STARTED ===");
  51. Files.walkFileTree(rootPath, createSetOf(FileVisitOption.FOLLOW_LINKS), MAX_DEPTH, fileVisitor);
  52. System.out.println("=== CONVERSION PROCESS FINISHED ===");
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. System.out.println("=== CONVERSION PROCESS ABORTED ===");
  56. }
  57. }
  58.  
  59. private void convertKennyXML(Path file) throws IOException {
  60. System.out.println("converting [" + file.toString() + "]");
  61.  
  62. InputStream inputStream = Files.newInputStream(file, StandardOpenOption.READ);
  63.  
  64. TextureAtlas atlas = JAXB.unmarshal(inputStream, TextureAtlas.class);
  65.  
  66. StringBuilder sb = new StringBuilder();
  67. String name = file.getFileName().toString();
  68.  
  69. sb.append(String.format("%1$s\n" +
  70. "format: RGBA8888\n" +
  71. "filter: Linear,Linear\n" +
  72. "repeat: none\n",
  73. name.substring(0,name.length()-4)+".png "));
  74.  
  75. for (SubTexture subTexture : atlas.subTextureList) {
  76. String subTextureName = subTexture.name;
  77. if (stripEndings) {
  78. subTextureName = subTextureName.substring(0, subTextureName.lastIndexOf('.'));
  79. }
  80. sb.append(String.format("%1$s\n" +
  81. " rotate: false\n" +
  82. " xy: %2$s, %3$s\n" +
  83. " size: %4$s, %5$s\n" +
  84. " orig: %4$s, %5$s\n" +
  85. " offset: 0, 0\n" +
  86. " index: -1\n",
  87. subTextureName, subTexture.x, subTexture.y, subTexture.width, subTexture.height));
  88.  
  89. }
  90.  
  91. String fileName = file.getFileName().toString();
  92. Path newPath = file.getParent().resolve(fileName.substring(0, fileName.length() - 4) + ".atlas");
  93. Files.write(newPath, sb.toString().getBytes(), StandardOpenOption.CREATE);
  94.  
  95. System.out.println("done [" + newPath.toString() + "]");
  96. }
  97.  
  98.  
  99. private static <E> Set<E> createSetOf(E... elements) {
  100. return new TreeSet<E>(Arrays.asList(elements));
  101. }
  102.  
  103. private static void printUsage() {
  104. System.out.println("ONLY ONE PARAMETER: Path to directory in which to search for");
  105. }
  106.  
  107. public static void main(String[] args) {
  108. // Uncomment next line for running inside of eclipse / IDE
  109. //args = new String[] { "E:\\Projects\\_Important\\Resources\\Kenny (Assets)\\Donation Pack v16" };
  110. args = new String[] {"C:\\Users\\SPL\\kirakos_game\\android\\assets\\Spritesheet"};
  111. if (args.length != 1) {
  112. printUsage();
  113. }
  114. try {
  115. KenneyToLibGdxAtlasConverter converter = new KenneyToLibGdxAtlasConverter(
  116. true, Paths.get(args[0]));
  117. converter.run();
  118. } catch (JAXBException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. @XmlAccessorType(XmlAccessType.FIELD)
  124. static public class SubTexture {
  125.  
  126. @XmlAttribute
  127. public String name;
  128.  
  129. @XmlAttribute
  130. public String x;
  131.  
  132. @XmlAttribute
  133. public String y;
  134.  
  135. @XmlAttribute
  136. public String width;
  137.  
  138. @XmlAttribute
  139. public String height;
  140. }
  141.  
  142. @XmlRootElement
  143. @XmlAccessorType(XmlAccessType.FIELD)
  144. static public class TextureAtlas {
  145.  
  146. @XmlAttribute
  147. public String imagePath;
  148.  
  149. @XmlElement(name = "SubTexture")
  150. public List<SubTexture> subTextureList;
  151.  
  152. }
  153. }
Add Comment
Please, Sign In to add comment