//==========================================================================//
// File:	controls.tst                                                       //
// Project: MUNGA           Brick: COntrols module                          //
// Contents: Test code for vector classes                                   //
//--------------------------------------------------------------------------//
//   Date    Who  Modification                                              //
// --------  ---  ----------------------------------------------------------//
// 12/12/94  CPB  Initial coding.                                           //
//--------------------------------------------------------------------------//
// Copyright (C) 1994, Virtual World Entertainment, Inc.                    //
// All Rights reserved worldwide                                            //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL              //
//==========================================================================//

#include "AdeptHeaders.hpp"
#include "controls_test.hpp"

//
//###########################################################################
//###########################################################################
//
TestControlsManager::TestControlsManager():
	ControlsManager()
{
}

//
//###########################################################################
//###########################################################################
//
TestControlsManager::~TestControlsManager()
{
}

//
//###########################################################################
//###########################################################################
//
void
	TestControlsManager::Execute()
{
}

//###########################################################################
//############################# TestReceiver ################################
//###########################################################################

#if 0
Derivation
	TestControlsReceiver::ClassDerivation(
		NullClassID,
		&TestControlsReceiver::DefaultData,
		Receiver::ClassDerivation,
		"TestControlsReceiver1"
	);

TestControlsReceiver::ClassData
	TestControlsReceiver::DefaultData(
		TestControlsReceiver::ClassDerivation,
		TestControlsReceiver::MessageHandlers
	);
#endif

TestControlsReceiver::TestControlsReceiver():
	Receiver(DefaultData)
{
	keyStroke = 0;
	joystickValue.x = 0.0f;
	joystickValue.y = 0.0f;
}

TestControlsReceiver::~TestControlsReceiver()
{
}

void
TestControlsReceiver::KeyboardMessageHandler(
	ReceiverDataMessageOf<int> *message
)
{
	keyStroke = message->dataContents;
}

void
TestControlsReceiver::JoystickMessageHandler(
	ReceiverDataMessageOf<Vector2DOf<Scalar> > *message
)
{
	joystickValue = message->dataContents;
}

const Receiver::MessageEntry
	TestControlsReceiver::MessageEntries[]=
{
	MESSAGE_ENTRY(TestControlsReceiver, Keyboard),
	MESSAGE_ENTRY(TestControlsReceiver, Joystick)
};

#if 0
Receiver::MessageHandlerSet
	TestControlsReceiver::MessageHandlers(
		ELEMENTS(TestControlsReceiver::MessageHandlerEntries),
		TestControlsReceiver::MessageHandlerEntries,
		Receiver::MessageHandlers
	);
#endif

//
//###########################################################################
//###########################################################################
//
bool
	ControlsManager::TestClass()
{
#if 0
	Test_Message("Starting ControlsManager test...\n");
	unsigned int fakeOwnerID(1);

	Vector2DOf<Scalar> vector1(0.0f, 0.0f);
	Vector2DOf<Scalar> vector2(1.0f, 2.0f);
	Vector2DOf<Scalar> vector3(3.0f, 4.0f);
	//
	//--------------------------------------------------------
	// Test ControlsInstanceDirectOf...
	//--------------------------------------------------------
	//
	{
		ControlsInstanceDirectOf< Vector2DOf<Scalar> > *instance1;

		instance1 = new ControlsInstanceDirectOf< Vector2DOf<Scalar> >(
			fakeOwnerID,&vector1
		);

		Check(instance1);
		Register_Object(instance1);

		instance1->Update(&vector2);
		Test(vector1 == vector2);

		Unregister_Object(instance1);
		delete instance1;
	}
	//
	//--------------------------------------------------------
	// Test ControlsMappingGroup
	//--------------------------------------------------------
	//
	{
		ControlsMappingGroup	testGroup;
		Check(&testGroup);

		ControlsInstanceDirectOf< Vector2DOf<Scalar> > *instance1;
		instance1 = new ControlsInstanceDirectOf< Vector2DOf<Scalar> >(
			fakeOwnerID,
			&vector1
		);
		Check(instance1);
		Register_Object(instance1);
		
		ControlsInstanceDirectOf< Vector2DOf<Scalar> > *instance2;
		instance2 = new ControlsInstanceDirectOf< Vector2DOf<Scalar> > (
			fakeOwnerID,
			&vector2
		);
		Check(instance2);
		Register_Object(instance2);
				
		testGroup.Add(instance1);
		testGroup.Add(instance2);

		ChainIteratorOf<ControlsInstance>
			i(testGroup.instanceList);

		ControlsInstance
			*controls_instance;

		while ((controls_instance=i.ReadAndNext()) != NULL)
		{
			Check(controls_instance);
		}

		testGroup.Update(&vector3);
		Test(vector1 == vector3);
		Test(vector2 == vector3);

		Unregister_Object(instance2);
		delete instance2;

		Unregister_Object(instance1);
		delete instance1;
	}
	//
	//--------------------------------------------------------
	// Create Receiver
	//--------------------------------------------------------
	//
	TestControlsReceiver	*my_receiver;

	my_receiver = new TestControlsReceiver();
	Check(my_receiver);
	Register_Object(my_receiver);

	//
	//--------------------------------------------------------
	// Test ControlsInstanceEventOf...
	//--------------------------------------------------------
	//
	{
		int	q0, q1;
		int	testKey = 0;

		ControlsInstanceEventOf< int > *instance1;

		instance1 = new ControlsInstanceEventOf< int > (
			fakeOwnerID,
			my_receiver,
			TestControlsReceiver::KeyboardMessageID
		);
		Check(instance1);
		Register_Object(instance1);

		ControlsInstanceDirectOf< int > *instance2;
		instance2 = new ControlsInstanceDirectOf< int > (
			fakeOwnerID,
			&testKey
		);
		Check(instance2);
		Register_Object(instance2);
				
		Test(my_receiver->keyStroke == 0);
		q0 = 1;
		instance1->Update(&q0);
		Test(my_receiver->keyStroke == q0);
		//
		//--------------------------------------------------------
		// Test ControlsMappingGroup, again
		//--------------------------------------------------------
		//
		ControlsMappingGroup	testGroup;
		Check(&testGroup);
		testGroup.Add(instance1);
		testGroup.Add(instance2);

		q1=2;
		testGroup.Update(&q1);
		Test(my_receiver->keyStroke == q1);
		Test(testKey == q1);

		Unregister_Object(instance2);
		delete instance2;
		Unregister_Object(instance1);
		delete instance1;
	}
	//
	//--------------------------------------------------------
	// Test ControlsUpdateManager
	//--------------------------------------------------------
	//
	{
		ControlsUpdateManager<int>	intGroup;
		int	source = 1;
		int	dest1 = 0;
		int	dest2 = 0;

		intGroup.Add(0,&dest1);
		intGroup.Add(0,&dest2);

		intGroup.ForceUpdate(&source);

		Test(dest1 == source);
		Test(dest2 == source);

		intGroup.Remove(0);

		dest1 = 0;
		dest2 = 0;
		intGroup.ForceUpdate(&source);
		Test(dest1 == 0);
		Test(dest2 == 0);
	}
	//
	//--------------------------------------------------------
	// Destroy Receiver
	//--------------------------------------------------------
	//
	Unregister_Object(my_receiver);
	delete my_receiver;

	//
	//--------------------------------------------------------
	// Return test result
	//--------------------------------------------------------
	//
#endif
	return true;
}

