Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct use of tooltip events #20

Merged
merged 2 commits into from
Aug 1, 2023
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
4 changes: 2 additions & 2 deletions src/main/java/com/cleanroommc/modularui/drawable/GuiDraw.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,12 @@ public static void drawText(String text, float x, float y, float scale, int colo
GlStateManager.enableBlend();
}

public static void drawTooltipBackground(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) {
// TODO theme color
int backgroundColor = 0xF0100010;
int borderColorStart = 0x505000FF;
int borderColorEnd = (borderColorStart & 0xFEFEFE) >> 1 | borderColorStart & 0xFF000000;
RenderTooltipEvent.Color colorEvent = new RenderTooltipEvent.Color(ItemStack.EMPTY, lines, x, y, TextRenderer.getFontRenderer(), backgroundColor, borderColorStart, borderColorEnd);
RenderTooltipEvent.Color 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
18 changes: 12 additions & 6 deletions src/main/java/com/cleanroommc/modularui/screen/Tooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Tooltip {

Expand Down Expand Up @@ -64,6 +64,10 @@ public void buildTooltip() {
}

public void draw(GuiContext context) {
draw(context, ItemStack.EMPTY);
}

public void draw(GuiContext context, @Nullable ItemStack stack) {
if (updateTooltipEveryTick) {
markDirty();
}
Expand All @@ -72,12 +76,14 @@ public void draw(GuiContext context) {
if (maxWidth <= 0) {
maxWidth = Integer.MAX_VALUE;
}
if (stack == null) stack = ItemStack.EMPTY;
Area screen = context.screen.getScreenArea();
int mouseX = context.getAbsMouseX(), mouseY = context.getAbsMouseY();
IconRenderer renderer = IconRenderer.SHARED;
//List<IIcon> icons = renderer.measureLines(this.lines);
List<String> textLines = Collections.emptyList();//icons.stream().filter(iIcon -> iIcon instanceof TextIcon).map(icon -> ((TextIcon) icon).getText()).collect(Collectors.toList());
RenderTooltipEvent.Pre event = new RenderTooltipEvent.Pre(ItemStack.EMPTY, Collections.emptyList(), mouseX, mouseY, screen.width, screen.height, maxWidth, TextRenderer.getFontRenderer());
List<String> textLines = lines.stream().filter(drawable -> drawable instanceof IKey).map(key -> ((IKey) key).get()).collect(Collectors.toList());
//icons.stream().filter(iIcon -> iIcon instanceof TextIcon).map(icon -> ((TextIcon) icon).getText()).collect(Collectors.toList());
RenderTooltipEvent.Pre event = new RenderTooltipEvent.Pre(stack, textLines, mouseX, mouseY, screen.width, screen.height, maxWidth, TextRenderer.getFontRenderer());
if (MinecraftForge.EVENT_BUS.post(event)) {
return;
}
Expand Down Expand Up @@ -106,9 +112,9 @@ public void draw(GuiContext context) {
GlStateManager.disableDepth();
GlStateManager.disableBlend();

GuiDraw.drawTooltipBackground(textLines, area.x, area.y, area.width, area.height);
GuiDraw.drawTooltipBackground(stack, textLines, area.x, area.y, area.width, area.height);

MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostBackground(ItemStack.EMPTY, textLines, area.x, area.y, TextRenderer.getFontRenderer(), area.width, area.height));
MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostBackground(stack, textLines, area.x, area.y, TextRenderer.getFontRenderer(), area.width, area.height));

GlStateManager.color(1f, 1f, 1f, 1f);

Expand All @@ -117,7 +123,7 @@ public void draw(GuiContext context) {
renderer.setPos(area.x, area.y);
renderer.draw(context, this.lines);

MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostText(ItemStack.EMPTY, textLines, area.x, area.y, TextRenderer.getFontRenderer(), area.width, area.height));
MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostText(stack, textLines, area.x, area.y, TextRenderer.getFontRenderer(), area.width, area.height));
}

public Rectangle determineTooltipArea(GuiContext context, List<IDrawable> lines, IconRenderer renderer, int screenWidth, int screenHeight, int mouseX, int mouseY) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/cleanroommc/modularui/widgets/ItemSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.cleanroommc.modularui.drawable.TextRenderer;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
import com.cleanroommc.modularui.screen.Tooltip;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.screen.GuiScreenWrapper;
import com.cleanroommc.modularui.screen.ModularScreen;
Expand Down Expand Up @@ -146,6 +147,14 @@ protected List<String> getItemTooltip(ItemStack stack) {
return getScreen().getScreenWrapper().getItemToolTip(stack);
}

@Override
public void drawForeground(GuiContext context) {
Tooltip tooltip = getTooltip();
if (tooltip != null && isHoveringFor(tooltip.getShowUpTimer())) {
tooltip.draw(getContext(), getSlot().getStack());
}
}

@SideOnly(Side.CLIENT)
private void drawSlot(Slot slotIn) {
GuiScreenWrapper guiScreen = getScreen().getScreenWrapper();
Expand Down