Skip to content

Commit

Permalink
added synable text widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Dec 10, 2023
1 parent f74c681 commit 2a16b64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package muramasa.antimatter.gui.widget;

import com.mojang.blaze3d.vertex.PoseStack;
import muramasa.antimatter.capability.IGuiHandler;
import muramasa.antimatter.gui.GuiInstance;
import muramasa.antimatter.gui.ICanSyncData;
import muramasa.antimatter.gui.IGuiElement;
import muramasa.antimatter.gui.Widget;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

public class SyncableTextWidget extends TextWidget {
String text = "";
final Function<IGuiHandler, String> syncFunction;
protected SyncableTextWidget(@NotNull GuiInstance gui, @Nullable IGuiElement parent, int color, Function<IGuiHandler, String> syncFunction) {
super(gui, parent, a-> ((SyncableTextWidget)a).text, color);
this.syncFunction = syncFunction;

}

public static WidgetSupplier build(Function<IGuiHandler, String> textSyncFunction, int color) {
return builder((a, b) -> new SyncableTextWidget(a, b, color, textSyncFunction));
}

@Override
public void init() {
super.init();
gui.syncString(() -> syncFunction.apply(gui.handler), s -> text = s, ICanSyncData.SyncDirection.SERVER_TO_CLIENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ protected TextBoxWidget(@NotNull GuiInstance gui, @Nullable IGuiElement parent,
this.consumer = consumer;
}

public static WidgetSupplier build(BiConsumer<IGuiHandler, String> consumer) {
return builder((i, p) -> new ProgressWidget(i, p));
}

@Override
public void render(PoseStack matrixStack, double mouseX, double mouseY, float partialTicks) {

textBox.render(matrixStack, (int) mouseX, (int) mouseY, partialTicks);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import muramasa.antimatter.gui.IGuiElement;
import muramasa.antimatter.gui.Widget;
import muramasa.antimatter.util.Utils;
import net.minecraft.client.Minecraft;

import java.util.function.Function;

Expand All @@ -19,12 +20,23 @@ protected TextWidget(GuiInstance gui, IGuiElement parent, String text, int color
this.color = color;
}

protected TextWidget(GuiInstance gui, IGuiElement parent, Function<TextWidget, String> getter, int color) {
super(gui, parent);
this.getter = getter;
this.color = color;
}

public static WidgetSupplier build(String text, int color) {
return builder((a, b) -> new TextWidget(a, b, text, color)).clientSide();
}

@Override
public void render(PoseStack matrixStack, double mouseX, double mouseY, float partialTicks) {
this.drawText(matrixStack, Utils.literal(getter.apply(this)), realX(), realY(), color);
String text = getter.apply(this);
int textWidth = Minecraft.getInstance().font.width(text);
int xScaled = textWidth / 2;
int xCenter = (getW() / 2);
int xPosition = xCenter - xScaled;
this.drawText(matrixStack, Utils.literal(text), realX() + xPosition, realY(), color);
}
}

0 comments on commit 2a16b64

Please sign in to comment.