Skip to content

Commit

Permalink
internal: redui cleanup and pre/post draw hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Nov 21, 2022
1 parent 8dfe664 commit 0559591
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 68 deletions.
15 changes: 14 additions & 1 deletion src/core/scala/mrtjp/core/vec/Rect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package mrtjp.core.vec

import mrtjp.core.vec.Rect.zeroRect

case class Rect(origin:Point, size:Size)
{
def this(x:Int, y:Int, width:Int, height:Int) = this(Point(x, y), Size(width, height))
Expand Down Expand Up @@ -37,7 +39,18 @@ case class Rect(origin:Point, size:Size)
def intersects(r:Rect) = contains(r.origin) || contains(r.maxPoint) || r.contains(origin) || r.contains(maxPoint)

def enclose(p:Point) = new Rect(Point(math.min(x, p.x), math.min(y, p.y)), Point(math.max(maxX, p.x), math.max(maxY, p.y)))
def union(r:Rect) = new Rect(Point(math.min(x, r.x), math.min(y, r.y)), Point(math.max(maxX, r.maxX), math.max(maxY, r.maxY)))

def union(r:Rect):Rect = {
if (this == zeroRect) return r
if (r == zeroRect) return this
new Rect(Point(math.min(x, r.x), math.min(y, r.y)), Point(math.max(maxX, r.maxX), math.max(maxY, r.maxY)))
}

def trap(r:Rect) = {
val dx = (if (r.x < x) x-r.x else 0) + (if (r.maxX > maxX) maxX-r.maxX else 0)
val dy = (if (r.y < y) y-r.y else 0) + (if (r.maxY > maxY) maxY-r.maxY else 0)
new Rect(r.x + dx, r.y + dy, r.width, r.height)
}
}

object Rect
Expand Down
14 changes: 5 additions & 9 deletions src/core/scala/mrtjp/projectred/redui/AbstractGuiNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ public abstract class AbstractGuiNode implements RedUINode {
private RedUIRootNode root;
private RedUINode parent;

private Point pos = Point.zeroPoint();
private Size size = Size.zeroSize();
private Rect frame = new Rect(pos, size);
private Rect frame = new Rect(Point.zeroPoint(), Size.zeroSize());
private double zPos = 0;

private boolean isHidden = false;

private final List<RedUINode> children = new LinkedList<>();

public void setPosition(int x, int y) {
pos = new Point(x, y);
frame = new Rect(pos, size);
frame = new Rect(new Point(x, y), frame.size());
}

public void setSize(int width, int height) {
size = new Size(width, height);
frame = new Rect(pos, size);
frame = new Rect(frame.origin(), new Size(width, height));
}

public void setHidden(boolean isHidden) {
Expand Down Expand Up @@ -68,7 +64,7 @@ public Rect getFrame() {

@Override
public Point getPosition() {
return pos;
return frame.origin();
}

@Override
Expand All @@ -78,7 +74,7 @@ public double getZPosition() {

@Override
public double getRelativeZPosition() {
return getParent().getZPosition() - zPos;
return zPos - getParent().getZPosition();
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/core/scala/mrtjp/projectred/redui/ButtonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
public class ButtonNode extends AbstractGuiNode {

private Runnable clickFunction = () -> { };
private Consumer<List<? extends ITextProperties>> tooltipBuilder = c -> { };
private Consumer<List<ITextProperties>> tooltipBuilder = c -> { };

private String buttonText = "";

public void setClickFunction(Runnable clickFunction) {
this.clickFunction = clickFunction;
}

public void setTooltipBuilder(Consumer<List<? extends ITextProperties>> tooltipBuilder) {
public void setTooltipBuilder(Consumer<List<ITextProperties>> tooltipBuilder) {
this.tooltipBuilder = tooltipBuilder;
}

Expand Down Expand Up @@ -62,7 +62,7 @@ public void drawFront(MatrixStack stack, Point mouse, float partialFrame) {
if (!isFirstHit(mouse))
return;

List<? extends ITextProperties> tooltip = new LinkedList<>();
List<ITextProperties> tooltip = new LinkedList<>();
tooltipBuilder.accept(tooltip);

// Draw tooltip in screen-space to allow it to force-fit on screen
Expand Down
Loading

0 comments on commit 0559591

Please sign in to comment.