Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ static IKey comp(@NotNull IKey... keys) {
* @return dynamic text key
*/
static IKey dynamic(@NotNull Supplier<@NotNull String> getter) {
return dynamicKey(() -> IKey.str(getter.get()));
}

/**
* Creates a dynamic text key.
*
* @param getter key supplier
* @return dynamic text key
*/
static IKey dynamicKey(@NotNull Supplier<@NotNull IKey> getter) {
return new DynamicKey(getter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package com.cleanroommc.modularui.drawable.text;

import com.cleanroommc.modularui.api.drawable.IKey;

import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.function.Supplier;

public class DynamicKey extends BaseKey {

private final Supplier<String> supplier;
private final Supplier<IKey> supplier;

public DynamicKey(Supplier<String> supplier) {
Objects.requireNonNull(supplier.get(), "IKey returns a null string!");
public DynamicKey(Supplier<IKey> supplier) {
Objects.requireNonNull(supplier.get(), "IKey returns a null key!");
this.supplier = supplier;
}

@Override
public String get() {
return this.supplier.get();
return toString(false, null);
}

@Override
public String getFormatted(@Nullable FormattingState parentFormatting) {
// formatting is prepended to each key
return toString(true, parentFormatting);
}

private String toString(boolean formatted, @Nullable FormattingState parentFormatting) {
IKey key = this.supplier.get();
if (formatted) {
// merge parent formatting and this formatting to no lose info
return key.getFormatted(FormattingState.merge(parentFormatting, getFormatting()));
} else {
return key.get();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/cleanroommc/modularui/test/TestGuis.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.cleanroommc.modularui.utils.fakeworld.FakeEntity;
import com.cleanroommc.modularui.utils.fakeworld.ISchema;
import com.cleanroommc.modularui.value.BoolValue;
import com.cleanroommc.modularui.value.IntValue;
import com.cleanroommc.modularui.value.StringValue;
import com.cleanroommc.modularui.widget.DraggableWidget;
import com.cleanroommc.modularui.widget.Widget;
Expand Down Expand Up @@ -277,6 +278,7 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
}

public static @NotNull ModularPanel buildRichTextUI() {
IntValue integer = new IntValue(0);
return new ModularPanel("main")
.size(176, 166)
.child(new RichTextWidget()
Expand Down Expand Up @@ -324,6 +326,14 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
.add(TextFormatting.RESET + "" + TextFormatting.UNDERLINE + "Underlined" + TextFormatting.RESET)
.newLine()
.add("A long line which should wrap around")
.newLine()
.addLine(IKey.comp(IKey.str("Dynamic ").style(IKey.GOLD), IKey.dynamicKey(() -> {
int i = integer.getIntValue() + 1;
integer.setIntValue(i);
return IKey.str("key [%s]", IKey.str("arg")
.style(IKey.UNDERLINE, IKey.BLACK))
.style(i % 30 > 5 ? IKey.RED : IKey.DARK_BLUE);
}).style(IKey.BOLD), IKey.str(" Test")))
.textShadow(false)
));
}
Expand Down