guiObjectView.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/guiObjectView.h"
  23. #include "renderInstance/renderPassManager.h"
  24. #include "lighting/lightManager.h"
  25. #include "lighting/lightInfo.h"
  26. #include "core/resourceManager.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "console/consoleTypes.h"
  30. #include "math/mathTypes.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "console/engineAPI.h"
  33. IMPLEMENT_CONOBJECT( GuiObjectView );
  34. ConsoleDocClass( GuiObjectView,
  35. "@brief GUI control which displays a 3D model.\n\n"
  36. "Model displayed in the control can have other objects mounted onto it, and the light settings can be adjusted.\n\n"
  37. "@tsexample\n"
  38. " new GuiObjectView(ObjectPreview)\n"
  39. " {\n"
  40. " shapeFile = \"art/shapes/items/kit/healthkit.dts\";\n"
  41. " mountedNode = \"mount0\";\n"
  42. " lightColor = \"1 1 1 1\";\n"
  43. " lightAmbient = \"0.5 0.5 0.5 1\";\n"
  44. " lightDirection = \"0 0.707 -0.707\";\n"
  45. " orbitDiststance = \"2\";\n"
  46. " minOrbitDiststance = \"0.917688\";\n"
  47. " maxOrbitDiststance = \"5\";\n"
  48. " cameraSpeed = \"0.01\";\n"
  49. " cameraZRot = \"0\";\n"
  50. " forceFOV = \"0\";\n"
  51. " reflectPriority = \"0\";\n"
  52. " };\n"
  53. "@endtsexample\n\n"
  54. "@see GuiControl\n\n"
  55. "@ingroup Gui3D\n"
  56. );
  57. IMPLEMENT_CALLBACK( GuiObjectView, onMouseEnter, void, (),(),
  58. "@brief Called whenever the mouse enters the control.\n\n"
  59. "@tsexample\n"
  60. "// The mouse has entered the control, causing the callback to occur\n"
  61. "GuiObjectView::onMouseEnter(%this)\n"
  62. " {\n"
  63. " // Code to run when the mouse enters this control\n"
  64. " }\n"
  65. "@endtsexample\n\n"
  66. "@see GuiControl\n\n"
  67. );
  68. IMPLEMENT_CALLBACK( GuiObjectView, onMouseLeave, void, (),(),
  69. "@brief Called whenever the mouse leaves the control.\n\n"
  70. "@tsexample\n"
  71. "// The mouse has left the control, causing the callback to occur\n"
  72. "GuiObjectView::onMouseLeave(%this)\n"
  73. " {\n"
  74. " // Code to run when the mouse leaves this control\n"
  75. " }\n"
  76. "@endtsexample\n\n"
  77. "@see GuiControl\n\n"
  78. );
  79. //------------------------------------------------------------------------------
  80. GuiObjectView::GuiObjectView()
  81. : mMaxOrbitDist( 5.0f ),
  82. mMinOrbitDist( 0.0f ),
  83. mOrbitDist( 5.0f ),
  84. mMouseState( None ),
  85. mModel( NULL ),
  86. mMountedModel( NULL ),
  87. mLastMousePoint( 0, 0 ),
  88. mLastRenderTime( 0 ),
  89. mRunThread( NULL ),
  90. mLight( NULL ),
  91. mAnimationSeq( -1 ),
  92. mMountNodeName( "mount0" ),
  93. mMountNode( -1 ),
  94. mCameraSpeed( 0.01f ),
  95. mLightColor( 1.0f, 1.0f, 1.0f ),
  96. mLightAmbient( 0.5f, 0.5f, 0.5f ),
  97. mLightDirection( 0.f, 0.707f, -0.707f )
  98. {
  99. mCameraMatrix.identity();
  100. mCameraRot.set( 0.0f, 0.0f, 3.9f );
  101. mCameraPos.set( 0.0f, 1.75f, 1.25f );
  102. mCameraMatrix.setColumn( 3, mCameraPos );
  103. mOrbitPos.set( 0.0f, 0.0f, 0.0f );
  104. // By default don't do dynamic reflection
  105. // updates for this viewport.
  106. mReflectPriority = 0.0f;
  107. }
  108. //------------------------------------------------------------------------------
  109. GuiObjectView::~GuiObjectView()
  110. {
  111. if( mModel )
  112. SAFE_DELETE( mModel );
  113. if( mMountedModel )
  114. SAFE_DELETE( mMountedModel );
  115. if( mLight )
  116. SAFE_DELETE( mLight );
  117. }
  118. //------------------------------------------------------------------------------
  119. void GuiObjectView::initPersistFields()
  120. {
  121. addGroup( "Model" );
  122. addField( "shapeFile", TypeStringFilename, Offset( mModelName, GuiObjectView ),
  123. "The object model shape file to show in the view." );
  124. addField( "skin", TypeRealString, Offset( mSkinName, GuiObjectView ),
  125. "The skin to use on the object model." );
  126. endGroup( "Model" );
  127. addGroup( "Animation" );
  128. addField( "animSequence", TypeRealString, Offset( mAnimationSeqName, GuiObjectView ),
  129. "The animation sequence to play on the model." );
  130. endGroup( "Animation" );
  131. addGroup( "Mounting" );
  132. addField( "mountedShapeFile", TypeStringFilename, Offset( mMountedModelName, GuiObjectView ),
  133. "Optional shape file to mount on the primary model (e.g. weapon)." );
  134. addField( "mountedSkin", TypeRealString, Offset( mMountSkinName, GuiObjectView ),
  135. "Skin name used on mounted shape file." );
  136. addField( "mountedNode", TypeRealString, Offset( mMountNodeName, GuiObjectView ),
  137. "Name of node on primary model to which to mount the secondary shape." );
  138. endGroup( "Mounting" );
  139. addGroup( "Lighting" );
  140. addField( "lightColor", TypeColorF, Offset( mLightColor, GuiObjectView ),
  141. "Diffuse color of the sunlight used to render the model." );
  142. addField( "lightAmbient", TypeColorF, Offset( mLightAmbient, GuiObjectView ),
  143. "Ambient color of the sunlight used to render the model." );
  144. addField( "lightDirection", TypePoint3F, Offset( mLightDirection, GuiObjectView ),
  145. "Direction from which the model is illuminated." );
  146. endGroup( "Lighting" );
  147. addGroup( "Camera" );
  148. addField( "orbitDiststance", TypeF32, Offset( mOrbitDist, GuiObjectView ),
  149. "Distance from which to render the model." );
  150. addField( "minOrbitDiststance", TypeF32, Offset( mMinOrbitDist, GuiObjectView ),
  151. "Maxiumum distance to which the camera can be zoomed out." );
  152. addField( "maxOrbitDiststance", TypeF32, Offset( mMaxOrbitDist, GuiObjectView ),
  153. "Minimum distance below which the camera will not zoom in further." );
  154. addField( "cameraSpeed", TypeF32, Offset( mCameraSpeed, GuiObjectView ),
  155. "Multiplier for mouse camera operations." );
  156. addField( "cameraRotation", TypePoint3F, Offset( mCameraRotation, GuiObjectView ),
  157. "Set the camera rotation." );
  158. endGroup( "Camera" );
  159. Parent::initPersistFields();
  160. }
  161. //------------------------------------------------------------------------------
  162. void GuiObjectView::onStaticModified( StringTableEntry slotName, const char* newValue )
  163. {
  164. Parent::onStaticModified( slotName, newValue );
  165. static StringTableEntry sShapeFile = StringTable->insert( "shapeFile" );
  166. static StringTableEntry sSkin = StringTable->insert( "skin" );
  167. static StringTableEntry sMountedShapeFile = StringTable->insert( "mountedShapeFile" );
  168. static StringTableEntry sMountedSkin = StringTable->insert( "mountedSkin" );
  169. static StringTableEntry sMountedNode = StringTable->insert( "mountedNode" );
  170. static StringTableEntry sLightColor = StringTable->insert( "lightColor" );
  171. static StringTableEntry sLightAmbient = StringTable->insert( "lightAmbient" );
  172. static StringTableEntry sLightDirection = StringTable->insert( "lightDirection" );
  173. static StringTableEntry sOrbitDistance = StringTable->insert( "orbitDistance" );
  174. static StringTableEntry sMinOrbitDistance = StringTable->insert( "minOrbitDistance" );
  175. static StringTableEntry sMaxOrbitDistance = StringTable->insert( "maxOrbitDistance" );
  176. static StringTableEntry sCameraRotation = StringTable->insert( "cameraRotation" );
  177. static StringTableEntry sAnimSequence = StringTable->insert( "animSequence" );
  178. if( slotName == sShapeFile )
  179. setObjectModel( String( mModelName ) );
  180. else if( slotName == sSkin )
  181. setSkin( String( mSkinName ) );
  182. else if( slotName == sMountedShapeFile )
  183. setMountedObject( String( mMountedModelName ) );
  184. else if( slotName == sMountedSkin )
  185. setMountSkin( String( mMountSkinName ) );
  186. else if( slotName == sMountedNode )
  187. setMountNode( String( mMountNodeName ) );
  188. else if( slotName == sLightColor )
  189. setLightColor( mLightColor );
  190. else if( slotName == sLightAmbient )
  191. setLightAmbient( mLightAmbient );
  192. else if( slotName == sLightDirection )
  193. setLightDirection( mLightDirection );
  194. else if( slotName == sOrbitDistance || slotName == sMinOrbitDistance || slotName == sMaxOrbitDistance )
  195. setOrbitDistance( mOrbitDist );
  196. else if( slotName == sCameraRotation )
  197. setCameraRotation( mCameraRotation );
  198. else if( slotName == sAnimSequence )
  199. setObjectAnimation( String( mAnimationSeqName ) );
  200. }
  201. //------------------------------------------------------------------------------
  202. bool GuiObjectView::onWake()
  203. {
  204. if( !Parent::onWake() )
  205. return false;
  206. if( !mLight )
  207. {
  208. mLight = LIGHTMGR->createLightInfo();
  209. mLight->setColor( mLightColor );
  210. mLight->setAmbient( mLightAmbient );
  211. mLight->setDirection( mLightDirection );
  212. }
  213. return true;
  214. }
  215. //------------------------------------------------------------------------------
  216. void GuiObjectView::onMouseDown( const GuiEvent &event )
  217. {
  218. if( !mActive || !mVisible || !mAwake )
  219. return;
  220. mMouseState = Rotating;
  221. mLastMousePoint = event.mousePoint;
  222. mouseLock();
  223. }
  224. //------------------------------------------------------------------------------
  225. void GuiObjectView::onMouseUp( const GuiEvent &event )
  226. {
  227. mouseUnlock();
  228. mMouseState = None;
  229. }
  230. //------------------------------------------------------------------------------
  231. void GuiObjectView::onMouseDragged( const GuiEvent &event )
  232. {
  233. if( mMouseState != Rotating )
  234. return;
  235. Point2I delta = event.mousePoint - mLastMousePoint;
  236. mLastMousePoint = event.mousePoint;
  237. mCameraRot.x += ( delta.y * mCameraSpeed );
  238. mCameraRot.z += ( delta.x * mCameraSpeed );
  239. }
  240. //------------------------------------------------------------------------------
  241. void GuiObjectView::onRightMouseDown( const GuiEvent &event )
  242. {
  243. mMouseState = Zooming;
  244. mLastMousePoint = event.mousePoint;
  245. mouseLock();
  246. }
  247. //------------------------------------------------------------------------------
  248. void GuiObjectView::onRightMouseUp( const GuiEvent &event )
  249. {
  250. mouseUnlock();
  251. mMouseState = None;
  252. }
  253. //------------------------------------------------------------------------------
  254. void GuiObjectView::onRightMouseDragged( const GuiEvent &event )
  255. {
  256. if( mMouseState != Zooming )
  257. return;
  258. S32 delta = event.mousePoint.y - mLastMousePoint.y;
  259. mLastMousePoint = event.mousePoint;
  260. mOrbitDist += ( delta * mCameraSpeed );
  261. }
  262. //------------------------------------------------------------------------------
  263. void GuiObjectView::setObjectAnimation( S32 index )
  264. {
  265. mAnimationSeq = index;
  266. mAnimationSeqName = String();
  267. if( mModel )
  268. _initAnimation();
  269. }
  270. //------------------------------------------------------------------------------
  271. void GuiObjectView::setObjectAnimation( const String& sequenceName )
  272. {
  273. mAnimationSeq = -1;
  274. mAnimationSeqName = sequenceName;
  275. if( mModel )
  276. _initAnimation();
  277. }
  278. //------------------------------------------------------------------------------
  279. void GuiObjectView::setObjectModel( const String& modelName )
  280. {
  281. SAFE_DELETE( mModel );
  282. mRunThread = 0;
  283. mModelName = String::EmptyString;
  284. // Load the shape.
  285. Resource< TSShape > model = ResourceManager::get().load( modelName );
  286. if( !model )
  287. {
  288. Con::warnf( "GuiObjectView::setObjectModel - Failed to load model '%s'", modelName.c_str() );
  289. return;
  290. }
  291. // Instantiate it.
  292. mModel = new TSShapeInstance( model, true );
  293. mModelName = modelName;
  294. if( !mSkinName.isEmpty() )
  295. mModel->reSkin( mSkinName );
  296. // Initialize camera values.
  297. mOrbitPos = mModel->getShape()->center;
  298. mMinOrbitDist = mModel->getShape()->radius;
  299. // Initialize animation.
  300. _initAnimation();
  301. _initMount();
  302. }
  303. //------------------------------------------------------------------------------
  304. void GuiObjectView::setSkin( const String& name )
  305. {
  306. if( mModel )
  307. mModel->reSkin( name, mSkinName );
  308. mSkinName = name;
  309. }
  310. //------------------------------------------------------------------------------
  311. void GuiObjectView::setMountSkin( const String& name )
  312. {
  313. if( mMountedModel )
  314. mMountedModel->reSkin( name, mMountSkinName );
  315. mMountSkinName = name;
  316. }
  317. //------------------------------------------------------------------------------
  318. void GuiObjectView::setMountNode( S32 index )
  319. {
  320. setMountNode( String::ToString( "mount%i", index ) );
  321. }
  322. //------------------------------------------------------------------------------
  323. void GuiObjectView::setMountNode( const String& name )
  324. {
  325. mMountNodeName = name;
  326. if( mModel )
  327. _initMount();
  328. }
  329. //------------------------------------------------------------------------------
  330. void GuiObjectView::setMountedObject( const String& modelName )
  331. {
  332. SAFE_DELETE( mMountedModel );
  333. mMountedModelName = String::EmptyString;
  334. // Load the model.
  335. Resource< TSShape > model = ResourceManager::get().load( modelName );
  336. if( !model )
  337. {
  338. Con::warnf( "GuiObjectView::setMountedObject - Failed to load object model '%s'",
  339. modelName.c_str() );
  340. return;
  341. }
  342. mMountedModel = new TSShapeInstance( model, true );
  343. mMountedModelName = modelName;
  344. if( !mMountSkinName.isEmpty() )
  345. mMountedModel->reSkin( mMountSkinName );
  346. if( mModel )
  347. _initMount();
  348. }
  349. //------------------------------------------------------------------------------
  350. bool GuiObjectView::processCameraQuery( CameraQuery* query )
  351. {
  352. // Adjust the camera so that we are still facing the model.
  353. Point3F vec;
  354. MatrixF xRot, zRot;
  355. xRot.set( EulerF( mCameraRot.x, 0.0f, 0.0f ) );
  356. zRot.set( EulerF( 0.0f, 0.0f, mCameraRot.z ) );
  357. mCameraMatrix.mul( zRot, xRot );
  358. mCameraMatrix.getColumn( 1, &vec );
  359. vec *= mOrbitDist;
  360. mCameraPos = mOrbitPos - vec;
  361. query->farPlane = 2100.0f;
  362. query->nearPlane = query->farPlane / 5000.0f;
  363. query->fov = 45.0f;
  364. mCameraMatrix.setColumn( 3, mCameraPos );
  365. query->cameraMatrix = mCameraMatrix;
  366. return true;
  367. }
  368. //------------------------------------------------------------------------------
  369. void GuiObjectView::onMouseEnter( const GuiEvent & event )
  370. {
  371. onMouseEnter_callback();
  372. }
  373. //------------------------------------------------------------------------------
  374. void GuiObjectView::onMouseLeave( const GuiEvent & event )
  375. {
  376. onMouseLeave_callback();
  377. }
  378. //------------------------------------------------------------------------------
  379. void GuiObjectView::renderWorld( const RectI& updateRect )
  380. {
  381. if( !mModel )
  382. return;
  383. GFXTransformSaver _saveTransforms;
  384. // Determine the camera position, and store off render state.
  385. MatrixF modelview;
  386. MatrixF mv;
  387. Point3F cp;
  388. modelview = GFX->getWorldMatrix();
  389. mv = modelview;
  390. mv.inverse();
  391. mv.getColumn( 3, &cp );
  392. RenderPassManager* renderPass = gClientSceneGraph->getDefaultRenderPass();
  393. S32 time = Platform::getVirtualMilliseconds();
  394. S32 dt = time - mLastRenderTime;
  395. mLastRenderTime = time;
  396. LIGHTMGR->unregisterAllLights();
  397. LIGHTMGR->setSpecialLight( LightManager::slSunLightType, mLight );
  398. GFX->setStateBlock( mDefaultGuiSB );
  399. F32 left, right, top, bottom, nearPlane, farPlane;
  400. bool isOrtho;
  401. GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
  402. Frustum frust( false, left, right, top, bottom, nearPlane, farPlane, MatrixF::Identity );
  403. SceneRenderState state
  404. (
  405. gClientSceneGraph,
  406. SPT_Diffuse,
  407. SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
  408. renderPass,
  409. false
  410. );
  411. // Set up our TS render state here.
  412. TSRenderState rdata;
  413. rdata.setSceneState( &state );
  414. // We might have some forward lit materials
  415. // so pass down a query to gather lights.
  416. LightQuery query;
  417. query.init( SphereF( Point3F::Zero, 1.0f ) );
  418. rdata.setLightQuery( &query );
  419. // Render primary model.
  420. if( mModel )
  421. {
  422. if( mRunThread )
  423. {
  424. mModel->advanceTime( dt / 1000.f, mRunThread );
  425. mModel->animate();
  426. }
  427. mModel->render( rdata );
  428. }
  429. // Render mounted model.
  430. if( mMountedModel && mMountNode != -1 )
  431. {
  432. GFX->pushWorldMatrix();
  433. GFX->multWorld( mModel->mNodeTransforms[ mMountNode ] );
  434. GFX->multWorld( mMountTransform );
  435. mMountedModel->render( rdata );
  436. GFX->popWorldMatrix();
  437. }
  438. renderPass->renderPass( &state );
  439. // Make sure to remove our fake sun.
  440. LIGHTMGR->unregisterAllLights();
  441. }
  442. //------------------------------------------------------------------------------
  443. void GuiObjectView::setOrbitDistance( F32 distance )
  444. {
  445. // Make sure the orbit distance is within the acceptable range
  446. mOrbitDist = mClampF( distance, mMinOrbitDist, mMaxOrbitDist );
  447. }
  448. //------------------------------------------------------------------------------
  449. void GuiObjectView::setCameraSpeed( F32 factor )
  450. {
  451. mCameraSpeed = factor;
  452. }
  453. //------------------------------------------------------------------------------
  454. void GuiObjectView::setCameraRotation( const EulerF& rotation )
  455. {
  456. mCameraRot.set(rotation);
  457. }
  458. //------------------------------------------------------------------------------
  459. void GuiObjectView::setLightColor( const ColorF& color )
  460. {
  461. mLightColor = color;
  462. if( mLight )
  463. mLight->setColor( color );
  464. }
  465. //------------------------------------------------------------------------------
  466. void GuiObjectView::setLightAmbient( const ColorF& color )
  467. {
  468. mLightAmbient = color;
  469. if( mLight )
  470. mLight->setAmbient( color );
  471. }
  472. //------------------------------------------------------------------------------
  473. void GuiObjectView::setLightDirection( const Point3F& direction )
  474. {
  475. mLightDirection = direction;
  476. if( mLight )
  477. mLight->setDirection( direction );
  478. }
  479. //------------------------------------------------------------------------------
  480. void GuiObjectView::_initAnimation()
  481. {
  482. AssertFatal( mModel, "GuiObjectView::_initAnimation - No model loaded!" );
  483. if( mAnimationSeqName.isEmpty() && mAnimationSeq == -1 )
  484. return;
  485. // Look up sequence by name.
  486. if( !mAnimationSeqName.isEmpty() )
  487. {
  488. mAnimationSeq = mModel->getShape()->findSequence( mAnimationSeqName );
  489. if( mAnimationSeq == -1 )
  490. {
  491. Con::errorf( "GuiObjectView::_initAnimation - Cannot find animation sequence '%s' on '%s'",
  492. mAnimationSeqName.c_str(),
  493. mModelName.c_str()
  494. );
  495. return;
  496. }
  497. }
  498. // Start sequence.
  499. if( mAnimationSeq != -1 )
  500. {
  501. if( mAnimationSeq >= mModel->getShape()->sequences.size() )
  502. {
  503. Con::errorf( "GuiObjectView::_initAnimation - Sequence '%i' out of range for model '%s'",
  504. mAnimationSeq,
  505. mModelName.c_str()
  506. );
  507. mAnimationSeq = -1;
  508. return;
  509. }
  510. if( !mRunThread )
  511. mRunThread = mModel->addThread();
  512. mModel->setSequence( mRunThread, mAnimationSeq, 0.f );
  513. }
  514. mLastRenderTime = Platform::getVirtualMilliseconds();
  515. }
  516. //------------------------------------------------------------------------------
  517. void GuiObjectView::_initMount()
  518. {
  519. AssertFatal( mModel, "GuiObjectView::_initMount - No model loaded!" );
  520. if( !mMountedModel )
  521. return;
  522. mMountTransform.identity();
  523. // Look up the node to which to mount to.
  524. if( !mMountNodeName.isEmpty() )
  525. {
  526. mMountNode = mModel->getShape()->findNode( mMountNodeName );
  527. if( mMountNode == -1 )
  528. {
  529. Con::errorf( "GuiObjectView::_initMount - No node '%s' on '%s'",
  530. mMountNodeName.c_str(),
  531. mModelName.c_str()
  532. );
  533. return;
  534. }
  535. }
  536. // Make sure mount node is valid.
  537. if( mMountNode != -1 && mMountNode >= mModel->getShape()->nodes.size() )
  538. {
  539. Con::errorf( "GuiObjectView::_initMount - Mount node index '%i' out of range for '%s'",
  540. mMountNode,
  541. mModelName.c_str()
  542. );
  543. mMountNode = -1;
  544. return;
  545. }
  546. // Look up node on the mounted model from
  547. // which to mount to the primary model's node.
  548. S32 mountPoint = mMountedModel->getShape()->findNode( "mountPoint" );
  549. if( mountPoint != -1 )
  550. {
  551. mMountedModel->getShape()->getNodeWorldTransform( mountPoint, &mMountTransform ),
  552. mMountTransform.inverse();
  553. }
  554. }
  555. //=============================================================================
  556. // Console Methods.
  557. //=============================================================================
  558. // MARK: ---- Console Methods ----
  559. //-----------------------------------------------------------------------------
  560. DefineEngineMethod( GuiObjectView, getModel, const char*, (),,
  561. "@brief Return the model displayed in this view.\n\n"
  562. "@tsexample\n"
  563. "// Request the displayed model name from the GuiObjectView object.\n"
  564. "%modelName = %thisGuiObjectView.getModel();\n"
  565. "@endtsexample\n\n"
  566. "@return Name of the displayed model.\n\n"
  567. "@see GuiControl")
  568. {
  569. return Con::getReturnBuffer( object->getModelName() );
  570. }
  571. //-----------------------------------------------------------------------------
  572. DefineEngineMethod( GuiObjectView, setModel, void, (const char* shapeName),,
  573. "@brief Sets the model to be displayed in this control.\n\n"
  574. "@param shapeName Name of the model to display.\n"
  575. "@tsexample\n"
  576. "// Define the model we want to display\n"
  577. "%shapeName = \"gideon.dts\";\n\n"
  578. "// Tell the GuiObjectView object to display the defined model\n"
  579. "%thisGuiObjectView.setModel(%shapeName);\n"
  580. "@endtsexample\n\n"
  581. "@see GuiControl")
  582. {
  583. object->setObjectModel( shapeName );
  584. }
  585. //-----------------------------------------------------------------------------
  586. DefineEngineMethod( GuiObjectView, getMountedModel, const char*, (),,
  587. "@brief Return the name of the mounted model.\n\n"
  588. "@tsexample\n"
  589. "// Request the name of the mounted model from the GuiObjectView object\n"
  590. "%mountedModelName = %thisGuiObjectView.getMountedModel();\n"
  591. "@endtsexample\n\n"
  592. "@return Name of the mounted model.\n\n"
  593. "@see GuiControl")
  594. {
  595. return Con::getReturnBuffer( object->getMountedModelName() );
  596. }
  597. //-----------------------------------------------------------------------------
  598. DefineEngineMethod( GuiObjectView, setMountedModel, void, (const char* shapeName),,
  599. "@brief Sets the model to be mounted on the primary model.\n\n"
  600. "@param shapeName Name of the model to mount.\n"
  601. "@tsexample\n"
  602. "// Define the model name to mount\n"
  603. "%modelToMount = \"GideonGlasses.dts\";\n\n"
  604. "// Inform the GuiObjectView object to mount the defined model to the existing model in the control\n"
  605. "%thisGuiObjectView.setMountedModel(%modelToMount);\n"
  606. "@endtsexample\n\n"
  607. "@see GuiControl")
  608. {
  609. object->setObjectModel(shapeName);
  610. }
  611. //-----------------------------------------------------------------------------
  612. DefineEngineMethod( GuiObjectView, getSkin, const char*, (),,
  613. "@brief Return the name of skin used on the primary model.\n\n"
  614. "@tsexample\n"
  615. "// Request the name of the skin used on the primary model in the control\n"
  616. "%skinName = %thisGuiObjectView.getSkin();\n"
  617. "@endtsexample\n\n"
  618. "@return Name of the skin used on the primary model.\n\n"
  619. "@see GuiControl")
  620. {
  621. return Con::getReturnBuffer( object->getSkin() );
  622. }
  623. //-----------------------------------------------------------------------------
  624. DefineEngineMethod( GuiObjectView, setSkin, void, (const char* skinName),,
  625. "@brief Sets the skin to use on the model being displayed.\n\n"
  626. "@param skinName Name of the skin to use.\n"
  627. "@tsexample\n"
  628. "// Define the skin we want to apply to the main model in the control\n"
  629. "%skinName = \"disco_gideon\";\n\n"
  630. "// Inform the GuiObjectView control to update the skin the to defined skin\n"
  631. "%thisGuiObjectView.setSkin(%skinName);\n"
  632. "@endtsexample\n\n"
  633. "@see GuiControl")
  634. {
  635. object->setSkin( skinName );
  636. }
  637. //-----------------------------------------------------------------------------
  638. DefineEngineMethod( GuiObjectView, getMountSkin, const char*, ( S32 param1, S32 param2),,
  639. "@brief Return the name of skin used on the mounted model.\n\n"
  640. "@tsexample\n"
  641. "// Request the skin name from the model mounted on to the main model in the control\n"
  642. "%mountModelSkin = %thisGuiObjectView.getMountSkin();\n"
  643. "@endtsexample\n\n"
  644. "@return Name of the skin used on the mounted model.\n\n"
  645. "@see GuiControl")
  646. {
  647. return Con::getReturnBuffer( object->getMountSkin() );
  648. }
  649. //-----------------------------------------------------------------------------
  650. DefineEngineMethod( GuiObjectView, setMountSkin, void, (const char* skinName),,
  651. "@brief Sets the skin to use on the mounted model.\n\n"
  652. "@param skinName Name of the skin to set on the model mounted to the main model in the control\n"
  653. "@tsexample\n"
  654. "// Define the name of the skin\n"
  655. "%skinName = \"BronzeGlasses\";\n\n"
  656. "// Inform the GuiObjectView Control of the skin to use on the mounted model\n"
  657. "%thisGuiObjectViewCtrl.setMountSkin(%skinName);\n"
  658. "@endtsexample\n\n"
  659. "@see GuiControl")
  660. {
  661. object->setMountSkin(skinName);
  662. }
  663. //-----------------------------------------------------------------------------
  664. DefineEngineMethod( GuiObjectView, setSeq, void, (const char* indexOrName),,
  665. "@brief Sets the animation to play for the viewed object.\n\n"
  666. "@param indexOrName The index or name of the animation to play.\n"
  667. "@tsexample\n"
  668. "// Set the animation index value, or animation sequence name.\n"
  669. "%indexVal = \"3\";\n"
  670. "//OR:\n"
  671. "%indexVal = \"idle\";\n\n"
  672. "// Inform the GuiObjectView object to set the animation sequence of the object in the control.\n"
  673. "%thisGuiObjectVew.setSeq(%indexVal);\n"
  674. "@endtsexample\n\n"
  675. "@see GuiControl")
  676. {
  677. if( dIsdigit( indexOrName[0] ) )
  678. object->setObjectAnimation( dAtoi( indexOrName ) );
  679. else
  680. object->setObjectAnimation( indexOrName );
  681. }
  682. //-----------------------------------------------------------------------------
  683. DefineEngineMethod( GuiObjectView, setMount, void, ( const char* shapeName, const char* mountNodeIndexOrName),,
  684. "@brief Mounts the given model to the specified mount point of the primary model displayed in this control.\n\n"
  685. "Detailed description\n\n"
  686. "@param shapeName Name of the model to mount.\n"
  687. "@param mountNodeIndexOrName Index or name of the mount point to be mounted to. If index, corresponds to \"mountN\" in your shape where N is the number passed here.\n"
  688. "@tsexample\n"
  689. "// Set the shapeName to mount\n"
  690. "%shapeName = \"GideonGlasses.dts\"\n\n"
  691. "// Set the mount node of the primary model in the control to mount the new shape at\n"
  692. "%mountNodeIndexOrName = \"3\";\n"
  693. "//OR:\n"
  694. "%mountNodeIndexOrName = \"Face\";\n\n"
  695. "// Inform the GuiObjectView object to mount the shape at the specified node.\n"
  696. "%thisGuiObjectView.setMount(%shapeName,%mountNodeIndexOrName);\n"
  697. "@endtsexample\n\n"
  698. "@see GuiControl")
  699. {
  700. if( dIsdigit( mountNodeIndexOrName[0] ) )
  701. object->setMountNode( dAtoi( mountNodeIndexOrName ) );
  702. else
  703. object->setMountNode( mountNodeIndexOrName );
  704. object->setMountedObject( shapeName );
  705. }
  706. //-----------------------------------------------------------------------------
  707. DefineEngineMethod( GuiObjectView, getOrbitDistance, F32, (),,
  708. "@brief Return the current distance at which the camera orbits the object.\n\n"
  709. "@tsexample\n"
  710. "// Request the current orbit distance\n"
  711. "%orbitDistance = %thisGuiObjectView.getOrbitDistance();\n"
  712. "@endtsexample\n\n"
  713. "@return The distance at which the camera orbits the object.\n\n"
  714. "@see GuiControl")
  715. {
  716. return object->getOrbitDistance();
  717. }
  718. //-----------------------------------------------------------------------------
  719. DefineEngineMethod( GuiObjectView, setOrbitDistance, void, (F32 distance),,
  720. "@brief Sets the distance at which the camera orbits the object. Clamped to the acceptable range defined in the class by min and max orbit distances.\n\n"
  721. "Detailed description\n\n"
  722. "@param distance The distance to set the orbit to (will be clamped).\n"
  723. "@tsexample\n"
  724. "// Define the orbit distance value\n"
  725. "%orbitDistance = \"1.5\";\n\n"
  726. "// Inform the GuiObjectView object to set the orbit distance to the defined value\n"
  727. "%thisGuiObjectView.setOrbitDistance(%orbitDistance);\n"
  728. "@endtsexample\n\n"
  729. "@see GuiControl")
  730. {
  731. object->setOrbitDistance( distance );
  732. }
  733. //-----------------------------------------------------------------------------
  734. DefineEngineMethod( GuiObjectView, getCameraSpeed, F32, (),,
  735. "@brief Return the current multiplier for camera zooming and rotation.\n\n"
  736. "@tsexample\n"
  737. "// Request the current camera zooming and rotation multiplier value\n"
  738. "%multiplier = %thisGuiObjectView.getCameraSpeed();\n"
  739. "@endtsexample\n\n"
  740. "@return Camera zooming / rotation multiplier value.\n\n"
  741. "@see GuiControl")
  742. {
  743. return object->getCameraSpeed();
  744. }
  745. //-----------------------------------------------------------------------------
  746. DefineEngineMethod( GuiObjectView, setCameraSpeed, void, (F32 factor),,
  747. "@brief Sets the multiplier for the camera rotation and zoom speed.\n\n"
  748. "@param factor Multiplier for camera rotation and zoom speed.\n"
  749. "@tsexample\n"
  750. "// Set the factor value\n"
  751. "%factor = \"0.75\";\n\n"
  752. "// Inform the GuiObjectView object to set the camera speed.\n"
  753. "%thisGuiObjectView.setCameraSpeed(%factor);\n"
  754. "@endtsexample\n\n"
  755. "@see GuiControl")
  756. {
  757. object->setCameraSpeed( factor );
  758. }
  759. //-----------------------------------------------------------------------------
  760. DefineEngineMethod( GuiObjectView, setLightColor, void, ( ColorF color),,
  761. "@brief Set the light color on the sun object used to render the model.\n\n"
  762. "@param color Color of sunlight.\n"
  763. "@tsexample\n"
  764. "// Set the color value for the sun\n"
  765. "%color = \"1.0 0.4 0.5\";\n\n"
  766. "// Inform the GuiObjectView object to change the sun color to the defined value\n"
  767. "%thisGuiObjectView.setLightColor(%color);\n"
  768. "@endtsexample\n\n"
  769. "@see GuiControl")
  770. {
  771. object->setLightColor( color );
  772. }
  773. //-----------------------------------------------------------------------------
  774. DefineEngineMethod( GuiObjectView, setLightAmbient, void, (ColorF color),,
  775. "@brief Set the light ambient color on the sun object used to render the model.\n\n"
  776. "@param color Ambient color of sunlight.\n"
  777. "@tsexample\n"
  778. "// Define the sun ambient color value\n"
  779. "%color = \"1.0 0.4 0.6\";\n\n"
  780. "// Inform the GuiObjectView object to set the sun ambient color to the requested value\n"
  781. "%thisGuiObjectView.setLightAmbient(%color);\n"
  782. "@endtsexample\n\n"
  783. "@see GuiControl")
  784. {
  785. object->setLightAmbient( color );
  786. }
  787. //-----------------------------------------------------------------------------
  788. DefineEngineMethod( GuiObjectView, setLightDirection, void, (Point3F direction),,
  789. "@brief Set the light direction from which to light the model.\n\n"
  790. "@param direction XYZ direction from which the light will shine on the model\n"
  791. "@tsexample\n"
  792. "// Set the light direction\n"
  793. "%direction = \"1.0 0.2 0.4\"\n\n"
  794. "// Inform the GuiObjectView object to change the light direction to the defined value\n"
  795. "%thisGuiObjectView.setLightDirection(%direction);\n"
  796. "@endtsexample\n\n"
  797. "@see GuiControl")
  798. {
  799. object->setLightDirection( direction );
  800. }