1
mirror of https://github.com/Shiewk/Widgets.git synced 2026-04-28 11:34:17 +02:00

Add widget width, height, text alignment and padding options

This commit is contained in:
Shy
2024-09-09 10:53:07 +02:00
parent 9e7c71e928
commit 1b1f17f2bd
3 changed files with 141 additions and 7 deletions
@@ -3,6 +3,8 @@ package de.shiewk.widgets.widgets;
import de.shiewk.widgets.ModWidget;
import de.shiewk.widgets.WidgetSettingOption;
import de.shiewk.widgets.WidgetSettings;
import de.shiewk.widgets.widgets.settings.EnumWidgetSetting;
import de.shiewk.widgets.widgets.settings.IntSliderWidgetSetting;
import de.shiewk.widgets.widgets.settings.RGBAColorWidgetSetting;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.font.TextRenderer;
@@ -14,18 +16,42 @@ import java.awt.*;
import java.util.List;
public abstract class BasicTextWidget extends ModWidget {
public enum TextAlignment {
RIGHT("right"),
CENTER("center"),
LEFT("left");
public final String key;
TextAlignment(String key) {
this.key = key;
}
public Text text(){
return Text.translatable("widgets.widgets.basictext.alignment." + key);
}
}
protected Text renderText = Text.empty();
private int textWidthH;
private int textX = 0;
private int textY = 0;
private int padding = 0;
private TextRenderer renderer = null;
private static ObjectArrayList<WidgetSettingOption> getCustomSettings(List<WidgetSettingOption> otherCustomOptions) {
final ObjectArrayList<WidgetSettingOption> list = new ObjectArrayList<>(otherCustomOptions);
list.add(new RGBAColorWidgetSetting("backgroundcolor", Text.translatable("widgets.widgets.basictext.background"), 0, 0, 0, 80));
list.add(new RGBAColorWidgetSetting("textcolor", Text.translatable("widgets.widgets.basictext.textcolor"), 255, 255, 255, 255));
list.add(new IntSliderWidgetSetting("width", Text.translatable("widgets.widgets.basictext.width"), 10, DEFAULT_WIDTH, 80*3));
list.add(new IntSliderWidgetSetting("height", Text.translatable("widgets.widgets.basictext.height"), 9, DEFAULT_HEIGHT, 80));
list.add(new EnumWidgetSetting<>("alignment", Text.translatable("widgets.widgets.basictext.alignment"), TextAlignment.class, TextAlignment.CENTER, TextAlignment::text));
list.add(new IntSliderWidgetSetting("padding", Text.translatable("widgets.widgets.basictext.padding"), 0, 5, 20));
return list;
}
protected BasicTextWidget(Identifier id, List<WidgetSettingOption> otherCustomOptions) {
super(id, getCustomSettings(otherCustomOptions));
getSettings().optionById("padding").setShowCondition(() -> this.textAlignment != TextAlignment.CENTER);
}
protected static final int
@@ -34,32 +60,39 @@ public abstract class BasicTextWidget extends ModWidget {
DEFAULT_BACKGROUND_COLOR = new Color(0, 0, 0, 80).getRGB(),
DEFAULT_TEXT_COLOR = new Color(255, 255 ,255, 255).getRGB();
protected int backgroundColor = DEFAULT_BACKGROUND_COLOR, textColor = DEFAULT_TEXT_COLOR;
protected int backgroundColor = DEFAULT_BACKGROUND_COLOR, textColor = DEFAULT_TEXT_COLOR, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
protected TextAlignment textAlignment = TextAlignment.CENTER;
@Override
public int width() {
return DEFAULT_WIDTH;
return width;
}
@Override
public int height() {
return DEFAULT_HEIGHT;
return height;
}
@Override
public void render(DrawContext context, long n, TextRenderer textRenderer, int posX, int posY) {
renderer = textRenderer;
context.fill(posX, posY, posX + width(), posY + height(), this.backgroundColor);
context.drawText(textRenderer, renderText, posX + (width() / 2) - textWidthH, posY + 6, this.textColor, true);
context.drawText(textRenderer, renderText, posX + textX, posY + textY, this.textColor, true);
}
@Override
public final void tick() {
tickWidget();
if (renderer != null){
this.textWidthH = renderer.getWidth(renderText) / 2;
int textWidth = renderer.getWidth(renderText);
switch (textAlignment){
case LEFT -> textX = padding;
case CENTER -> textX = width() / 2 - textWidth / 2;
case RIGHT -> textX = width() - padding - textWidth;
}
}
textY = (height-9) / 2;
}
public abstract void tickWidget();
@@ -67,5 +100,9 @@ public abstract class BasicTextWidget extends ModWidget {
public void onSettingsChanged(WidgetSettings settings) {
this.backgroundColor = ((RGBAColorWidgetSetting) settings.optionById("backgroundcolor")).getColor();
this.textColor = ((RGBAColorWidgetSetting) settings.optionById("textcolor")).getColor();
this.width = ((IntSliderWidgetSetting) settings.optionById("width")).getValue();
this.height = ((IntSliderWidgetSetting) settings.optionById("height")).getValue();
this.textAlignment = (TextAlignment) ((EnumWidgetSetting<?>) settings.optionById("alignment")).getValue();
this.padding = ((IntSliderWidgetSetting) settings.optionById("padding")).getValue();
}
}
@@ -0,0 +1,90 @@
package de.shiewk.widgets.widgets.settings;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import de.shiewk.widgets.WidgetSettingOption;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
public class IntSliderWidgetSetting extends WidgetSettingOption {
private int value;
private final int minValue;
private final int maxValue;
private boolean changed = false;
private boolean clicked = false;
public IntSliderWidgetSetting(String id, Text name, int minValue, int defaultValue, int maxValue) {
super(id, name);
this.value = defaultValue;
this.minValue = minValue;
this.maxValue = maxValue;
}
@Override
public JsonElement saveState() {
return new JsonPrimitive(value);
}
@Override
public void loadState(JsonElement state) {
if (state.isJsonPrimitive() && state.getAsJsonPrimitive().isNumber()){
this.value = state.getAsInt();
}
}
private int valueToXPos(int value){
return MathHelper.lerp((float) (value - minValue) / (maxValue - minValue), getX() + 5, getX() + 155);
}
private int xPosToValue(int xpos){
return MathHelper.lerp((xpos - getX() - 5) / 150f, minValue, maxValue);
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
int xp = valueToXPos(getValue());
context.drawHorizontalLine(getX() + 5, getX() + 155, getY() + 6, 0xffffffff);
context.drawVerticalLine(getX() + 4, getY() + 3, getY() + 10, 0xffffffff);
context.drawVerticalLine(getX() + 155, getY() + 3, getY() + 10, 0xffffffff);
context.fill(xp-2, getY() + 3, xp+2, getY() + 10, 0xffffffff);
final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
context.drawText(textRenderer, String.valueOf(getValue()), getX() + 160, getY() + 3, 0xffffffff, true);
if (clicked){
this.changed = true;
this.value = MathHelper.clamp(xPosToValue(mouseX), minValue, maxValue);
}
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
this.clicked = true;
return super.mouseClicked(mouseX, mouseY, button);
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
this.clicked = false;
boolean t = this.changed;
this.changed = false;
return t;
}
public int getValue() {
return value;
}
@Override
public int getWidth() {
return 180;
}
@Override
public int getHeight() {
return 15;
}
}
@@ -49,5 +49,12 @@
"widgets.widgets.cps.appearance.slash": "Split (Slash)",
"widgets.widgets.cps.appearance.unified": "Unified",
"widgets.ui.editPositions.snap": "Align positions: %s",
"widgets.ui.editPositions.snap.help": "Aligns the widget with positions of other widgets"
"widgets.ui.editPositions.snap.help": "Aligns the widget with positions of other widgets",
"widgets.widgets.basictext.width": "Widget width",
"widgets.widgets.basictext.height": "Widget height",
"widgets.widgets.basictext.alignment": "Text alignment",
"widgets.widgets.basictext.alignment.left": "Left",
"widgets.widgets.basictext.alignment.center": "Center",
"widgets.widgets.basictext.alignment.right": "Right",
"widgets.widgets.basictext.padding": "Text padding"
}