Skip to content

Instantly share code, notes, and snippets.

@ProZhar
Last active November 2, 2017 18:32
Show Gist options
  • Select an option

  • Save ProZhar/b5017123df4fd3eb967048994fbf33ec to your computer and use it in GitHub Desktop.

Select an option

Save ProZhar/b5017123df4fd3eb967048994fbf33ec to your computer and use it in GitHub Desktop.
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.DistanceFieldFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.SerializationException;
public class Skin2 extends Skin {
public Skin2(FileHandle skinFile) {
super(skinFile);
}
public Skin2(FileHandle skinFile, TextureAtlas atlas) {
super(skinFile, atlas);
}
public Skin2(TextureAtlas atlas) {
super(atlas);
}
@Override
protected Json getJsonLoader(final FileHandle skinFile) {
final Skin2 skin = this;
//first get default json with default BitmapFont.class Serializer
final Json json = super.getJsonLoader(skinFile);
// set new serializer
json.setSerializer(BitmapFont.class, new Json.ReadOnlySerializer<BitmapFont>() {
public DistanceFieldFont 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);
Boolean markupEnabled = json.readValue("markupEnabled", Boolean.class, false, jsonData);
float distanceFieldSmoothing = json.readValue("distanceFieldSmoothing", float.class, 0f, jsonData);
if (distanceFieldSmoothing < 0f) throw new IllegalArgumentException("distanceFieldSmoothing cannot be < 0.");
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 {
DistanceFieldFont font;
Array<TextureRegion> regions = skin.getRegions(regionName);
if (regions != null)
font = new DistanceFieldFont(new BitmapFont.BitmapFontData(fontFile, flip), regions, true);
else {
TextureRegion region = skin.optional(regionName, TextureRegion.class);
if (region != null)
font = new DistanceFieldFont(fontFile, region, flip);
else {
FileHandle imageFile = fontFile.parent().child(regionName + ".png");
if (imageFile.exists())
font = new DistanceFieldFont(fontFile, imageFile, flip);
else
font = new DistanceFieldFont(fontFile, flip);
}
}
font.getData().markupEnabled = markupEnabled;
// Scaled size is the desired cap height to scale the font to.
if (scaledSize != -1) font.getData().setScale(scaledSize / font.getCapHeight());
if (distanceFieldSmoothing == 0f) {
regions = font.getRegions();
for (TextureRegion region : regions.items)
region.getTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
}
font.setDistanceFieldSmoothing(distanceFieldSmoothing);
return font;
} catch (RuntimeException ex) {
throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
}
}
});
return json;
}
public DistanceFieldFont getDistanceFieldFont (String name) {
return (DistanceFieldFont) get(name, BitmapFont.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment