/***************************************************************************
 *   Copyright (C) 2009 by Stefan Bühler                                   *
 *   stbuehler@web.de                                                      *
 *                                                                         *
 *   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.             *
 ***************************************************************************/
#ifndef TORUSCHESSFIELD3D_H
#define TORUSCHESSFIELD3D_H

#include <QGLWidget>
#include <QGLPixelBuffer>

#include "toruschess.h"
#include "piecelibrary.h"

/**
	@author Stefan Bühler <stbuehler@web.de>
*/
namespace toruschess {

	class Field3D : public QGLWidget {
	Q_OBJECT
	public:
		typedef enum { PLANE, TORUS } BoardMode;
		typedef enum { FIG2D, FIG3D } FigureMode;
		
		Field3D(Game *game, QWidget *parent = 0);
		virtual ~Field3D();
		
		void markMoves(const QList<Move> &moves);
		
		virtual QSize sizeHint() const;
		
		BoardMode boardMode() const { return m_boardMode; }
		void setBoardMode(BoardMode m) { m_boardMode = m; updateGL(); }
		
		FigureMode figureMode() const { return m_figureMode; }
		void setFigureMode(FigureMode m);
		
	protected:
		virtual void initializeGL();
		virtual void resizeGL(int w, int h);
		virtual void paintGL();
		
		virtual void mousePressEvent(QMouseEvent *event);
		virtual void mouseMoveEvent(QMouseEvent *event);
		virtual void mouseReleaseEvent(QMouseEvent *event);
		virtual void wheelEvent(QWheelEvent *event);
		
		virtual void timerEvent(QTimerEvent *event);
		
	protected slots:
		void fieldUpdated();
		
	private:
		void updateCam();
		
		void createTexture();
		void freeTexture();
		void updateTexture();
		
		bool findPos(int mx, int my, Pos &p);
		
		Game *m_game;
		PieceLibrary *m_lib;
		
		bool m_marked[8][8];
		QList<Move> m_markedMoves;
		
		BoardMode m_boardMode;
		FigureMode m_figureMode;
		
		/* Texture data */
		QImage *m_textureBuffer;
		GLuint m_textureID;
		
		/* Cam data */
		double m_camDist, m_camRotX, m_camRotY, m_camRotZ;
		int m_originX, m_originY;
		int m_mouseLastX, m_mouseLastY;
		
		/* skybox */
		GLUquadric *m_skySphere;
		GLuint m_skyTextureID;
		int m_skyTimerID;
		double m_skyOffset;
		
		/* Pick */
		GLuint m_pickTextureID;
		QGLPixelBuffer *m_pickBuffer;
		
		static const int m_textureSize = 1024;
	};

}

#endif
