
#include "stdafx.h"
#include "MultitouchDrawing.h"

#include "Infrastructure\Common.h"
#include "MultiTouchManager.h"
#include "CellWarsGameManager.h"
#include "Util\DrawUtil.h"

#include <TuioClient.h>
#include <TuioObject.h>
#include <TuioCursor.h>
//#include <TuioListener.h>
#include <TuioPoint.h>
#include <list>

using namespace TUIO;

#define DRAW_CURSOR_PATH

MultitouchDrawing::MultitouchDrawing()
{
}

MultitouchDrawing::MultitouchDrawing(MultiTouchManager *useThisMultiTouchManager)
{
	useThisMTManager = useThisMultiTouchManager;
}

void MultitouchDrawing::Start()
{
}

void MultitouchDrawing::Stop()
{
}

void MultitouchDrawing::ReceiveMessage(Message *m)
{
}

void MultitouchDrawing::Render()
{
	TUIO::TuioClient *tuioClient = useThisMTManager->getTUIOClient();

	// draw the cursors
	tuioClient->lockCursorList(); // first locking, then getting the list, or sometimes multithreading problems
	std::list<TuioCursor*> cursorList = tuioClient->getTuioCursors();
	for (std::list<TuioCursor*>::iterator iter = cursorList.begin(); iter!=cursorList.end(); iter++) 
	{
		TuioCursor *tuioCursor = (*iter);
		std::list<TuioPoint> path = tuioCursor->getPath();
		if (path.size()>0) {

#ifdef DRAW_CURSOR_PATH
			TuioPoint last_point = path.front();
			
			// path
			glColor4f(1.0f, 1.0f, 0.0f, 0.75f);
			for (std::list<TuioPoint>::iterator point = path.begin(); point!=path.end(); point++) 
			{
				Vector2 lastPointAngelCoords(MultiTouchManager::TUIOToAngelCoordinates(Vector2(last_point.getX(), last_point.getY())));
				Vector2 thisPointAngelCoords(MultiTouchManager::TUIOToAngelCoordinates(Vector2(point->getX(), point->getY())));
				DrawLine(lastPointAngelCoords, thisPointAngelCoords);
				last_point.update(point->getX(),point->getY());
			}
#endif

			// finger tip
			glColor4f(1.0f, 0.15f, 0.15f, 0.75f);
			Vector2 touchPosInAngelCoords(MultiTouchManager::TUIOToAngelCoordinates(Vector2(path.back().getX(), path.back().getY())));
			DrawPoint(touchPosInAngelCoords, 0.5f);
			//glRasterPos2f(tuioCursor->getScreenX(width),tuioCursor->getScreenY(height));
			//sprintf(id,"%d",tuioCursor->getCursorID());
			//drawString(id);
		}
	}
	tuioClient->unlockCursorList();

	// draw the objects
	/*
	tuioClient->lockObjectList();
	std::list<TuioObject*> objectList = tuioClient->getTuioObjects();
	for (std::list<TuioObject*>::iterator iter = objectList.begin(); iter!=objectList.end(); iter++) {
		TuioObject* tuioObject = (*iter);
		
		Vector2 objectPosInAngelCoords(MultiTouchManager::TUIOToAngelCoordinates(Vector2(tuioObject->getX(), tuioObject->getY())));
		glColor4f(0.75f, 0.75f, 0.75f, 0.75f);
		DrawCross(objectPosInAngelCoords, 1);
		//sprintf(id,"%d",tuioObject->getSymbolID());
		//drawString(id);
		
	}
	tuioClient->unlockObjectList();
	*/
}

void MultitouchDrawing::Update(float dt)
{
}
