package speiger.src.coreengine.rendering.gui.components.icon;

import speiger.src.coreengine.math.misc.ColorObject;
import speiger.src.coreengine.rendering.gui.renderer.UIRenderer;
import speiger.src.coreengine.rendering.textures.base.ITexture;

public class TexturedIcon implements IIcon
{
	ColorObject color = ColorObject.WHITE;
	ITexture texture;
	boolean forceBounds = false;
	
	public TexturedIcon(ITexture texture)
	{
		this.texture = texture;
	}
	
	public TexturedIcon(ColorObject color, ITexture texture)
	{
		this.color = color;
		this.texture = texture;
	}
	
	public void setTexture(ITexture newTexture)
	{
		if(texture != null)
		{
			texture.deleteTexture();
			texture = null;
		}
		texture = newTexture;
	}
	
	public TexturedIcon forceBounds(boolean value)
	{
		forceBounds = value;
		return this;
	}
	
	@Override
	public void render(UIRenderer render, float minX, float minY, float maxX, float maxY, float scale)
	{
		if(forceBounds)
		{
			float width = texture.getWidth();
			float height = texture.getHeight();
			float ratio = height / width;
			maxY = Math.min(minY + ((maxY - minY) * ratio), maxY);
		}
		render.setActiveTexture(texture).drawTexturedQuad(minX, minY, maxX, maxY, color, texture.getUMin(), texture.getVMin(), texture.getUMax(), texture.getVMax());
	}
	
}
