Skip to content

Commit 92bdcc0

Browse files
AdModal: Cleanup and improve tooltip handling
1 parent 485f776 commit 92bdcc0

File tree

1 file changed

+90
-22
lines changed

1 file changed

+90
-22
lines changed

src/main/java/gg/essential/ad/modal/AdModal.java

Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gg.essential.ad.modal;
22

33
import gg.essential.ad.Draw;
4+
import gg.essential.ad.EssentialAd;
45
import gg.essential.ad.EssentialUtil;
56
import gg.essential.ad.Resources;
67
import gg.essential.ad.data.AdData;
@@ -21,20 +22,25 @@
2122

2223
public class AdModal extends Modal {
2324

24-
private final Map<ModalData.Feature, ResourceLocation> iconTextures = new HashMap<>();
25-
2625
private static final ResourceLocation X_ICON = Resources.load("x.png");
2726
private static final ResourceLocation ESSENTIAL_LOGO = Resources.load("essential.png");
2827
private static final ResourceLocation REMOVE_INTEGRATION = Resources.load("removeintegration.png");
2928

29+
private final Map<FeatureEntry, ResourceLocation> iconTextures = new HashMap<>();
30+
3031
private final ModalData modalData;
31-
private final String tooltip;
32+
private final String parterModsTooltip;
33+
private final List<FeatureEntry> features = new ArrayList<>();
3234

3335
private int featuresOffset;
3436

3537
public AdModal(ModalData modalData, List<AdData.PartnerMod> partnerMods) {
3638
this.modalData = modalData;
37-
this.tooltip = getTooltip(partnerMods);
39+
this.parterModsTooltip = getTooltip(partnerMods);
40+
41+
for (ModalData.Feature feature : modalData.getFeatures()) {
42+
features.add(FeatureEntry.fromInfra(feature));
43+
}
3844
}
3945

4046
@Override
@@ -45,7 +51,7 @@ public void init() {
4551
height += Font.getMultilineStringHeight(modalData.getSubtitle("en_us"), 10);
4652
height += 7;
4753
featuresOffset = height;
48-
height += 13 * modalData.getFeatures().size();
54+
height += 13 * features.size();
4955
height += 6;
5056
int learnMoreOffset = height;
5157
height += 24;
@@ -95,37 +101,34 @@ public void draw(Draw draw) {
95101
// Features
96102
{
97103
int maxWidth = 0;
98-
for (ModalData.Feature feature : modalData.getFeatures()) {
99-
maxWidth = Math.max(Font.getStringWidth(feature.getText("en_us").replaceAll("__", "")), maxWidth);
104+
for (FeatureEntry feature : features) {
105+
maxWidth = Math.max(Font.getStringWidth(feature.getDisplayedText()), maxWidth);
100106
}
101107

102108
int featureX = centreX - (maxWidth + 14) / 2;
103109

104110
int featureY = startY + featuresOffset;
105111

106-
for (ModalData.Feature feature : modalData.getFeatures()) {
112+
for (FeatureEntry feature : features) {
107113
ResourceLocation location = iconTextures.computeIfAbsent(feature, AdModal::loadIconTexture);
108114
draw.texturedRect(location, featureX + 1, featureY + 1, 10, 10, 0, 0, 10, 10, 0xFF000000);
109115
draw.texturedRect(location, featureX, featureY, 10, 10, 0, 0, 10, 10, 0xFF0a82fd);
110116

111-
//fixme improve handling, caching?
112-
String text = feature.getText("en_us");
113-
String tooltip = feature.getTooltip("en_us");
117+
int textX = featureX + 14;
118+
int textY = featureY + 1;
119+
120+
String text = feature.text;
121+
FeatureTooltip tooltip = feature.tooltip;
114122
if (tooltip != null) {
115-
int tooltipStart = text.indexOf("__");
116-
if (tooltipStart == -1) return;
117-
int tooltipEnd = text.indexOf("__", tooltipStart + 2);
118-
if (tooltipEnd == -1) return;
123+
int tooltipStart = tooltip.tooltipStart;
124+
int tooltipEnd = tooltip.tooltipEnd;
119125

120126
String beforeTooltip = text.substring(0, tooltipStart);
121127
int beforeTooltipWidth = Font.getStringWidth(beforeTooltip);
122128
String withTooltip = text.substring(tooltipStart + 2, tooltipEnd);
123129
int withTooltipWidth = Font.getStringWidth(withTooltip);
124130
String afterTooltip = text.substring(tooltipEnd + 2);
125131

126-
int textX = featureX + 14;
127-
int textY = featureY + 1;
128-
129132
if (!beforeTooltip.isEmpty()) {
130133
draw.string(beforeTooltip, textX, textY, 0xFFe5e5e5, 0xFF000000);
131134
textX += beforeTooltipWidth;
@@ -137,7 +140,13 @@ public void draw(Draw draw) {
137140

138141
if (draw.hovered(textX, textY, withTooltipWidth, 10)) {
139142
int finalX = textX;
140-
Draw.deferred(d -> Tooltip.drawTooltip(d, this.tooltip, Tooltip.Position.BELOW, finalX, textY, withTooltipWidth, 10));
143+
String tooltipText;
144+
if (tooltip.text.equals("MOD_PARTNERS")) {
145+
tooltipText = this.parterModsTooltip;
146+
} else {
147+
tooltipText = tooltip.text;
148+
}
149+
Draw.deferred(d -> Tooltip.drawTooltip(d, tooltipText, Tooltip.Position.BELOW, finalX, textY, withTooltipWidth, 10));
141150
}
142151

143152
textX += withTooltipWidth;
@@ -146,7 +155,7 @@ public void draw(Draw draw) {
146155
draw.string(afterTooltip, textX, textY, 0xFFe5e5e5, 0xFF000000);
147156
}
148157
} else {
149-
draw.string(feature.getText("en_us"), featureX + 14, featureY + 1, 0xFFe5e5e5, 0xFF000000);
158+
draw.string(text, textX, textY, 0xFFe5e5e5, 0xFF000000);
150159
}
151160

152161
featureY += 13;
@@ -209,8 +218,67 @@ public void close() {
209218
super.close();
210219
}
211220

212-
private static ResourceLocation loadIconTexture(ModalData.Feature feature) {
213-
byte[] bytes = Base64.getDecoder().decode(feature.getIcon());
221+
private static ResourceLocation loadIconTexture(FeatureEntry feature) {
222+
byte[] bytes = Base64.getDecoder().decode(feature.icon);
214223
return Resources.load(new ByteArrayInputStream(bytes));
215224
}
225+
226+
private static class FeatureEntry {
227+
private final String text;
228+
private final String icon;
229+
private final FeatureTooltip tooltip;
230+
231+
public FeatureEntry(String text, String icon, FeatureTooltip tooltip) {
232+
this.text = text;
233+
this.icon = icon;
234+
this.tooltip = tooltip;
235+
}
236+
237+
public String getDisplayedText() {
238+
if (tooltip != null) {
239+
return text.substring(0, tooltip.tooltipStart)
240+
+ text.substring(tooltip.tooltipStart + 2, tooltip.tooltipEnd)
241+
+ text.substring(tooltip.tooltipEnd + 2);
242+
} else {
243+
return text;
244+
}
245+
}
246+
247+
public static FeatureEntry fromInfra(ModalData.Feature feature) {
248+
String text = feature.getText("en_us");
249+
String tooltipText = feature.getTooltip("en_us");
250+
251+
FeatureTooltip tooltip = null;
252+
if (tooltipText != null) {
253+
tooltip = FeatureTooltip.parse(text, tooltipText);
254+
}
255+
return new FeatureEntry(text, feature.getIcon(), tooltip);
256+
}
257+
}
258+
259+
private static class FeatureTooltip {
260+
private final String text;
261+
private final int tooltipStart;
262+
private final int tooltipEnd;
263+
264+
public FeatureTooltip(String text, int tooltipStart, int tooltipEnd) {
265+
this.text = text;
266+
this.tooltipStart = tooltipStart;
267+
this.tooltipEnd = tooltipEnd;
268+
}
269+
270+
public static FeatureTooltip parse(String text, String tooltipText) {
271+
int tooltipStart = text.indexOf("__");
272+
if (tooltipStart == -1) {
273+
EssentialAd.LOGGER.warn("Invalid tooltip for string: {}", text);
274+
return null;
275+
}
276+
int tooltipEnd = text.indexOf("__", tooltipStart + 2);
277+
if (tooltipEnd == -1) {
278+
EssentialAd.LOGGER.warn("Invalid tooltip for string: {}", text);
279+
return null;
280+
}
281+
return new FeatureTooltip(tooltipText, tooltipStart, tooltipEnd);
282+
}
283+
}
216284
}

0 commit comments

Comments
 (0)