feat(assets): supports dynamic font loading
It supports loading a dynamic font with size in runtime now.
This commit is contained in:
@ -3,16 +3,53 @@ package org.vibecoders.moongazer.managers;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
|
||||
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
|
||||
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Assets {
|
||||
private static final AssetManager assetManager = new AssetManager();
|
||||
private static final FileHandleResolver resolver = new InternalFileHandleResolver();
|
||||
private static final Logger log = org.slf4j.LoggerFactory.getLogger(Assets.class);
|
||||
private static final ArrayList<String> loadedFonts = new ArrayList<>();
|
||||
private static boolean startLoadAll = false;
|
||||
private static boolean loadedAll = false;
|
||||
|
||||
public static <T> T getAsset(String fileName, Class<T> type) {
|
||||
return assetManager.get(fileName, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and returns a BitmapFont of the specified size from the given TTF file.
|
||||
* <p>
|
||||
* Special file name "ui" is mapped to "fonts/H7GBKHeavy.ttf" (Wuthering Waves UI font).
|
||||
*
|
||||
* @param fileName the font name
|
||||
* @param size the font size
|
||||
* @return the loaded BitmapFont
|
||||
*/
|
||||
public static BitmapFont getFont(String fileName, int size) {
|
||||
if (fileName.equals("ui")) {
|
||||
fileName = "fonts/H7GBKHeavy.ttf";
|
||||
}
|
||||
// Only works for .ttf files but okay.
|
||||
var fontKey = fileName.split(".", 1)[0] + "-" + size + ".ttf";
|
||||
if (!loadedFonts.contains(fontKey)) {
|
||||
var params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
|
||||
params.fontFileName = fileName;
|
||||
params.fontParameters.size = size;
|
||||
assetManager.load(fontKey, BitmapFont.class, params);
|
||||
assetManager.finishLoadingAsset(fontKey);
|
||||
loadedFonts.add(fontKey);
|
||||
}
|
||||
return assetManager.get(fontKey, BitmapFont.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads assets required for the intro scene only.
|
||||
*
|
||||
@ -24,12 +61,28 @@ public class Assets {
|
||||
}
|
||||
|
||||
public static void loadAll() {
|
||||
if (startLoadAll) {
|
||||
log.warn("loadAll() called multiple times!");
|
||||
return;
|
||||
}
|
||||
log.info("Loading all assets....");
|
||||
log.warn("stub");
|
||||
startLoadAll = true;
|
||||
// Add loader for TTF fonts
|
||||
assetManager.setLoader(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.class,
|
||||
new FreeTypeFontGeneratorLoader(resolver));
|
||||
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
|
||||
}
|
||||
|
||||
public static boolean isLoadedAll() {
|
||||
return loadedAll;
|
||||
}
|
||||
|
||||
public static void waitUntilLoaded() {
|
||||
assetManager.finishLoading();
|
||||
if (startLoadAll) {
|
||||
log.info("All assets loaded.");
|
||||
loadedAll = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static AssetManager getAssetManager() {
|
||||
|
||||
@ -2,12 +2,25 @@ package org.vibecoders.moongazer.scenes;
|
||||
|
||||
import static org.vibecoders.moongazer.Constants.*;
|
||||
|
||||
import org.vibecoders.moongazer.managers.Assets;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
|
||||
|
||||
/**
|
||||
* Main menu scene.
|
||||
*/
|
||||
public class MainMenu extends Scene {
|
||||
Label textLabel;
|
||||
|
||||
public MainMenu() {
|
||||
super();
|
||||
var font = Assets.getFont("ui", 24);
|
||||
textLabel = new Label("Moongazer", new LabelStyle(font, Color.BLACK));
|
||||
textLabel.setPosition(WINDOW_WIDTH / 2f - textLabel.getWidth() / 2f, WINDOW_HEIGHT / 2f - textLabel.getHeight() / 2f);
|
||||
}
|
||||
/**
|
||||
* Renders the main menu scene.
|
||||
* @param batch The SpriteBatch to draw with.
|
||||
@ -15,5 +28,6 @@ public class MainMenu extends Scene {
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
batch.draw(TEXTURE_WHITE, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
textLabel.draw(batch, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
app/src/main/resources/fonts/H7GBKHeavy.ttf
Normal file
BIN
app/src/main/resources/fonts/H7GBKHeavy.ttf
Normal file
Binary file not shown.
Reference in New Issue
Block a user