Skip to content

Commit

Permalink
Hoverable item tooltips in chat (Wynntils#335)
Browse files Browse the repository at this point in the history
* Add support for encoding/decoding items into text

* Add function to force identification analysis type

* Add chat item copy prompt and chat support

* Improved encoding efficiency

* Use placeholder strings in chat input box for clarity

* Support for multiple items in one message

* Copyright header & formatting
  • Loading branch information
P0keDev authored Jul 18, 2021
1 parent 20eec70 commit 6220d60
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/wynntils/core/framework/enums/Powder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public char getSymbol() {
return symbol;
}

public String getColor() {
return color;
}

public String getColoredSymbol() {
return color + symbol;
}

public String getLetterRepresentation() {
return this.name().substring(0, 1).toLowerCase();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.wynntils.modules.questbook.instances.DiscoveryInfo;
import com.wynntils.modules.questbook.managers.QuestManager;
import com.wynntils.modules.utilities.configs.TranslationConfig;
import com.wynntils.modules.utilities.managers.ChatItemManager;
import com.wynntils.webapi.services.TranslationManager;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.init.Items;
Expand Down Expand Up @@ -269,7 +270,6 @@ public static Pair<ITextComponent, Pair<Supplier<Boolean>, Function<ITextCompone

// clickable coordinates
if (ChatConfig.INSTANCE.clickableCoordinates && coordinateReg.matcher(McIf.getUnformattedText(in)).find()) {

ITextComponent temp = new TextComponentString("");
for (ITextComponent texts : in) {
Matcher m = coordinateReg.matcher(texts.getUnformattedComponentText());
Expand Down Expand Up @@ -316,6 +316,45 @@ public static Pair<ITextComponent, Pair<Supplier<Boolean>, Function<ITextCompone
in = temp;
}

// chat item tooltips
if (ChatItemManager.ENCODED_PATTERN.matcher(McIf.getUnformattedText(in)).find()) {
ITextComponent temp = new TextComponentString("");
for (ITextComponent comp : in) {
Matcher m = ChatItemManager.ENCODED_PATTERN.matcher(comp.getUnformattedComponentText());
if (!m.find()) {
ITextComponent newComponent = new TextComponentString(comp.getUnformattedComponentText());
newComponent.setStyle(comp.getStyle().createShallowCopy());
temp.appendSibling(newComponent);
continue;
}

do {
String text = McIf.getUnformattedText(comp);
Style style = comp.getStyle();

ITextComponent item = ChatItemManager.decodeItem(m.group());
if (item == null) { // couldn't decode, skip
comp = new TextComponentString(comp.getUnformattedComponentText());
comp.setStyle(style.createShallowCopy());
continue;
}

ITextComponent preText = new TextComponentString(text.substring(0, m.start()));
preText.setStyle(style.createShallowCopy());
temp.appendSibling(preText);

temp.appendSibling(item);

comp = new TextComponentString(text.substring(m.end()));
comp.setStyle(style.createShallowCopy());

m = ChatItemManager.ENCODED_PATTERN.matcher(comp.getUnformattedText()); // recreate matcher for new substring
} while (m.find()); // search for multiple items in the same message
temp.appendSibling(comp); // leftover text after item(s)
}
in = temp;
}

return new Pair<>(in, null);
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/wynntils/modules/chat/overlays/gui/ChatGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.wynntils.modules.chat.language.WynncraftLanguage;
import com.wynntils.modules.chat.managers.TabManager;
import com.wynntils.modules.chat.overlays.ChatOverlay;
import com.wynntils.modules.utilities.managers.ChatItemManager;

import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.gui.GuiButton;
Expand All @@ -29,6 +31,8 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;

public class ChatGUI extends GuiChat {

Expand All @@ -42,6 +46,8 @@ public class ChatGUI extends GuiChat {
private ChatButton addTab = null;
private Map<WynncraftLanguage, ChatButton> languageButtons = new HashMap<>();

private Map<String, String> chatItems = new HashMap<>();

public ChatGUI() {

}
Expand Down Expand Up @@ -101,7 +107,27 @@ protected void keyTyped(char typedChar, int keyCode) throws IOException {
typedChar = output.b;
}

if (!chatItems.isEmpty() && (keyCode == Keyboard.KEY_RETURN || keyCode == Keyboard.KEY_NUMPADENTER)) { // message is being sent
for (Entry<String, String> item : chatItems.entrySet()) { // replace the placeholders with the actual encoded strings
this.inputField.setText(this.inputField.getText().replace("<" + item.getKey() + ">", item.getValue()));
}
chatItems.clear();
}

super.keyTyped(typedChar, keyCode);

// replace encoded strings with placeholders for less confusion
Matcher m = ChatItemManager.ENCODED_PATTERN.matcher(this.inputField.getText());
while (m.find()) {
String encodedItem = m.group();
String name = m.group("Name");
while (chatItems.containsKey(name)) { // avoid overwriting entries
name += "_";
}

this.inputField.setText(this.inputField.getText().replace(encodedItem, "<" + name + ">"));
chatItems.put(name, encodedItem);
}
}

@Override
Expand Down Expand Up @@ -156,6 +182,12 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) {
}
}

@Override
public void onGuiClosed() {
super.onGuiClosed();
chatItems.clear();
}

private String getDisplayName(ChatTab tab) {
if (tab.hasMentions()) {
return TextFormatting.RED + tab.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* * Copyright © Wynntils - 2018 - 2021.
*/

package com.wynntils.modules.questbook.managers;

import java.util.ArrayList;
Expand Down
Loading

0 comments on commit 6220d60

Please sign in to comment.