Skip to content

Instantly share code, notes, and snippets.

@DCubix
Created April 30, 2018 22:11
Show Gist options
  • Save DCubix/1f6e76958fdef346db6cc65b51638c88 to your computer and use it in GitHub Desktop.
Save DCubix/1f6e76958fdef346db6cc65b51638c88 to your computer and use it in GitHub Desktop.
Clipping for GUI
public boolean beginClipping(int x, int y, int w, int h) {
IDevice dev = Age.Device;
if (!dev.isEnabled(Feature.Scissor)) {
dev.setFeatures(true, Feature.Scissor);
}
if (!clipRectStack.isEmpty()) {
Rectangle parent = clipRectStack.lastElement();
int minX = Math.max(parent.x, x);
int maxX = Math.min(parent.x + parent.width, x + w);
if (maxX - minX < 1) return false;
int minY = Math.max(parent.y, y);
int maxY = Math.min(parent.y + parent.height, y + h);
if (maxY - minY < 1) return false;
x = minX;
y = minY;
w = maxX - minX;
h = Math.max(1, maxY - minY);
}
clipRectStack.push(new Rectangle(x, y, w, h));
dev.setScissor(x, y, w, h);
return true;
}
public void endClipping() {
IDevice dev = Age.Device;
if (!clipRectStack.isEmpty()) {
clipRectStack.pop();
}
if (!clipRectStack.isEmpty()) {
Rectangle scissor = clipRectStack.peek();
dev.setScissor(scissor.x, scissor.y, scissor.width, scissor.height);
} else {
dev.setScissor(0, 0, width, height);
if (dev.isEnabled(Feature.Scissor)) {
dev.setFeatures(false, Feature.Scissor);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment