tools/tooluserdata/ortooluserdata_tool.cxx

tools/tooluserdata/ortooluserdata_tool.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
//--- Class declarations
#include "ortooluserdata_tool.h"
//--- Registration defines
#define ORTOOLUSERDATA__CLASS ORTOOLUSERDATA__CLASSNAME
#define ORTOOLUSERDATA__LABEL "User Data"
#define ORTOOLUSERDATA__DESC "OR - User Data Tool Description"
//--- FiLMBOX implementation and registration
FBToolImplementation( ORTOOLUSERDATA__CLASS );
FBRegisterTool ( ORTOOLUSERDATA__CLASS,
ORTOOLUSERDATA__LABEL,
ORTOOLUSERDATA__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* Tool creation function.
************************************************/
bool ORToolUserData::FBCreate()
{
StartSize[0] = 400;
StartSize[1] = 280;
// Tool options
mUserId = 99;
UICreate ();
UIConfigure ();
return true;
}
/************************************************
* FiLMBOX Destruction function.
************************************************/
void ORToolUserData::FBDestroy()
{
// If the idle anim callback is still in the system, remove it.
mSystem.OnUIIdle.Remove( this, (FBCallback) &ORToolUserData::EventIdleAnim );
mSystem.OnConnectionStateNotify.Remove( this, (FBCallback) &ORToolUserData::ConnectionStateNotify);
}
/************************************************
* Create the UI (Assign regions & controls).
************************************************/
void ORToolUserData::UICreate()
{
int lS = 4;
// Create regions.
AddRegion("LabelDirections","LabelDirections",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
-lS, kFBAttachRight, "", 1.0,
50, kFBAttachNone, NULL,1.0);
AddRegion("TabPanel","TabPanel",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachBottom,"LabelDirections", 1.0,
-lS, kFBAttachRight, "", 1.0,
25, kFBAttachNone, NULL, 1.0);
AddRegion("Layout", "Layout",
0, kFBAttachLeft, "TabPanel", 1.0,
0, kFBAttachBottom,"TabPanel", 1.0,
0, kFBAttachRight, "TabPanel", 1.0,
-lS, kFBAttachBottom,"", 1.0 );
// Assign regions
SetControl( "LabelDirections", mLabelDirections );
SetControl( "TabPanel", mTabPanel );
SetControl( "Layout", mLayoutNodes );
// Create sub-layouts
UICreateLayoutNodes();
UICreateLayoutRemove();
UICreateLayoutExtras();
}
void ORToolUserData::UICreateLayoutNodes()
{
int lH = 18;
int lS = 4;
// Add regions
mLayoutNodes.AddRegion( "LabelDirectionsNodes", "LabelDirectionsNodes",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
-lS, kFBAttachRight, "", 1.0,
55, kFBAttachNone, NULL, 1.0);
mLayoutNodes.AddRegion( "ButtonAddToSelected", "ButtonAddToSelected",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachBottom, "LabelDirectionsNodes",1.0,
150, kFBAttachNone, NULL, 1.0,
lH, kFBAttachNone, NULL, 1.0);
mLayoutNodes.AddRegion( "LabelId", "LabelId",
20, kFBAttachNone, "", 1.0,
lS, kFBAttachBottom, "ButtonAddToSelected", 1.0,
50, kFBAttachNone, "", 1.0,
lH, kFBAttachNone, "", 1.0);
mLayoutNodes.AddRegion( "LabelName", "LabelName",
lS, kFBAttachRight, "LabelId", 1.0,
0, kFBAttachTop, "LabelId", 1.0,
100, kFBAttachNone, "", 1.0,
0, kFBAttachHeight, "LabelId", 1.0);
mLayoutNodes.AddRegion( "LabelType", "LabelType",
lS, kFBAttachRight, "LabelName", 1.0,
0, kFBAttachTop, "LabelName", 1.0,
0, kFBAttachWidth, "LabelName", 1.0,
0, kFBAttachHeight, "LabelName", 1.0);
mLayoutNodes.AddRegion( "EditNumberUserId1", "EditNumberUserId1",
20, kFBAttachNone, "", 1.0,
lS, kFBAttachBottom, "LabelId", 1.0,
50, kFBAttachNone, "", 1.0,
lH, kFBAttachNone, "", 1.0);
mLayoutNodes.AddRegion( "EditName1", "EditName1",
lS, kFBAttachRight, "EditNumberUserId1", 1.0,
0, kFBAttachTop, "EditNumberUserId1", 1.0,
100, kFBAttachNone, "", 1.0,
0, kFBAttachHeight, "EditNumberUserId1", 1.0);
mLayoutNodes.AddRegion( "ListType1", "ListType1",
lS, kFBAttachRight, "EditName1", 1.0,
0, kFBAttachTop, "EditName1", 1.0,
0, kFBAttachWidth, "EditName1", 1.0,
0, kFBAttachHeight, "EditNumberUserId1", 1.0);
// Assign regions
mLayoutNodes.SetControl( "LabelDirectionsNodes", mLabelDirectionsNodes );
mLayoutNodes.SetControl( "LabelId", mLabelId );
mLayoutNodes.SetControl( "LabelName", mLabelName );
mLayoutNodes.SetControl( "LabelType", mLabelType );
mLayoutNodes.SetControl( "ButtonAddToSelected", mButtonAddToSelected );
mLayoutNodes.SetControl( "EditNumberUserId1", mEditNumberUserId );
mLayoutNodes.SetControl( "EditName1", mEditName );
mLayoutNodes.SetControl( "ListType1", mListType );
}
void ORToolUserData::UICreateLayoutRemove()
{
int lS = 4;
int lH = 23;
// Create regions
mLayoutRemove.AddRegion( "LabelDirectionsRemove", "LabelDirectionsRemove",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
-lS, kFBAttachRight, "", 1.0,
45, kFBAttachNone, NULL, 1.0);
mLayoutRemove.AddRegion( "ContainerModel", "ContainerModel",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachBottom, "LabelDirectionsRemove", 1.0,
150, kFBAttachNone, NULL, 1.0,
lH, kFBAttachNone, NULL, 1.0);
mLayoutRemove.AddRegion( "ButtonRemoveNodes", "ButtonRemoveNodes",
lS, kFBAttachRight, "ContainerModel", 1.0,
0, kFBAttachTop, "ContainerModel", 1.0,
100, kFBAttachNone, NULL, 1.0,
0, kFBAttachHeight, "ContainerModel", 1.0);
mLayoutRemove.AddRegion( "ListUserNodes", "ListUserNodes",
0, kFBAttachLeft, "ContainerModel", 1.0,
lS, kFBAttachBottom, "ContainerModel", 1.0,
0, kFBAttachRight, "ButtonRemoveNodes", 1.0,
-lS, kFBAttachBottom, "", 1.0);
// Assign regions
mLayoutRemove.SetControl( "LabelDirectionsRemove", mLabelDirectionsRemove );
mLayoutRemove.SetControl( "ButtonRemoveNodes", mButtonRemoveNodes );
mLayoutRemove.SetControl( "ContainerModel", mContainerModel );
mLayoutRemove.SetControl( "ListUserNodes", mListUserNodes );
}
void ORToolUserData::UICreateLayoutExtras()
{
int lS=4;
int lH=18;
int lW=150;
// Create regions
mLayoutExtras.AddRegion( "LabelDirectionsExtras", "LabelDirectionsExtras",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
-lS, kFBAttachRight, "", 1.0,
35, kFBAttachNone, NULL, 1.0);
mLayoutExtras.AddRegion( "ButtonJLCooperTest", "ButtonJLCooperTest",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachBottom, "LabelDirectionsExtras", 1.0,
lW, kFBAttachNone, NULL, 1.0,
lH, kFBAttachNone, NULL, 1.0);
mLayoutExtras.AddRegion( "ButtonFBXTest", "ButtonFBXTest",
0, kFBAttachLeft, "ButtonJLCooperTest", 1.0,
lS, kFBAttachBottom, "ButtonJLCooperTest", 1.0,
0, kFBAttachWidth, "ButtonJLCooperTest", 1.0,
0, kFBAttachHeight, "ButtonJLCooperTest", 1.0);
// Assign regions
mLayoutExtras.SetControl( "LabelDirectionsExtras", mLabelDirectionsExtras );
mLayoutExtras.SetControl( "ButtonJLCooperTest", mButtonJLCooperTest );
mLayoutExtras.SetControl( "ButtonFBXTest", mButtonFBXTest );
}
/************************************************
* Configure the UI (setup each item).
************************************************/
void ORToolUserData::UIConfigure()
{
mLabelDirections.Caption = "This tool enables the user to add user-specified connectors to any\n"
"element in the scene. There is also a section for some other sample\n"
"code (loading FBX files, communicating with a JLCooper device, etc..).";
mTabPanel.Items.SetString("Add Custom Properties~Remove Custom Properties~Extras");
mTabPanel.OnChange.Add( this, (FBCallback)&ORToolUserData::EventTabPanelChange );
// Watch for the deletation of the model
mSystem.OnConnectionStateNotify.Add( this, (FBCallback) &ORToolUserData::ConnectionStateNotify);
UIConfigureLayoutNodes();
UIConfigureLayoutRemove();
UIConfigureLayoutExtras();
}
void ORToolUserData::UIConfigureLayoutNodes()
{
mLabelDirectionsNodes.Caption = "1. Select a model in the scene.\n"
"2. Configure the properties to be added.\n"
"3. Click 'add to selected' to add.\n"
"You can see the added properties in a relations constraint";
mButtonAddToSelected.Caption = "Add Properties To Selected";
mLabelId.Caption = "Id";
mLabelName.Caption = "Name";
mLabelType.Caption = "Type";
mLabelId.Justify = kFBTextJustifyCenter;
mLabelName.Justify = kFBTextJustifyCenter;
mLabelType.Justify = kFBTextJustifyCenter;
mEditNumberUserId.Value = 99;
mEditNumberUserId.Precision = 3.0;
mEditNumberUserId.Min = 0;
mEditNumberUserId.Max = 1000;
mEditName.Text = "Custom";
mListType.Items.Add(ANIMATIONNODE_TYPE_ACTION, kFBPT_double);
mListType.Items.Add(ANIMATIONNODE_TYPE_BOOL, kFBPT_double);
mListType.Items.Add(ANIMATIONNODE_TYPE_NUMBER, kFBPT_double);
// Add callbacks
mButtonAddToSelected.OnClick.Add ( this, (FBCallback) &ORToolUserData::EventButtonAddToSelected );
}
void ORToolUserData::UIConfigureLayoutRemove()
{
mLabelDirectionsRemove.Caption = "1. Drag a model into the container\n"
"2. Select the properties to remove\n"
"3. Double-click the container to unselect";
mButtonRemoveNodes.Caption = "Remove Properties";
mContainerModel.IconPosition = kFBIconLeft;
mListUserNodes.Style = kFBVerticalList;
mListUserNodes.MultiSelect = true;
mButtonRemoveNodes.OnClick.Add ( this, (FBCallback) &ORToolUserData::EventButtonRemoveSelectedClick );
mContainerModel.OnDragAndDrop.Add ( this, (FBCallback) &ORToolUserData::EventContainerModelDragAndDrop );
mContainerModel.OnDblClick.Add ( this, (FBCallback) &ORToolUserData::EventContainerModelDblClick );
}
void ORToolUserData::UIConfigureLayoutExtras()
{
mLabelDirectionsExtras.Caption = "1. Click on JLCooperTest to send a message to a loaded JLCooper console\n"
"2. Click on FBXTest to open a file using the FBFbxManager class";
mButtonJLCooperTest.Caption = "Send JLCooper Message";
mButtonJLCooperTest.OnClick.Add ( this, (FBCallback) &ORToolUserData::EventButtonSendMessage );
mButtonFBXTest.Caption = "FBXTest";
mButtonFBXTest.OnClick.Add ( this, (FBCallback) &ORToolUserData::EventButtonFBXTest );
}
void ORToolUserData::UIReset()
{
}
/************************************************
* FBX Test button callback.
************************************************/
void ORToolUserData::EventButtonFBXTest( HISender pSender, HKEvent pEvent )
{
FBFilePopup lPopup;
lPopup.Caption = "FBX file to load";
lPopup.Style = kFBFilePopupOpen;
// We suggest the SDK's scenes.
FBString lScenesPath( mSystem.ApplicationPath );
lScenesPath += "\\..\\OpenRealitySDK\\Scenes";
lPopup.Path = (char*)lScenesPath;
lPopup.Filter = "*.fbx";
if( lPopup.Execute() )
{
FBApplication::TheOne().FileMerge(lPopup.FullFilename);
}
}
/************************************************
* Send message to JLCooper.
************************************************/
void ORToolUserData::EventButtonSendMessage( HISender pSender, HKEvent pEvent )
{
static bool lOn = false;
FBJLCooper lJLCooper;
if( ! lOn )
{
lOn = true;
lJLCooper.LCDClearScreen();
mSystem.OnUIIdle.Add( this, (FBCallback)&ORToolUserData::EventIdleAnim );
}
else
{
lOn = false;
mSystem.OnUIIdle.Remove( this, (FBCallback)&ORToolUserData::EventIdleAnim );
lJLCooper.LCDClearScreen();
}
}
/************************************************
* Send message to JLCooper.
************************************************/
void ORToolUserData::EventIdleAnim( HISender pSender, HKEvent pEvent )
{
FBJLCooper lJLCooper;
FBTime lTime;
lTime = mSystem.LocalTime;
lJLCooper.LCDSendString( lTime.GetTimeString() );
lJLCooper.TCSetTime( lTime );
}
/************************************************
* Remove selected nodes from model.
************************************************/
void ORToolUserData::EventButtonRemoveSelectedClick( HISender pSender, HKEvent pEvent )
{
FBProperty* lProperty;
int i;
if( mHdlModel.Ok())
{
for( i=0; i< mListUserNodes.Items.GetCount(); i++ )
{
if( mListUserNodes.IsSelected( i ) )
{
lProperty = (FBProperty*) mListUserNodes.Items.GetReferenceAt( i );
mHdlModel->PropertyRemove( lProperty );
}
}
RefreshRemoveNodes();
}
}
/************************************************
* Refresh the model's user nodes.
************************************************/
void ORToolUserData::RefreshRemoveNodes()
{
mContainerModel.Items.Clear();
mListUserNodes.Items.Clear();
if( !mHdlModel.Ok())
{
return;
}
int i;
mContainerModel.Items.Add( mHdlModel->Name, (kReference)mHdlModel() );
for( i=0; i< mHdlModel->PropertyList.GetCount(); i++ )
{
FBProperty* Property = mHdlModel->PropertyList[i];
if( Property->IsUserProperty() )
{
mListUserNodes.Items.Add( Property->GetName(), (kReference) Property );
}
}
}
/************************************************
* Drag & Drop.
************************************************/
void ORToolUserData::EventContainerModelDragAndDrop( HISender pSender, HKEvent pEvent )
{
FBEventDragAndDrop lDragAndDrop( pEvent );
switch( lDragAndDrop.State )
{
{
lDragAndDrop.Accept();
}
break;
{
mHdlModel = (FBModel*) lDragAndDrop.Get(0);
RefreshRemoveNodes();
}
break;
}
}
/************************************************
* UI Callback to add the nodes to selected models.
************************************************/
void ORToolUserData::EventContainerModelDblClick( HISender pSender, HKEvent pEvent )
{
mContainerModel.Items.Clear();
mListUserNodes.Items.Clear();
mHdlModel=NULL;
}
/************************************************
* UI Callback to add the nodes to selected models.
************************************************/
void ORToolUserData::EventButtonAddToSelected( HISender pSender, HKEvent pEvent )
{
FBModel* lRoot = mSystem.SceneRootModel;
mUserId = mEditNumberUserId.Value;
mName = mEditName.Text;
mDataType = mListType.Items[ mListType.ItemIndex ];
mType = (FBPropertyType)mListType.Items.GetReferenceAt( mListType.ItemIndex );
AddNodesToModel( lRoot );
RefreshRemoveNodes();
}
/************************************************
* Tab panel change callback.
************************************************/
void ORToolUserData::EventTabPanelChange( HISender pSender, HKEvent pEvent )
{
switch( mTabPanel.ItemIndex )
{
case 0: SetControl("Layout", mLayoutNodes ); break;
case 1: SetControl("Layout", mLayoutRemove ); break;
case 2: SetControl("Layout", mLayoutExtras ); break;
}
}
/************************************************
* Recursive node adding function.
************************************************/
void ORToolUserData::AddNodesToModel( FBModel* pModel, bool pRecursive, bool pSelected )
{
int i;
if( ! pSelected || ((bool)pModel->Selected) )
{
FBProperty* lProperty = pModel->PropertyCreate( mName, mType,mDataType, true, true );
if (lProperty->IsAnimatable())
{
FBPropertyAnimatable* lPropertyAnimatable = (FBPropertyAnimatable*)lProperty;
lPropertyAnimatable->SetAnimated( true );
FBAnimationNode* lAnimationNode = lPropertyAnimatable->GetAnimationNode();
if (lAnimationNode)
{
lAnimationNode->Reference = mUserId;
}
}
}
if( pRecursive )
{
for( i=0; i<pModel->Children.GetCount(); i++)
{
AddNodesToModel( pModel->Children[i], pRecursive, pSelected );
}
}
}
void ORToolUserData::ConnectionStateNotify( HISender /*pSender*/, HKEvent pEvent )
{
FBEventConnectionStateNotify lEvent(pEvent);
if( lEvent.Action == kFBDestroy )
{
FBPlug* lPlug = lEvent.Plug;
FBModel* lModel = (FBModel*)lPlug;
if( lModel == mHdlModel )
{
mHdlModel=NULL;
mContainerModel.Items.Clear();
mListUserNodes.Items.Clear();
}
}
}