mirror of
https://github.com/Shiewk/Widgets.git
synced 2026-04-28 11:34:17 +02:00
Add direction widget (#7)
This commit is contained in:
@@ -70,6 +70,7 @@ public class WidgetsModClient implements ClientModInitializer {
|
|||||||
WidgetManager.register(new ArmorHudWidget(Identifier.of(WidgetsMod.MOD_ID, "armor")));
|
WidgetManager.register(new ArmorHudWidget(Identifier.of(WidgetsMod.MOD_ID, "armor")));
|
||||||
WidgetManager.register(new InventoryWidget(Identifier.of(WidgetsMod.MOD_ID, "inventory")));
|
WidgetManager.register(new InventoryWidget(Identifier.of(WidgetsMod.MOD_ID, "inventory")));
|
||||||
WidgetManager.register(new WorldTimeWidget(Identifier.of(WidgetsMod.MOD_ID, "worldtime")));
|
WidgetManager.register(new WorldTimeWidget(Identifier.of(WidgetsMod.MOD_ID, "worldtime")));
|
||||||
|
WidgetManager.register(new DirectionWidget(Identifier.of(WidgetsMod.MOD_ID, "direction")));
|
||||||
WidgetManager.register(TPSWidget.INSTANCE);
|
WidgetManager.register(TPSWidget.INSTANCE);
|
||||||
|
|
||||||
ComboWidget comboWidget = new ComboWidget(Identifier.of(WidgetsMod.MOD_ID, "combo"));
|
ComboWidget comboWidget = new ComboWidget(Identifier.of(WidgetsMod.MOD_ID, "combo"));
|
||||||
|
|||||||
@@ -57,4 +57,10 @@ public class WidgetUtils {
|
|||||||
return "0".repeat(2 - s.length()) + s;
|
return "0".repeat(2 - s.length()) + s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String reduceDigits(double v, int digits) {
|
||||||
|
if (digits == 0) return String.valueOf((int) Math.floor(v));
|
||||||
|
double f = Math.pow(10, digits);
|
||||||
|
return String.valueOf(Math.floor(v * f) / f);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package de.shiewk.widgets.widgets;
|
||||||
|
|
||||||
|
import de.shiewk.widgets.WidgetSettings;
|
||||||
|
import de.shiewk.widgets.utils.WidgetUtils;
|
||||||
|
import de.shiewk.widgets.widgets.settings.EnumWidgetSetting;
|
||||||
|
import de.shiewk.widgets.widgets.settings.IntSliderWidgetSetting;
|
||||||
|
import de.shiewk.widgets.widgets.settings.ToggleWidgetSetting;
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
|
import net.minecraft.client.font.TextRenderer;
|
||||||
|
import net.minecraft.client.gui.DrawContext;
|
||||||
|
import net.minecraft.client.network.ClientPlayerEntity;
|
||||||
|
import net.minecraft.text.MutableText;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.math.MathHelper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static net.minecraft.text.Text.*;
|
||||||
|
|
||||||
|
public class DirectionWidget extends BasicTextWidget {
|
||||||
|
|
||||||
|
public enum DisplayFormat {
|
||||||
|
YAW_ONLY(true),
|
||||||
|
DIRECTION_ONLY(false),
|
||||||
|
YAW_DIRECTION(true),
|
||||||
|
DIRECTION_YAW(true);
|
||||||
|
|
||||||
|
public final boolean showsYaw;
|
||||||
|
|
||||||
|
DisplayFormat(boolean showsYaw) {
|
||||||
|
this.showsYaw = showsYaw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Text format(int digits) {
|
||||||
|
String yaw = "0";
|
||||||
|
MutableText direction = literal("Direction");
|
||||||
|
ClientPlayerEntity player = MinecraftClient.getInstance().player;
|
||||||
|
if (player != null) {
|
||||||
|
yaw = WidgetUtils.reduceDigits(MathHelper.wrapDegrees(player.getYaw()), digits);
|
||||||
|
direction = translatable("widgets.widgets.direction." + player.getHorizontalFacing().name().toLowerCase(Locale.ROOT));
|
||||||
|
}
|
||||||
|
return switch (this){
|
||||||
|
case YAW_ONLY -> literal(yaw);
|
||||||
|
case DIRECTION_ONLY -> direction;
|
||||||
|
case YAW_DIRECTION -> literal(yaw+" (").append(direction).append(")");
|
||||||
|
case DIRECTION_YAW -> direction.append(" ("+yaw+")");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Text format(){
|
||||||
|
return format(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DisplayFormat displayFormat;
|
||||||
|
protected int digits = 1;
|
||||||
|
protected boolean realtime = false;
|
||||||
|
|
||||||
|
public DirectionWidget(Identifier id) {
|
||||||
|
super(id, List.of(
|
||||||
|
new EnumWidgetSetting<>(
|
||||||
|
"format",
|
||||||
|
translatable("widgets.widgets.direction.display"),
|
||||||
|
DisplayFormat.class,
|
||||||
|
DisplayFormat.DIRECTION_YAW,
|
||||||
|
DisplayFormat::format
|
||||||
|
),
|
||||||
|
new IntSliderWidgetSetting("digits", Text.translatable("widgets.widgets.direction.digits"), 0, 1, 3),
|
||||||
|
new ToggleWidgetSetting("realtime", translatable("widgets.widgets.common.realtime"), false)
|
||||||
|
));
|
||||||
|
getSettings().optionById("digits").setShowCondition(() -> displayFormat.showsYaw);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tickWidget() {
|
||||||
|
if (!realtime) refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void renderScaled(DrawContext context, long n, TextRenderer textRenderer, int posX, int posY) {
|
||||||
|
if (realtime) refresh();
|
||||||
|
super.renderScaled(context, n, textRenderer, posX, posY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refresh() {
|
||||||
|
formatAndSetRenderText(displayFormat.format(digits).getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text getName() {
|
||||||
|
return translatable("widgets.widgets.direction");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text getDescription() {
|
||||||
|
return translatable("widgets.widgets.direction.description");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSettingsChanged(WidgetSettings settings) {
|
||||||
|
super.onSettingsChanged(settings);
|
||||||
|
this.displayFormat = (DisplayFormat) settings.optionById("format").getValue();
|
||||||
|
this.realtime = (boolean) settings.optionById("realtime").getValue();
|
||||||
|
this.digits = (int) settings.optionById("digits").getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,8 @@ import net.minecraft.util.math.Vec3d;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static de.shiewk.widgets.utils.WidgetUtils.reduceDigits;
|
||||||
|
|
||||||
public class SpeedWidget extends BasicTextWidget {
|
public class SpeedWidget extends BasicTextWidget {
|
||||||
|
|
||||||
public enum Unit {
|
public enum Unit {
|
||||||
@@ -80,13 +82,7 @@ public class SpeedWidget extends BasicTextWidget {
|
|||||||
avg += v;
|
avg += v;
|
||||||
}
|
}
|
||||||
avg /= averagingWindow.length;
|
avg /= averagingWindow.length;
|
||||||
formatAndSetRenderText(reduceDigits(avg) + unit.displayName);
|
formatAndSetRenderText(reduceDigits(avg, digitsAfterComma) + unit.displayName);
|
||||||
}
|
|
||||||
|
|
||||||
private String reduceDigits(double v) {
|
|
||||||
if (digitsAfterComma == 0) return String.valueOf((int) Math.floor(v));
|
|
||||||
double f = Math.pow(10, digitsAfterComma);
|
|
||||||
return String.valueOf(Math.floor(v * f) / f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -107,6 +107,14 @@
|
|||||||
"widgets.widgets.cps.left": "Zeigt/Zählt Links-Klicks",
|
"widgets.widgets.cps.left": "Zeigt/Zählt Links-Klicks",
|
||||||
"widgets.widgets.cps.middle": "Zeigt/Zählt Mittel-Klicks",
|
"widgets.widgets.cps.middle": "Zeigt/Zählt Mittel-Klicks",
|
||||||
"widgets.widgets.cps.right": "Zeigt/Zählt Rechts-Klicks",
|
"widgets.widgets.cps.right": "Zeigt/Zählt Rechts-Klicks",
|
||||||
|
"widgets.widgets.direction": "Richtung",
|
||||||
|
"widgets.widgets.direction.description": "Zeigt an, in welche Richtung du guckst",
|
||||||
|
"widgets.widgets.direction.digits": "Nachkommastellen",
|
||||||
|
"widgets.widgets.direction.display": "Format",
|
||||||
|
"widgets.widgets.direction.east": "Osten",
|
||||||
|
"widgets.widgets.direction.north": "Norden",
|
||||||
|
"widgets.widgets.direction.south": "Süden",
|
||||||
|
"widgets.widgets.direction.west": "Westen",
|
||||||
"widgets.widgets.fps": "FPS",
|
"widgets.widgets.fps": "FPS",
|
||||||
"widgets.widgets.fps.description": "Zeigt deine aktuellen FPS an.",
|
"widgets.widgets.fps.description": "Zeigt deine aktuellen FPS an.",
|
||||||
"widgets.widgets.inventory": "Inventar",
|
"widgets.widgets.inventory": "Inventar",
|
||||||
|
|||||||
@@ -107,6 +107,14 @@
|
|||||||
"widgets.widgets.cps.left": "Display/Count left clicks",
|
"widgets.widgets.cps.left": "Display/Count left clicks",
|
||||||
"widgets.widgets.cps.middle": "Display/Count middle clicks",
|
"widgets.widgets.cps.middle": "Display/Count middle clicks",
|
||||||
"widgets.widgets.cps.right": "Display/Count right clicks",
|
"widgets.widgets.cps.right": "Display/Count right clicks",
|
||||||
|
"widgets.widgets.direction": "Direction",
|
||||||
|
"widgets.widgets.direction.description": "Shows in which direction you are looking",
|
||||||
|
"widgets.widgets.direction.digits": "Digits after comma",
|
||||||
|
"widgets.widgets.direction.display": "Format",
|
||||||
|
"widgets.widgets.direction.east": "East",
|
||||||
|
"widgets.widgets.direction.north": "North",
|
||||||
|
"widgets.widgets.direction.south": "South",
|
||||||
|
"widgets.widgets.direction.west": "West",
|
||||||
"widgets.widgets.fps": "FPS",
|
"widgets.widgets.fps": "FPS",
|
||||||
"widgets.widgets.fps.description": "Shows your current FPS.",
|
"widgets.widgets.fps.description": "Shows your current FPS.",
|
||||||
"widgets.widgets.inventory": "Inventory",
|
"widgets.widgets.inventory": "Inventory",
|
||||||
|
|||||||
Reference in New Issue
Block a user