Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
import java.util.Arrays;
import java.util.Collection;
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 @@ -101,36 +98,25 @@ public static void drawLines(Collection<Vector2D> points, Color color) {
}

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();
if (!renderer.isPresent()) return;
if (box == null) renderer.get().disableScissor();
else renderer.get().enableScissor(box.x, box.y, box.w, box.h);
renderer.ifPresent(renderer2DInterface -> renderer2DInterface.enableScissor(x, y, w, h));
}

public static void endFrame() {
setScissor(null);
scissorStack.clear();
Optional<Interface> renderer = Interface.get();
renderer.ifPresent(Interface::clearScissors);
}

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

public static boolean scissorContains(Vector2D point) {
Optional<Interface> renderer = Interface.get();
return renderer
.map(renderer2DInterface -> renderer2DInterface.scissorContains(point))
.orElse(false);
}

public interface Interface extends FunctionHolder {
Expand All @@ -149,16 +135,9 @@ static Optional<Interface> get() {
void enableScissor(double x, double y, double w, double h);

void disableScissor();
}

private static class ScissorBox {
public double x, y, w, h;
void clearScissors();

public ScissorBox(double x, double y, double w, double h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
boolean scissorContains(Vector2D point);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import io.github.kurrycat.mpkmod.compatibility.MCClasses.Renderer2D;
import io.github.kurrycat.mpkmod.util.JSONPos2D;
import io.github.kurrycat.mpkmod.util.Mouse;
import io.github.kurrycat.mpkmod.util.Vector2D;
Expand Down Expand Up @@ -73,7 +74,8 @@ public PopupMenu getPopupMenu() {
public boolean contains(Vector2D testPos) {
if (testPos == null) return false;
if (getDisplayedPos() == null) return false;
return testPos.isInRectBetween(getDisplayedPos(), getDisplayedPos().add(getDisplayedSize()));
return Renderer2D.scissorContains(testPos) &&
testPos.isInRectBetween(getDisplayedPos(), getDisplayedPos().add(getDisplayedSize()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.kurrycat.mpkmod.util;

public 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;
}

public boolean contains(Vector2D point) {
return x <= point.getX() && point.getX() < x + w &&
y <= point.getY() && point.getY() < y + h;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.kurrycat.mpkmod.util.Debug;
import io.github.kurrycat.mpkmod.util.Vector2D;
import io.github.kurrycat.mpkmod.util.Vector3D;
import io.github.kurrycat.mpkmod.util.ScissorBox;
import io.github.kurrycat.mpknetapi.common.network.packet.MPKPacket;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class FunctionCompatibility implements FunctionHolder,
Profiler.Interface {
public static final Set<Integer> pressedButtons = new HashSet<>();
public MatrixStack matrixStack = new MatrixStack();
private static final Stack<ScissorBox> scissorStack = new Stack<>();

public void playButtonSound() {
MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
Expand Down Expand Up @@ -253,19 +255,49 @@ public Vector2D getScreenSize() {
}

public void enableScissor(double x, double y, double w, double h) {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
Window r = MinecraftClient.getInstance().getWindow();

double scaleFactor = r.getScaleFactor();
double posX = x * scaleFactor;
double posY = r.getFramebufferHeight() - (y + h) * scaleFactor;
double width = w * scaleFactor;
double height = h * scaleFactor;
GL11.glScissor((int) posX, (int) posY, Math.max(0, (int) width), Math.max(0, (int) height));
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);
}

public void disableScissor() {
GL11.glDisable(GL11.GL_SCISSOR_TEST);
if (!scissorStack.isEmpty()) scissorStack.pop();
if (scissorStack.isEmpty()) setScissor(null);
else setScissor(scissorStack.peek());
}

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

private void setScissor(ScissorBox box) {
if (box == null) {
GL11.glDisable(GL11.GL_SCISSOR_TEST);
} else {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
Window r = MinecraftClient.getInstance().getWindow();

double scaleFactor = r.getScaleFactor();
double posX = box.x * scaleFactor;
double posY = r.getFramebufferHeight() - (box.y + box.h) * scaleFactor;
double width = box.w * scaleFactor;
double height = box.h * scaleFactor;
GL11.glScissor((int) posX, (int) posY, Math.max(0, (int) width), Math.max(0, (int) height));
}
}

public boolean scissorContains(Vector2D point) {
return scissorStack.isEmpty() || scissorStack.peek().contains(point);
}

public void drawString(String text, double x, double y, Color color, double fontSize, boolean shadow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.kurrycat.mpkmod.util.Debug;
import io.github.kurrycat.mpkmod.util.Vector2D;
import io.github.kurrycat.mpkmod.util.Vector3D;
import io.github.kurrycat.mpkmod.util.ScissorBox;
import io.github.kurrycat.mpknetapi.common.network.packet.MPKPacket;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
Expand Down Expand Up @@ -35,8 +36,8 @@
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.List;

public class FunctionCompatibility implements FunctionHolder,
SoundManager.Interface,
Expand All @@ -49,6 +50,7 @@ public class FunctionCompatibility implements FunctionHolder,
Profiler.Interface {
public static final Set<Integer> pressedButtons = new HashSet<>();
public DrawContext drawContext = null;
private static final Stack<ScissorBox> scissorStack = new Stack<>();

public void playButtonSound() {
MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
Expand Down Expand Up @@ -254,21 +256,50 @@ public Vector2D getScreenSize() {
}

public void enableScissor(double x, double y, double w, double h) {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
Window r = MinecraftClient.getInstance().getWindow();

double scaleFactor = r.getScaleFactor();
double posX = x * scaleFactor;
double posY = r.getFramebufferHeight() - (y + h) * scaleFactor;
double width = w * scaleFactor;
double height = h * scaleFactor;
GL11.glScissor((int) posX, (int) posY, Math.max(0, (int) width), Math.max(0, (int) height));
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);
}

public void disableScissor() {
GL11.glDisable(GL11.GL_SCISSOR_TEST);
if (!scissorStack.isEmpty()) scissorStack.pop();
if (scissorStack.isEmpty()) setScissor(null);
else setScissor(scissorStack.peek());
}

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

private void setScissor(ScissorBox box) {
if (box == null) {
GL11.glDisable(GL11.GL_SCISSOR_TEST);
} else {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
Window r = MinecraftClient.getInstance().getWindow();

double scaleFactor = r.getScaleFactor();
double posX = box.x * scaleFactor;
double posY = r.getFramebufferHeight() - (box.y + box.h) * scaleFactor;
double width = box.w * scaleFactor;
double height = box.h * scaleFactor;
GL11.glScissor((int) posX, (int) posY, Math.max(0, (int) width), Math.max(0, (int) height));
}
}

public boolean scissorContains(Vector2D point) {
return scissorStack.isEmpty() || scissorStack.peek().contains(point);
}

public void drawString(String text, double x, double y, Color color, double fontSize, boolean shadow) {
if (drawContext == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.*;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.Window;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundEvents;
Expand All @@ -31,11 +30,10 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.util.List;
import java.util.*;
import java.util.List;

public class FunctionCompatibility implements FunctionHolder,
SoundManager.Interface,
Expand Down Expand Up @@ -253,21 +251,33 @@ public Vector2D getScreenSize() {
}

public void enableScissor(double x, double y, double w, double h) {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
Window r = MinecraftClient.getInstance().getWindow();

double scaleFactor = r.getScaleFactor();
double posX = x * scaleFactor;
double posY = r.getFramebufferHeight() - (y + h) * scaleFactor;
double width = w * scaleFactor;
double height = h * scaleFactor;
GL11.glScissor((int) posX, (int) posY, Math.max(0, (int) width), Math.max(0, (int) height));
int x1 = (int) x;
int y1 = (int) y;
int x2 = (int) (x + w);
int y2 = (int) (y + h);
drawContext.enableScissor(x1, y1, x2, y2);
}

public void disableScissor() {
GL11.glDisable(GL11.GL_SCISSOR_TEST);
try {
drawContext.disableScissor();
} catch (IllegalStateException ignored) {}
}

public void clearScissors() {
boolean clearedAll = false;
while (!clearedAll) {
try {
drawContext.disableScissor();
} catch (IllegalStateException e) {
clearedAll = true;
}
}
}

public boolean scissorContains(Vector2D point) {
return drawContext.scissorContains(point.getXI(), point.getYI());
}

public void drawString(String text, double x, double y, Color color, double fontSize, boolean shadow) {
if (drawContext == null) return;
Expand Down
Loading
Loading