Skip to content

Commit 2308b1e

Browse files
committed
Work on better multiplier controls
1 parent 9d1033e commit 2308b1e

File tree

4 files changed

+123
-44
lines changed

4 files changed

+123
-44
lines changed

src/main/java/gregtech/api/util/GTUtility.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,13 @@ public static int safeCastLongToInt(long v) {
924924
return v > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) v;
925925
}
926926

927+
/**
928+
* Safely multiply two Ints without overflowing
929+
*/
930+
public static int safeIntegerMultiplication(int a, int b) {
931+
return safeCastLongToInt((long) a * b);
932+
}
933+
927934
/**
928935
* Scales a proposed recipe voltage according to a provided Material working tier.
929936
*

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEInputBus.java

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
import com.cleanroommc.modularui.drawable.ItemDrawable;
5656
import com.cleanroommc.modularui.factory.PosGuiData;
5757
import com.cleanroommc.modularui.screen.ModularPanel;
58+
import com.cleanroommc.modularui.value.IntValue;
5859
import com.cleanroommc.modularui.value.sync.IntSyncValue;
60+
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
5961
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
6062
import com.cleanroommc.modularui.value.sync.SyncHandlers;
6163
import com.cleanroommc.modularui.widget.Widget;
@@ -77,6 +79,7 @@ public class MetaTileEntityMEInputBus extends MetaTileEntityAEHostableChannelPar
7779

7880
public static final String ITEM_BUFFER_TAG = "ItemSlots";
7981
public static final String WORKING_TAG = "WorkingEnabled";
82+
public static final String SYNC_HANDLER_NAME = "aeSync";
8083

8184
public final static int CONFIG_SIZE = 16;
8285
protected ExportOnlyAEItemList aeItemHandler;
@@ -217,9 +220,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
217220
final boolean isStocking = getAEItemHandler().isStocking();
218221
guiSyncManager.registerSlotGroup("extra_slot", 1);
219222

220-
final String syncHandlerName = "aeSync";
221223
AEItemSyncHandler syncHandler = new AEItemSyncHandler(getAEItemHandler(), this::markDirty, circuitInventory);
222-
guiSyncManager.syncValue(syncHandlerName, syncHandler);
224+
guiSyncManager.syncValue(SYNC_HANDLER_NAME, 0, syncHandler);
223225

224226
Grid configGrid = new Grid()
225227
.pos(7, 25)
@@ -229,7 +231,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
229231
.minRowHeight(18)
230232
.matrix(Grid.mapToMatrix((int) Math.sqrt(CONFIG_SIZE), CONFIG_SIZE,
231233
index -> new AEItemConfigSlot(isStocking, index, this::isAutoPull)
232-
.syncHandler(syncHandlerName)
234+
.syncHandler(SYNC_HANDLER_NAME, 0)
233235
.debugName("Index " + index)));
234236

235237
for (IWidget aeWidget : configGrid.getChildren()) {
@@ -255,7 +257,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
255257
.matrix(Grid.mapToMatrix((int) Math.sqrt(CONFIG_SIZE), CONFIG_SIZE,
256258
index -> new AEItemDisplaySlot(index)
257259
.background(GTGuiTextures.SLOT_DARK)
258-
.syncHandler(syncHandlerName)
260+
.syncHandler(SYNC_HANDLER_NAME, 0)
259261
.debugName("Index " + index))))
260262
.child(Flow.column()
261263
.pos(7 + 18 * 4, 25)
@@ -274,24 +276,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
274276
.height(18)
275277
.top(5)
276278
.right(7)
277-
.childIf(!isStocking, new ButtonWidget<>()
278-
.width(9)
279-
.height(18)
280-
.onMousePressed(mouseButton -> {
281-
syncHandler.modifyConfigAmounts((index, amount) -> Math.max(1, amount / 2));
282-
283-
return true;
284-
})
285-
.addTooltipLine(IKey.str("Click to divide all slots by 2"))) // TODO: lang
286-
.childIf(!isStocking, new ButtonWidget<>()
287-
.width(9)
288-
.height(18).onMousePressed(mouseButton -> {
289-
syncHandler.modifyConfigAmounts(
290-
(index, amount) -> GTUtility.safeCastLongToInt((long) amount * 2));
291-
292-
return true;
293-
})
294-
.addTooltipLine(IKey.str("Click to multiply all slots by 2"))) // TODO: lang
279+
.childIf(!isStocking, getMultiplierWidget(guiSyncManager))
295280
.child(getSettingWidget(guiSyncManager)));
296281
}
297282

@@ -343,6 +328,56 @@ protected int getSettingsPopupHeight() {
343328
return 33 + 14 + 5;
344329
}
345330

331+
protected Widget<?> getMultiplierWidget(PanelSyncManager syncManager) {
332+
IPanelHandler multiplierPopup = syncManager.panel("multiplier_panel", this::buildMultiplierPopup, true);
333+
334+
return new ButtonWidget<>()
335+
.onMousePressed(mouse -> {
336+
if (multiplierPopup.isPanelOpen()) {
337+
multiplierPopup.closePanel();
338+
} else {
339+
multiplierPopup.openPanel();
340+
}
341+
342+
return true;
343+
})
344+
.addTooltipLine(IKey.lang("gregtech.machine.me.multiplier.button"));
345+
// TODO button overlay
346+
}
347+
348+
protected ModularPanel buildMultiplierPopup(PanelSyncManager syncManager, IPanelHandler syncHandler) {
349+
AEItemSyncHandler aeSyncHandler = (AEItemSyncHandler) ((PanelSyncHandler) syncHandler).getSyncManager()
350+
.getSyncHandler(PanelSyncManager.makeSyncKey(SYNC_HANDLER_NAME, 0));
351+
IntValue multiplier = new IntValue(2);
352+
353+
return GTGuis.blankPopupPanel("multiplier", 100, 32)
354+
.child(new ButtonWidget<>()
355+
.onMousePressed(mouse -> {
356+
aeSyncHandler.modifyConfigAmounts(
357+
(index, amount) -> Math.max(1, amount / multiplier.getIntValue()));
358+
return true;
359+
})
360+
.left(5)
361+
.top(7)
362+
.overlay(IKey.str("÷")))
363+
.child(new TextFieldWidget()
364+
.alignX(0.5f)
365+
.top(5)
366+
.widthRel(0.5f)
367+
.height(18)
368+
.setNumbers(2, Integer.MAX_VALUE)
369+
.value(multiplier))
370+
.child(new ButtonWidget<>()
371+
.onMousePressed(mouse -> {
372+
aeSyncHandler.modifyConfigAmounts((index, amount) -> GTUtility
373+
.safeIntegerMultiplication(amount, multiplier.getIntValue()));
374+
return true;
375+
})
376+
.right(5)
377+
.top(7)
378+
.overlay(IKey.str("x")));
379+
}
380+
346381
protected Widget<?> getExtraButton() {
347382
return new Widget<>()
348383
.size(18);

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEInputHatch.java

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
import com.cleanroommc.modularui.drawable.ItemDrawable;
4949
import com.cleanroommc.modularui.factory.PosGuiData;
5050
import com.cleanroommc.modularui.screen.ModularPanel;
51+
import com.cleanroommc.modularui.value.IntValue;
5152
import com.cleanroommc.modularui.value.sync.IntSyncValue;
53+
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
5254
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
5355
import com.cleanroommc.modularui.widget.Widget;
5456
import com.cleanroommc.modularui.widgets.ButtonWidget;
@@ -67,6 +69,7 @@ public class MetaTileEntityMEInputHatch extends MetaTileEntityAEHostableChannelP
6769

6870
public static final String FLUID_BUFFER_TAG = "FluidTanks";
6971
public static final String WORKING_TAG = "WorkingEnabled";
72+
public static final String SYNC_HANDLER_NAME = "aeSync";
7073

7174
public final static int CONFIG_SIZE = 16;
7275
protected ExportOnlyAEFluidList aeFluidHandler;
@@ -177,9 +180,8 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
177180

178181
final boolean isStocking = getAEFluidHandler().isStocking();
179182

180-
final String syncHandlerName = "aeSync";
181183
AEFluidSyncHandler syncHandler = new AEFluidSyncHandler(getAEFluidHandler(), this::markDirty);
182-
guiSyncManager.syncValue(syncHandlerName, syncHandler);
184+
guiSyncManager.syncValue(SYNC_HANDLER_NAME, 0, syncHandler);
183185

184186
Grid configGrid = new Grid()
185187
.pos(7, 25)
@@ -189,7 +191,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
189191
.minRowHeight(18)
190192
.matrix(Grid.mapToMatrix((int) Math.sqrt(CONFIG_SIZE), CONFIG_SIZE,
191193
index -> new AEFluidConfigSlot(isStocking, index, this::isAutoPull)
192-
.syncHandler(syncHandlerName)
194+
.syncHandler(SYNC_HANDLER_NAME, 0)
193195
.debugName("Index " + index)));
194196

195197
for (IWidget aeWidget : configGrid.getChildren()) {
@@ -215,7 +217,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
215217
.matrix(Grid.mapToMatrix((int) Math.sqrt(CONFIG_SIZE), CONFIG_SIZE,
216218
index -> new AEFluidDisplaySlot(index)
217219
.background(GTGuiTextures.SLOT_DARK)
218-
.syncHandler(syncHandlerName)
220+
.syncHandler(SYNC_HANDLER_NAME, 0)
219221
.debugName("Index " + index))))
220222
.child(Flow.column()
221223
.pos(7 + 18 * 4, 25)
@@ -231,24 +233,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
231233
.height(18)
232234
.top(5)
233235
.right(7)
234-
.childIf(!isStocking, new ButtonWidget<>()
235-
.width(9)
236-
.height(18)
237-
.onMousePressed(mouseButton -> {
238-
syncHandler.modifyConfigAmounts((index, amount) -> Math.max(1, amount / 2));
239-
240-
return true;
241-
})
242-
.addTooltipLine(IKey.str("Click to divide all slots by 2"))) // TODO: lang
243-
.childIf(!isStocking, new ButtonWidget<>()
244-
.width(9)
245-
.height(18).onMousePressed(mouseButton -> {
246-
syncHandler.modifyConfigAmounts(
247-
(index, amount) -> GTUtility.safeCastLongToInt((long) amount * 2));
248-
249-
return true;
250-
})
251-
.addTooltipLine(IKey.str("Click to multiply all slots by 2"))) // TODO: lang
236+
.childIf(!isStocking, getMultiplierWidget(guiSyncManager))
252237
.child(getSettingWidget(guiSyncManager)));
253238
}
254239

@@ -301,6 +286,57 @@ protected int getSettingsPopupHeight() {
301286
return 33 + 14 + 5;
302287
}
303288

289+
protected Widget<?> getMultiplierWidget(PanelSyncManager syncManager) {
290+
IPanelHandler multiplierPopup = syncManager.panel("multiplier_panel", this::buildMultiplierPopup, true);
291+
292+
return new ButtonWidget<>()
293+
.onMousePressed(mouse -> {
294+
if (multiplierPopup.isPanelOpen()) {
295+
multiplierPopup.closePanel();
296+
} else {
297+
multiplierPopup.openPanel();
298+
}
299+
300+
return true;
301+
})
302+
.addTooltipLine(IKey.lang("gregtech.machine.me.multiplier.button"));
303+
// TODO button overlay
304+
}
305+
306+
protected ModularPanel buildMultiplierPopup(PanelSyncManager syncManager, IPanelHandler syncHandler) {
307+
AEFluidSyncHandler aeSyncHandler = (AEFluidSyncHandler) ((PanelSyncHandler) syncHandler).getSyncManager()
308+
.getSyncHandler(PanelSyncManager.makeSyncKey(SYNC_HANDLER_NAME, 0));
309+
IntValue multiplier = new IntValue(2);
310+
311+
return GTGuis.createPopupPanel("multiplier", 100, 35)
312+
.child(new ButtonWidget<>()
313+
.onMousePressed(mouse -> {
314+
aeSyncHandler.modifyConfigAmounts(
315+
(index, amount) -> Math.max(1, amount / multiplier.getIntValue()));
316+
return true;
317+
})
318+
.left(5)
319+
.top(7)
320+
.overlay(IKey.str("÷")))
321+
.child(new TextFieldWidget()
322+
.alignX(0.5f)
323+
.top(5)
324+
.widthRel(0.5f)
325+
.height(20)
326+
.setNumbers(2, Integer.MAX_VALUE)
327+
.setDefaultNumber(2)
328+
.value(multiplier))
329+
.child(new ButtonWidget<>()
330+
.onMousePressed(mouse -> {
331+
aeSyncHandler.modifyConfigAmounts((index, amount) -> GTUtility
332+
.safeIntegerMultiplication(amount, multiplier.getIntValue()));
333+
return true;
334+
})
335+
.right(5)
336+
.top(7)
337+
.overlay(IKey.str("x")));
338+
}
339+
304340
protected Widget<?> getExtraButton() {
305341
return new Widget<>()
306342
.size(18);

src/main/resources/assets/gregtech/lang/en_us.lang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5530,6 +5530,7 @@ gregtech.machine.me.settings.button=ME Settings
55305530
gregtech.machine.me.settings.refresh_rate=Refresh rate:
55315531
gregtech.machine.me.settings.minimum=Minimum stack size:
55325532
gregtech.machine.me.settings.minimum.tooltip=Set to 0 to disable the minimum stack size filtering
5533+
gregtech.machine.me.multiplier.button=Config multiplier
55335534

55345535
# Universal tooltips
55355536
gregtech.universal.tooltip.voltage_in=§aVoltage IN: §f%,d EU/t (%s§f)

0 commit comments

Comments
 (0)