
#include "conn.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 connector_byte_swapping_data[]=
{
	_begin_bs_array, 1,

	//	unsigned long flags;
	_4byte,

	//	file_tag collection_reference_tag;
	_4byte,

	//	short normal_sequence_index;
	_2byte,
	
	// short_fixed origin_object_height_fraction;
	_2byte,	

	// short_world_distance distance_between_interpolants;
	_2byte,	

	//	short damaged_sequence_index;
	_2byte,

	//------- postprocessed stuff
	//	short collection_reference_index;
	//	short collection_index;
	//	short color_table_index;
	3*sizeof(short),
	
	// short unused[5];
	5*sizeof(short),

	_end_bs_array
};

connector_definition default_conn =
{
	0,
	-1,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0, 0, 0, 0, 0
};

// ==============================  EDIT WINDOW  ========================================
void edit_conn( tag_entry* e )
{
	TconnEditForm* form = new TconnEditForm( e );
	form->AttachWindow( 1003 );
}

void defaultnew_conn( tag_entry *e )
{
	connector_definition def;
	
	byte_swap_move_be( "conn", &def, &default_conn, sizeof(connector_definition), 1, connector_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_conn( tag_entry *e )
{
	connector_definition def;
	read_connector_definition( &def, e );
	add_tag_dependency( 'core', def.collection_reference_tag );
}


// ==============================  READ TAG DATA  ========================================
bool read_connector_definition( connector_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "conn", e, def, SIZEOF_STRUCT_CONNECTOR_DEFINITION, 1, connector_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_connector_definition( connector_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "conn", e, def, SIZEOF_STRUCT_CONNECTOR_DEFINITION, 1, connector_byte_swapping_data);
}


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


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

	return save_connector_definition( def, e );
}


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


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

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

}


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

	tag_entry* e;
	tag new_id;
	
	switch( arg )
	{
		case 10301:	// Choose a tag
			new_id = pick_tag_of_type('core', GetTag( 10301 ), fEntry );
			if(new_id != 0) SetTag( 10301, 'core', new_id );
		break; 
		
		case 10302:	// Open tag window
			e = get_entry('core', GetTag( 10301 ) );
			if(e) TRevengeApp::OpenItem( e );
		break;
		
		default:
			return -1;
	}
	
	return 0;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TconnEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "conn", fEntry, &def, SIZEOF_STRUCT_CONNECTOR_DEFINITION, 1, connector_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TconnEditForm::SetValues()
{
	int i;

	SetTag( 10301, 'core', def.collection_reference_tag );
	
	XGPopControl* pList;
	pList = dynamic_cast<XGPopControl*>(GetWindow()->FindViewByID( 10303 ));
	assert(pList);
	pList->DeleteAll();
	pList->Insert(0, "None");
	for (i = 1; i < 200; i++) {
		pList->Insert(i, "Sequence");
	}
	pList->SetValue( def.normal_sequence_index );

	XGPopControl* pList2;
	pList2 = dynamic_cast<XGPopControl*>(GetWindow()->FindViewByID( 10305 ));
	assert(pList2);
	pList2->DeleteAll();
	pList2->Insert(0, "None");
	for (i = 1; i < 200; i++) {
		pList2->Insert(i, "Sequence");
	}
	pList2->SetValue( def.damaged_sequence_index );

	SetShortFixed( 10307, def.origin_object_height_fraction );
	SetShortWorldDistance( 10308, def.distance_between_interpolants );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TconnEditForm::GetValues()
{

	def.collection_reference_tag = GetTag( 10301 );
	
	XGPopControl* pList;
	pList = dynamic_cast<XGPopControl*>(GetWindow()->FindViewByID( 10303 ));
	assert(pList);
	def.normal_sequence_index = pList->GetValue();
	
	XGPopControl* pList2;
	pList2 = dynamic_cast<XGPopControl*>(GetWindow()->FindViewByID( 10305 ));
	assert(pList2);
	def.damaged_sequence_index = pList2->GetValue();

	def.origin_object_height_fraction = GetShortFixed( 10307 );
	def.distance_between_interpolants = GetShortWorldDistance( 10308 );
}

