
#include "loli.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 loli_definition_byte_swapping_data[]=
{
	_begin_bs_array, 1,
		//	word flags;
		_2byte,

		//	short fade_in_ticks;
		_2byte,
		//	short sustain_ticks;
		_2byte,
		//	short fade_out_ticks;
		_2byte,

		//	world_distance radius;
		_4byte,

		//	struct rgb_color dim_color;
		_2byte, _2byte, _2byte, _2byte,
		//	struct rgb_color bright_color;
		_2byte, _2byte, _2byte, _2byte,

		//	fixed flicker_speed_0;
		_2byte,
		//	fixed flicker_speed_1;
		_2byte,
		
		//	word flicker_amplitude_0;
		_2byte,
		//	word flicker_amplitude_1;
		_2byte,

		//	fixed jitter_speed_x;
		_2byte,
		//	short_world_distance jitter_size_x;
		_2byte,
		
		//	fixed jitter_speed_y;
		_2byte,
		//	short_world_distance jitter_size_y;
		_2byte,
		
		//	fixed jitter_speed_z;
		_2byte,
		//	short_world_distance jitter_size_z;
		_2byte,
		
	_end_bs_array
};


// ==============================  EDIT WINDOW  ========================================
void edit_loli( tag_entry* e )
{
	TloliEditForm* form = new TloliEditForm( e );
	form->AttachWindow( 1030 );
}


static loli_definition default_loli =
{
	0,
	10,
	-1,
	10,
	INTEGER_TO_WORLD(5),
	{ 0x8080, 0x4040, 0x0000, 0 },
	{ 0xffff, 0xc0c0, 0x4040, 0 },
	2048,
	10000,
	10,
	2,
	
	2048,
	0,
	
	2048,
	0,
	
	2048,
	0
};

void defaultnew_loli( tag_entry* e )
{
	loli_definition def;
	byte_swap_move_be( "loli", &def, &default_loli, sizeof(loli_definition), 1, loli_definition_byte_swapping_data);
	set_entry_data( e, &def, sizeof(def), false );
}


void dependency_loli( tag_entry *e )
{
#if 0 // no dependencies
	loli_definition def;
	read_loli_definition( &def, e );
#endif
}


// ==============================  READ TAG DATA  ========================================
bool read_loli_definition( loli_definition* def, tag_entry* e )
{
	return load_tag_data_into_struct( "loli", e, def, sizeof(loli_definition), 1, loli_definition_byte_swapping_data);
}


// ==============================  SAVE TAG DATA  ========================================
bool save_loli_definition( loli_definition* def, tag_entry* e )
{
	return save_tag_data_from_struct( "loli", e, def, sizeof(loli_definition), 1, loli_definition_byte_swapping_data);
}


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


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

	return save_loli_definition( def, e );
}


// ==============================  DISPATCH  ========================================
BEGINDISPATCH(TloliEditForm,TTagEditForm)
	DISPATCHTABLE(KXGControlClick,TloliEditForm::DoControlClick)
	DISPATCHTABLE(KXGKeyClick,TloliEditForm::DoKeyClick)
ENDDISPATCH


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

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

}


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

	return -1;
}

		
long TloliEditForm::DoKeyClick( long arg, void * )
{
	if (arg >= 16 && arg <= 22) {
		def.dim_color.red = GetInt( 16 ) * 0x101;
		def.dim_color.green = GetInt( 17 ) * 0x101;
		def.dim_color.blue = GetInt( 18 ) * 0x101;
		def.bright_color.red = GetInt( 20 ) * 0x101;
		def.bright_color.green = GetInt( 21 ) * 0x101;
		def.bright_color.blue = GetInt( 22 ) * 0x101;
		SetColor( 15, def.dim_color );
		SetColor( 19, def.bright_color );
		return 0;
	}
	return -1;
}

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


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


// ========================================  TAG NEEDS UPDATING  ==================================================
bool TloliEditForm::NeedCommit()
{
	GetValues();
	return tag_data_needs_update( "loli", fEntry, &def, sizeof(loli_definition), 1, loli_definition_byte_swapping_data);
}



// ========================================  SET INTERFACE VALUES  ==================================================
void TloliEditForm::SetValues()
{
	SetWorldDistance( 1, def.radius );
	SetAngle( 2, def.jitter_speed_x );
	SetShortWorldDistance( 3, def.jitter_size_x );
	SetAngle( 4, def.jitter_speed_y );
	SetShortWorldDistance( 5, def.jitter_size_y );
	SetAngle( 6, def.jitter_speed_z );
	SetShortWorldDistance( 7, def.jitter_size_z );
	SetAngle( 8, def.flicker_speed_0 );
	SetInt( 9, def.flicker_amplitude_0 );
	SetAngle( 10, def.flicker_speed_1 );
	SetInt( 11, def.flicker_amplitude_1 );
	SetInt( 12, def.fade_in_ticks );
	SetInt( 13, def.sustain_ticks );
	SetInt( 14, def.fade_out_ticks );
	SetColor( 15, def.dim_color );
	SetInt( 16, def.dim_color.red >> 8 );
	SetInt( 17, def.dim_color.green >> 8 );
	SetInt( 18, def.dim_color.blue >> 8 );
	SetColor( 19, def.bright_color );
	SetInt( 20, def.bright_color.red >> 8 );
	SetInt( 21, def.bright_color.green >> 8 );
	SetInt( 22, def.bright_color.blue >> 8 );
}


// ========================================  GET INTERFACE VALUES  ==================================================
void TloliEditForm::GetValues()
{
	def.radius = GetWorldDistance( 1 );
	def.jitter_speed_x = GetAngle( 2 );
	def.jitter_size_x = GetShortWorldDistance( 3 );
	def.jitter_speed_y = GetAngle( 4 );
	def.jitter_size_y = GetShortWorldDistance( 5 );
	def.jitter_speed_z = GetAngle( 6 );
	def.jitter_size_z = GetShortWorldDistance( 7 );
	def.flicker_speed_0 = GetAngle( 8 );
	def.flicker_amplitude_0 = GetInt( 9 );
	def.flicker_speed_1 = GetAngle( 10 );
	def.flicker_amplitude_1 = GetInt( 11 );
	def.fade_in_ticks = GetInt( 12 );
	def.sustain_ticks = GetInt( 13 );
	def.fade_out_ticks = GetInt( 14 );
	def.dim_color.red = GetInt( 16 ) * 0x101;
	def.dim_color.green = GetInt( 17 ) * 0x101;
	def.dim_color.blue = GetInt( 18 ) * 0x101;
	def.bright_color.red = GetInt( 20 ) * 0x101;
	def.bright_color.green = GetInt( 21 ) * 0x101;
	def.bright_color.blue = GetInt( 22 ) * 0x101;
}

