Search in sources :

Example 36 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project gdx-skineditor by cobolfoo.

the class Skin method getJsonLoader.

protected Json getJsonLoader(final FileHandle skinFile) {
    final Skin skin = this;
    final Json json = new Json() {

        public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData) {
            // actual value by name.
            if (jsonData.isString() && !ClassReflection.isAssignableFrom(CharSequence.class, type))
                return get(jsonData.asString(), type);
            return super.readValue(type, elementType, jsonData);
        }
    };
    json.setTypeName(null);
    json.setUsePrototypes(false);
    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {

        public Skin read(Json json, JsonValue typeToValueMap, Class ignored) {
            for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
                try {
                    readNamedObjects(json, ClassReflection.forName(valueMap.name()), valueMap);
                } catch (ReflectionException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class type, JsonValue valueMap) {
            for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
                Object object = json.readValue(type, valueEntry);
                if (object == null)
                    continue;
                try {
                    add(valueEntry.name(), object, type);
                } catch (Exception ex) {
                    throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name(), ex);
                }
            }
        }
    });
    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {

        public BitmapFont read(Json json, JsonValue jsonData, Class type) {
            String path = json.readValue("file", String.class, jsonData);
            int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
            Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
            FileHandle fontFile = skinFile.parent().child(path);
            if (!fontFile.exists())
                fontFile = Gdx.files.internal(path);
            if (!fontFile.exists())
                throw new SerializationException("Font file not found: " + fontFile);
            // Use a region with the same name as the font, else use
            // a PNG file in the same directory as the FNT file.
            String regionName = fontFile.nameWithoutExtension();
            try {
                BitmapFont font;
                TextureRegion region = skin.optional(regionName, TextureRegion.class);
                if (region != null)
                    font = new BitmapFont(fontFile, region, flip);
                else {
                    FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                    if (imageFile.exists())
                        font = new BitmapFont(fontFile, imageFile, flip);
                    else
                        font = new BitmapFont(fontFile, flip);
                }
                // the font to.
                if (scaledSize != -1)
                    font.setScale(scaledSize / font.getCapHeight());
                return font;
            } catch (RuntimeException ex) {
                throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
            }
        }
    });
    json.setSerializer(Color.class, new ReadOnlySerializer<Color>() {

        public Color read(Json json, JsonValue jsonData, Class type) {
            if (jsonData.isString())
                return get(jsonData.asString(), Color.class);
            String hex = json.readValue("hex", String.class, (String) null, jsonData);
            if (hex != null)
                return Color.valueOf(hex);
            float r = json.readValue("r", float.class, 0f, jsonData);
            float g = json.readValue("g", float.class, 0f, jsonData);
            float b = json.readValue("b", float.class, 0f, jsonData);
            float a = json.readValue("a", float.class, 1f, jsonData);
            return new Color(r, g, b, a);
        }
    });
    json.setSerializer(TintedDrawable.class, new ReadOnlySerializer() {

        public Object read(Json json, JsonValue jsonData, Class type) {
            String name = json.readValue("name", String.class, jsonData);
            Color color = json.readValue("color", Color.class, jsonData);
            TintedDrawable td = new TintedDrawable();
            td.name = name;
            td.color = color;
            td.drawable = newDrawable(name, color);
            return td;
        //				return newDrawable(name, color);
        }
    });
    return json;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) SerializationException(com.badlogic.gdx.utils.SerializationException) FileHandle(com.badlogic.gdx.files.FileHandle) Color(com.badlogic.gdx.graphics.Color) JsonValue(com.badlogic.gdx.utils.JsonValue) Json(com.badlogic.gdx.utils.Json) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ReadOnlySerializer(com.badlogic.gdx.utils.Json.ReadOnlySerializer) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 37 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project gdx-skineditor by cobolfoo.

the class NewFontDialog method refreshFontPreview.

/**
	 * 
	 */
public void refreshFontPreview() {
    try {
        String fontName = selectFonts.getSelected();
        Gdx.app.log("FontPickerDialog", "Refreshing preview for font: " + fontName);
        File fontPath = game.fm.fonts.get(selectFonts.getSelected());
        Gdx.app.log("FontPickerDialog", "Loading font from file:" + fontPath);
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontPath);
        UnicodeFont unicodeFont = new UnicodeFont(font, Integer.valueOf(selectSize.getSelected()), checkBold.isChecked(), checkItalic.isChecked());
        if (checkShadow.isChecked() == true) {
            ColorEffect colorEffect = new ColorEffect();
            colorEffect.setColor(java.awt.Color.BLACK);
            unicodeFont.getEffects().add(colorEffect);
            ShadowEffect shadow = new ShadowEffect();
            shadow.setOpacity(1.0f);
            shadow.setXDistance(1);
            shadow.setYDistance(1);
            shadow.setColor(java.awt.Color.WHITE);
            unicodeFont.getEffects().add(shadow);
        } else {
            ColorEffect colorEffect = new ColorEffect();
            colorEffect.setColor(java.awt.Color.WHITE);
            unicodeFont.getEffects().add(colorEffect);
        }
        unicodeFont.addAsciiGlyphs();
        String newFontName = generateProperFontName(fontName);
        textFontName.setText(newFontName);
        // Create bitmap font
        BMFontUtil bfu = new BMFontUtil(unicodeFont);
        FileHandle handle = new FileHandle(System.getProperty("java.io.tmpdir")).child(newFontName);
        FileHandle handleFont = new FileHandle(handle.file().getAbsolutePath() + ".fnt");
        bfu.save(handle.file());
        FileHandle handleImage = new FileHandle(System.getProperty("java.io.tmpdir")).child(newFontName + ".png");
        TextField.TextFieldStyle textStyle = new TextField.TextFieldStyle();
        textStyle.cursor = game.skin.getDrawable("cursor");
        textStyle.selection = game.skin.getDrawable("selection");
        textStyle.background = game.skin.getDrawable("textfield");
        textStyle.fontColor = Color.YELLOW;
        textStyle.font = new BitmapFont(handleFont, handleImage, false);
        textFontPreview.setStyle(textStyle);
        // Have to do this to force clipping of font
        textFontPreview.setText(textFontPreview.getText());
    } catch (Exception e) {
        e.printStackTrace();
        textFontPreview.getStyle().font = game.skin.getFont("default-font");
        // Have to do this to force clipping of font
        textFontPreview.setText(textFontPreview.getText());
    }
}
Also used : UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) FileHandle(com.badlogic.gdx.files.FileHandle) Font(java.awt.Font) UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) ShadowEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ShadowEffect) BMFontUtil(com.badlogic.gdx.tools.hiero.BMFontUtil) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) File(java.io.File)

Example 38 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project gdx-skineditor by cobolfoo.

the class SkinEditorGame method create.

@Override
public void create() {
    opt = new OptionalChecker();
    fm = new SystemFonts();
    fm.refreshFonts();
    // Create projects folder if not already here
    FileHandle dirProjects = new FileHandle("projects");
    if (dirProjects.isDirectory() == false) {
        dirProjects.mkdirs();
    }
    // Rebuild from raw resources, kind of overkill, might disable it for production
    TexturePacker.Settings settings = new TexturePacker.Settings();
    settings.combineSubdirectories = true;
    TexturePacker.process(settings, "resources/raw/", ".", "resources/uiskin");
    batch = new SpriteBatch();
    skin = new Skin();
    atlas = new TextureAtlas(Gdx.files.internal("resources/uiskin.atlas"));
    skin.addRegions(new TextureAtlas(Gdx.files.local("resources/uiskin.atlas")));
    skin.load(Gdx.files.local("resources/uiskin.json"));
    screenMain = new MainScreen(this);
    screenWelcome = new WelcomeScreen(this);
    setScreen(screenWelcome);
}
Also used : WelcomeScreen(org.shadebob.skineditor.screens.WelcomeScreen) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) MainScreen(org.shadebob.skineditor.screens.MainScreen)

Example 39 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project gdx-skineditor by cobolfoo.

the class MenuBar method showExportDialog.

/*
	 * Show export dialog
	 */
protected void showExportDialog() {
    final Preferences prefs = Gdx.app.getPreferences("skin_editor_project_" + game.screenMain.getcurrentProject());
    final TextField textDirectory = new TextField(prefs.getString("export_to_directory"), game.skin);
    Dialog dlg = new Dialog("Export to Directory", game.skin) {

        @Override
        protected void result(Object object) {
            if ((Boolean) object == true) {
                if (textDirectory.getText().isEmpty() == true) {
                    game.showNotice("Warning", "Directory field is empty!", game.screenMain.stage);
                    return;
                }
                FileHandle targetDirectory = new FileHandle(textDirectory.getText());
                if (targetDirectory.exists() == false) {
                    game.showNotice("Warning", "Directory not found!", game.screenMain.stage);
                    return;
                }
                // Copy uiskin.* and *.fnt 
                FileHandle projectFolder = Gdx.files.local("projects").child(game.screenMain.getcurrentProject());
                for (FileHandle file : projectFolder.list()) {
                    if (file.name().startsWith("uiskin.") || (file.extension().equalsIgnoreCase("fnt"))) {
                        Gdx.app.log("MenuBar", "Copying file: " + file.name() + " ...");
                        FileHandle target = targetDirectory.child(file.name());
                        file.copyTo(target);
                    }
                }
                game.showNotice("Operation Completed", "Project successfully exported!", game.screenMain.stage);
            }
        }
    };
    dlg.pad(20);
    Table table = dlg.getContentTable();
    table.padTop(20);
    table.add("Directory:");
    table.add(textDirectory).width(320);
    TextButton buttonChoose = new TextButton("...", game.skin);
    buttonChoose.addListener(new ChangeListener() {

        @Override
        public void changed(ChangeEvent event, Actor actor) {
            // Need to steal focus first with this hack (Thanks to Z-Man)
            Frame frame = new Frame();
            frame.setUndecorated(true);
            frame.setOpacity(0);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.toFront();
            frame.setVisible(false);
            frame.dispose();
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int ret = chooser.showOpenDialog(null);
            if (ret == JFileChooser.APPROVE_OPTION) {
                File f = chooser.getSelectedFile();
                textDirectory.setText(f.getAbsolutePath());
                // Store to file
                prefs.putString("export_to_directory", f.getAbsolutePath());
                prefs.flush();
            }
        }
    });
    table.add(buttonChoose);
    table.row();
    table.padBottom(20);
    dlg.button("Export", true);
    dlg.button("Cancel", false);
    dlg.key(com.badlogic.gdx.Input.Keys.ENTER, true);
    dlg.key(com.badlogic.gdx.Input.Keys.ESCAPE, false);
    dlg.show(getStage());
}
Also used : TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Frame(java.awt.Frame) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) FileHandle(com.badlogic.gdx.files.FileHandle) JFileChooser(javax.swing.JFileChooser) DrawablePickerDialog(org.shadebob.skineditor.DrawablePickerDialog) Dialog(com.badlogic.gdx.scenes.scene2d.ui.Dialog) ColorPickerDialog(org.shadebob.skineditor.ColorPickerDialog) FontPickerDialog(org.shadebob.skineditor.FontPickerDialog) Actor(com.badlogic.gdx.scenes.scene2d.Actor) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) Preferences(com.badlogic.gdx.Preferences) File(java.io.File)

Example 40 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project gdx-skineditor by cobolfoo.

the class WelcomeScreen method showDeleteDialog.

/**
	 * 
	 */
private void showDeleteDialog() {
    Dialog dlgStyle = new Dialog("Delete Project", game.skin) {

        @Override
        protected void result(Object object) {
            if ((Boolean) object == false) {
                return;
            }
            // We delete it
            FileHandle projectFolder = Gdx.files.local("projects/" + (String) listProjects.getSelected());
            projectFolder.deleteDirectory();
            refreshProjects();
        }
    };
    dlgStyle.pad(20);
    dlgStyle.getContentTable().add("You are sure you want to delete this project?");
    dlgStyle.button("OK", true);
    dlgStyle.button("Cancel", false);
    dlgStyle.key(com.badlogic.gdx.Input.Keys.ENTER, true);
    dlgStyle.key(com.badlogic.gdx.Input.Keys.ESCAPE, false);
    dlgStyle.show(stage);
}
Also used : NinePatchEditorDialog(org.shadebob.skineditor.NinePatchEditorDialog) Dialog(com.badlogic.gdx.scenes.scene2d.ui.Dialog) FileHandle(com.badlogic.gdx.files.FileHandle)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)79 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)23 IOException (java.io.IOException)22 Array (com.badlogic.gdx.utils.Array)18 File (java.io.File)18 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Texture (com.badlogic.gdx.graphics.Texture)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)5 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)4 Json (com.badlogic.gdx.utils.Json)4 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3 Color (com.badlogic.gdx.graphics.Color)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3