Skip to content

Commit

Permalink
fix #103
Browse files Browse the repository at this point in the history
brachy84 committed Jan 19, 2025
1 parent 3f7c5f4 commit 606032d
Showing 6 changed files with 115 additions and 28 deletions.
34 changes: 33 additions & 1 deletion src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java
Original file line number Diff line number Diff line change
@@ -31,6 +31,30 @@ public interface IKey extends IDrawable {
IKey LINE_FEED = str("\n");
IKey SPACE = str(" ");

// Formatting for convenience
TextFormatting BLACK = TextFormatting.BLACK;
TextFormatting DARK_BLUE = TextFormatting.DARK_BLUE;
TextFormatting DARK_GREEN = TextFormatting.DARK_GREEN;
TextFormatting DARK_AQUA = TextFormatting.DARK_AQUA;
TextFormatting DARK_RED = TextFormatting.DARK_RED;
TextFormatting DARK_PURPLE = TextFormatting.DARK_PURPLE;
TextFormatting GOLD = TextFormatting.GOLD;
TextFormatting GRAY = TextFormatting.GRAY;
TextFormatting DARK_GRAY = TextFormatting.DARK_GRAY;
TextFormatting BLUE = TextFormatting.BLUE;
TextFormatting GREEN = TextFormatting.GREEN;
TextFormatting AQUA = TextFormatting.AQUA;
TextFormatting RED = TextFormatting.RED;
TextFormatting LIGHT_PURPLE = TextFormatting.LIGHT_PURPLE;
TextFormatting YELLOW = TextFormatting.YELLOW;
TextFormatting WHITE = TextFormatting.WHITE;
TextFormatting OBFUSCATED = TextFormatting.OBFUSCATED;
TextFormatting BOLD = TextFormatting.BOLD;
TextFormatting STRIKETHROUGH = TextFormatting.STRIKETHROUGH;
TextFormatting UNDERLINE = TextFormatting.UNDERLINE;
TextFormatting ITALIC = TextFormatting.ITALIC;
TextFormatting RESET = TextFormatting.RESET;

/**
* Creates a translated text.
*
@@ -140,12 +164,20 @@ static IKey dynamic(@NotNull Supplier<@NotNull String> getter) {
String get();

/**
* @param parentFormatting formatting of the parent in case of composite keys
* @return the current formatted string
*/
default String getFormatted() {
default String getFormatted(@Nullable TextFormatting[] parentFormatting) {
return get();
}

/**
* @return the current formatted string
*/
default String getFormatted() {
return getFormatted(null);
}

@SideOnly(Side.CLIENT)
@Override
default void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
Original file line number Diff line number Diff line change
@@ -12,10 +12,8 @@ public abstract class BaseKey implements IKey {
private TextFormatting[] formatting;

@Override
public String getFormatted() {
if (this.formatting == null) return get();
if (FontRenderHelper.isReset(this.formatting)) return TextFormatting.RESET + get();
return FontRenderHelper.getFormatting(this.formatting, new StringBuilder()).append(get()).append(TextFormatting.RESET).toString();
public String getFormatted(@Nullable TextFormatting[] parentFormatting) {
return FontRenderHelper.format(this.formatting, parentFormatting, get());
}

@Override
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
package com.cleanroommc.modularui.drawable.text;

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

import net.minecraft.util.text.TextFormatting;

import org.jetbrains.annotations.Nullable;

public class CompoundKey extends BaseKey {

private static final IKey[] EMPTY = new IKey[0];

private final IKey[] keys;
private String string;
private long time = 0;

public CompoundKey(IKey... keys) {
this.keys = keys == null || keys.length == 0 ? EMPTY : keys;
}

@Override
public String get() {
if (ClientScreenHandler.getTicks() != this.time) {
this.time = ClientScreenHandler.getTicks();
StringBuilder builder = new StringBuilder();
for (IKey key : this.keys) {
return toString(false, null);
}

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

private String toString(boolean formatted, TextFormatting @Nullable [] parentFormatting) {
StringBuilder builder = new StringBuilder();
for (IKey key : this.keys) {
if (formatted) {
// merge parent formatting and this formatting to no lose info
builder.append(key.getFormatted(FontRenderHelper.mergeState(parentFormatting, getFormatting())));
} else {
builder.append(key.get());
}
this.string = builder.toString();
}
return this.string;
return builder.toString();
}

public IKey[] getKeys() {
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import net.minecraft.util.text.TextFormatting;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
@@ -70,18 +71,55 @@ public static void parseFormattingState(TextFormatting[] state, String text) {

public static String getFormatting(TextFormatting[] state) {
if (isReset(state)) return TextFormatting.RESET.toString();
StringBuilder builder = getFormatting(state, new StringBuilder());
StringBuilder builder = appendFormatting(state, new StringBuilder());
return builder.length() == 0 ? StringUtils.EMPTY : builder.toString();
}

public static StringBuilder getFormatting(TextFormatting[] state, StringBuilder builder) {
public static StringBuilder appendFormatting(TextFormatting[] state, StringBuilder builder) {
return appendFormatting(state, null, builder);
}

public static StringBuilder appendFormatting(TextFormatting[] state, TextFormatting @Nullable [] fallback, StringBuilder builder) {
for (int i = 0, n = 6; i < n; i++) {
TextFormatting formatting = state[i];
if (formatting != null) builder.append(formatting);
if (state[i] != null) {
builder.append(state[i]);
} else if (fallback != null && fallback[i] != null) {
builder.append(fallback[i]);
}
}
return builder;
}

public static String format(@Nullable TextFormatting[] state, @Nullable TextFormatting[] parentState, String text) {
if (state == null) {
if (parentState == null) return text;
return appendFormatting(parentState, new StringBuilder().append(TextFormatting.RESET)).append(text).toString();
}
StringBuilder s = appendFormatting(state, parentState, new StringBuilder().append(TextFormatting.RESET))
.append(text);
return s.toString();
}

public static TextFormatting @NotNull [] mergeState(TextFormatting @Nullable [] state1, TextFormatting @Nullable [] state2) {
return mergeState(state1, state2, null);
}

public static TextFormatting @NotNull [] mergeState(TextFormatting @Nullable [] state1, TextFormatting @Nullable [] state2, TextFormatting @Nullable [] result) {
if (state1 == null) {
if (state2 == null) return createFormattingState();
return state2;
} else if (state2 == null) {
return state1;
}
if (isReset(state2)) return state2; // state2 has higher priority
if (result == null) result = Arrays.copyOf(state1, state1.length);
for (int i = 0, n = 6; i < n; i++) {
TextFormatting formatting = state2[i];
if (formatting != null) addAfter(result, formatting);
}
return result;
}

public static boolean isReset(TextFormatting[] state) {
return state[6] != null;
}
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ public static void onItemUse(PlayerInteractEvent.RightClickItem event) {
.screenScale(0.5f)
.open(new TestGui());*/
//ClientGUI.open(new ResizerTest());
ClientGUI.open(new TestGui());
ClientGUI.open(new TestGuis());
}
}
}
Original file line number Diff line number Diff line change
@@ -14,27 +14,24 @@
import com.cleanroommc.modularui.widget.DraggableWidget;
import com.cleanroommc.modularui.widgets.RichTextWidget;
import com.cleanroommc.modularui.widgets.SchemaWidget;

import com.cleanroommc.modularui.widgets.SortableListWidget;

import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.TextFormatting;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class ResizerTest extends CustomModularScreen {
public class TestGuis extends CustomModularScreen {

@Override
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
return buildSortableListUI(context);
return buildRichTextUI(context);
}

public @NotNull ModularPanel buildSpriteUI(ModularGuiContext context) {
@@ -81,15 +78,25 @@ public class ResizerTest extends CustomModularScreen {
return true;
}))
.add(" you. ")
.add(TextFormatting.GREEN + "This is a long ")
.add(IKey.str("string").format(TextFormatting.DARK_PURPLE)
.add(IKey.GREEN + "This is a long ")
.add(IKey.str("string").format(IKey.DARK_PURPLE)
.asTextIcon()
.asHoverable()
.addTooltipLine("Text Tooltip"))
.add(" of characters" + TextFormatting.RESET)
.add(" of characters" + IKey.RESET)
.add(" and not numbers as some might think...")
.newLine()
.add("")
.newLine()
.add(IKey.comp(IKey.comp(
IKey.str("Underline all: "),
IKey.comp(
IKey.str("Green Text, "),
IKey.str("this is red").format(IKey.RED),
IKey.str(" and this should be green again"))
.format(IKey.GREEN),
IKey.str(". Still underlined, "))
.format(IKey.UNDERLINE), IKey.str("but not anymore.")))
.newLine()
.textShadow(false)
));
}

0 comments on commit 606032d

Please sign in to comment.