Enable loading fonts to GUI
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public interface Font {
|
||||
String getName();
|
||||
|
||||
ByteBuffer getByteBuffer();
|
||||
}
|
||||
@@ -14,5 +14,11 @@ public interface GUI extends Renderable, Disposable {
|
||||
|
||||
void fillColor(Color color);
|
||||
|
||||
void setFontFace(String fontUid);
|
||||
|
||||
void setFontSize(float size);
|
||||
|
||||
void putText(float x, float y, CharSequence text);
|
||||
|
||||
Color createColor(float red, float green, float blue, float alpha);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,52 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.Font;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.gui.asset.FontAsset;
|
||||
import com.bartlomiejpluta.base.engine.world.tileset.asset.TileSetAsset;
|
||||
import com.bartlomiejpluta.base.engine.gui.model.DefaultFont;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
import com.bartlomiejpluta.base.engine.util.res.ResourcesManager;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DefaultFontManager implements FontManager {
|
||||
private final Map<String, TileSetAsset> assets = new HashMap<>();
|
||||
private final Map<String, FontAsset> assets = new HashMap<>();
|
||||
private final Map<String, ByteBuffer> fontBuffers = new HashMap<>();
|
||||
private final ProjectConfiguration configuration;
|
||||
private final ResourcesManager resourcesManager;
|
||||
|
||||
@Override
|
||||
public void registerAsset(FontAsset asset) {
|
||||
log.info("Registering [{}] font asset under UID: [{}]", asset.getSource(), asset.getUid());
|
||||
assets.put(asset.getUid(), asset);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer loadObject(String uid) {
|
||||
// TODO(Implement some kind of font object)
|
||||
// The method should return useful resource associated with the font
|
||||
// that can be easily consumed by NanoVG.
|
||||
// Likely it will be just integer with a unique font ID.
|
||||
return null;
|
||||
public Font loadObject(String uid) {
|
||||
var buffer = fontBuffers.get(uid);
|
||||
|
||||
if (buffer == null) {
|
||||
var asset = assets.get(uid);
|
||||
|
||||
if (asset == null) {
|
||||
throw new AppException("The font asset with UID: [%s] does not exist", uid);
|
||||
}
|
||||
|
||||
var source = configuration.projectFile("fonts", asset.getSource());
|
||||
buffer = resourcesManager.loadResourceAsByteBuffer(source);
|
||||
log.info("Loading font from assets to cache under the key: [{}]", uid);
|
||||
fontBuffers.put(uid, buffer);
|
||||
}
|
||||
|
||||
return new DefaultFont(uid, buffer.duplicate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.Font;
|
||||
import com.bartlomiejpluta.base.engine.common.manager.AssetManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.asset.FontAsset;
|
||||
|
||||
public interface FontManager extends AssetManager<FontAsset, Integer> {
|
||||
public interface FontManager extends AssetManager<FontAsset, Font> {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.Font;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class DefaultFont implements Font {
|
||||
private final String name;
|
||||
private final ByteBuffer byteBuffer;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bartlomiejpluta.base.engine.gui.render;
|
||||
package com.bartlomiejpluta.base.engine.gui.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.Color;
|
||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||
@@ -7,11 +7,15 @@ import com.bartlomiejpluta.base.api.game.gui.Widget;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.model.NanoVGColorAdapter;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.lwjgl.nanovg.NanoVG.*;
|
||||
import static org.lwjgl.nanovg.NanoVGGL3.*;
|
||||
@@ -25,6 +29,13 @@ public class NanoVGGUI implements GUI {
|
||||
private Widget root;
|
||||
|
||||
private final List<NanoVGColorAdapter> createdColors = new LinkedList<>();
|
||||
private final Set<String> loadedFonts = new HashSet<>();
|
||||
|
||||
private final FontManager fontManager;
|
||||
|
||||
public NanoVGGUI(FontManager fontManager) {
|
||||
this.fontManager = fontManager;
|
||||
}
|
||||
|
||||
public void init(Screen screen) {
|
||||
context = nvgCreate(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
|
||||
@@ -61,6 +72,11 @@ public class NanoVGGUI implements GUI {
|
||||
nvgFill(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putText(float x, float y, CharSequence text) {
|
||||
nvgText(context, x, y, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color createColor(float red, float green, float blue, float alpha) {
|
||||
var color = new NanoVGColorAdapter(red, green, blue, alpha);
|
||||
@@ -68,8 +84,25 @@ public class NanoVGGUI implements GUI {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFontFace(String fontUid) {
|
||||
if (!loadedFonts.contains(fontUid)) {
|
||||
var font = fontManager.loadObject(fontUid);
|
||||
nvgCreateFontMem(context, fontUid, font.getByteBuffer(), 0);
|
||||
loadedFonts.add(fontUid);
|
||||
}
|
||||
|
||||
nvgFontFace(context, fontUid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFontSize(float size) {
|
||||
nvgFontSize(context, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
createdColors.forEach(NanoVGColorAdapter::dispose);
|
||||
nvgDelete(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.bartlomiejpluta.base.api.internal.gc.Cleanable;
|
||||
import com.bartlomiejpluta.base.api.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.api.internal.render.ShaderManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.manager.FontManager;
|
||||
import com.bartlomiejpluta.base.engine.gui.render.NanoVGGUI;
|
||||
import com.bartlomiejpluta.base.engine.project.loader.ClassLoader;
|
||||
import com.bartlomiejpluta.base.engine.world.entity.manager.EntityManager;
|
||||
@@ -32,6 +33,7 @@ public class RenderableContext implements Context, Updatable, Renderable, Cleana
|
||||
private final EntityManager entityManager;
|
||||
private final ImageManager imageManager;
|
||||
private final MapManager mapManager;
|
||||
private final FontManager fontManager;
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
@Getter
|
||||
@@ -70,7 +72,7 @@ public class RenderableContext implements Context, Updatable, Renderable, Cleana
|
||||
|
||||
@Override
|
||||
public GUI newGUI() {
|
||||
var gui = new NanoVGGUI();
|
||||
var gui = new NanoVGGUI(fontManager);
|
||||
guis.add(gui);
|
||||
gui.init(screen);
|
||||
return gui;
|
||||
|
||||
Reference in New Issue
Block a user