Skip to content

Commit

Permalink
feat: fabrication v2
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Nov 27, 2023
2 parents 59590bc + 343af6f commit be594f7
Show file tree
Hide file tree
Showing 273 changed files with 16,111 additions and 370 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import java.time.Instant
plugins {
id 'java'
id 'maven-publish'
id 'com.github.johnrengelman.shadow'
}

// Get mod version from CI, else suffix a timestamp (calculated here bc timestamp can change if done separately in each subproject)
Expand Down Expand Up @@ -61,6 +62,11 @@ subprojects { p ->
}
}

// Defined explicitly so publishing can access shadowJar property
project(':fabrication') {
apply plugin: 'com.github.johnrengelman.shadow'
}

publishing {
repositories {
maven {
Expand All @@ -76,6 +82,7 @@ publishing {
artifact project(':core').jar
artifact project(':expansion').jar
artifact project(':exploration').jar
artifact project(':fabrication').shadowJar
artifact project(':illumination').jar
artifact project(':integration').jar
artifact project(':transmission').jar
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/mrtjp/projectred/lib/Rect.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ public Rect union(Rect rect) {
return new Rect(x, y, w, h);
}

public Rect expand(int x, int y) {
int dw = width() + x < 0 ? -width() : x;
int dh = height() + y < 0 ? -height() : y;
return new Rect(origin.subtract(dw / 2, dh / 2), new Size(width() + dw, height() + dh));
}

public Rect trap(Rect rect) {
int dx = (rect.x() < x() ? x() - rect.x() : 0) + (rect.maxX() > maxX() ? rect.maxX() - maxX() : 0);
int dy = (rect.y() < y() ? y() - rect.y() : 0) + (rect.maxY() > maxY() ? rect.maxY() - maxY() : 0);
int dx = (rect.x() < x() ? x() - rect.x() : 0) - (rect.maxX() > maxX() ? rect.maxX() - maxX() : 0);
int dy = (rect.y() < y() ? y() - rect.y() : 0) - (rect.maxY() > maxY() ? rect.maxY() - maxY() : 0);
return new Rect(rect.x() + dx, rect.y() + dy, rect.width(), rect.height());
}

Expand Down
20 changes: 19 additions & 1 deletion core/src/main/java/mrtjp/projectred/redui/ButtonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

import static net.minecraft.client.gui.GuiComponent.drawCenteredString;

Expand All @@ -15,6 +16,8 @@ public class ButtonNode extends AbstractButtonNode {

private Runnable clickFunction = () -> { };
private Consumer<List<Component>> tooltipBuilder = c -> { };
private Supplier<Boolean> isSelectedFunction = () -> false;
private Supplier<Boolean> isEnabledFunction = () -> true;

private String buttonText = "";

Expand All @@ -26,6 +29,14 @@ public void setTooltipBuilder(Consumer<List<Component>> tooltipBuilder) {
this.tooltipBuilder = tooltipBuilder;
}

public void setIsSelectedFunction(Supplier<Boolean> isSelectedFunction) {
this.isSelectedFunction = isSelectedFunction;
}

public void setIsEnabledFunction(Supplier<Boolean> isEnabledFunction) {
this.isEnabledFunction = isEnabledFunction;
}

public void setButtonText(String buttonText) {
this.buttonText = buttonText;
}
Expand All @@ -37,7 +48,14 @@ protected void onButtonClicked() {

@Override
protected boolean isButtonDisabled() {
return false;
return !isEnabledFunction.get();
}

@Override
protected int getButtonState(boolean mouseover) {
if (isSelectedFunction.get())
return BUTTON_STATE_HIGHLIGHT;
return super.getButtonState(mouseover);
}

@Override
Expand Down
59 changes: 59 additions & 0 deletions core/src/main/java/mrtjp/projectred/redui/ItemStackNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package mrtjp.projectred.redui;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import mrtjp.projectred.lib.Point;
import mrtjp.projectred.lib.Rect;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;

import java.util.List;

import static net.minecraft.client.gui.GuiComponent.fillGradient;

public class ItemStackNode extends AbstractGuiNode {

private ItemStack itemStack;

public ItemStackNode(ItemStack itemStack) {
this.itemStack = itemStack;
setSize(16, 16);
}

public ItemStackNode() {
this(ItemStack.EMPTY);
}

public void setItemStack(ItemStack itemStack) {
this.itemStack = itemStack;
}

@Override
public void drawBack(PoseStack stack, Point mouse, float partialFrame) {

// Would be nice if renderGuiItem can take the matrix stack...
Point screenPos = convertParentPointToScreen(getPosition());
getRoot().getItemRenderer().renderGuiItem(itemStack, screenPos.x, screenPos.y);

if (isFirstHit(mouse)) {
int slotColor = -2130706433;
RenderSystem.disableDepthTest();
RenderSystem.colorMask(true, true, true, false);
Rect frame = getFrame();
fillGradient(stack, frame.x(), frame.y(), frame.x() + frame.width(), frame.y() + frame.height(), slotColor, slotColor, 0);
RenderSystem.colorMask(true, true, true, true);
RenderSystem.enableDepthTest();
}
}

@Override
public void drawFront(PoseStack stack, Point mouse, float partialFrame) {
if (isFirstHit(mouse)) {
Minecraft minecraft = getRoot().getMinecraft();
List<Component> tooltip = itemStack.getTooltipLines(minecraft.player, minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL);
renderTooltip(stack, mouse, tooltip);
}
}
}
39 changes: 27 additions & 12 deletions core/src/main/java/mrtjp/projectred/redui/RedUINode.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ default Point convertPointTo(Point p, RedUINode to) {
return p.add(difference);
}

default Point convertPointToParent(Point p) {
return p.add(getPosition());
}

default Point convertPointFromParent(Point p) {
return p.subtract(getPosition());
}

/**
* Convert a given point <code>p</code> in this node's coordinate system to screen space
*
Expand Down Expand Up @@ -270,6 +278,14 @@ default Point getScreenOffset() {
*/
default Rect convertRectTo(Rect r, RedUINode to) { return new Rect(convertPointTo(r.origin, to), r.size); }

default Rect convertRectToParent(Rect r) {
return new Rect(convertPointToParent(r.origin), r.size);
}

default Rect convertRectFromParent(Rect r) {
return new Rect(convertPointFromParent(r.origin), r.size);
}

/**
* Converts a rectangle from this node's coordinate system to screen space
*
Expand Down Expand Up @@ -361,26 +377,25 @@ default Rect calculateGL11Frame() {
}

/**
* Calculate a frame encompassing all frames in the entire subtree. Hidden nodes and all its
* descendants are excluded.
* Combines frame encompassing all children with frame of this node.
*
* @return Rect in the coordinate system of this node's parent
*/
default Rect calculateAccumulatedFrame() {
Rect screenSpaceFrame = getSubTree(n -> !n.isHidden()).stream()
.map(n -> n.convertParentRectToScreen(n.getFrame()))
.reduce(Rect.ZERO, Rect::union);
return convertScreenRectToParent(screenSpaceFrame);
return getFrame().union(calculateChildrenFrame());
}

/**
* Calculate a frame encompassing all visible children and their subtrees.
*
* @return Rect in the coordinate system of this node's parent
*/
default Rect calculateChildrenFrame() {
List<RedUINode> subTree = getSubTree(n -> !n.isHidden());
subTree.remove(0); // drop this node

Rect screenSpaceFrame = subTree.stream()
.map(n -> n.convertParentRectToScreen(n.getFrame()))
Rect childrenFrame = getOurChildren().stream()
.filter(n -> !n.isHidden())
.map(RedUINode::calculateAccumulatedFrame)
.reduce(Rect.ZERO, Rect::union);
return convertScreenRectToParent(screenSpaceFrame);
return convertRectToParent(childrenFrame);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.client.gui.Font;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FormattedCharSequence;

import javax.annotation.Nullable;
import java.util.List;
Expand Down
19 changes: 6 additions & 13 deletions core/src/main/java/mrtjp/projectred/redui/ScrollBarNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public abstract class ScrollBarNode extends AbstractGuiNode {

private final ScrollAxis axis;

private Point lastDragPosition = Point.ZERO;
private Point initialClickPosition = Point.ZERO;
private Rect initialSliderFrame = new Rect(0, 0, 0, 0);
private boolean isDraggingSlider = false;
private Rect sliderFrame = new Rect(0, 0, 0, 0);

Expand All @@ -34,19 +35,13 @@ public void drawBack(PoseStack stack, Point mouse, float partialFrame) {
drawSlider(stack, sliderFrame);
}

@Override
public void drawFront(PoseStack stack, Point mouse, float partialFrame) {
if (!isFirstHit(mouse)) return;

renderTooltip(stack, mouse, Collections.singletonList(new TextComponent("Scroll (" + scrollPercentage + ")")));
}

@Override
public boolean mouseClicked(Point p, int glfwMouseButton, boolean consumed) {
if (isFirstHit(p)) {
if (sliderFrame.contains(p)) {
isDraggingSlider = true;
lastDragPosition = p;
initialClickPosition = p;
initialSliderFrame = sliderFrame;
return true;
}

Expand All @@ -71,10 +66,8 @@ public boolean mouseDragged(Point p, int glfwMouseButton, long timeHeld, boolean
if (!isDraggingSlider)
return false;

Point delta = p.subtract(lastDragPosition).multiply(axis.vec);
lastDragPosition = p;

Rect targetSliderFrame = new Rect(sliderFrame.origin.add(delta), sliderFrame.size);
Point delta = p.subtract(initialClickPosition).multiply(axis.vec);
Rect targetSliderFrame = new Rect(initialSliderFrame.origin.add(delta), sliderFrame.size);
sliderFrame = getFrame().trap(targetSliderFrame);
recalcScrollPercentage();

Expand Down

0 comments on commit be594f7

Please sign in to comment.