Skip to content

Commit 70c4940

Browse files
committed
Improve the ScreenRectangle we use for tooltip rendering of slot based lists
1 parent ea18b72 commit 70c4940

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

src/main/java/mekanism/client/gui/element/GuiDropdown.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import mekanism.common.registries.MekanismSounds;
1212
import net.minecraft.client.gui.GuiGraphics;
1313
import net.minecraft.client.gui.components.Tooltip;
14+
import net.minecraft.client.gui.navigation.ScreenRectangle;
1415
import net.minecraft.network.chat.Component;
1516
import net.minecraft.resources.ResourceLocation;
1617
import org.jetbrains.annotations.NotNull;
18+
import org.jetbrains.annotations.Nullable;
1719

1820
public class GuiDropdown<TYPE extends Enum<TYPE> & IDropdownEnum<TYPE>> extends GuiTexturedElement {
1921

@@ -22,6 +24,9 @@ public class GuiDropdown<TYPE extends Enum<TYPE> & IDropdownEnum<TYPE>> extends
2224
private final Supplier<TYPE> curType;
2325
private final TYPE[] options;
2426

27+
@Nullable
28+
private ScreenRectangle cachedTooltipRect;
29+
2530
private boolean isOpen;
2631

2732
public GuiDropdown(IGuiWrapper gui, int x, int y, int width, Class<TYPE> enumClass, Supplier<TYPE> curType, Consumer<TYPE> handler) {
@@ -95,6 +100,12 @@ public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mou
95100
pose.popPose();
96101
}
97102

103+
@NotNull
104+
@Override
105+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
106+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
107+
}
108+
98109
@Override
99110
public void updateTooltip(int mouseX, int mouseY) {
100111
int index = getHoveredIndex(mouseX, mouseY);
@@ -103,9 +114,11 @@ public void updateTooltip(int mouseX, int mouseY) {
103114
Component tooltip = t.getTooltip();
104115
return tooltip == null ? null : Tooltip.create(tooltip);
105116
});
117+
cachedTooltipRect = new ScreenRectangle(getX() + 1, getY() + 12 + index * 10, width - 2, 10);
106118
setTooltip(text);
107119
} else {
108120
clearTooltip();
121+
cachedTooltipRect = null;
109122
}
110123
}
111124

src/main/java/mekanism/client/gui/element/GuiElement.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public ScreenRectangle getRectangle() {
167167
return new ScreenRectangle(getGuiLeft() + getButtonX(), getGuiTop() + getButtonY(), getButtonWidth(), getButtonHeight());
168168
}
169169

170+
@NotNull
171+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
172+
return getRectangle();
173+
}
174+
170175
@NotNull
171176
@Override
172177
public List<GuiElement> children() {
@@ -296,7 +301,7 @@ public void renderToolTip(@NotNull GuiGraphics guiGraphics, int mouseX, int mous
296301
Tooltip tooltip = getTooltip();
297302
if (tooltip != null) {
298303
//Note: We only call this method if we are hovering the proper spot
299-
tooltip.refreshTooltipForNextRenderPass(true, isFocused(), getRectangle());
304+
tooltip.refreshTooltipForNextRenderPass(true, isFocused(), getTooltipRectangle(mouseX, mouseY));
300305
}
301306
//We do this before child renders so that if one has a tooltip then they can override the target tooltip
302307
for (GuiElement child : children) {

src/main/java/mekanism/client/gui/element/custom/GuiSupportedUpgrades.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import mekanism.common.util.UpgradeUtils;
1616
import net.minecraft.client.gui.GuiGraphics;
1717
import net.minecraft.client.gui.components.Tooltip;
18+
import net.minecraft.client.gui.navigation.ScreenRectangle;
1819
import net.minecraft.client.renderer.RenderType;
1920
import net.minecraft.network.chat.Component;
2021
import org.jetbrains.annotations.NotNull;
@@ -40,6 +41,8 @@ public static int calculateNeededRows() {
4041
private List<Component> lastInfo = Collections.emptyList();
4142
@Nullable
4243
private Tooltip lastTooltip;
44+
@Nullable
45+
private ScreenRectangle cachedTooltipRect;
4346

4447
public GuiSupportedUpgrades(IGuiWrapper gui, int x, int y, Set<Upgrade> supportedUpgrades) {
4548
super(gui, x, y, 125, ELEMENT_SIZE * calculateNeededRows() + 2);
@@ -71,6 +74,12 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
7174
drawTextScaledBound(guiGraphics, MekanismLang.UPGRADES_SUPPORTED.translate(), relativeX + 2, relativeY + 3, titleTextColor(), 54);
7275
}
7376

77+
@NotNull
78+
@Override
79+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
80+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
81+
}
82+
7483
@Override
7584
public void updateTooltip(int mouseX, int mouseY) {
7685
for (int i = 0; i < EnumUtils.UPGRADES.length; i++) {
@@ -88,13 +97,16 @@ public void updateTooltip(int mouseX, int mouseY) {
8897
if (!info.equals(lastInfo)) {
8998
lastInfo = info;
9099
lastTooltip = MultiLineTooltip.createMulti(info);
100+
//Note: We only have to update the tooltip rect if the tooltip changed as we know none of the elements share the same tooltips
101+
cachedTooltipRect = new ScreenRectangle(getX() + 1 + pos.x, getY() + 1 + pos.y, ELEMENT_SIZE, ELEMENT_SIZE);
91102
}
92103
setTooltip(lastTooltip);
93104
//We can break once we managed to find a tooltip to render
94105
return;
95106
}
96107
}
97108
lastInfo = Collections.emptyList();
109+
cachedTooltipRect = null;
98110
setTooltip(lastTooltip = null);
99111
}
100112

src/main/java/mekanism/client/gui/element/graph/GuiGraph.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import mekanism.common.util.MekanismUtils.ResourceType;
1414
import net.minecraft.client.gui.GuiGraphics;
1515
import net.minecraft.client.gui.components.Tooltip;
16+
import net.minecraft.client.gui.navigation.ScreenRectangle;
1617
import net.minecraft.network.chat.Component;
1718
import net.minecraft.resources.ResourceLocation;
1819
import org.jetbrains.annotations.NotNull;
@@ -30,6 +31,8 @@ public abstract class GuiGraph<COLLECTION extends Collection<?>, HANDLER extends
3031
private Component lastInfo = null;
3132
@Nullable
3233
private Tooltip lastTooltip;
34+
@Nullable
35+
private ScreenRectangle cachedTooltipRect;
3336

3437
protected boolean fixedScale = false;
3538

@@ -79,6 +82,12 @@ public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mou
7982

8083
protected abstract Component getDataDisplay(int hoverIndex);
8184

85+
@NotNull
86+
@Override
87+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
88+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
89+
}
90+
8291
@Override
8392
public void updateTooltip(int mouseX, int mouseY) {
8493
int hoverIndex = mouseX - getX();
@@ -88,9 +97,11 @@ public void updateTooltip(int mouseX, int mouseY) {
8897
lastInfo = info;
8998
lastTooltip = Tooltip.create(info);
9099
}
100+
cachedTooltipRect = new ScreenRectangle(getX(), getGuiTop() + getButtonY(), 1, getButtonHeight());
91101
} else {
92102
lastInfo = null;
93103
lastTooltip = null;
104+
cachedTooltipRect = null;
94105
}
95106
setTooltip(lastTooltip);
96107
}

src/main/java/mekanism/client/gui/element/scroll/GuiModuleScrollList.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import mekanism.common.util.MekanismUtils.ResourceType;
2222
import net.minecraft.client.gui.GuiGraphics;
2323
import net.minecraft.client.gui.components.Tooltip;
24+
import net.minecraft.client.gui.navigation.ScreenRectangle;
2425
import net.minecraft.network.chat.Component;
2526
import net.minecraft.resources.ResourceLocation;
2627
import net.minecraft.world.item.ItemStack;
28+
import org.jetbrains.annotations.NotNull;
2729
import org.jetbrains.annotations.Nullable;
2830

2931
public class GuiModuleScrollList extends GuiScrollList {
@@ -45,6 +47,8 @@ public class GuiModuleScrollList extends GuiScrollList {
4547
private Component lastInfo = null;
4648
@Nullable
4749
private Tooltip lastTooltip;
50+
@Nullable
51+
private ScreenRectangle cachedTooltipRect;
4852

4953
public GuiModuleScrollList(IGuiWrapper gui, int x, int y, int width, int height, Supplier<ItemStack> itemSupplier, Consumer<Module<?>> callback) {
5054
super(gui, x, y, width, height, TEXTURE_HEIGHT / 3, GuiElementHolder.HOLDER, GuiElementHolder.HOLDER_SIZE);
@@ -137,6 +141,12 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
137141
});
138142
}
139143

144+
@NotNull
145+
@Override
146+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
147+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
148+
}
149+
140150
@Override
141151
public void updateTooltip(int mouseX, int mouseY) {
142152
if (currentContainer != null && mouseX >= getX() + 1 && mouseX < getX() + barXShift - 1) {
@@ -154,11 +164,13 @@ public void updateTooltip(int mouseX, int mouseY) {
154164
lastInfo = info;
155165
lastTooltip = Tooltip.create(info);
156166
}
167+
cachedTooltipRect = new ScreenRectangle(getX() + 1, getY() + 1 + multipliedElement, barXShift - 2, elementHeight);
157168
setTooltip(lastTooltip);
158169
return;
159170
}
160171
}
161172
}
173+
cachedTooltipRect = null;
162174
lastInfo = null;
163175
setTooltip(lastTooltip = null);
164176
}

src/main/java/mekanism/client/gui/element/scroll/GuiRobitSkinSelectScroll.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import mekanism.common.util.MekanismUtils;
2626
import net.minecraft.client.gui.GuiGraphics;
2727
import net.minecraft.client.gui.components.Tooltip;
28+
import net.minecraft.client.gui.navigation.ScreenRectangle;
2829
import net.minecraft.client.renderer.LightTexture;
2930
import net.minecraft.client.renderer.MultiBufferSource;
3031
import net.minecraft.client.renderer.RenderType;
@@ -55,6 +56,8 @@ public class GuiRobitSkinSelectScroll extends GuiElement {
5556
private ResourceKey<RobitSkin> lastSkin;
5657
@Nullable
5758
private Tooltip lastTooltip;
59+
@Nullable
60+
private ScreenRectangle cachedTooltipRect;
5861

5962
public GuiRobitSkinSelectScroll(IGuiWrapper gui, int x, int y, EntityRobit robit, Supplier<List<ResourceKey<RobitSkin>>> unlockedSkins) {
6063
super(gui, x, y, INNER_DIMENSIONS + 12, INNER_DIMENSIONS);
@@ -136,9 +139,15 @@ public void tick() {
136139
ticks++;
137140
}
138141

142+
@NotNull
143+
@Override
144+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
145+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
146+
}
147+
139148
@Override
140149
public void updateTooltip(int mouseX, int mouseY) {
141-
ResourceKey<RobitSkin> skin = getSkin(mouseX, mouseY);
150+
ResourceKey<RobitSkin> skin = getSkin(mouseX, mouseY, true);
142151
if (skin == null) {
143152
lastTooltip = null;
144153
} else if (lastSkin != skin) {
@@ -156,23 +165,29 @@ public boolean mouseScrolled(double mouseX, double mouseY, double xDelta, double
156165
@Override
157166
public void onClick(double mouseX, double mouseY, int button) {
158167
super.onClick(mouseX, mouseY, button);
159-
ResourceKey<RobitSkin> skin = getSkin(mouseX, mouseY);
168+
ResourceKey<RobitSkin> skin = getSkin(mouseX, mouseY, false);
160169
if (skin != null) {
161170
selectedSkin = skin;
162171
}
163172
}
164173

165-
private ResourceKey<RobitSkin> getSkin(double mouseX, double mouseY) {
174+
private ResourceKey<RobitSkin> getSkin(double mouseX, double mouseY, boolean updateTooltipRect) {
166175
List<ResourceKey<RobitSkin>> skins = getUnlockedSkins();
167176
if (skins != null) {
168177
int slotX = (int) ((mouseX - getX()) / SLOT_DIMENSIONS), slotY = (int) ((mouseY - getY()) / SLOT_DIMENSIONS);
169178
if (slotX >= 0 && slotY >= 0 && slotX < SLOT_COUNT && slotY < SLOT_COUNT) {
170179
int slot = (slotY + scrollBar.getCurrentSelection()) * SLOT_COUNT + slotX;
171180
if (slot < skins.size()) {
181+
if (updateTooltipRect) {
182+
cachedTooltipRect = new ScreenRectangle(getX() + slotX * SLOT_DIMENSIONS, getY() + slotY * SLOT_DIMENSIONS, SLOT_DIMENSIONS, SLOT_DIMENSIONS);
183+
}
172184
return skins.get(slot);
173185
}
174186
}
175187
}
188+
if (updateTooltipRect) {
189+
cachedTooltipRect = null;
190+
}
176191
return null;
177192
}
178193

src/main/java/mekanism/client/gui/element/scroll/GuiUpgradeScrollList.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import mekanism.common.util.UpgradeUtils;
1717
import net.minecraft.client.gui.GuiGraphics;
1818
import net.minecraft.client.gui.components.Tooltip;
19+
import net.minecraft.client.gui.navigation.ScreenRectangle;
1920
import net.minecraft.resources.ResourceLocation;
21+
import org.jetbrains.annotations.NotNull;
2022
import org.jetbrains.annotations.Nullable;
2123

2224
public class GuiUpgradeScrollList extends GuiScrollList {
@@ -30,6 +32,8 @@ public class GuiUpgradeScrollList extends GuiScrollList {
3032
private final Runnable onSelectionChange;
3133
@Nullable
3234
private Upgrade selectedType;
35+
@Nullable
36+
private ScreenRectangle cachedTooltipRect;
3337

3438
public GuiUpgradeScrollList(IGuiWrapper gui, int x, int y, int width, int height, TileComponentUpgrade component, Runnable onSelectionChange) {
3539
super(gui, x, y, width, height, TEXTURE_HEIGHT / 3, GuiElementHolder.HOLDER, GuiElementHolder.HOLDER_SIZE);
@@ -83,6 +87,12 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
8387
titleTextColor(), 44));
8488
}
8589

90+
@NotNull
91+
@Override
92+
protected ScreenRectangle getTooltipRectangle(int mouseX, int mouseY) {
93+
return cachedTooltipRect == null ? super.getTooltipRectangle(mouseX, mouseY) : cachedTooltipRect;
94+
}
95+
8696
@Override
8797
public void updateTooltip(int mouseX, int mouseY) {
8898
if (mouseX >= getX() + 1 && mouseX < getX() + barXShift - 1) {
@@ -96,11 +106,13 @@ public void updateTooltip(int mouseX, int mouseY) {
96106
Upgrade upgrade = upgrades[index];
97107
int multipliedElement = elementHeight * i;
98108
if (mouseY >= getY() + 1 + multipliedElement && mouseY < getY() + 1 + multipliedElement + elementHeight) {
109+
cachedTooltipRect = new ScreenRectangle(getX() + 1, getY() + 1 + multipliedElement, barXShift - 2, elementHeight);
99110
setTooltip(tooltips.computeIfAbsent(upgrade, u -> Tooltip.create(u.getDescription())));
100111
return;
101112
}
102113
}
103114
}
115+
cachedTooltipRect = null;
104116
clearTooltip();
105117
}
106118

0 commit comments

Comments
 (0)