package speiger.src.coreengine.platform.graphics.opengl.utils.states;

import org.lwjgl.opengl.GL11;

import speiger.src.coreengine.math.vector.ints.Vec4i;

public class ScissorState extends GLState {
	Vec4i current = Vec4i.MINUS_ONE;
	Vec4i backup = null;
	
	public ScissorState() {
		super(GL11.GL_SCISSOR_TEST);
	}
	
	public ScissorState set(Vec4i bounds) {
		if(current.equals(bounds)) return this;
		current = bounds.asImmutable();
		if(Vec4i.MINUS_ONE.equals(bounds)) {
			disable();
			return this;
		}
		enable();
		GL11.glScissor(bounds.x(), bounds.y(), bounds.z() - bounds.x(), bounds.w() - bounds.y());
		return this;
	}
	
	public void backup() {
		backup = current;
	}
	
	public void restore() {
		if(backup == null) return;
		set(backup);
		backup = null;
	}
	
	public Vec4i current() {
		return current;
	}
	
	@Override
	public void setDefault() {
		super.setDefault();
		current = Vec4i.MINUS_ONE;
	}
	
	@Override
	public void reapply() {
		super.reapply();
		GL11.glScissor(current.x(), current.y(), current.z() - current.x(), current.w() - current.y());
	}
}
