Skip to content

Commit 7f936e8

Browse files
committed
A few minor GUI fixes and additions.
1 parent 1fddcf8 commit 7f936e8

File tree

4 files changed

+113
-26
lines changed

4 files changed

+113
-26
lines changed

Diff for: src/main/java/codechicken/lib/gui/modular/elements/GuiElement.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import org.jetbrains.annotations.Nullable;
2020

2121
import java.security.InvalidParameterException;
22-
import java.util.ArrayList;
23-
import java.util.Collections;
24-
import java.util.List;
22+
import java.util.*;
2523
import java.util.function.Consumer;
2624
import java.util.function.Supplier;
2725

@@ -53,6 +51,7 @@ public class GuiElement<T extends GuiElement<T>> extends ConstrainedGeometry<T>
5351
private final List<GuiElement<?>> removeQueue = new ArrayList<>();
5452
private final List<GuiElement<?>> childElements = new ArrayList<>();
5553
private final List<GuiElement<?>> childElementsUnmodifiable = Collections.unmodifiableList(childElements);
54+
private final Map<GuiElement<?>, Integer> reorderMap = new HashMap<>();
5655
public boolean initialized = false;
5756

5857
private Minecraft mc;
@@ -119,6 +118,15 @@ public void addChild(GuiElement<?> child) {
119118
}
120119

121120
protected void applyQueuedChildUpdates() {
121+
if (!reorderMap.isEmpty()) {
122+
reorderMap.forEach((element, index) -> {
123+
if (!childElements.contains(element)) return;
124+
childElements.remove(element);
125+
childElements.add(Math.min(index, childElements.size()), element);
126+
});
127+
reorderMap.clear();
128+
}
129+
122130
if (!removeQueue.isEmpty()) {
123131
childElements.removeAll(removeQueue);
124132
removeQueue.clear();
@@ -155,6 +163,27 @@ public void removeChild(GuiElement<?> child) {
155163
addedQueue.remove(child);
156164
}
157165

166+
/**
167+
* Sets a child as the last element in the child lest, meaning it will render over all other elements.
168+
*/
169+
public void bringChildToForeground(GuiElement<?> child) {
170+
sendChildToIndex(child, childElements.size() - 1);
171+
}
172+
173+
/**
174+
* sends a child to a specific index in the child lest.
175+
*/
176+
public void sendChildToIndex(GuiElement<?> child, int index) {
177+
reorderMap.put(child, index);
178+
}
179+
180+
/**
181+
* Sets a child as the first element in the child lest, meaning it will render under all other elements.
182+
*/
183+
public void sendChildToBackground(GuiElement<?> child) {
184+
sendChildToIndex(child, 0);
185+
}
186+
158187
@Override
159188
public boolean isDescendantOf(GuiElement<?> ancestor) {
160189
return ancestor == parent || parent.isDescendantOf(ancestor);

Diff for: src/main/java/codechicken/lib/gui/modular/elements/GuiManipulable.java

+73-18
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import codechicken.lib.gui.modular.lib.geometry.Rectangle;
99
import net.minecraft.client.Minecraft;
1010
import net.minecraft.client.gui.Font;
11-
import net.minecraft.resources.ResourceLocation;
1211
import org.jetbrains.annotations.NotNull;
1312
import org.jetbrains.annotations.Nullable;
1413

14+
import java.util.function.Consumer;
15+
1516
import static codechicken.lib.gui.modular.lib.geometry.Constraint.literal;
1617
import static codechicken.lib.gui.modular.lib.geometry.Constraint.match;
1718
import static codechicken.lib.gui.modular.lib.geometry.GeoParam.*;
@@ -44,6 +45,7 @@ public class GuiManipulable extends GuiElement<GuiManipulable> implements Conten
4445
private boolean dragBottom = false;
4546
private boolean dragRight = false;
4647
private boolean enableCursors = false;
48+
private boolean resetOnUiInit = true;
4749

4850
//Made available for external position restraints
4951
public int xMin = 0;
@@ -53,8 +55,8 @@ public class GuiManipulable extends GuiElement<GuiManipulable> implements Conten
5355

5456
protected Rectangle minSize = Rectangle.create(0, 0, 50, 50);
5557
protected Rectangle maxSize = Rectangle.create(0, 0, 256, 256);
56-
protected Runnable onMovedCallback = null;
57-
protected Runnable onResizedCallback = null;
58+
protected Consumer<Boolean> onMovedCallback = null;
59+
protected Consumer<Boolean> onResizedCallback = null;
5860
protected PositionRestraint positionRestraint = draggable -> {
5961
if (xMin < 0) {
6062
int move = -xMin;
@@ -112,7 +114,15 @@ public GuiManipulable constrain(GeoParam param, @Nullable Constraint constraint)
112114
@Override
113115
public void onScreenInit(Minecraft mc, Font font, int screenWidth, int screenHeight) {
114116
super.onScreenInit(mc, font, screenWidth, screenHeight);
115-
resetBounds();
117+
if (resetOnUiInit) resetBounds();
118+
}
119+
120+
/**
121+
* @param resetOnUiInit If true, element bounds will be reset on UI init. (Default: true)
122+
*/
123+
public GuiManipulable setResetOnUiInit(boolean resetOnUiInit) {
124+
this.resetOnUiInit = resetOnUiInit;
125+
return this;
116126
}
117127

118128
@Override
@@ -137,6 +147,11 @@ public GuiManipulable addTopHandle(int handleSize) {
137147
return this;
138148
}
139149

150+
public GuiManipulable setTopHandle(GuiElement<?> topHandle) {
151+
this.topHandle = topHandle;
152+
return this;
153+
}
154+
140155
public GuiManipulable addBottomHandle(int handleSize) {
141156
this.bottomHandle
142157
.constrain(BOTTOM, match(contentElement.get(BOTTOM)))
@@ -146,6 +161,11 @@ public GuiManipulable addBottomHandle(int handleSize) {
146161
return this;
147162
}
148163

164+
public GuiManipulable setBottomHandle(GuiElement<?> bottomHandle) {
165+
this.bottomHandle = bottomHandle;
166+
return this;
167+
}
168+
149169
public GuiManipulable addLeftHandle(int handleSize) {
150170
this.leftHandle
151171
.constrain(LEFT, match(contentElement.get(LEFT)))
@@ -155,6 +175,11 @@ public GuiManipulable addLeftHandle(int handleSize) {
155175
return this;
156176
}
157177

178+
public GuiManipulable setLeftHandle(GuiElement<?> leftHandle) {
179+
this.leftHandle = leftHandle;
180+
return this;
181+
}
182+
158183
public GuiManipulable addRightHandle(int handleSize) {
159184
this.rightHandle
160185
.constrain(RIGHT, match(contentElement.get(RIGHT)))
@@ -164,6 +189,11 @@ public GuiManipulable addRightHandle(int handleSize) {
164189
return this;
165190
}
166191

192+
public GuiManipulable setRightHandle(GuiElement<?> rightHandle) {
193+
this.rightHandle = rightHandle;
194+
return this;
195+
}
196+
167197
public GuiManipulable addMoveHandle(int handleSize) {
168198
this.moveHandle
169199
.constrain(TOP, match(contentElement.get(TOP)))
@@ -173,6 +203,11 @@ public GuiManipulable addMoveHandle(int handleSize) {
173203
return this;
174204
}
175205

206+
public GuiManipulable setMoveHandle(GuiElement<?> moveHandle) {
207+
this.moveHandle = moveHandle;
208+
return this;
209+
}
210+
176211
/**
177212
* You can use this to retrieve the current move handle.
178213
* You are free to update the constraints on this handle, but it must be constrained relative to the content element.
@@ -222,11 +257,21 @@ public GuiManipulable enableCursors(boolean enableCursors) {
222257
}
223258

224259
public GuiManipulable setOnMovedCallback(Runnable onMovedCallback) {
260+
this.onMovedCallback = finished -> onMovedCallback.run();
261+
return this;
262+
}
263+
264+
public GuiManipulable setOnMovedCallback(Consumer<Boolean> onMovedCallback) {
225265
this.onMovedCallback = onMovedCallback;
226266
return this;
227267
}
228268

229269
public GuiManipulable setOnResizedCallback(Runnable onResizedCallback) {
270+
this.onResizedCallback = finished -> onResizedCallback.run();
271+
return this;
272+
}
273+
274+
public GuiManipulable setOnResizedCallback(Consumer<Boolean> onResizedCallback) {
230275
this.onResizedCallback = onResizedCallback;
231276
return this;
232277
}
@@ -324,13 +369,12 @@ public void mouseMoved(double mouseX, double mouseY) {
324369
int xMove = (int) (mouseX - dragXOffset) - xMin;
325370
int yMove = (int) (mouseY - dragYOffset) - yMin;
326371
if (dragPos) {
327-
Rectangle previous = Rectangle.create(xMin, yMin, xMax - xMin, yMax - yMin);
328372
xMin += xMove;
329373
xMax += xMove;
330374
yMin += yMove;
331375
yMax += yMove;
332-
validatePosition();
333-
onMoved();
376+
validatePosition(false);
377+
onMoved(false);
334378
} else {
335379
Rectangle min = getMinSize();
336380
Rectangle max = getMaxSize();
@@ -358,43 +402,54 @@ public void mouseMoved(double mouseX, double mouseY) {
358402
if (xMax - xMin > max.width()) xMax = xMin + (int) max.width();
359403
if (xMax > scaledScreenWidth()) xMax = scaledScreenWidth();
360404
}
361-
validatePosition();
362-
onResized();
405+
validatePosition(false);
406+
onResized(false);
363407
}
364408
}
365409
super.mouseMoved(mouseX, mouseY);
366410
}
367411

368412
@Override
369413
public boolean mouseReleased(double mouseX, double mouseY, int button, boolean consumed) {
370-
if (isDragging) {
371-
validatePosition();
414+
if (isMoving()) {
415+
validatePosition(true);
416+
}
417+
if (isResizing()) {
418+
onResized(true);
372419
}
373420
isDragging = dragPos = dragTop = dragLeft = dragBottom = dragRight = false;
374421
return super.mouseReleased(mouseX, mouseY, button, consumed);
375422
}
376423

377-
protected void validatePosition() {
424+
protected void validatePosition(boolean finished) {
378425
double x = xMin;
379426
double y = yMin;
380427
positionRestraint.restrainPosition(this);
381-
if ((x != xMin || y != yMin) && onMovedCallback != null) {
382-
onMovedCallback.run();
428+
if ((x != xMin || y != yMin)) {
429+
onMoved(finished);
383430
}
384431
}
385432

386-
protected void onMoved() {
433+
protected void onMoved(boolean finished) {
387434
if (onMovedCallback != null) {
388-
onMovedCallback.run();
435+
onMovedCallback.accept(finished);
389436
}
390437
}
391438

392-
protected void onResized() {
439+
protected void onResized(boolean finished) {
393440
if (onResizedCallback != null) {
394-
onResizedCallback.run();
441+
onMovedCallback.accept(finished);
395442
}
396443
}
397444

445+
public boolean isMoving() {
446+
return dragPos;
447+
}
448+
449+
public boolean isResizing() {
450+
return dragTop || dragLeft || dragBottom || dragRight;
451+
}
452+
398453
public interface PositionRestraint {
399454
void restrainPosition(GuiManipulable draggable);
400455
}

Diff for: src/main/java/codechicken/lib/gui/modular/elements/GuiSlots.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ public void renderBackground(GuiRender render, double mouseX, double mouseY, flo
346346

347347
for (int index = 0; index < slotCount; index++) {
348348
Slot slot = slots.getSlot(index + firstSlot);
349-
render.texRect(slotTexture.apply(slot), slot.x + root.xMin() - 1, slot.y + root.yMin() - 1, 18, 18);
349+
Material tex = slotTexture.apply(slot);
350+
if (tex != null) {
351+
render.texRect(tex, slot.x + root.xMin() - 1, slot.y + root.yMin() - 1, 18, 18);
352+
}
350353
}
351354

352355
render.pose().translate(0, 0, 0.4);

Diff for: src/main/java/codechicken/lib/inventory/container/modular/ModularGuiContainerMenu.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ protected boolean moveItemStackTo(ItemStack stack, List<Slot> targets, boolean r
244244
itemStack2 = slot.getItem();
245245
if (!itemStack2.isEmpty() && ItemStack.isSameItemSameTags(stack, itemStack2)) {
246246
int l = itemStack2.getCount() + stack.getCount();
247-
if (l <= stack.getMaxStackSize()) {
247+
if (l <= Math.min(stack.getMaxStackSize(), slot.getMaxStackSize(stack))) {
248248
stack.setCount(0);
249249
itemStack2.setCount(l);
250250
slot.setChanged();
251251
moved = true;
252-
} else if (itemStack2.getCount() < stack.getMaxStackSize()) {
252+
} else if (itemStack2.getCount() < Math.min(stack.getMaxStackSize(), slot.getMaxStackSize(stack))) {
253253
stack.shrink(stack.getMaxStackSize() - itemStack2.getCount());
254254
itemStack2.setCount(stack.getMaxStackSize());
255255
slot.setChanged();
@@ -284,8 +284,8 @@ protected boolean moveItemStackTo(ItemStack stack, List<Slot> targets, boolean r
284284
slot = targets.get(position);
285285
itemStack2 = slot.getItem();
286286
if (itemStack2.isEmpty() && slot.mayPlace(stack)) {
287-
if (stack.getCount() > slot.getMaxStackSize()) {
288-
slot.set(stack.split(slot.getMaxStackSize()));
287+
if (stack.getCount() > slot.getMaxStackSize(stack)) {
288+
slot.set(stack.split(slot.getMaxStackSize(stack)));
289289
} else {
290290
slot.set(stack.split(stack.getCount()));
291291
}

0 commit comments

Comments
 (0)