Skip to content

Commit bccd3ed

Browse files
committed
rewrite margin/padding apply
1 parent 991e1b3 commit bccd3ed

File tree

9 files changed

+85
-40
lines changed

9 files changed

+85
-40
lines changed

src/main/java/com/cleanroommc/modularui/GuiError.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
import com.cleanroommc.modularui.api.widget.IGuiElement;
44

5+
import com.cleanroommc.modularui.network.NetworkHandler;
6+
7+
import com.cleanroommc.modularui.network.NetworkUtils;
8+
59
import org.apache.logging.log4j.Level;
610

711
import java.util.Objects;
812

913
public class GuiError {
1014

1115
public static void throwNew(IGuiElement guiElement, Type type, String msg) {
12-
GuiErrorHandler.INSTANCE.pushError(guiElement, type, msg);
16+
if (NetworkUtils.isClient()) {
17+
GuiErrorHandler.INSTANCE.pushError(guiElement, type, msg);
18+
}
1319
}
1420

1521
private final Level level = Level.ERROR;
@@ -23,9 +29,25 @@ protected GuiError(String msg, IGuiElement reference, Type type) {
2329
this.type = type;
2430
}
2531

32+
public Level getLevel() {
33+
return level;
34+
}
35+
36+
public IGuiElement getReference() {
37+
return reference;
38+
}
39+
40+
public Type getType() {
41+
return type;
42+
}
43+
44+
public String getMsg() {
45+
return msg;
46+
}
47+
2648
@Override
2749
public String toString() {
28-
return "[" + this.level.name() + "][" + this.reference.toString() + "][" + this.type.toString() + "]: " + this.msg;
50+
return "MUI [" + this.type.toString() + "][" + this.reference.toString() + "]: " + this.msg;
2951
}
3052

3153
@Override

src/main/java/com/cleanroommc/modularui/GuiErrorHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public class GuiErrorHandler {
1919
private final Set<GuiError> errorSet = new ObjectOpenHashSet<>();
2020
private final List<GuiError> errors = new ArrayList<>();
2121

22-
private GuiErrorHandler() {
23-
}
22+
private GuiErrorHandler() {}
2423

2524
public void clear() {
2625
this.errors.clear();
@@ -29,7 +28,7 @@ public void clear() {
2928
void pushError(IGuiElement reference, GuiError.Type type, String msg) {
3029
GuiError error = new GuiError(msg, reference, type);
3130
if (this.errorSet.add(error)) {
32-
ModularUI.LOGGER.error(msg);
31+
ModularUI.LOGGER.log(error.getLevel(), error);
3332
this.errors.add(error);
3433
}
3534
}
@@ -38,6 +37,5 @@ public List<GuiError> getErrors() {
3837
return this.errors;
3938
}
4039

41-
public void drawErrors(int x, int y) {
42-
}
40+
public void drawErrors(int x, int y) {}
4341
}

src/main/java/com/cleanroommc/modularui/screen/ClientScreenHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.screen;
22

3+
import com.cleanroommc.modularui.GuiErrorHandler;
34
import com.cleanroommc.modularui.ModularUI;
45
import com.cleanroommc.modularui.ModularUIConfig;
56
import com.cleanroommc.modularui.api.IMuiScreen;
@@ -124,7 +125,7 @@ public static void onGuiOpen(GuiOpenEvent event) {
124125
currentScreen = null;
125126
lastChar = null;
126127
}
127-
128+
GuiErrorHandler.INSTANCE.clear();
128129
OverlayManager.onGuiOpen(event);
129130
}
130131

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
171171
})
172172
.animationDuration(500)
173173
.interpolation(Interpolation.BOUNCE_OUT)
174-
.normalView(new ItemDrawable(Blocks.CRAFTING_TABLE).asIcon().asWidget().size(20).pos(0, 0))
174+
.collapsedView(new ItemDrawable(Blocks.CRAFTING_TABLE).asIcon().asWidget().size(20).pos(0, 0))
175175
.expandedView(new ParentWidget<>()
176176
.debugName("crafting tab")
177177
.coverChildren()
@@ -184,7 +184,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
184184
.key('O', new ItemSlot().slot(new ModularCraftingSlot(this.craftingInventory, 9)))
185185
.key('D', new ItemDisplayWidget().syncHandler("display_item").displayAmount(true))
186186
.build()
187-
.margin(5, 5, 20, 5))))
187+
.margin(5, 5, 20, 5).debugName("crafting"))))
188188
.child(Flow.column()
189189
.sizeRel(1f)
190190
.paddingBottom(7)
@@ -198,11 +198,12 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
198198
.addPage(new ParentWidget<>()
199199
.debugName("page 1 parent")
200200
.sizeRel(1f, 1f)
201-
.padding(7)
201+
.padding(7, 0)
202202
.child(new Row()
203203
.debugName("buttons, slots and more tests")
204204
.height(137)
205205
.coverChildrenWidth()
206+
.verticalCenter()
206207
//.padding(7)
207208
.child(new Column()
208209
.debugName("buttons and slots test")
@@ -247,17 +248,17 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
247248
//.flex(flex -> flex.left(3)) // ?
248249
.overlay(IKey.str("Button 2")))
249250
.child(new TextFieldWidget()
250-
.size(60, 20)
251+
.size(60, 18)
251252
.value(SyncHandlers.string(() -> this.value, val -> this.value = val))
252-
.margin(0, 3)
253+
.margin(0, 2)
253254
.hintText("hint"))
254255
.child(new TextFieldWidget()
255-
.size(60, 20)
256+
.size(60, 18)
256257
.value(SyncHandlers.doubleNumber(() -> this.doubleValue, val -> this.doubleValue = val))
257258
.setNumbersDouble(Function.identity())
258259
.hintText("number"))
259260
.child(IKey.str("Test string").asWidget().padding(2).debugName("test string"))
260-
.child(IKey.EMPTY.asWidget().debugName("Empty Ikey")))
261+
.child(IKey.EMPTY.asWidget().debugName("Empty IKey")))
261262
.child(new Column()
262263
.debugName("button and slots test 2")
263264
.coverChildren()
@@ -270,7 +271,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
270271
.progress(() -> this.progress / (double) this.duration)
271272
.texture(GuiTextures.PROGRESS_CYCLE, 20)
272273
.direction(ProgressWidget.Direction.CIRCULAR_CW))
273-
.child(new Row().widthRel(1f).height(18)
274+
.child(new Row().coverChildrenWidth().height(18)
274275
.child(new ToggleButton()
275276
.value(new BoolValue.Dynamic(() -> cycleStateValue.getIntValue() == 0, val -> cycleStateValue.setIntValue(0)))
276277
.overlay(GuiTextures.CYCLE_BUTTON_DEMO.getSubArea(0, 0, 1, 1 / 3f)))
@@ -313,7 +314,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
313314
}
314315
return new ItemSlot().slot(SyncHandlers.itemSlot(this.bigInventory, index).slotGroup("item_inv"));
315316
})
316-
.build()
317+
.build().debugName("9 slot inv")
317318
//.marginBottom(2)
318319
.child(new SortButtons()
319320
.slotGroup("item_inv")
@@ -323,7 +324,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager syncManager, UI
323324
.row("FII")
324325
.key('F', index -> new FluidSlot().syncHandler("mixer_fluids", index))
325326
.key('I', index -> ItemSlot.create(index >= 2).slot(new ModularSlot(this.mixerItems, index).slotGroup("mixer_items")))
326-
.build())
327+
.build().debugName("mixer inv"))
327328
.child(new Row()
328329
.coverChildrenHeight()
329330
.child(new CycleButtonWidget()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void getWidgetsAt(IViewportStack stack, HoveredWidgetList widgets, int x,
6767
}
6868

6969
@Override
70-
public void onResized() {
71-
super.onResized();
70+
public void postResize() {
71+
super.postResize();
7272
if (this.scroll.getScrollX() != null) {
7373
this.scroll.getScrollX().clamp(this.scroll);
7474
if (!this.keepScrollBarInArea) {

src/main/java/com/cleanroommc/modularui/widget/sizer/DimensionSizer.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.widget.sizer;
22

3+
import com.cleanroommc.modularui.GuiError;
34
import com.cleanroommc.modularui.ModularUI;
45
import com.cleanroommc.modularui.ModularUIConfig;
56
import com.cleanroommc.modularui.api.GuiAxis;
@@ -268,19 +269,45 @@ public void coverChildrenForEmpty(Area area, Area relativeTo) {
268269
}
269270
}
270271

271-
public void applyMarginAndPaddingToPos(Area area, Area relativeTo) {
272+
public void applyMarginAndPaddingToPos(IGuiElement parent, Area area, Area relativeTo) {
272273
// apply self margin and parent padding if not done yet
273274
if (isMarginPaddingApplied()) return;
274275
setMarginPaddingApplied(true);
275-
int o = area.getMargin().getStart(this.axis) + relativeTo.getPadding().getStart(this.axis);
276-
if (o == 0) return;
277-
if (this.start != null && !this.start.isRelative()) return;
278-
if (this.end != null && !this.end.isRelative() && (this.size == null || !this.size.isRelative())) return;
279-
area.setRelativePoint(this.axis, area.getRelativePoint(this.axis) + o);
276+
int left = area.getMargin().getStart(this.axis) + relativeTo.getPadding().getStart(this.axis);
277+
int right = area.getMargin().getEnd(this.axis) + relativeTo.getPadding().getEnd(this.axis);
278+
if (left > 0 && ((this.start != null && !this.start.isRelative()) ||
279+
(this.end != null && !this.end.isRelative() && (this.size == null || !this.size.isRelative())))) {
280+
left = 0;
281+
}
282+
if (right > 0 && ((this.end != null && !this.end.isRelative()) ||
283+
(this.start != null && !this.start.isRelative() && (this.size == null || !this.size.isRelative())))) {
284+
right = 0;
285+
}
286+
if (left == 0 && right == 0) return;
287+
int parentS = relativeTo.getSize(this.axis);
288+
int s = area.getSize(this.axis);
289+
int rp = area.getRelativePoint(this.axis); // relative pos
290+
if (left > 0) {
291+
if (right > 0) {
292+
if (left + right + s > parentS) {
293+
// widget and margin + padding is larger than available space
294+
area.setRelativePoint(this.axis, left);
295+
GuiError.throwNew(parent, GuiError.Type.SIZING, "Margin/padding is set on both sides on axis " + this.axis +
296+
", but total size exceeds parent size.");
297+
return;
298+
}
299+
if (right > parentS - s - rp) area.setRelativePoint(this.axis, parentS - right - s);
300+
else if (left > rp) area.setRelativePoint(this.axis, left);
301+
return;
302+
}
303+
if (left > rp) area.setRelativePoint(this.axis, left);
304+
} else if (right > 0) {
305+
if (right > parentS - s - rp) area.setRelativePoint(this.axis, parentS - right - s);
306+
}
280307
}
281308

282309
private int calcSize(Unit s, int parentSize, boolean parentSizeCalculated) {
283-
if (this.coverChildren) return 18;
310+
if (this.coverChildren) return 18; // placeholder value
284311
float val = s.getValue();
285312
if (s.isRelative()) {
286313
if (!parentSizeCalculated) return (int) val;

src/main/java/com/cleanroommc/modularui/widget/sizer/Flex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ public void applyPos(IGuiElement parent) {
502502
Area relativeTo = getRelativeTo().getArea();
503503
Area area = parent.getArea();
504504
// apply margin and padding if not done yet
505-
this.x.applyMarginAndPaddingToPos(area, relativeTo);
506-
this.y.applyMarginAndPaddingToPos(area, relativeTo);
505+
this.x.applyMarginAndPaddingToPos(parent, area, relativeTo);
506+
this.y.applyMarginAndPaddingToPos(parent, area, relativeTo);
507507
// after all widgets x, y, width and height have been calculated we can now calculate the absolute position
508508
area.applyPos(relativeTo.x, relativeTo.y);
509509
Area parentArea = parent.getParentArea();

src/main/java/com/cleanroommc/modularui/widgets/Expandable.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,10 @@ public void beforeResize(boolean onOpen) {
5454
this.currentChildren = Collections.singletonList(this.expanded ? this.expandedView : this.normalView);
5555
}
5656

57-
@Override
58-
public void onResized() {
59-
super.onResized();
60-
currentChildren = children;
61-
}
62-
6357
@Override
6458
public void postResize() {
6559
super.postResize();
60+
currentChildren = children;
6661
if (this.animator != null) {
6762
this.animator.stop(true);
6863
this.animator = null;
@@ -142,18 +137,18 @@ public void getWidgetsAt(IViewportStack stack, HoveredWidgetList widgets, int x,
142137
public Expandable expanded(boolean expanded) {
143138
if (this.expanded == expanded) return this;
144139
this.expanded = expanded;
145-
if (expanded) {
146-
this.normalView.setEnabled(false);
147-
this.expandedView.setEnabled(true);
148-
}
149140
if (isValid()) {
141+
if (expanded) {
142+
this.normalView.setEnabled(false);
143+
this.expandedView.setEnabled(true);
144+
}
150145
this.areaSnapshot = getArea().copyOrImmutable();
151146
scheduleResize();
152147
}
153148
return this;
154149
}
155150

156-
public Expandable normalView(IWidget normalView) {
151+
public Expandable collapsedView(IWidget normalView) {
157152
this.normalView = normalView;
158153
this.children.set(0, normalView);
159154
if (isValid()) {

src/main/java/com/cleanroommc/modularui/widgets/SortableListWidget.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void beforeResize(boolean onOpen) {
6464

6565
@Override
6666
public void postResize() {
67+
super.postResize();
6768
if (this.scheduleAnimation && !this.widgetAreaSnapshots.isEmpty()) {
6869
@UnmodifiableView @NotNull List<Item<T>> typeChildren = getTypeChildren();
6970
for (int i = 0; i < typeChildren.size(); i++) {

0 commit comments

Comments
 (0)