Skip to content

Commit

Permalink
added chained scissor support
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat committed Jul 12, 2023
1 parent 1f0d9f7 commit 7e5335a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import java.awt.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Stack;

public class Renderer2D {
private static final Stack<ScissorBox> scissorStack = new Stack<>();

/**
* @param pos top left corner of the rectangle
* @param size size of the rectangle (edge is contained within)
Expand Down Expand Up @@ -98,15 +100,37 @@ public static void drawLines(Collection<Vector2D> points, Color color) {
renderer.ifPresent(renderer2DInterface -> renderer2DInterface.drawLines(points, color));
}

//TODO: fix scissor inside each other
public static void enableScissor(double x, double y, double w, double h) {
ScissorBox box;
if (scissorStack.isEmpty()) box = new ScissorBox(x, y, w, h);
else {
ScissorBox prev = scissorStack.peek();
double bx = Math.max(prev.x, x), by = Math.max(prev.y, y);
box = new ScissorBox(bx, by,
Math.min(x + w, prev.x + prev.w) - bx,
Math.min(y + h, prev.y + prev.h) - by
);
}
scissorStack.push(box);
setScissor(box);
}

private static void setScissor(ScissorBox box) {
Optional<Interface> renderer = Interface.get();
renderer.ifPresent(renderer2DInterface -> renderer2DInterface.enableScissor(x, y, w, h));
if (!renderer.isPresent()) return;
if (box == null) renderer.get().disableScissor();
else renderer.get().enableScissor(box.x, box.y, box.w, box.h);
}

public static void endFrame() {
setScissor(null);
scissorStack.clear();
}

public static void disableScissor() {
Optional<Interface> renderer = Interface.get();
renderer.ifPresent(Interface::disableScissor);
if (!scissorStack.isEmpty()) scissorStack.pop();
if (scissorStack.isEmpty()) setScissor(null);
else setScissor(scissorStack.peek());
}

public interface Interface extends FunctionHolder {
Expand All @@ -126,4 +150,15 @@ static Optional<Interface> get() {

void disableScissor();
}

private static class ScissorBox {
public double x, y, w, h;

public ScissorBox(double x, double y, double w, double h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void onMouseScroll(Vector2D mousePos, int delta) {

public final void drawScreen(Vector2D mouse, float partialTicks) {
render(mouse, partialTicks);
Renderer2D.endFrame();
}

public void render(Vector2D mouse, float partialTicks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ public class ScrollableList<I extends ScrollableListItem<I>> extends Component i
public Div content;

public ScrollableList() {
topCover = new Div(Vector2D.ZERO, new Vector2D(1, 0));
topCover = new Div(new Vector2D(0,0), new Vector2D(1, 0));
topCover.backgroundColor = backgroundColor;
passPositionTo(topCover, PERCENT.SIZE_X, Anchor.TOP_LEFT);

titleComponent = new TextRectangle(
Vector2D.ZERO,
new Vector2D(0,0),
new Vector2D(1, 1),
"", null, Color.WHITE
);
topCover.addChild(titleComponent, PERCENT.SIZE);

bottomCover = new Div(Vector2D.ZERO, new Vector2D(1, 0));
bottomCover = new Div(new Vector2D(0,0), new Vector2D(1, 0));
bottomCover.backgroundColor = backgroundColor;
passPositionTo(bottomCover, PERCENT.SIZE_X, Anchor.BOTTOM_LEFT);

content = new Div(Vector2D.ZERO, Vector2D.ZERO);
content = new Div(new Vector2D(0,0), new Vector2D(0,0));
stretchYBetween(content, topCover, bottomCover);

scrollBar = new ScrollBar<>(this);
Expand Down Expand Up @@ -70,8 +70,12 @@ public void render(Vector2D mouse) {
double itemWidth = getDisplayedSize().getX() - 2;
if (shouldRenderScrollbar()) itemWidth -= scrollBar.barWidth - 1;

Renderer2D.enableScissor(content.getDisplayedPos().getX(), content.getDisplayedPos().getY(),
content.getDisplayedSize().getX(), content.getDisplayedSize().getY() - 1);
Renderer2D.enableScissor(
content.getDisplayedPos().getX(),
content.getDisplayedPos().getY(),
content.getDisplayedSize().getX(),
content.getDisplayedSize().getY() -
(bottomCover.getDisplayedSize().getY() > 0 ? 0 : 1));

int i = 0;
for (I item : getItems()) {
Expand Down

0 comments on commit 7e5335a

Please sign in to comment.