//#################################################################################
//
// BASE CLASS OF YOUR DLL interface
//
//#################################################################################

#ifdef ACTIVE_EDITOR

#include "stdafx.h"

#include "ACP_Base.h"
#define HieFriend
#define D_ObjsTbls_Define
#include "TFA.h"
#include "incGam.h"
#include "Itf.h"
#include "Tot.h"
#undef D_ObjsTbls_Define

#include "DlgVwCt.h"
#include "T3DFRMBs.hpp"

//Stefan Dumitrean 26-05-98
#include "oac.h"
#include "T3DFrame.hpp"
#define T3DFamilyName "Families"
//End Stefan Dumitrean 26-05-98

#include "T3Dinterf.hpp"
#include "T3DWorld.hpp"
#include "CPA_Grid.hpp"
#include "AnimView.h"

//#include "gli.h"

#include "X:\Cpa\Main\inc\_EditID.h"

// infos
static CString g_csT3DName = "Tool3D / Viewer";
static CString g_csT3DAuthor = "Marc Trabucato";
static CString g_csT3DVersion = "V 6.0.3   18/03/99";
static CString g_csT3DFrenchHelpFile = "T3D.hlp";
static CString g_csT3DEnglishHelpFile = "";

BOOL g_bTipGridFirstTime = TRUE;
BOOL g_bTipAnimFirstTime = TRUE;
BOOL g_bTipToolFirstTime = TRUE;

//--------------------------------------------------------------------------------
// MACROS
//--------------------------------------------------------------------------------

#define M_GetNbObjects()            ( m_oListOfVWObjInfo . GetCount() )
#define M_DeviceCamera(p_oDevice)   ( ( (DEV_ViewPort3D*) p_oDevice -> GetViewPort() ) ->GetCamera() )
#define M_DeviceCameraSO(p_oDevice)   ( ( (DEV_ViewPort3D*) p_oDevice -> GetViewPort() ) ->GetCameraSO() )
#define M_RedrawAll()               ( GetInterface()->fn_vUpdateAll(E_mc_JustDraw) )
#define M_ContactGrid()             ( (CPA_Grid*)m_p_Tool3DContact )
// ALX
#define     M_GetLSTViewPort()        ((DEV_ViewPort3D*)GetInterface()->GetMultiDevice()->GetFocusDevice()->GetViewPort())
#define     M_GetLSTCameraInterface() (M_GetLSTViewPort()->GetCameraInterface())
// End ALX

//--------------------------------------------------------------------------------
// CONSTANTS
//--------------------------------------------------------------------------------

// Menu Entry ID
#define C_uiMenuNone            10
#define C_uiMenuChar            11
#define C_uiMenuMod             12
#define C_uiMenuGeom            13
#define C_uiMenuViewer          14
#define C_uiMenuViewerCt        15
#define C_uiMenuTable           16
#define C_uiMenuAnim            17

#define C_uiMenuShowGrid        20
#define C_uiMenuShowSelection   21
#define C_uiMenuAddToViewer     22
#define C_uiMenuResetViewer     23
#define C_uiMenuOpenTable       30
#define C_uiMenuViewerTarget    60  // must be the greatest

#define C_xCameraDist       MTH_M_xFloatToReal( 3.5 )

// keyboard
static tdstKeyboardActionDef g_T3DKeyboardAction[] =
{
	// navigation
	{ "navigation mode",				KA_ID_NAVMODE				},
	{ "left case",						KA_ID_LEFT					},
	{ "right case",						KA_ID_RIGHT					},
	{ "up case",						KA_ID_UP					},
	{ "down case",						KA_ID_DOWN					},
 // end of list
	{ NULL, 0 }
};


// the DLL global definition
tdstDLLIdentity     g_stTool3DIdentity;

//--------------------------------------------------------------------------------
// CONSTRUCTOR.
// You must put here all your DLL interface definitions.
//--------------------------------------------------------------------------------
Tool3D_Interface::Tool3D_Interface(void)
{
	// ************************** private internal
    m_p_stDLLIdentity = &g_stTool3DIdentity;

    // Does your DLL can output in main game view ?
	m_stBaseDLLDefinition.bCanOutputIn3DView = TRUE;

    // Does your DLL can be refresh by engine ?
	m_stBaseDLLDefinition.bCanBeRefreshedByEngine = TRUE;

    // TODO : Add your own code here
    SetEditorInfo(g_csT3DName, g_csT3DAuthor, g_csT3DVersion, g_csT3DFrenchHelpFile, g_csT3DEnglishHelpFile);

    m_wTypeOfTool           = C_wTool3D_None;
    m_p_PreviousFRMBase     = NULL;
    m_p_FRMBase             = NULL;
    m_p_Tool3DMultiDevice   = NULL;
    m_p_PreviousMultiDevice = NULL;
    m_p_InitialRoot         = NULL;
    m_p_Tool3DWorld         = NULL;
    m_p_Tool3DContact       = NULL;
    m_p_oSprObjActor        = NULL;
//Stefan Dumitrean 2-06-98
	m_bAnimPlayerInvoked = FALSE;
//End Stefan Dumitrean 2-06-98

    m_oListOfVWObjInfo . RemoveAll();

	m_p_oKeyboard = new CPA_KeyActionConfiguration("Tools\\T3D\\T3DKeys.ini", g_T3DKeyboardAction);
    m_p_oKeyboard->mfn_vSetObjectName("Tool3D Editor");

}

///////////////////////
// Begin/End methods //
///////////////////////

void Tool3D_Interface::fn_vConstruct(void)
{
    POS_tdstCompletePosition stMatrix;
    MTH3D_tdstVector st3DX, st3DY, st3DZ;

	POS_fn_vSetIdentityMatrix(&stMatrix);
    m_p_PreviousFRMBase = NULL;
    // light Up
	GLI_xCreateLight( &m_hLightUp, GLI_C_lParallelLight );
	GLI_xSetLightState( m_hLightUp, 1 );
    // light Down
	GLI_xCreateLight( &m_hLightDown, GLI_C_lParallelLight );
	GLI_xSetLightState( m_hLightDown, 1 );
    GLI_xGetLightMatrix( m_hLightDown, &stMatrix );
    POS_fn_vGetRotationMatrix( &stMatrix, &st3DX, &st3DY, &st3DZ );
    MTH3D_M_vMulScalarVector( &st3DZ, -1, &st3DZ );
    POS_fn_vSetRotationMatrix( &stMatrix, &st3DX, &st3DY, &st3DZ );
    POS_fn_vNormalizeMatrix( &stMatrix );
    GLI_xSetLightMatrix( m_hLightDown, &stMatrix );
    //
	/*
	 * create bottom frame
	 */
//Stefan Dumitrean 26-05-98
//	m_p_oAnimFrame = new FRMBase();
	m_p_oAnimFrame = new Tool3D_Frame();
//End Stefan Dumitrean 26-05-98

	m_p_oAnimFrame->CreateBase( "Anim viewer", 2, 3, GetMainWorld() );
	m_p_oAnimFrame->m_iInitHeight = 200 ;
	m_p_oAnimFrame->mfn_vEnableCaption( FALSE );

    HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle( m_p_stDLLIdentity->hModule );

    /*
	 * create view
	 */
//Stefan Dumitrean 26-05-98
//	M_FRMAssociateView( m_p_oAnimFrame, CAnimationView );
//	m_p_oAnimView = (CAnimationView *) m_p_oAnimFrame->GetActiveView();
	m_p_oAnimView = (CAnimationView *) m_p_oAnimFrame->m_fn_p_oGetPane(1);
//End Stefan Dumitrean 26-05-98

    m_p_oAnimView -> mfn_vSetEditor( this );
    m_p_oAnimView -> OnInitialUpdate();

    AfxSetResourceHandle( hOldInst );
}

void Tool3D_Interface::fn_vBeforeEditor(void)
{
    if(!m_p_Tool3DWorld)
    {
        // create world
        m_p_Tool3DWorld = new Tool3D_World( GetMainWorld() );

        // Frame
        m_p_FRMBase = new Tool3D_FRMBaseMenu;
        m_p_FRMBase->CreateBase("3DTool",GetMainWorld());

        m_p_FRMBase->mfn_vSetTool3D(this);

        m_p_Tool3DMultiDevice = (DEV_MultiDevice3D *) m_p_FRMBase -> m_oSplitter . GetPane (0 , 0);
        m_p_Tool3DMultiDevice->SetInterface(NULL);
        m_p_Tool3DMultiDevice->SetContact(NULL);

        m_p_Tool3DWorld -> SetMultiDevice ( m_p_Tool3DMultiDevice );

        m_p_oAnimView -> mfn_vSetWorld( m_p_Tool3DWorld );
    }
}


////
void Tool3D_Interface::fn_vJustBeforeClosing(void)
{
    delete m_p_Tool3DWorld;
    m_p_Tool3DWorld = NULL;
    delete m_p_Tool3DMultiDevice;
    m_p_Tool3DMultiDevice = NULL;
    delete m_p_FRMBase;
    m_p_FRMBase = NULL;
    //
    mfn_vResetViewerContent();
}

////////////////////
// Editor Methods //
////////////////////

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
tdePermission Tool3D_Interface::fn_eAcceptNewEditor(CPA_EditorBase *pNewEditor)
{
/*    if( pNewEditor -> GetCurrentEditorName() == C_szDLLActorName )
        return C_Deny;
    else
*/        return C_CloseBefore;
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
BOOL Tool3D_Interface::fn_bCanActivateEditor(CPA_List<CPA_BaseObject> *pParams)
{
    BOOL bCanOpen = ( ! fn_bIsCurrentEditor() &&
                      ( fn_bToolActive(C_wTool3D_Char) || ( fn_bToolActive(C_wTool3D_None) && ( /*Stefan Dumitrean 3-06-98 */m_bAnimPlayerInvoked ||/*End*/ fn_bCanOpenTool(C_wTool3D_Char) ) ) )
                    );
    if( bCanOpen && ! fn_bToolActive(C_wTool3D_Char) )
        m_p_oSprObjActor = M_GetWorld()->GetSingleSelection();

    return bCanOpen;
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
BOOL Tool3D_Interface::fn_bCanCloseEditor(void)
{
    return TRUE;
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void Tool3D_Interface::fn_vOnActivateEditor(CPA_List<CPA_BaseObject> *pParams, BOOL bBackActivated /*=FALSE*/)
{
    CPA_Family          *p_oFamily1     = NULL;
    CPA_Family          *p_oFamily      = NULL;
    CPA_Animation       *p_oAnimation1  = NULL;
    CPA_Animation       *p_oAnimation   = NULL;
    unsigned short      uwFrame1        = 0;
    unsigned short      uwFrame			= 0;

	CPA_List<CPA_BaseObject> clObjectsList;
	CPA_SuperObject		*p_clSuperObject;

	POSITION pos;

//Stefan Dumitrean 3-06-98
//to treat the case when we invoke the Anim Player without an instance selected
	if( m_bAnimPlayerInvoked && (!m_p_oSprObjActor || m_p_oSprObjActor->GetRealTypeName() != C_szActorInstanceTypeName) )
	{
		m_p_oSprObjActor = NULL;
		fn_vOnActivateEditorAnim();
		// tip of day
		GetInterface()->fn_vDisplayTipOfDay("Anim Player", "TAN", g_bTipAnimFirstTime);
		g_bTipAnimFirstTime = FALSE;
		return;
	}
//End Stefan Dumitrean 3-06-98

    ASSERT( m_p_oSprObjActor );

	if (mfn_bFindInfoAboutActor( m_p_oSprObjActor, &p_oFamily, &p_oAnimation, NULL, &uwFrame))
	{
// Shaitan -> for actors without POs
		if (m_p_oSprObjActor->GetCount() == 0)
		{
			m_p_oSprObjActor = NULL;
			
			GetMainWorld()->fn_lFindObjects( &clObjectsList, "", C_szActorInstanceTypeName );
			pos = clObjectsList.GetHeadPosition();

			while (pos && !m_p_oSprObjActor)
			{	
				p_clSuperObject = ((CPA_Actor *)clObjectsList.GetNext(pos))->m_pub_fn_pclGetSuperObject();
				if (mfn_bFindInfoAboutActor(p_clSuperObject, &p_oFamily1, &p_oAnimation1, NULL, &uwFrame1))
				{
					if ((p_oFamily1 == p_oFamily) && (p_clSuperObject->GetCount() > 0))
						m_p_oSprObjActor = p_clSuperObject;
				}
			}
		}

		if (!m_p_oSprObjActor)
		{
			fn_vOnActivateEditorAnim();
			// tip of day
			GetInterface()->fn_vDisplayTipOfDay("Anim Player", "TAN", g_bTipAnimFirstTime);
			g_bTipAnimFirstTime = FALSE;
			return;
		}
// End Shaitan

		if( ! fn_bToolActive(C_wTool3D_Char) )
		{
			OnQueryOpen(NULL,(WPARAM)C_wTool3D_Char,(LPARAM)m_p_oSprObjActor);
		}

		// can open editor
		g_oFrameGest.mfn_vSetRefresh( FALSE );
		g_oFrameGest.mfn_vActivateWindow ( m_p_FRMBase ) ;
		g_oFrameGest.mfn_vActivateWindow( m_p_oAnimFrame );
		g_oFrameGest.mfn_vSetRefresh( TRUE );
		m_p_oAnimView -> mfn_vInitDialog( p_oFamily, p_oAnimation, uwFrame );
//Stefan Dumitrean 27-05-98
		m_p_oAnimView->mfn_vSetFamilySelDialog( m_p_oAnimFrame->m_p_clDialogList );

		m_p_oAnimFrame->m_p_clDialogList->fn_vInitAllLists();
		m_p_oAnimFrame->m_p_clDialogList->fn_vReinitDialog();	
		m_p_CurrentFamily = p_oFamily;
		m_p_oAnimFrame->m_p_clDialogList->fn_vUpdateSelection( E_lrm_ChangeSelection );
//End Stefan Dumitrean 27-05-98
		// tip of day
		GetInterface()->fn_vDisplayTipOfDay("Anim Player", "TAN", g_bTipAnimFirstTime);
		g_bTipAnimFirstTime = FALSE;

	}
	else
	{	
		m_p_oSprObjActor = NULL;
		fn_vOnActivateEditorAnim();
		// tip of day
		GetInterface()->fn_vDisplayTipOfDay("Anim Player", "TAN", g_bTipAnimFirstTime);
		g_bTipAnimFirstTime = FALSE;
		return;
	}
}

/*-----------------------------------------------------------------------------
 *  Description :
 *---------------------------------------------------------------------------*/
void Tool3D_Interface::fn_vOnCloseEditor(void)
{
	m_p_oAnimView -> m_fn_vStopAnim();
}

BOOL Tool3D_Interface::_OnMouseMove(UINT nFlags, tdstMousePos *p_stPos, MTH3D_tdstVector *p_stVect)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnLButtonDblClk(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stPickInfo)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnRButtonDblClk(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stPickInfo)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnLButtonDown(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stPickInfo)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnRButtonDown(UINT nFlags, tdstMousePos *p_stPos, ACP_tdxIndex xIndex, HIE_tdstPickInfo *p_stPickInfo)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnLButtonUp(UINT nFlags, tdstMousePos *p_stPos)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
BOOL Tool3D_Interface::_OnRButtonUp(UINT nFlags, tdstMousePos *p_stPos)
{
	if( fn_bIsCurrentEditor() && m_p_oAnimView -> m_bPlay )
		return TRUE;
	else
		return FALSE;
}
//////////////////
// Menu Methods //
//////////////////

BOOL Tool3D_Interface::fn_bDefineSubMenu(EDT_SubMenu *_p_oEDTSubMenu)
{
//ANNECY MT NewInterface 26/03/98 {
	if(_p_oEDTSubMenu->GetSubMenuType() == C_SubMenuTool)
	{
        // Animation view
//Stefan Dumitrean 3-06-98
//this item is never gray ( from now on )
//      _p_oEDTSubMenu->AddAnEntry(this,"Anim Player",C_uiMenuAnim,fn_bIsCurrentEditor(),fn_bIsCurrentEditor()||fn_bCanOpenTool(C_wTool3D_Char)||fn_bToolActive(C_wTool3D_Char));
		_p_oEDTSubMenu->AddAnEntry(this,"Anim Player",C_uiMenuAnim,fn_bIsCurrentEditor(),TRUE );
//End Stefan Dumitrean 3-06-98
		return TRUE;
	}
	else
//ENDANNECY MT NewInterface }
//ANNECY Shaitan NewInterface 24/03/98 {
    if(_p_oEDTSubMenu->GetSubMenuType() ==  C_SubMenu3DView)
//ENDANNECY Shaitan NewInterface }
    {
        // world view
        _p_oEDTSubMenu->AddAnEntry(this,"World view",C_uiMenuNone,fn_bToolActive(C_wTool3D_None),fn_bToolActive(C_wTool3D_None)?FALSE:TRUE);
        _p_oEDTSubMenu->AddASeparator();
        // character view
        _p_oEDTSubMenu->AddAnEntry(this,"Character view",C_uiMenuChar,fn_bToolActive(C_wTool3D_Char),fn_bToolActive(C_wTool3D_Char)||fn_bCanOpenTool(C_wTool3D_Char));
        // table view
        _p_oEDTSubMenu->AddAnEntry(this,"Character Objects Table view",C_uiMenuTable,fn_bToolActive(C_wTool3D_Table),fn_bToolActive(C_wTool3D_Table)||fn_bCanOpenTool(C_wTool3D_Table));
        // grid view
        if(fn_bToolActive(C_wTool3D_Mod_Object))
        {
            EDT_SubMenu *p_oSubMenu = _p_oEDTSubMenu -> fn_p_oGetNewSubMenu("Modules grid view");
            if( M_ContactGrid()->m_fn_bIsGridShowed() )
                p_oSubMenu -> AddAnEntry(this,"Hide Grid",C_uiMenuShowGrid);
            else
                p_oSubMenu -> AddAnEntry(this,"Show Grid",C_uiMenuShowGrid);
            if( M_ContactGrid()->m_fn_bIsSelectionShowed() )
                p_oSubMenu -> AddAnEntry(this,"Hide Selection",C_uiMenuShowSelection);
            else
                p_oSubMenu -> AddAnEntry(this,"Show Selection",C_uiMenuShowSelection);
            p_oSubMenu -> AddAnEntry(this, "Close", C_uiMenuMod);
            _p_oEDTSubMenu -> AddASubMenu( p_oSubMenu );
        }
        else
        {
            _p_oEDTSubMenu->AddAnEntry(this,"Modules grid view",C_uiMenuMod,FALSE,fn_bCanOpenTool(C_wTool3D_Mod_Object));
        }
        // single module view
        _p_oEDTSubMenu->AddAnEntry(this,"Single module view",C_uiMenuGeom,fn_bToolActive(C_wTool3D_Object),fn_bToolActive(C_wTool3D_Object)||fn_bCanOpenTool(C_wTool3D_Object));
 
		// viewer
        _p_oEDTSubMenu->AddASeparator();
        EDT_SubMenu *p_oSubMenu = _p_oEDTSubMenu -> fn_p_oGetNewSubMenu("Viewer");
        if(fn_bToolActive(C_wTool3D_Viewer))
            p_oSubMenu -> AddAnEntry(this, "Close", C_uiMenuViewer);
        else
            p_oSubMenu -> AddAnEntry(this, "Open", C_uiMenuViewer, FALSE, fn_bCanOpenTool(C_wTool3D_Viewer));
        p_oSubMenu -> AddAnEntry(this, "Edit Content" , C_uiMenuViewerCt , FALSE, fn_bToolActive(C_wTool3D_None) ) ;
        p_oSubMenu -> AddAnEntry(this, "Reset Content", C_uiMenuResetViewer, FALSE, M_GetNbObjects());
        p_oSubMenu -> AddASeparator();
        POSITION stPos = m_oListOfVWObjInfo . GetHeadPosition();
        int i = 0;
        while( stPos )
        {
            tdst_VW_ObjInfo *p_stVWOI = m_oListOfVWObjInfo . GetNext( stPos );
            if( p_stVWOI -> m_p_oSuperObject )
                p_oSubMenu -> AddAnEntry(this, (char*)(LPCTSTR)p_stVWOI -> m_p_oSuperObject -> GetObject() -> GetName(), C_uiMenuViewerTarget+i, FALSE,
                                         fn_bToolActive(C_wTool3D_Viewer) || fn_bToolActive(C_wTool3D_None) );
            i++;
        }
        _p_oEDTSubMenu -> AddASubMenu( p_oSubMenu );

		return TRUE;
    }
	return FALSE;
}

void Tool3D_Interface::_OnSubMenuCommand(EDT_SubMenu *_p_oEDTSubMenu,UINT uiMsgID)
{
//ANNECY MT NewInterface 26/03/98 {
	if(_p_oEDTSubMenu->GetSubMenuType() == C_SubMenuTool)
	{
		switch (uiMsgID)
		{
		case C_uiMenuAnim:	// Animation view
            if( fn_bIsCurrentEditor() )
			{
                GetMainWorld() -> fn_bCloseEditor(this);
				if(fn_bToolActive(C_wTool3D_Char))
					OnQueryClose(NULL, 0, 0);
			}
            else
			{
//Stefan Dumitrean 3-06-98
				m_bAnimPlayerInvoked = TRUE;
//End Stefan Dumitrean 3-06-98
                GetMainWorld() -> fn_bActivateEditor( this, NULL );
			}
            break;
		default:
			break;
		}        
	}
	else
//ENDANNECY MT NewInterface }
//ANNECY Shaitan NewInterface 24/03/98 {
    if(_p_oEDTSubMenu->GetSubMenuType() == C_SubMenu3DView)
//ENDANNECY Shaitan NewInterface }
    {
        switch (uiMsgID)
        {
        case C_uiMenuChar:
            if(fn_bToolActive(C_wTool3D_Char))
                OnQueryClose(NULL, 0, 0);
            else
                OnQueryOpen(NULL,(WPARAM)C_wTool3D_Char,(LPARAM)M_GetWorld()->GetSingleSelection());
            break;
        case C_uiMenuTable:
            if(fn_bToolActive(C_wTool3D_Table))
                OnQueryClose(NULL, 0, 0);
            else
            {
                EditorObjectTable *p_oObjectTable = NULL;

                if( mfn_bFindInfoAboutActor( M_GetWorld() ->GetSingleSelection(), NULL, NULL, & p_oObjectTable, NULL ) )
                    OnQueryOpen(NULL,(WPARAM)C_wTool3D_Table,(LPARAM)p_oObjectTable);
            }
            break;
        case C_uiMenuMod:
            if(fn_bToolActive(C_wTool3D_Mod_Object))
                OnQueryClose(NULL, 0, 0);
            else
                OnQueryOpen(NULL,(WPARAM)C_wTool3D_Mod_Object,(LPARAM)M_GetWorld()->GetSingleSelection());
            break;
        case C_uiMenuGeom:
            if(fn_bToolActive(C_wTool3D_Object))
                OnQueryClose(NULL, 0, 0);
            else
                OnQueryOpen(NULL,(WPARAM)C_wTool3D_Object,(LPARAM)M_GetWorld()->GetSingleSelection()->GetObject());
            break;
        case C_uiMenuViewer:
            if(fn_bToolActive(C_wTool3D_Viewer))
                OnQueryClose(NULL, 0, 0);
            else
                OnQueryOpen(NULL, (WPARAM)C_wTool3D_Viewer, 0);
            break;
        case C_uiMenuViewerCt:
            mfn_vEditViewerContent();
            break;
        case C_uiMenuResetViewer:
            mfn_vResetViewerContent();
            break;
        case C_uiMenuNone:
            if(!fn_bToolActive(C_wTool3D_None))
                OnQueryClose(NULL, 0, 0);
            break;
        case C_uiMenuShowGrid:
            if(M_ContactGrid()->m_fn_bIsGridShowed())
                OnQueryAction(NULL, C_wTool3D_QA_HideGrid, 0);
            else
                OnQueryAction(NULL, C_wTool3D_QA_ShowGrid, 0);
            break;
        case C_uiMenuShowSelection:
            if(M_ContactGrid()->m_fn_bIsSelectionShowed())
                OnQueryAction(NULL, C_wTool3D_QA_HideSelect, 0);
            else
                OnQueryAction(NULL, C_wTool3D_QA_ShowSelect, 0);
            break;
        default:
            int iTarget = uiMsgID - C_uiMenuViewerTarget;
            if( ( iTarget >= 0 ) && ( iTarget < M_GetNbObjects() ) )
                mfn_vRestoreCameraOnTarget( iTarget );
            break;
        }
    }
}

//--------------------------------------------------------------------------------
// Description
//--------------------------------------------------------------------------------
BOOL Tool3D_Interface::fn_bDefinePopupMenu(EDT_PopUpMenu *pPopup, CPA_List<CPA_SuperObject> *pSelection, BOOL bIsAlone)
{
    ASSERT( pPopup -> GetSubMenuType() == C_PopupMenu ) ;
    EDT_SubMenu *p_oSubMenu;
    BOOL bResult = FALSE;

    // Module Grid View
    switch( m_wTypeOfTool )
    {
    case C_wTool3D_Mod_Object:

        p_oSubMenu = pPopup -> fn_p_oGetNewSubMenu("Modules grid view");
        p_oSubMenu -> AddAnEntry(this,"Close",C_uiMenuMod);
        p_oSubMenu -> AddASeparator();
        if( M_ContactGrid()->m_fn_bIsGridShowed() )
            p_oSubMenu -> AddAnEntry(this,"Hide Grid",C_uiMenuShowGrid);
        else
            p_oSubMenu -> AddAnEntry(this,"Show Grid",C_uiMenuShowGrid);
        if( M_ContactGrid()->m_fn_bIsSelectionShowed() )
            p_oSubMenu -> AddAnEntry(this,"Hide Selection",C_uiMenuShowSelection);
        else
            p_oSubMenu -> AddAnEntry(this,"Show Selection",C_uiMenuShowSelection);
        pPopup -> AddASubMenu( p_oSubMenu );
        bResult = TRUE;
        break;
    case C_wTool3D_Char:
        pPopup -> AddAnEntry(this,"Close Character View", C_uiMenuChar);
        bResult = TRUE;
        break;
    case C_wTool3D_Geom_Object:
        pPopup -> AddAnEntry(this,"Close Single Module View", C_uiMenuGeom);
        bResult = TRUE;
        break;
    }
    // viewer
    if( fn_bToolActive(C_wTool3D_None) && ( pSelection != NULL ) && ( pSelection -> GetCount() ) )
    {
        pPopup -> AddAnEntry(this, "Add selected objet(s) on Viewer", C_uiMenuAddToViewer );
        bResult = TRUE;
    }
    return bResult;
}

//--------------------------------------------------------------------------------
// Description
//--------------------------------------------------------------------------------
void Tool3D_Interface::_OnPopUpMenuCommand(UINT m_IDCmdMsg)
{
    switch ( m_IDCmdMsg )
    {
    case C_uiMenuChar:
        if(fn_bToolActive(C_wTool3D_Char))
            OnQueryClose(NULL, 0, 0);
        else
            OnQueryOpen(NULL,(WPARAM)C_wTool3D_Char,(LPARAM)M_GetWorld()->GetSingleSelection());
        break;
    case C_uiMenuMod:
        if(fn_bToolActive(C_wTool3D_Mod_Object))
            OnQueryClose(NULL, 0, 0);
        else
            OnQueryOpen(NULL,(WPARAM)C_wTool3D_Mod_Object,(LPARAM)M_GetWorld()->GetSingleSelection());
        break;
    case C_uiMenuGeom:
        if(fn_bToolActive(C_wTool3D_Geom_Object))
            OnQueryClose(NULL, 0, 0);
        else
            OnQueryOpen(NULL,(WPARAM)C_wTool3D_Geom_Object,(LPARAM)M_GetWorld()->GetSingleSelection());
        break;
    case C_uiMenuAddToViewer:
        if( M_GetWorld() -> GetCountSelected() == 1)
            mfn_vAddAnObject( M_GetWorld()->GetSingleSelection(), TRUE );
        else if( M_GetWorld() -> GetCountSelected() > 1)
            mfn_vAddAListOfSuperObject( M_GetWorld() -> GetListSelected(), TRUE );
        break;
    case C_uiMenuShowGrid:
        if(M_ContactGrid()->m_fn_bIsGridShowed())
            OnQueryAction(NULL, C_wTool3D_QA_HideGrid, 0);
        else
            OnQueryAction(NULL, C_wTool3D_QA_ShowGrid, 0);
        break;
    case C_uiMenuShowSelection:
        if(M_ContactGrid()->m_fn_bIsSelectionShowed())
            OnQueryAction(NULL, C_wTool3D_QA_HideSelect, 0);
        else
            OnQueryAction(NULL, C_wTool3D_QA_ShowSelect, 0);
        break;
    }
}

///////////////////
// Query methods //
///////////////////
////
long Tool3D_Interface::OnQueryOpen(CPA_EditorBase* pCallingDLL, WPARAM _wParam, LPARAM _lParam)
{
	// if editors are in fullscreen, send a window message
//	if (M_bFullScreenEditor())
//		M_GetMainWnd()->SendMessage(WM_COMMAND,CPA_IDCB_FULLSCREEN);

	// WPARAM = type of tool opened
    if( !fn_bTypeOfToolOK( _wParam ) )
        return ( (long) FALSE );

    // LPARAM = object used
    if( !fn_bCanUseObject( (long)_wParam, (long)_lParam ) )
        return ( (long) FALSE );

    // if tool is already opened with the right mode, do nothing
	if (m_wTypeOfTool == _wParam)
		return ((long) TRUE);

	// if tool already opened, close it
    if(m_wTypeOfTool != C_wTool3D_None)
        OnQueryClose(pCallingDLL,0,0);

//	M_GetMainApp()->m_bViewerOpened = TRUE;
    // light
	GetMainWorld() -> m_a_tdxhArrayOfLocalLights[ GetMainWorld() -> m_lNbLocalLights++ ] = m_hLightDown;
	GetMainWorld() -> m_a_tdxhArrayOfLocalLights[ GetMainWorld() -> m_lNbLocalLights++ ] = m_hLightDown;
	GetMainWorld() -> m_a_tdxhArrayOfLocalLights[ GetMainWorld() -> m_lNbLocalLights++ ] = m_hLightUp;
	GetMainWorld() -> m_a_tdxhArrayOfLocalLights[ GetMainWorld() -> m_lNbLocalLights++ ] = m_hLightUp;

    m_wTypeOfTool = _wParam;
    m_p_oSprObjActor = NULL;

    m_p_PreviousMultiDevice = GetInterface()->GetMultiDevice();
    // create contact
    switch (m_wTypeOfTool)
    {
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
        case C_wTool3D_Table:
            m_p_Tool3DContact = new CPA_Grid(m_p_oKeyboard, m_p_Tool3DMultiDevice);
            break;
        case C_wTool3D_Char:
        case C_wTool3D_Geom_Object:
        case C_wTool3D_Viewer:
        case C_wTool3D_Object:
            m_p_Tool3DContact = new CPA_Contact(m_p_Tool3DMultiDevice);
            break;
    }
    m_p_Tool3DMultiDevice -> SetContact ( m_p_Tool3DContact ) ;
    m_p_Tool3DMultiDevice -> SetWorld ( m_p_Tool3DWorld ) ;
    m_p_Tool3DMultiDevice -> SetInterface ( GetInterface() ) ;
   //chbani GetInterface()->SetMultiDevice(m_p_Tool3DMultiDevice);
    m_csLastSelectMode = GetInterface()-> GetSelectMode();
    GetInterface()-> SetSelectMode("All Types");
	// create camera for main device (if there is none!)
	if (((DEV_ViewPort3D*)(m_p_Tool3DMultiDevice->GetDevice(0)->GetViewPort()))->GetCamera() == NULL)
	{
		CPA_CameraDLLBase *p_oCamDLL;
		CPA_SuperObject *p_oCameraSO;

		p_oCamDLL = (CPA_CameraDLLBase*) GetMainWorld()->GetObjectDLLWithName(C_szDLLCameraName);
		ASSERT(p_oCamDLL != NULL);

		p_oCameraSO = p_oCamDLL->CreateDefaultCamera();
		ASSERT(p_oCameraSO != NULL);

		((DEV_ViewPort3D*)(m_p_Tool3DMultiDevice->GetDevice(0)->GetViewPort()))->ChangeCamera(p_oCameraSO);
	}

    GetInterface()->SetMultiDevice(m_p_Tool3DMultiDevice); //chbani 
	// create world
    switch (m_wTypeOfTool)
    {
        // char
        case C_wTool3D_Char:
            //m_p_oSprObjActor = (CPA_SuperObject*)_lParam;
            //m_p_Tool3DWorld -> mfn_vCreateHierarchyFromActor( (CPA_SuperObject*)_lParam) ;
            mfn_vOpenAnim( (CPA_SuperObject*)_lParam ) ;
            break;
        // mod
        case C_wTool3D_Mod_Object:
            m_p_Tool3DWorld -> mfn_vCreateGridHierarchyFromActor((CPA_SuperObject*)_lParam);
            break;
        case C_wTool3D_Mod_List:
            m_p_Tool3DWorld -> mfn_vCreateGridHierarchyFromList((CPA_List<CPA_BaseObject>*)_lParam,GetMainWorld());
            break;
        case C_wTool3D_Table:
            m_p_Tool3DWorld -> mfn_vCreateGridHierarchyFromTable((CPA_BaseObject*)_lParam,GetMainWorld());
            break;
        // geom
        case C_wTool3D_Geom_Object:
            m_p_Tool3DWorld -> mfn_vCreateHierarchyFromSuperObject( (CPA_SuperObject*)_lParam) ;
            break;
        case C_wTool3D_Viewer:
            m_p_Tool3DWorld -> mfn_vCreateViewerHierarchyFromVWOIList(&m_oListOfVWObjInfo,GetMainWorld());
            break;
        case C_wTool3D_Object:
            m_p_Tool3DWorld -> mfn_vCreateHierarchyFromObject((CPA_BaseObject*)_lParam);
            break;
    }
    m_p_Tool3DMultiDevice->SetFocusDevice(0);

    // activate Frame
    m_p_PreviousFRMBase = (FRMBaseMenu*)g_oFrameGest.ma_p_oWinArray[2][2];
    m_p_FRMBase->fn_vShowGeneralInterface(TRUE);
    g_oFrameGest.mfn_vSetRefresh(FALSE);
    g_oFrameGest.mfn_vActivateWindow (m_p_FRMBase) ;
    g_oFrameGest.mfn_vSetRefresh(TRUE);
//    m_p_FRMBase->m_dwCaptionStyle = WS_CAPTION|WS_MINIMIZEBOX;
//    m_p_FRMBase->mfn_vEnableCaption(TRUE);
    // special init
    switch (m_wTypeOfTool)
    {
        case C_wTool3D_Char:
            mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
            m_p_FRMBase -> SetWindowText( "Tool3D : Character View" );
            break;
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
        case C_wTool3D_Table:
            // positione super-object on a grid
            M_ContactGrid()->m_fn_vPositionWorldOnGrid();
            M_ContactGrid()->m_fn_vShowGrid();
            M_ContactGrid()->m_fn_vShowSelection();
            m_p_FRMBase -> SetWindowText( "Tool3D : Modules Grid View" );
            break;
        case C_wTool3D_Geom_Object:
            mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
            m_p_FRMBase -> SetWindowText( "Tool3D : Single Module View" );
            break;
        case C_wTool3D_Viewer:
            mfn_vRestoreCameraOnTarget( 0 );
            m_p_FRMBase -> SetWindowText( "Tool3D : Viewer" );
            break;
        case C_wTool3D_Object:
            mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
            m_p_FRMBase -> SetWindowText( "Tool3D : Single Module View" );
            break;
    }
    //
    m_p_Tool3DMultiDevice -> SetFocusDevice( 0 );
    //
    GetInterface() -> fn_vChangeWorld();

    // call Zone DLL
    CPA_DLLBase *p_oDLLZone = GetMainWorld() -> GetObjectDLLWithName( C_szDLLZDxName );
    if( p_oDLLZone )
        p_oDLLZone -> OnQueryInfos( pCallingDLL, C_uiQueryInfoT3DStateChange, m_wTypeOfTool);

    // redraw the world
    M_RedrawAll();

	// display Tip of the Day
    switch (m_wTypeOfTool)
    {
        case C_wTool3D_Char:
			GetInterface()->fn_vDisplayTipOfDay("Tool 3D", "T3D", g_bTipToolFirstTime);
			g_bTipToolFirstTime = FALSE;
            break;
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
        case C_wTool3D_Table:
			GetInterface()->fn_vDisplayTipOfDay("Grid View", "T3G", g_bTipGridFirstTime);
			g_bTipGridFirstTime = FALSE;
            break;
        case C_wTool3D_Geom_Object:
			GetInterface()->fn_vDisplayTipOfDay("Tool 3D", "T3D", g_bTipToolFirstTime);
			g_bTipToolFirstTime = FALSE;
            break;
        case C_wTool3D_Viewer:
			GetInterface()->fn_vDisplayTipOfDay("Anim Player", "TAN", g_bTipAnimFirstTime);
			g_bTipAnimFirstTime = FALSE;
            break;
        case C_wTool3D_Object:
			GetInterface()->fn_vDisplayTipOfDay("Tool 3D", "T3D", g_bTipToolFirstTime);
			g_bTipToolFirstTime = FALSE;
            break;
    }

    // update buttons
	m_p_FRMBase->m_oGeneralDialogBar.ShowWindow(TRUE);

    return ((long)TRUE);
}

long Tool3D_Interface::OnQueryAction(CPA_EditorBase*, WPARAM _wParam, LPARAM _lParam)
{
    long lResult = (long)TRUE;
    // WPARAM = type of action
    // LPARAM = data action
    switch (_wParam)
    {
        // C_3DCharTool
        // C_3DModTool
        case C_wTool3D_QA_ShowGrid:
            if( (m_wTypeOfTool==C_wTool3D_Mod_Object) || (m_wTypeOfTool==C_wTool3D_Mod_List) || (m_wTypeOfTool==C_wTool3D_Table))
            {
                M_ContactGrid()->m_fn_vShowGrid();
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_HideGrid:
            if( (m_wTypeOfTool==C_wTool3D_Mod_Object) || (m_wTypeOfTool==C_wTool3D_Mod_List) || (m_wTypeOfTool==C_wTool3D_Table) )
            {
                M_ContactGrid()->m_fn_vHideGrid();
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_ShowSelect:
            if( (m_wTypeOfTool==C_wTool3D_Mod_Object) || (m_wTypeOfTool==C_wTool3D_Mod_List) || (m_wTypeOfTool==C_wTool3D_Table) )
            {
                M_ContactGrid()->m_fn_vShowSelection();
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_HideSelect:
            if( (m_wTypeOfTool==C_wTool3D_Mod_Object) || (m_wTypeOfTool==C_wTool3D_Mod_List) || (m_wTypeOfTool==C_wTool3D_Table) )
            {
                M_ContactGrid()->m_fn_vHideSelection();
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_NewChar:
            if( (m_wTypeOfTool==C_wTool3D_Char) && fn_bCanUseObject(C_wTool3D_Char,_lParam) )
            {
                m_p_Tool3DWorld -> mfn_vResetWorld();
                //m_p_Tool3DWorld -> mfn_vCreateHierarchyFromActor( (CPA_SuperObject*)_lParam);
                mfn_vOpenAnim( (CPA_SuperObject*)_lParam);
                mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
                m_p_Tool3DMultiDevice -> SetFocusDevice( 0 );
                //
				GetInterface() -> fn_vChangeWorld();
                // redraw the world
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_NewSuperObject:
            if( (m_wTypeOfTool==C_wTool3D_Geom_Object) && fn_bCanUseObject(C_wTool3D_Geom_Object,_lParam) )
            {
                m_p_Tool3DWorld -> mfn_vResetWorld();
                m_p_Tool3DWorld -> mfn_vCreateHierarchyFromSuperObject( (CPA_SuperObject*)_lParam);
                mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
                m_p_Tool3DMultiDevice -> SetFocusDevice( 0 );
                //
				GetInterface() -> fn_vChangeWorld();
                // redraw the world
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_NewTable:
            if( (m_wTypeOfTool==C_wTool3D_Table) && fn_bCanUseObject(C_wTool3D_Table,_lParam) )
            {
                m_p_Tool3DWorld -> mfn_vResetWorld();
                m_p_Tool3DWorld -> mfn_vCreateGridHierarchyFromTable( (CPA_BaseObject*)_lParam, GetMainWorld());
                // positione super-object on a grid
                M_ContactGrid()->m_fn_vPositionWorldOnGrid();
                M_ContactGrid()->m_fn_vShowGrid();
                M_ContactGrid()->m_fn_vShowSelection();
                m_p_Tool3DMultiDevice -> SetFocusDevice( 0 );
                //
				GetInterface() -> fn_vChangeWorld();
                // redraw the world
                M_RedrawAll();
            }
            break;
        case C_wTool3D_QA_NewObject:
            if( (m_wTypeOfTool==C_wTool3D_Object) && fn_bCanUseObject(C_wTool3D_Object,_lParam) )
            {
                m_p_Tool3DWorld -> mfn_vResetWorld();
                m_p_Tool3DWorld -> mfn_vCreateHierarchyFromObject( (CPA_BaseObject*)_lParam);
                mfn_vCenterCamera( m_p_Tool3DMultiDevice -> GetFocusDevice() , m_p_Tool3DWorld -> GetRoot() -> GetSuperObjectFirstChild() );
                m_p_Tool3DMultiDevice -> SetFocusDevice( 0 );
                //
				GetInterface() -> fn_vChangeWorld();
                // redraw the world
                M_RedrawAll();
            }
            break;
        // C_3DGeomTool
        default:
            lResult = (long)FALSE;
    }
    return lResult;
}

long Tool3D_Interface::OnQueryInfos(CPA_EditorBase*, WPARAM _wParam, LPARAM _lParam)
{
    long lResult = (long)FALSE;
    //_wParam = Request
    // return ptr on object according to WPARAM
    switch(_wParam)
    {
        case C_wTool3D_QI_CanOpenChar:
            // only if current selection is a character
            lResult = (long) fn_bCanOpenTool(C_wTool3D_Char);
            break;
        case C_wTool3D_QI_CanOpenMod:
            // only if current selection is a character
            lResult = (long) fn_bCanOpenTool(C_wTool3D_Mod_Object);
            break;
        case C_wTool3D_QI_CanOpenGeom:
            // only if current selection is a geometric object
            lResult = (long) fn_bCanOpenTool(C_wTool3D_Geom_Object);
            break;
        case C_wTool3D_QI_CanOpenViewer:
            // only if viewer contents isn't empty
            lResult = (long) ( (CPA_List<CPA_BaseObject>*)_lParam ) -> GetCount();
            break;
        case C_wTool3D_QI_CanOpenTable:
            // only if viewer contents isn't empty
            lResult = (long) fn_bCanOpenTool(C_wTool3D_Geom_Object);
            break;
        case C_wTool3D_Object:
            // only if current selection is an object
            lResult = (long) fn_bCanOpenTool(C_wTool3D_Object);
            break;
        case C_wTool3D_QI_GetState:
            lResult = (long) m_wTypeOfTool;
            break;
        case C_wTool3D_QI_GetWorld:
            lResult = (long) m_p_Tool3DWorld;
            break;
        case C_wTool3D_QI_GetSOOnGrid:
            if ( (m_wTypeOfTool==C_wTool3D_Mod_Object) || (m_wTypeOfTool==C_wTool3D_Mod_List) || (m_wTypeOfTool==C_wTool3D_Table))
                lResult = (long) M_ContactGrid() -> m_fn_p_oGetSuperObjectFromPos( (tdstMousePos*) _lParam );
            break;
    }
    return lResult;
}

long Tool3D_Interface::OnQueryClose(CPA_EditorBase* pCallingDLL, WPARAM, LPARAM)
{
    long lResult = (long)TRUE;

     if( fn_bIsCurrentEditor() )
        GetMainWorld() -> fn_bCloseEditor(this);

    switch (m_wTypeOfTool)
    {
        case C_wTool3D_Viewer:
        case C_wTool3D_Char:
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
        case C_wTool3D_Geom_Object:
        case C_wTool3D_Object:
        case C_wTool3D_Table:
            // activate previous frame
            g_oFrameGest.mfn_vSetRefresh(FALSE);
            g_oFrameGest.mfn_vActivateWindow (m_p_PreviousFRMBase) ;
            g_oFrameGest.mfn_vSetRefresh(TRUE);
			M_GetMainWnd()->SendMessage(WM_SYSCOMMAND,SC_RESTORE);
            // delete hierarchy
            m_p_Tool3DWorld -> mfn_vResetWorld();
            delete m_p_Tool3DContact;
            // restore previous multidevice
            GetInterface()->SetMultiDevice(m_p_PreviousMultiDevice);
            GetInterface()->SetSelectMode(m_csLastSelectMode);

            m_p_Tool3DMultiDevice->SetInterface(NULL);
            m_p_Tool3DMultiDevice->SetContact(NULL);
            // light
            GetMainWorld() -> m_lNbLocalLights -= 4;
			// restore buttons
			m_p_PreviousFRMBase->m_oGeneralDialogBar.ShowWindow(TRUE);

            break;
        default:
            lResult = (long)FALSE;
    }
    m_wTypeOfTool = C_wTool3D_None;

    // call Zone DLL
    CPA_DLLBase *p_oDLLZone = GetMainWorld() -> GetObjectDLLWithName( C_szDLLZDxName );
    if( p_oDLLZone )
        p_oDLLZone -> OnQueryInfos( pCallingDLL, C_uiQueryInfoT3DStateChange, m_wTypeOfTool);

//	M_GetMainApp()->m_bViewerOpened = FALSE;
    return lResult;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
// Description :
//--------------------------------------------------------------------------------
BOOL Tool3D_Interface::fn_bTypeOfToolOK(long _wType)
{
    return ( (_wType==C_wTool3D_Char) ||
             (_wType==C_wTool3D_Mod_Object)  || (_wType==C_wTool3D_Mod_List)  ||
             (_wType==C_wTool3D_Geom_Object) || (_wType==C_wTool3D_Viewer)    ||
             (_wType==C_wTool3D_Table)       || (_wType==C_wTool3D_Object)
           );
}

//--------------------------------------------------------------------------------
// Description :
//--------------------------------------------------------------------------------
BOOL Tool3D_Interface::fn_bCanUseObject(long _wType, long _lParam)
{
    CPA_BaseObject  *p_oObject = NULL;
    CPA_BaseObject  *p_oReaObj = NULL;

    switch (_wType)
    {
    case C_wTool3D_Char:
    case C_wTool3D_Mod_Object:
        p_oReaObj = (CPA_BaseObject*)_lParam;
        if( p_oReaObj -> fn_bIsOfType( C_szSuperObjectTypeName ) )
        {
            p_oObject = ((CPA_SuperObject*)p_oReaObj) -> GetObject();
            return ( p_oObject &&
                     p_oObject -> fn_bIsOfType( C_szActorInstanceTypeName ) &&
                     (((CPA_SuperObject*)p_oReaObj) -> GetCount() > 0)
                   );
        }
        return FALSE;
        break;
    case C_wTool3D_Geom_Object:
        p_oReaObj = (CPA_BaseObject*)_lParam;
        if( p_oReaObj -> fn_bIsOfType( C_szSuperObjectTypeName ) )
        {
            p_oObject = ((CPA_SuperObject*)p_oReaObj) -> GetObject();
            return ( p_oObject &&
                     ( p_oObject -> fn_bIsOfType( C_szGeometricObjectTypeName ) ||
                       p_oObject -> fn_bIsOfType( C_szPhysicalObjectTypeName ) ||
                       p_oObject -> fn_bIsOfType( C_szInstanciatedPhysicalObjectTypeName )
                     )
                   );
        }
        return FALSE;
        break;
    case C_wTool3D_None:
        return TRUE;
        break;
    case C_wTool3D_Mod_List:
        return ( _lParam && ((CPA_List<CPA_BaseObject>*)_lParam) -> GetCount() );
    case C_wTool3D_Viewer:
        return ( (BOOL) m_oListOfVWObjInfo . GetCount() );
        break;
    case C_wTool3D_Table:
        p_oReaObj = (CPA_BaseObject*)_lParam;
        return ( p_oReaObj && p_oReaObj -> fn_bIsOfType( C_szDLLObjectTableName ) );
        break;
    case C_wTool3D_Object:
        p_oObject = (CPA_BaseObject*)_lParam;
        return ( p_oObject &&
                 ( p_oObject -> fn_bIsOfType( C_szGeometricObjectTypeName ) ||
                   p_oObject -> fn_bIsOfType( C_szPhysicalObjectTypeName ) ||
                   p_oObject -> fn_bIsOfType( C_szInstanciatedPhysicalObjectTypeName )
                 )
               );
        break;
    }
    return FALSE;
}

//--------------------------------------------------------------------------------
// Description :
//--------------------------------------------------------------------------------
BOOL Tool3D_Interface::fn_bCanOpenTool(long _wType)
{
    BOOL bResult = FALSE;
    CPA_SuperObject *p_oSprObj = NULL;
    CPA_BaseObject *p_oObject = NULL;

    switch (_wType)
    {
        case C_wTool3D_Char:
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
        case C_wTool3D_Table:
            if( M_GetWorld()->GetCountSelected() == 1 )
            {
                CPA_Family          *p_oFamily      = NULL;
                CPA_Animation       *p_oAnimation   = NULL;
                EditorObjectTable   *p_oObjectTable = NULL;
                unsigned short        uwFrame        = 0;

                p_oSprObj = M_GetWorld()->GetSingleSelection();
                p_oObject = p_oSprObj -> GetObject();
                bResult = ( p_oObject &&
                            p_oObject -> fn_bIsOfType( C_szActorInstanceTypeName ) &&
                            ( p_oSprObj -> GetCount() > 0) &&
                            mfn_bFindInfoAboutActor( p_oSprObj, &p_oFamily, &p_oAnimation, &p_oObjectTable, &uwFrame )
                          );
            }
            break;
        case C_wTool3D_Geom_Object:
            if( M_GetWorld()->GetCountSelected() == 1 )
            {
                p_oSprObj = M_GetWorld()->GetSingleSelection();
                p_oObject = p_oSprObj -> GetObject();
                bResult = ( p_oObject &&
                            ( p_oObject -> fn_bIsOfType( C_szGeometricObjectTypeName ) ||
                              p_oObject -> fn_bIsOfType( C_szPhysicalObjectTypeName )  ||
                              p_oObject -> fn_bIsOfType( C_szInstanciatedPhysicalObjectTypeName )
                            )
                          );
            }
            break;
        case C_wTool3D_None:
            bResult = TRUE;
            break;
        case C_wTool3D_Viewer:
            bResult = (fn_bToolActive(C_wTool3D_None) && m_oListOfVWObjInfo . GetCount());
            break;
        case C_wTool3D_Object:
            if( M_GetWorld()->GetCountSelected() == 1 )
            {
                p_oObject = M_GetWorld()->GetSingleSelection() -> GetObject();
                bResult = ( p_oObject &&
                            ( p_oObject -> fn_bIsOfType( C_szGeometricObjectTypeName ) ||
                              p_oObject -> fn_bIsOfType( C_szPhysicalObjectTypeName )  ||
                              p_oObject -> fn_bIsOfType( C_szInstanciatedPhysicalObjectTypeName )
                            )
                          );
            }
            break;
    }
    return bResult;
}

//--------------------------------------------------------------------------------
// Description :
//--------------------------------------------------------------------------------
BOOL Tool3D_Interface::fn_bToolActive(long _wType)
{
    BOOL bResult = FALSE;

    switch(_wType)
    {
        case C_wTool3D_Mod_Object:
        case C_wTool3D_Mod_List:
            bResult = ( (m_wTypeOfTool==C_wTool3D_Mod_Object)  || (m_wTypeOfTool==C_wTool3D_Mod_List) );
            break;
        case C_wTool3D_Geom_Object:
            bResult = (m_wTypeOfTool==C_wTool3D_Geom_Object);
            break;
        case C_wTool3D_Char:
        case C_wTool3D_None:
        case C_wTool3D_Viewer:
        case C_wTool3D_Table:
        case C_wTool3D_Object:
            bResult = (m_wTypeOfTool==_wType);
            break;
    }
    return bResult;
}

//--------------------------------------------------------------------------------
// Description : add a new object to list
//--------------------------------------------------------------------------------
void Tool3D_Interface::mfn_vAddAnObject(CPA_SuperObject *_p_oSprObj, BOOL _bUseCameraPos /*=TRUE*/)
{
    tdst_VW_ObjInfo *p_stObjInfo = NULL;

    // object already targeted ?
    POSITION stPos = m_oListOfVWObjInfo . GetHeadPosition();
    while( stPos )
    {
        tdst_VW_ObjInfo *p_stVWOI = m_oListOfVWObjInfo . GetNext( stPos );
        if( p_stVWOI -> m_p_oSuperObject == _p_oSprObj )
        {
            p_stObjInfo = p_stVWOI;
            break;
        }
    }
    // if not found, create one
    if( p_stObjInfo == NULL )
    {
        p_stObjInfo = (tdst_VW_ObjInfo*) malloc( sizeof (tdst_VW_ObjInfo ) );
        m_oListOfVWObjInfo . AddHead( p_stObjInfo );
    }

    // fill tdst_VW_ObjInfo
    p_stObjInfo -> m_p_oSuperObject = _p_oSprObj;
    p_stObjInfo -> m_bUsePos = _bUseCameraPos;
	POS_fn_vSetIdentityMatrix(&p_stObjInfo -> stMatrix);
    if(p_stObjInfo -> m_bUsePos)
    {
        CPA_CameraDLLBase           *p_oCameraInterface   = M_GetLSTCameraInterface ();
        CPA_BaseObject              *p_oCamera            = M_DeviceCamera( M_GetMultiDevice3D()->GetFocusDevice() );
        POS_tdxHandleToPosition      p_stMatrix           = HIE_fn_hGetSuperObjectMatrix( _p_oSprObj->GetStruct() );
        POS_tdstCompletePosition     stCameraMatrix , stInvMatrix;

		POS_fn_vSetIdentityMatrix(&stCameraMatrix);
		POS_fn_vSetIdentityMatrix(&stInvMatrix);

        p_oCameraInterface -> GetMatrix( p_oCamera, &stCameraMatrix );
        POS_fn_vInvertMatrix( &stInvMatrix, p_stMatrix );
        POS_fn_vMulMatrixMatrix( & p_stObjInfo -> stMatrix, &stInvMatrix, & stCameraMatrix );
    }
}

//--------------------------------------------------------------------------------
// Description : add a list of objects to list
//--------------------------------------------------------------------------------
void Tool3D_Interface::mfn_vAddAListOfSuperObject(CPA_List<CPA_SuperObject> *_p_oList, BOOL _bUseCameraPos /*=TRUE*/)
{
    // add superobject to list
    POSITION stPos = _p_oList -> GetHeadPosition ();
    while ( stPos )
    {
        CPA_SuperObject *p_oSprObj = _p_oList -> GetNext( stPos );
        if( p_oSprObj && p_oSprObj -> GetObject() )
            mfn_vAddAnObject( p_oSprObj , _bUseCameraPos );
    }
}

//--------------------------------------------------------------------------------
// Description : Set camera to see target
//--------------------------------------------------------------------------------
void Tool3D_Interface::mfn_vCenterCamera(DEV_Device *p_oDevice, CPA_SuperObject *_p_oSprObj)
{
    CPA_CameraDLLBase *p_oCameraInterface = M_GetLSTCameraInterface ();
    CPA_BaseObject    *p_oCamera = M_DeviceCamera( p_oDevice );
    MTH3D_tdstVector stObjPos;
    MTH_tdxReal xRadius;


    // get Object Position
    if( m_p_Tool3DWorld -> mfn_bComputeCenterAndRadiusBoundingBox( _p_oSprObj, &stObjPos, &xRadius ) )
    {
        MTH_tdxReal      xAlphaX,xRatio;
        CPA_CameraCoords CameraCoords;

        GLI_xGetCameraAspectAndRatio((GLI_tdxHandleToCamera)p_oCamera->GetData(),&xAlphaX,&xRatio);

/*
        float fD1 = MTH_M_xRealToFloat(MTH_M_xDiv(xRadius, MTH_M_xTan(MTH_M_xDiv(xAlphaX,2))));
        float fD2 = MTH_M_xRealToFloat(MTH_M_xDiv(xRadius, MTH_M_xTan(MTH_M_xDiv(MTH_M_xMul(xAlphaX,xRatio),2))));
    	xRadius = ((fD1 > fD2)? fD1 : fD2);
*/
        CameraCoords . SetXYZ (MTH_M_xRealToFloat( MTH3D_M_xGetXofVector( &stObjPos ) ),
                               MTH_M_xRealToFloat( MTH3D_M_xGetYofVector( &stObjPos ) ),
                               MTH_M_xRealToFloat( MTH3D_M_xGetZofVector( &stObjPos ) ) );
        CameraCoords . SetAxisSystem (WorldCoordinates);
        p_oCameraInterface -> SetTargetPoint( p_oCamera, & CameraCoords);
        p_oCameraInterface -> SetTargetType (p_oCamera , tPoint);
        // set position camera
        CameraCoords . SetXYZ (MTH_M_xRealToFloat( MTH3D_M_xGetXofVector( &stObjPos )),
                               MTH_M_xRealToFloat( MTH3D_M_xGetYofVector( &stObjPos ) + MTH_M_xNeg( xRadius )),
                               MTH_M_xRealToFloat( MTH3D_M_xGetZofVector( &stObjPos )));
        CameraCoords . SetAxisSystem (WorldCoordinates);
        p_oCameraInterface -> SetPosition (p_oCamera , & CameraCoords);

        // Free camera
        p_oCameraInterface -> SetTargetType (p_oCamera , tNone);
    }
}

//--------------------------------------------------------------------------------
// Description : Set camera to see target   (NEW)
//--------------------------------------------------------------------------------
void Tool3D_Interface::mfn_vCenterCameraOnTarget(tdst_VW_ObjInfo *_p_stObjInfo)
{
    CPA_CameraDLLBase *p_oCameraInterface = M_GetLSTCameraInterface ();
    CPA_BaseObject    *p_oCamera = M_GetLSTViewPort() -> GetCamera();
    MTH3D_tdstVector   stObjPos;
    MTH_tdxReal        xRadius;
    CPA_SuperObject   *p_oSprObj = _p_stObjInfo -> m_p_oSuperObject;
    CPA_CameraCoords   CameraCoords;

    MTH3D_M_vNullVector( &stObjPos );
    xRadius = 0;

    // set target
    p_oCameraInterface -> SetTargetSuperObject( p_oCamera, p_oSprObj );
    p_oCameraInterface -> SetTargetType (p_oCamera , tSuperObject);
    // set camera pos
    if( _p_stObjInfo -> m_bUsePos )
    {
        POS_tdxHandleToPosition     hMatrix = HIE_fn_hGetSuperObjectMatrix( p_oSprObj -> GetStruct() );
        POS_tdstCompletePosition    stCameraMatrix;

		POS_fn_vSetIdentityMatrix(&stCameraMatrix);
        POS_fn_vMulMatrixMatrix( & stCameraMatrix, hMatrix, & _p_stObjInfo -> stMatrix );

        p_oCameraInterface -> SetMatrix( p_oCamera, & stCameraMatrix );
    }
    else
    {
        m_p_Tool3DWorld -> mfn_bComputeCenterAndRadiusBoundingBox( p_oSprObj, &stObjPos, &xRadius );
        xRadius = (float) MTH_M_xMul( xRadius ? xRadius : 1 , C_xCameraDist );
        // set position camera
        CameraCoords . SetXYZ ( MTH_M_xRealToFloat( MTH3D_M_xGetXofVector( &stObjPos ) ),
                                MTH_M_xRealToFloat( MTH3D_M_xGetYofVector( &stObjPos ) + MTH_M_xNeg( xRadius ) ),
                                MTH_M_xRealToFloat( MTH3D_M_xGetZofVector( &stObjPos ) ) );
        CameraCoords . SetAxisSystem (WorldCoordinates);
        p_oCameraInterface -> SetPosition (p_oCamera , & CameraCoords);
    }

    // Free camera
    p_oCameraInterface -> SetTargetType (p_oCamera , tNone);
}

// ----------------------------------------------------------------------------
// Description : Open Viewer Content Dialog
// ----------------------------------------------------------------------------
void Tool3D_Interface::mfn_vEditViewerContent()
{
    HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle( m_p_stDLLIdentity->hModule );

    CDlgViewerContent *p_oDlg = new CDlgViewerContent(GetMainWorld(),&m_oListOfVWObjInfo);
    int iResult = p_oDlg -> DoModal();

    if( iResult == C_OPENVIEWER )
        OnQueryOpen(NULL, (WPARAM)C_wTool3D_Viewer, 0);

    delete p_oDlg;

    AfxSetResourceHandle( hOldInst );
}

// ----------------------------------------------------------------------------
// Description : Restore camera on saved target (NEW)
// ----------------------------------------------------------------------------
void Tool3D_Interface::mfn_vRestoreCameraOnTarget( int _iTarget )
{
    ASSERT( ( _iTarget >= 0 ) && ( _iTarget<= M_GetNbObjects() ) );
    POSITION stPos = m_oListOfVWObjInfo . FindIndex( _iTarget );
    if ( stPos )
    {
        mfn_vCenterCameraOnTarget( m_oListOfVWObjInfo . GetAt( stPos ) );
        M_RedrawAll();
    }
}

// ----------------------------------------------------------------------------
// Description : Restore camera on saved target (NEW)
// ----------------------------------------------------------------------------
void Tool3D_Interface::mfn_vResetViewerContent()
{
    while( ! m_oListOfVWObjInfo . IsEmpty() )
        free( m_oListOfVWObjInfo . RemoveHead() );
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
void Tool3D_Interface::mfn_vOpenAnim(CPA_SuperObject *_p_oSprObjActor)
{
    m_p_oSprObjActor = m_p_Tool3DWorld -> mfn_p_oCreateHierarchyFromAnim(_p_oSprObjActor);
}

// ----------------------------------------------------------------------------
// Description :
// ----------------------------------------------------------------------------
BOOL Tool3D_Interface::mfn_bFindInfoAboutActor(CPA_SuperObject *_p_oSprObj, CPA_Family **_p_p_oFamily,
                                               CPA_Animation **_p_p_oAnim, EditorObjectTable **_p_p_oObjectTable,
                                               unsigned short *_p_uwFrame)
{
    BOOL bResult = TRUE;
    CPA_Family *p_oFamily = NULL;

    // init
    if( _p_p_oFamily )      *_p_p_oFamily       = NULL;
    if( _p_p_oAnim )        *_p_p_oAnim         = NULL;
    if( _p_p_oObjectTable ) *_p_p_oObjectTable  = NULL;
    if( _p_uwFrame )        *_p_uwFrame         = 0;
    //
    HIE_tdxHandleToSuperObject      hSO             = _p_oSprObj -> GetStruct();
    MS_tdxHandleTo3dData            h3dData         = M_GetMSHandle( hSO, 3dData );

    if( h3dData == NULL ) return FALSE;

    tdxHandleToState                hState          = fn_h3dDataGetCurrentState( h3dData ) ? fn_h3dDataGetCurrentState( h3dData )
																						   : fn_h3dDataGetInitialState( h3dData );
    struct tdstAnim3d_              *p_stAnim       = hState ? fn_p_stGetAnimInState( hState ) : NULL;
    unsigned short                  uwFrame         = fn_uw3dDataGetCurrentFrame( h3dData );
    tdxHandleToObjectsTablesList    hObjectTable    = fn_h3dDataGetCurrentObjectsTable( h3dData );

    p_oFamily = (CPA_Family*) _p_oSprObj -> GetObject() -> GetOwner() -> GetOwner();

    if( _p_p_oFamily )
    {
        *_p_p_oFamily = p_oFamily;
        bResult = ( *_p_p_oFamily != NULL );
    }

    if( _p_p_oAnim )
    {
        *_p_p_oAnim = p_stAnim ? (CPA_Animation*)GetMainWorld() -> fn_p_oFindObjectWithEngine( p_stAnim, C_szAnimationTypeName, p_oFamily ) : NULL;
        bResult &= ( ( *_p_p_oAnim != NULL ) && ( (*_p_p_oAnim) -> fn_bIsAvailable() ) );
    }

    if( _p_uwFrame )
    {
        *_p_uwFrame = uwFrame;
    }

    if( _p_p_oObjectTable )
    {
        *_p_p_oObjectTable = hObjectTable ? (EditorObjectTable*)GetMainWorld() -> fn_p_oFindObjectWithEngine( hObjectTable, C_szObjectTableTypeName, p_oFamily ) : NULL;
        bResult &= ( ( *_p_p_oObjectTable != NULL ) && ( (*_p_p_oObjectTable) -> fn_bIsAvailable() ) );
    }
    return bResult;
}


//Stefan Dumitrean 28-05-98
void Tool3D_Interface::GetListsForDialog(CPA_DialogList * pDialog)
{

	CPA_List<CPA_BaseObject> clObjectsList;
	if( GetMainWorld()->fn_lFindObjects( &clObjectsList, "", C_szFamilyTypeName ) )
	{
		POSITION pos;
		m_p_oAnimFrame->m_p_clBaseObjectList->DeleteAllElements();
		pos = clObjectsList.GetHeadPosition();
		while( pos )
			m_p_oAnimFrame->m_p_clBaseObjectList->fn_bAddObject( clObjectsList.GetNext( pos ) );
		m_p_oAnimFrame->m_p_clDialogList->fn_vReinitDialog();
	}
	pDialog->fn_vAddANewList( T3DFamilyName, m_p_oAnimFrame->m_p_clBaseObjectList );
}

void Tool3D_Interface::fn_vInitDefaultParameters(CPA_DialogList *pDialog)
{
//	pDialog->SetDefaultDisplay( m_p_oAnimFrame->m_bDisplayAllFam );
	pDialog->SetDefaultDisplay( FALSE );

	pDialog->SetDefaultButtonState( FALSE );

//	pDialog->SetDefaultTestName( "Show Loaded Families" );
	pDialog->SetDefaultTestName( "" );

	pDialog->SetDefaultTypeName( T3DFamilyName );
}

BOOL Tool3D_Interface::fn_bOnButtonTest(CPA_DialogList *pDialog, CString csTypeName, tdeListDrawMode eDraw )
{
	m_p_oAnimFrame->m_bDisplayAllFam = !m_p_oAnimFrame->m_bDisplayAllFam;
	pDialog->SetDefaultDisplay( m_p_oAnimFrame->m_bDisplayAllFam );
	if( m_p_oAnimFrame->m_bDisplayAllFam )
		pDialog->fn_vInitButtonTest("Show Loaded Families", TRUE, FALSE );
	else
		pDialog->fn_vInitButtonTest("Show All Families", TRUE, FALSE );
	pDialog->fn_vUpdateSelection( E_lrm_ReinitList );
	pDialog->fn_vUpdateSelection( E_lrm_ChangeSelection );
	return TRUE;
}

CPA_BaseObject* Tool3D_Interface::GetDialogSelection (CPA_DialogList *pDialog, CString csTypeName, tdeListDrawMode eDrawMode)
{
	return (CPA_BaseObject*)m_p_CurrentFamily;
}

BOOL Tool3D_Interface::fn_bOnSelChangeListObjects(CPA_DialogList *pDialog, CString csTypeName, CPA_BaseObject *pSelectedObject, BOOL bUnselect)
{
	CPA_List<CPA_BaseObject> clObjectsList;
	CPA_Actor *pInstance = NULL;
	CPA_Actor *p_clActor;
	CPA_Family *pOldFamily = m_p_CurrentFamily;
    CPA_Family *p_oFamily1     = NULL;
    CPA_Animation *p_oAnimation1  = NULL;
	unsigned short uwFrame1       = 0;

	
	if (!bUnselect && pSelectedObject)
	{
		m_p_CurrentFamily = (CPA_Family *) pSelectedObject;
		
		if (GetMainWorld()->fn_lFindObjects(&clObjectsList, "", C_szActorInstanceTypeName))
		{
//find the first instance of family m_p_CurrentFamily
			POSITION pos = clObjectsList.GetHeadPosition();
			while (pos)
			{
				p_clActor = (CPA_Actor*)clObjectsList.GetNext(pos);
				if (p_clActor && p_clActor->GetOwner() && p_clActor->GetOwner()->GetOwner())
				{
					if (p_clActor->GetOwner()->GetOwner()->GetName() == m_p_CurrentFamily->GetName())
					{
						if (mfn_bFindInfoAboutActor(p_clActor->m_pub_fn_pclGetSuperObject(), &p_oFamily1, &p_oAnimation1, NULL, &uwFrame1))
						{
							pInstance = p_clActor;
							m_p_Tool3DWorld->mfn_vResetWorld();
							m_p_Tool3DWorld->mfn_p_oCreateHierarchyFromAnim( p_clActor->m_pub_fn_pclGetSuperObject() );
							break;
						}
					}
				}
			}
		}
		if (!pInstance)
		{
			CPA_DLLBase *pclActorDLL = (CPA_DLLBase *) GetMainWorld()->GetObjectDLLWithName(C_szDLLActorName);
			CPA_SuperObject * pSupObj = (CPA_SuperObject *) pclActorDLL->OnQueryAction(this, C_uiActor_GetAnActorInstance, (LPARAM)m_p_CurrentFamily);
			if (pSupObj && mfn_bFindInfoAboutActor(pSupObj, &p_oFamily1, &p_oAnimation1, NULL, &uwFrame1))
			{
				m_p_Tool3DWorld->mfn_vResetWorld();
				m_p_Tool3DWorld->mfn_p_oCreateHierarchyFromAnim(pSupObj);
			}
			else
			{
				m_p_CurrentFamily = pOldFamily;
				pDialog->fn_vUpdateSelection( E_lrm_ChangeSelection );
				m_p_oAnimView->mfn_vInitDialog( m_p_CurrentFamily );
				return TRUE;
			}
		}
		m_p_oAnimView->mfn_vInitDialog( m_p_CurrentFamily );
	}

	return FALSE;
}

BOOL Tool3D_Interface::fn_bOnDblClkListObjects(CPA_DialogList *pDialog, CString csTypeName, CPA_BaseObject *pSelectedObject)
{
	HCURSOR     hWaitCursor;
	HCURSOR     hOldCursor;
	CPA_Family *p_clFamily = (CPA_Family*)pSelectedObject;

	hWaitCursor = AfxGetApp()->LoadStandardCursor( IDC_WAIT );
	hOldCursor = SetCursor( hWaitCursor );

    if( p_clFamily->GetEditor()->fn_bLoadBaseObject( p_clFamily ) )
		fn_bOnSelChangeListObjects( pDialog, csTypeName, pSelectedObject, FALSE );

	SetCursor( hOldCursor );
	return TRUE;
}


void Tool3D_Interface::fn_vOnActivateEditorAnim()
{
    CPA_Family          *p_oFamily      = NULL;
    CPA_Animation       *p_oAnimation   = NULL;
    CPA_Family          *p_oFamily1     = NULL;
    CPA_Animation       *p_oAnimation1  = NULL;

	CPA_List<CPA_BaseObject> clObjectsList;
	CPA_SuperObject		*p_clSuperObject;

	POSITION pos;

    unsigned short      uwFrame        = 0;
	unsigned short      uwFrame1       = 0;

	GetMainWorld()->fn_lFindObjects( &clObjectsList, "", C_szActorInstanceTypeName );
	pos = clObjectsList.GetHeadPosition();

	while (pos)
	{	
		p_clSuperObject = ((CPA_Actor *)clObjectsList.GetNext(pos))->m_pub_fn_pclGetSuperObject();
		if (mfn_bFindInfoAboutActor( p_clSuperObject, &p_oFamily1, &p_oAnimation1, NULL, &uwFrame1) &&
			p_clSuperObject->GetCount() > 0)
		{
			if (m_p_oSprObjActor)
			{
				if (p_oFamily1->GetName() < p_oFamily->GetName())
				{
					m_p_oSprObjActor = p_clSuperObject;
					p_oFamily = p_oFamily1;
					p_oAnimation = p_oAnimation1;
				}
			}
			else
			{
				m_p_oSprObjActor = p_clSuperObject;
				p_oFamily = p_oFamily1;
				p_oAnimation = p_oAnimation1;
			}
		}
	}

	if (m_p_oSprObjActor)
	{
		if( ! fn_bToolActive(C_wTool3D_Char) )
		{
			OnQueryOpen(NULL,(WPARAM)C_wTool3D_Char,(LPARAM)m_p_oSprObjActor);
		}

		// can open editor
		g_oFrameGest.mfn_vSetRefresh( FALSE );
		g_oFrameGest.mfn_vActivateWindow ( m_p_FRMBase ) ;
		g_oFrameGest.mfn_vActivateWindow( m_p_oAnimFrame );
		g_oFrameGest.mfn_vSetRefresh( TRUE );
		m_p_oAnimView -> mfn_vInitDialog( p_oFamily, p_oAnimation, uwFrame );
//Stefan Dumitrean 27-05-98
		m_p_oAnimView->mfn_vSetFamilySelDialog( m_p_oAnimFrame->m_p_clDialogList );

		m_p_oAnimFrame->m_p_clDialogList->fn_vInitAllLists();
		m_p_oAnimFrame->m_p_clDialogList->fn_vReinitDialog();	
		m_p_CurrentFamily = p_oFamily;
		m_p_oAnimFrame->m_p_clDialogList->fn_vUpdateSelection( E_lrm_ChangeSelection );

		return;
//End Stefan Dumitrean 27-05-98

	}
    // can't open editor
    GetMainWorld() -> fn_bCloseEditor(this);
}

#endif //ACTIVE_EDITOR