Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/cleanroommc/modularui/ModularUIConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public class ModularUIConfig {
@Config.RequiresMcRestart
@Config.Comment("Enables a test overlay shown on title screen and watermark shown on every GuiContainer.")
public static boolean enableTestOverlays = false;

@Config.Comment("If true, vanilla tooltip will be replaced with MUI's RichTooltip")
public static boolean replaceVanillaTooltips = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cleanroommc.modularui.core.mixin;

import com.cleanroommc.modularui.ModularUIConfig;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.screen.viewport.GuiContext;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.client.config.GuiUtils;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(value = GuiUtils.class, remap = false)
public class GuiUtilsMixin {

@Inject(method = "drawHoveringText(Lnet/minecraft/item/ItemStack;Ljava/util/List;IIIIILnet/minecraft/client/gui/FontRenderer;)V",
at = @At("HEAD"), cancellable = true)
private static void postRichTooltipEvent(ItemStack stack, List<String> textLines, int x, int y, int w, int h, int maxTextWidth, FontRenderer font, CallbackInfo ci) {
if (ModularUIConfig.replaceVanillaTooltips && !textLines.isEmpty()) {
RichTooltip tooltip = new RichTooltip();
tooltip.parent(area -> RichTooltip.findIngredientArea(area, x, y));
// Other positions don't really work due to the lack of GuiContext in non-modular uis
tooltip.add(textLines.get(0)).newLine();
if (!stack.isEmpty()) {
tooltip.spaceLine();
}
for (int i = 1, n = textLines.size(); i < n; i++) {
tooltip.add(textLines.get(i)).newLine();
}

tooltip.draw(GuiContext.getDefault(), stack);
// Canceling vanilla tooltip rendering
ci.cancel();
}
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/cleanroommc/modularui/drawable/GuiDraw.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cleanroommc.modularui.drawable;

import com.cleanroommc.modularui.drawable.text.TextRenderer;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.screen.RichTooltipEvent;
import com.cleanroommc.modularui.utils.Color;

import net.minecraft.client.Minecraft;
Expand All @@ -19,6 +21,7 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;

import java.util.List;
Expand Down Expand Up @@ -592,12 +595,17 @@ public static void drawText(String text, float x, float y, float scale, int colo
GlStateManager.enableBlend();
}

public static void drawTooltipBackground(ItemStack stack, List<String> lines, int x, int y, int textWidth, int height) {
public static void drawTooltipBackground(ItemStack stack, List<String> lines, int x, int y, int textWidth, int height, @Nullable RichTooltip tooltip) {
// TODO theme color
int backgroundColor = 0xF0100010;
int borderColorStart = 0x505000FF;
int borderColorEnd = (borderColorStart & 0xFEFEFE) >> 1 | borderColorStart & 0xFF000000;
RenderTooltipEvent.Color colorEvent = new RenderTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd);
RenderTooltipEvent.Color colorEvent;
if (tooltip != null) {
colorEvent = new RichTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd, tooltip);
} else {
colorEvent = new RenderTooltipEvent.Color(stack, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd);
}
MinecraftForge.EVENT_BUS.post(colorEvent);
backgroundColor = colorEvent.getBackground();
borderColorStart = colorEvent.getBorderStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Area getRenderedArea() {

@Override
public @NotNull RichTooltip tooltip() {
if (this.tooltip == null) this.tooltip = new RichTooltip(area -> area.set(getRenderedArea()));
if (this.tooltip == null) this.tooltip = new RichTooltip().parent(area -> area.set(getRenderedArea()));
return tooltip;
}

Expand Down
54 changes: 27 additions & 27 deletions src/main/java/com/cleanroommc/modularui/drawable/text/RichText.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;

import com.cleanroommc.modularui.utils.TooltipLines;

import net.minecraft.client.gui.FontRenderer;

import java.util.ArrayList;
Expand All @@ -15,6 +17,7 @@ public class RichText implements IDrawable, IRichTextBuilder<RichText> {
private static final TextRenderer renderer = new TextRenderer();

private final List<Object> elements = new ArrayList<>();
private TooltipLines stringList;
private Alignment alignment = Alignment.CenterLeft;
private float scale = 1f;
private Integer color = null;
Expand All @@ -26,35 +29,17 @@ public boolean isEmpty() {
return this.elements.isEmpty();
}

public List<String> getStringRepresentation() {
List<String> list = new ArrayList<>();
StringBuilder builder = new StringBuilder();
for (Object o : this.elements) {
if (o == IKey.LINE_FEED) {
list.add(builder.toString());
builder.delete(0, builder.length());
continue;
}
String s = null;
if (o instanceof IKey key) {
s = key.get();
} else if (o instanceof String s1) {
s = s1;
} else if (o instanceof TextIcon ti) {
s = ti.getText();
}
if (s != null) {
for (String part : s.split("\n")) {
builder.append(part);
list.add(builder.toString());
builder.delete(0, builder.length());
}
}
public List<String> getAsStrings() {
if (this.stringList == null) {
this.stringList = new TooltipLines(this.elements);
}
if (!list.isEmpty() && list.get(list.size() - 1).isEmpty()) {
list.remove(list.size() - 1);
return this.stringList;
}

private void clearStrings() {
if (this.stringList != null) {
this.stringList.clearCache();
}
return list;
}

public int getMinWidth() {
Expand Down Expand Up @@ -95,6 +80,7 @@ public IRichTextBuilder<?> getRichText() {

public RichText add(String s) {
this.elements.add(s);
clearStrings();
return this;
}

Expand All @@ -103,18 +89,21 @@ public RichText add(IDrawable drawable) {
Object o = drawable;
if (!(o instanceof IKey) && !(o instanceof IIcon)) o = drawable.asIcon();
this.elements.add(o);
clearStrings();
return this;
}

@Override
public RichText addLine(ITextLine line) {
this.elements.add(line);
clearStrings();
return this;
}

@Override
public RichText clearText() {
this.elements.clear();
clearStrings();
return this;
}

Expand Down Expand Up @@ -154,6 +143,7 @@ public RichText insertTitleMargin(int margin) {
} else {
objects.add(i + 1, Spacer.of(margin));
}
clearStrings();
return this;
}
}
Expand Down Expand Up @@ -201,4 +191,14 @@ public Object getHoveringElement(FontRenderer fr, int x, int y) {
}
return null;
}

public RichText copy() {
RichText copy = new RichText();
copy.elements.addAll(this.elements);
copy.alignment = this.alignment;
copy.scale = this.scale;
copy.color = this.color;
copy.shadow = this.shadow;
return copy;
}
}
Loading