// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include "../hacks/winhacks.h"

#include <iostream>
using namespace std;

#include <GL/gl.h>

#include "widgettransform.h"

#include "../3rdparty/tinyxml/tinyxml.h"
#include "../math/vector2d.h"
#include "../math/vector3d.h"
#include "../math/matrix.h"

WidgetTransform::WidgetTransform(void) {
	child = NULL;
	
	for(int x = 0; x < 4; x++) {
		for(int y = 0; y < 4; y++) {
			
			// Set 1 along the diagonal.
			// Is this code too hacky looking? :P
			transform[x + y * 4] = (x == y);
		}
	}
	
}

WidgetTransform::WidgetTransform(TiXmlElement *xml) : WidgetSingleParent(xml) {
}

WidgetTransform::~WidgetTransform(void) {
}

void WidgetTransform::draw(void) {
	if(child) {
		// Push MY clipping area.
		// So the child can do whatever.
		pushClipArea(0, 0, 
			width, height);

		glPushMatrix();
		
		glMultMatrixd(transform);

		child->draw();
		
		glPopMatrix();
		
		popClipArea();		
	}
}

bool WidgetTransform::handleEvent(const WidgetEvent *event) {
	
	float oldOffsetX = debugDrawOffsetX;
	float oldOffsetY = debugDrawOffsetY;
	
	bool ret = false;
	
	ret |= WidgetSingleParent::handleEvent(event);
	
	// FIXME: Should we return immediately if ret is true?
	
	// Copy the whole event so whatever we don't change is sane.
	WidgetEvent shiftedEvent = *event;
	
	if(event->type == WIDGETEVENT_MOUSEMOTION ||
		event->type == WIDGETEVENT_MOUSEDOWN ||
		event->type == WIDGETEVENT_MOUSEUP) {
		
		Vector2D mousePos(shiftedEvent.mouse.x, shiftedEvent.mouse.y);
		
		mousePos = widgetProjectOntoZPlane(mousePos, Matrix4x4(transform));
		shiftedEvent.mouse.x = mousePos.x;
		shiftedEvent.mouse.y = mousePos.y;
		
		// TODO: Figure out what the hell to do here.
		if(child) ret |= child->handleEvent(&shiftedEvent);
	} else {
		
		// Everything else.
		if(child) ret |= child->handleEvent(&shiftedEvent);
	}
	
	debugDrawOffsetX = oldOffsetX;
	debugDrawOffsetY = oldOffsetY;
	
	return ret;
}

// TODO: Lua callbacks.
