Search in sources :

Example 1 with MinecraftClientAccessor

use of com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor in project SpeedRunIGT by RedLime.

the class MinecraftClientMixin method onInit.

/**
 * Add import font system
 */
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/debug/DebugRenderer;<init>(Lnet/minecraft/client/MinecraftClient;)V", shift = At.Shift.BEFORE))
public void onInit(RunArgs args, CallbackInfo ci) {
    this.resourceManager.registerListener(new SinglePreparationResourceReloadListener<Map<Identifier, List<Font>>>() {

        @Override
        protected Map<Identifier, List<Font>> prepare(ResourceManager manager, Profiler profiler) {
            SpeedRunIGT.FONT_MAPS.clear();
            try {
                HashMap<Identifier, List<Font>> map = new HashMap<>();
                File[] fontFiles = SpeedRunIGT.FONT_PATH.toFile().listFiles();
                if (fontFiles == null)
                    return new HashMap<>();
                for (File file : Arrays.stream(fontFiles).filter(file -> file.getName().endsWith(".ttf")).collect(Collectors.toList())) {
                    File config = SpeedRunIGT.FONT_PATH.resolve(file.getName().substring(0, file.getName().length() - 4) + ".json").toFile();
                    if (config.exists()) {
                        FontUtils.addFont(map, file, config);
                    } else {
                        FontUtils.addFont(map, file, null);
                    }
                }
                return map;
            } catch (Throwable e) {
                return new HashMap<>();
            }
        }

        @Override
        protected void apply(Map<Identifier, List<Font>> loader, ResourceManager manager, Profiler profiler) {
            try {
                FontManagerAccessor fontManager = (FontManagerAccessor) ((MinecraftClientAccessor) MinecraftClient.getInstance()).getFontManager();
                for (Map.Entry<Identifier, List<Font>> listEntry : loader.entrySet()) {
                    FontStorage fontStorage = new FontStorage(fontManager.getTextureManager(), listEntry.getKey());
                    fontStorage.setFonts(listEntry.getValue());
                    fontManager.getFontStorages().put(listEntry.getKey(), fontStorage);
                }
                TimerDrawer.fontHeightMap.clear();
            } catch (Throwable e) {
                SpeedRunIGT.error("Error! failed import timer fonts!");
                e.printStackTrace();
            }
        }
    });
}
Also used : MinecraftClientAccessor(com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor) HashMap(java.util.HashMap) ReloadableResourceManager(net.minecraft.resource.ReloadableResourceManager) ResourceManager(net.minecraft.resource.ResourceManager) Font(net.minecraft.client.font.Font) Identifier(net.minecraft.util.Identifier) Profiler(net.minecraft.util.profiler.Profiler) FontStorage(net.minecraft.client.font.FontStorage) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with MinecraftClientAccessor

use of com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor in project SpeedRunIGT by RedLime.

the class TimerDrawer method draw.

public void draw() {
    if (!toggle)
        return;
    client.getProfiler().push("create");
    MutableText igtText = getIGTText();
    MutableText rtaText = getRTAText();
    client.getProfiler().swap("font");
    // 폰트 조정
    float fontHeight = 8;
    FontManagerAccessor fontManager = (FontManagerAccessor) ((MinecraftClientAccessor) client).getFontManager();
    if (getTimerFont() != MinecraftClient.DEFAULT_FONT_ID && fontManager.getFontStorages().containsKey(getTimerFont())) {
        rtaText.setStyle(rtaText.getStyle().withFont(getTimerFont()));
        igtText.setStyle(igtText.getStyle().withFont(getTimerFont()));
        fontHeight = fontHeightMap.computeIfAbsent(getTimerFont().toString(), key -> {
            RenderableGlyph glyph = ((FontStorageAccessor) fontManager.getFontStorages().get(getTimerFont())).invokeRenderableGlyph('I');
            return glyph.getHeight() / glyph.getOversample();
        });
    }
    // 초기 값 조정
    client.getProfiler().swap("init");
    TimerElement igtTimerElement = new TimerElement();
    TimerElement rtaTimerElement = new TimerElement();
    rtaTimerElement.init(rtaXPos, rtaYPos, rtaScale, rtaText, rtaColor, rtaDecoration, fontHeight);
    igtTimerElement.init(igtXPos, igtYPos, igtScale, igtText, igtColor, igtDecoration, fontHeight);
    MatrixStack matrixStack = new MatrixStack();
    // 배경 렌더
    client.getProfiler().swap("background");
    if (bgOpacity > 0.01f) {
        Position rtaMin = new Position(rtaTimerElement.getPosition().getX() - rtaPadding, rtaTimerElement.getPosition().getY() - rtaPadding);
        Position rtaMax = new Position(rtaMin.getX() + rtaTimerElement.getScaledTextWidth() + ((rtaPadding - 1) + rtaPadding), rtaMin.getY() + rtaTimerElement.getScaledTextHeight() + ((rtaPadding - 1) + rtaPadding));
        Position igtMin = new Position(igtTimerElement.getPosition().getX() - igtPadding, igtTimerElement.getPosition().getY() - igtPadding);
        Position igtMax = new Position(igtMin.getX() + igtTimerElement.getScaledTextWidth() + ((igtPadding - 1) + igtPadding), igtMin.getY() + igtTimerElement.getScaledTextHeight() + ((igtPadding - 1) + igtPadding));
        int opacity = BackgroundHelper.ColorMixer.getArgb((int) (bgOpacity * 255), 0, 0, 0);
        if (rtaMin.getX() < igtMax.getX() && rtaMin.getY() < igtMax.getY() && igtMin.getX() < rtaMax.getX() && igtMin.getY() < rtaMax.getY()) {
            DrawableHelper.fill(matrixStack, Math.min(rtaMin.getX(), igtMin.getX()), Math.min(rtaMin.getY(), igtMin.getY()), Math.max(rtaMax.getX(), igtMax.getX()), Math.max(rtaMax.getY(), igtMax.getY()), opacity);
        } else {
            if (rtaScale != 0)
                DrawableHelper.fill(matrixStack, rtaMin.getX(), rtaMin.getY(), rtaMax.getX(), rtaMax.getY(), opacity);
            if (igtScale != 0)
                DrawableHelper.fill(matrixStack, igtMin.getX(), igtMin.getY(), igtMax.getX(), igtMax.getY(), opacity);
        }
    }
    // 렌더
    client.getProfiler().swap("draw");
    if (igtScale != 0)
        igtTimerElement.draw(matrixStack, translateZ);
    if (rtaScale != 0)
        rtaTimerElement.draw(matrixStack, translateZ);
    client.getProfiler().pop();
}
Also used : MutableText(net.minecraft.text.MutableText) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) SpeedRunOption(com.redlimerl.speedrunigt.option.SpeedRunOption) SpeedRunOptions(com.redlimerl.speedrunigt.option.SpeedRunOptions) LiteralText(net.minecraft.text.LiteralText) FontStorageAccessor(com.redlimerl.speedrunigt.mixins.access.FontStorageAccessor) MatrixStack(net.minecraft.client.util.math.MatrixStack) HashMap(java.util.HashMap) TimerDecimals(com.redlimerl.speedrunigt.option.SpeedRunOptions.TimerDecimals) MinecraftClientAccessor(com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor) DrawableHelper(net.minecraft.client.gui.DrawableHelper) TimerDecoration(com.redlimerl.speedrunigt.option.SpeedRunOptions.TimerDecoration) RenderableGlyph(net.minecraft.client.font.RenderableGlyph) MutableText(net.minecraft.text.MutableText) Identifier(net.minecraft.util.Identifier) MinecraftClient(net.minecraft.client.MinecraftClient) BackgroundHelper(net.minecraft.client.gui.hud.BackgroundHelper) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) MatrixStack(net.minecraft.client.util.math.MatrixStack) RenderableGlyph(net.minecraft.client.font.RenderableGlyph)

Aggregations

FontManagerAccessor (com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor)2 MinecraftClientAccessor (com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor)2 HashMap (java.util.HashMap)2 Identifier (net.minecraft.util.Identifier)2 FontStorageAccessor (com.redlimerl.speedrunigt.mixins.access.FontStorageAccessor)1 SpeedRunOption (com.redlimerl.speedrunigt.option.SpeedRunOption)1 SpeedRunOptions (com.redlimerl.speedrunigt.option.SpeedRunOptions)1 TimerDecimals (com.redlimerl.speedrunigt.option.SpeedRunOptions.TimerDecimals)1 TimerDecoration (com.redlimerl.speedrunigt.option.SpeedRunOptions.TimerDecoration)1 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 MinecraftClient (net.minecraft.client.MinecraftClient)1 Font (net.minecraft.client.font.Font)1 FontStorage (net.minecraft.client.font.FontStorage)1 RenderableGlyph (net.minecraft.client.font.RenderableGlyph)1 DrawableHelper (net.minecraft.client.gui.DrawableHelper)1 BackgroundHelper (net.minecraft.client.gui.hud.BackgroundHelper)1 MatrixStack (net.minecraft.client.util.math.MatrixStack)1 ReloadableResourceManager (net.minecraft.resource.ReloadableResourceManager)1