module gedo.video.rect;



private import std.math;
private import gedo.math.vector;
private import gedo.video.ggl;
private import gedo.video.color;
private import gedo.video.texture;
private import gedo.video.staticmesh;



public class Rect
{
public:
	static this()
	{
		_instance = new Rect(0, 0, 0, 0);
	}


	static Rect opCall(Vector2D p, Vector2D s)
	{
		_instance.set(p, s);

		return _instance;
	}


	static Rect opCall(float x, float y, float w, float h)
	{
		_instance.set(x, y, w, h);

		return _instance;
	}


	this(Vector2D p, Vector2D s)
	{
		set(p, s);
	}


	this(float x, float y, float w, float h)
	{
		set(x, y, w, h);
	}


	void set(Vector2D p, Vector2D s)
	{
		_x = p[0];
		_y = p[1];
		_w = s[0];
		_h = s[1];
	}


	void set(float x, float y, float w, float h)
	{
		_x = x;
		_y = y;
		_w = w;
		_h = h;
	}


	void fill(Color c)
	{
		auto plane = StaticMesh.plane;

		GGL_pushMatrix();

		GGL_translate(_x, _y, 0);

		GGL_scale(_w, _h, 1);

		plane.draw(null, c);

		GGL_popMatrix();
	}


	void frame(float l, Color c)
	{
		//   l       w       l
		// +---+-----------+---+
		// |   |     2     |   | l
		// |   +-----------+   |
		// |   |           |   |
		// | 3 |           | 4 | h
		// |   |           |   |
		// |   +-----------+   |
		// |   |     1     |   | l
		// +---+-----------+---+

		auto plane = StaticMesh.plane;

		GGL_pushMatrix();

		GGL_translate(_x, _y, 0);

		// 1
		GGL_pushMatrix();

		GGL_translate(0, -(_h + l) * 0.5, 0);

		GGL_scale(_w, l, 1);

		plane.draw(null, c);

		GGL_popMatrix();

		// 2
		GGL_pushMatrix();

		GGL_translate(0, (_h + l) * 0.5, 0);

		GGL_scale(_w, l, 1);

		plane.draw(null, c);

		GGL_popMatrix();

		// 3
		GGL_pushMatrix();

		GGL_translate(-(_w + l) * 0.5, 0, 0);

		GGL_scale(l, _h + 2*l, 1);

		plane.draw(null, c);

		GGL_popMatrix();

		// 4
		GGL_pushMatrix();

		GGL_translate((_w + l) * 0.5, 0, 0);

		GGL_scale(l, _h + 2*l, 1);

		plane.draw(null, c);

		GGL_popMatrix();

		GGL_popMatrix();
	}


	void draw(Texture t, Color c)
	{
		auto plane = StaticMesh.plane;

		GGL_pushMatrix();

		GGL_translate(_x, _y, 0);

		GGL_scale(_w, _h, 1);

		plane.draw(t, c);

		GGL_popMatrix();
	}


private:
	static Rect _instance;

	float _x;
	float _y;
	float _w;
	float _h;
}

