feat: move text in replace text colors
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
package moe.nea.firmament.mixins.accessor;
|
||||
|
||||
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package moe.nea.firmament.features.texturepack
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.minecraft.client.MinecraftClient
|
||||
import net.minecraft.client.gui.DrawContext
|
||||
import net.minecraft.client.gui.screen.Screen
|
||||
import net.minecraft.client.gui.screen.ingame.HandledScreen
|
||||
@@ -8,12 +10,14 @@ import net.minecraft.client.render.RenderLayer
|
||||
import net.minecraft.resource.ResourceManager
|
||||
import net.minecraft.resource.SinglePreparationResourceReloader
|
||||
import net.minecraft.screen.slot.Slot
|
||||
import net.minecraft.text.Text
|
||||
import net.minecraft.util.Identifier
|
||||
import net.minecraft.util.profiler.Profiler
|
||||
import moe.nea.firmament.Firmament
|
||||
import moe.nea.firmament.annotations.Subscribe
|
||||
import moe.nea.firmament.events.FinalizeResourceManagerEvent
|
||||
import moe.nea.firmament.events.ScreenChangeEvent
|
||||
import moe.nea.firmament.features.texturepack.CustomTextColors.cache
|
||||
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen
|
||||
import moe.nea.firmament.util.ErrorUtil.intoCatch
|
||||
import moe.nea.firmament.util.IdentifierSerializer
|
||||
@@ -25,6 +29,8 @@ object CustomScreenLayouts : SinglePreparationResourceReloader<List<CustomScreen
|
||||
val predicates: Preds,
|
||||
val background: BackgroundReplacer? = null,
|
||||
val slots: List<SlotReplacer> = listOf(),
|
||||
val playerTitle: TitleReplacer = TitleReplacer(),
|
||||
val containerTitle: TitleReplacer = TitleReplacer()
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@@ -87,6 +93,55 @@ object CustomScreenLayouts : SinglePreparationResourceReloader<List<CustomScreen
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
enum class Alignment {
|
||||
@SerialName("left")
|
||||
LEFT,
|
||||
@SerialName("center")
|
||||
CENTER,
|
||||
@SerialName("right")
|
||||
RIGHT
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TitleReplacer(
|
||||
val x: Int = 0,
|
||||
val y: Int = 0,
|
||||
val align: Alignment = Alignment.LEFT,
|
||||
val replace: String? = null
|
||||
)
|
||||
|
||||
fun alignText(text: Text, x: Int, width: Int): Int {
|
||||
var currentText = mapReplaceText(text)
|
||||
val align = if (currentText.string == "Inventory") activeScreenOverride?.playerTitle?.align ?: Alignment.LEFT
|
||||
else activeScreenOverride?.containerTitle?.align ?: Alignment.LEFT
|
||||
|
||||
val textWidth = MinecraftClient.getInstance().textRenderer.getWidth(Text.literal(currentText.string))
|
||||
|
||||
|
||||
return when (align) {
|
||||
Alignment.LEFT -> x
|
||||
Alignment.CENTER -> x + (width - textWidth) / 2
|
||||
Alignment.RIGHT -> x + (width - textWidth)
|
||||
}
|
||||
}
|
||||
|
||||
fun mapReplaceText(text: Text): Text {
|
||||
val replaceText = if (text.string == "Inventory") activeScreenOverride?.playerTitle?.replace ?: null else activeScreenOverride?.containerTitle?.replace ?: null
|
||||
if (replaceText == null) return text
|
||||
return Text.literal(replaceText)
|
||||
}
|
||||
|
||||
fun mapTextToX(text: Text, x: Int): Int {
|
||||
return x + if (text.string == "Inventory") activeScreenOverride?.playerTitle?.x
|
||||
?: 0 else activeScreenOverride?.containerTitle?.x ?: 0
|
||||
}
|
||||
|
||||
fun mapTextToY(text: Text, y: Int): Int {
|
||||
return y + if (text.string == "Inventory") activeScreenOverride?.playerTitle?.y
|
||||
?: 0 else activeScreenOverride?.containerTitle?.y ?: 0
|
||||
}
|
||||
|
||||
|
||||
@Subscribe
|
||||
fun onStart(event: FinalizeResourceManagerEvent) {
|
||||
|
||||
@@ -27,7 +27,8 @@ object CustomTextColors : SinglePreparationResourceReloader<CustomTextColors.Tex
|
||||
val baseOverride = TextOverride(
|
||||
StringMatcher.Equals("", false),
|
||||
defaultColor,
|
||||
false
|
||||
0,
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
@@ -35,7 +36,8 @@ object CustomTextColors : SinglePreparationResourceReloader<CustomTextColors.Tex
|
||||
data class TextOverride(
|
||||
val predicate: StringMatcher,
|
||||
val override: Int,
|
||||
val hidden: Boolean = false,
|
||||
val x: Int = 0,
|
||||
val y: Int = 0,
|
||||
)
|
||||
|
||||
@Subscribe
|
||||
@@ -43,14 +45,14 @@ object CustomTextColors : SinglePreparationResourceReloader<CustomTextColors.Tex
|
||||
event.resourceManager.registerReloader(this)
|
||||
}
|
||||
|
||||
val cache = WeakCache.memoize<Text, Optional<Int>>("CustomTextColor") { text ->
|
||||
val cache = WeakCache.memoize<Text, Optional<TextOverride>>("CustomTextColor") { text ->
|
||||
val override = textOverrides ?: return@memoize Optional.empty()
|
||||
Optional.of(override.overrides.find { it.predicate.matches(text) }?.override ?: override.defaultColor)
|
||||
Optional.ofNullable(override.overrides.find { it.predicate.matches(text) })
|
||||
}
|
||||
|
||||
fun mapTextColor(text: Text, oldColor: Int): Int {
|
||||
if (textOverrides == null) return oldColor
|
||||
return cache(text).getOrNull() ?: oldColor
|
||||
val override = cache(text).orElse(null)
|
||||
return override?.override ?: textOverrides?.defaultColor ?: oldColor
|
||||
}
|
||||
|
||||
override fun prepare(
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package moe.nea.firmament.mixins.custommodels;
|
||||
|
||||
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||
import moe.nea.firmament.features.texturepack.CustomScreenLayouts;
|
||||
import moe.nea.firmament.features.texturepack.CustomTextColors;
|
||||
import moe.nea.firmament.mixins.accessor.AccessorHandledScreen;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.ingame.AnvilScreen;
|
||||
@@ -31,7 +32,8 @@ public class ReplaceTextColorInHandledScreen {
|
||||
expect = 0,
|
||||
require = 0)
|
||||
private int replaceTextColorWithVariableShadow(DrawContext instance, TextRenderer textRenderer, Text text, int x, int y, int color, boolean shadow, Operation<Integer> original) {
|
||||
return original.call(instance, textRenderer, text, x, y, CustomTextColors.INSTANCE.mapTextColor(text, color), shadow);
|
||||
int width = ((AccessorHandledScreen) this).getBackgroundWidth_Firmament();
|
||||
return original.call(instance, textRenderer, CustomScreenLayouts.INSTANCE.mapReplaceText(text), CustomScreenLayouts.INSTANCE.alignText(text, CustomScreenLayouts.INSTANCE.mapTextToX(text, x), width), CustomScreenLayouts.INSTANCE.mapTextToY(text, y), CustomTextColors.INSTANCE.mapTextColor(text, color), shadow);
|
||||
}
|
||||
|
||||
@WrapOperation(
|
||||
@@ -42,7 +44,8 @@ public class ReplaceTextColorInHandledScreen {
|
||||
expect = 0,
|
||||
require = 0)
|
||||
private int replaceTextColorWithShadow(DrawContext instance, TextRenderer textRenderer, Text text, int x, int y, int color, Operation<Integer> original) {
|
||||
return original.call(instance, textRenderer, text, x, y, CustomTextColors.INSTANCE.mapTextColor(text, color));
|
||||
int width = ((AccessorHandledScreen) this).getBackgroundWidth_Firmament();
|
||||
return original.call(instance, textRenderer, CustomScreenLayouts.INSTANCE.mapReplaceText(text), CustomScreenLayouts.INSTANCE.alignText(text, CustomScreenLayouts.INSTANCE.mapTextToX(text, x), width), CustomScreenLayouts.INSTANCE.mapTextToY(text, y), CustomTextColors.INSTANCE.mapTextColor(text, color));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mixin(AbstractFurnaceScreen.class)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package moe.nea.firmament.mixins.custommodels.screenlayouts;
|
||||
|
||||
|
||||
import moe.nea.firmament.features.texturepack.CustomScreenLayouts;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.screen.ingame.*;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package moe.nea.firmament.mixins.custommodels.screenlayouts;
|
||||
|
||||
|
||||
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
|
||||
import moe.nea.firmament.features.texturepack.CustomScreenLayouts;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
|
||||
Reference in New Issue
Block a user