#ifndef TRACKBALL_H
#define TRACKBALL_H

#include <GL/gl.h>


namespace nsToolkit {

class Trackball
{
	private:
		bool update;
		bool frictionless;
		bool temporaryStop;

		GLfloat angle;
		GLfloat axis[3];
		GLfloat transform[16];

		float zoom, zoomMin, zoomMax;

		int lastX, lastY;
		float lastPosition[3];

		float translateX, translateY;
		float translateXMin, translateYMin, translateXMax, translateYMax;
		float lastTranslateX, lastTranslateY;

		void projectToSphere(int x, int y, int windowWidth, int windowHeight, float v[3]);

	public:
		Trackball(void);

		float getZoom(void);
		void setZoom(float zoom);
		float getZoomMin(void);
		void setZoomMin(float min);
		float getZoomMax(void);
		void setZoomMax(float max);

		bool getFrictionless(void);
		void setFrictionless(bool status);

		float getTranslateX(void);
		float getTranslateY(void);
		void setTranslateRectangle(float xmin, float ymin, float xmax, float ymax);

		void reset(void);

		/*
		 * This method should be called whenever a new frame is
		 * ready for rendering. The "display" function is a good
		 * place for it. Call it when all "global settings" are
		 * done(camera, light sources) and before performing any
		 * other OpenGL related drawings.
		 */
		void eventExpose(void);

		// Call this method whenever the "rotation mouse button" is pressed.
		void eventRotationBegin(int x, int y, int windowWidth, int windowHeight);
		// Call this method whenever the mouse is moved while the "rotation mouse button" is pressed.
		void eventRotationUpdate(int x, int y, int windowWidth, int windowHeight);

		// Call this method whenever the "zoom mouse button" is pressed
		void eventZoomBegin(int y);
		// Call this method whenever the mouse is moved while the "zoom mouse button" is pressed.
		void eventZoomUpdate(int y, int windowHeight);

		// Call this method whenever the "translate mouse button" is pressed.
		void eventTranslateBegin(int x, int y);
		// Call this method whenever the mouse is moved while the "translate mouse button" is pressed.
		void eventTranslateUpdate(int x, int y, int windowWidth, int windowHeight);
		// Call this method whenever the "translate mouse button" is released.
		void eventTranslateEnd(void);
};

}

#endif
