
#include "form.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 formation_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	// SHORT LINE
	// short maximum_units_per_short_line_row;
	// short_world_distance short_line_unit_separation;
	_2byte,
	_2byte,

	// LONG LINE
	// short maximum_units_per_long_line_row;
	// short_world_distance long_line_unit_separation;
	_2byte,
	_2byte,

	// LOOSE LINE
	// short maximum_units_per_loose_line_row;
	// short_world_distance loose_line_unit_separation;
	_2byte,
	_2byte,

	// STAGGERED LINE
	// short maximum_units_per_staggered_line_row;
	// short_world_distance staggered_line_unit_separation;
	_2byte,
	_2byte,

	// BOX
	// short_world_distance box_unit_separation;
	_2byte,

	// MOB
	// short_world_distance minimum_mob_unit_separation;
	// short_fixed mob_x_bias;
	_2byte,
	_2byte,

	// short_world_distance minimum_encirclement_radius;
	// short_world_distance encirclement_unit_separation;
	_2byte,
	_2byte,

	// SHALLOW ENCIRCLEMENT
	// angle shallow_encirclement_arc_length;
	_2byte,

	// DEEP ENCIRCLEMENT
	// angle deep_encirclement_arc_length;
	_2byte,

	// VANGUARD
	// short_world_distance vanguard_front_row_separation;
	// short_world_distance vanguard_unit_separation;
	// short_world_distance vanguard_row_separation;
	_2byte,
	_2byte,
	_2byte,
	
	// CIRCLE
	// short_world_distance circle_unit_separation;
	_2byte,
	
	// short unused;
	sizeof(short),
	
	_end_bs_array
};

formation_definition default_form =
{
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	0
};

// ==============================  EDIT WINDOW  ========================================
void edit_form( tag_entry* e )
{
	TformEditForm* form = new TformEditForm( e );
	form->AttachWindow( 1005 );
}

void defaultnew_form( tag_entry *e )
{
	formation_definition def;
	
	byte_swap_move_be( "form", &def, &default_form, sizeof(formation_definition), 1, formation_definition_byte_swapping_data );
	set_entry_data( e, &def, sizeof(def), false );
}

void dependency_form( tag_entry *e )
{
#if 0 // no dependencies
	formation_definition def;
	read_formation_definition( &def, e );
#endif
}



// ==============================  READ TAG DATA  ========================================
bool read_formation_definition( formation_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "form", e, def, SIZEOF_STRUCT_FORMATION_DEFINITION, 1, formation_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_formation_definition( formation_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "form", e, def, SIZEOF_STRUCT_FORMATION_DEFINITION, 1, formation_definition_byte_swapping_data);
}


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


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

	return save_formation_definition( def, e );
}


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


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

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

}


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

	return -1;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TformEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "form", fEntry, &def, SIZEOF_STRUCT_FORMATION_DEFINITION, 1, formation_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TformEditForm::SetValues()
{
	
	SetInt( 10501, def.maximum_units_per_short_line_row );
	SetShortWorldDistance( 10502, def.short_line_unit_separation );

	SetInt( 10503, def.maximum_units_per_long_line_row );
	SetShortWorldDistance( 10504, def.long_line_unit_separation );
	
	SetInt( 10505, def.maximum_units_per_loose_line_row );
	SetShortWorldDistance( 10506, def.loose_line_unit_separation );
	
	SetInt( 10507, def.maximum_units_per_staggered_line_row );
	SetShortWorldDistance( 10508, def.staggered_line_unit_separation );

	SetShortWorldDistance( 10509, def.box_unit_separation );

	SetShortWorldDistance( 10510, def.minimum_mob_unit_separation );
	SetShortFixed( 10511, def.mob_x_bias );
	
	SetShortWorldDistance( 10512, def.minimum_encirclement_radius );
	SetShortWorldDistance( 10513, def.encirclement_unit_separation );
	SetAngle( 10514, def.shallow_encirclement_arc_length );
	SetAngle( 10515, def.deep_encirclement_arc_length );

	SetShortWorldDistance( 10516, def.vanguard_front_row_separation );
	SetShortWorldDistance( 10517, def.vanguard_unit_separation );
	SetShortWorldDistance( 10518, def.vanguard_row_separation );

	SetShortWorldDistance( 10519, def.circle_unit_separation );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TformEditForm::GetValues()
{
	
	def.maximum_units_per_short_line_row = GetInt( 10501 );
	def.short_line_unit_separation = GetShortWorldDistance( 10502 );

	def.maximum_units_per_long_line_row = GetInt( 10503 );
	def.long_line_unit_separation = GetShortWorldDistance( 10504 );
	
	def.maximum_units_per_loose_line_row = GetInt( 10505 );
	def.loose_line_unit_separation = GetShortWorldDistance( 10506 );
	
	def.maximum_units_per_staggered_line_row = GetInt( 10507 );
	def.staggered_line_unit_separation = GetShortWorldDistance( 10508 );

	def.box_unit_separation = GetShortWorldDistance( 10509 );

	def.minimum_mob_unit_separation = GetShortWorldDistance( 10510 );
	def.mob_x_bias = GetShortFixed( 10511 );
	
	def.minimum_encirclement_radius = GetShortWorldDistance( 10512 );
	def.encirclement_unit_separation = GetShortWorldDistance( 10513 );
	def.shallow_encirclement_arc_length = GetAngle( 10514 );
	def.deep_encirclement_arc_length = GetAngle( 10515 );

	def.vanguard_front_row_separation = GetShortWorldDistance( 10516 );
	def.vanguard_unit_separation = GetShortWorldDistance( 10517 );
	def.vanguard_row_separation = GetShortWorldDistance( 10518 );

	def.circle_unit_separation = GetShortWorldDistance( 10519 );
}

