Skip to content

Commit

Permalink
HoldShiftTooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
ZiYueCommentary committed Sep 20, 2024
1 parent 013b46d commit 58109ad
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mtr.mod.block.IBlock;
import ziyue.tjmetro.mod.BlockList;
import ziyue.tjmetro.mod.TianjinMetro;
import ziyue.tjmetro.mod.data.IGuiExtension;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -90,6 +91,10 @@ public void onBreak2(World world, BlockPos pos, BlockState state, PlayerEntity p
super.onBreak2(world, pos, state, player);
}

@Override
public void addTooltips(ItemStack stack, @Nullable BlockView world, List<MutableText> tooltip, TooltipContext options) {
IGuiExtension.addHoldShiftTooltip(tooltip, TextHelper.translatable("tooltip.tjmetro.metal_detection_door"));
}
@Override
public void addBlockProperties(List<HolderBase<?>> properties) {
properties.add(FACING);
Expand Down
103 changes: 99 additions & 4 deletions fabric/src/main/java/ziyue/tjmetro/mod/data/IGuiExtension.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package ziyue.tjmetro.mod.data;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.mtr.mapping.holder.MutableText;
import org.mtr.mapping.holder.Screen;
import org.mtr.mapping.holder.TextFormatting;
import org.mtr.mapping.mapper.GraphicsHolder;
import org.mtr.mapping.mapper.TextHelper;
import org.mtr.mod.config.Config;
import org.mtr.mod.data.IGui;
import org.mtr.mod.generated.lang.TranslationProvider;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -80,10 +85,13 @@ static ImmutablePair<String, String> splitTranslation(String text) {
return new ImmutablePair<>(text.substring(0, separatorIndex), text.substring(separatorIndex + 1));
}

static String insertTranslation(String keyCJK, String key, int expectedArguments, String... arguments) {
return insertTranslation(keyCJK, key, null, expectedArguments, arguments);
}

/**
* Formatting the string.
*
* @author ZiYueCommentary
* @see IGui#insertTranslation(TranslationProvider.TranslationHolder, TranslationProvider.TranslationHolder, String, int, String...)
* @since 1.0.0-beta-2
*/
static String insertTranslation(String keyCJK, String key, @Nullable String overrideFirst, int expectedArguments, String... arguments) {
if (arguments.length < expectedArguments) {
return "";
Expand Down Expand Up @@ -147,4 +155,91 @@ static String insertTranslation(String keyCJK, String key, @Nullable String over
return "";
}
}

/**
* @author ZiYueCommentary
* @see #insertTranslation(String, String, String, int, String...)
* @since 1.0.0-beta-2
*/
static String insertTranslation(String keyCJK, String key, int expectedArguments, String... arguments) {
return insertTranslation(keyCJK, key, null, expectedArguments, arguments);
}

/**
* Adding a hold-shift-tooltip to the tooltips.
*
* @param list hover Text List, just like pointer in C/C++
* @param component a component that waits for split
* @param regex the delimiting regular expression
* @param limit the result threshold, as described above
* @return hover text lists
* @author ZiYueCommentary
* @since 1.0.0-beta-2
*/
static List<MutableText> addHoldShiftTooltip(List<MutableText> list, MutableText component, boolean wordWarp, String regex, int limit) {
if (Screen.hasShiftDown()) {
// Adding a space in the end is the simplest way to fix the bug. Do not ask me why.
String[] texts = (component.getString() + " ").split(regex, limit);
if (wordWarp) {
for (String text : texts) {
int start = 0;
while (start < text.length()) {
int end = start;
int currentWidth = 0;
int lastSpace = -1;

while (end < text.length() && currentWidth + GraphicsHolder.getTextWidth(text.substring(start, end + 1)) <= 380) { // 380 is the maximum width
if (text.charAt(end) == ' ') {
lastSpace = end;
}
currentWidth += GraphicsHolder.getTextWidth(text.substring(end, end + 1));
end++;
}

if (lastSpace != -1 && lastSpace > start) {
list.add(TextHelper.literal(text.substring(start, lastSpace)));
start = lastSpace + 1;
} else {
list.add(TextHelper.literal(text.substring(start, end)));
start = end;
}
}
}
} else {
for (String text : texts) {
list.add(TextHelper.literal(text));
}
}
} else {
list.add(TextHelper.translatable("tooltip.tjmetro.shift").formatted(TextFormatting.YELLOW));
}
return list;
}

/**
* @author ZiYueCommentary
* @see #addHoldShiftTooltip(List, MutableText, boolean, String, int)
* @since 1.0.0-beta-2
*/
static List<MutableText> addHoldShiftTooltip(List<MutableText> list, MutableText component, boolean wordWarp, String regex) {
return addHoldShiftTooltip(list, component, wordWarp, regex, 0);
}

/**
* @author ZiYueCommentary
* @see #addHoldShiftTooltip(List, MutableText, boolean, String, int)
* @since 1.0.0-beta-2
*/
static List<MutableText> addHoldShiftTooltip(List<MutableText> list, MutableText component, boolean wordWarp) {
return addHoldShiftTooltip(list, component, wordWarp, "\n", 0);
}

/**
* @author ZiYueCommentary
* @see #addHoldShiftTooltip(List, MutableText, boolean, String, int)
* @since 1.0.0-beta-2
*/
static List<MutableText> addHoldShiftTooltip(List<MutableText> list, MutableText component) {
return addHoldShiftTooltip(list, component, true, "\n", 0);
}
}
2 changes: 1 addition & 1 deletion fabric/src/main/resources/assets/tjmetro/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"tooltip.tjmetro.custom_content": "Support custom display content",
"tooltip.tjmetro.custom_color": "Support custom display color",
"tooltip.tjmetro.station_name_sign": "A small station name sign, you can change display content with wrench.",
"tooltip.tjmetro.metal_detection_door": "A device for clearing items from players' inventory. Note this device will deleting item directly, instead of taking out item.",
"tooltip.tjmetro.metal_detection_door": "A device for clearing items from players' inventory. Note this device will delete item directly, instead of taking out items.",
"tooltip.tjmetro.use_tianjin_metro_font": "Render texts with mod font in some blocks.",
"tooltip.tjmetro.shift": "Hold SHIFT for more info",

Expand Down

0 comments on commit 58109ad

Please sign in to comment.