/*******************************************************************************
**
** The source file for the Jahshaka glworldcontrols.cpp file
** The Jahshaka Project
** Copyright (C) 2000-2005 The Jahshaka Project
** Released under the GNU General Public License
**
*******************************************************************************/

#include "glworld.h"
#include "glworldlists.h"
#include <timelineSlider.h>
#include <qlistview.h>

////////////////////////////////////////////
// layer controls

void
GLWorld::resetAllSliders()
{
    motionNode* sliders = getActiveJahLayer()->layernodes->m_node;
    sliders->rx = 0.0f;
    sliders->ry = 0.0f;
    sliders->rz = 0.0f;
    sliders->sx = 1.0f;
    sliders->sy = 1.0f;
    sliders->sz = 1.0f;
    sliders->tx = 0.0f;
    sliders->ty = 0.0f;
    sliders->tz = 0.0f;
    sliders->Alpha = 100.0f;

    updateSliders();
}

//active layer controls here
void GLWorld::setXRotation( int degrees )
{ 
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->rx = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->rx = (GLfloat)(degrees);
    }
    else
    {
        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->rx += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->rx -= 5.0f;
                    }
                }
            }
        }
        else
        {
            getActiveJahLayer()->layernodes->m_node->rx = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    if ( getActiveJahLayer()->drawtheKey && !getActiveJahLayer()->getUsePbuffer() )
    {
        motionNode* slider = getActiveJahLayer()->layernodes->m_node;
        slider->rx = jahstd::Min<float>(slider->rx, 90.0f);
        slider->rx = jahstd::Max<float>(slider->rx, -90.0f);
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

void GLWorld::setYRotation( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->ry = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->ry = (GLfloat)(degrees);
    }
    else
    {
        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->ry += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->ry -= 5.0f;
                    }
                }
            }
        }
        else
        {
			getActiveJahLayer()->layernodes->m_node->ry = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    if ( getActiveJahLayer()->drawtheKey && !getActiveJahLayer()->getUsePbuffer() )
    {
        motionNode* slider = getActiveJahLayer()->layernodes->m_node;
        slider->ry = jahstd::Min<float>(slider->ry, 90.0f);
        slider->ry = jahstd::Max<float>(slider->ry, -90.0f);
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

void GLWorld::setZRotation( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->rz = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->rz = (GLfloat)(degrees);
    }
    else
    {
        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->rz += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->rz -= 5.0f;
                    }
                }
            }
        }
        else
        {
			getActiveJahLayer()->layernodes->m_node->rz = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}



void GLWorld::setXTrans   ( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->tx = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->tx = (GLfloat)(degrees);
    }
    else
    {

        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->tx += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->tx -= 5.0f;
                    }
                }
            }
        }
        else
        {
        getActiveJahLayer()->layernodes->m_node->tx = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);

}
void GLWorld::setYTrans   ( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->ty = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->ty = (GLfloat)(degrees);
    }
    else
    {

        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->ty += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->ty -= 5.0f;
                    }
                }
            }
        }
        else
        {
			getActiveJahLayer()->layernodes->m_node->ty = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}
void GLWorld::setZTrans   ( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        camera->layernodes->m_node->tz = (GLfloat)(degrees);
        getActiveJahLayer()->layernodes->m_node->tz = (GLfloat)(degrees);
    }
    else
    {

        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->tz += 5.0f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->tz -= 5.0f;
                    }
                }
            }
        }
        else
        {
			getActiveJahLayer()->layernodes->m_node->tz = (GLfloat)(degrees );
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

//default scale is 1 so things are different here
//as a value of 0 is no scaling...
float GLWorld::setScaleValue   ( int degree )
{
    float degrees = degree;

    GLfloat thescale;

    if (degree==0)
    {
        thescale=1;
    }
    else
    {
        thescale = 1.0 + (GLfloat)(degrees/100);
    }

    return thescale;

}

int GLWorld::getScaleValue   ( float degree )
{
    //still needs some debuging due to rounding errors...
    //QString rere;

    int theval;
    //debug("the val:"+rere.setNum(degree));
    if (degree==1)
    {
        theval = 0;
    }
    else
    {
        theval = -(int)((1-degree)*100);
        //debug("the val:"+rere.setNum(theval));

    }

    return theval;
}

void GLWorld::setXScale   ( int degrees )
{

    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        //camera->m_node->tz = (GLfloat)(degrees);
    }
    else
    {
        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->sx += 0.1f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->sx += 0.1f;
                    }
                }
            }
        }
        else
        {
            getActiveJahLayer()->layernodes->m_node->sx = setScaleValue(degrees);
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

void GLWorld::setYScale   ( int degrees )
{

    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        //camera->m_node->tz = (GLfloat)(degrees);
    }
    else
    {

        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->sy += 0.1f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->sy += 0.1f;
                    }
                }
            }
        }
        else
        {
            getActiveJahLayer()->layernodes->m_node->sy = setScaleValue(degrees);
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

void GLWorld::setZScale   ( int degrees )
{
    static int lastdegree;

    if ( !getActiveJahLayer() )
    {
        return;
    }

    if (getActiveJahLayer() == camera)
    {
        //camera->m_node->tz = (GLfloat)(degrees);
    }
    else
    {
        if ( multipleLayersSelectedInListView() )
        {
            LayerListEntry* layer_list_entry = getLayerList()->first();

            for(; layer_list_entry; layer_list_entry = getLayerList()->next() )
            {
                JahLayer* jah_layer = layer_list_entry->getJahLayer();

                if ( jah_layer->getCheckListItem() && jah_layer->getCheckListItem()->isSelected() )
                {
                    if (degrees > lastdegree)
                    {
                        jah_layer->layernodes->m_node->sz += 0.1f;
                    }
                    else if (degrees < lastdegree) 
                    {
                        jah_layer->layernodes->m_node->sz += 0.1f;
                    }
                }
            }
        }
        else
        {
            getActiveJahLayer()->layernodes->m_node->sz = setScaleValue(degrees);
        }

        lastdegree = degrees;
    }

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);


}

void GLWorld::setAlpha    ( int degrees )  
{ 
	JahLayer* current_jah_layer = getActiveJahLayer();

    if (!current_jah_layer)
    {
        return;
    }

    current_jah_layer->layernodes->m_node->Alpha = (GLfloat)(degrees);

    updateEffects();

    setDontTrack(true);
    updateGL();
    setDontTrack(false);
}

void GLWorld::setZoom     ( int degrees )  
{ 
    camera->layernodes->m_node->sc = (GLfloat)(degrees); updateGL();   
}


void GLWorld::setExtrude  ( int depth )  
{

    if (depth < 0)
    {
        depth = 0;
    }

    getActiveJahLayer()->layernodes->m_node->extrude = float(depth);
    getActiveJahLayer()->updateFont = true;

    updateGL(); 
}

void GLWorld::setKeyframeDrawStatus()
{

    if (getActiveJahLayer() != camera) 
    {
        getActiveJahLayer()->drawKeyframes = !getActiveJahLayer()->drawKeyframes;
    }

    updateGL(); 
}

void GLWorld::setColor() 
{
    QColor newcolor;

    GLfloat redcolor, greencolor, bluecolor;

    QColor c = QColorDialog::getColor( newcolor, 0, 0 );

    QRgb thecolor=c.rgb();

    int r=qRed(thecolor);   
    if (r==0)  redcolor = 0.0;    else {  redcolor   = (r/255.0); }

    int g=qGreen(thecolor); 
    if (g==0)  greencolor = 0.0;  else {  greencolor = (g/255.0);  }

    int b=qBlue(thecolor);  
    if (b==0)  bluecolor  = 0.0;  else {  bluecolor  = (b/255.0);  }

    getActiveJahLayer()->Red = redcolor;
    getActiveJahLayer()->Blue = bluecolor;
    getActiveJahLayer()->Green = greencolor;

}


////////////////////////////////////////////
// layer video controls

//we need to add inframe and outframe or startslipframe and endslipframe

//if forceplay we update visuals here as well

void GLWorld::setinFrames      ( int frame )   
{

    getActiveJahLayer()->m_inframe    = frame;
    getActiveJahLayer()->m_outframe   = frame + getActiveJahLayer()->asset.getNumFrames();

    //is it possible to only update the layer and not the entire scene? no
    if (forceplay) 
    { 
        updateAnimation(controllerslider->value()); 
    } 
    else 
    { 
        updateGL();  
    }

    //updateSliders();
    updateSliderValues();
    slotUpdateListviewCheckboxes();
}

void GLWorld::setoutFrames      ( int frame )   
{

    getActiveJahLayer()->m_outframe    = frame;
    getActiveJahLayer()->m_inframe     = frame - getActiveJahLayer()->asset.getNumFrames();

    if (forceplay) 
    { 
        updateAnimation(controllerslider->value()); 
    } 
    else 
    { 
        updateGL(); 
    }

    //updateSliders();
    updateSliderValues();
    slotUpdateListviewCheckboxes();
}


///////////////////////////////////////
//change to key in and key out as well

void GLWorld::setinKeyFrames      ( int frame )   
{

    getActiveJahLayer()->m_keyinframe = frame;
    getActiveJahLayer()->m_keyoutframe   = frame + getActiveJahLayer()->keyasset.getNumFrames();

    if (forceplay) 
        updateAnimation(controllerslider->value()); 
    else 
        updateGL();

    //updateSliders();
    updateSliderValues();
    slotUpdateListviewCheckboxes();
}

void GLWorld::setoutKeyFrames      ( int frame )   
{

    getActiveJahLayer()->m_keyoutframe = frame;
    getActiveJahLayer()->m_keyinframe   = frame - getActiveJahLayer()->keyasset.getNumFrames();

    if (forceplay) 
    { 
        updateAnimation(controllerslider->value()); 
    } 
    else 
    { 
        updateGL(); 
    }

    //updateSliders();
    updateSliderValues();
    slotUpdateListviewCheckboxes();
}



///////////////////////////////////////////////////////
// update lights

void GLWorld::updateLights( void) 
{
    JahLayer* thelayer = getActiveJahLayer();

    switch ( thelayer->lightNumber ) 
	{ 
        case 1     :  {  
                        #ifdef GL_LIGHT1
                        glLightfv(GL_LIGHT1, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT1, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT1, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 2     :  {  
                        #ifdef GL_LIGHT2
                        glLightfv(GL_LIGHT2, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT2, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT2, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 3     :  {  
                        #ifdef GL_LIGHT3
                        glLightfv(GL_LIGHT3, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT3, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT3, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 4     :  {  
                        #ifdef GL_LIGHT4
                        glLightfv(GL_LIGHT4, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT4, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT4, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 5     :  {  
                        #ifdef GL_LIGHT5
                        glLightfv(GL_LIGHT5, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT5, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT5, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 6     :  { 
                        #ifdef GL_LIGHT6
                        glLightfv(GL_LIGHT6, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT6, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT6, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        case 7     :  {  
                        #ifdef GL_LIGHT7
                        glLightfv(GL_LIGHT7, GL_DIFFUSE, thelayer->light_diffuse);
                        glLightfv(GL_LIGHT7, GL_AMBIENT, thelayer->light_ambient);
                        glLightfv(GL_LIGHT7, GL_SPECULAR, thelayer->light_specular);
                        #endif
                        break; 
                      }
        default    :  {  
                        break; 
                      }

    }


}






