Skip to content

Commit

Permalink
feat: custom tooltip border colors & background color
Browse files Browse the repository at this point in the history
* fixes #12
  • Loading branch information
UltimatChamp committed Feb 3, 2025
1 parent c7b2f28 commit a5870bb
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public void onInitializeClient() {

list.add(1, new EffectsTooltipComponent(stack));

int color = TooltipHelper.borderColorProvider.getItemBorderColor(stack);
if (stack.getItem() instanceof ArmorItem || stack.getItem() instanceof EntityBucketItem || stack.getItem() instanceof SpawnEggItem) {
list.add(new ModelViewerComponent(stack, 0xff000000 | color));
list.add(new ModelViewerComponent(stack));
} else {
list.add(new ColorBorderComponent(0xff000000 | color));
list.add(new ColorBorderComponent(stack));
}

if (MinecraftClient.getInstance().options.advancedItemTooltips) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static Text getDisplayName(ItemStack stack) {
return displayNameProvider.getDisplayName(stack);
}

public static int getItemBorderColor(ItemStack stack) {
return borderColorProvider.getItemBorderColor(stack);
}

private static class DefaultItemRarityNameProvider implements ItemRarityNameProvider {
@Override
public Text getRarityName(ItemStack stack) {
Expand Down Expand Up @@ -54,15 +58,15 @@ public int getItemBorderColor(ItemStack stack) {
color = TranslationStringColorParser.getColorFromTranslation(getDisplayName(stack));
}

if (color == null || color == 0xFFFFFF) {
if (color == null || color == -1) {
//? if >1.20.4 {
color = stack.getRarity().getFormatting().getColorValue();
//?} else {
/*color = stack.getRarity().formatting.getColorValue();
*///?}
if (color == null || color == 0xFFFFFF) color = 0xFFFFFFFF;
}

if (color == null) color = 0xFFFFFF;
return color;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
package dev.ultimatchamp.enhancedtooltips.component;

import dev.ultimatchamp.enhancedtooltips.TooltipHelper;
import dev.ultimatchamp.enhancedtooltips.config.EnhancedTooltipsConfig;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Rarity;

public class ColorBorderComponent extends TooltipBackgroundComponent {
private final int color;
private final ItemStack stack;

public ColorBorderComponent(int color) {
this.color = color;
public ColorBorderComponent(ItemStack stack) {
this.stack = stack;
}

@Override
protected void renderBorder(DrawContext context, int x, int y, int width, int height, int z, int page) {
int endColor = 1347420415;
int startColor = 0xff000000 | TooltipHelper.getItemBorderColor(stack);
if (TooltipHelper.getItemBorderColor(stack) == -1) startColor = EnhancedTooltipsConfig.BorderColor.COMMON.getColor().getRGB();

renderVerticalLine(context, x, y, height - 2, z, color, endColor);
renderVerticalLine(context, x + width - 1, y, height - 2, z, color, endColor);
renderHorizontalLine(context, x, y - 1, width, z, color);
int endColor = EnhancedTooltipsConfig.BorderColor.END_COLOR.getColor().getRGB();

if (EnhancedTooltipsConfig.load().borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM) {
if (stack.getRarity() == Rarity.UNCOMMON) {
startColor = EnhancedTooltipsConfig.load().customBorderColors.uncommon.getRGB();
} else if (stack.getRarity() == Rarity.RARE) {
startColor = EnhancedTooltipsConfig.load().customBorderColors.rare.getRGB();
} else if (stack.getRarity() == Rarity.EPIC) {
startColor = EnhancedTooltipsConfig.load().customBorderColors.epic.getRGB();
} else {
startColor = EnhancedTooltipsConfig.load().customBorderColors.common.getRGB();
}

endColor = EnhancedTooltipsConfig.load().customBorderColors.endColor.getRGB();
}

renderVerticalLine(context, x, y, height - 2, z, startColor, endColor);
renderVerticalLine(context, x + width - 1, y, height - 2, z, startColor, endColor);
renderHorizontalLine(context, x, y - 1, width, z, startColor);
renderHorizontalLine(context, x, y - 1 + height - 1, width, z, endColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class ModelViewerComponent extends ColorBorderComponent {
private final ItemStack stack;
private final EnhancedTooltipsConfig config;

public ModelViewerComponent(ItemStack stack, int color) {
super(color);
public ModelViewerComponent(ItemStack stack) {
super(stack);
this.stack = stack;
this.config = EnhancedTooltipsConfig.load();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.ultimatchamp.enhancedtooltips.component;

import dev.ultimatchamp.enhancedtooltips.config.EnhancedTooltipsConfig;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.TooltipComponent;
Expand All @@ -13,39 +14,41 @@ public void render(DrawContext context, int x, int y, int width, int height, int
int k = width + INNER_PADDING * 2;
int l = height + INNER_PADDING * 2;

renderHorizontalLine(context, i, j - 1, k, z, -267386864);
renderHorizontalLine(context, i, j + l, k, z, -267386864);
int bgColor = EnhancedTooltipsConfig.load().backgroundColor.getRGB();

renderHorizontalLine(context, i, j - 1, k, z, bgColor);
renderHorizontalLine(context, i, j + l, k, z, bgColor);
renderRectangle(context, i, j, k, l, z);
renderVerticalLine(context, i - 1, j, l, z);
renderVerticalLine(context, i + k, j, l, z);
renderVerticalLine(context, i - 1, j, l, z, bgColor, bgColor);
renderVerticalLine(context, i + k, j, l, z, bgColor, bgColor);
renderBorder(context, i, j + 1, k, l, z, page);
}

protected void renderBorder(DrawContext context, int x, int y, int width, int height, int z, int page) {
int startColor = 1347420415;
int endColor = 1347420415;
int startColor = EnhancedTooltipsConfig.BorderColor.COMMON.getColor().getRGB();
int endColor = EnhancedTooltipsConfig.BorderColor.END_COLOR.getColor().getRGB();

if (EnhancedTooltipsConfig.load().borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM) {
startColor = EnhancedTooltipsConfig.load().customBorderColors.common.getRGB();
endColor = EnhancedTooltipsConfig.load().customBorderColors.endColor.getRGB();
}

renderVerticalLine(context, x, y, height - 2, z, startColor, endColor);
renderVerticalLine(context, x + width - 1, y, height - 2, z, startColor, endColor);
renderHorizontalLine(context, x, y - 1, width, z, startColor);
renderHorizontalLine(context, x, y - 1 + height - 1, width, z, endColor);
}

private void renderVerticalLine(DrawContext context, int x, int y, int height, int z) {
context.fill(x, y, x + 1, y + height, z, -267386864);
}

protected void renderVerticalLine(DrawContext context, int x, int y, int height, int z, int startColor, int endColor) {
if (startColor == -1 || startColor == 0xffffff) startColor = endColor;
context.fillGradient(x, y, x + 1, y + height, z, startColor, endColor);
}

protected void renderHorizontalLine(DrawContext context, int x, int y, int width, int z, int color) {
if (color == -1 || color == 0xffffff) color = 1347420415;
context.fill(x, y, x + width, y + 1, z, color);
}

protected void renderRectangle(DrawContext context, int x, int y, int width, int height, int z) {
context.fill(x, y, x + width, y + height, z, -267386864);
context.fill(x, y, x + width, y + height, z, EnhancedTooltipsConfig.load().backgroundColor.getRGB());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.TranslatableOption;

import java.awt.*;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
Expand All @@ -24,12 +25,13 @@ public class EnhancedTooltipsConfig {
public boolean itemBadges = true;
//?}

@Comment("RARITY/ITEM_NAME (default: RARITY)")
@Comment("-> Border\nRARITY/ITEM_NAME/CUSTOM (default: RARITY)")
public BorderColorMode borderColor = BorderColorMode.RARITY;

public enum BorderColorMode implements TranslatableOption {
RARITY(0, "enhancedtooltips.config.borderColor.rarity"),
ITEM_NAME(1, "enhancedtooltips.config.borderColor.itemName");
ITEM_NAME(1, "enhancedtooltips.config.borderColor.itemName"),
CUSTOM(2, "generator.custom");

private final int id;
private final String translationKey;
Expand All @@ -48,6 +50,46 @@ public String getTranslationKey() {
}
}

public CustomBorderColorsConfig customBorderColors = new CustomBorderColorsConfig();

public static class CustomBorderColorsConfig {
@Comment("(default: #5000FF50)")
public Color common = BorderColor.COMMON.getColor();

@Comment("(default: #FFFF55FF)")
public Color uncommon = BorderColor.UNCOMMON.getColor();

@Comment("(default: #55FFFFFF)")
public Color rare = BorderColor.RARE.getColor();

@Comment("(default: #FF00FFFF)")
public Color epic = BorderColor.EPIC.getColor();

@Comment("(default: #5000FF50)")
public Color endColor = BorderColor.END_COLOR.getColor();
}

public enum BorderColor {
COMMON(0x505000FF),
UNCOMMON(0xFFFFFF55),
RARE(0xFF55FFFF),
EPIC(0xFFFF00FF),
END_COLOR(0x505000FF);

private final int rgb;

BorderColor(int rgb) {
this.rgb = rgb;
}

public Color getColor() {
return new Color(rgb, true);
}
}

@Comment("-> Background\n(default: #100010F0)")
public Color backgroundColor = new Color(0xF0100010, true);

@Comment("-> Food & Drinks\n(default: true)")
public boolean hungerTooltip = true;

Expand All @@ -59,8 +101,8 @@ public String getTranslationKey() {

public enum EffectsTooltipMode implements TranslatableOption {
OFF(0, "options.off"),
WITHOUT_ICONS(0, "enhancedtooltips.config.effectsTooltip.withoutIcons"),
WITH_ICONS(1, "enhancedtooltips.config.effectsTooltip.withIcons");
WITHOUT_ICONS(1, "enhancedtooltips.config.effectsTooltip.withoutIcons"),
WITH_ICONS(2, "enhancedtooltips.config.effectsTooltip.withIcons");

private final int id;
private final String translationKey;
Expand Down Expand Up @@ -94,7 +136,7 @@ public String getTranslationKey() {
public enum DurabilityTooltipMode implements TranslatableOption {
OFF(0, "options.off"),
VALUE(1, "enhancedtooltips.config.durabilityTooltip.value"),
PERCENTAGE(0, "enhancedtooltips.config.durabilityTooltip.percentage");
PERCENTAGE(2, "enhancedtooltips.config.durabilityTooltip.percentage");

private final int id;
private final String translationKey;
Expand All @@ -116,7 +158,26 @@ public String getTranslationKey() {
@Comment("(default: false)")
public boolean durabilityBar = false;

private static final Jankson JANKSON = Jankson.builder().build();
private static final Jankson JANKSON = Jankson.builder()
.registerSerializer(Color.class, (color, marshaller) -> new JsonPrimitive(String.format("#%02X%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()))) // in RRGGBBAA
.registerDeserializer(JsonPrimitive.class, Color.class, (json, marshaller) -> {
String hex = json.asString();
if (hex.startsWith("#")) hex = hex.substring(1);

if (hex.length() == 8) {
int r = Integer.parseInt(hex.substring(0, 2), 16); // Red
int g = Integer.parseInt(hex.substring(2, 4), 16); // Green
int b = Integer.parseInt(hex.substring(4, 6), 16); // Blue
int a = Integer.parseInt(hex.substring(6, 8), 16); // Alpha
return new Color(r, g, b, a);
} else if (hex.length() == 6) { // no alpha
return Color.decode("#" + hex);
} else {
throw new IllegalArgumentException("Invalid color format: " + json.asString());
}
})
.build();

private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("enhancedtooltips.json5");

private static EnhancedTooltipsConfig cachedConfig;
Expand Down Expand Up @@ -171,9 +232,7 @@ private static JsonObject ensureDefaults(JsonObject configJson) {
var defaultConfig = new EnhancedTooltipsConfig();

for (var field : EnhancedTooltipsConfig.class.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (Modifier.isStatic(field.getModifiers())) continue;

try {
var fieldName = field.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.ultimatchamp.enhancedtooltips.config;

import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.ColorControllerBuilder;
import dev.isxander.yacl3.api.controller.TickBoxControllerBuilder;
import dev.isxander.yacl3.gui.controllers.cycling.EnumController;
import dev.isxander.yacl3.gui.controllers.slider.FloatSliderController;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

import java.awt.*;

public class EnhancedTooltipsGui {
public static Screen createConfigScreen(Screen parent) {
var config = EnhancedTooltipsConfig.load();
Expand Down Expand Up @@ -55,6 +58,9 @@ public static Screen createConfigScreen(Screen parent) {
.controller(TickBoxControllerBuilder::create)
.build())
//?}
.build())
.group(OptionGroup.createBuilder()
.name(Text.translatable("enhancedtooltips.config.group.border"))
.option(Option.<EnhancedTooltipsConfig.BorderColorMode>createBuilder()
.name(Text.translatable("enhancedtooltips.config.borderColor"))
.description(OptionDescription.createBuilder()
Expand All @@ -67,6 +73,68 @@ public static Screen createConfigScreen(Screen parent) {
)
.customController(opt -> new EnumController<>(opt, EnhancedTooltipsConfig.BorderColorMode.class))
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.rarity.common"))
.binding(
EnhancedTooltipsConfig.BorderColor.COMMON.getColor(),
() -> config.customBorderColors.common,
(value) -> config.customBorderColors.common = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.available(config.borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.rarity.uncommon"))
.binding(
EnhancedTooltipsConfig.BorderColor.UNCOMMON.getColor(),
() -> config.customBorderColors.uncommon,
(value) -> config.customBorderColors.uncommon = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.available(config.borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.rarity.rare"))
.binding(
EnhancedTooltipsConfig.BorderColor.RARE.getColor(),
() -> config.customBorderColors.rare,
(value) -> config.customBorderColors.rare = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.available(config.borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.rarity.epic"))
.binding(
EnhancedTooltipsConfig.BorderColor.EPIC.getColor(),
() -> config.customBorderColors.epic,
(value) -> config.customBorderColors.epic = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.available(config.borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM)
.build())
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.config.customBorderColors.endColor"))
.binding(
EnhancedTooltipsConfig.BorderColor.END_COLOR.getColor(),
() -> config.customBorderColors.endColor,
(value) -> config.customBorderColors.endColor = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.available(config.borderColor == EnhancedTooltipsConfig.BorderColorMode.CUSTOM)
.build())
.build())
.group(OptionGroup.createBuilder()
.name(Text.translatable("enhancedtooltips.config.group.background"))
.option(Option.<Color>createBuilder()
.name(Text.translatable("enhancedtooltips.config.backgroundColor"))
.binding(
new Color(0xF0100010, true),
() -> config.backgroundColor,
(value) -> config.backgroundColor = value
)
.controller(opt -> ColorControllerBuilder.create(opt).allowAlpha(true))
.build())
.build())
.group(OptionGroup.createBuilder()
.name(Text.translatable("itemGroup.foodAndDrink"))
Expand Down
Loading

0 comments on commit a5870bb

Please sign in to comment.