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;
}
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());
}
}
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);
}
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());
}
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);
}
Aggregations