gdx-studio
git clone https://git.pyrossh.dev/gdx-studio
An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop
src_libs/com/badlogic/gdx/tools/imagepacker/TexturePackerFileProcessor.java
| a62d533 | 1 | /******************************************************************************* |
| a62d533 | 2 | * Copyright 2011 See AUTHORS file. |
| a62d533 | 3 | * |
| a62d533 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| a62d533 | 5 | * you may not use this file except in compliance with the License. |
| a62d533 | 6 | * You may obtain a copy of the License at |
| a62d533 | 7 | * |
| a62d533 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| a62d533 | 9 | * |
| a62d533 | 10 | * Unless required by applicable law or agreed to in writing, software |
| a62d533 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| a62d533 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| a62d533 | 13 | * See the License for the specific language governing permissions and |
| a62d533 | 14 | * limitations under the License. |
| a62d533 | 15 | ******************************************************************************/ |
| a62d533 | 16 | |
| a62d533 | 17 | package com.badlogic.gdx.tools.imagepacker; |
| a62d533 | 18 | |
| a62d533 | 19 | import com.badlogic.gdx.tools.FileProcessor; |
| a62d533 | 20 | import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Settings; |
| a62d533 | 21 | import com.badlogic.gdx.utils.GdxRuntimeException; |
| a62d533 | 22 | import com.badlogic.gdx.utils.Json; |
| a62d533 | 23 | import com.badlogic.gdx.utils.JsonReader; |
| a62d533 | 24 | import com.badlogic.gdx.utils.ObjectMap; |
| a62d533 | 25 | import com.badlogic.gdx.utils.SerializationException; |
| a62d533 | 26 | |
| a62d533 | 27 | import java.io.File; |
| a62d533 | 28 | import java.io.FileReader; |
| a62d533 | 29 | import java.util.ArrayList; |
| a62d533 | 30 | import java.util.Collections; |
| a62d533 | 31 | import java.util.Comparator; |
| a62d533 | 32 | import java.util.regex.Pattern; |
| a62d533 | 33 | |
| a62d533 | 34 | /** @author Nathan Sweet */ |
| a62d533 | 35 | public class TexturePackerFileProcessor extends FileProcessor {
|
| a62d533 | 36 | private final Settings defaultSettings; |
| a62d533 | 37 | private ObjectMap<File, Settings> dirToSettings = new ObjectMap(); |
| a62d533 | 38 | private Json json = new Json(); |
| a62d533 | 39 | private String packFileName; |
| a62d533 | 40 | private File root; |
| a62d533 | 41 | ArrayList<File> ignoreDirs = new ArrayList(); |
| a62d533 | 42 | |
| a62d533 | 43 | public TexturePackerFileProcessor () {
|
| a62d533 | 44 | this(new Settings(), "pack.atlas"); |
| a62d533 | 45 | } |
| a62d533 | 46 | |
| a62d533 | 47 | public TexturePackerFileProcessor (Settings defaultSettings, String packFileName) {
|
| a62d533 | 48 | this.defaultSettings = defaultSettings; |
| a62d533 | 49 | |
| a62d533 | 50 | if (packFileName.indexOf('.') == -1) packFileName += ".atlas";
|
| a62d533 | 51 | this.packFileName = packFileName; |
| a62d533 | 52 | |
| a62d533 | 53 | setFlattenOutput(true); |
| a62d533 | 54 | addInputSuffix(".png", ".jpg");
|
| a62d533 | 55 | } |
| a62d533 | 56 | |
| a62d533 | 57 | public ArrayList<Entry> process (File inputFile, File outputRoot) throws Exception {
|
| a62d533 | 58 | root = inputFile; |
| a62d533 | 59 | |
| a62d533 | 60 | // Collect pack.json setting files. |
| a62d533 | 61 | final ArrayList<File> settingsFiles = new ArrayList(); |
| a62d533 | 62 | FileProcessor settingsProcessor = new FileProcessor() {
|
| a62d533 | 63 | protected void processFile (Entry inputFile) throws Exception {
|
| a62d533 | 64 | settingsFiles.add(inputFile.inputFile); |
| a62d533 | 65 | } |
| a62d533 | 66 | }; |
| a62d533 | 67 | settingsProcessor.addInputRegex("pack\\.json");
|
| a62d533 | 68 | settingsProcessor.process(inputFile, null); |
| a62d533 | 69 | // Sort parent first. |
| a62d533 | 70 | Collections.sort(settingsFiles, new Comparator<File>() {
|
| a62d533 | 71 | public int compare (File file1, File file2) {
|
| a62d533 | 72 | return file1.toString().length() - file2.toString().length(); |
| a62d533 | 73 | } |
| a62d533 | 74 | }); |
| a62d533 | 75 | for (File settingsFile : settingsFiles) {
|
| a62d533 | 76 | // Find first parent with settings, or use defaults. |
| a62d533 | 77 | Settings settings = null; |
| a62d533 | 78 | File parent = settingsFile.getParentFile(); |
| a62d533 | 79 | while (true) {
|
| a62d533 | 80 | if (parent.equals(root)) break; |
| a62d533 | 81 | parent = parent.getParentFile(); |
| a62d533 | 82 | settings = dirToSettings.get(parent); |
| a62d533 | 83 | if (settings != null) {
|
| a62d533 | 84 | settings = new Settings(settings); |
| a62d533 | 85 | break; |
| a62d533 | 86 | } |
| a62d533 | 87 | } |
| a62d533 | 88 | if (settings == null) settings = new Settings(defaultSettings); |
| a62d533 | 89 | // Merge settings from current directory. |
| a62d533 | 90 | try {
|
| a62d533 | 91 | json.readFields(settings, new JsonReader().parse(new FileReader(settingsFile))); |
| a62d533 | 92 | } catch (SerializationException ex) {
|
| a62d533 | 93 | throw new GdxRuntimeException("Error reading settings file: " + settingsFile, ex);
|
| a62d533 | 94 | } |
| a62d533 | 95 | dirToSettings.put(settingsFile.getParentFile(), settings); |
| a62d533 | 96 | } |
| a62d533 | 97 | |
| a62d533 | 98 | // Do actual processing. |
| a62d533 | 99 | return super.process(inputFile, outputRoot); |
| a62d533 | 100 | } |
| a62d533 | 101 | |
| a62d533 | 102 | public ArrayList<Entry> process (File[] files, File outputRoot) throws Exception {
|
| a62d533 | 103 | // Delete pack file and images. |
| a62d533 | 104 | if (outputRoot.exists()) {
|
| a62d533 | 105 | new File(outputRoot, packFileName).delete(); |
| a62d533 | 106 | FileProcessor deleteProcessor = new FileProcessor() {
|
| a62d533 | 107 | protected void processFile (Entry inputFile) throws Exception {
|
| a62d533 | 108 | inputFile.inputFile.delete(); |
| a62d533 | 109 | } |
| a62d533 | 110 | }; |
| a62d533 | 111 | deleteProcessor.setRecursive(false); |
| a62d533 | 112 | |
| a62d533 | 113 | String prefix = packFileName; |
| a62d533 | 114 | int dotIndex = prefix.lastIndexOf('.');
|
| a62d533 | 115 | if (dotIndex != -1) prefix = prefix.substring(0, dotIndex); |
| a62d533 | 116 | deleteProcessor.addInputRegex(Pattern.quote(prefix) + "\\d*\\.(png|jpg)"); |
| a62d533 | 117 | |
| a62d533 | 118 | deleteProcessor.process(outputRoot, null); |
| a62d533 | 119 | } |
| a62d533 | 120 | return super.process(files, outputRoot); |
| a62d533 | 121 | } |
| a62d533 | 122 | |
| a62d533 | 123 | protected void processDir (Entry inputDir, ArrayList<Entry> files) throws Exception {
|
| a62d533 | 124 | if (ignoreDirs.contains(inputDir.inputFile)) return; |
| a62d533 | 125 | |
| a62d533 | 126 | // Find first parent with settings, or use defaults. |
| a62d533 | 127 | Settings settings = null; |
| a62d533 | 128 | File parent = inputDir.inputFile; |
| a62d533 | 129 | while (true) {
|
| a62d533 | 130 | settings = dirToSettings.get(parent); |
| a62d533 | 131 | if (settings != null) break; |
| a62d533 | 132 | if (parent.equals(root)) break; |
| a62d533 | 133 | parent = parent.getParentFile(); |
| a62d533 | 134 | } |
| a62d533 | 135 | if (settings == null) settings = defaultSettings; |
| a62d533 | 136 | |
| a62d533 | 137 | if (settings.combineSubdirectories) {
|
| a62d533 | 138 | // Collect all files under subdirectories and ignore subdirectories so they won't be packed twice. |
| a62d533 | 139 | files = new FileProcessor(this) {
|
| a62d533 | 140 | protected void processDir (Entry entryDir, ArrayList<Entry> files) {
|
| a62d533 | 141 | ignoreDirs.add(entryDir.inputFile); |
| a62d533 | 142 | } |
| a62d533 | 143 | |
| a62d533 | 144 | protected void processFile (Entry entry) {
|
| a62d533 | 145 | addProcessedFile(entry); |
| a62d533 | 146 | } |
| a62d533 | 147 | }.process(inputDir.inputFile, null); |
| a62d533 | 148 | } |
| a62d533 | 149 | |
| a62d533 | 150 | if (files.isEmpty()) return; |
| a62d533 | 151 | |
| a62d533 | 152 | // Pack. |
| a62d533 | 153 | System.out.println(inputDir.inputFile.getName()); |
| a62d533 | 154 | TexturePacker2 packer = new TexturePacker2(root, settings); |
| a62d533 | 155 | for (Entry file : files) |
| a62d533 | 156 | packer.addImage(file.inputFile); |
| a62d533 | 157 | packer.pack(inputDir.outputDir, packFileName); |
| a62d533 | 158 | } |
| a62d533 | 159 | } |