
#include <GLES/gl.h>
#include "TouchControlsContainer.h"


using namespace touchcontrols;

void TouchControlsContainer::addControlGroup(TouchControls *cntrl)
{
	controls.push_back((cntrl));
}

void TouchControlsContainer::editControls(TouchControls * ctrl)
{
	editingControls = ctrl;
	editingControls->edit();
}

bool TouchControlsContainer::processPointer(int action, int pid, float x, float y)
{
	if (editingControls == 0)
	{
		int size = controls.size();
		bool under=false;
		for (int n=0;n<size;n++)
		{
			TouchControls *cs = controls.at(n);
			if (cs->enabled)
				if (cs->processPointer(action,pid, x, y))
				{
					under=true;
				}
		}
		//if (!under) downInSpace = true;
		return false;
	}
	else
	{
		editingControls->processPointer(action,pid, x, y);
	}

}

int TouchControlsContainer::draw ()
{

	if (editingControls == 0)
	{
		openGL_start.emit();

		int size = controls.size();
		for (int n=0;n<size;n++) //draw
		{
			TouchControls *c = controls.at(size-1-n);
			if (c->enabled)
			{
				if (c->draw()) //So we check if now editing the controls..
					editingControls = c;
			}
		}

		openGL_end.emit();
		return 0;
	}
	else
	{
		openGL_start.emit();

		if (!editingControls->drawEditor()) //Check if finished editing..
			editingControls = 0;

		openGL_end.emit();
		return 1;
	}
}

void TouchControlsContainer::initGL ()
{
	int size = controls.size();
	for (int n=0;n<size;n++) //draw
	{
		TouchControls *c = controls.at(size-1-n);
		c->initGL();
	}
}
