
#include "anim.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 animation_shadow_map_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	//short width, height;
	_2byte, _2byte,

	//short bytes_per_row;
	//word pad;
	_2byte,
	sizeof(short),

	//byte *shadow_map;
	_4byte,
	
	_end_bs_array
};

static byte_swap_code model_animation_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,
	
	// unsigned long flags;
	_4byte,
	
	// short number_of_frames;
	// short ticks_per_frame;
	_2byte,
	_2byte,
	
	// struct model_animation_frame_definition[MAXIMUM_FRAMES_PER_ANIMATION];
	
		_begin_bs_array, MAXIMUM_FRAMES_PER_ANIMATION,
		
		// unsigned long flags;
		_4byte,
		
		// file_tag model_tag;
		// short model_index; // postprocessed
		_4byte,
		sizeof(short),

		// short permutation_index;
		_2byte,

		// word unused[2];
		2*sizeof(short),
		
		_end_bs_array,
	
	// word unused[222];
	222*sizeof(short),
	
	// short shadow_map_width, shadow_map_height;
	// short shadow_bytes_per_row;
	// word pad;
	_2byte, _2byte,
	_2byte,
	sizeof(short),

	// short origin_offset_x, origin_offset_y;
	_2byte, _2byte,

	// file_tag forward_sound_tag;
	// file_tag backward_sound_tag;
	_4byte,
	_4byte,
	
	// short forward_sound_type;
	// short backward_sound_type;
	2*sizeof(short),

	// word unused2[20];
	20*sizeof(short),

	// int shadow_maps_offset, shadow_maps_size;
	// byte *shadow_maps;
	_4byte, _4byte,
	_4byte,
	
	_end_bs_array
};


// ==============================  EDIT WINDOW  ========================================
void edit_anim( tag_entry* e )
{
	TanimEditForm* form = new TanimEditForm( e );
	form->AttachWindow( 1014 );
}


// ==============================  READ TAG DATA  ========================================
bool read_model_animation_definition( model_animation_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "anim", e, def, SIZEOF_STRUCT_MODEL_ANIMATION_DEFINITION, 1, model_animation_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_model_animation_definition( model_animation_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "anim", e, def, SIZEOF_STRUCT_MODEL_ANIMATION_DEFINITION, 1, model_animation_definition_byte_swapping_data);
}


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


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

	return save_model_animation_definition( def, e );
}


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


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

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

}


// ==============================  INPUT HANDLING  ========================================
long TanimEditForm::DoControlClick( long arg, void *ptr )
{
#ifdef __MWERKS__
	#pragma unused(ptr)
#endif
/*
	tag_entry* e;
	tag new_id;
	
	switch( arg )
	{
		case 10001:
			// Choose a tag
			new_id = pick_tag_of_type('soun', GetTag( 10001 ));
			if(new_id != 0) {
				SetTag( 10001, 'soun', new_id );
			}
		break; 
		
		case 10002:
			// Open tag window
			e = get_entry('soun', GetTag( 10001 ) );
			if(e) {
				TRevengeApp::OpenItem( e );
			}
		break;
		
		default:
			return -1;
	}
*/	
	return -1;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TanimEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "anim", fEntry, &def, SIZEOF_STRUCT_MODEL_ANIMATION_DEFINITION, 1, model_animation_definition_byte_swapping_data);
}

		
// ========================================  SET INTERFACE VALUES  ==================================================
void TanimEditForm::SetValues()
{
/*
	SetTag( 10001, 'soun', def.sound_tags[0] );
	if(def.flags == _ambient_sound_background_sound) {
		SetFlag( 10003, FALSE );
	} else {
		SetFlag( 10003, TRUE );
	}
		
	SetShortWorldDistance( 10005, def.inner_radius );
	SetShortWorldDistance( 10006, def.outer_radius );
	SetFloat( 10007, def.period_lower_bound / (float)TICKS_PER_SECOND, 3 );
	SetFloat( 10008, (def.period_lower_bound + def.period_delta) / (float)TICKS_PER_SECOND, 3 );
	SetShortWorldDistance( 10009, def.random_sound_radius );
*/
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TanimEditForm::GetValues()
{
/*
	def.sound_tags[0] = GetTag( 10001 );
	if(GetFlag( 10003 )) {
		def.flags = _ambient_sound_random_sound;
	} else {
		def.flags = _ambient_sound_background_sound;
	}
	
	def.inner_radius = GetShortWorldDistance( 10005 );
	def.outer_radius = GetShortWorldDistance( 10006 );
	def.period_lower_bound = GetFloat( 10007 ) * (float)TICKS_PER_SECOND;
	def.period_delta = (GetFloat( 10008 ) * (float)TICKS_PER_SECOND) - (float)def.period_lower_bound;
	def.random_sound_radius = GetShortWorldDistance( 10009 );
*/
}

