/***************************************************************************
                          gamedesigner.h  -  description
                             -------------------
    begin                : Mit Sep 25 13:11:41 CEST 2002
    copyright            : (C) 2002 by Harald Krippel
    email                : neuro.harald@surfeu.at
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef GAMEDESIGNER_H
#define GAMEDESIGNER_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qwidget.h>
#include <qgl.h>
#include <qmutex.h>
#include <listviews.h>
#include <globdefines.h>


// plib
#include <plib/ssg.h>
#include <plib/ssgAux.h>

// SPL
#include "spl_prg.h"

// ODE
#include <ode/ode.h>

#include "camera.h"


class ssgaSky;
class phThread;
class shader;

/** Glcontrol is the base class of the project */
class Glcontrol : public QGLWidget
{
  Q_OBJECT 
  public:
    /** construtor */
    Glcontrol(QWidget* parent=0, const char *name=0);

    virtual void	transform();

    /** destructor */
    ~Glcontrol();

    ssgTransform *globj;
    ssgRoot      *scene;

    
public slots:
    void		setMouseMode(int mode);

    void		setXRotation( double degrees );
    void		setYRotation( double degrees );
    void		setZRotation( double degrees );

    void		setScale( double s );

    void		setXTrans( double x );
    void		setYTrans( double y );
    void		setZTrans( double z );

    void		setXScale( double x );
    void		setYScale( double y );
    void		setZScale( double z );

    virtual void	setRotationImpulse( double x, double y, double z );
    virtual void	setTranslationImpulse( double x, double y, double z );
    virtual void	setScaleImpulse( double x, double y, double z );
    void    initFolderListItem(FolderListItem * i,const QString &path);
    void    setFocus(FolderListItem * i);
    void    setFocusObject(FolderListItem * i);
    void    setFocusPlayer(FolderListItem * i);
    void    setFocusBarrier(FolderListItem * i);
    void    setFocusBody(FolderListItem * i);
    void    setFocusLamp(FolderListItem * i);
    void    setFocusFog(FolderListItem * i);
    void    setFocusFire(FolderListItem * i);
    void    setFocusScene(FolderListItem * i);
    void    setFocusSky(FolderListItem * i);
    void    setFocusSound(FolderListItem * i);
    void    setFocusSPL(FolderListItem * i);
    void    setFocusShader(FolderListItem * i);
    void    setFocusCursor(FolderListItem * i);
    void    setFocusGUIText(FolderListItem * i);
    void    setFocusGUIButton(FolderListItem * i);
    void    setFocusGUIOSButton(FolderListItem * i);
    void    setFocusGUIInput(FolderListItem * i);
    void    setFocusWaveSys(FolderListItem * i);
    void    setFocusTerrain(FolderListItem * i);
    void    setFocusCamera(FolderListItem * i);

    void    StopSound();
    void    viewCamera(FolderListItem * i);
    void    switchCamera(FolderListItem * i);
    void    loadObject(FolderListItem * i, ssgTransform *obj);
    void    addFire(FolderListItem * i, ssgTransform *obj);
    void    addLensflare(FolderListItem * i, ssgTransform *obj);
    void    addWaveSys(FolderListItem * i, ssgTransform *obj);
    void    addCamera(FolderListItem * i, ssgTransform *obj);

    static void droplet_create ( ssgaParticleSystem *, int, ssgaParticle *p );
    void    addPartSys(FolderListItem * i, ssgTransform *obj);
    void    addFog(FolderListItem * i);
    void    addPlayer(FolderListItem * i, ssgTransform *obj);
    void    addPlayerFile(FolderListItem * i, QString path , QString file, ssgTransform *obj);
    void    addBarrier(FolderListItem * i, ssgTransform *obj);
    void    addBarrierFile(FolderListItem * i,QString path , QString file, ssgTransform *obj);
    void    addBody(FolderListItem * i, ssgTransform *obj);
    void    addBodyFile(FolderListItem * i,QString path , QString file, ssgTransform *obj);
    void    addSky(FolderListItem * i, ssgTransform *obj);
    void    addSkyFile(FolderListItem * i, QString path , QString file, ssgTransform *obj);
    void    addTerrain(FolderListItem * i, ssgTransform *obj);
    void    addTerrainFile(FolderListItem * i, QString path , QString file, ssgTransform *obj);
    void    addSpl(FolderListItem * i, ssgTransform *obj);
    void    addShader(FolderListItem * i, ssgTransform *obj);
    void    addSPLFile(FolderListItem * i, QString path , QString file, ssgTransform *obj);
    void    addShaderFile(FolderListItem * i, QString path , QString vfile, QString ffile, ssgTransform *obj);
    void    addSound(FolderListItem * i, ssgTransform *obj);
    void    addSoundFile(FolderListItem * i, QString path , QString file, ssgTransform *obj);
    void    addCursor(FolderListItem * i);
    void    addGUITXT(FolderListItem * i);
    void    addGUIButton(FolderListItem * i);
    void    addGUIInput(FolderListItem * i);
    void    addGUIOSButton(FolderListItem * i);
    void    addNetwork(FolderListItem * i);

    void    loadObjectFile(QString path , QString file, ssgTransform *obj);
    void    makemyCurrent();
    void    DeleteScene();
    void    gtimerDone();
    void    updatePH();
    void    puDelete();
    void    puRedraw();    
    void    phDelete();
    void    changeAttribute (FolderListItem * i, char *attr, char *value);
    void    switchScene(FolderListItem * i);
    void    getPoint(int x, int y, sgVec3 *poi);    
    // wavesys Callbacks  uuugggssssss @@@ H.K.
    static int enableTexGen ( ssgEntity * );
    static int disableTexGen ( ssgEntity * );
    // 3D-Object Callbacks  uuugggssssss @@@ H.K.
    static int objprecall  ( ssgEntity * );
    static int objpostcall ( ssgEntity * );


protected:

    void		initializeGL();
    void    load_database ();
    void    init_graphics ();
    void    update_motion ();
    void		paintGL();
    void		resizeGL( int w, int h );
    void		mousePressEvent( QMouseEvent *e );
    void		mouseReleaseEvent( QMouseEvent *e );
    void		mouseMoveEvent( QMouseEvent * );
//    void		mouseDoubleClickEvent( QMouseEvent * );
    void		wheelEvent( QWheelEvent * );

    void    keyPressEvent(QKeyEvent *e);
    void    initVariables();
    void    init_gui();

    bool animation;

protected slots:

private:
    FolderListItem *focusitem;
    bool wasAnimated;
    QPoint oldPos;

    QTimer* gtimer;
    phThread *phthread;

    // Mutex
    QMutex ph_mutex;
    
    int delay;
    int mouse_mode;   

    ssgaFire      *gl_fire_obj[MAX_FIRE];
    int gl_fire_num;

    ssgaParticleSystem *gl_partsys_obj[MAX_PARTSYS];
    int gl_partsys_num;

    ssgaWaveSystem *gl_wavesys_obj[MAX_WAVESYS];
    int gl_wavesys_num;

    static ssgCallback   waveprecall[MAX_WAVESYS];
    static ssgCallback   wavepostcall[MAX_WAVESYS];
    
    ssgaSky  *gl_sky_obj[MAX_SKY];  
    int gl_sky_num;
        
    int gl_player_num;
    Player  *gl_player[MAX_PLAYER];
    
    int gl_barrier_num;
    Barrier  *gl_barrier[MAX_BARRIER];

    int gl_body_num;
    Body  *gl_body[MAX_BODY];
        
    int gl_cursor_num;
    cursor3d *gl_cursor_obj[MAX_CURSOR];

    GLfloat xRot, yRot, zRot;
    GLfloat xTrans, yTrans, zTrans;
    GLfloat xScale, yScale, zScale;
    GLfloat scale;
    
    GLfloat cxRot, cyRot, czRot;
    GLfloat cxTrans, cyTrans, czTrans;
    int gllook;
    // Fog
    int gl_fog;
    sgVec4 skyfogcol ;
    GLfloat fogdensity;
    GLfloat fogstart;
    GLfloat fogend;
    int     fogmode;
    int     foghint;
    // context
    Camera *my_context;
    float mgox;
    float mgoy;
    int keylook;

};

#endif
