Search in sources :

Example 11 with Settings

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings in project Eidolons by IDemiurge.

the class TexturePackerLaunch method packImages.

private static void packImages(String[] folders) {
    Settings settings = getSetting();
    for (String sub : folders) {
        // List<File> files = FileManager.getFilesFromDirectory(PathFinder.getImagePath() + sub, false, true);
        // for (File folder : files) {
        String inputDir = PathFinder.getImagePath() + sub;
        // Math.sqrt(files.size())
        String outputDir = inputDir;
        String packFileName = sub;
        TexturePacker.process(settings, inputDir, outputDir, packFileName);
    // }
    }
}
Also used : Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Example 12 with Settings

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings in project libgdx by libgdx.

the class TexturePackerFileProcessor method processDir.

protected void processDir(Entry inputDir, ArrayList<Entry> files) throws Exception {
    if (ignoreDirs.contains(inputDir.inputFile))
        return;
    // Find first parent with settings, or use defaults.
    Settings settings = null;
    File parent = inputDir.inputFile;
    while (true) {
        settings = dirToSettings.get(parent);
        if (settings != null)
            break;
        if (parent == null || parent.equals(root))
            break;
        parent = parent.getParentFile();
    }
    if (settings == null)
        settings = defaultSettings;
    if (settings.ignore)
        return;
    if (settings.combineSubdirectories) {
        // Collect all files under subdirectories and ignore subdirectories so they won't be packed twice.
        files = new FileProcessor(this) {

            protected void processDir(Entry entryDir, ArrayList<Entry> files) {
                ignoreDirs.add(entryDir.inputFile);
            }

            protected void processFile(Entry entry) {
                addProcessedFile(entry);
            }
        }.process(inputDir.inputFile, null);
    }
    if (files.isEmpty())
        return;
    // Sort by name using numeric suffix, then alpha.
    Collections.sort(files, new Comparator<Entry>() {

        final Pattern digitSuffix = Pattern.compile("(.*?)(\\d+)$");

        public int compare(Entry entry1, Entry entry2) {
            String full1 = entry1.inputFile.getName();
            int dotIndex = full1.lastIndexOf('.');
            if (dotIndex != -1)
                full1 = full1.substring(0, dotIndex);
            String full2 = entry2.inputFile.getName();
            dotIndex = full2.lastIndexOf('.');
            if (dotIndex != -1)
                full2 = full2.substring(0, dotIndex);
            String name1 = full1, name2 = full2;
            int num1 = 0, num2 = 0;
            Matcher matcher = digitSuffix.matcher(full1);
            if (matcher.matches()) {
                try {
                    num1 = Integer.parseInt(matcher.group(2));
                    name1 = matcher.group(1);
                } catch (Exception ignored) {
                }
            }
            matcher = digitSuffix.matcher(full2);
            if (matcher.matches()) {
                try {
                    num2 = Integer.parseInt(matcher.group(2));
                    name2 = matcher.group(1);
                } catch (Exception ignored) {
                }
            }
            int compare = name1.compareTo(name2);
            if (compare != 0 || num1 == num2)
                return compare;
            return num1 - num2;
        }
    });
    // Pack.
    if (!settings.silent)
        System.out.println(inputDir.inputFile.getName());
    TexturePacker packer = new TexturePacker(root, settings);
    for (Entry file : files) packer.addImage(file.inputFile);
    packer.pack(inputDir.outputDir, packFileName);
}
Also used : FileProcessor(com.badlogic.gdx.tools.FileProcessor) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) File(java.io.File) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 13 with Settings

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings in project libgdx by libgdx.

the class TexturePackerTest method render.

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Settings settings = new Settings();
    settings.fast = false;
    settings.pot = false;
    settings.maxWidth = 1024;
    settings.maxHeight = 1024;
    settings.rotation = false;
    settings.paddingX = 0;
    if (pages == null) {
        Random random = new Random(1243);
        Array<Rect> inputRects = new Array();
        for (int i = 0; i < 240; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + i;
            rect.height = 16 + random.nextInt(120);
            rect.width = 16 + random.nextInt(240);
            inputRects.add(rect);
        }
        for (int i = 0; i < 10; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + (40 + i);
            rect.height = 400 + random.nextInt(340);
            rect.width = 1 + random.nextInt(10);
            inputRects.add(rect);
        }
        long s = System.nanoTime();
        pages = new MaxRectsPacker(settings).pack(inputRects);
        long e = System.nanoTime();
        System.out.println("fast: " + settings.fast);
        System.out.println((e - s) / 1e6f + " ms");
        System.out.println();
    }
    int x = 20, y = 20;
    for (Page page : pages) {
        renderer.setColor(Color.GRAY);
        renderer.begin(ShapeType.Filled);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.end();
        renderer.setColor(Color.RED);
        renderer.begin(ShapeType.Line);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.setColor(Color.GREEN);
        renderer.rect(x, y, page.width + settings.paddingX * 2, page.height + settings.paddingY * 2);
        renderer.end();
        x += page.width + 20;
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Random(java.util.Random) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Example 14 with Settings

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings in project skin-composer by raeleus.

the class TexturePackerFileProcessor method process.

public ArrayList<Entry> process(File inputFile, File outputRoot) throws Exception {
    root = inputFile;
    // Collect pack.json setting files.
    final ArrayList<File> settingsFiles = new ArrayList();
    FileProcessor settingsProcessor = new FileProcessor() {

        protected void processFile(Entry inputFile) throws Exception {
            settingsFiles.add(inputFile.inputFile);
        }
    };
    settingsProcessor.addInputRegex("pack\\.json");
    settingsProcessor.process(inputFile, null);
    // Sort parent first.
    Collections.sort(settingsFiles, new Comparator<File>() {

        public int compare(File file1, File file2) {
            return file1.toString().length() - file2.toString().length();
        }
    });
    for (File settingsFile : settingsFiles) {
        // Find first parent with settings, or use defaults.
        Settings settings = null;
        File parent = settingsFile.getParentFile();
        while (true) {
            if (parent.equals(root))
                break;
            parent = parent.getParentFile();
            settings = dirToSettings.get(parent);
            if (settings != null) {
                settings = new Settings(settings);
                break;
            }
        }
        if (settings == null)
            settings = new Settings(defaultSettings);
        // Merge settings from current directory.
        merge(settings, settingsFile);
        dirToSettings.put(settingsFile.getParentFile(), settings);
    }
    // Do actual processing.
    return super.process(inputFile, outputRoot);
}
Also used : FileProcessor(com.badlogic.gdx.tools.FileProcessor) ArrayList(java.util.ArrayList) File(java.io.File) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Example 15 with Settings

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings in project skin-composer by raeleus.

the class TexturePackerFileProcessor method process.

public ArrayList<Entry> process(File[] files, File outputRoot) throws Exception {
    // Delete pack file and images.
    if (outputRoot.exists()) {
        // Load root settings to get scale.
        File settingsFile = new File(root, "pack.json");
        Settings rootSettings = defaultSettings;
        if (settingsFile.exists()) {
            rootSettings = new Settings(rootSettings);
            merge(rootSettings, settingsFile);
        }
        String atlasExtension = rootSettings.atlasExtension == null ? "" : rootSettings.atlasExtension;
        atlasExtension = Pattern.quote(atlasExtension);
        for (int i = 0, n = rootSettings.scale.length; i < n; i++) {
            FileProcessor deleteProcessor = new FileProcessor() {

                protected void processFile(Entry inputFile) throws Exception {
                    inputFile.inputFile.delete();
                }
            };
            deleteProcessor.setRecursive(false);
            String scaledPackFileName = rootSettings.getScaledPackFileName(packFileName, i);
            File packFile = new File(scaledPackFileName);
            String prefix = packFile.getName();
            int dotIndex = prefix.lastIndexOf('.');
            if (dotIndex != -1)
                prefix = prefix.substring(0, dotIndex);
            deleteProcessor.addInputRegex("(?i)" + prefix + "\\d*\\.(png|jpg|jpeg)");
            deleteProcessor.addInputRegex("(?i)" + prefix + atlasExtension);
            String dir = packFile.getParent();
            if (dir == null)
                deleteProcessor.process(outputRoot, null);
            else if (// 
            new File(outputRoot + "/" + dir).exists())
                deleteProcessor.process(outputRoot + "/" + dir, null);
        }
    }
    return super.process(files, outputRoot);
}
Also used : FileProcessor(com.badlogic.gdx.tools.FileProcessor) File(java.io.File) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Aggregations

Settings (com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)22 File (java.io.File)17 FileProcessor (com.badlogic.gdx.tools.FileProcessor)9 ArrayList (java.util.ArrayList)7 TexturePacker (com.badlogic.gdx.tools.texturepacker.TexturePacker)5 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)4 BufferedImage (java.awt.image.BufferedImage)4 TreeMap (java.util.TreeMap)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)2 Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)2 Array (com.badlogic.gdx.utils.Array)2 IOException (java.io.IOException)2 Random (java.util.Random)2 ApplicationListener (com.badlogic.gdx.ApplicationListener)1 LwjglApplication (com.badlogic.gdx.backends.lwjgl.LwjglApplication)1 LwjglApplicationConfiguration (com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration)1 FileHandle (com.badlogic.gdx.files.FileHandle)1 Json (com.badlogic.gdx.utils.Json)1