Search in sources :

Example 91 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogTenPatch method populate.

private void populate() {
    setSize(stage.getWidth(), stage.getHeight());
    var root = getContentTable();
    root.pad(10);
    root.clear();
    var top = new Table();
    top.setTouchable(Touchable.enabled);
    var bottom = new Table();
    bottom.setBackground(skin.getDrawable("white"));
    bottom.setTouchable(Touchable.enabled);
    var splitPane = new SplitPane(top, bottom, true, skin);
    splitPane.setSplitAmount(splitValue);
    root.add(splitPane).grow();
    splitPane.addListener(verticalResizeArrowListener);
    splitPane.addListener(new DragListener() {

        {
            setTapSquareSize(0f);
        }

        @Override
        public void drag(InputEvent event, float x, float y, int pointer) {
            super.drag(event, x, y, pointer);
            if (event.getListenerActor().equals(event.getTarget())) {
                tenPatchWidget.center();
            }
            splitValue = splitPane.getSplitAmount();
        }
    });
    var label = new Label("Ten Patch Editor", skin, "title-no-line");
    top.add(label);
    top.row();
    var table = new Table();
    top.add(table).growX().space(5);
    table.defaults().space(5);
    label = new Label("Name:", skin, "white");
    label.setName("nameLabel");
    table.add(label);
    var textField = new TextField(drawableData.name, skin);
    textField.setName("nameField");
    table.add(textField).growX();
    textField.addListener(ibeamListener);
    textField.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            drawableData.name = textField.getText();
            validateName();
        }
    });
    table.defaults().uniform().fill();
    var textButton = new TextButton("Save to File", skin);
    table.add(textButton);
    textButton.addListener(handListener);
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Runnable runnable = () -> {
                String[] filterPatterns = null;
                if (!Utils.isMac()) {
                    filterPatterns = new String[] { "*.png" };
                }
                var file = desktopWorker.saveDialog("Save as 9patch...", projectData.getLastDrawablePath(), filterPatterns, "Image files");
                if (file != null) {
                    Gdx.app.postRunnable(() -> {
                        var fileHandle = new FileHandle(file);
                        if (!fileHandle.name().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
                            fileHandle = fileHandle.sibling(fileHandle.nameWithoutExtension() + ".9.png");
                        }
                        saveToImageFile(fileHandle);
                    });
                }
            };
            dialogFactory.showDialogLoading(runnable);
        }
    });
    textButton = new TextButton("Clear", skin);
    table.add(textButton);
    textButton.addListener(handListener);
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            drawableData.tenPatchData.clear();
        }
    });
    textButton = new TextButton("More info...", skin);
    table.add(textButton);
    textButton.addListener(handListener);
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            showAboutDialog();
        }
    });
    top.row();
    tenPatchWidget = new TenPatchWidget(skin);
    tenPatchWidget.setTenPatchData(drawableData.tenPatchData);
    tenPatchWidget.setTextureRegion(loadTextureFile(drawableData.file));
    top.add(tenPatchWidget).grow();
    tenPatchWidget.getStretchSwitchButton().addListener(handListener);
    tenPatchWidget.getModeSwitchButton().addListener(handListener);
    tenPatchWidget.addListener(new InputListener() {

        @Override
        public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            getStage().setScrollFocus(tenPatchWidget);
        }

        @Override
        public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
            zoomToMouse = false;
            if (pointer == -1) {
                if (getStage() != null) {
                    getStage().setScrollFocus(null);
                }
            }
        }

        @Override
        public boolean scrolled(InputEvent event, float x, float y, float amountX, float amountY) {
            zoomToMouse = true;
            Slider slider = findActor("ten-patch-zoom");
            slider.setValue(slider.getValue() - SCROLL_AMOUNT * amountY);
            return true;
        }
    });
    tenPatchWidget.addListener(new TenPatchWidget.TenPatchListener() {

        @Override
        public void valueChanged(TenPatchData tenPatchData) {
            updatePreview();
        }
    });
    top.row();
    table = new Table();
    table.pad(5);
    top.add(table).growX();
    table.defaults().space(5);
    var imageButton = new ImageButton(skin, "grid-light");
    imageButton.setName("grid-light");
    imageButton.setProgrammaticChangeEvents(false);
    table.add(imageButton);
    imageButton.addListener(handListener);
    imageButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            var imageButton = (ImageButton) actor;
            if (imageButton.isChecked()) {
                tenPatchWidget.setGridMode(TenPatchWidget.GridMode.LIGHT);
            } else {
                tenPatchWidget.setGridMode(TenPatchWidget.GridMode.NONE);
            }
            imageButton = findActor("grid-dark");
            imageButton.setChecked(false);
        }
    });
    imageButton = new ImageButton(skin, "grid-dark");
    imageButton.setName("grid-dark");
    imageButton.setProgrammaticChangeEvents(false);
    table.add(imageButton);
    imageButton.addListener(handListener);
    imageButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            var imageButton = (ImageButton) actor;
            if (imageButton.isChecked()) {
                tenPatchWidget.setGridMode(TenPatchWidget.GridMode.DARK);
            } else {
                tenPatchWidget.setGridMode(TenPatchWidget.GridMode.NONE);
            }
            imageButton = findActor("grid-light");
            imageButton.setChecked(false);
        }
    });
    imageButton = new ImageButton(skin, "resize");
    table.add(imageButton).expandX().right();
    imageButton.addListener(handListener);
    imageButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            tenPatchWidget.zoomAndCenter();
            Slider slider = findActor("ten-patch-zoom");
            slider.setValue(tenPatchWidget.getZoomScale());
        }
    });
    var slider = new Slider(1, 100, 1f, false, skin, "zoom-horizontal");
    slider.setName("ten-patch-zoom");
    slider.setValue(1);
    table.add(slider);
    slider.addListener(handListener);
    slider.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            var slider = (Slider) actor;
            if (!zoomToMouse) {
                tenPatchWidget.setZoomScale(slider.getValue());
            } else {
                temp.set(Gdx.input.getX(), Gdx.input.getY());
                tenPatchWidget.screenToLocalCoordinates(temp);
                tenPatchWidget.setZoomScale(temp.x, temp.y, slider.getValue());
            }
        }
    });
    top.row();
    table = new Table();
    top.add(table).growX();
    table.defaults().space(3);
    table.pad(5);
    var imageTextButton = new ImageTextButton("Animation...", skin, "ten-patch-animation");
    table.add(imageTextButton);
    imageTextButton.addListener(handListener);
    imageTextButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            var dialog = new DialogTenPatchAnimation(drawableData);
            dialog.addListener(new DialogTenPatchAnimation.DialogTenPatchAnimationListener() {

                @Override
                public void animationUpdated(DialogTenPatchAnimation.DialogTenPatchAnimationEvent event) {
                    updatePreview();
                }
            });
            dialog.show(getStage());
        }
    });
    textButton = new TextButton("Load Patches", skin);
    table.add(textButton);
    textButton.addListener(handListener);
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Runnable runnable = () -> {
                String[] filterPatterns = null;
                if (!Utils.isMac()) {
                    filterPatterns = new String[] { "*.9.png" };
                }
                var file = desktopWorker.openDialog("Load patches from file...", projectData.getLastDrawablePath(), filterPatterns, "Nine patch files");
                if (file != null) {
                    Gdx.app.postRunnable(() -> {
                        var fileHandle = new FileHandle(file);
                        if (fileHandle.name().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
                            loadPatchesFromFile(fileHandle);
                        }
                    });
                }
            };
            dialogFactory.showDialogLoading(runnable);
        }
    });
    textButton = new TextButton("Auto Patches", skin);
    table.add(textButton);
    textButton.addListener(handListener);
    textButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            var pixmap = new Pixmap(fileHandle);
            if (fileHandle.path().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
                var cropped = new Pixmap(pixmap.getWidth() - 2, pixmap.getHeight() - 2, Pixmap.Format.RGBA8888);
                cropped.setBlending(Pixmap.Blending.None);
                cropped.drawPixmap(pixmap, 0, 0, 1, 1, pixmap.getWidth() - 2, pixmap.getHeight() - 2);
                pixmap.dispose();
                pixmap = cropped;
            }
            var patches = Utils.calculatePatches(pixmap);
            drawableData.tenPatchData.horizontalStretchAreas.clear();
            drawableData.tenPatchData.horizontalStretchAreas.add(patches.left);
            drawableData.tenPatchData.horizontalStretchAreas.add(pixmap.getWidth() - patches.right - 1);
            drawableData.tenPatchData.verticalStretchAreas.clear();
            drawableData.tenPatchData.verticalStretchAreas.add(patches.bottom);
            drawableData.tenPatchData.verticalStretchAreas.add(pixmap.getHeight() - patches.top - 1);
            drawableData.tenPatchData.combineContiguousSretchAreas(true);
            drawableData.tenPatchData.removeInvalidStretchAreas(true);
            drawableData.tenPatchData.combineContiguousSretchAreas(false);
            drawableData.tenPatchData.removeInvalidStretchAreas(false);
            updatePreview();
            pixmap.dispose();
        }
    });
    imageTextButton = new ImageTextButton("More settings...", skin, "ten-patch-scrolling");
    table.add(imageTextButton);
    imageTextButton.addListener(handListener);
    imageTextButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            var dialog = new DialogTenPatchSettings(drawableData, tenPatchDrawable);
            dialog.addListener(new DialogTenPatchSettings.DialogTenPatchSettingsListener() {

                @Override
                public void settingsUpdated(DialogTenPatchSettings.DialogTenPatchSettingsEvent event) {
                    updatePreview();
                }
            });
            dialog.show(getStage());
        }
    });
    label = new Label("Preview", skin, "title-no-line");
    bottom.add(label);
    bottom.row();
    table = new Table();
    bottom.add(table).grow();
    var resizer = new ResizeWidget(null, skin);
    resizer.setName("resizer");
    resizer.setTouchable(Touchable.enabled);
    resizer.setResizingFromCenter(true);
    resizer.setAllowDragging(false);
    var cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_ne"), 16, 16);
    var resizeFourArrowListener = new ResizeFourArrowListener(cursor);
    resizer.getBottomLeftHandle().addListener(resizeFourArrowListener);
    resizer.getTopRightHandle().addListener(resizeFourArrowListener);
    cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_nw"), 16, 16);
    resizeFourArrowListener = new ResizeFourArrowListener(cursor);
    resizer.getTopLeftHandle().addListener(resizeFourArrowListener);
    resizer.getBottomRightHandle().addListener(resizeFourArrowListener);
    cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_vertical"), 16, 16);
    resizeFourArrowListener = new ResizeFourArrowListener(cursor);
    resizer.getBottomHandle().addListener(resizeFourArrowListener);
    resizer.getTopHandle().addListener(resizeFourArrowListener);
    cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_horizontal"), 16, 16);
    resizeFourArrowListener = new ResizeFourArrowListener(cursor);
    resizer.getLeftHandle().addListener(resizeFourArrowListener);
    resizer.getRightHandle().addListener(resizeFourArrowListener);
    table.add(resizer).grow();
    String name = drawableData.file.nameWithoutExtension();
    var matcher = Pattern.compile(".*(?=\\.9$)").matcher(name);
    if (matcher.find()) {
        name = matcher.group();
    }
    originalRegion = atlasData.getAtlas().findRegion(name);
    tenPatchDrawable = new TenPatchDrawable(new int[0], new int[0], false, originalRegion);
    if (drawableData.tenPatchData.colorName != null) {
        tenPatchDrawable.getColor().set(jsonData.getColorByName(drawableData.tenPatchData.colorName).color);
    }
    tenPatchDrawable.setTiling(drawableData.tenPatchData.tile);
    table = new Table();
    table.setName("tenPatchTable");
    table.setBackground(tenPatchDrawable);
    resizer.setActor(table);
    layout();
    resizer.getStack().setSize(100, 100);
    bottom.row();
    table = new Table();
    table.setBackground(skin.getDrawable("white"));
    table.pad(5);
    bottom.add(table).growX();
    table.defaults().space(5);
    label = new Label("Content:", skin);
    table.add(label).right();
    var selectBox = new SelectBox<String>(skin);
    selectBox.setName("contentSelectBox");
    selectBox.setItems("None", "Text", "Color", "Drawable");
    table.add(selectBox);
    selectBox.addListener(handListener);
    selectBox.getList().addListener(handListener);
    selectBox.getList().addListener(new ClickListener() {

        @Override
        public void clicked(InputEvent event, float x, float y) {
            selectBox.fire(new ChangeListener.ChangeEvent());
        }
    });
    selectBox.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            createPreview();
        }
    });
    imageButton = new ImageButton(skin, "color");
    table.add(imageButton);
    imageButton.addListener(handListener);
    imageButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            dialogFactory.showDialogColors(new StyleProperty(), (colorData, pressedCancel) -> {
                if (colorData == null) {
                    previewColor.set(DEFAULT_PREVIEW_COLOR);
                } else {
                    previewColor.set(colorData.color);
                }
                updatePreview();
            }, null);
        }
    });
    label = new Label("Background:", skin);
    table.add(label).padLeft(15);
    imageButton = new ImageButton(skin, "color");
    table.add(imageButton);
    imageButton.addListener(handListener);
    imageButton.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            dialogFactory.showDialogColors(new StyleProperty(), (colorData, pressedCancel) -> {
                if (colorData == null) {
                    bottom.setColor(Color.WHITE);
                } else {
                    bottom.setColor(colorData.color);
                }
                updatePreview();
            }, null);
        }
    });
    table.add().expandX();
    root = getButtonTable();
    root.pad(10);
    root.clearChildren();
    root.defaults().uniform().fill();
    textButton = new TextButton("OK", skin);
    textButton.setName("okayButton");
    button(textButton, true);
    validateName();
    textButton.addListener(handListener);
    textButton = new TextButton("Cancel", skin);
    button(textButton, false);
    textButton.addListener(handListener);
    updatePreview();
    layout();
    tenPatchWidget.zoomAndCenter();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Array(com.badlogic.gdx.utils.Array) Scaling(com.badlogic.gdx.utils.Scaling) Utils(com.ray3k.skincomposer.utils.Utils) Gdx(com.badlogic.gdx.Gdx) Texture(com.badlogic.gdx.graphics.Texture) TenPatchDrawable(com.ray3k.tenpatch.TenPatchDrawable) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) ResizeWidget(com.ray3k.stripe.ResizeWidget) Vector2(com.badlogic.gdx.math.Vector2) Locale(java.util.Locale) IntArray(com.badlogic.gdx.utils.IntArray) TenPatchWidget(com.ray3k.skincomposer.TenPatchWidget) Actions(com.badlogic.gdx.scenes.scene2d.actions.Actions) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Input(com.badlogic.gdx.Input) PixmapIO(com.badlogic.gdx.graphics.PixmapIO) CrushMode(com.ray3k.tenpatch.TenPatchDrawable.CrushMode) com.badlogic.gdx.scenes.scene2d(com.badlogic.gdx.scenes.scene2d) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener) DrawableData(com.ray3k.skincomposer.data.DrawableData) com.badlogic.gdx.scenes.scene2d.ui(com.badlogic.gdx.scenes.scene2d.ui) Pixmap(com.badlogic.gdx.graphics.Pixmap) FilesDroppedListener(com.ray3k.skincomposer.FilesDroppedListener) Align(com.badlogic.gdx.utils.Align) Color(com.badlogic.gdx.graphics.Color) PopTable(com.ray3k.stripe.PopTable) DragListener(com.badlogic.gdx.scenes.scene2d.utils.DragListener) Keys(com.badlogic.gdx.Input.Keys) ResizeFourArrowListener(com.ray3k.skincomposer.ResizeFourArrowListener) Main(com.ray3k.skincomposer.Main) Pattern(java.util.regex.Pattern) LabelStyle(com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle) StageResizeListener(com.ray3k.skincomposer.StageResizeListener) StyleProperty(com.ray3k.skincomposer.data.StyleProperty) FileHandle(com.badlogic.gdx.files.FileHandle) TenPatchWidget(com.ray3k.skincomposer.TenPatchWidget) ResizeFourArrowListener(com.ray3k.skincomposer.ResizeFourArrowListener) DragListener(com.badlogic.gdx.scenes.scene2d.utils.DragListener) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) ClickListener(com.badlogic.gdx.scenes.scene2d.utils.ClickListener) PopTable(com.ray3k.stripe.PopTable) TenPatchDrawable(com.ray3k.tenpatch.TenPatchDrawable) StyleProperty(com.ray3k.skincomposer.data.StyleProperty) ResizeWidget(com.ray3k.stripe.ResizeWidget) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 92 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogPathErrors method resetDrawableTable.

private void resetDrawableTable(Main main, Skin skin, Array<DrawableData> drawables, Array<FontData> fonts, Array<FreeTypeFontData> freeTypeFonts) {
    dataTable.clear();
    if (drawables.size > 0) {
        Label label = new Label("Drawable Name", skin, "black");
        dataTable.add(label);
        label = new Label("Path", skin, "black");
        dataTable.add(label);
        dataTable.add();
        label = new Label("Found?", skin, "black");
        dataTable.add(label);
        dataTable.row();
        Image image = new Image(skin, "welcome-separator");
        dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        for (DrawableData drawable : drawables) {
            dataTable.row();
            label = new Label(drawable.name, skin);
            dataTable.add(label);
            label = new Label(drawable.file.path(), skin);
            label.setWrap(true);
            label.setAlignment(Align.left);
            dataTable.add(label).growX();
            TextButton textButton = new TextButton("browse...", skin);
            textButton.addListener(handListener);
            dataTable.add(textButton).padLeft(10.0f);
            textButton.addListener(new ChangeListener() {

                @Override
                public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                    String[] filterPatterns = null;
                    if (!Utils.isMac()) {
                        filterPatterns = new String[] { "*.png", "*.jpg", "*.jpeg", "*.bmp", "*.gif" };
                    }
                    var defaultPath = drawable.file.parent().exists() ? drawable.file.parent().path() + "/" : "";
                    File file = desktopWorker.openDialog("Locate " + drawable.file.name() + "...", defaultPath, filterPatterns, "Image files");
                    if (file != null) {
                        FileHandle fileHandle = new FileHandle(file);
                        drawable.file = fileHandle;
                        if (!foundDrawables.contains(drawable, true)) {
                            foundDrawables.add(drawable);
                        }
                        resolveAssetsFromFolder(fileHandle.parent(), drawables, fonts, freeTypeFonts);
                        resetDrawableTable(main, skin, drawables, fonts, freeTypeFonts);
                    }
                }
            });
            if (foundDrawables.contains(drawable, true)) {
                label = new Label("YES", skin, "white");
                label.setColor(Color.GREEN);
                dataTable.add(label);
            } else {
                label = new Label("NO", skin, "white");
                label.setColor(Color.RED);
                dataTable.add(label);
            }
            dataTable.row();
            image = new Image(skin, "welcome-separator");
            dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        }
    }
    if (fonts.size > 0) {
        dataTable.row();
        dataTable.defaults().padTop(20.0f);
        Label label = new Label("Font Name", skin, "black");
        dataTable.add(label);
        label = new Label("Path", skin, "black");
        dataTable.add(label);
        dataTable.add();
        label = new Label("Found?", skin, "black");
        dataTable.add(label);
        dataTable.defaults().reset();
        dataTable.row();
        Image image = new Image(skin, "welcome-separator");
        dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        for (FontData font : fonts) {
            dataTable.row();
            label = new Label(font.getName(), skin);
            dataTable.add(label);
            label = new Label(font.file.path(), skin);
            label.setWrap(true);
            label.setAlignment(Align.left);
            dataTable.add(label).growX();
            TextButton textButton = new TextButton("browse...", skin);
            textButton.addListener(handListener);
            dataTable.add(textButton);
            textButton.addListener(new ChangeListener() {

                @Override
                public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                    String[] filterPatterns = null;
                    if (!Utils.isMac()) {
                        filterPatterns = new String[] { "*.fnt" };
                    }
                    var defaultPath = font.file.parent().exists() ? font.file.parent().path() + "/" : "";
                    File file = desktopWorker.openDialog("Locate " + font.file.name() + "...", defaultPath, filterPatterns, "Font files");
                    if (file != null) {
                        FileHandle fileHandle = new FileHandle(file);
                        font.file = fileHandle;
                        if (!foundFonts.contains(font, true)) {
                            foundFonts.add(font);
                        }
                        resolveAssetsFromFolder(fileHandle.parent(), drawables, fonts, freeTypeFonts);
                        resetDrawableTable(main, skin, drawables, fonts, freeTypeFonts);
                    }
                }
            });
            if (foundFonts.contains(font, true)) {
                label = new Label("YES", skin, "white");
                label.setColor(Color.GREEN);
                dataTable.add(label);
            } else {
                label = new Label("NO", skin, "white");
                label.setColor(Color.RED);
                dataTable.add(label);
            }
            dataTable.row();
            image = new Image(skin, "welcome-separator");
            dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        }
    }
    if (freeTypeFonts.size > 0) {
        dataTable.row();
        dataTable.defaults().padTop(20.0f);
        Label label = new Label("FreeTypeFont Name", skin, "black");
        dataTable.add(label);
        label = new Label("Path", skin, "black");
        dataTable.add(label);
        dataTable.add();
        label = new Label("Found?", skin, "black");
        dataTable.add(label);
        dataTable.defaults().reset();
        dataTable.row();
        Image image = new Image(skin, "welcome-separator");
        dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        for (var font : freeTypeFonts) {
            dataTable.row();
            label = new Label(font.name, skin);
            dataTable.add(label);
            label = new Label(font.file.path(), skin);
            label.setWrap(true);
            label.setAlignment(Align.left);
            dataTable.add(label).growX();
            TextButton textButton = new TextButton("browse...", skin);
            textButton.addListener(handListener);
            dataTable.add(textButton);
            textButton.addListener(new ChangeListener() {

                @Override
                public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                    String[] filterPatterns = null;
                    if (!Utils.isMac()) {
                        filterPatterns = new String[] { "*.ttf", "*.otf" };
                    }
                    var defaultPath = font.file.parent().exists() ? font.file.parent().path() + "/" : "";
                    File file = desktopWorker.openDialog("Locate " + font.file.name() + "...", defaultPath, filterPatterns, "Font Files (*.TTF;*.OTF");
                    if (file != null) {
                        FileHandle fileHandle = new FileHandle(file);
                        font.file = fileHandle;
                        if (!foundFreeTypeFonts.contains(font, true)) {
                            foundFreeTypeFonts.add(font);
                        }
                        resolveAssetsFromFolder(fileHandle.parent(), drawables, fonts, freeTypeFonts);
                        resetDrawableTable(main, skin, drawables, fonts, freeTypeFonts);
                    }
                }
            });
            if (foundFreeTypeFonts.contains(font, true)) {
                label = new Label("YES", skin, "white");
                label.setColor(Color.GREEN);
                dataTable.add(label);
            } else {
                label = new Label("NO", skin, "white");
                label.setColor(Color.RED);
                dataTable.add(label);
            }
            dataTable.row();
            image = new Image(skin, "welcome-separator");
            dataTable.add(image).colspan(4).pad(5.0f).padLeft(0.0f).padRight(0.0f).growX();
        }
    }
    dataTable.row();
    dataTable.add().grow().colspan(4);
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) FreeTypeFontData(com.ray3k.skincomposer.data.FreeTypeFontData) FontData(com.ray3k.skincomposer.data.FontData) DrawableData(com.ray3k.skincomposer.data.DrawableData) Actor(com.badlogic.gdx.scenes.scene2d.Actor) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) File(java.io.File)

Example 93 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogFonts method newFontDialog.

private void newFontDialog() {
    dialogFactory.showDialogLoading(() -> {
        String defaultPath = "";
        if (projectData.getLastFontPath() != null) {
            FileHandle fileHandle = new FileHandle(projectData.getLastFontPath());
            if (fileHandle.exists()) {
                defaultPath = projectData.getLastFontPath();
            }
        }
        String[] filterPatterns = null;
        if (!Utils.isMac()) {
            filterPatterns = new String[] { "*.fnt" };
        }
        List<File> files = desktopWorker.openMultipleDialog("Choose font file(s)...", defaultPath, filterPatterns, "Font files (*.fnt)");
        if (files != null && files.size() > 0) {
            Gdx.app.postRunnable(() -> {
                FileHandle fileHandle = new FileHandle(files.get(0).getParentFile());
                projectData.setLastFontPath(fileHandle.path() + "/");
                checkExistingDrawablesDialog(files, () -> {
                    fontNameDialog(files, 0);
                });
            });
        }
    });
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) File(java.io.File)

Example 94 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogFonts method produceAtlas.

private boolean produceAtlas() {
    try {
        if (atlas != null) {
            atlas.dispose();
            atlas = null;
        }
        if (!atlasData.atlasCurrent) {
            FileHandle defaultsFile = Main.appFolder.child("texturepacker/atlas-internal-settings.json");
            atlasData.writeAtlas(defaultsFile);
            atlasData.atlasCurrent = true;
        }
        atlas = atlasData.getAtlas();
        for (FontData font : fonts) {
            BitmapFontData fontData = new BitmapFontData(font.file, false);
            Array<TextureRegion> regions = new Array<>();
            for (String path : fontData.imagePaths) {
                FileHandle file = new FileHandle(path);
                if (!file.exists()) {
                    file = fontData.fontFile.sibling(fontData.fontFile.nameWithoutExtension() + ".png");
                }
                TextureRegion region = atlas.findRegion(file.nameWithoutExtension());
                if (region != null) {
                    regions.add(region);
                }
            }
            fontMap.put(font, new BitmapFont(fontData, regions, true));
        }
        return true;
    } catch (Exception e) {
        Gdx.app.error(getClass().getName(), "Error while attempting to generate drawables.", e);
        dialogFactory.showDialogError("Drawables Error...", "Error while attempting to generate drawables. Open log?");
        return false;
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 95 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project skin-composer by raeleus.

the class DialogFonts method initialize.

public void initialize(Main main, EventListener listener) {
    this.main = main;
    maxTextureWidth = 1024;
    maxTextureHeight = 1024;
    // extract max texture dimensions from atlas-export-settings.json
    FileHandle defaultsFile = Main.appFolder.child("texturepacker/atlas-export-settings.json");
    if (defaultsFile.exists()) {
        JsonReader reader = new JsonReader();
        JsonValue val = reader.parse(defaultsFile);
        for (JsonValue child : val.iterator()) {
            if (child.name.equals("maxWidth") && child.isNumber()) {
                maxTextureWidth = child.asInt();
            } else if (child.name.equals("maxHeight") && child.isNumber()) {
                maxTextureHeight = child.asInt();
            }
        }
    }
    this.listener = listener;
    fonts = jsonData.getFonts();
    freeTypeFonts = jsonData.getFreeTypeFonts();
    drawables = atlasData.getDrawables();
    fontDrawables = atlasData.getFontDrawables();
    fontMap = new ObjectMap<>();
    produceAtlas();
    filesDroppedListener = (Array<FileHandle> files) -> {
        Iterator<FileHandle> iter = files.iterator();
        while (iter.hasNext()) {
            FileHandle file = iter.next();
            if (file.isDirectory()) {
                files.addAll(file.list());
                iter.remove();
            } else if (!file.name().toLowerCase().endsWith(".fnt")) {
                iter.remove();
            }
        }
        if (files.size > 0) {
            fontNameDialog(files, 0);
        }
    };
    desktopWorker.addFilesDroppedListener(filesDroppedListener);
    populate();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)207 File (java.io.File)62 IOException (java.io.IOException)38 Array (com.badlogic.gdx.utils.Array)36 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)28 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)16 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)16 Texture (com.badlogic.gdx.graphics.Texture)15 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)13 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)12 ArrayList (java.util.ArrayList)12 Region (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region)11 Color (com.badlogic.gdx.graphics.Color)10 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)10 IntArray (com.badlogic.gdx.utils.IntArray)10 Pixmap (com.badlogic.gdx.graphics.Pixmap)9 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)9 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Locale (java.util.Locale)9 FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)8