Skip to content

Commit fc83635

Browse files
committed
add simulate to text draw
"improve" fix to drawing scrolling text remove dirty field from ui builder
1 parent a4ba4ab commit fc83635

File tree

3 files changed

+73
-33
lines changed

3 files changed

+73
-33
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import gregtech.api.util.function.FloatSupplier;
2121
import gregtech.common.ConfigHolder;
2222
import gregtech.common.items.ToolItems;
23+
import gregtech.common.mui.widget.ScrollableTextWidget;
2324

2425
import net.minecraft.item.ItemStack;
2526
import net.minecraft.network.PacketBuffer;
@@ -77,8 +78,8 @@ public class MultiblockUIBuilder {
7778
private IKey idlingKey = IKey.lang("gregtech.multiblock.idling").style(TextFormatting.GRAY);
7879
private IKey pausedKey = IKey.lang("gregtech.multiblock.work_paused").style(TextFormatting.GOLD);
7980
private IKey runningKey = IKey.lang("gregtech.multiblock.running").style(TextFormatting.GREEN);
80-
private boolean dirty;
8181
private Runnable onRebuild;
82+
private Runnable postRebuild;
8283

8384
@NotNull
8485
InternalSyncer getSyncer() {
@@ -797,18 +798,11 @@ public void sync(String key, PanelSyncManager syncManager) {
797798
}
798799

799800
/**
800-
* Builds the passed in rich text with operations and drawables. <br />
801-
* Will clear and rebuild if this builder is marked dirty
801+
* Builds the passed in rich text with operations and drawables.
802802
*
803803
* @param richText the rich text to add drawables to
804804
*/
805805
public void build(IRichTextBuilder<?> richText) {
806-
if (dirty) {
807-
clear();
808-
onRebuild();
809-
runAction();
810-
dirty = false;
811-
}
812806
for (Operation op : operations) {
813807
op.accept(richText);
814808
}
@@ -820,11 +814,10 @@ private void onRebuild() {
820814
}
821815
}
822816

823-
/**
824-
* Mark this builder as dirty. Will be rebuilt during {@link #build(IRichTextBuilder) build()}
825-
*/
826-
public void markDirty() {
827-
dirty = true;
817+
private void postRebuild() {
818+
if (this.postRebuild != null) {
819+
this.postRebuild.run();
820+
}
828821
}
829822

830823
/*
@@ -855,6 +848,15 @@ public void onRebuild(Runnable onRebuild) {
855848
this.onRebuild = onRebuild;
856849
}
857850

851+
/**
852+
* The runnable is called after rebuilding, usually used for {@link ScrollableTextWidget#postRebuild()}
853+
*
854+
* @param postRebuild the runnable to run after rebuilding
855+
*/
856+
public void postRebuild(Runnable postRebuild) {
857+
this.postRebuild = postRebuild;
858+
}
859+
858860
private void addHoverableKey(IKey key, IDrawable... hover) {
859861
if (isServer()) return;
860862
addKey(KeyUtil.setHover(key, hover));
@@ -1042,6 +1044,7 @@ public void readOnClient(int id, PacketBuffer buf) {
10421044
getSyncer().readBuffer(buf);
10431045
onRebuild();
10441046
runAction();
1047+
postRebuild();
10451048
}
10461049
}
10471050

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ protected Widget<?> createScreen(PanelSyncManager syncManager) {
360360
.debugName("display_text")
361361
.sizeRel(1f)
362362
.alignment(Alignment.TopLeft)
363-
.margin(4, 4)
363+
.margin(4)
364364
.autoUpdate(true)
365-
.textBuilder(display::build));
365+
.textBuilder(display));
366366
}
367367

368368
if (this.screenFunction != null) {

src/main/java/gregtech/common/mui/widget/ScrollableTextWidget.java

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gregtech.common.mui.widget;
22

3+
import gregtech.api.metatileentity.multiblock.ui.MultiblockUIBuilder;
34
import gregtech.api.mui.IconAcessor;
45

56
import net.minecraft.client.gui.FontRenderer;
@@ -18,24 +19,26 @@
1819
import com.cleanroommc.modularui.screen.ModularScreen;
1920
import com.cleanroommc.modularui.screen.RichTooltip;
2021
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
22+
import com.cleanroommc.modularui.theme.WidgetTheme;
23+
import com.cleanroommc.modularui.utils.Alignment;
2124
import com.cleanroommc.modularui.utils.HoveredWidgetList;
2225
import com.cleanroommc.modularui.widget.Widget;
2326
import com.cleanroommc.modularui.widget.scroll.ScrollArea;
2427
import com.cleanroommc.modularui.widget.scroll.ScrollData;
2528
import com.cleanroommc.modularui.widget.sizer.Area;
29+
import com.cleanroommc.modularui.widget.sizer.Box;
2630
import org.jetbrains.annotations.NotNull;
2731
import org.jetbrains.annotations.Nullable;
2832

29-
import java.util.function.Consumer;
30-
3133
public class ScrollableTextWidget extends Widget<ScrollableTextWidget>
3234
implements IRichTextBuilder<ScrollableTextWidget>, Interactable, IViewport,
3335
JeiIngredientProvider {
3436

3537
private final RichText text = new RichText();
36-
private Consumer<RichText> builder;
38+
private MultiblockUIBuilder builder;
3739
private boolean dirty = false;
3840
private boolean autoUpdate = false;
41+
private boolean initScroll;
3942
private Object lastIngredient;
4043

4144
private final ScrollArea scroll = new ScrollArea();
@@ -161,26 +164,60 @@ public void preDraw(ModularGuiContext context, boolean transformed) {
161164
if (!transformed) {
162165
Stencil.applyAtZero(this.scroll, context);
163166
} else {
164-
drawText(context);
167+
drawText(context, false);
165168
}
166169
}
167170

168-
private void drawText(ModularGuiContext context) {
171+
public void postRebuild() {
172+
if (initScroll || !isValid()) return;
173+
initScroll = true;
174+
175+
// i really hate how this is done but it works
176+
// this is responsible for scrolling to the right value on ui ope
177+
// eg half-way for center, and at the bottom for bottom alignments
178+
drawText(getContext(), true);
179+
int size = this.scroll.getScrollY().getScrollSize();
180+
if (size <= getArea().h()) return;
181+
size -= getArea().h();
182+
183+
Alignment alignment = this.text.getAlignment();
184+
int scroll = (int) (size * alignment.y);
185+
this.scroll.getScrollY().scrollTo(this.scroll, scroll);
186+
}
187+
188+
private void drawText(ModularGuiContext context, boolean simulate) {
169189
if (this.autoUpdate || this.dirty) {
170190
if (this.builder != null) {
171191
this.text.clearText();
172-
this.builder.accept(this.text);
192+
this.builder.build(this.text);
173193
}
174194
this.dirty = false;
175195
}
176-
this.text.setupRenderer(this.renderer, getArea().getPadding().left, getArea().getPadding().top - getScrollY(),
177-
getArea().paddedWidth(), getArea().paddedHeight(),
178-
getWidgetTheme(context.getTheme()).getTextColor(),
179-
getWidgetTheme(context.getTheme()).getTextShadow());
180-
this.text.compileAndDraw(this.renderer, context, false);
196+
197+
Alignment alignment = this.text.getAlignment();
198+
Area area = getArea();
199+
Box padding = area.getPadding();
200+
WidgetTheme widgetTheme = getWidgetTheme(context.getTheme());
201+
202+
this.text.compileAndDraw(this.renderer, context, true);
203+
181204
// this isn't perfect, but i hope it's good enough
182-
int diff = (int) Math.ceil((this.renderer.getLastHeight() - getArea().h()) / 2);
183-
this.scroll.getScrollY().setScrollSize(getArea().h() + Math.max(0, diff));
205+
int diff = (int) Math.ceil((this.renderer.getLastHeight() - area.h()) / 2);
206+
this.scroll.getScrollY().setScrollSize(area.h() + Math.max(0, diff));
207+
208+
if (!simulate) {
209+
// this is responsible for centering the text if there's not enough to scroll
210+
int x = padding.left;
211+
int y = (int) (area.h() * alignment.y);
212+
if (alignment.y == 0.5f) y -= (int) (this.renderer.getLastHeight() / 2);
213+
y = Math.min(Math.max(padding.top, y), area.h() - padding.bottom);
214+
if (alignment.y > 0.5f) y -= area.h(); // why?
215+
this.text.setupRenderer(this.renderer, x, y - getScrollY(),
216+
area.paddedWidth(), area.paddedHeight(),
217+
widgetTheme.getTextColor(), widgetTheme.getTextShadow());
218+
219+
this.text.compileAndDraw(this.renderer, context, false);
220+
}
184221
}
185222

186223
@Override
@@ -215,11 +252,11 @@ public ScrollableTextWidget autoUpdate(boolean autoUpdate) {
215252
/**
216253
* A builder which is called every time before drawing when {@link #dirty} is true.
217254
*
218-
* @param builder text builder
219-
* @return this
255+
* @param uiBuilder@return this
220256
*/
221-
public ScrollableTextWidget textBuilder(Consumer<RichText> builder) {
222-
this.builder = builder;
257+
public ScrollableTextWidget textBuilder(MultiblockUIBuilder uiBuilder) {
258+
this.builder = uiBuilder;
259+
this.builder.postRebuild(this::postRebuild);
223260
markDirty();
224261
return this;
225262
}

0 commit comments

Comments
 (0)