Skip to content

Commit 16f8f04

Browse files
committed
feat: add structured texts
1 parent 59f79ba commit 16f8f04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+674
-229
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
plugins {
22
id("org.jetbrains.kotlin.jvm")
33
id("org.jetbrains.kotlin.plugin.lombok")
4+
}
5+
6+
dependencies {
7+
annotationProcessor(Dependencies.LOMBOK)
8+
compileOnly(Dependencies.LOMBOK)
49
}

common/src/main/java/io/github/notenoughupdates/moulconfig/Config.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package io.github.notenoughupdates.moulconfig;
2222

23+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText;
2324
import io.github.notenoughupdates.moulconfig.gui.HorizontalAlign;
2425
import io.github.notenoughupdates.moulconfig.processor.ProcessedCategory;
2526
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
@@ -36,22 +37,22 @@ public HorizontalAlign alignCategory(ProcessedCategory category, boolean isSelec
3637
return HorizontalAlign.CENTER;
3738
}
3839

39-
public String formatCategoryName(ProcessedCategory category, boolean isSelected) {
40+
public StructuredText formatCategoryName(ProcessedCategory category, boolean isSelected) {
4041
if (isSelected) {
41-
return "§b§n" + category.getDisplayName();
42+
return category.getDisplayName().underlined().aqua();
4243
} else if (category.getParentCategoryId() == null) {
43-
return "§7" + category.getDisplayName();
44+
return category.getDisplayName().grey();
4445
} else {
45-
return "§8" + category.getDisplayName();
46+
return category.getDisplayName().darkGrey();
4647
}
4748
}
4849

4950
public List<Social> getSocials() {
5051
return new ArrayList<>();
5152
}
5253

53-
public String getTitle() {
54-
return "Config GUI";
54+
public StructuredText getTitle() {
55+
return StructuredText.of("Config GUI");
5556
}
5657

5758
public void saveNow() {

common/src/main/java/io/github/notenoughupdates/moulconfig/Social.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@
2323
import io.github.notenoughupdates.moulconfig.common.ClickType;
2424
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
2525
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation;
26+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText;
2627

2728
import java.awt.*;
2829
import java.net.URI;
2930
import java.net.URISyntaxException;
3031
import java.util.Arrays;
32+
import java.util.Collections;
3133
import java.util.List;
3234

3335
public abstract class Social {
3436

35-
public static Social forLink(String name, MyResourceLocation icon, String link) {
37+
public static Social forLink(StructuredText name, MyResourceLocation icon, String link) {
3638
try {
3739
return new URLSocial(name, new URI(link), icon);
3840
} catch (URISyntaxException e) {
@@ -42,16 +44,16 @@ public static Social forLink(String name, MyResourceLocation icon, String link)
4244

4345
public abstract void onClick();
4446

45-
public abstract List<String> getTooltip();
47+
public abstract List<StructuredText> getTooltip();
4648

4749
public abstract MyResourceLocation getIcon();
4850

4951
private static class URLSocial extends Social {
50-
private final String name;
52+
private final StructuredText name;
5153
private final URI url;
5254
private final MyResourceLocation icon;
5355

54-
private URLSocial(String name, URI url, MyResourceLocation icon) {
56+
private URLSocial(StructuredText name, URI url, MyResourceLocation icon) {
5557
this.name = name;
5658
this.url = url;
5759
this.icon = icon;
@@ -62,13 +64,13 @@ public void onClick() {
6264
try {
6365
Desktop.getDesktop().browse(url);
6466
} catch (Exception e) {
65-
IMinecraft.instance.sendClickableChatMessage("Click here to open " + name, url.toString(), ClickType.OPEN_LINK);
67+
IMinecraft.instance.sendClickableChatMessage(StructuredText.of("Click here to open ").append(name), url.toString(), ClickType.OPEN_LINK);
6668
}
6769
}
6870

6971
@Override
70-
public List<String> getTooltip() {
71-
return Arrays.asList(name);
72+
public List<StructuredText> getTooltip() {
73+
return Collections.singletonList(name);
7274
}
7375

7476
@Override
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package io.github.notenoughupdates.moulconfig.common
22

3+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
4+
35
/**
46
* Not for manual implementation. This should be implemented by the corresponding platform.
57
*/
68
interface IFontRenderer {
79
val height: Int
8-
fun getStringWidth(string: String): Int
10+
fun getStringWidth(string: StructuredText): Int
11+
fun getStringWidth(string: String): Int = getStringWidth(StructuredText.of(string))
912
fun getCharWidth(char: Char): Int
10-
fun splitText(text: String, width: Int): List<String>
11-
fun trimStringToWidth(string: String, maxWidth: Int, reverse: Boolean): String
12-
fun trimStringToWidth(string: String, maxWidth: Int): String {
13-
return trimStringToWidth(string, maxWidth, false)
14-
}
13+
fun splitText(text: StructuredText, width: Int): List<StructuredText>
14+
fun splitLines(text: StructuredText): List<StructuredText>
15+
16+
fun trimStringToWidth(string: String, width: Int) = trimStringToWidth(string, width, false)
17+
fun trimStringToWidth(string: String, width: Int, reverse: Boolean): String
18+
1519
}

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IItemStack.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.notenoughupdates.moulconfig.common
22

3+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
34
import org.jetbrains.annotations.ApiStatus
45

56
/**
@@ -9,8 +10,8 @@ import org.jetbrains.annotations.ApiStatus
910
*/
1011
@ApiStatus.NonExtendable
1112
interface IItemStack {
12-
fun getLore(): List<String>
13-
fun getDisplayName(): String
13+
fun getLore(): List<StructuredText>
14+
fun getDisplayName(): StructuredText
1415

1516
fun getStackSize(): Int
1617
fun getItemId(): MyResourceLocation

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IMinecraft.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.notenoughupdates.moulconfig.common
22

3+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText
34
import io.github.notenoughupdates.moulconfig.gui.GuiComponent
45
import io.github.notenoughupdates.moulconfig.gui.GuiContext
56
import io.github.notenoughupdates.moulconfig.gui.GuiElement
@@ -44,9 +45,18 @@ interface IMinecraft {
4445

4546
fun addExtraBuiltinConfigProcessors(processor: MoulConfigProcessor<*>)
4647

47-
fun sendClickableChatMessage(message: String, action: String, type: ClickType)
48+
fun sendClickableChatMessage(message: StructuredText, action: String, type: ClickType) // TODO: should this be in the structured text?
4849

49-
fun getKeyName(keyCode: Int): String
50+
fun getKeyName(keyCode: Int): StructuredText
51+
52+
fun createLiteral(text: String): StructuredText
53+
fun createTranslatable(key: String, vararg args: StructuredText): StructuredText
54+
55+
/**
56+
* Create a structured text from an untyped platform object. Must be a platform type exactly, not a string or a structured text.
57+
*/
58+
@ApiStatus.Experimental
59+
fun createStructuredTextInternal(obj: Any): StructuredText?
5060

5161
/**
5262
* This is a method to provide a render context. Note that constructing this context directly will potentially give

common/src/main/java/io/github/notenoughupdates/moulconfig/common/RenderContext.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.notenoughupdates.moulconfig.common;
22

3+
import io.github.notenoughupdates.moulconfig.common.text.StructuredText;
34
import io.github.notenoughupdates.moulconfig.internal.NinePatchRenderer;
45
import juuxel.libninepatch.NinePatch;
56
import org.jetbrains.annotations.ApiStatus;
@@ -63,7 +64,7 @@ default boolean isLogicalCtrlDown() {
6364
}
6465
}
6566

66-
default void drawStringScaledMaxWidth(@NotNull String text, @NotNull IFontRenderer fontRenderer, int x, int y, boolean shadow, int width, int color) {
67+
default void drawStringScaledMaxWidth(@NotNull StructuredText text, @NotNull IFontRenderer fontRenderer, int x, int y, boolean shadow, int width, int color) {
6768
pushMatrix();
6869
translate(x, y);
6970
float scale = Math.min(1F, Math.max(0.1F, width / (float) fontRenderer.getStringWidth(text)));
@@ -73,7 +74,7 @@ default void drawStringScaledMaxWidth(@NotNull String text, @NotNull IFontRender
7374
}
7475

7576
default void drawStringCenteredScaledMaxWidth(
76-
@NotNull String text,
77+
@NotNull StructuredText text,
7778
@NotNull IFontRenderer fr,
7879
float x, float y,
7980
boolean shadow,
@@ -126,7 +127,7 @@ default void drawOpenCloseTriangle(boolean isOpen, float x, float y, float width
126127
}
127128
}
128129

129-
void drawString(@NotNull IFontRenderer fontRenderer, @NotNull String text, int x, int y, int color, boolean shadow);
130+
void drawString(@NotNull IFontRenderer fontRenderer, @NotNull StructuredText text, int x, int y, int color, boolean shadow);
130131

131132
void drawColoredRect(float left, float top, float right, float bottom, int color);
132133

@@ -222,11 +223,11 @@ default void drawDarkRect(int x, int y, int width, int height) {
222223
@Deprecated
223224
void clearScissor(); // TODO: this sort of escapes out of the current context.
224225

225-
void renderItemStack(@NotNull IItemStack itemStack, int x, int y, @Nullable String overlayText);
226+
void renderItemStack(@NotNull IItemStack itemStack, int x, int y, @Nullable StructuredText overlayText);
226227

227-
void drawTooltipNow(int x, int y, @NotNull List<@NotNull String> tooltipLines);
228+
void drawTooltipNow(int x, int y, @NotNull List<@NotNull StructuredText> tooltipLines);
228229

229-
default void scheduleDrawTooltip(int x, int y, @NotNull List<String> tooltipLines) {
230+
default void scheduleDrawTooltip(int x, int y, @NotNull List<StructuredText> tooltipLines) {
230231
// TODO: should this do some form of conflict resolution?
231232
drawOnTop(Layer.TOOLTIP, ScissorBehaviour.ESCAPE, it -> it.drawTooltipNow(x, y, tooltipLines));
232233
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.github.notenoughupdates.moulconfig.common.text;
2+
3+
import io.github.notenoughupdates.moulconfig.internal.ColourUtil;
4+
import io.github.notenoughupdates.moulconfig.internal.MathUtil;
5+
import lombok.Getter;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Unmodifiable;
8+
9+
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.List;
12+
13+
/**
14+
* A list of (named) colours available on most backends
15+
*/
16+
public enum DefaultFormattingColour {
17+
BLACK(0x000000),
18+
DARK_BLUE(0x0000aa),
19+
DARK_GREEN(0x00aa00),
20+
DARK_AQUA(0x00aaaa),
21+
DARK_RED(0xaa0000),
22+
DARK_PURPLE(0xaa00aa),
23+
GOLD(0xffaa00),
24+
GREY(0xaaaaaa),
25+
DARK_GREY(0x555555),
26+
BLUE(0x5555ff),
27+
GREEN(0x55ff55),
28+
AQUA(0x55ffff),
29+
RED(0xff5555),
30+
LIGHT_PURPLE(0xff55ff),
31+
YELLOW(0xffff55),
32+
WHITE(0xffffff),
33+
;
34+
35+
@Getter
36+
final int rgb;
37+
38+
DefaultFormattingColour(int rgb) {
39+
this.rgb = rgb;
40+
}
41+
42+
public static @NotNull DefaultFormattingColour estimate(int rgb) {
43+
int r = ColourUtil.unpackARGBRedI(rgb);
44+
int g = ColourUtil.unpackARGBGreenI(rgb);
45+
int b = ColourUtil.unpackARGBBlueI(rgb);
46+
int minDistance = Integer.MAX_VALUE;
47+
DefaultFormattingColour best = null;
48+
for (DefaultFormattingColour candidate : ALL_COLOURS) {
49+
int crgb = candidate.getRgb();
50+
int cr = ColourUtil.unpackARGBRedI(crgb);
51+
int cg = ColourUtil.unpackARGBGreenI(crgb);
52+
int cb = ColourUtil.unpackARGBBlueI(crgb);
53+
int sqDist =
54+
MathUtil.squaredDistance(cr, r)
55+
+ MathUtil.squaredDistance(cg, g)
56+
+ MathUtil.squaredDistance(cb, b);
57+
if (sqDist < minDistance)
58+
best = candidate;
59+
}
60+
assert best != null;
61+
return best;
62+
}
63+
64+
public static final @Unmodifiable
65+
@NotNull List<@NotNull DefaultFormattingColour> ALL_COLOURS
66+
= Collections.unmodifiableList(Arrays.asList(values()));
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.notenoughupdates.moulconfig.common.text;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public interface StructuredStyle { // TODO: make this an interface
6+
default @NotNull StructuredStyle withColour(@NotNull DefaultFormattingColour colour) {
7+
return withColour(colour.getRgb());
8+
}
9+
10+
@NotNull StructuredStyle withColour(int rgb);
11+
12+
@NotNull StructuredStyle withBold(boolean bold);
13+
14+
@NotNull StructuredStyle withItalic(boolean italic);
15+
16+
@NotNull StructuredStyle withUnderline(boolean underline);
17+
18+
@NotNull StructuredStyle withStrikethrough(boolean strikethrough);
19+
20+
@NotNull StructuredStyle withObfuscated(boolean obfuscated);
21+
}

0 commit comments

Comments
 (0)