#include "view.h"

/*these are the key codes for camera changes*/

#define SWIVEL_LEFT     'a'
#define SWIVEL_RIGHT	'd'
#define MOVE_FOWARDS	's'
#define MOVE_BACKWARDS	'x'

#define QUIT		'.'

#define PI              (3.14159265358979323846)

extern Display *display;




void interpretKeyStroke(view,result)
View *view;
int result;
{
	REAL swivel,translation;
	TMatrix posR,negR;
	
	swivel = PI/18.0;
	translation = 10.0;
	
	/*construct the left and right rotataion matrices - rotation about z*/
	setZRotationMatrix(posR.TM,swivel);
	setZRotationMatrix(negR.TM,-swivel);

	switch(result){

		case SWIVEL_LEFT	: 	/*rotate VPN about z axis*/
						transformVPNOfView(view,&posR);
						break;
									
		case SWIVEL_RIGHT	: 	/*rotate VPN about z axis*/
						transformVPNOfView(view,&negR);
						break;
								
										
		case MOVE_FOWARDS	: 	/*move VRP*/
						translateVRPOfView(view,translation);
						break;		
			
		case MOVE_BACKWARDS	: 	/*move VRP*/
						translateVRPOfView(view,-translation);
						break;		
								
		case QUIT		:	EXIT;
			
		default			:  	 /*donothing*/;
						break;
	}
}


void mainEventLoop(view,facelist,tree)
View *view;
FaceList *facelist;
Tree *tree;
{
	XEvent event;
	REAL swivel,translation;
	TMatrix posR,negR;
	char result[1];
	
	swivel = PI/18.0;
	translation = 10.0;
	
	/*construct the left and right rotataion matrices - rotation about z*/
	setZRotationMatrix(posR.TM,swivel);
	setZRotationMatrix(negR.TM,-swivel);
	
	while(1){
		XNextEvent(display,&event);

		switch(event.type){
		  case Expose : /*donothing should really redraw all here*/
			break;

		  case MappingNotify :
			XRefreshKeyboardMapping(&event);
			break;

		  case KeyPress :
			XLookupString(&event.xkey,result,1,NULL,NULL);
			interpretKeyStroke(view,(int)result[0]);
			break;

		  default :
			/*donothing*/
			break;
		}
	
		setView(view);
		clearDrawable();
		displayTreeOnView(view,facelist,tree);
	}
}