Skip to content

Commit

Permalink
move singularity into the upgrade panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Mari023 committed Jul 1, 2024
1 parent 8d9a156 commit 067a6a3
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- fix upgrade panel tooltip being larger than the panel
- move singularity into the upgrade panel
2 changes: 0 additions & 2 deletions src/main/java/de/mari_023/ae2wtlib/terminal/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public record Icon(int x, int y, int width, int height, Texture texture) {
public static final Icon UPGRADE_BACKGROUND_SCROLLING_MIDDLE = new Icon(48, 85, 29, 18);
public static final Icon UPGRADE_BACKGROUND_SCROLLING_BOTTOM = new Icon(48, 103, 29, 25);

public static final Icon SINGULARITY_BACKGROUND = new Icon(100, 105, 28, 23);

private Icon(int x, int y) {
this(x, y, 16, 16);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

Expand All @@ -10,7 +11,9 @@
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

import appeng.api.upgrades.IUpgradeInventory;
import appeng.api.upgrades.IUpgradeableObject;
import appeng.api.upgrades.Upgrades;
import appeng.client.Point;
Expand All @@ -19,13 +22,16 @@
import appeng.core.localization.GuiText;
import appeng.menu.slot.AppEngSlot;

import de.mari_023.ae2wtlib.AE2wtlibItems;

public class ScrollingUpgradesPanel implements ICompositeWidget {
private static final int SLOT_SIZE = 18;
private static final int PADDING = 5;
private static final int MAX_ROWS = 8;
private static final int SCROLLBAR_WIDTH = 5;

private final List<Slot> slots;
private final Supplier<IUpgradeInventory> upgrades;

// The screen origin in window space (used to layout slots)
private Point screenOrigin = Point.ZERO;
Expand All @@ -39,15 +45,29 @@ public class ScrollingUpgradesPanel implements ICompositeWidget {

private final List<Component> tooltips;

public ScrollingUpgradesPanel(List<Slot> slots, IUpgradeableObject upgradeableObject, WidgetContainer widgets) {
public ScrollingUpgradesPanel(List<Slot> slots, IUpgradeableObject upgradeableObject, WidgetContainer widgets,
Supplier<IUpgradeInventory> upgrades) {
this.slots = slots;
this.upgrades = upgrades;
tooltips = Upgrades.getTooltipLinesForMachine(upgradeableObject.getUpgrades().getUpgradableItem());
tooltips.addFirst(GuiText.CompatibleUpgrades.text());

scrollbar = widgets.addScrollBar("upgradeScrollbar", Scrollbar.SMALL);
// The scrollbar ranges from 0 to the number of rows not visible
scrollbar.setRange(0, getUpgradeSlotCount() - getVisibleSlotCount(), 1);
scrollbar.setCaptureMouseWheel(false);
setScrollbarRange();
}

private boolean singularitySlotHidden() {
return isDisabledSlotEmpty((AppEngSlot) slots.getFirst())
&& !upgrades.get().isInstalled(AE2wtlibItems.QUANTUM_BRIDGE_CARD);
}

private boolean isDisabledSlotEmpty(AppEngSlot slot) {
boolean enabled = slot.isSlotEnabled();
slot.setSlotEnabled(true);
ItemStack stack = slot.getItem();
slot.setSlotEnabled(enabled);
return stack.isEmpty();
}

@Override
Expand All @@ -62,7 +82,7 @@ public void setPosition(Point position) {
@Override
public void setSize(int width, int height) {
maxRows = (height - PADDING * 2) / SLOT_SIZE;
scrollbar.setRange(0, getUpgradeSlotCount() - getVisibleSlotCount(), 1);
setScrollbarRange();
scrollbar.setVisible(scrolling());
}

Expand All @@ -86,19 +106,22 @@ public void updateBeforeRender() {
int slotOriginX = x;
int slotOriginY = y + PADDING;
int currentFirstSlot = scrollbar.getCurrentScroll();
setScrollbarRange();

for (int i = 0; i < slots.size(); i++) {
Slot s = slots.get(i);
int i = 0;
for (Slot s : slots) {
if (!(s instanceof AppEngSlot slot))
continue;

if (currentFirstSlot <= i && currentFirstSlot + maxRows > i) {
slot.setSlotEnabled(true);
} else {
slot.setSlotEnabled(false);
if (s == slots.getFirst() && singularitySlotHidden()) {
((AppEngSlot) slots.getFirst()).setSlotEnabled(false);
continue;
}

slot.setSlotEnabled(currentFirstSlot <= i && currentFirstSlot + maxRows > i);

i++;

if (!slot.isActive()) {
continue;
}
Expand Down Expand Up @@ -144,20 +167,23 @@ public Tooltip getTooltip(int mouseX, int mouseY) {
return new Tooltip(tooltips);
}

/**
* We need this function since the cell workbench can dynamically change how many upgrade slots are active based on
* the cell in the workbench.
*/
private int getUpgradeSlotCount() {
int count = 0;
for (Slot slot : slots) {
if (slot instanceof AppEngSlot) {
count++;
}
}
if (singularitySlotHidden())
count--;
return count;
}

private void setScrollbarRange() {
// The scrollbar ranges from 0 to the number of rows not visible
scrollbar.setRange(0, getUpgradeSlotCount() - getVisibleSlotCount(), 1);
}

private int getVisibleSlotCount() {
return Math.min(maxRows, getUpgradeSlotCount());
}
Expand Down
56 changes: 0 additions & 56 deletions src/main/java/de/mari_023/ae2wtlib/terminal/SingularityPanel.java

This file was deleted.

6 changes: 1 addition & 5 deletions src/main/java/de/mari_023/ae2wtlib/wat/WATScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import appeng.client.gui.me.patternaccess.PatternAccessTermScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.client.gui.widgets.ToolboxPanel;
import appeng.menu.SlotSemantics;

import de.mari_023.ae2wtlib.terminal.ScrollingUpgradesPanel;
import de.mari_023.ae2wtlib.terminal.WTMenuHost;
import de.mari_023.ae2wtlib.wut.IUniversalTerminalCapable;

Expand All @@ -18,11 +16,9 @@ public WATScreen(WATMenu container, Inventory playerInventory, Component title,
if (getMenu().isWUT())
addToLeftToolbar(cycleTerminalButton());

widgets.add("scrollingUpgrades",
new ScrollingUpgradesPanel(menu.getSlots(SlotSemantics.UPGRADE), menu.getHost(), widgets));
addUpgradePanel(widgets, getMenu());
if (getMenu().getToolbox().isPresent())
widgets.add("toolbox", new ToolboxPanel(style, getMenu().getToolbox().getName()));
addSingularityPanel(widgets, getMenu());
}

@Override
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/de/mari_023/ae2wtlib/wct/WCTScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import appeng.client.gui.me.items.CraftingTermScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.menu.SlotSemantics;

import de.mari_023.ae2wtlib.TextConstants;
import de.mari_023.ae2wtlib.terminal.*;
Expand Down Expand Up @@ -38,10 +37,7 @@ public WCTScreen(WCTMenu container, Inventory playerInventory, Component title,
trashButton.setMessage(TextConstants.TRASH);

widgets.add("player", new PlayerEntityWidget(Objects.requireNonNull(Minecraft.getInstance().player)));
addSingularityPanel(widgets, getMenu());

widgets.add("scrollingUpgrades",
new ScrollingUpgradesPanel(menu.getSlots(SlotSemantics.UPGRADE), menu.getHost(), widgets));
addUpgradePanel(widgets, getMenu());
}

private void setMagnetMode() {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/de/mari_023/ae2wtlib/wet/WETScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import appeng.client.gui.me.items.PatternEncodingTermScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.menu.SlotSemantics;

import de.mari_023.ae2wtlib.terminal.ScrollingUpgradesPanel;
import de.mari_023.ae2wtlib.terminal.WTMenuHost;
import de.mari_023.ae2wtlib.wut.IUniversalTerminalCapable;

Expand All @@ -16,9 +14,7 @@ public WETScreen(WETMenu container, Inventory playerInventory, Component title,
super(container, playerInventory, title, style);
if (getMenu().isWUT())
addToLeftToolbar(cycleTerminalButton());
addSingularityPanel(widgets, getMenu());
widgets.add("scrollingUpgrades",
new ScrollingUpgradesPanel(menu.getSlots(SlotSemantics.UPGRADE), menu.getHost(), widgets));
addUpgradePanel(widgets, getMenu());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.mari_023.ae2wtlib.wut;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.Contract;
Expand All @@ -10,13 +11,13 @@
import appeng.client.gui.WidgetContainer;
import appeng.core.network.serverbound.HotkeyPacket;
import appeng.menu.AEBaseMenu;
import appeng.menu.slot.AppEngSlot;
import appeng.menu.SlotSemantics;

import de.mari_023.ae2wtlib.AE2wtlibSlotSemantics;
import de.mari_023.ae2wtlib.TextConstants;
import de.mari_023.ae2wtlib.networking.CycleTerminalPacket;
import de.mari_023.ae2wtlib.terminal.IconButton;
import de.mari_023.ae2wtlib.terminal.SingularityPanel;
import de.mari_023.ae2wtlib.terminal.ScrollingUpgradesPanel;
import de.mari_023.ae2wtlib.terminal.WTMenuHost;

public interface IUniversalTerminalCapable {
Expand Down Expand Up @@ -59,14 +60,15 @@ default IconButton cycleTerminalButton() {
}

/**
* creates and adds the background for the singularity
* creates and adds the upgrade panel
*
* @param widgets the WidgetContainer where the widget will be added
* @param menu the menu corresponding to this screen
*/
default void addSingularityPanel(WidgetContainer widgets, AEBaseMenu menu) {
widgets.add("singularity",
new SingularityPanel((AppEngSlot) menu.getSlots(AE2wtlibSlotSemantics.SINGULARITY).getFirst(),
() -> getHost().getUpgrades()));
default void addUpgradePanel(WidgetContainer widgets, AEBaseMenu menu) {
var upgrades = new ArrayList<>(menu.getSlots(AE2wtlibSlotSemantics.SINGULARITY));
upgrades.addAll(menu.getSlots(SlotSemantics.UPGRADE));
widgets.add("scrollingUpgrades",
new ScrollingUpgradesPanel(upgrades, getHost(), widgets, () -> getHost().getUpgrades()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
},
"upgradeScrollbar": {
"right": -17,
"top": 5,
"top": 6,
"height": 34
},
"upgrades": {
"left": -9999,
"top": -9999
},
"singularity": {
"left": 2,
"top": -21
}
}
}

0 comments on commit 067a6a3

Please sign in to comment.