top.hendrixshen.magiclib.util.minecraft.render.TextRenderUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magiclib-minecraft-api-1.19.3-fabric Show documentation
Show all versions of magiclib-minecraft-api-1.19.3-fabric Show documentation
Unleash magic into Minecraft, infuse souls, ascend to heaven!
/*
* This file is part of the TweakerMore project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2023 Fallen_Breath and contributors
*
* TweakerMore is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TweakerMore is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TweakerMore. If not, see .
*/
package top.hendrixshen.magiclib.util.minecraft.render;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.class_2583;
import net.minecraft.class_310;
import net.minecraft.class_327;
import net.minecraft.class_5223;
import net.minecraft.class_5225;
import net.minecraft.class_5481;
//#if MC > 11502
import com.google.common.collect.Lists;
import org.apache.commons.lang3.mutable.MutableFloat;
import org.apache.commons.lang3.tuple.Triple;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.mixin.minecraft.accessor.StringSplitterAccessor;
import java.util.List;
//#endif
/**
* Reference to TweakerMore
*/
@Environment(EnvType.CLIENT)
public class TextRenderUtil {
//#if MC > 11502
public static @NotNull class_5481 string2formattedCharSequence(String string) {
return visitor -> class_5223.method_27479(string, class_2583.field_24360, visitor);
}
public static @NotNull String formattedCharSequence2string(@NotNull class_5481 text) {
StringBuilder builder = new StringBuilder();
text.accept((index, style, codePoint) -> {
builder.append((char) codePoint);
return true;
});
return builder.toString();
}
public static class_5481 trim(@NotNull class_5481 text, int maxWidth,
PostTrimModifier postTrimModifier) {
class_327 font = class_310.method_1551().field_1772;
class_5225.class_5231 widthRetriever = ((StringSplitterAccessor) font.method_27527())
.magiclib$widthProvider();
List> elements = Lists.newArrayList();
MutableFloat width = new MutableFloat(0);
boolean hasTrimmed = text.accept((index, style, codePoint) -> {
width.add(widthRetriever.getWidth(codePoint, style));
boolean ok = width.getValue() <= maxWidth;
if (ok) {
elements.add(Triple.of(index, style, codePoint));
}
return ok;
});
class_5481 trimmedText = formattedCharSink -> {
for (Triple element : elements) {
if (!formattedCharSink.accept(element.getLeft(), element.getMiddle(), element.getRight())) {
return false;
}
}
return true;
};
if (hasTrimmed) {
trimmedText = postTrimModifier.modify(trimmedText);
}
return trimmedText;
}
public static class_5481 trim(class_5481 text, int maxWidth) {
return TextRenderUtil.trim(text, maxWidth, t -> t);
}
//#endif
public static String trim(String text, int maxWidth, PostTrimModifier postTrimModifier) {
class_310 mc = class_310.method_1551();
//#if MC > 11502
String trimmedText = mc.field_1772.method_27523(text, maxWidth);
//#else
//$$ String trimmedText = mc.font.substrByWidth(text, maxWidth, false);
//#endif
if (trimmedText.length() < text.length()) {
trimmedText = postTrimModifier.modify(trimmedText);
}
return trimmedText;
}
public static String trim(String text, int maxWidth) {
return TextRenderUtil.trim(text, maxWidth, t -> t);
}
@FunctionalInterface
public interface PostTrimModifier {
T modify(T trimmedText);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy