Better text drawing in radial menu using tooltips.

Hold shift for description.
Fixed keybind text.
This commit is contained in:
Christian Knaapen
2023-02-03 20:00:08 +01:00
parent 07667efe98
commit 589059fd44
4 changed files with 94 additions and 132 deletions

View File

@@ -15,6 +15,7 @@ import net.minecraft.core.Direction;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText; import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource; import net.minecraft.util.RandomSource;
@@ -22,6 +23,10 @@ import nl.requios.effortlessbuilding.ClientEvents;
import nl.requios.effortlessbuilding.EffortlessBuilding; import nl.requios.effortlessbuilding.EffortlessBuilding;
import nl.requios.effortlessbuilding.EffortlessBuildingClient; import nl.requios.effortlessbuilding.EffortlessBuildingClient;
import nl.requios.effortlessbuilding.buildmode.ModeOptions; import nl.requios.effortlessbuilding.buildmode.ModeOptions;
import nl.requios.effortlessbuilding.create.foundation.item.ItemDescription;
import nl.requios.effortlessbuilding.create.foundation.item.TooltipHelper;
import nl.requios.effortlessbuilding.create.foundation.utility.Components;
import nl.requios.effortlessbuilding.create.foundation.utility.Lang;
import org.apache.commons.lang3.text.WordUtils; import org.apache.commons.lang3.text.WordUtils;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@@ -39,8 +44,6 @@ import nl.requios.effortlessbuilding.buildmode.ModeOptions.OptionEnum;
* https://github.com/AlgorithmX2/Chisels-and-Bits/blob/1.12/src/main/java/mod/chiselsandbits/client/gui/ChiselsAndBitsMenu.java * https://github.com/AlgorithmX2/Chisels-and-Bits/blob/1.12/src/main/java/mod/chiselsandbits/client/gui/ChiselsAndBitsMenu.java
*/ */
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class RadialMenu extends Screen { public class RadialMenu extends Screen {
public static final RadialMenu instance = new RadialMenu(); public static final RadialMenu instance = new RadialMenu();
@@ -181,7 +184,7 @@ public class RadialMenu extends Screen {
drawIcons(ms, middleX, middleY, modes, buttons); drawIcons(ms, middleX, middleY, modes, buttons);
drawTexts(ms, currentBuildMode, middleX, middleY, modes, buttons, options); drawTexts(ms, currentBuildMode, middleX, middleY, modes, buttons, options, mouseXX, mouseYY);
ms.popPose(); ms.popPose();
} }
@@ -311,7 +314,7 @@ public class RadialMenu extends Screen {
ms.popPose(); ms.popPose();
} }
private void drawTexts(PoseStack ms, BuildModeEnum currentBuildMode, double middleX, double middleY, ArrayList<MenuRegion> modes, ArrayList<MenuButton> buttons, OptionEnum[] options) { private void drawTexts(PoseStack ms, BuildModeEnum currentBuildMode, double middleX, double middleY, ArrayList<MenuRegion> modes, ArrayList<MenuButton> buttons, OptionEnum[] options, int mouseX, int mouseY) {
//font.drawStringWithShadow("Actions", (int) (middleX - buttonDistance - 13) - font.getStringWidth("Actions") * 0.5f, (int) middleY - 38, 0xffffffff); //font.drawStringWithShadow("Actions", (int) (middleX - buttonDistance - 13) - font.getStringWidth("Actions") * 0.5f, (int) middleY - 38, 0xffffffff);
//Draw option strings //Draw option strings
@@ -352,60 +355,25 @@ public class RadialMenu extends Screen {
for (final MenuButton button : buttons) { for (final MenuButton button : buttons) {
if (button.highlighted) { if (button.highlighted) {
String text = ChatFormatting.AQUA + button.name; var tooltip = new ArrayList<Component>();
String description = ChatFormatting.WHITE + button.description; tooltip.add(Components.literal(button.name).withStyle(ChatFormatting.AQUA));
//Add keybind in brackets
String keybind = findKeybind(button, currentBuildMode);
boolean hasKeybind = !keybind.isEmpty();
keybind = ChatFormatting.GRAY + "(" + WordUtils.capitalizeFully(keybind) + ")";
if (button.textSide == Direction.WEST) {
font.draw(ms, text, (int) (middleX + button.x1 - 8) - font.width(text),
(int) (middleY + button.y1 + 6), whiteTextColor);
} else if (button.textSide == Direction.EAST) {
font.draw(ms, text, (int) (middleX + button.x2 + 8),
(int) (middleY + button.y1 + 6), whiteTextColor);
} else if (button.textSide == Direction.UP || button.textSide == Direction.NORTH) {
int y = (int) (middleY + button.y1 - 14);
font.draw(ms, text, (int) (middleX + (button.x1 + button.x2) * 0.5 - font.width(text) * 0.5), y, whiteTextColor);
y -= 12;
if (hasKeybind) {
font.draw(ms, keybind, (int) (middleX + (button.x1 + button.x2) * 0.5 - font.width(keybind) * 0.5), y, whiteTextColor);
y -= 12;
}
if (!description.isEmpty())
font.drawWordWrap(FormattedText.of(description), (int) (middleX + (button.x1 + button.x2) * 0.5 - actionDescriptionWidth * 0.5), y, actionDescriptionWidth, whiteTextColor);
} else if (button.textSide == Direction.DOWN || button.textSide == Direction.SOUTH) {
int y = (int) (middleY + button.y1 + 26);
font.draw(ms, text, (int) (middleX + (button.x1 + button.x2) * 0.5 - font.width(text) * 0.5), y, whiteTextColor);
y += 12;
if (hasKeybind) {
font.draw(ms, keybind, (int) (middleX + (button.x1 + button.x2) * 0.5 - font.width(keybind) * 0.5), y, whiteTextColor);
y += 12;
}
if (!description.isEmpty())
font.drawWordWrap(FormattedText.of(description), (int) (middleX + (button.x1 + button.x2) * 0.5 - actionDescriptionWidth * 0.5), y, actionDescriptionWidth, whiteTextColor);
//Add description when holding shift
if (!button.description.isEmpty()) {
tooltip.add(TooltipHelper.holdShift(ItemDescription.Palette.Blue, hasShiftDown()));
if (hasShiftDown()) tooltip.add(Component.literal(button.description).withStyle(ChatFormatting.GRAY));
} }
//Add keybind in brackets
var keybind = findKeybind(button);
if (keybind != null) tooltip.add(Lang.translateDirect("tooltip.keybind", keybind.withStyle(ChatFormatting.GRAY)).withStyle(ChatFormatting.DARK_GRAY));
renderComponentTooltip(ms, tooltip, mouseX, mouseY);
} }
} }
} }
private String findKeybind(MenuButton button, BuildModeEnum currentBuildMode){ private MutableComponent findKeybind(MenuButton button){
String result = "";
int keybindingIndex = -1; int keybindingIndex = -1;
if (button.action == ActionEnum.OPEN_MODIFIER_SETTINGS) keybindingIndex = 1; if (button.action == ActionEnum.OPEN_MODIFIER_SETTINGS) keybindingIndex = 1;
if (button.action == ActionEnum.UNDO) keybindingIndex = 2; if (button.action == ActionEnum.UNDO) keybindingIndex = 2;
@@ -414,23 +382,9 @@ public class RadialMenu extends Screen {
if (keybindingIndex != -1) { if (keybindingIndex != -1) {
KeyMapping keyMap = ClientEvents.keyBindings[keybindingIndex]; KeyMapping keyMap = ClientEvents.keyBindings[keybindingIndex];
if (!keyMap.getKeyModifier().name().equals("None")) { return Components.keybind(keyMap.getName());
result = keyMap.getKeyModifier().name() + " ";
}
result += I18n.get(keyMap.getKey().getName());
} }
return null;
if (currentBuildMode.options.length > 0) {
//Add (ctrl) to first two actions of first option
if (button.action == currentBuildMode.options[0].actions[0]
|| button.action == currentBuildMode.options[0].actions[1]) {
result = I18n.get(ClientEvents.keyBindings[4].getKey().getDisplayName().getString());
if (result.equals("Left Control")) result = "Ctrl";
}
}
result = result.replace("Key.keyboard.", "");
return result;
} }
private boolean inTriangle(final double x1, final double y1, final double x2, final double y2, private boolean inTriangle(final double x1, final double y1, final double x2, final double y2,

View File

@@ -102,7 +102,9 @@ public class MirrorEntry extends BaseModifierEntry<Mirror> {
//Radius //Radius
radiusInput = new LabeledScrollInput(0, 0, 27, 18) radiusInput = new LabeledScrollInput(0, 0, 27, 18)
.withRange(0, ReachHelper.getMaxMirrorRadius(Minecraft.getInstance().player)) .withRange(0, ReachHelper.getMaxMirrorRadius(Minecraft.getInstance().player))
.titled(Component.literal("Radius. Use Reach Upgrade items to increase maximum.")) .titled(Minecraft.getInstance().player.isCreative() ?
Component.literal("Radius") :
Component.literal("Radius. Use Reach Upgrade items to increase maximum."))
.calling(value -> { .calling(value -> {
modifier.radius = value; modifier.radius = value;
onValueChanged(); onValueChanged();
@@ -212,21 +214,21 @@ public class MirrorEntry extends BaseModifierEntry<Mirror> {
//Show lines button //Show lines button
if (modifier.drawLines) { if (modifier.drawLines) {
showLinesButton.setIcon(AllIcons.I_SHOW_LINES); showLinesButton.setIcon(AllIcons.I_SHOW_LINES);
showLinesButton.setToolTip(Components.literal("Showing mirror lines")); showLinesButton.setToolTip(Components.literal("Show mirror lines"));
} }
else { else {
showLinesButton.setIcon(AllIcons.I_HIDE_LINES); showLinesButton.setIcon(AllIcons.I_HIDE_LINES);
showLinesButton.setToolTip(Components.literal("Not showing mirror lines")); showLinesButton.setToolTip(Components.literal("Show mirror lines"));
} }
//Show areas button //Show areas button
if (modifier.drawPlanes) { if (modifier.drawPlanes) {
showAreasButton.setIcon(AllIcons.I_SHOW_AREAS); showAreasButton.setIcon(AllIcons.I_SHOW_AREAS);
showAreasButton.setToolTip(Components.literal("Showing mirror areas")); showAreasButton.setToolTip(Components.literal("Show mirror areas"));
} }
else { else {
showAreasButton.setIcon(AllIcons.I_HIDE_AREAS); showAreasButton.setIcon(AllIcons.I_HIDE_AREAS);
showAreasButton.setToolTip(Components.literal("Not showing mirror areas")); showAreasButton.setToolTip(Components.literal("Show mirror areas"));
} }
} }
} }

View File

@@ -43,14 +43,14 @@ public class RadialMirrorEntry extends BaseModifierEntry<RadialMirror> {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
final int index = i; final int index = i;
var scrollInput = new LabeledScrollInput(0, 0, 36, 18) var scrollInput = new LabeledScrollInput(0, 0, 36, 18)
.showControlScrollsSlowerTooltip() .showControlScrollsSlowerTooltip()
.titled(Component.literal(i == 0 ? "X Position" : i == 1 ? "Y Position" : "Z Position")) .titled(Component.literal(i == 0 ? "X Position" : i == 1 ? "Y Position" : "Z Position"))
.format(integer -> Component.literal(df.format(integer / 2.0))) .format(integer -> Component.literal(df.format(integer / 2.0)))
.withStepFunction(stepContext -> stepContext.shift ? 20 : stepContext.control ? 1 : 2) .withStepFunction(stepContext -> stepContext.shift ? 20 : stepContext.control ? 1 : 2)
.calling(value -> { .calling(value -> {
modifier.position = MathHelper.with(modifier.position, index, value / 2.0); modifier.position = MathHelper.with(modifier.position, index, value / 2.0);
onValueChanged(); onValueChanged();
}); });
scrollInput.setState((int) (MathHelper.get(modifier.position, index) * 2.0)); scrollInput.setState((int) (MathHelper.get(modifier.position, index) * 2.0));
positionInputs.add(scrollInput); positionInputs.add(scrollInput);
} }
@@ -58,80 +58,80 @@ public class RadialMirrorEntry extends BaseModifierEntry<RadialMirror> {
//Player position button //Player position button
playerPositionButton = new IconButton(0, 0, AllIcons.I_PLAYER) playerPositionButton = new IconButton(0, 0, AllIcons.I_PLAYER)
.withCallback(() -> { .withCallback(() -> {
modifier.position = Vec3.atLowerCornerOf(Minecraft.getInstance().player.blockPosition()); modifier.position = Vec3.atLowerCornerOf(Minecraft.getInstance().player.blockPosition());
onValueChanged(); onValueChanged();
}); });
playerPositionButton.setToolTip(Components.literal("Set to player position")); playerPositionButton.setToolTip(Components.literal("Set to player position"));
listeners.add(playerPositionButton); listeners.add(playerPositionButton);
//Toggle offset button //Toggle offset button
toggleOffsetButton = new IconButton(0, 0, AllIcons.I_BLOCK_CENTER) toggleOffsetButton = new IconButton(0, 0, AllIcons.I_BLOCK_CENTER)
.withCallback(() -> { .withCallback(() -> {
if (modifier.position.x == Math.floor(modifier.position.x)) { if (modifier.position.x == Math.floor(modifier.position.x)) {
modifier.position = new Vec3( modifier.position = new Vec3(
Math.floor(modifier.position.x) + 0.5, Math.floor(modifier.position.x) + 0.5,
Math.floor(modifier.position.y) + 0.5, Math.floor(modifier.position.y) + 0.5,
Math.floor(modifier.position.z) + 0.5 Math.floor(modifier.position.z) + 0.5
); );
} }
else { else {
modifier.position = new Vec3( modifier.position = new Vec3(
Math.floor(modifier.position.x), Math.floor(modifier.position.x),
Math.floor(modifier.position.y), Math.floor(modifier.position.y),
Math.floor(modifier.position.z) Math.floor(modifier.position.z)
); );
} }
onValueChanged(); onValueChanged();
}); });
listeners.add(toggleOffsetButton); listeners.add(toggleOffsetButton);
//Slices //Slices
slicesInput = new LabeledScrollInput(0, 0, 27, 18) slicesInput = new LabeledScrollInput(0, 0, 27, 18)
.withRange(3, 1000) .withRange(3, 1000)
.titled(Component.literal("Slices")) .titled(Component.literal("Slices"))
.calling(value -> { .calling(value -> {
modifier.slices = value; modifier.slices = value;
onValueChanged(); onValueChanged();
}); });
slicesInput.setState(modifier.slices); slicesInput.setState(modifier.slices);
listeners.add(slicesInput); listeners.add(slicesInput);
//Alternate //Alternate
alternateButton = new IconButton(0, 0, AllIcons.I_ALTERNATE_OFF) alternateButton = new IconButton(0, 0, AllIcons.I_ALTERNATE_OFF)
.withCallback(() -> { .withCallback(() -> {
modifier.alternate = !modifier.alternate; modifier.alternate = !modifier.alternate;
onValueChanged(); onValueChanged();
}); });
listeners.add(alternateButton); listeners.add(alternateButton);
//Radius //Radius
radiusInput = new LabeledScrollInput(0, 0, 27, 18) radiusInput = new LabeledScrollInput(0, 0, 27, 18)
.withRange(0, ReachHelper.getMaxMirrorRadius(Minecraft.getInstance().player)) .withRange(0, ReachHelper.getMaxMirrorRadius(Minecraft.getInstance().player))
.titled(Minecraft.getInstance().player.isCreative() ? .titled(Minecraft.getInstance().player.isCreative() ?
Component.literal("Radius") : Component.literal("Radius") :
Component.literal("Radius. Use Reach Upgrade items to increase maximum.")) Component.literal("Radius. Use Reach Upgrade items to increase maximum."))
.calling(value -> { .calling(value -> {
modifier.radius = value; modifier.radius = value;
onValueChanged(); onValueChanged();
}); });
radiusInput.setState(modifier.radius); radiusInput.setState(modifier.radius);
listeners.add(radiusInput); listeners.add(radiusInput);
//Show lines button //Show lines button
showLinesButton = new IconButton(0, 0, AllIcons.I_SHOW_LINES) showLinesButton = new IconButton(0, 0, AllIcons.I_SHOW_LINES)
.withCallback(() -> { .withCallback(() -> {
modifier.drawLines = !modifier.drawLines; modifier.drawLines = !modifier.drawLines;
onValueChanged(); onValueChanged();
}); });
listeners.add(showLinesButton); listeners.add(showLinesButton);
//Show areas button //Show areas button
showAreasButton = new IconButton(0, 0, AllIcons.I_SHOW_AREAS) showAreasButton = new IconButton(0, 0, AllIcons.I_SHOW_AREAS)
.withCallback(() -> { .withCallback(() -> {
modifier.drawPlanes = !modifier.drawPlanes; modifier.drawPlanes = !modifier.drawPlanes;
onValueChanged(); onValueChanged();
}); });
listeners.add(showAreasButton); listeners.add(showAreasButton);
for (ScrollInput positionInput : positionInputs) { for (ScrollInput positionInput : positionInputs) {
@@ -221,21 +221,21 @@ public class RadialMirrorEntry extends BaseModifierEntry<RadialMirror> {
//Show lines button //Show lines button
if (modifier.drawLines) { if (modifier.drawLines) {
showLinesButton.setIcon(AllIcons.I_SHOW_LINES); showLinesButton.setIcon(AllIcons.I_SHOW_LINES);
showLinesButton.setToolTip(Components.literal("Showing mirror lines")); showLinesButton.setToolTip(Components.literal("Show mirror lines"));
} }
else { else {
showLinesButton.setIcon(AllIcons.I_HIDE_LINES); showLinesButton.setIcon(AllIcons.I_HIDE_LINES);
showLinesButton.setToolTip(Components.literal("Not showing mirror lines")); showLinesButton.setToolTip(Components.literal("Show mirror lines"));
} }
//Show areas button //Show areas button
if (modifier.drawPlanes) { if (modifier.drawPlanes) {
showAreasButton.setIcon(AllIcons.I_SHOW_AREAS); showAreasButton.setIcon(AllIcons.I_SHOW_AREAS);
showAreasButton.setToolTip(Components.literal("Showing mirror areas")); showAreasButton.setToolTip(Components.literal("Show mirror areas"));
} }
else { else {
showAreasButton.setIcon(AllIcons.I_HIDE_AREAS); showAreasButton.setIcon(AllIcons.I_HIDE_AREAS);
showAreasButton.setToolTip(Components.literal("Not showing mirror areas")); showAreasButton.setToolTip(Components.literal("Show mirror areas"));
} }
} }
} }

View File

@@ -90,5 +90,11 @@
"effortlessbuilding.gui.scrollInput.scrollToAdjustAmount": "Scroll to Adjust Amount", "effortlessbuilding.gui.scrollInput.scrollToAdjustAmount": "Scroll to Adjust Amount",
"effortlessbuilding.gui.scrollInput.scrollToSelect": "Scroll to Select", "effortlessbuilding.gui.scrollInput.scrollToSelect": "Scroll to Select",
"effortlessbuilding.gui.scrollInput.shiftScrollsFaster": "Shift to Scroll Faster", "effortlessbuilding.gui.scrollInput.shiftScrollsFaster": "Shift to Scroll Faster",
"effortlessbuilding.gui.scrollInput.controlScrollsSlower": "Control to Scroll Slower" "effortlessbuilding.gui.scrollInput.controlScrollsSlower": "Control to Scroll Slower",
"effortlessbuilding.tooltip.holdForDescription": "Hold [%1$s] for Summary",
"effortlessbuilding.tooltip.holdForControls": "Hold [%1$s] for Controls",
"effortlessbuilding.tooltip.keyShift": "Shift",
"effortlessbuilding.tooltip.keyCtrl": "Ctrl",
"effortlessbuilding.tooltip.keybind": "[%1$s]"
} }