| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.font.GlyphInfo;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 6 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.function.Function;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.gui.Font;
|
| 13 | import net.minecraft.client.gui.GlyphSource;
|
| 14 | import net.minecraft.client.gui.font.glyphs.BakedGlyph;
|
| 15 | import net.minecraft.client.renderer.rendertype.RenderType;
|
| 16 | import net.minecraft.client.renderer.texture.TextureAtlas;
|
| 17 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 18 | import net.minecraft.network.chat.Style;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import org.joml.Matrix4fc;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class AtlasGlyphProvider {
|
| 24 | private static final GlyphInfo GLYPH_INFO = GlyphInfo.simple(8.0F);
|
| 25 | private final TextureAtlas atlas;
|
| 26 | private final GlyphRenderTypes renderTypes;
|
| 27 | private final GlyphSource missingWrapper;
|
| 28 | private final Map<Identifier, GlyphSource> wrapperCache = new HashMap<>();
|
| 29 | private final Function<Identifier, GlyphSource> spriteResolver;
|
| 30 |
|
| 31 | public AtlasGlyphProvider(final TextureAtlas atlas) {
|
| 32 | this.atlas = atlas;
|
| 33 | this.renderTypes = GlyphRenderTypes.createForColorTexture(atlas.location());
|
| 34 | TextureAtlasSprite missingSprite = atlas.missingSprite();
|
| 35 | this.missingWrapper = this.createSprite(missingSprite);
|
| 36 | this.spriteResolver = id -> {
|
| 37 | TextureAtlasSprite sprite = atlas.getSprite(id);
|
| 38 | return sprite == missingSprite ? this.missingWrapper : this.createSprite(sprite);
|
| 39 | };
|
| 40 | }
|
| 41 |
|
| 42 | public GlyphSource sourceForSprite(final Identifier spriteId) {
|
| 43 | return this.wrapperCache.computeIfAbsent(spriteId, this.spriteResolver);
|
| 44 | }
|
| 45 |
|
| 46 | private GlyphSource createSprite(final TextureAtlasSprite sprite) {
|
| 47 | return new SingleSpriteSource(
|
| 48 | new BakedGlyph() {
|
| 49 | @Override
|
| 50 | public GlyphInfo info() {
|
| 51 | return AtlasGlyphProvider.GLYPH_INFO;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public TextRenderable.Styled createGlyph(
|
| 56 | final float x, final float y, final int color, final int shadowColor, final Style style, final float boldOffset, final float shadowOffset
|
| 57 | ) {
|
| 58 | return new AtlasGlyphProvider.Instance(
|
| 59 | AtlasGlyphProvider.this.renderTypes, AtlasGlyphProvider.this.atlas.getTextureView(), sprite, x, y, color, shadowColor, shadowOffset, style
|
| 60 | );
|
| 61 | }
|
| 62 | }
|
| 63 | );
|
| 64 | }
|
| 65 |
|
| 66 | @Environment(EnvType.CLIENT)
|
| 67 | private record Instance(
|
| 68 | GlyphRenderTypes renderTypes,
|
| 69 | GpuTextureView textureView,
|
| 70 | TextureAtlasSprite sprite,
|
| 71 | float x,
|
| 72 | float y,
|
| 73 | int color,
|
| 74 | int shadowColor,
|
| 75 | float shadowOffset,
|
| 76 | Style style
|
| 77 | ) implements PlainTextRenderable {
|
| 78 | @Override
|
| 79 | public void renderSprite(
|
| 80 | final Matrix4fc pose, final VertexConsumer buffer, final int packedLightCoords, final float offsetX, final float offsetY, final float z, final int color
|
| 81 | ) {
|
| 82 | float x0 = offsetX + this.left();
|
| 83 | float x1 = offsetX + this.right();
|
| 84 | float y0 = offsetY + this.top();
|
| 85 | float y1 = offsetY + this.bottom();
|
| 86 | buffer.addVertex(pose, x0, y0, z).setUv(this.sprite.getU0(), this.sprite.getV0()).setColor(color).setLight(packedLightCoords);
|
| 87 | buffer.addVertex(pose, x0, y1, z).setUv(this.sprite.getU0(), this.sprite.getV1()).setColor(color).setLight(packedLightCoords);
|
| 88 | buffer.addVertex(pose, x1, y1, z).setUv(this.sprite.getU1(), this.sprite.getV1()).setColor(color).setLight(packedLightCoords);
|
| 89 | buffer.addVertex(pose, x1, y0, z).setUv(this.sprite.getU1(), this.sprite.getV0()).setColor(color).setLight(packedLightCoords);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public RenderType renderType(final Font.DisplayMode displayMode) {
|
| 94 | return this.renderTypes.select(displayMode);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public RenderPipeline guiPipeline() {
|
| 99 | return this.renderTypes.guiPipeline();
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|