Skip to content

Commit f6bf593

Browse files
ResearchHead logic and more line drawing
1 parent 3200ec7 commit f6bf593

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

src/main/java/com/portingdeadmods/researchd/client/screens/graph/ResearchNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, flo
108108
Point outputConnPoint = outputs.get(i).getConnectionPoint();
109109
children.get(i).getInputs().forEach(input -> {
110110
Point inputConnPoint = input.getConnectionPoint();
111-
ResearchLine.connectNodes(outputConnPoint, inputConnPoint).render(guiGraphics);
111+
ResearchLine.createLConnection(outputConnPoint, inputConnPoint, true).render(guiGraphics);
112112
});
113113
}
114114
}

src/main/java/com/portingdeadmods/researchd/client/screens/lines/ResearchHead.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.portingdeadmods.researchd.client.screens.lines;
22

3+
import com.portingdeadmods.researchd.api.research.Research;
34
import com.portingdeadmods.researchd.client.screens.ResearchScreenWidget;
45
import com.portingdeadmods.researchd.client.screens.graph.ResearchNode;
56
import com.portingdeadmods.researchd.utils.UniqueArray;
67
import net.minecraft.client.gui.GuiGraphics;
8+
import net.minecraft.resources.ResourceKey;
79

810
import java.awt.*;
911
import java.util.HashSet;
@@ -14,17 +16,27 @@ public class ResearchHead {
1416
private int x;
1517
private int y;
1618
private final boolean isInput;
19+
private final ResearchNode research;
1720

1821
/**
1922
*
2023
* @param x Should be based on the x position of the node, split evenly if there are multiple heads
2124
* @param y Should be the y position of the node <br>(+ Node height if it's output)
2225
* @param isInput Should be true if it's an input head, false if it's an output head
26+
* @param research {@link ResearchNode} the research this head is linked to. If the head is made through {@link #inputsOf(ResearchNode)} it will be linked to each of the children of the node, and vice versa
2327
*/
28+
public ResearchHead(int x, int y, boolean isInput, ResearchNode research) {
29+
this.x = x;
30+
this.y = y;
31+
this.isInput = isInput;
32+
this.research = research;
33+
}
34+
2435
public ResearchHead(int x, int y, boolean isInput) {
2536
this.x = x;
2637
this.y = y;
2738
this.isInput = isInput;
39+
this.research = null;
2840
}
2941

3042
public int getX() { return this.x; }
@@ -60,7 +72,8 @@ public void render(GuiGraphics graphics) {
6072
*/
6173
public static UniqueArray<ResearchHead> inputsOf(ResearchNode node) {
6274
UniqueArray<ResearchHead> positions = new UniqueArray<>();
63-
int parentCount = node.getParents().size();
75+
UniqueArray<ResearchNode> parents = node.getParents();
76+
int parentCount = parents.size();
6477
int width = ResearchScreenWidget.PANEL_WIDTH; // Node width
6578

6679
if (parentCount == 0) return positions;
@@ -69,7 +82,7 @@ public static UniqueArray<ResearchHead> inputsOf(ResearchNode node) {
6982
if (parentCount == 1) {
7083
int x = node.getX() + width / 2;
7184
int y = node.getY();
72-
positions.add(new ResearchHead(x, y, true));
85+
positions.add(new ResearchHead(x, y, true, parents.get(0)));
7386
return positions;
7487
}
7588

@@ -78,7 +91,11 @@ public static UniqueArray<ResearchHead> inputsOf(ResearchNode node) {
7891
startingX -= ((parentCount - 1) / 2) * 3; // 3 px per pair of heads
7992

8093
for (int i = 0; i < parentCount; i++) {
81-
positions.addLast(new ResearchHead(startingX + i * 3, node.getY(), true));
94+
positions.addLast(new ResearchHead(startingX + i * 3,
95+
node.getY(),
96+
true,
97+
parents.get(i))
98+
);
8299
}
83100

84101
return positions;
@@ -90,7 +107,8 @@ public static UniqueArray<ResearchHead> inputsOf(ResearchNode node) {
90107
*/
91108
public static UniqueArray<ResearchHead> outputsOf(ResearchNode node) {
92109
UniqueArray<ResearchHead> positions = new UniqueArray<>();
93-
int childCount = node.getChildren().size();
110+
UniqueArray<ResearchNode> children = node.getChildren();
111+
int childCount = children.size();
94112
int width = ResearchScreenWidget.PANEL_WIDTH; // Node width
95113

96114
if (childCount == 0) return positions;
@@ -99,7 +117,7 @@ public static UniqueArray<ResearchHead> outputsOf(ResearchNode node) {
99117
if (childCount == 1) {
100118
int x = node.getX() + width / 2;
101119
int y = node.getY() + ResearchScreenWidget.PANEL_HEIGHT - 1;
102-
positions.add(new ResearchHead(x, y, false));
120+
positions.add(new ResearchHead(x, y, false, children.get(0)));
103121
return positions;
104122
}
105123

@@ -108,7 +126,12 @@ public static UniqueArray<ResearchHead> outputsOf(ResearchNode node) {
108126
startingX -= ((childCount - 1) / 2) * 3; // 2 px per pair of heads
109127

110128
for (int i = 0; i < childCount; i++) {
111-
positions.addLast(new ResearchHead(startingX + i * 3, node.getY() + ResearchScreenWidget.PANEL_HEIGHT - 1, false));
129+
positions.addLast(new ResearchHead(
130+
startingX + i * 3,
131+
node.getY() + ResearchScreenWidget.PANEL_HEIGHT - 1,
132+
false,
133+
children.get(i)
134+
));
112135
}
113136

114137
return positions;

src/main/java/com/portingdeadmods/researchd/client/screens/lines/ResearchLine.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,47 @@ public ResearchLine vertical(int dy) {
6969
* Builds a simple L-shaped connection between two points.
7070
* Goes vertical first, then horizontal.
7171
*/
72-
public static ResearchLine createLConnection(Point start, Point end) {
73-
return ResearchLine.start(start)
74-
.then(start.x, end.y)
75-
.then(end);
72+
public static ResearchLine createLConnection(Point start, Point end, boolean verticalFirst) {
73+
if (verticalFirst) {
74+
return ResearchLine.start(start)
75+
.then(start.x, end.y)
76+
.then(end);
77+
} else {
78+
return ResearchLine.start(start)
79+
.then(end.x, start.y)
80+
.then(end);
81+
}
82+
}
83+
84+
/**
85+
* The same as the L connection but with a vertical offset.
86+
* Goes vertical first (verticalOffset), then horizontal, then the remaining vertical distance.
87+
*/
88+
public static ResearchLine createSConnection(Point start, Point end, int verticalOffset) {
89+
if (verticalOffset < 0 || verticalOffset > Math.abs(start.y - end.y)) {
90+
throw new IllegalArgumentException("Vertical offset must be between 0 and the vertical distance between the points");
91+
}
92+
93+
if (start.y < end.y) {
94+
return ResearchLine.start(start)
95+
.then(start.x, start.y + verticalOffset)
96+
.then(end.x, start.y + verticalOffset)
97+
.then(end);
98+
} else {
99+
return ResearchLine.start(start)
100+
.then(start.x, start.y - verticalOffset)
101+
.then(end.x, start.y - verticalOffset)
102+
.then(end);
103+
}
76104
}
77105

78106
/**
79107
* Builds a connection between input and output points of research nodes
80108
* with an L-shaped path.
81109
*/
82-
public static ResearchLine connectNodes(Point outputPoint, Point inputPoint) {
110+
public static ResearchLine connectNodes(Point outputPoint, Point inputPoint, boolean verticalFirst) {
83111
// Creates path from output -> input with a vertical segment followed by horizontal
84-
return createLConnection(outputPoint, inputPoint);
112+
return createLConnection(outputPoint, inputPoint, verticalFirst);
85113
}
86114

87115
public void render(@NotNull GuiGraphics guiGraphics) {

0 commit comments

Comments
 (0)