
#include "wind.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  ========================================
static byte_swap_code wind_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,

//	unsigned long flags;
	_4byte,

//	int time_between_adjustments_lower_bound, time_between_adjustments_delta;
//	fixed maximum_adjustment_per_tick;
	_4byte, _4byte,
	_4byte,

//	fixed_vector3d velocity_lower_bound;
//	fixed_vector3d velocity_delta;
	_4byte, _4byte, _4byte,
	_4byte, _4byte, _4byte,

	//------- postprocessed stuff
//	int ticks_until_wind_changes;
//	fixed_vector3d wind_velocity;
//	fixed_vector3d target_wind;
	sizeof(int),
	2*sizeof(fixed_vector3d),

//	short unused[30];
	30*sizeof(short),
	
	_end_bs_array
};

wind_definition default_wind =
{
	0,
	0, 0,
	0,
	0, 0, 0,
	0, 0, 0,
	0,
	0, 0, 0,
	0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 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_wind( tag_entry* e )
{
	TwindEditForm* form = new TwindEditForm( e );
	form->AttachWindow( 1028 );
}

void defaultnew_wind( tag_entry *e )
{
	wind_definition def;
	byte_swap_move_be( "wind", &def, &default_wind, sizeof(wind_definition), 1, wind_definition_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_wind( tag_entry *e )
{
#if 0 // no dependencies
	wind_definition def;
	read_wind_definition( &def, e );
#endif
}

// ==============================  READ TAG DATA  ========================================
bool read_wind_definition( wind_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "wind", e, def, SIZEOF_STRUCT_WIND_DEFINITION, 1, wind_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_wind_definition( wind_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "wind", e, def, SIZEOF_STRUCT_WIND_DEFINITION, 1, wind_definition_byte_swapping_data);
}


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


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

	return save_wind_definition( def, e );
}


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


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

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

}


// ==============================  INPUT HANDLING  ========================================
long TwindEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif

	return -1;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TwindEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "wind", fEntry, &def, SIZEOF_STRUCT_WIND_DEFINITION, 1, wind_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TwindEditForm::SetValues()
{
	SetFloat( 12801, def.time_between_adjustments_lower_bound / TICKS_PER_SECOND, 3 );
	SetFloat( 12802, get_upper_bound( (float)def.time_between_adjustments_lower_bound, (float)def.time_between_adjustments_delta ) / TICKS_PER_SECOND, 3 );
	SetFixed( 12803, def.maximum_adjustment_per_tick );
	SetFixed( 12804, def.velocity_lower_bound.i );
	SetFixed( 12805, get_upper_bound( def.velocity_lower_bound.i, def.velocity_delta.i ) );
	SetFixed( 12806, def.velocity_lower_bound.j );
	SetFixed( 12807, get_upper_bound( def.velocity_lower_bound.j, def.velocity_delta.j ) );
	SetFixed( 12808, def.velocity_lower_bound.k );
	SetFixed( 12809, get_upper_bound( def.velocity_lower_bound.k, def.velocity_delta.k ) );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TwindEditForm::GetValues()
{
	def.time_between_adjustments_lower_bound = GetFloat( 12801 ) * TICKS_PER_SECOND;
	def.time_between_adjustments_delta = get_delta( (float)GetFloat( 12801 ) * TICKS_PER_SECOND, (float)GetFloat( 12802 ) * TICKS_PER_SECOND );
	def.maximum_adjustment_per_tick = GetFixed( 12803 );
	def.velocity_lower_bound.i = GetFixed( 12804 );
	def.velocity_delta.i = get_delta( GetFixed( 12804 ), GetFixed( 12805 ) );
	def.velocity_lower_bound.j = GetFixed( 12806 );
	def.velocity_delta.j = get_delta( GetFixed( 12806 ), GetFixed( 12807 ) );
	def.velocity_lower_bound.k = GetFixed( 12808 );
	def.velocity_delta.k = get_delta( GetFixed( 12808 ), GetFixed( 12809 ) );
}

