guiObjectView.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. endGroup( "Camera" );
  157. Parent::initPersistFields();
  158. }
  159. //------------------------------------------------------------------------------
  160. void GuiObjectView::onStaticModified( StringTableEntry slotName, const char* newValue )
  161. {
  162. Parent::onStaticModified( slotName, newValue );
  163. static StringTableEntry sShapeFile = StringTable->insert( "shapeFile" );
  164. static StringTableEntry sSkin = StringTable->insert( "skin" );
  165. static StringTableEntry sMountedShapeFile = StringTable->insert( "mountedShapeFile" );
  166. static StringTableEntry sMountedSkin = StringTable->insert( "mountedSkin" );
  167. static StringTableEntry sMountedNode = StringTable->insert( "mountedNode" );
  168. static StringTableEntry sLightColor = StringTable->insert( "lightColor" );
  169. static StringTableEntry sLightAmbient = StringTable->insert( "lightAmbient" );
  170. static StringTableEntry sLightDirection = StringTable->insert( "lightDirection" );
  171. static StringTableEntry sOrbitDistance = StringTable->insert( "orbitDistance" );
  172. static StringTableEntry sMinOrbitDistance = StringTable->insert( "minOrbitDistance" );
  173. static StringTableEntry sMaxOrbitDistance = StringTable->insert( "maxOrbitDistance" );
  174. static StringTableEntry sAnimSequence = StringTable->insert( "animSequence" );
  175. if( slotName == sShapeFile )
  176. setObjectModel( String( mModelName ) );
  177. else if( slotName == sSkin )
  178. setSkin( String( mSkinName ) );
  179. else if( slotName == sMountedShapeFile )
  180. setMountedObject( String( mMountedModelName ) );
  181. else if( slotName == sMountedSkin )
  182. setMountSkin( String( mMountSkinName ) );
  183. else if( slotName == sMountedNode )
  184. setMountNode( String( mMountNodeName ) );
  185. else if( slotName == sLightColor )
  186. setLightColor( mLightColor );
  187. else if( slotName == sLightAmbient )
  188. setLightAmbient( mLightAmbient );
  189. else if( slotName == sLightDirection )
  190. setLightDirection( mLightDirection );
  191. else if( slotName == sOrbitDistance || slotName == sMinOrbitDistance || slotName == sMaxOrbitDistance )
  192. setOrbitDistance( mOrbitDist );
  193. else if( slotName == sAnimSequence )
  194. setObjectAnimation( String( mAnimationSeqName ) );
  195. }
  196. //------------------------------------------------------------------------------
  197. bool GuiObjectView::onWake()
  198. {
  199. if( !Parent::onWake() )
  200. return false;
  201. if( !mLight )
  202. {
  203. mLight = LIGHTMGR->createLightInfo();
  204. mLight->setColor( mLightColor );
  205. mLight->setAmbient( mLightAmbient );
  206. mLight->setDirection( mLightDirection );
  207. }
  208. return true;
  209. }
  210. //------------------------------------------------------------------------------
  211. void GuiObjectView::onMouseDown( const GuiEvent &event )
  212. {
  213. if( !mActive || !mVisible || !mAwake )
  214. return;
  215. mMouseState = Rotating;
  216. mLastMousePoint = event.mousePoint;
  217. mouseLock();
  218. }
  219. //------------------------------------------------------------------------------
  220. void GuiObjectView::onMouseUp( const GuiEvent &event )
  221. {
  222. mouseUnlock();
  223. mMouseState = None;
  224. }
  225. //------------------------------------------------------------------------------
  226. void GuiObjectView::onMouseDragged( const GuiEvent &event )
  227. {
  228. if( mMouseState != Rotating )
  229. return;
  230. Point2I delta = event.mousePoint - mLastMousePoint;
  231. mLastMousePoint = event.mousePoint;
  232. mCameraRot.x += ( delta.y * mCameraSpeed );
  233. mCameraRot.z += ( delta.x * mCameraSpeed );
  234. }
  235. //------------------------------------------------------------------------------
  236. void GuiObjectView::onRightMouseDown( const GuiEvent &event )
  237. {
  238. mMouseState = Zooming;
  239. mLastMousePoint = event.mousePoint;
  240. mouseLock();
  241. }
  242. //------------------------------------------------------------------------------
  243. void GuiObjectView::onRightMouseUp( const GuiEvent &event )
  244. {
  245. mouseUnlock();
  246. mMouseState = None;
  247. }
  248. //------------------------------------------------------------------------------
  249. void GuiObjectView::onRightMouseDragged( const GuiEvent &event )
  250. {
  251. if( mMouseState != Zooming )
  252. return;
  253. S32 delta = event.mousePoint.y - mLastMousePoint.y;
  254. mLastMousePoint = event.mousePoint;
  255. mOrbitDist += ( delta * mCameraSpeed );
  256. }
  257. //------------------------------------------------------------------------------
  258. void GuiObjectView::setObjectAnimation( S32 index )
  259. {
  260. mAnimationSeq = index;
  261. mAnimationSeqName = String();
  262. if( mModel )
  263. _initAnimation();
  264. }
  265. //------------------------------------------------------------------------------
  266. void GuiObjectView::setObjectAnimation( const String& sequenceName )
  267. {
  268. mAnimationSeq = -1;
  269. mAnimationSeqName = sequenceName;
  270. if( mModel )
  271. _initAnimation();
  272. }
  273. //------------------------------------------------------------------------------
  274. void GuiObjectView::setObjectModel( const String& modelName )
  275. {
  276. SAFE_DELETE( mModel );
  277. mRunThread = 0;
  278. mModelName = String::EmptyString;
  279. // Load the shape.
  280. Resource< TSShape > model = ResourceManager::get().load( modelName );
  281. if( !model )
  282. {
  283. Con::warnf( "GuiObjectView::setObjectModel - Failed to load model '%s'", modelName.c_str() );
  284. return;
  285. }
  286. // Instantiate it.
  287. mModel = new TSShapeInstance( model, true );
  288. mModelName = modelName;
  289. if( !mSkinName.isEmpty() )
  290. mModel->reSkin( mSkinName );
  291. // Initialize camera values.
  292. mOrbitPos = mModel->getShape()->center;
  293. mMinOrbitDist = mModel->getShape()->radius;
  294. // Initialize animation.
  295. _initAnimation();
  296. _initMount();
  297. }
  298. //------------------------------------------------------------------------------
  299. void GuiObjectView::setSkin( const String& name )
  300. {
  301. if( mModel )
  302. mModel->reSkin( name, mSkinName );
  303. mSkinName = name;
  304. }
  305. //------------------------------------------------------------------------------
  306. void GuiObjectView::setMountSkin( const String& name )
  307. {
  308. if( mMountedModel )
  309. mMountedModel->reSkin( name, mMountSkinName );
  310. mMountSkinName = name;
  311. }
  312. //------------------------------------------------------------------------------
  313. void GuiObjectView::setMountNode( S32 index )
  314. {
  315. setMountNode( String::ToString( "mount%i", index ) );
  316. }
  317. //------------------------------------------------------------------------------
  318. void GuiObjectView::setMountNode( const String& name )
  319. {
  320. mMountNodeName = name;
  321. if( mModel )
  322. _initMount();
  323. }
  324. //------------------------------------------------------------------------------
  325. void GuiObjectView::setMountedObject( const String& modelName )
  326. {
  327. SAFE_DELETE( mMountedModel );
  328. mMountedModelName = String::EmptyString;
  329. // Load the model.
  330. Resource< TSShape > model = ResourceManager::get().load( modelName );
  331. if( !model )
  332. {
  333. Con::warnf( "GuiObjectView::setMountedObject - Failed to load object model '%s'",
  334. modelName.c_str() );
  335. return;
  336. }
  337. mMountedModel = new TSShapeInstance( model, true );
  338. mMountedModelName = modelName;
  339. if( !mMountSkinName.isEmpty() )
  340. mMountedModel->reSkin( mMountSkinName );
  341. if( mModel )
  342. _initMount();
  343. }
  344. //------------------------------------------------------------------------------
  345. bool GuiObjectView::processCameraQuery( CameraQuery* query )
  346. {
  347. // Adjust the camera so that we are still facing the model.
  348. Point3F vec;
  349. MatrixF xRot, zRot;
  350. xRot.set( EulerF( mCameraRot.x, 0.0f, 0.0f ) );
  351. zRot.set( EulerF( 0.0f, 0.0f, mCameraRot.z ) );
  352. mCameraMatrix.mul( zRot, xRot );
  353. mCameraMatrix.getColumn( 1, &vec );
  354. vec *= mOrbitDist;
  355. mCameraPos = mOrbitPos - vec;
  356. query->farPlane = 2100.0f;
  357. query->nearPlane = query->farPlane / 5000.0f;
  358. query->fov = 45.0f;
  359. mCameraMatrix.setColumn( 3, mCameraPos );
  360. query->cameraMatrix = mCameraMatrix;
  361. return true;
  362. }
  363. //------------------------------------------------------------------------------
  364. void GuiObjectView::onMouseEnter( const GuiEvent & event )
  365. {
  366. onMouseEnter_callback();
  367. }
  368. //------------------------------------------------------------------------------
  369. void GuiObjectView::onMouseLeave( const GuiEvent & event )
  370. {
  371. onMouseLeave_callback();
  372. }
  373. //------------------------------------------------------------------------------
  374. void GuiObjectView::renderWorld( const RectI& updateRect )
  375. {
  376. if( !mModel )
  377. return;
  378. GFXTransformSaver _saveTransforms;
  379. // Determine the camera position, and store off render state.
  380. MatrixF modelview;
  381. MatrixF mv;
  382. Point3F cp;
  383. modelview = GFX->getWorldMatrix();
  384. mv = modelview;
  385. mv.inverse();
  386. mv.getColumn( 3, &cp );
  387. RenderPassManager* renderPass = gClientSceneGraph->getDefaultRenderPass();
  388. S32 time = Platform::getVirtualMilliseconds();
  389. S32 dt = time - mLastRenderTime;
  390. mLastRenderTime = time;
  391. LIGHTMGR->unregisterAllLights();
  392. LIGHTMGR->setSpecialLight( LightManager::slSunLightType, mLight );
  393. GFX->setStateBlock( mDefaultGuiSB );
  394. F32 left, right, top, bottom, nearPlane, farPlane;
  395. bool isOrtho;
  396. GFX->getFrustum( &left, &right, &bottom, &top, &nearPlane, &farPlane, &isOrtho );
  397. Frustum frust( false, left, right, top, bottom, nearPlane, farPlane, MatrixF::Identity );
  398. SceneRenderState state
  399. (
  400. gClientSceneGraph,
  401. SPT_Diffuse,
  402. SceneCameraState( GFX->getViewport(), frust, GFX->getWorldMatrix(), GFX->getProjectionMatrix() ),
  403. renderPass,
  404. false
  405. );
  406. // Set up our TS render state here.
  407. TSRenderState rdata;
  408. rdata.setSceneState( &state );
  409. // We might have some forward lit materials
  410. // so pass down a query to gather lights.
  411. LightQuery query;
  412. query.init( SphereF( Point3F::Zero, 1.0f ) );
  413. rdata.setLightQuery( &query );
  414. // Render primary model.
  415. if( mModel )
  416. {
  417. if( mRunThread )
  418. {
  419. mModel->advanceTime( dt / 1000.f, mRunThread );
  420. mModel->animate();
  421. }
  422. mModel->render( rdata );
  423. }
  424. // Render mounted model.
  425. if( mMountedModel && mMountNode != -1 )
  426. {
  427. GFX->pushWorldMatrix();
  428. GFX->multWorld( mModel->mNodeTransforms[ mMountNode ] );
  429. GFX->multWorld( mMountTransform );
  430. mMountedModel->render( rdata );
  431. GFX->popWorldMatrix();
  432. }
  433. renderPass->renderPass( &state );
  434. // Make sure to remove our fake sun.
  435. LIGHTMGR->unregisterAllLights();
  436. }
  437. //------------------------------------------------------------------------------
  438. void GuiObjectView::setOrbitDistance( F32 distance )
  439. {
  440. // Make sure the orbit distance is within the acceptable range
  441. mOrbitDist = mClampF( distance, mMinOrbitDist, mMaxOrbitDist );
  442. }
  443. //------------------------------------------------------------------------------
  444. void GuiObjectView::setCameraSpeed( F32 factor )
  445. {
  446. mCameraSpeed = factor;
  447. }
  448. //------------------------------------------------------------------------------
  449. void GuiObjectView::setLightColor( const ColorF& color )
  450. {
  451. mLightColor = color;
  452. if( mLight )
  453. mLight->setColor( color );
  454. }
  455. //------------------------------------------------------------------------------
  456. void GuiObjectView::setLightAmbient( const ColorF& color )
  457. {
  458. mLightAmbient = color;
  459. if( mLight )
  460. mLight->setAmbient( color );
  461. }
  462. //------------------------------------------------------------------------------
  463. void GuiObjectView::setLightDirection( const Point3F& direction )
  464. {
  465. mLightDirection = direction;
  466. if( mLight )
  467. mLight->setDirection( direction );
  468. }
  469. //------------------------------------------------------------------------------
  470. void GuiObjectView::_initAnimation()
  471. {
  472. AssertFatal( mModel, "GuiObjectView::_initAnimation - No model loaded!" );
  473. if( mAnimationSeqName.isEmpty() && mAnimationSeq == -1 )
  474. return;
  475. // Look up sequence by name.
  476. if( !mAnimationSeqName.isEmpty() )
  477. {
  478. mAnimationSeq = mModel->getShape()->findSequence( mAnimationSeqName );
  479. if( mAnimationSeq == -1 )
  480. {
  481. Con::errorf( "GuiObjectView::_initAnimation - Cannot find animation sequence '%s' on '%s'",
  482. mAnimationSeqName.c_str(),
  483. mModelName.c_str()
  484. );
  485. return;
  486. }
  487. }
  488. // Start sequence.
  489. if( mAnimationSeq != -1 )
  490. {
  491. if( mAnimationSeq >= mModel->getShape()->sequences.size() )
  492. {
  493. Con::errorf( "GuiObjectView::_initAnimation - Sequence '%i' out of range for model '%s'",
  494. mAnimationSeq,
  495. mModelName.c_str()
  496. );
  497. mAnimationSeq = -1;
  498. return;
  499. }
  500. if( !mRunThread )
  501. mRunThread = mModel->addThread();
  502. mModel->setSequence( mRunThread, mAnimationSeq, 0.f );
  503. }
  504. mLastRenderTime = Platform::getVirtualMilliseconds();
  505. }
  506. //------------------------------------------------------------------------------
  507. void GuiObjectView::_initMount()
  508. {
  509. AssertFatal( mModel, "GuiObjectView::_initMount - No model loaded!" );
  510. if( !mMountedModel )
  511. return;
  512. mMountTransform.identity();
  513. // Look up the node to which to mount to.
  514. if( !mMountNodeName.isEmpty() )
  515. {
  516. mMountNode = mModel->getShape()->findNode( mMountNodeName );
  517. if( mMountNode == -1 )
  518. {
  519. Con::errorf( "GuiObjectView::_initMount - No node '%s' on '%s'",
  520. mMountNodeName.c_str(),
  521. mModelName.c_str()
  522. );
  523. return;
  524. }
  525. }
  526. // Make sure mount node is valid.
  527. if( mMountNode != -1 && mMountNode >= mModel->getShape()->nodes.size() )
  528. {
  529. Con::errorf( "GuiObjectView::_initMount - Mount node index '%i' out of range for '%s'",
  530. mMountNode,
  531. mModelName.c_str()
  532. );
  533. mMountNode = -1;
  534. return;
  535. }
  536. // Look up node on the mounted model from
  537. // which to mount to the primary model's node.
  538. S32 mountPoint = mMountedModel->getShape()->findNode( "mountPoint" );
  539. if( mountPoint != -1 )
  540. {
  541. mMountedModel->getShape()->getNodeWorldTransform( mountPoint, &mMountTransform ),
  542. mMountTransform.inverse();
  543. }
  544. }
  545. //=============================================================================
  546. // Console Methods.
  547. //=============================================================================
  548. // MARK: ---- Console Methods ----
  549. //-----------------------------------------------------------------------------
  550. DefineEngineMethod( GuiObjectView, getModel, const char*, (),,
  551. "@brief Return the model displayed in this view.\n\n"
  552. "@tsexample\n"
  553. "// Request the displayed model name from the GuiObjectView object.\n"
  554. "%modelName = %thisGuiObjectView.getModel();\n"
  555. "@endtsexample\n\n"
  556. "@return Name of the displayed model.\n\n"
  557. "@see GuiControl")
  558. {
  559. return Con::getReturnBuffer( object->getModelName() );
  560. }
  561. //-----------------------------------------------------------------------------
  562. DefineEngineMethod( GuiObjectView, setModel, void, (const char* shapeName),,
  563. "@brief Sets the model to be displayed in this control.\n\n"
  564. "@param shapeName Name of the model to display.\n"
  565. "@tsexample\n"
  566. "// Define the model we want to display\n"
  567. "%shapeName = \"gideon.dts\";\n\n"
  568. "// Tell the GuiObjectView object to display the defined model\n"
  569. "%thisGuiObjectView.setModel(%shapeName);\n"
  570. "@endtsexample\n\n"
  571. "@see GuiControl")
  572. {
  573. object->setObjectModel( shapeName );
  574. }
  575. //-----------------------------------------------------------------------------
  576. DefineEngineMethod( GuiObjectView, getMountedModel, const char*, (),,
  577. "@brief Return the name of the mounted model.\n\n"
  578. "@tsexample\n"
  579. "// Request the name of the mounted model from the GuiObjectView object\n"
  580. "%mountedModelName = %thisGuiObjectView.getMountedModel();\n"
  581. "@endtsexample\n\n"
  582. "@return Name of the mounted model.\n\n"
  583. "@see GuiControl")
  584. {
  585. return Con::getReturnBuffer( object->getMountedModelName() );
  586. }
  587. //-----------------------------------------------------------------------------
  588. DefineEngineMethod( GuiObjectView, setMountedModel, void, (const char* shapeName),,
  589. "@brief Sets the model to be mounted on the primary model.\n\n"
  590. "@param shapeName Name of the model to mount.\n"
  591. "@tsexample\n"
  592. "// Define the model name to mount\n"
  593. "%modelToMount = \"GideonGlasses.dts\";\n\n"
  594. "// Inform the GuiObjectView object to mount the defined model to the existing model in the control\n"
  595. "%thisGuiObjectView.setMountedModel(%modelToMount);\n"
  596. "@endtsexample\n\n"
  597. "@see GuiControl")
  598. {
  599. object->setObjectModel(shapeName);
  600. }
  601. //-----------------------------------------------------------------------------
  602. DefineEngineMethod( GuiObjectView, getSkin, const char*, (),,
  603. "@brief Return the name of skin used on the primary model.\n\n"
  604. "@tsexample\n"
  605. "// Request the name of the skin used on the primary model in the control\n"
  606. "%skinName = %thisGuiObjectView.getSkin();\n"
  607. "@endtsexample\n\n"
  608. "@return Name of the skin used on the primary model.\n\n"
  609. "@see GuiControl")
  610. {
  611. return Con::getReturnBuffer( object->getSkin() );
  612. }
  613. //-----------------------------------------------------------------------------
  614. DefineEngineMethod( GuiObjectView, setSkin, void, (const char* skinName),,
  615. "@brief Sets the skin to use on the model being displayed.\n\n"
  616. "@param skinName Name of the skin to use.\n"
  617. "@tsexample\n"
  618. "// Define the skin we want to apply to the main model in the control\n"
  619. "%skinName = \"disco_gideon\";\n\n"
  620. "// Inform the GuiObjectView control to update the skin the to defined skin\n"
  621. "%thisGuiObjectView.setSkin(%skinName);\n"
  622. "@endtsexample\n\n"
  623. "@see GuiControl")
  624. {
  625. object->setSkin( skinName );
  626. }
  627. //-----------------------------------------------------------------------------
  628. DefineEngineMethod( GuiObjectView, getMountSkin, const char*, ( S32 param1, S32 param2),,
  629. "@brief Return the name of skin used on the mounted model.\n\n"
  630. "@tsexample\n"
  631. "// Request the skin name from the model mounted on to the main model in the control\n"
  632. "%mountModelSkin = %thisGuiObjectView.getMountSkin();\n"
  633. "@endtsexample\n\n"
  634. "@return Name of the skin used on the mounted model.\n\n"
  635. "@see GuiControl")
  636. {
  637. return Con::getReturnBuffer( object->getMountSkin() );
  638. }
  639. //-----------------------------------------------------------------------------
  640. DefineEngineMethod( GuiObjectView, setMountSkin, void, (const char* skinName),,
  641. "@brief Sets the skin to use on the mounted model.\n\n"
  642. "@param skinName Name of the skin to set on the model mounted to the main model in the control\n"
  643. "@tsexample\n"
  644. "// Define the name of the skin\n"
  645. "%skinName = \"BronzeGlasses\";\n\n"
  646. "// Inform the GuiObjectView Control of the skin to use on the mounted model\n"
  647. "%thisGuiObjectViewCtrl.setMountSkin(%skinName);\n"
  648. "@endtsexample\n\n"
  649. "@see GuiControl")
  650. {
  651. object->setMountSkin(skinName);
  652. }
  653. //-----------------------------------------------------------------------------
  654. DefineEngineMethod( GuiObjectView, setSeq, void, (const char* indexOrName),,
  655. "@brief Sets the animation to play for the viewed object.\n\n"
  656. "@param indexOrName The index or name of the animation to play.\n"
  657. "@tsexample\n"
  658. "// Set the animation index value, or animation sequence name.\n"
  659. "%indexVal = \"3\";\n"
  660. "//OR:\n"
  661. "%indexVal = \"idle\";\n\n"
  662. "// Inform the GuiObjectView object to set the animation sequence of the object in the control.\n"
  663. "%thisGuiObjectVew.setSeq(%indexVal);\n"
  664. "@endtsexample\n\n"
  665. "@see GuiControl")
  666. {
  667. if( dIsdigit( indexOrName[0] ) )
  668. object->setObjectAnimation( dAtoi( indexOrName ) );
  669. else
  670. object->setObjectAnimation( indexOrName );
  671. }
  672. //-----------------------------------------------------------------------------
  673. DefineEngineMethod( GuiObjectView, setMount, void, ( const char* shapeName, const char* mountNodeIndexOrName),,
  674. "@brief Mounts the given model to the specified mount point of the primary model displayed in this control.\n\n"
  675. "Detailed description\n\n"
  676. "@param shapeName Name of the model to mount.\n"
  677. "@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"
  678. "@tsexample\n"
  679. "// Set the shapeName to mount\n"
  680. "%shapeName = \"GideonGlasses.dts\"\n\n"
  681. "// Set the mount node of the primary model in the control to mount the new shape at\n"
  682. "%mountNodeIndexOrName = \"3\";\n"
  683. "//OR:\n"
  684. "%mountNodeIndexOrName = \"Face\";\n\n"
  685. "// Inform the GuiObjectView object to mount the shape at the specified node.\n"
  686. "%thisGuiObjectView.setMount(%shapeName,%mountNodeIndexOrName);\n"
  687. "@endtsexample\n\n"
  688. "@see GuiControl")
  689. {
  690. if( dIsdigit( mountNodeIndexOrName[0] ) )
  691. object->setMountNode( dAtoi( mountNodeIndexOrName ) );
  692. else
  693. object->setMountNode( mountNodeIndexOrName );
  694. object->setMountedObject( shapeName );
  695. }
  696. //-----------------------------------------------------------------------------
  697. DefineEngineMethod( GuiObjectView, getOrbitDistance, F32, (),,
  698. "@brief Return the current distance at which the camera orbits the object.\n\n"
  699. "@tsexample\n"
  700. "// Request the current orbit distance\n"
  701. "%orbitDistance = %thisGuiObjectView.getOrbitDistance();\n"
  702. "@endtsexample\n\n"
  703. "@return The distance at which the camera orbits the object.\n\n"
  704. "@see GuiControl")
  705. {
  706. return object->getOrbitDistance();
  707. }
  708. //-----------------------------------------------------------------------------
  709. DefineEngineMethod( GuiObjectView, setOrbitDistance, void, (F32 distance),,
  710. "@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"
  711. "Detailed description\n\n"
  712. "@param distance The distance to set the orbit to (will be clamped).\n"
  713. "@tsexample\n"
  714. "// Define the orbit distance value\n"
  715. "%orbitDistance = \"1.5\";\n\n"
  716. "// Inform the GuiObjectView object to set the orbit distance to the defined value\n"
  717. "%thisGuiObjectView.setOrbitDistance(%orbitDistance);\n"
  718. "@endtsexample\n\n"
  719. "@see GuiControl")
  720. {
  721. object->setOrbitDistance( distance );
  722. }
  723. //-----------------------------------------------------------------------------
  724. DefineEngineMethod( GuiObjectView, getCameraSpeed, F32, (),,
  725. "@brief Return the current multiplier for camera zooming and rotation.\n\n"
  726. "@tsexample\n"
  727. "// Request the current camera zooming and rotation multiplier value\n"
  728. "%multiplier = %thisGuiObjectView.getCameraSpeed();\n"
  729. "@endtsexample\n\n"
  730. "@return Camera zooming / rotation multiplier value.\n\n"
  731. "@see GuiControl")
  732. {
  733. return object->getCameraSpeed();
  734. }
  735. //-----------------------------------------------------------------------------
  736. DefineEngineMethod( GuiObjectView, setCameraSpeed, void, (F32 factor),,
  737. "@brief Sets the multiplier for the camera rotation and zoom speed.\n\n"
  738. "@param factor Multiplier for camera rotation and zoom speed.\n"
  739. "@tsexample\n"
  740. "// Set the factor value\n"
  741. "%factor = \"0.75\";\n\n"
  742. "// Inform the GuiObjectView object to set the camera speed.\n"
  743. "%thisGuiObjectView.setCameraSpeed(%factor);\n"
  744. "@endtsexample\n\n"
  745. "@see GuiControl")
  746. {
  747. object->setCameraSpeed( factor );
  748. }
  749. //-----------------------------------------------------------------------------
  750. DefineEngineMethod( GuiObjectView, setLightColor, void, ( ColorF color),,
  751. "@brief Set the light color on the sun object used to render the model.\n\n"
  752. "@param color Color of sunlight.\n"
  753. "@tsexample\n"
  754. "// Set the color value for the sun\n"
  755. "%color = \"1.0 0.4 0.5\";\n\n"
  756. "// Inform the GuiObjectView object to change the sun color to the defined value\n"
  757. "%thisGuiObjectView.setLightColor(%color);\n"
  758. "@endtsexample\n\n"
  759. "@see GuiControl")
  760. {
  761. object->setLightColor( color );
  762. }
  763. //-----------------------------------------------------------------------------
  764. DefineEngineMethod( GuiObjectView, setLightAmbient, void, (ColorF color),,
  765. "@brief Set the light ambient color on the sun object used to render the model.\n\n"
  766. "@param color Ambient color of sunlight.\n"
  767. "@tsexample\n"
  768. "// Define the sun ambient color value\n"
  769. "%color = \"1.0 0.4 0.6\";\n\n"
  770. "// Inform the GuiObjectView object to set the sun ambient color to the requested value\n"
  771. "%thisGuiObjectView.setLightAmbient(%color);\n"
  772. "@endtsexample\n\n"
  773. "@see GuiControl")
  774. {
  775. object->setLightAmbient( color );
  776. }
  777. //-----------------------------------------------------------------------------
  778. DefineEngineMethod( GuiObjectView, setLightDirection, void, (Point3F direction),,
  779. "@brief Set the light direction from which to light the model.\n\n"
  780. "@param direction XYZ direction from which the light will shine on the model\n"
  781. "@tsexample\n"
  782. "// Set the light direction\n"
  783. "%direction = \"1.0 0.2 0.4\"\n\n"
  784. "// Inform the GuiObjectView object to change the light direction to the defined value\n"
  785. "%thisGuiObjectView.setLightDirection(%direction);\n"
  786. "@endtsexample\n\n"
  787. "@see GuiControl")
  788. {
  789. object->setLightDirection( direction );
  790. }