//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// box.cc
//
// Vincent LE PRINCE <vincentleprince@users.sourceforge.net>
// Copyright (C) 2000-2001 Vincent LE PRINCE
// This file is part of the TRUEVISION Package

//   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., 675 Mass Ave, Cambridge, MA 02139, USA.  */ 
//*******************************************************************************************
#include "include/box.h"
#include "include/viewmanager.h"
#include "include/objectlist.h"
#include "include/tvio.h"

//**************************************
// Constructeur
//**************************************
Box::Box( app_objs *appref ) : Object3D_with_material( appref )
{
type = TV_OBJ3D_BOX;
category = TV_OBJ3D_OBJECTS;
set_name( "Box" );

// Base
location = new ObjParam_point( N_("Location"), "LOC", NULL, app_ref, true );
location->set( 0, 0, 0 );
size = new ObjParam_scale( N_("Size"), "SIZE", NULL, app_ref, true );
size->set( 0.2, 0.2, 0.2 );
rotation = new ObjParam_rotation( N_("Rotation"), "ROT", NULL, app_ref, true );
rotation->set( 0, 0, 0 );
}

Box::Box( Box & ref ) : Object3D_with_material( ref )
{
location = new ObjParam_point( *ref.location );
size = new ObjParam_scale( *ref.size );
rotation = new ObjParam_rotation( *ref.rotation );
}

Box::~Box()
{
delete location;
delete size;
delete rotation;
}

//**************************************
// Display
// Dessin de la boite
//**************************************
void Box::display( glview *view )
{
if ( hidden->value() ) return;
if ( location->changed() || size->changed() || rotation->changed() )
	list.invalidate();

Object3D::display( view );	
if ( ! list.exec() )
	{
	// mise à zero des parametres
	location->unchange();
	size->unchange();
	rotation->unchange();

	// creation de la liste si necessaire
	list.begin();
	glPushMatrix();

	set_color();
	
	// Position et direction
	gfloat x, y, z;
	gfloat a, b, c;
	location->get( x, y, z );
	glTranslatef( x, y, z );
	rotation->gl_rotate();

	size->get( a, b, c );
	x = -a/2; y = -b/2; z = -c/2;
	a = -x; b = -y; c = -z;

	gfloat vertex[24][3] = { {x,y,z}, {x,y,c}, {x,b,c}, {x,b,z},
							 {x,b,z}, {x,b,c}, {a,b,c}, {a,b,z},
							 {a,b,z}, {a,b,c}, {a,y,c}, {a,y,z},
							 {a,y,z}, {a,y,c}, {x,y,c}, {x,y,z},
							 {x,y,z}, {x,b,z}, {a,b,z}, {a,y,z},
							 {a,b,c}, {x,b,c}, {x,y,c}, {a,y,c}	};
	gfloat normal[6][3] = { {-1,0,0}, {0,1,0}, {1,0,0}, {0,-1,0}, {0,0,-1}, {0,0,1} };

	register int i,j;
	glBegin( GL_QUADS );
	for ( i = 0; i < 6 ; i++ )
		{
		//j = i/4;
		glNormal3f( normal[i][0], normal[i][1], normal[i][2] );			
		j = i*4;
		glVertex3f( vertex[j][0], vertex[j][1], vertex[j][2] ); j++;
		glVertex3f( vertex[j][0], vertex[j][1], vertex[j][2] ); j++;
		glVertex3f( vertex[j][0], vertex[j][1], vertex[j][2] ); j++;
		glVertex3f( vertex[j][0], vertex[j][1], vertex[j][2] );
		}		
	glEnd();
	glPopMatrix();
	list.end();
	}
}


//***********************************************
// Edit
//***********************************************
void Box::edit_widget( GtkWidget *wid )
{
bool tt = true;
// Options communes
Object3D::edit_widget( wid );

// Options de geometrie
new_frame( edit_cont, _("General settings") );
location->get_widget( frame, tt );
size->get_widget( frame, tt );
rotation->get_widget( frame, tt );

texture->get_widget( edit_cont, tt );

gtk_widget_show_all( wid );
}

//***********************************************
// Mouse drag
//***********************************************
void Box::mouse_drag( struct drag_info *drag )
{
VMAN_DEF
OBJLIST_DEF

switch( vmanager->get_pointer_mode() )
	{
	case TV_PMODE_TRANSLATE:
		location->mouse_drag( drag );
		break;

	case TV_PMODE_SCALE:
		{ size->mouse_drag( drag ); }
		break;

	case TV_PMODE_ROTATE:
		{ rotation->mouse_drag( drag ); break; }

	case TV_PMODE_CUSTOM:
		((ObjParam_point*)(objlist->get_current_param()))->mouse_drag( drag );
		break;

	default: 
		break;
	}
}

//***********************************************
// Pref_changed
//***********************************************
void Box::pref_changed()
{
Object3D::pref_changed();
location->pref_changed();
size->pref_changed();
rotation->pref_changed();
}

//***********************************************
// Destroy editor
//***********************************************
void Box::destroy_editor()
{
Object3D::destroy_editor();
location->clear_widget();
size->clear_widget();
texture->clear_widget();
rotation->clear_widget();
}

//***********************************************
// Output to povray
//***********************************************
void Box::output_to_povray( ofstream & file )
{
if ( !render->value() ) return;
file << "\n\n//Box " << name->value();
file << "\nbox {\n\t";

float a, b, c, x, y, z;
size->get( a, b, c );
a = a/2; b = b/2; c = c/2;
file << '<' << -a << ',' << -b << ',' << c << ">,\n\t<" << a << ',' << b << ',' << -c << ">\n\t";
texture->output_to_povray( file );
file << "\n\t";
rotation->output_to_povray( file );
location->get( x, y, z );
file << "\n\ttranslate <" << x << "," << y << "," << -z << "> \n\t";
Object3D::output_to_povray( file );
file << "\n}";
}


//***********************************************
// Save & load
//***********************************************
void Box::save( ofstream & file )
{
file << "\nBOX{\n";
save_basics( file );
location->save( file );
size->save( file );
rotation->save( file );
texture->save( file );
file << "\n}";
}

bool Box::load( ifstream & file, char *ltag )
{
if ( strcmp( ltag, "BOX" ) ) return false;
set_load_progress( file );

char * tag = NULL;
do {
	tag = tvio_get_next_tag( file );
	if ( tag == NULL ) break;
	
	if ( load_basics( file, tag ) ) continue;
 	if ( location->load( file, tag ) ) continue;
 	if ( size->load( file, tag ) ) continue;
 	if ( rotation->load( file, tag ) ) continue;
		
	tvio_skip_section(file );
	} while ( tag != NULL );

return true;
}
