Skip to content

Commit 5fa18d3

Browse files
committed
properly apply padding for scrollbar
1 parent 65b710d commit 5fa18d3

File tree

13 files changed

+323
-106
lines changed

13 files changed

+323
-106
lines changed

src/main/java/com/cleanroommc/modularui/drawable/Icon.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public Box getMargin() {
4747
@SideOnly(Side.CLIENT)
4848
@Override
4949
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
50-
x += this.margin.left;
51-
y += this.margin.top;
50+
x += this.margin.getLeft();
51+
y += this.margin.getTop();
5252
width -= this.margin.horizontal();
5353
height -= this.margin.vertical();
5454
if (this.width > 0) {
@@ -145,19 +145,7 @@ public void loadFromJson(JsonObject json) {
145145
JsonHelper.getBoolean(json, true, "autoHeight", "autoSize") ? 0 :
146146
JsonHelper.getInt(json, 0, "height", "h", "size");
147147
this.alignment = JsonHelper.deserialize(json, Alignment.class, Alignment.Center, "alignment", "align");
148-
this.margin.all(JsonHelper.getInt(json, 0, "margin"));
149-
if (json.has("marginHorizontal")) {
150-
this.margin.left = json.get("marginHorizontal").getAsInt();
151-
this.margin.right = this.margin.left;
152-
}
153-
if (json.has("marginVertical")) {
154-
this.margin.top = json.get("marginVertical").getAsInt();
155-
this.margin.bottom = this.margin.top;
156-
}
157-
this.margin.top = JsonHelper.getInt(json, this.margin.top, "marginTop");
158-
this.margin.bottom = JsonHelper.getInt(json, this.margin.bottom, "marginBottom");
159-
this.margin.left = JsonHelper.getInt(json, this.margin.left, "marginLeft");
160-
this.margin.right = JsonHelper.getInt(json, this.margin.right, "marginRight");
148+
this.margin.fromJson(json);
161149
}
162150

163151
public static Icon ofJson(JsonObject json) {
@@ -170,10 +158,7 @@ public boolean saveToJson(JsonObject json) {
170158
json.addProperty("width", this.width);
171159
json.addProperty("height", this.height);
172160
json.add("alignment", JsonHelper.serialize(this.alignment));
173-
json.addProperty("marginTop", this.margin.top);
174-
json.addProperty("marginBottom", this.margin.bottom);
175-
json.addProperty("marginLeft", this.margin.left);
176-
json.addProperty("marginRight", this.margin.right);
161+
this.margin.toJson(json);
177162
return true;
178163
}
179164

src/main/java/com/cleanroommc/modularui/test/TestTile.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
215215
.child(new ButtonWidget<>()
216216
.size(60, 18)
217217
.overlay(IKey.dynamic(() -> "Button " + this.val)))
218-
.child(new Rectangle()
219-
.setColor(Color.RED.main)
220-
.asWidget()
221-
.size(32, 2)
222-
.setEnabledIf(widget -> this.val > 10))
223218
.child(new FluidSlot()
224219
.margin(2)
225220
.syncHandler(SyncHandlers.fluidSlot(this.fluidTank)))
@@ -249,16 +244,19 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
249244
.overlay(IKey.str("Button 2")))
250245
.child(new TextFieldWidget()
251246
.size(60, 18)
247+
.paddingTop(1)
252248
.value(SyncHandlers.string(() -> this.value, val -> this.value = val))
253249
.margin(0, 2)
254250
.hintText("hint"))
255251
.child(new TextFieldWidget()
256252
.size(60, 18)
253+
.paddingTop(1)
257254
.value(SyncHandlers.doubleNumber(() -> this.doubleValue, val -> this.doubleValue = val))
258255
.setNumbersDouble(Function.identity())
259256
.hintText("number"))
260257
.child(IKey.str("Test string").asWidget().padding(2).debugName("test string"))
261-
.child(IKey.EMPTY.asWidget().debugName("Empty IKey")))
258+
//.child(IKey.EMPTY.asWidget().debugName("Empty IKey"))
259+
)
262260
.child(new Column()
263261
.debugName("button and slots test 2")
264262
.coverChildren()
@@ -361,13 +359,14 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
361359
return true;
362360
}))
363361
.child(new ListWidget<>()
362+
.debugName("test config list")
364363
.widthRel(1f).top(50).bottom(2)
365364
/*.child(new Rectangle().setColor(0xFF606060).asWidget()
366365
.top(1)
367366
.left(32)
368367
.size(1, 40))*/
369368
.child(new Row()
370-
.debugName("bogo test config 1")
369+
.debugName("test config 1")
371370
.widthRel(1f).coverChildrenHeight()
372371
.crossAxisAlignment(Alignment.CrossAxis.CENTER)
373372
.childPadding(2)
@@ -376,11 +375,10 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
376375
.stateOverlay(GuiTextures.CHECK_BOX)
377376
.size(14, 14)
378377
.margin(8, 4))
379-
.child(IKey.lang("bogosort.gui.enable_refill").asWidget()
380-
.height(14)
381-
.marginLeft(10)))
378+
.child(IKey.str("Boolean config").asWidget()
379+
.height(14)))
382380
.child(new Row()
383-
.debugName("bogo test config 2")
381+
.debugName("test config 2")
384382
.widthRel(1f).height(14)
385383
.childPadding(2)
386384
.child(new TextFieldWidget()
@@ -389,24 +387,24 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
389387
.setTextAlignment(Alignment.Center)
390388
.background(new Rectangle().setColor(0xFFb1b1b1))
391389
.setTextColor(IKey.TEXT_COLOR)
392-
.size(30, 14))
393-
.child(IKey.lang("bogosort.gui.refill_threshold").asWidget()
390+
.size(20, 14))
391+
.child(IKey.str("Number config").asWidget()
394392
.height(14)))
395-
.child(IKey.lang("bogosort.gui.hotbar_scrolling").asWidget()
393+
.child(IKey.str("Config title").asWidget()
396394
.color(0xFF404040)
397395
.alignment(Alignment.CenterLeft)
398396
.left(5).height(14)
399397
.tooltip(tooltip -> tooltip.showUpTimer(10)
400-
.addLine(IKey.lang("bogosort.gui.hotbar_scrolling.tooltip"))))
398+
.addLine(IKey.str("Config title tooltip"))))
401399
.child(new Row()
402-
.debugName("bogo test config 3")
400+
.debugName("test config 3")
403401
.widthRel(1f).height(14)
404402
.childPadding(2)
405403
.child(new CycleButtonWidget()
406404
.value(new BoolValue(false))
407405
.stateOverlay(GuiTextures.CHECK_BOX)
408406
.size(14, 14))
409-
.child(IKey.lang("bogosort.gui.enabled").asWidget()
407+
.child(IKey.str("Boolean config 3").asWidget()
410408
.height(14)))))
411409
.addPage(new ParentWidget<>()
412410
.debugName("page 4 storage")

src/main/java/com/cleanroommc/modularui/widget/AbstractScrollWidget.java

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@
1111
import com.cleanroommc.modularui.utils.HoveredWidgetList;
1212
import com.cleanroommc.modularui.widget.scroll.HorizontalScrollData;
1313
import com.cleanroommc.modularui.widget.scroll.ScrollArea;
14+
import com.cleanroommc.modularui.widget.scroll.ScrollData;
1415
import com.cleanroommc.modularui.widget.scroll.VerticalScrollData;
1516
import com.cleanroommc.modularui.widget.sizer.Area;
1617

1718
import org.jetbrains.annotations.NotNull;
1819
import org.jetbrains.annotations.Nullable;
1920

2021
/**
21-
* A scrollable parent widget. Children can be added
22+
* A scrollable parent widget. Children can be added.
2223
*
2324
* @param <I> type of children (in most cases just {@link IWidget})
2425
* @param <W> type of this widget
2526
*/
2627
public abstract class AbstractScrollWidget<I extends IWidget, W extends AbstractScrollWidget<I, W>> extends AbstractParentWidget<I, W> implements IViewport, Interactable {
2728

2829
private final ScrollArea scroll = new ScrollArea();
29-
private boolean keepScrollBarInArea = false;
30+
private boolean scrollXActive, scrollYActive;
3031

3132
public AbstractScrollWidget(@Nullable HorizontalScrollData x, @Nullable VerticalScrollData y) {
3233
super();
@@ -67,19 +68,27 @@ public void getWidgetsAt(IViewportStack stack, HoveredWidgetList widgets, int x,
6768
}
6869

6970
@Override
70-
public void postResize() {
71-
super.postResize();
72-
if (this.scroll.getScrollX() != null) {
73-
this.scroll.getScrollX().clamp(this.scroll);
74-
if (!this.keepScrollBarInArea) {
75-
getArea().height += this.scroll.getScrollX().getThickness();
76-
}
71+
public void beforeResize(boolean onOpen) {
72+
super.beforeResize(onOpen);
73+
if (onOpen) checkScrollbarActive(true);
74+
getScrollArea().getScrollPadding().scrollPaddingAll(0);
75+
applyAdditionalOffset(this.scroll.getScrollX());
76+
applyAdditionalOffset(this.scroll.getScrollY());
77+
}
78+
79+
private void checkScrollbarActive(boolean onOpen) {
80+
boolean scrollYActive = this.scroll.getScrollY() != null && this.scroll.getScrollY().isScrollBarActive(getScrollArea());
81+
boolean scrollXActive = this.scroll.getScrollX() != null && this.scroll.getScrollX().isScrollBarActive(getScrollArea(), this.scrollYActive);
82+
if (!onOpen && (scrollYActive != this.scrollYActive || scrollXActive != this.scrollXActive)) {
83+
scheduleResize();
7784
}
78-
if (this.scroll.getScrollY() != null) {
79-
this.scroll.getScrollY().clamp(this.scroll);
80-
if (!this.keepScrollBarInArea) {
81-
getArea().width += this.scroll.getScrollY().getThickness();
82-
}
85+
this.scrollXActive = scrollXActive;
86+
this.scrollYActive = scrollYActive;
87+
}
88+
89+
private void applyAdditionalOffset(ScrollData data) {
90+
if (data != null && data.isScrollBarActive(getScrollArea())) {
91+
getScrollArea().getScrollPadding().scrollPadding(data.getAxis().getOther(), data.isOnAxisStart(), data.getThickness());
8392
}
8493
}
8594

@@ -111,6 +120,7 @@ public boolean onMouseRelease(int mouseButton) {
111120
@Override
112121
public void onUpdate() {
113122
super.onUpdate();
123+
checkScrollbarActive(false);
114124
this.scroll.drag(getContext().getAbsMouseX(), getContext().getAbsMouseY());
115125
}
116126

@@ -136,21 +146,4 @@ public int getScrollX() {
136146
public int getScrollY() {
137147
return this.scroll.getScrollY() != null ? this.scroll.getScrollY().getScroll() : 0;
138148
}
139-
140-
/**
141-
* Sets whether the scroll bar should be kept inside the area of this widget, which might cause it to overlap with the content of this widget.
142-
* By setting the value to false, the size of this widget is expanded by the thickness of the scrollbars after the tree is resized.
143-
* Default: false
144-
*
145-
* @param value if the scroll bar should be kept inside the widgets area
146-
* @return this
147-
*/
148-
public W keepScrollBarInArea(boolean value) {
149-
this.keepScrollBarInArea = value;
150-
return getThis();
151-
}
152-
153-
public W keepScrollBarInArea() {
154-
return keepScrollBarInArea(true);
155-
}
156149
}

src/main/java/com/cleanroommc/modularui/widget/scroll/ScrollArea.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cleanroommc.modularui.utils.Color;
66
import com.cleanroommc.modularui.utils.MathUtils;
77
import com.cleanroommc.modularui.widget.sizer.Area;
8+
import com.cleanroommc.modularui.widget.sizer.Box;
89

910
import net.minecraft.client.gui.GuiScreen;
1011
import net.minecraftforge.fml.relauncher.Side;
@@ -20,6 +21,7 @@ public class ScrollArea extends Area {
2021

2122
private HorizontalScrollData scrollX;
2223
private VerticalScrollData scrollY;
24+
private final ScrollPadding scrollPadding = new ScrollPadding();
2325
private int scrollBarBackgroundColor = Color.withAlpha(Color.BLACK.main, 0.25f);
2426

2527
public ScrollArea(int x, int y, int w, int h) {
@@ -28,6 +30,15 @@ public ScrollArea(int x, int y, int w, int h) {
2830

2931
public ScrollArea() {}
3032

33+
@Override
34+
public Box getPadding() {
35+
return this.scrollPadding;
36+
}
37+
38+
public ScrollPadding getScrollPadding() {
39+
return this.scrollPadding;
40+
}
41+
3142
public void setScrollData(ScrollData data) {
3243
if (data instanceof HorizontalScrollData scrollData) {
3344
this.scrollX = scrollData;

0 commit comments

Comments
 (0)