
#include "meli.h"

#ifndef PandaWave_RESOURCES
#include "RevengeResources.h"
#endif

#include "RevengeApp.h"
#include <XMessages.h>
#include "tagpick.h"
#include <assert.h>
#include <XPopup.h>
#include <XWindow.h>


// ==============================  BYTE SWAPPING DATA  ========================================
byte_swap_code mesh_lighting_byte_swapping_data[]=
{
	_begin_bs_array, 1,

	// fixed_vector3d incident_light; // direction of incident light (.k<0)
	_4byte,	_4byte,	_4byte,

	// The diffuse light intensity ranges from 0.0 to maximum_diffuse_light_intensity.
	// (ambient_light_intensity +  diffuse_light_intensity) is pinned
	// between 0.0 and maximum_total_light_intensity.
	// fixed ambient_light_intensity;	
	// fixed maximum_diffuse_light_intensity;
	// fixed model_shading_diffuse_light_intensity; // when a pixel is shaded by the model, it 
													// gets this value for the diffuse light intensity.	
	_4byte,
	_4byte,
	_4byte,
	
	// fixed maximum_total_light_intensity; // [0.0,1.0]
	_4byte,

	// short maximum_shadow_distance; // in mesh texture pixels
	_2byte,	

	// short filter_width;
	_2byte,	
	// real gaussian_width;
	_4byte,

	//short unused[14];
	14*sizeof(short),

	_end_bs_array
};

mesh_lighting_data default_meli =
{
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};


// ==============================  EDIT WINDOW  ========================================
void edit_meli( tag_entry* e )
{
	TmeliEditForm* form = new TmeliEditForm( e );
	form->AttachWindow( 1012 );
}

void defaultnew_meli( tag_entry *e )
{
	mesh_lighting_data def;
	byte_swap_move_be( "meli", &def, &default_meli, sizeof(mesh_lighting_data), 1, mesh_lighting_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_meli( tag_entry *e )
{
#if 0 // no dependencies
	mesh_lighting_data def;
	read_mesh_lighting_data( &def, e );
#endif	// no dependencies
}


// ==============================  READ TAG DATA  ========================================
bool read_mesh_lighting_data( mesh_lighting_data* def, tag_entry* e )
{
	return load_tag_data_into_struct( "meli", e, def, SIZEOF_STRUCT_MESH_LIGHTING_DATA, 1, mesh_lighting_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_mesh_lighting_data( mesh_lighting_data* def, tag_entry* e )
{
	return save_tag_data_from_struct( "meli", e, def, SIZEOF_STRUCT_MESH_LIGHTING_DATA, 1, mesh_lighting_byte_swapping_data);
}


// ==============================  READ TAG DATA (WITH ID) ========================================
bool read_mesh_lighting_data( mesh_lighting_data* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'meli', id );
	if( 0 == e ) {
		return FALSE;
	}
	
	return read_mesh_lighting_data( def, e );	
}


// ==============================  SAVE TAG DATA (WITH ID) ========================================
bool save_mesh_lighting_data( mesh_lighting_data* def, tag id )
{
	tag_entry* e;
	
	assert( def );
	
	e = get_entry( 'meli', id );
	if( 0 == e ) {
		return FALSE;
	}

	return save_mesh_lighting_data( def, e );
}


// ==============================  DISPATCH  ========================================
BEGINDISPATCH(TmeliEditForm,TTagEditForm)
	DISPATCHTABLE(KXGControlClick,TmeliEditForm::DoControlClick)
ENDDISPATCH


TmeliEditForm::TmeliEditForm( struct tag_entry *e ) : TTagEditForm( e )
{
}

// ==============================  INITIALIZE FORM =======================================
void TmeliEditForm::DoFormInitialize()
{
	TTagEditForm::DoFormInitialize();

}


// ==============================  INPUT HANDLING  ========================================
long TmeliEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif
	return -1;
}

		
// ========================================  SAVE TO TAG  ==================================================
void TmeliEditForm::CommitToTag()
{
	GetValues();
	save_mesh_lighting_data( &def, fEntry );
}


// ========================================  RELOAD TAG  ==================================================
void TmeliEditForm::ReloadTag()
{
	read_mesh_lighting_data( &def, fEntry );
	SetValues();
}


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TmeliEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "meli", fEntry, &def, SIZEOF_STRUCT_MESH_LIGHTING_DATA, 1, mesh_lighting_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TmeliEditForm::SetValues()
{
	SetFixed(11201, def.incident_light.i);
	SetFixed(11202, def.incident_light.j);
	SetFixed(11203, def.incident_light.k);
	SetFixed(11204, def.ambient_light_intensity);	
	SetFixed(11205, def.maximum_diffuse_light_intensity);
	SetFixed(11206, def.model_shading_diffuse_light_intensity);
	SetFixed(11207, def.maximum_total_light_intensity);
	SetInt(11208, def.maximum_shadow_distance);
	SetFloat(11209, def.gaussian_width);
	SetInt(11210, def.filter_width);
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TmeliEditForm::GetValues()
{
	def.incident_light.i = GetFixed(11201);
	def.incident_light.j = GetFixed(11202);
	def.incident_light.k = GetFixed(11203);
	def.ambient_light_intensity = GetFixed(11204);	
	def.maximum_diffuse_light_intensity = GetFixed(11205);
	def.model_shading_diffuse_light_intensity = GetFixed(11206);
	def.maximum_total_light_intensity = GetFixed(11207);
	def.maximum_shadow_distance = GetInt(11208);
	def.gaussian_width = GetFloat(11209);
	def.filter_width = GetInt(11210);
}

