Skip to content

Commit daf83c9

Browse files
committed
port to rich text
1 parent 36a9249 commit daf83c9

File tree

4 files changed

+96
-93
lines changed

4 files changed

+96
-93
lines changed

src/main/java/gregtech/api/metatileentity/multiblock/ui/MultiblockUIFactory.java

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import com.cleanroommc.modularui.api.drawable.IDrawable;
2222
import com.cleanroommc.modularui.api.drawable.IKey;
23+
import com.cleanroommc.modularui.api.drawable.IRichTextBuilder;
2324
import com.cleanroommc.modularui.api.widget.IWidget;
24-
import com.cleanroommc.modularui.drawable.text.DynamicKey;
25+
import com.cleanroommc.modularui.drawable.text.RichText;
2526
import com.cleanroommc.modularui.factory.PosGuiData;
2627
import com.cleanroommc.modularui.screen.ModularPanel;
28+
import com.cleanroommc.modularui.screen.RichTooltip;
2729
import com.cleanroommc.modularui.utils.Alignment;
2830
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
2931
import com.cleanroommc.modularui.value.sync.IntSyncValue;
@@ -35,24 +37,20 @@
3537
import com.cleanroommc.modularui.widget.scroll.VerticalScrollData;
3638
import com.cleanroommc.modularui.widgets.CycleButtonWidget;
3739
import com.cleanroommc.modularui.widgets.ProgressWidget;
40+
import com.cleanroommc.modularui.widgets.RichTextWidget;
3841
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
39-
import com.cleanroommc.modularui.widgets.layout.Column;
4042
import com.cleanroommc.modularui.widgets.layout.Flow;
41-
import com.cleanroommc.modularui.widgets.layout.Row;
42-
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
43-
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
4443
import org.jetbrains.annotations.NotNull;
4544
import org.jetbrains.annotations.Nullable;
4645

4746
import java.io.IOException;
4847
import java.util.ArrayList;
49-
import java.util.Collections;
5048
import java.util.List;
49+
import java.util.Objects;
5150
import java.util.function.BiFunction;
5251
import java.util.function.BooleanSupplier;
5352
import java.util.function.Consumer;
5453
import java.util.function.DoubleSupplier;
55-
import java.util.function.Function;
5654
import java.util.function.IntSupplier;
5755
import java.util.function.Supplier;
5856

@@ -144,7 +142,7 @@ private Widget<?> createIndicator() {
144142
.pos(174 - 5, screenHeight - 18 - 3)
145143
.onUpdateListener(w -> w.overlay(getIndicatorOverlay(builder)))
146144
.tooltip(tooltip -> tooltip.setAutoUpdate(true))
147-
.tooltipBuilder(tooltip -> tooltip.addDrawableLines(builder.getTextList()));
145+
.tooltipBuilder(builder::buildTooltip);
148146
}
149147

150148
private IDrawable getIndicatorOverlay(Builder builder) {
@@ -308,27 +306,29 @@ public MultiblockUIFactory customScreen(Supplier<ParentWidget<?>> customScreen)
308306
protected Widget<?> createScreen(PanelSyncManager syncManager) {
309307
final var builder = builder();
310308
this.displayText.accept(builder);
311-
var col = new Column();
312-
builder.build(col);
309+
var richTextWidget = new RichTextWidget();
310+
builder.buildDisplay(richTextWidget);
313311
final var compare = builder();
314312

315313
return new ParentWidget<>()
316314
.child(createIndicator())
317315
.child(customScreen != null ? customScreen.get() : new ScrollWidget<>(new VerticalScrollData())
318316
.sizeRel(1f)
319-
.child(col.expanded()
317+
.child(richTextWidget.sizeRel(1f)
318+
.alignment(Alignment.TopLeft)
320319
.margin(4, 4)
321320
.onUpdateListener(column -> {
322321
// really debating on if the display screen should be its own widget
323-
compare.clear();
322+
// compare.clear();
324323
this.displayText.accept(compare);
325-
if (!builder.hasChanged(compare) && !dirty) return;
326-
builder.clear();
327-
column.getChildren().clear();
328-
this.displayText.accept(builder);
329-
builder.build(column);
330-
resize(column);
331-
dirty = false;
324+
// if (!builder.hasChanged(compare) && !dirty) return;
325+
// builder.clear();
326+
if (builder.hasChanged(compare))
327+
column.markDirty();
328+
// this.displayText.accept(builder);
329+
// builder.build(column);
330+
// resize(column);
331+
// dirty = false;
332332
})))
333333
.background(GTGuiTextures.DISPLAY)
334334
.size(190, screenHeight)
@@ -465,9 +465,11 @@ protected static Builder builder() {
465465
@SuppressWarnings({ "UnusedReturnValue", "unused" })
466466
public static class Builder {
467467

468-
private final List<IDrawable> textList;
469-
private Function<IDrawable, Widget<?>> widgetFunction = Builder::keyMapper;
470-
private final Int2ObjectMap<IDrawable> tooltips = new Int2ObjectArrayMap<>();
468+
private final RichText text = new RichText();
469+
private final List<Consumer<IRichTextBuilder<? extends IRichTextBuilder<?>>>> textList = new ArrayList<>();
470+
// private final List<IDrawable> textList;
471+
// private Function<IDrawable, Widget<?>> widgetFunction = Builder::keyMapper;
472+
// private final Int2ObjectMap<IDrawable> tooltips = new Int2ObjectArrayMap<>();
471473

472474
private BooleanSupplier isWorkingEnabled = () -> false;
473475
private BooleanSupplier isActive = () -> false;
@@ -479,15 +481,15 @@ public static class Builder {
479481
private IKey runningKey = IKey.lang("gregtech.multiblock.running").format(TextFormatting.GREEN);
480482
private boolean dirty;
481483

482-
protected static Widget<?> keyMapper(IDrawable key) {
483-
return key.asWidget()
484-
.widthRel(1f)
485-
.height(12);
486-
}
484+
// protected static Widget<?> keyMapper(IDrawable key) {
485+
// return key.asWidget()
486+
// .widthRel(1f)
487+
// .height(12);
488+
// }
487489

488-
private Builder() {
489-
this.textList = new ArrayList<>();
490-
}
490+
// private Builder() {
491+
// this.textList = new ArrayList<>();
492+
// }
491493

492494
public Builder structureFormed(boolean structureFormed) {
493495
this.isStructureFormed = structureFormed;
@@ -672,11 +674,11 @@ public Builder addWorkingStatusLine() {
672674

673675
addKey(KeyUtil.string(() -> {
674676
if (!isWorkingEnabled.getAsBoolean()) {
675-
return TextFormatting.GOLD + pausedKey.get();
677+
return pausedKey.getFormatted();
676678
} else if (isActive.getAsBoolean()) {
677-
return TextFormatting.GREEN + runningKey.get();
679+
return runningKey.getFormatted();
678680
} else {
679-
return TextFormatting.GRAY + idlingKey.get();
681+
return idlingKey.getFormatted();
680682
}
681683
}));
682684
return this;
@@ -881,23 +883,13 @@ public Builder addFuelNeededLine(String fuelName, IntSupplier previousRecipeDura
881883

882884
/** Insert an empty line into the text list. */
883885
public Builder addEmptyLine() {
884-
addKey(IKey.EMPTY); // this is going to cause problems maybe
886+
this.text.newLine();
885887
return this;
886888
}
887889

888890
/** Add custom text dynamically, allowing for custom application logic. */
889-
public Builder addCustom(Consumer<List<IDrawable>> customConsumer) {
890-
List<IDrawable> customKeys = new ArrayList<>();
891-
customConsumer.accept(customKeys);
892-
customKeys.forEach(this::addKey);
893-
return this;
894-
}
895-
896-
/**
897-
* @param widgetFunction function to build widgets from keys
898-
*/
899-
public Builder widgetFunction(Function<IDrawable, Widget<?>> widgetFunction) {
900-
this.widgetFunction = widgetFunction;
891+
public Builder addCustom(Consumer<RichText> customConsumer) {
892+
customConsumer.accept(this.text);
901893
return this;
902894
}
903895

@@ -909,41 +901,35 @@ protected void clear() {
909901
textList.clear();
910902
}
911903

912-
protected void build(ParentWidget<?> parent) {
913-
for (int i = 0; i < textList.size(); i++) {
914-
var line = this.widgetFunction.apply(textList.get(i));
915-
if (tooltips.containsKey(i))
916-
line.addTooltipLine(tooltips.get(i));
917-
parent.child(line);
918-
}
904+
protected void buildDisplay(RichTextWidget parent) {
905+
parent.textBuilder(richText -> this.textList.forEach(t -> t.accept(richText)));
919906
}
920907

921-
protected List<IDrawable> getTextList() {
922-
return Collections.unmodifiableList(textList);
908+
protected void buildTooltip(RichTooltip tooltip) {
909+
this.textList.forEach(t -> t.accept(tooltip));
923910
}
924911

925-
protected boolean hasChanged(Builder other) {
926-
if (textList.size() != other.textList.size()) return true;
927-
for (int i = 0; i < textList.size(); i++) {
928-
IDrawable left = textList.get(i), right = other.textList.get(i);
929-
930-
// dynamic keys are impossible to check, skip
931-
if (left instanceof DynamicKey && right instanceof DynamicKey)
932-
continue;
912+
protected RichText getTextList() {
913+
return this.text;
914+
}
933915

934-
if (!left.equals(right))
916+
protected boolean hasChanged(Builder other) {
917+
List<String> cur = text.getStringRepresentation();
918+
List<String> oth = other.text.getStringRepresentation();
919+
if (cur.size() != oth.size()) return true;
920+
for (int i = 0; i < cur.size(); i++) {
921+
if (!Objects.equals(cur.get(i), oth.get(i)))
935922
return true;
936923
}
937924
return false;
938925
}
939926

940927
private void addKey(IDrawable key) {
941-
this.textList.add(key);
928+
textList.add(richText -> richText.addLine(key));
942929
}
943930

944-
private void addKey(IDrawable key, IDrawable hover) {
945-
this.tooltips.put(textList.size(), hover);
946-
addKey(key);
931+
private void addKey(IKey key, IDrawable hover) {
932+
addKey(key.asTextIcon().asHoverable().addTooltipLine(hover));
947933
}
948934
}
949935
}

src/main/java/gregtech/common/metatileentities/multi/MetaTileEntityLargeBoiler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
import codechicken.lib.render.pipeline.IVertexOperation;
4040
import codechicken.lib.vec.Matrix4;
4141
import com.cleanroommc.modularui.api.GuiAxis;
42-
import com.cleanroommc.modularui.api.drawable.IDrawable;
4342
import com.cleanroommc.modularui.api.drawable.IKey;
4443
import com.cleanroommc.modularui.api.widget.Interactable;
4544
import com.cleanroommc.modularui.drawable.ItemDrawable;
4645
import com.cleanroommc.modularui.drawable.Rectangle;
46+
import com.cleanroommc.modularui.drawable.text.RichText;
4747
import com.cleanroommc.modularui.screen.ModularPanel;
4848
import com.cleanroommc.modularui.utils.Color;
4949
import com.cleanroommc.modularui.value.sync.BooleanSyncValue;
@@ -219,27 +219,27 @@ protected MultiblockUIFactory createUIFactory() {
219219
});
220220
}
221221

222-
private void addCustomData(List<IDrawable> keyList) {
222+
private void addCustomData(RichText keyList) {
223223
if (isStructureFormed()) {
224224
// Steam Output line
225225
IKey steamOutput = KeyUtil.number(TextFormatting.AQUA,
226226
recipeLogic::getLastTickSteam, " L/t");
227227

228-
keyList.add(KeyUtil.lang(TextFormatting.GRAY,
228+
keyList.addLine(KeyUtil.lang(TextFormatting.GRAY,
229229
"gregtech.multiblock.large_boiler.steam_output", steamOutput));
230230

231231
// Efficiency line
232232
IKey efficiency = KeyUtil.number(
233233
() -> getNumberColor(recipeLogic.getHeatScaled()),
234234
recipeLogic::getHeatScaled, "%");
235-
keyList.add(KeyUtil.lang(TextFormatting.GRAY,
235+
keyList.addLine(KeyUtil.lang(TextFormatting.GRAY,
236236
"gregtech.multiblock.large_boiler.efficiency", efficiency));
237237

238238
// Throttle line
239239
IKey throttle = KeyUtil.number(
240240
() -> getNumberColor(getThrottle()),
241241
this::getThrottle, "%");
242-
keyList.add(KeyUtil.lang(TextFormatting.GRAY,
242+
keyList.addLine(KeyUtil.lang(TextFormatting.GRAY,
243243
"gregtech.multiblock.large_boiler.throttle", throttle));
244244
}
245245
}

src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityElectricBlastFurnace.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import net.minecraftforge.fml.relauncher.Side;
4848
import net.minecraftforge.fml.relauncher.SideOnly;
4949

50-
import com.cleanroommc.modularui.api.drawable.IDrawable;
50+
import com.cleanroommc.modularui.drawable.text.RichText;
5151
import com.cleanroommc.modularui.value.sync.DoubleSyncValue;
5252
import com.cleanroommc.modularui.value.sync.IntSyncValue;
5353
import org.jetbrains.annotations.NotNull;
@@ -119,13 +119,13 @@ protected MultiblockUIFactory createUIFactory() {
119119
.addProgressLine(progress::getDoubleValue));
120120
}
121121

122-
private Consumer<List<IDrawable>> addHeatCapacity(IntSyncValue temp) {
122+
private Consumer<RichText> addHeatCapacity(IntSyncValue temp) {
123123
return keyList -> {
124124
if (isStructureFormed()) {
125125
var heatString = KeyUtil.number(TextFormatting.RED,
126126
temp::getIntValue, "K");
127127

128-
keyList.add(KeyUtil.lang(TextFormatting.GRAY,
128+
keyList.addLine(KeyUtil.lang(TextFormatting.GRAY,
129129
"gregtech.multiblock.blast_furnace.max_temperature", heatString));
130130
}
131131
};

src/main/java/gregtech/common/metatileentities/multi/electric/generator/MetaTileEntityLargeTurbine.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import gregtech.api.metatileentity.multiblock.ui.MultiblockUIFactory;
1212
import gregtech.api.mui.GTGuiTextures;
1313
import gregtech.api.mui.sync.FixedIntArraySyncValue;
14-
import gregtech.api.mui.widget.GregtechDisplayScreen;
1514
import gregtech.api.pattern.BlockPattern;
1615
import gregtech.api.pattern.FactoryBlockPattern;
1716
import gregtech.api.pattern.PatternMatchContext;
@@ -208,22 +207,40 @@ protected MultiblockUIFactory createUIFactory() {
208207
.syncValue("dura", rotorFree)
209208
.syncValue("fuel_amount", fuelAmount)
210209
.syncValue("prev_duration", prevDuration)
211-
.customScreen(() -> new GregtechDisplayScreen(this)
212-
.padding(4)
213-
.energy(this::getMaxVoltage, recipeLogic::getRecipeEUt)
214-
.addLine(buffer -> {
215-
buffer.writeBoolean(isStructureFormed());
216-
if (isStructureFormed())
217-
buffer.writeInt(noRotor ? -1 : getRotorHolder().getTotalEfficiency());
218-
}, buffer -> {
219-
if (!buffer.readBoolean()) return null;
220-
int i = buffer.readInt();
221-
if (i < 0) return null;
222-
return KeyUtil.lang(TextFormatting.GRAY,
223-
"gregtech.multiblock.turbine.efficiency", i);
224-
})
225-
.fuelNeeded(recipeLogic::getRecipeFluidInputInfo, recipeLogic::getPreviousRecipeDuration)
226-
.status())
210+
// .customScreen(() -> new ScrollWidget<>(new VerticalScrollData())
211+
// .padding(4).sizeRel(1f)
212+
// .child(new RichTextWidget()
213+
// .sizeRel(1f)
214+
// .autoUpdate(true)
215+
// .alignment(Alignment.TopLeft)
216+
// .textBuilder(richText -> {
217+
// richText.add(KeyUtil.lang(TextFormatting.WHITE, getMetaFullName())).newLine();
218+
//
219+
// if (!isStructureFormed())
220+
// richText.add(KeyUtil.lang(TextFormatting.RED, "gregtech.multiblock.invalid_structure")).newLine();
221+
//
222+
// long v = getMaxVoltage();
223+
// String voltageName = GTValues.VOCNF[GTUtility.getFloorTierByVoltage(v)];
224+
// richText.add(KeyUtil.lang(TextFormatting.GRAY, "gregtech.multiblock.max_energy_per_tick",
225+
// TextFormattingUtil.formatNumbers(v), IKey.str(voltageName).format(TextFormatting.RESET))).newLine();
226+
// }))
227+
// new GregtechDisplayScreen(this)
228+
// .padding(4)
229+
// .energy(this::getMaxVoltage, recipeLogic::getRecipeEUt)
230+
// .addLine(buffer -> {
231+
// buffer.writeBoolean(isStructureFormed());
232+
// if (isStructureFormed())
233+
// buffer.writeInt(noRotor ? -1 : getRotorHolder().getTotalEfficiency());
234+
// }, buffer -> {
235+
// if (!buffer.readBoolean()) return null;
236+
// int i = buffer.readInt();
237+
// if (i < 0) return null;
238+
// return KeyUtil.lang(TextFormatting.GRAY,
239+
// "gregtech.multiblock.turbine.efficiency", i);
240+
// })
241+
// .fuelNeeded(recipeLogic::getRecipeFluidInputInfo, recipeLogic::getPreviousRecipeDuration)
242+
// .status()
243+
// )
227244
.configureDisplayText(builder -> builder
228245
.setWorkingStatus(recipeLogic::isWorkingEnabled, recipeLogic::isActive)
229246
.addEnergyProductionLine(getMaxVoltage(), recipeLogic.getRecipeEUt())

0 commit comments

Comments
 (0)