package speiger.src.coreengine.rendering.gui.helper.constrains;

import speiger.src.coreengine.rendering.gui.GuiComponent;
import speiger.src.coreengine.rendering.gui.helper.constrains.Constrain.ConstrainTarget;

public class ComponentConstrains
{
	Constrain xPos;
	Constrain yPos;
	Constrain width;
	Constrain height;
	
	public ComponentConstrains(Constrain xPos, Constrain yPos, Constrain width, Constrain height)
	{
		this.xPos = xPos;
		this.yPos = yPos;
		this.width = width;
		this.height = height;
	}
	
	public void setOwner(GuiComponent owner, GuiComponent parent)
	{
		set(xPos, owner, parent, ConstrainTarget.X_POS);
		set(yPos, owner, parent, ConstrainTarget.Y_POS);
		set(width, owner, parent, ConstrainTarget.WIDTH);
		set(height, owner, parent, ConstrainTarget.HEIGHT);
	}
	
	public void onComponentChanged()
	{
		apply(xPos);
		apply(yPos);
		apply(width);
		apply(height);
	}
	
	private void set(Constrain c, GuiComponent owner, GuiComponent parent, ConstrainTarget target)
	{
		if(c != null)
		{
			c.setComponents(owner, parent, target);
		}
	}
	
	private void apply(Constrain c)
	{
		if(c != null)
		{
			c.apply();
		}
	}
}
