#include "Graphic.h"
#include "ViewForm.h"
#include "DotNetString.h"
#include "ImageForm.h"
#include <set>

CRoleSprite* pMainSprite = NULL;
CRoleSprite* pTargetSprite = NULL; 

GameObjDbStruct* Obj;

using namespace ObjDataEdit;


enum PaperdollType 
{
	ePaperdoll_Torso	,	
	ePaperdoll_Hand		,
	ePaperdoll_Shoulder	,
	ePaperdoll_Back		,
	ePaperdoll_Belt		,
	ePaperdoll_Foot		,
	ePaperdoll_Leg		,
	ePaperdoll_Helmet	,                 
	ePaperdoll_Hair		,
	ePaperdoll_Face		,

	ePaperdoll_End
};

char* PaperdollTypeName[ ePaperdoll_End ] = 
				{		
				  PAPERDOLL_PART_NAME_TORSO
				, PAPERDOLL_PART_NAME_HAND
				, PAPERDOLL_PART_NAME_SHOULDER
				, PAPERDOLL_PART_NAME_BACK
				, PAPERDOLL_PART_NAME_BELT
				, PAPERDOLL_PART_NAME_FOOT
				, PAPERDOLL_PART_NAME_LEG
				, PAPERDOLL_PART_NAME_HELMET
				, PAPERDOLL_PART_NAME_HAIR
				, PAPERDOLL_PART_NAME_HEAD

				};

/*
enum PaperdollType 
{
	ePaperdoll_Torso	,	
	ePaperdoll_Hand		,
	ePaperdoll_Shoulder	,
	ePaperdoll_Back		,
	ePaperdoll_Belt		,
	ePaperdoll_Foot		,	
	ePaperdoll_Leg		,
	ePaperdoll_Helmet	,
	ePaperdoll_Face		,
	ePaperdoll_Hair		,

	ePaperdoll_End
};

char* PaperdollTypeName[ ePaperdoll_End ] = 
{		
	  PAPERDOLL_PART_NAME_TORSO
	, PAPERDOLL_PART_NAME_HAND
	, PAPERDOLL_PART_NAME_SHOULDER
	, PAPERDOLL_PART_NAME_BACK
	, PAPERDOLL_PART_NAME_BELT
	, PAPERDOLL_PART_NAME_FOOT
	, PAPERDOLL_PART_NAME_LEG
	, PAPERDOLL_PART_NAME_HELMET
	, PAPERDOLL_PART_NAME_HEAD
	, PAPERDOLL_PART_NAME_HAIR

};
*/
enum  RuneImagePosENUM
{
	EM_RuneImagePos_Head	,
	EM_RuneImagePos_Torso	,
	EM_RuneImagePos_Leg		,
	EM_RuneImagePos_Belt	,
	EM_RuneImagePos_Hand	,
	EM_RuneImagePos_Foot	,
	EM_RuneImagePos_Back	,
	EM_RuneImagePos_Helmet	,
	EM_RuneImagePos_Hair	,
	EM_RuneImagePos_Shoulder,
};

void ViewForm::Init( )
{
    g_ViewForm = new ViewForm;
    g_ViewForm->Show();
    g_ViewForm->Hide();

	g_ViewForm->SetFilePath( );

}

Void ViewForm::ViewForm_Load(System::Object *  sender, System::EventArgs *  e)
{
	char* ResourcePathStr = Global::GetIniString( "ResourcePath" );

	_Clock = 0;
	g_pGraphic = new CGraphic;
    if ( g_pGraphic->RuneDevInitialize( (HWND)PanelScreen->Handle.ToInt32()  , true , ResourcePathStr ) )
    {
		SetResourcePath();
        if( pMainSprite == NULL )
        {
            pMainSprite = new CRoleSprite;
        }
        if( pTargetSprite == NULL )
        {
            pTargetSprite = new CRoleSprite;
            CRuVector3 Pos( 15,0,0);
            pTargetSprite->SetPos( Pos );

        }

		_ComboBoxEQList->Add( CB_Clothes    );        
		_ComboBoxEQList->Add( CB_Gloves     );
		_ComboBoxEQList->Add( CB_Shoulder   );
		_ComboBoxEQList->Add( CB_Back       );
		_ComboBoxEQList->Add( CB_Belt       );
		_ComboBoxEQList->Add( CB_Shoes      );   
		_ComboBoxEQList->Add( CB_Pants      );
		_ComboBoxEQList->Add( CB_Head       );      
		_ComboBoxEQList->Add( CB_Hair		);
		_ComboBoxEQList->Add( CB_Face       );

		_PosIDMap[ EM_GameObjectImageArmorPos_Clothes	] = EM_RuneImagePos_Torso;
		_PosIDMap[ EM_GameObjectImageArmorPos_Gloves	] = EM_RuneImagePos_Hand;
		_PosIDMap[ EM_GameObjectImageArmorPos_Shoulder	] = EM_RuneImagePos_Shoulder;
		_PosIDMap[ EM_GameObjectImageArmorPos_Back		] = EM_RuneImagePos_Back; 
		_PosIDMap[ EM_GameObjectImageArmorPos_Belt		] = EM_RuneImagePos_Belt;
		_PosIDMap[ EM_GameObjectImageArmorPos_Shoes		] = EM_RuneImagePos_Foot;
		_PosIDMap[ EM_GameObjectImageArmorPos_Pants		] = EM_RuneImagePos_Leg;        
		_PosIDMap[ EM_GameObjectImageArmorPos_Head		] = EM_RuneImagePos_Helmet; 
		_PosIDMap[ EM_GameObjectImageArmorPos_Hair		] = EM_RuneImagePos_Hair;		
		_PosIDMap[ EM_GameObjectImageArmorPos_Face		] = EM_RuneImagePos_Head;

 
		timer1->Enabled = true;
    }
	else
	{
		delete g_pGraphic;
		g_pGraphic = NULL;
	}

	CB_MagicPoint->Items->Clear();

	for( unsigned int i = 0 ; i < Global::PointEnumName.size() ; i++ )
	{
		String* Str =  Global::PointEnumName[i].c_str();
		CB_MagicPoint->Items->Add(Str);
	}
    
}

Void ViewForm::OnTimeProc(System::Object *  sender, System::EventArgs *  e)
{

    Global::CritSect.Enter();
	timer1->Enabled = false;

#ifndef _DEBUG
	try
	{
#endif
		if( _IsNeedUpdate  == true )
		{
			_IsNeedUpdate = false;
			UpDateGraphic();
		}

		if( g_pGraphic )
		{
			g_pGraphic->Update( );
			UpDateMagicAction( );
		}
/*
		if( _IsValueChange )
		{
			_IsValueChange = false;
			UpDate();
		}
		*/
#ifndef _DEBUG
	}
	catch(...)
	{
		//MessageBox::Show( "ViewForm::OnTimeProc 錯誤" );
	}
#endif

	timer1->Enabled = true;
    Global::CritSect.Leave();
}


Void ViewForm::ViewForm_Closing(System::Object *  sender, System::ComponentModel::CancelEventArgs *  e)
{
    e->Cancel = true;
    Hide();
}

void ViewForm::SetResourcePath( )
{
	Global::CritSect.Enter();
	char* ResourcePathStr = Global::GetIniString( "ResourcePath" );
	if( g_pGraphic )
		g_pGraphic->SetResourcePath( ResourcePathStr );		
    
    SearchAllItem( ResourcePathStr );
	Global::CritSect.Leave();
}
System::Void ViewForm::BT_unEQ_Click(System::Object*  sender, System::EventArgs*  e)
{
	
	if(		_GameObj.Image.ImageType != EM_GameObjectImageType_Normal 
		&&	!( _GameObj.Image.ImageType == EM_GameObjectImageType_Weapon && CB_ShowWeapoin->Checked ) )
	{		
		pMainSprite->SetObject( "" );  
		pMainSprite->PaperdollBuild();
		pMainSprite->SetObject( DotNetStr::StringToChar( CB_MainObj->Text ).c_str() );  
		pMainSprite->PaperdollBuild();
	}
	
}
System::Void ViewForm::CB_PlayMagicAgain(System::Object*  sender, System::EventArgs*  e)
{
	ResetMagicACT();
}

System::Void ViewForm::PanelScreen_SizeChanged(System::Object *  sender, System::EventArgs *  e)
{
	Global::CritSect.Enter();
    if( PanelScreen->Size.Width != 0 && PanelScreen->Size.Height != 0 )
	{
		if( g_pGraphic )
			g_pGraphic->SetScreenSize( PanelScreen->Size.Width , PanelScreen->Size.Height , true );
	}
	 Global::CritSect.Leave();
    
}

void    ViewForm::SetObjInfo( GameObjDbStruct* ImageObj )
{
	try
	{
		GameObjDbStruct __pin *Data = &_GameObj;
		_GameObj.Classification = EM_ObjectClass_None;
		if( ImageObj->Classification == EM_ObjectClass_Image )
		{
			*Data = *ImageObj;        
			_LObjImageID = 0;
		}
		else 
		{
			if( ImageObj->ImageID == _LObjImageID )
				return;

			_LObjImageID = ImageObj->ImageID;

			GameObjDbStruct* TempObj;
			TempObj = ImageForm::St_GetImageObj( _LObjImageID , sqlCommand1 );
			if( TempObj != NULL )
			{
				*Data = *TempObj;        
			}
		}

		int ArmorPos = Data->Image.ArmorPos;

		_IsNeedUpdate = true;
//		UpDate( );

	}
	catch (...)
	{
		//MessageBox::Show( "ViewForm::SetObjInfo1 有問題" );
	}

}

void    ViewForm::SetObjInfo( int ImageObjID )
{
	try
	{
		GameObjDbStruct __pin *Data = &_GameObj;

		if( ImageObjID == _LObjImageID )
			return;

		_LObjImageID = ImageObjID;

		GameObjDbStruct* TempObj;        
		TempObj = ImageForm::St_GetImageObj( _LObjImageID , sqlCommand1 );
		if( TempObj != NULL )
		{
			*Data = *TempObj;        
		}
		else
		{
			_GameObj.Classification = EM_ObjectClass_None;
		}
//		Global::CritSect.Enter();
		//UpDate( );
		_IsNeedUpdate = true;
		//Global::CritSect.Leave();
	}
	catch (...)
	{
		//MessageBox::Show( "ViewForm::SetObjInfo2 有問題" );
	}

}

#define DEF_PI 3.14159f

void    ViewForm::BaseUpDate( )
{
    GameObjDbStruct __pin *Data = &_GameObj;

	if( _GameObj.Image.ImageType == EM_GameObjectImageType_Magic )
	{
		CRuVector3  MainPos( float( NB_MainX->Value ) , float( NB_MainY->Value ) , float( NB_MainZ->Value ) );
		pMainSprite->SetPos( MainPos );
		pMainSprite->SetDirection( 0 ); 
	}
	else
	{
		CRuVector3  MainPos( 0  , 0 , 0 );
		pMainSprite->SetPos( MainPos );
		pMainSprite->SetDirection( DEF_PI/2 ); 
	}

	if( _GameObj.Image.ImageType == EM_GameObjectImageType_Weapon && CB_ShowWeapoin->Checked == true )
	{
		pMainSprite->PaperdollBuild();
		return;
	}

	//如果為人物物件
	if( _GameObj.Image.ImageType == EM_GameObjectImageType_Normal )
	{
		pMainSprite->SetPaperdollHairColor( (int)Data->Image.HairColor.Color );
		pMainSprite->SetPaperdollSkinColor( (int)Data->Image.SkinColor.Color );

		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_HAIR , Data->Image.PaperdollHair.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_HEAD , Data->Image.PaperdollFace.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_HELMET , Data->Image.PaperdollHead.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_HAND , Data->Image.PaperdollGloves.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_FOOT , Data->Image.PaperdollShoes.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_TORSO , Data->Image.PaperdollClothes.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_LEG , Data->Image.PaperdollPants.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_BACK , Data->Image.PaperdollBack.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_BELT , Data->Image.PaperdollBelt.Name );
		pMainSprite->SetPaperdollPart( PAPERDOLL_PART_NAME_SHOULDER , Data->Image.PaperdollShouder.Name );

		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_HAIR	, Data->Image.PaperdollHair.MainColor.Color   , Data->Image.PaperdollHair.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_HEAD	, Data->Image.PaperdollFace.MainColor.Color   , Data->Image.PaperdollFace.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_HELMET	, Data->Image.PaperdollHead.MainColor.Color   , Data->Image.PaperdollHead.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_HAND	, Data->Image.PaperdollGloves.MainColor.Color , Data->Image.PaperdollGloves.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_FOOT	, Data->Image.PaperdollShoes.MainColor.Color  , Data->Image.PaperdollShoes.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_TORSO	, Data->Image.PaperdollClothes.MainColor.Color, Data->Image.PaperdollClothes.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_LEG		, Data->Image.PaperdollPants.MainColor.Color  , Data->Image.PaperdollPants.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_BACK	, Data->Image.PaperdollBack.MainColor.Color   , Data->Image.PaperdollBack.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_BELT	, Data->Image.PaperdollBelt.MainColor.Color   , Data->Image.PaperdollBelt.OffColor.Color );
		pMainSprite->SetPaperdollColor( PAPERDOLL_PART_NAME_SHOULDER, Data->Image.PaperdollShouder.MainColor.Color, Data->Image.PaperdollShouder.OffColor.Color );

		pMainSprite->SetBoneScale( BONE_SCALE_NAME_CHEST		, float( Data->Image.BoneScaleChest )	/ 100 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_ARM			, float( Data->Image.BoneScaleHand )	/ 100 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_PELVIS		, float( Data->Image.BoneScaleButtock )	/ 100 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_LEGS			, float( Data->Image.BoneScaleLeg )		/ 100 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_BREASTS		, float( Data->Image.BoneScaleBra )		/ 100 );

//		pMainSprite->
	}
	else
	{

		for(int i = 0; i < _ComboBoxEQList->Count; i++ )
		{
			ComboBox* comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[i] );
			
			pMainSprite->SetPaperdollColor( PaperdollTypeName[i] , 0 , 0 );

			if( comboBox->Text->Length == 0 )
				continue;
			pMainSprite->SetPaperdollPart( PaperdollTypeName[i] , DotNetStr::StringToChar( comboBox->Text ).c_str()  );
		}
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_CHEST		, 1 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_ARM			, 1 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_PELVIS		, 1 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_LEGS			, 1 );
		pMainSprite->SetBoneScale( BONE_SCALE_NAME_BREASTS		, 1 );

		pMainSprite->SetPaperdollHairColor( 0 );
		pMainSprite->SetPaperdollSkinColor( 0 );
	}

    if( CB_MainHand->Text->Length == 0 )
        pMainSprite->SetRightObject( NULL );
    else
        pMainSprite->SetRightObject( DotNetStr::StringToChar( CB_MainHand->Text ).c_str() );

    if( CB_SecondHand->Text->Length == 0 )
        pMainSprite->SetLeftObject( NULL );
    else
        pMainSprite->SetLeftObject( DotNetStr::StringToChar( CB_SecondHand->Text ).c_str() );

    pMainSprite->PaperdollBuild();

	//如果沒設定目標物	
    if( pTargetSprite->SetObject( DotNetStr::StringToChar( CB_TargetObj->Text ).c_str() ) == false )
	{
		pTargetSprite->SetObject( DotNetStr::StringToChar( CB_MainObj->Text ).c_str() );
	}
	pTargetSprite->PaperdollBuild();

    CRuVector3 TargetPos( float( NB_TargetX->Value ) , float( NB_TargetY->Value ) , float( NB_TargetZ->Value ) );
    pTargetSprite->SetPos( TargetPos );
	pTargetSprite->SetDirection( DEF_PI );

    CB_MotionList->Items->Clear();

	CRuArrayList<const char *> motionNamesOut;
	if( pMainSprite->GetRuEntity() != NULL )
	{
		RuACT_EnumerateMotionNames(pMainSprite->GetRuEntity() , motionNamesOut );
		for( int i = 0 ; i < motionNamesOut.Count() ; i++ )
		{
			String* Str = String::Format( "{0}" , (String*)motionNamesOut[i] );
			CB_MotionList->Items->Add( Str );
		}
	}
	/*
    for( int i = 0 ; i < 256 ; i++ )
    {
        if( pMainSprite->GetRuEntity() == NULL )
            break;
        if( RuACT_HasMotion( pMainSprite->GetRuEntity() , pMainSprite->GetMotionName(i) ) == TRUE )
        {
            //String* Str = String::Format( "{0} {1}" , __box( i ) , (String*)pMainSprite->GetMotionName(i) );
			String* Str = String::Format( "{0}" , (String*)pMainSprite->GetMotionName(i) );
            CB_MotionList->Items->Add( Str );
        }
    }
	*/

	if( CB_MotionList->Text->Length != 0 )
		pMainSprite->PlayMotion( DotNetStr::StringToChar( CB_MotionList->Text ).c_str() );
	else
		pMainSprite->PlayMotion( DotNetStr::StringToChar( "stand_idle01" ).c_str() );

	if( CB_ShowPoint->Checked == true )
		pMainSprite->ShowPointWidget( 80 , 0xffff0000 );

	//設定特效
	ResetMagicACT();
}

void    ViewForm::UpDateGraphic( )
{
	if( g_pGraphic == NULL )
		return;

	g_pGraphic->IsShowGrid( CB_ShowGrid->Checked );

	_Clock = 0;

    g_pGraphic->SetTextureImageFile( NULL );    
    pTargetSprite->Hide();

    GameObjDbStruct __pin *Data = &_GameObj;

    if( _GameObj.Classification != EM_ObjectClass_Image )
    {
        pMainSprite->Hide();
        return;
    }
    pMainSprite->Show();

    if( _GameObj.Classification != EM_ObjectClass_Image )
        return;

    g_pGraphic->SetTextureImageFile( Data->Image.ACTField );    

    for(int i = 0; i < _ComboBoxEQList->Count; i++ )
    {
        ComboBox* comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[i] );
        comboBox->BackColor = System::Drawing::SystemColors::Window;
    }

    CB_MainHand->BackColor = System::Drawing::SystemColors::Window;
    CB_SecondHand->BackColor = System::Drawing::SystemColors::Window;
    CB_MainObj->BackColor = System::Drawing::SystemColors::Window;


    
    if(		_GameObj.Image.ImageType != EM_GameObjectImageType_Normal 
		&&	!( _GameObj.Image.ImageType == EM_GameObjectImageType_Weapon && CB_ShowWeapoin->Checked ) )
    {		
        if( String::Compare( CB_MainObj->Text  , _LMainModle ) != 0 )
        {
            _LMainModle = CB_MainObj->Text;
            _IsMainChange = true;
            pMainSprite->SetObject( DotNetStr::StringToChar( CB_MainObj->Text ).c_str() );
        }        
    }
	float ScaleRate;
	pMainSprite->SetScale( 1 );
    switch( _GameObj.Image.ImageType )
    {
    case EM_GameObjectImageType_Normal:

        if( String::Compare( (String*)Data->Image.ACTWorld  , _LMainModle ) != 0 )
        {
            _LMainModle = Data->Image.ACTWorld;
            _IsMainChange = true;
            pMainSprite->SetObject( DotNetStr::StringToChar( Data->Image.ACTWorld ).c_str() );
        }
        
        CB_MainObj->BackColor = System::Drawing::SystemColors::InactiveCaption;

		for(int i = 0; i < _ComboBoxEQList->Count; i++ )
		{
			ComboBox* comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[i] );
			comboBox->BackColor = System::Drawing::SystemColors::InactiveCaption;
		}

        BaseUpDate( );

		//設定縮放比
		ScaleRate = float( Data->Image.ModelSize ) / 100; 
		pMainSprite->SetScale( ScaleRate );

        break;
    case EM_GameObjectImageType_Magic:
        BaseUpDate( );
        pTargetSprite->Show();
        break;

    case EM_GameObjectImageType_Armor:
        {
            BaseUpDate( );
            if( _ComboBoxEQList->Count <= _GameObj.Image.ArmorPos )
                break;

			int ArmorPos = _GameObj.Image.ArmorPos;
            ComboBox* comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[ ArmorPos ] );
            comboBox->BackColor = System::Drawing::SystemColors::InactiveCaption;

            if( strlen( Data->Image.ACTWorld ) == 0 )
            {

                if( comboBox->Text->Length == 0)
                    pMainSprite->SetPaperdollPart( PaperdollTypeName[ _GameObj.Image.ArmorPos ] );
                else
                    pMainSprite->SetPaperdollPart( PaperdollTypeName[ _GameObj.Image.ArmorPos ] , DotNetStr::StringToChar( comboBox->Text ).c_str() );

				pMainSprite->SetPaperdollColor( PaperdollTypeName[ _GameObj.Image.ArmorPos] , 0 , 0 );
            }
            else
			{
                pMainSprite->SetPaperdollPart( PaperdollTypeName[ _GameObj.Image.ArmorPos] , Data->Image.ACTWorld );
				pMainSprite->SetPaperdollColor( PaperdollTypeName[ _GameObj.Image.ArmorPos] , Data->Image.ArmorMainColor.Color , Data->Image.ArmorOffColor.Color );
			}
            break;

        }
    case EM_GameObjectImageType_Weapon:

		if( CB_ShowWeapoin->Checked == false )
		{
			BaseUpDate( );
			CB_MainHand->BackColor = System::Drawing::SystemColors::Window;
			pMainSprite->SetRightObject( DotNetStr::StringToChar( Data->Image.ACTWorld ).c_str() );
		}
		else
		{
			if( String::Compare( (String*)Data->Image.ACTWorld  , _LMainModle ) != 0 )
			{
				_LMainModle = Data->Image.ACTWorld;
				_IsMainChange = true;
				pMainSprite->SetObject( DotNetStr::StringToChar( Data->Image.ACTWorld ).c_str() );
			}
			BaseUpDate( );
		}

        break;
    }
    
    pMainSprite->PaperdollBuild();

    SetComboBox( );

}

System::Void ViewForm::OnMouseWheel(Object* Sender , System::Windows::Forms::MouseEventArgs* e) 
{
	Global::CritSect.Enter();
    float zDelta = e->Delta * 0.02f;
	if( g_pGraphic )
		g_pGraphic->GetCamera()->MoveCamera( CRuVector3(0.0f, 0.0f, zDelta), true );
	Global::CritSect.Leave();
}

System::Void ViewForm::OnMouseMove(System::Object *  sender, System::Windows::Forms::MouseEventArgs*  e)
{
    //float zDelta = e->Delta * 0.02f;
    //g_pRuneDev->GetCamera()->MoveCamera( CRuVector3(0.0f, 0.0f, zDelta), true );
	if( g_pGraphic == NULL )
		return;

	Global::CritSect.Enter();

    int MouseX = e->X;
    int MouseY = e->Y;

    float xDelta = ( MouseX - _LMouseX ) * 0.004f;
    float yDelta = ( MouseY - _LMouseY ) * 0.004f;

    if( _IscameraCycle )
    {
        g_pGraphic->GetCamera()->Cycle( xDelta, CRuVector3(0.0f, 1.0f, 0.0f) );
        g_pGraphic->GetCamera()->Cycle( yDelta, g_pGraphic->GetCamera()->GetCameraRightVector() );				
    }

    _LMouseX  = MouseX;
    _LMouseY  = MouseY;

	 Global::CritSect.Leave();
}

 void    ViewForm::SetComboBox( )
 {
     if( _IsMainChange == false )
         return;
     _IsMainChange = false;
     
     if( pMainSprite->GetRuEntity() == NULL )
         return;
     IRuPaperdoll* m_actPaperdoll = RuEntity_FindFirstPaperdoll( pMainSprite->GetRuEntity() );

     // Fill out fields in the preview window
     if(m_actPaperdoll)
     {
         CRuPaperdollTemplate *paperdollTemplate = m_actPaperdoll->GetPaperdollTemplate();
		 static bool IsInit = false;
		 if( IsInit == false )
		 {
			paperdollTemplate->Construct();
			IsInit = true;
		 }
		 
         
         for(int i = 0; i < _ComboBoxEQList->Count; ++i)
         {
             // Abort if we have no more combo boxes available to fill
             int Pos = _PosIDMap[i];
             if(Pos >= paperdollTemplate->GetNumParts() )
                 break;

             ComboBox *comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[i]);
			 ComboBox *comboBox_Image = static_cast<ComboBox *>(_ComboBoxEQList_Image->Item[i]);

             // Clear all existing paperdoll part combo boxes
             comboBox->Items->Clear();
			 comboBox_Image->Items->Clear();

             // Add empty item
             comboBox->Items->Add( (String*)"" );
             comboBox_Image->Items->Add( (String*)"" );

             // Add items
             INT32 numBaseItems = paperdollTemplate->GetNumBaseItems( Pos );
			 //String* Str = paperdollTemplate->GetPartName( i );
	  		 //comboBox_Image->Text = Str;
			 
             for(INT32 j = 0; j < numBaseItems; ++j)
             {
                 INT32 numItems = paperdollTemplate->GetNumItems( Pos , j );

                 for(INT32 k = 0; k < numItems; ++k)
                 {
                     comboBox->Items->Add( (String*)(paperdollTemplate->GetItemName( Pos , j, k )));
                     comboBox_Image->Items->Add( (String*)(paperdollTemplate->GetItemName( Pos , j, k )));
                 }
             }
         }
     }
     else
     {
         // Clear all existing paperdoll part combo boxes
         for(int i = 0; i < _ComboBoxEQList->Count; ++i)
         {
            ComboBox *comboBox = static_cast<ComboBox *>(_ComboBoxEQList->Item[i]);
			ComboBox *comboBox_Image = static_cast<ComboBox *>(_ComboBoxEQList->Item[i]);			
            comboBox->Items->Clear();
			comboBox_Image->Items->Clear();
         }
     }
  
 }

 System::Void ViewForm::CB_MotionList_TextChanged(System::Object *  sender, System::EventArgs *  e)
 {
	 pMainSprite->PlayMotion( DotNetStr::StringToChar( CB_MotionList->Text ).c_str() );
 }

 void    ViewForm::SearchAllItem( char* ResourcePath )
 {
    set < string > DBInfo;
    char Buf[512];

    sqlCommand1->CommandText = "Select ACTWorld from ImageObjectDB";
    SqlDataReader* myReader = sqlCommand1->ExecuteReader();
    while( myReader->Read())
    {
        String* Str =  myReader->GetString( 0 ) ;
		std::string  File = DotNetStr::StringToChar( Str );
        FilePathnormalize( (char*)File.c_str() , Buf );
        DBInfo.insert( Buf );
    }
    myReader->Close();
    //DBInfo.find()

    char* File;
    string Path;
    Path = CombineFilePath( ResourcePath , "model\\item" );

    vector<string>& FileList =*( AllFileList_Sub( Path , "*.ros" ) );
//	sort( FileList.begin() , FileList.end() );

    CB_MainHand->Items->Clear();
    CB_SecondHand->Items->Clear();
    for( int i = 0 ; i < (int)FileList.size() ; i++ )
    {
        File = CombineFilePath( "model\\item\\" , FileList[i].c_str() );
        FilePathnormalize( File , Buf );
		if( strstr( Buf , "act_" ) == NULL )
			continue;

        if( DBInfo.find( Buf ) == DBInfo.end() )
            CB_MainHand->Items->Add( (String*)( File ) );

        CB_SecondHand->Items->Add( (String*)( File ) );

    }



    Path = CombineFilePath( ResourcePath , "model\\character" );

    vector<string>& FileList1 =*( AllFileList_Sub( Path , "*.ros" ) );
//	sort( FileList1.begin() , FileList1.end() );

    CB_MainObj->Items->Clear();
    CB_TargetObj->Items->Clear();
    for( int i = 0 ; i < (int)FileList1.size() ; i++ )
    {
        File = CombineFilePath( "model\\character\\" , FileList1[i].c_str() );

        FilePathnormalize( File , Buf );
        //檢查是否有 act_
        if( strstr( Buf , "act_" ) == NULL )
            continue;

		if( strstr( Buf , "act_weapon" ) != NULL )
		{
			File = CombineFilePath( "model\\character\\" , FileList[i].c_str() );
			FilePathnormalize( File , Buf );
			if( DBInfo.find( Buf ) == DBInfo.end() )
				CB_MainHand->Items->Add( (String*)( File ) );

			CB_SecondHand->Items->Add( (String*)( File ) );

			continue;
		}

        if( DBInfo.find( Buf ) == DBInfo.end() )
            CB_MainObj->Items->Add( (String*)( File ) );

        CB_TargetObj->Items->Add( (String*)( File ) );

    }


	Path = CombineFilePath( ResourcePath , "model\\item\\quest_item" );

	vector<string>& FileList2 =*( AllFileList_Sub( Path , "*.ros" ) );
//	sort( FileList2.begin() , FileList2.end() );

	for( int i = 0 ; i < (int)FileList2.size() ; i++ )
	{
		File = CombineFilePath( "model\\item\\quest_item\\" , FileList2[i].c_str() );

		FilePathnormalize( File , Buf );

		if( DBInfo.find( Buf ) == DBInfo.end() )
			CB_MainObj->Items->Add( (String*)( File ) );

		CB_TargetObj->Items->Add( (String*)( File ) );

	}



 }
 System::Void ViewForm::CB_ShowPoint_CheckedChanged(System::Object*  sender, System::EventArgs*  e)
 {
	 if( CB_ShowPoint->Checked == true )
		 pMainSprite->ShowPointWidget( 80 , 0xffff0000 );
	 else
		 pMainSprite->ClearPointWidget();
 }

 System::Void ViewForm::SearchPath_DClick(System::Object*  sender, System::EventArgs*  e)
 {
	 if( openFileDialog_Base->ShowDialog() == DialogResult::OK )
	 {
		 String* File = openFileDialog_Base->FileName->ToLower();
		 //String* Path = openFileDialog_Base->InitialDirectory->ToLower();
		 String* Path = ((String*)Global::GetIniString( "ResourcePath" ))->ToLower();
		 File = File->Replace( "\\" , "/");
		 Path = Path->Replace( "\\" , "/");
		 Path = Path->Replace( "//" , "/");
		 File = File->Replace( Path , "" );

		 TextBox* Edit = static_cast<TextBox*>(sender);
		 Edit->Text = File;    
	 }
 }

 void    ViewForm::SetFilePath( )
 {
	//openFileDialog_Base->InitialDirectory = (String*)Global::GetIniString( "ResourcePath" );
 }

 void    ViewForm::ResetMagicACT( )
 {
	 if( _GameObj.Image.ImageType == EM_GameObjectImageType_Magic )
		 return;

	pMainSprite->ClearAllAttachEntity( );
	if( TB_MagicACT->Text->Length == 0 )
		return;
	
	int ID = pMainSprite->AttachEntity( DotNetStr::StringToChar( TB_MagicACT->Text ).c_str() , DotNetStr::StringToChar( CB_MagicPoint->Text ).c_str() );	

	CRuEntity*	pEntity = pMainSprite->GetAttachEntity( ID );
	if( pEntity == NULL )
		return;

	if ( RuACT_HasMotion( pEntity , "explode"  ) == TRUE )
	{
		RuACT_PlayMotionEx( pEntity , -1 , "explode" );
	}
	if ( RuACT_HasMotion( pEntity , "idle"  ) == TRUE )
	{
		RuACT_PlayMotionEx( pEntity , -1 , "idle" );
	}

	//int MotionID = IBaseSprite::GetMotionEnum( DotNetStr::StringToChar( CB_MotionList->Text ) );
	//pMainSprite->PlayMotion( MotionID );
	pMainSprite->PlayMotion( DotNetStr::StringToChar( CB_MotionList->Text ).c_str() );

 }

 //更新魔法效果動態
 void	ViewForm::UpDateMagicAction( )
 {
	GameObjDbStruct __pin *GameObj = &_GameObj;

	if( _GameObj.Image.ImageType != EM_GameObjectImageType_Magic )
		return;

	
	if( _Clock == 0 )
	{
		pMainSprite->ClearAllAttachEntity( );
		pTargetSprite->ClearAllAttachEntity( );

	}
	//第一秒
	else if( _Clock == 10 )
	{
		//施法準備
		pMainSprite->PlayMotion( GameObj->Image.GatherMotion  );

		for( int i = 0 ; i < 3 ; i++ )
		{
			MagicPartical*	MagicP = &GameObj->Image.GatherPartical[i];
			int ID = pMainSprite->AttachEntity( MagicP->Name , MagicP->Point );	

			CRuEntity*	pEntity = pMainSprite->GetAttachEntity( ID );
			if( pEntity == NULL )
				continue;
			
			if ( RuACT_HasMotion( pEntity , "explode"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "explode" );
			}
			if ( RuACT_HasMotion( pEntity , "idle"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "idle" );
			}
		}
	}
	//第二秒 施法
	else if( _Clock == 20 )
	{
		pMainSprite->PlayMotion( GameObj->Image.ShootMotion  );

	}
	//射出
	else if( _Clock == 25 )
	{
		CEffectSprite* ShootEffect = new CEffectSprite();
		CLineEffectSprite* LineEffect = new CLineEffectSprite();
		
		ShootEffect->SetRelative( pMainSprite );
		ShootEffect->SetAttachPoint( IBaseSprite::GetAttachPointEnum( GameObj->Image.ShootPoint ) );
		ShootEffect->SetObject( GameObj->Image.ShootACT );
		ShootEffect->SetEffectMotionStep( eEffectMotion_Step_Explode );

		LineEffect->BeginSprite( pMainSprite , IBaseSprite::GetAttachPointEnum( GameObj->Image.FlyBeginPoint ) );
		LineEffect->SetRelative( pTargetSprite );
		LineEffect->SetAttachPoint( IBaseSprite::GetAttachPointEnum( GameObj->Image.TargetPoint ) );
		if( GameObj->Image.IsThrowType == false )
			LineEffect->SetObject( GameObj->Image.FlyACT );
		else
		{
			char Buf[256];
			sprintf( Buf , GameObj->Image.FlyACT , GameObj->Image.ThrowTypePreviewID );
			LineEffect->SetObject( Buf );
		}
		LineEffect->SetMoveSpeed( 2 );
		LineEffect->SetEffectMotionStep( eEffectMotion_Step_Appear );
	}
	//身上效果消失
	else if( _Clock == 35 )
	{
		pMainSprite->ClearAllAttachEntity( );
	}
	//命中
	else if( _Clock == 45 )
	{	
		pMainSprite->PlayMotion( "stand_idle01" );

		int ID;
		//命中
		if( strcmp( "freeze_down" , GameObj->Image.TargetPoint) == 0  )
			ID = pTargetSprite->AttachEntity( GameObj->Image.ExplodeACT , "p_down" );		
		else
			ID = pTargetSprite->AttachEntity( GameObj->Image.ExplodeACT , GameObj->Image.TargetPoint );		

		CRuEntity*	pEntity = pTargetSprite->GetAttachEntity( ID );
		if( pEntity != NULL )
		{
			if ( RuACT_HasMotion( pEntity , "explode"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "explode" );
			}
			if ( RuACT_HasMotion( pEntity , "idle"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "idle" );
			}
		}

		//元素命中
		ID = pTargetSprite->AttachEntity( GameObj->Image.ExtendACT , GameObj->Image.ExtenPoint );	

		pEntity = pTargetSprite->GetAttachEntity( ID );
		if( pEntity != NULL )
		{
			if ( RuACT_HasMotion( pEntity , "explode"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "explode" );
			}
			if ( RuACT_HasMotion( pEntity , "idle"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "idle" );
			}
		}

		ID = pTargetSprite->AttachEntity( GameObj->Image.BuffACT , GameObj->Image.BuffPoint );	

		pEntity = pTargetSprite->GetAttachEntity( ID );
		if( pEntity != NULL )
		{
			if ( RuACT_HasMotion( pEntity , "explode"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "explode" );
			}
			if ( RuACT_HasMotion( pEntity , "idle"  ) == TRUE )
			{
				RuACT_PlayMotionEx( pEntity , -1 , "idle" );
			}
		}

	}

	_Clock++;

	if( _Clock > 60 )
		_Clock = 0;

 }