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

Allow resizing BasicTextWidgets

This commit is contained in:
Shy
2024-10-20 12:21:21 +02:00
parent 59acc266f2
commit 0027b67eac
6 changed files with 41 additions and 14 deletions
@@ -5,4 +5,5 @@ public interface Dimensionable {
int height(); int height();
double getX(int mx); double getX(int mx);
double getY(int my); double getY(int my);
float getScaleFactor();
} }
@@ -19,6 +19,10 @@ public abstract class ModWidget implements Dimensionable {
this.settings = WidgetSettings.ofId(id, customSettings); this.settings = WidgetSettings.ofId(id, customSettings);
} }
public float getScaleFactor(){
return 1f;
}
public final Identifier getId() { public final Identifier getId() {
return id; return id;
} }
@@ -41,8 +41,8 @@ public class WidgetRenderer implements HudRenderCallback, ClientTickEvents.Start
drawContext, drawContext,
timeNano, timeNano,
textRenderer, textRenderer,
(int) Math.round(Math.min(translateToScreen(settings.posX, windowWidth), windowWidth - widget.width())), (int) Math.round(Math.min(translateToScreen(settings.posX, windowWidth), windowWidth - (widget.width() * widget.getScaleFactor()))),
(int) Math.round(Math.min(translateToScreen(settings.posY, windowHeight), windowHeight - widget.height())) (int) Math.round(Math.min(translateToScreen(settings.posY, windowHeight), windowHeight - (widget.height() * widget.getScaleFactor())))
); );
profiler.pop(); profiler.pop();
} }
@@ -32,6 +32,11 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
public double getY(int my) { public double getY(int my) {
return y; return y;
} }
@Override
public float getScaleFactor() {
return 1f;
}
} }
private final Screen parent; private final Screen parent;
@@ -58,7 +63,7 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
for (Dimensionable rect : this.getAlignments(widget)) { for (Dimensionable rect : this.getAlignments(widget)) {
if (rect == widget) continue; if (rect == widget) continue;
final double nwx = rect.getX(this.width); final double nwx = rect.getX(this.width);
final double nww = rect.width(); final double nww = rect.width() * rect.getScaleFactor();
if (endX < nwx + factor && endX > nwx - factor){ if (endX < nwx + factor && endX > nwx - factor){
return new AlignResult(nwx - width, true); return new AlignResult(nwx - width, true);
} else if (x < nwx + factor && x > nwx - factor){ } else if (x < nwx + factor && x > nwx - factor){
@@ -81,7 +86,7 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
alignments.add(new Alignment(this.width / 2, this.height / 2, this.width / 2 - 2, this.height / 2 - 2)); alignments.add(new Alignment(this.width / 2, this.height / 2, this.width / 2 - 2, this.height / 2 - 2));
alignments.add(new Alignment(2, this.height / 2, this.width / 2 - 2, this.height / 2 - 2)); alignments.add(new Alignment(2, this.height / 2, this.width / 2 - 2, this.height / 2 - 2));
alignments.add(new Alignment(this.width / 2, 2, this.width / 2 - 2, this.height / 2 - 2)); alignments.add(new Alignment(this.width / 2, 2, this.width / 2 - 2, this.height / 2 - 2));
alignments.add(new Alignment(this.width / 2 - rel.width() / 2, this.height / 2 - rel.height() / 2, rel.width(), rel.height())); alignments.add(new Alignment((int) ((float) this.width / 2 - (rel.width() * rel.getScaleFactor()) / 2), (int) ((float) this.height / 2 - (rel.height() * rel.getScaleFactor()) / 2), (int) (rel.width() * rel.getScaleFactor()), (int) (rel.height() * rel.getScaleFactor())));
return alignments; return alignments;
} }
@@ -92,7 +97,7 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
for (Dimensionable rect : this.getAlignments(widget)) { for (Dimensionable rect : this.getAlignments(widget)) {
if (rect == widget) continue; if (rect == widget) continue;
final double nwy = rect.getY(this.height); final double nwy = rect.getY(this.height);
final double nwh = rect.height(); final double nwh = rect.height() * rect.getScaleFactor();
if (endY < nwy + factor && endY > nwy - factor){ if (endY < nwy + factor && endY > nwy - factor){
return new AlignResult(nwy - height, true); return new AlignResult(nwy - height, true);
} else if (y < nwy + factor && y > nwy - factor){ } else if (y < nwy + factor && y > nwy - factor){
@@ -115,9 +120,9 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
assert client != null; assert client != null;
for (ModWidget widget : WidgetManager.getEnabledWidgets()) { for (ModWidget widget : WidgetManager.getEnabledWidgets()) {
final WidgetSettings settings = widget.getSettings(); final WidgetSettings settings = widget.getSettings();
final int ww = widget.width(); final int ww = (int) (widget.width() * widget.getScaleFactor());
double wx = Math.min(translateToScreen(settings.posX, this.width), this.width - ww); double wx = Math.min(translateToScreen(settings.posX, this.width), this.width - ww);
final int wh = widget.height(); final int wh = (int) (widget.height() * widget.getScaleFactor());
double wy = Math.min(translateToScreen(settings.posY, this.height), this.height - wh); double wy = Math.min(translateToScreen(settings.posY, this.height), this.height - wh);
if (selectedWidget == widget){ if (selectedWidget == widget){
final AlignResult alignedX = alignX(wx, ww, widget); final AlignResult alignedX = alignX(wx, ww, widget);
@@ -151,13 +156,13 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
@Override @Override
public boolean mouseReleased(double mouseX, double mouseY, int button) { public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (button == 0 && selectedWidget != null){ if (button == 0 && selectedWidget != null){
final AlignResult alignedX = alignX(translateToScreen(selectedWidget.getSettings().posX, this.width), selectedWidget.width(), selectedWidget); final AlignResult alignedX = alignX(translateToScreen(selectedWidget.getSettings().posX, this.width), (int) (selectedWidget.width() * selectedWidget.getScaleFactor()), selectedWidget);
if (alignedX != null){ if (alignedX != null){
selectedWidget.getSettings().setPosX(translateToWidgetSettingsValue(alignedX.result(), this.width), selectedWidget.width(), this.width); selectedWidget.getSettings().setPosX(translateToWidgetSettingsValue(alignedX.result(), this.width), (int) (selectedWidget.width() * selectedWidget.getScaleFactor()), this.width);
} }
final AlignResult alignedY = alignY(translateToScreen(selectedWidget.getSettings().posY, this.height), selectedWidget.height(), selectedWidget); final AlignResult alignedY = alignY(translateToScreen(selectedWidget.getSettings().posY, this.height), (int) (selectedWidget.height() * selectedWidget.getScaleFactor()), selectedWidget);
if (alignedY != null){ if (alignedY != null){
selectedWidget.getSettings().setPosY(translateToWidgetSettingsValue(alignedY.result(), this.height), selectedWidget.height(), this.height); selectedWidget.getSettings().setPosY(translateToWidgetSettingsValue(alignedY.result(), this.height), (int) (selectedWidget.height() * selectedWidget.getScaleFactor()), this.height);
} }
onEdit.accept(selectedWidget); onEdit.accept(selectedWidget);
selectedWidget = null; selectedWidget = null;
@@ -180,9 +185,9 @@ public class EditWidgetPositionsScreen extends AnimatedScreen {
final ModWidget widget = selectedWidget; final ModWidget widget = selectedWidget;
if (widget != null){ if (widget != null){
final WidgetSettings settings = widget.getSettings(); final WidgetSettings settings = widget.getSettings();
final int ww = widget.width(); final int ww = (int) (widget.width() * widget.getScaleFactor());
final int wx = (int) Math.min(translateToScreen(settings.posX, this.width), this.width - ww); final int wx = (int) Math.min(translateToScreen(settings.posX, this.width), this.width - ww);
final int wh = widget.height(); final int wh = (int) (widget.height() * widget.getScaleFactor());
final int wy = (int) Math.min(translateToScreen(settings.posY, this.height), this.height - wh); final int wy = (int) Math.min(translateToScreen(settings.posY, this.height), this.height - wh);
if (mouseX <= wx + ww + deltaX && mouseX >= wx + deltaX){ if (mouseX <= wx + ww + deltaX && mouseX >= wx + deltaX){
if (mouseY <= wy + wh + deltaY && mouseY >= wy + deltaY){ if (mouseY <= wy + wh + deltaY && mouseY >= wy + deltaY){
@@ -31,7 +31,7 @@ public class WidgetSettingsScreen extends AnimatedScreen {
@Override @Override
public void renderScreenContents(DrawContext context, int mouseX, int mouseY, float delta) { public void renderScreenContents(DrawContext context, int mouseX, int mouseY, float delta) {
context.drawText(textRenderer, previewText, this.width * 3 / 4 - textRenderer.getWidth(previewText) / 2, this.height / 50, 0xffffffff, false); context.drawText(textRenderer, previewText, this.width * 3 / 4 - textRenderer.getWidth(previewText) / 2, this.height / 50, 0xffffffff, false);
widget.render(context, Util.getMeasuringTimeNano(), textRenderer, this.width * 3 / 4 - widget.width() / 2, this.height / 2 - widget.height() / 2); widget.render(context, Util.getMeasuringTimeNano(), textRenderer, (int) ((float) (this.width * 3) / 4 - (widget.width() * widget.getScaleFactor()) / 2), (int) ((float) this.height / 2 - (widget.height() * widget.getScaleFactor()) / 2));
} }
@Override @Override
@@ -9,6 +9,7 @@ import de.shiewk.widgets.widgets.settings.RGBAColorWidgetSetting;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.font.TextRenderer; import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
@@ -45,6 +46,7 @@ public abstract class BasicTextWidget extends ModWidget {
list.add(new RGBAColorWidgetSetting("textcolor", Text.translatable("widgets.widgets.basictext.textcolor"), 255, 255, 255, 255)); 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("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 IntSliderWidgetSetting("height", Text.translatable("widgets.widgets.basictext.height"), 9, DEFAULT_HEIGHT, 80));
list.add(new IntSliderWidgetSetting("size", Text.translatable("widgets.widgets.common.sizePercent"), 25, 100, 400));
list.add(new EnumWidgetSetting<>("alignment", Text.translatable("widgets.widgets.basictext.alignment"), TextAlignment.class, TextAlignment.CENTER, TextAlignment::text)); 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)); list.add(new IntSliderWidgetSetting("padding", Text.translatable("widgets.widgets.basictext.padding"), 0, 5, 20));
return list; return list;
@@ -60,6 +62,8 @@ public abstract class BasicTextWidget extends ModWidget {
DEFAULT_BACKGROUND_COLOR = new Color(0, 0, 0, 80).getRGB(), DEFAULT_BACKGROUND_COLOR = new Color(0, 0, 0, 80).getRGB(),
DEFAULT_TEXT_COLOR = new Color(255, 255 ,255, 255).getRGB(); DEFAULT_TEXT_COLOR = new Color(255, 255 ,255, 255).getRGB();
protected float size = 2f;
protected int backgroundColor = DEFAULT_BACKGROUND_COLOR, textColor = DEFAULT_TEXT_COLOR, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; protected int backgroundColor = DEFAULT_BACKGROUND_COLOR, textColor = DEFAULT_TEXT_COLOR, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
protected TextAlignment textAlignment = TextAlignment.CENTER; protected TextAlignment textAlignment = TextAlignment.CENTER;
@@ -75,9 +79,21 @@ public abstract class BasicTextWidget extends ModWidget {
@Override @Override
public void render(DrawContext context, long n, TextRenderer textRenderer, int posX, int posY) { public void render(DrawContext context, long n, TextRenderer textRenderer, int posX, int posY) {
MatrixStack matrices = context.getMatrices();
if (size != 1f){
matrices.push();
matrices.translate(-(size-1) * posX, -(size-1) * posY, 0);
matrices.scale(size, size, 1);
}
renderer = textRenderer; renderer = textRenderer;
context.fill(posX, posY, posX + width(), posY + height(), this.backgroundColor); context.fill(posX, posY, posX + width(), posY + height(), this.backgroundColor);
context.drawText(textRenderer, renderText, posX + textX, posY + textY, this.textColor, true); context.drawText(textRenderer, renderText, posX + textX, posY + textY, this.textColor, true);
if (size != 1f) matrices.pop();
}
@Override
public float getScaleFactor() {
return size;
} }
@Override @Override
@@ -104,5 +120,6 @@ public abstract class BasicTextWidget extends ModWidget {
this.height = ((IntSliderWidgetSetting) settings.optionById("height")).getValue(); this.height = ((IntSliderWidgetSetting) settings.optionById("height")).getValue();
this.textAlignment = (TextAlignment) ((EnumWidgetSetting<?>) settings.optionById("alignment")).getValue(); this.textAlignment = (TextAlignment) ((EnumWidgetSetting<?>) settings.optionById("alignment")).getValue();
this.padding = ((IntSliderWidgetSetting) settings.optionById("padding")).getValue(); this.padding = ((IntSliderWidgetSetting) settings.optionById("padding")).getValue();
this.size = 0.01f * ((IntSliderWidgetSetting) settings.optionById("size")).getValue();
} }
} }