class InventoryWndCharacterView extends UICommonAPI;

const ORCRIDERSCALE = 0.803f;
const ORCRIDERX = 0;
const ORCRIDERY = 1;
const TIMER_ID_ANIMATION = 19;
const TIMER_DELAY = 2000;
const TIMER_ID_CLICK = 2;
const TIMER_DELAY_CLICK = 150;

var	ButtonHandle m_CloseButton;
//var L2Util l2UtilScript;

// ?¬Õ ¬¨¬é¬¨¢ç 
var CharacterViewportWindowHandle	m_ObjectViewport;
var	ButtonHandle 		m_BtnRotateLeft;
var	ButtonHandle		m_BtnRotateRight;
var bool isDown;
var bool isAniPlaing;
var int m_MeshType;

//var string expenedNode;


/*********************************************************************************************
 * On
 * *******************************************************************************************/
function OnRegisterEvent()
{
	//RegisterEvent(EV_InventoryOpenWindow);
	//RegisterEvent(EV_InventoryHideWindow);
	//RegisterEvent(EV_InventoryToggleWindow);
	RegisterEvent(EV_ChangeCharacterPawn);
	//RegisterEvent(EV_SkillListStart );
	//RegisterEvent(EV_SkillList );
	//RegisterEvent(EV_SkillListEnd );
}

function OnLoad()
{
	InitHandleCOD();
}

function OnEvent(int Event_ID, string param)
{
	//Debug ( "OnEvent " @ Event_ID ) ;	
	switch( Event_ID )
	{		
		//case EV_InventoryOpenWindow:    //2580
		//	HandleOpenWindow(param);
		//	break;
		//case EV_InventoryHideWindow:    //2590
		//	HandleHideWindow();
		//	break;  		
		case EV_ChangeCharacterPawn:    //3810
			HandleChangeCharacterPawn(param);
			break;
		//case EV_SkillListStart :
		//	HandleSkillListStart();
		//	break;
		//case EV_SkillList :
		//	HandleSkillList( param);
		//	break;
		//case EV_SkillListEnd : 
		//	MakeNodes () ;
		//	ExpandNodes();
		//	break;
		default:
			break;
	};
}

//function OnShow()
//{
//	Debug( "onShow");
//}

//function OnHide()
//{	
//}

function OnClickButton ( string StrID )
{
	switch ( StrID ) 
	{
		case "CloseButton" :
			ToggleMe();
		break;
	}
}


function OnLButtonDown (WindowHandle  a_WindowHandle, int X, int Y)
{	
	//Debug ( "LButonDown" ) ;

	if (a_WindowHandle == m_BtnRotateLeft)
		m_ObjectViewport.StartRotation(false);
	else if (a_WindowHandle == m_BtnRotateRight)
		m_ObjectViewport.StartRotation(true);
	
	else if ( a_WindowHandle == m_ObjectViewport )  
	{	
		// ¬¦¡þ¬×? ¡¾¬×?¬²
		m_hOwnerWnd.SetTimer ( TIMER_ID_CLICK, TIMER_DELAY_CLICK ) ;
		isDown = true;
	}

	//else if ( !isDown && a_WindowHandle != m_CloseButton) 
	//{
	//	// m_ObjectViewport ?¡¾¬¦¬¤ ¬©¬±¬×¬Û, focus ¡Æ? ¥ì¬©¬¢¬è ?¬¬?¬£?¬». ?????? 
	//	//Me.SetFocus();
		
	//	isDown = true;
	//}

}

function OnLButtonUp (WindowHandle  a_WindowHandle, int X, int Y)
{	
	//Debug ( "LButonUp" ) ;

	if (a_WindowHandle == m_BtnRotateLeft)
		m_ObjectViewport.EndRotation();
	else if (a_WindowHandle == m_BtnRotateRight)
		m_ObjectViewport.EndRotation();
	// 2¬¤¬¬¡Æ? ?¬¸?¬± ¬¡¬Ù¡í¬ï ¬×¡¤¡¾¬Ó
	else if ( !isAniPlaing && isDown && a_WindowHandle == m_ObjectViewport) 
	{	
		isAniPlaing = true;
		if(m_MeshType == 18 || m_MeshType == 19)
		{
			m_ObjectViewport.PlayAnimation(3);					
		}
		else
		{
			playRandAttackAnimation();
		}
		m_hOwnerWnd.KillTimer (  TIMER_ID_ANIMATION) ;
		m_hOwnerWnd.SetTimer ( TIMER_ID_ANIMATION, TIMER_DELAY);	
	}
	
	m_hOwnerWnd.KillTimer (  TIMER_ID_CLICK ) ;
	isDown = false;	
}

function OnRButtonUp ( WindowHandle a_WindowHandle, int X, int Y)
{
	if ( !isAniPlaing && isDown && a_WindowHandle == m_ObjectViewport) 
	{	
		isAniPlaing = true;
		playRandAnimation() ;		
		m_hOwnerWnd.KillTimer (  TIMER_ID_ANIMATION) ;
		m_hOwnerWnd.SetTimer ( TIMER_ID_ANIMATION, TIMER_DELAY);	
	}
	
	m_hOwnerWnd.KillTimer (  TIMER_ID_CLICK ) ;
	isDown = false;	
}

function OnRButtonDown (WindowHandle  a_WindowHandle, int X, int Y)
{	
	if ( a_WindowHandle == m_ObjectViewport )  
	{	
		// ¬¦¡þ¬×? ¡¾¬×?¬²
		m_hOwnerWnd.SetTimer ( TIMER_ID_CLICK, TIMER_DELAY_CLICK ) ;
		isDown = true;
	}
}


function OnTimer(int TimerID) 
{
	
	//Debug("Min" @min @ "Sec" @ Sec);
	switch ( TimerID ) 
	{
		case TIMER_ID_ANIMATION : 
			m_hOwnerWnd.KillTimer (  TIMER_ID_ANIMATION) ;
			isAniPlaing = false;
		break;
		case TIMER_ID_CLICK     : 
			m_hOwnerWnd.KillTimer (  TIMER_ID_CLICK ) ; 
			isDown = false;       
		break;
	}
}


/***********************************************************************************************
 * ?¬¸?¬±¬×¬À¬¡¬®?¬©
 * *********************************************************************************************/
function playRandAttackAnimation() 
{
	local int aniType;
	aniType = Rand(3) +1;
	//Debug ( "OnTimer"@  aniType ) ;
	m_ObjectViewport.PlayAttackAnimation( aniType  ) ;
}


function playRandAnimation() 
{
	local int aniType;
	aniType = Rand(13) +1;
	//Debug ( "OnTimer"@  aniType ) ;
	m_ObjectViewport.PlayAnimation( aniType  ) ;
}



/*function HandleOpenWindow(string param)
{
	local int open;

	ParseInt(param, "Open", open);

	if(open==0) return;
	
	OpenWindow();
}

function OpenWindow()
{
	Me.ShowWindow();
	Me.SetFocus();
}

function HandleHideWindow()
{	
	HideWindow(m_WindowName);
	//end of branch
}*/



/********************************************************************************************
 * ¬¤¬¬¡¾¬Ó¬ª¡© ¢à¬¹ ¡¾¬Ó?¡í handle ??¬¨¬¤
 * ******************************************************************************************/
function InitHandleCOD()
{
	m_BtnRotateLeft = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".TabCharacterView.BtnRotateLeft");
	m_BtnRotateRight = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".TabCharacterView.BtnRotateRight");
	m_CloseButton = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".CloseButton");
	m_ObjectViewport = GetCharacterViewportWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".TabCharacterView.ObjectViewport");
	m_ObjectViewport.SetDragRotationRate(300);
}


/********************************************************************************************
 * ¬×¢ç??¬¦¬×¬¨¢ç ¬¤?¬×¢ç 
 * ******************************************************************************************/
//function HandleRestart()
//{
//	Me.HideWindow() ;	
//}

/********************************************************************************************
 * pawn 
 * ******************************************************************************************/
function HandleChangeCharacterPawn(string param)
{
	local UserInfo uInfo;
	ParseInt (param, "MeshType", m_MeshType);

	switch (m_MeshType)
	{
		case 0:
         // ¬ª¬À¬×¬·_¬¡¬î¡í¬Ù_??
         m_ObjectViewport.SetCharacterScale(1.f);
         m_ObjectViewport.SetCharacterOffsetX(-2);
         m_ObjectViewport.SetCharacterOffsetY(-3);                                       
         break;
         case 1:
         // ¬ª¬À¬×¬·_¬¡¬î¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(1.03f);
         m_ObjectViewport.SetCharacterOffsetX(-2);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         case 8:
         // ¬ª¬À¬×¬·_¢à¬ï¡í¬Ù_??
         m_ObjectViewport.SetCharacterScale(1.047f);
         m_ObjectViewport.SetCharacterOffsetX(2);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         case 9:
         // ¬ª¬À¬×¬·_¢à¬ï¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(1.07f);
         m_ObjectViewport.SetCharacterOffsetX(-1);
         m_ObjectViewport.SetCharacterOffsetY(-6);                                       
         break;
         case 6:
         // ?¢´¬©¬¢_¬¡¬î¡í¬Ù_??
         m_ObjectViewport.SetCharacterScale(0.98f);
         m_ObjectViewport.SetCharacterOffsetX(-2);
         m_ObjectViewport.SetCharacterOffsetY(-4);                                       
         break;
         case 7:
         // ?¢´¬©¬¢_¬¡¬î¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(1.04f);
         m_ObjectViewport.SetCharacterOffsetX(-4);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         // case q
         // ?¢´¬©¬¢_¢à¬ï¡í¬Ù_??
         // SetCharacterOffsetX(-2);
         // SetCharacterOffsetY(-4);
         // ?¢´¬©¬¢_¢à¬ï¡í¬Ù_?¨Ï
         // SetCharacterOffsetX(-4);
         // SetCharacterOffsetY(-5);
         case 2:
         // ?¬»?¢´_¬¡¬î¡í¬Ù_??
         m_ObjectViewport.SetCharacterScale(0.99f);
         m_ObjectViewport.SetCharacterOffsetX(-1);
         m_ObjectViewport.SetCharacterOffsetY(-4);                                       
         break;
         case 3:
         // ?¬»?¢´_¬¡¬î¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(1.015f);
         m_ObjectViewport.SetCharacterOffsetX(-1);
         m_ObjectViewport.SetCharacterOffsetY(-4);
         break;
         // ?¬»?¢´_¢à¬ï¡í¬Ù_??
         // SetCharacterOffsetX(-1);
         // SetCharacterOffsetY(-4);
         // ?¬»?¢´_¢à¬ï¡í¬Ù_?¨Ï
         // SetCharacterOffsetX(-1);
         // SetCharacterOffsetY(-4);
         case 10:
			// ?¬¡¬¦¨Ï_¬¡¬î¡í¬Ù_??                             
			if(GetPlayerInfo(uInfo))
			{
				if(uInfo.Class == 217)
				{
					m_ObjectViewport.SetCharacterScale(0.8030f);
					m_ObjectViewport.SetCharacterOffsetX(0);
					m_ObjectViewport.SetCharacterOffsetY(1);					
				}
				else
				{
					m_ObjectViewport.SetCharacterScale(0.9530f);
					m_ObjectViewport.SetCharacterOffsetX(0);
					m_ObjectViewport.SetCharacterOffsetY(-6);
				}
			}                                    
			break;
         case 11:
         // ?¬¡¬¦¨Ï_¬¡¬î¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(0.97f);
         m_ObjectViewport.SetCharacterOffsetX(2);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         case 12:
         // ?¬¡¬¦¨Ï_¢à¬ï¡í¬Ù_??
         m_ObjectViewport.SetCharacterScale(0.955f);
         m_ObjectViewport.SetCharacterOffsetX(-2);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         case 13:
         // ?¬¡¬¦¨Ï_¢à¬ï¡í¬Ù_?¨Ï
         m_ObjectViewport.SetCharacterScale(0.985f);
         m_ObjectViewport.SetCharacterOffsetX(0);
         m_ObjectViewport.SetCharacterOffsetY(-5);                                       
         break;
         case 4:
         // ¥ì¬Ö?¬è¬©¬¢_??
         m_ObjectViewport.SetCharacterScale(1.043f);
         m_ObjectViewport.SetCharacterOffsetX(0);
         m_ObjectViewport.SetCharacterOffsetY(1);                                       
         break;
         case 5:
         // ¥ì¬Ö?¬è¬©¬¢_?¨Ï
         m_ObjectViewport.SetCharacterScale(1.09f);
         m_ObjectViewport.SetCharacterOffsetX(0);
         m_ObjectViewport.SetCharacterOffsetY(-3);                                       
         break;
         case 14:
			// ¬¥¡ì¬×¢Ò?¢´_??
			m_ObjectViewport.SetCharacterScale(0.993f);
			m_ObjectViewport.SetCharacterOffsetX(-5);
			m_ObjectViewport.SetCharacterOffsetY(-4);                                       
			break;
         case 15:
			// ¬¥¡ì¬×¢Ò?¢´_?¨Ï
			m_ObjectViewport.SetCharacterScale(1.01f);
			m_ObjectViewport.SetCharacterOffsetX(0);
			m_ObjectViewport.SetCharacterOffsetY(-3);                                       
			break;
         case 17:
			// ?¬¨¬×?¬¦¬¹¬¡¬®?¬à
			m_ObjectViewport.SetCharacterScale(1.015f);
			m_ObjectViewport.SetCharacterOffsetX(-1);
			m_ObjectViewport.SetCharacterOffsetY(-1);
			break;
		case 18:
			m_ObjectViewport.SetCharacterScale(1.0150f);
			m_ObjectViewport.SetCharacterOffsetX(-1);
			m_ObjectViewport.SetCharacterOffsetY(-1);
			break;
		case 19:
			m_ObjectViewport.SetCharacterScale(1.0150f);
			m_ObjectViewport.SetCharacterOffsetX(-1);
			m_ObjectViewport.SetCharacterOffsetY(-1);
			break;
	}
}

/********************************************************************************************
 * etc, util
 * ******************************************************************************************/
function ToggleMe () 
{
	local InventoryWnd inventoryWndScript;	
	inventoryWndScript = InventoryWnd(GetScript( "InventoryWnd" ) ) ;
	inventoryWndScript.toogleCharacterViewPort( !m_hOwnerWnd.IsShowWindow () ) ;
}

// ¬×¡í¬¢¬»¬¡¬µ ... ¬¤?¬×¢ç
function String GetEllipsisString ( string str, int maxWidth  ) 
{
	local string fixedString;
	local int nWidth, nHeight ;		
	
	//str = str @ "123456789";
	GetTextSizeDefault( str $ "..."  , nWidth, nHeight);
	if ( nWidth < maxWidth ) return Str;	
	
	fixedString = DivideStringWithWidth(str, maxWidth);	
	if (fixedString != str)
		fixedString =  fixedString $ "...";
	
	return fixedString ;
}

function CharacterClickec () 
{
	if ( !m_hOwnerWnd.IsShowWindow () ) ToggleMe();
	GetTabHandle(m_hOwnerWnd.m_WindowNameWithFullPath $".CharacterTab").SetTopOrder(0, true);
}
