Skip to content

Commit

Permalink
Added a text supplier for TextWidget. (#69)
Browse files Browse the repository at this point in the history
* Added text supplier for TextWidget.

* comments + spotless

* Restore TestTile.
  • Loading branch information
AbdielKavash authored Mar 7, 2024
1 parent 78669ca commit 417a5a7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/
public class TextWidget extends Widget {

private final Text text;
private Text text;
protected String localised;
protected Supplier<Text> textSupplier = null;
private int maxWidth = -1;
private Alignment textAlignment = Alignment.Center;
private final TextRenderer textRenderer = new TextRenderer();
Expand Down Expand Up @@ -76,6 +77,9 @@ public void onRebuild() {

@Override
public void onScreenUpdate() {
if (textSupplier != null) {
text = textSupplier.get();
}
if (isDynamic || isAutoSized()) {
String l = getText().getFormatted();
if (!l.equals(localised)) {
Expand All @@ -102,6 +106,30 @@ public Text getText() {
return text;
}

/**
* The textSupplier will ONLY be called on the client. It must have access to all the data it needs to build the
* text from the client side.
*/
public TextWidget setTextSupplier(Supplier<Text> textSupplier) {
this.textSupplier = textSupplier;
this.isDynamic = textSupplier != null;
return this;
}

/**
* The stringSupplier will ONLY be called on the client. It must have access to all the data it needs to build the
* text from the client side.
*/
public TextWidget setStringSupplier(Supplier<String> stringSupplier) {
if (stringSupplier != null) {
this.textSupplier = () -> new Text(stringSupplier.get());
this.isDynamic = true;
} else {
this.isDynamic = false;
}
return this;
}

public TextWidget setDefaultColor(int color) {
this.text.color(color);
return this;
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/gtnewhorizons/modularui/test/TestTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.gtnewhorizons.modularui.common.widget.CycleButtonWidget;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.DropDownWidget;
import com.gtnewhorizons.modularui.common.widget.DynamicTextWidget;
import com.gtnewhorizons.modularui.common.widget.ExpandTab;
import com.gtnewhorizons.modularui.common.widget.FluidSlotWidget;
import com.gtnewhorizons.modularui.common.widget.MultiChildWidget;
Expand Down Expand Up @@ -97,14 +98,7 @@ public int getSlotLimit(int slot) {
public ModularWindow createWindow(UIBuildContext buildContext) {
phantomInventory.setStackInSlot(1, new ItemStack(Items.diamond, Integer.MAX_VALUE));
ModularWindow.Builder builder = ModularWindow.builder(new Size(176, 272));
// .addFromJson("modularui:test", buildContext);
/*
* buildContext.applyToWidget("background", DrawableWidget.class, widget -> { widget.
* addTooltip("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum."
* ) .addTooltip("Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
* .addTooltip("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet"
* ); });
*/

List<Integer> nums = IntStream.range(1, 101).boxed().collect(Collectors.toList());
builder.setBackground(ModularUITextures.VANILLA_BACKGROUND).bindPlayerInventory(buildContext.getPlayer());
buildContext.addSyncedWindow(1, this::createAnotherWindow);
Expand Down Expand Up @@ -200,6 +194,20 @@ private Widget createPage1() {
.setSetter(val -> doubleValue = val)//
.setTextColor(Color.WHITE.dark(1)).setBackground(DISPLAY.withOffset(-2, -2, 4, 4))
.setSize(92, 20).setPos(100, 50))
.addChild(
new TextWidget("TextWidget: " + numberFormat.format(System.currentTimeMillis() % 100_000_000))
.setTextAlignment(Alignment.CenterLeft).setPos(0, 140))
.addChild(
new DynamicTextWidget(
() -> new Text(
"DynamicTextWidget: "
+ numberFormat.format(System.currentTimeMillis() % 100_000_000)))
.setTextAlignment(Alignment.CenterLeft).setPos(0, 150))
.addChild(
new TextWidget().setStringSupplier(
() -> "w/ Supplier: " + numberFormat.format(System.currentTimeMillis() % 100_000_000))
.setTextAlignment(Alignment.CenterLeft).setPos(0, 160))

.addChild(
SlotWidget.phantom(phantomInventory, 1).setShiftClickPriority(1).setIgnoreStackSizeLimit(true)
.setControlsAmount(true).setPos(28, 30))
Expand Down Expand Up @@ -231,6 +239,7 @@ private Widget createPage1() {
new DrawableWidget()
.setDrawable(new FluidDrawable().setFluid(FluidRegistry.LAVA).withFixedSize(32, 16))
.setPos(70, 100).setSize(32, 16))

.setPos(10, 10).setDebugLabel("Page1");
}

Expand Down

0 comments on commit 417a5a7

Please sign in to comment.