field.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 "console/engineAPI.h"
  23. #include "platform/platform.h"
  24. #include "gui/editor/inspector/field.h"
  25. #include "console/script.h"
  26. #include "gui/buttons/guiIconButtonCtrl.h"
  27. #include "gui/editor/guiInspector.h"
  28. #include "core/util/safeDelete.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "math/mathTypes.h"
  31. #include "core/strings/stringUnit.h"
  32. IMPLEMENT_CONOBJECT(GuiInspectorField);
  33. ConsoleDocClass( GuiInspectorField,
  34. "@brief The GuiInspectorField control is a representation of a single abstract "
  35. "field for a given ConsoleObject derived object.\n\n"
  36. "Editor use only.\n\n"
  37. "@internal"
  38. );
  39. //-----------------------------------------------------------------------------
  40. GuiInspectorField::GuiInspectorField( GuiInspector* inspector,
  41. GuiInspectorGroup* parent,
  42. AbstractClassRep::Field* field )
  43. : mParent( parent ),
  44. mInspector( inspector ),
  45. mField( field ),
  46. mFieldArrayIndex( NULL ),
  47. mEdit( NULL ),
  48. mTargetObject(NULL),
  49. mUseHeightOverride(false),
  50. mHighlighted(false),
  51. mHeightOverride(18),
  52. mSpecialEditField(false),
  53. mVariableName(StringTable->EmptyString()),
  54. mCallbackName(StringTable->EmptyString()),
  55. mVariableType(StringTable->EmptyString())
  56. {
  57. if( field != NULL )
  58. mCaption = field->pFieldname;
  59. else
  60. mCaption = StringTable->EmptyString();
  61. setCanSave( false );
  62. setBounds(0,0,100,18);
  63. if( field != NULL )
  64. _setFieldDocs( field->pFieldDocs );
  65. }
  66. //-----------------------------------------------------------------------------
  67. GuiInspectorField::GuiInspectorField()
  68. : mParent( NULL ),
  69. mInspector( NULL ),
  70. mField( NULL ),
  71. mEdit( NULL ),
  72. mCaption( StringTable->EmptyString() ),
  73. mFieldArrayIndex( NULL ),
  74. mHighlighted( false ),
  75. mTargetObject(NULL),
  76. mVariableName(StringTable->EmptyString()),
  77. mVariableType(StringTable->EmptyString()),
  78. mCallbackName(StringTable->EmptyString()),
  79. mSpecialEditField(false),
  80. mUseHeightOverride(false),
  81. mHeightOverride(18)
  82. {
  83. setCanSave( false );
  84. }
  85. //-----------------------------------------------------------------------------
  86. GuiInspectorField::~GuiInspectorField()
  87. {
  88. }
  89. //-----------------------------------------------------------------------------
  90. void GuiInspectorField::init( GuiInspector *inspector, GuiInspectorGroup *group )
  91. {
  92. mInspector = inspector;
  93. mParent = group;
  94. }
  95. //-----------------------------------------------------------------------------
  96. bool GuiInspectorField::onAdd()
  97. {
  98. setInspectorProfile();
  99. if ( !Parent::onAdd() )
  100. return false;
  101. if ( !mInspector )
  102. return false;
  103. mEdit = constructEditControl();
  104. if ( mEdit == NULL )
  105. return false;
  106. S32 fieldHeight = 18;
  107. if (mUseHeightOverride)
  108. fieldHeight = mHeightOverride;
  109. setBounds(0,0,100, fieldHeight);
  110. // Add our edit as a child
  111. addObject( mEdit );
  112. // Calculate Caption and EditCtrl Rects
  113. updateRects();
  114. // Force our editField to set it's value
  115. updateValue();
  116. Con::evaluatef("%d.edit = %d;", this->getId(), mEdit->getId());
  117. return true;
  118. }
  119. //-----------------------------------------------------------------------------
  120. bool GuiInspectorField::resize( const Point2I &newPosition, const Point2I &newExtent )
  121. {
  122. if ( !Parent::resize( newPosition, newExtent ) )
  123. return false;
  124. return updateRects();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void GuiInspectorField::onRender( Point2I offset, const RectI &updateRect )
  128. {
  129. RectI ctrlRect(offset, getExtent());
  130. // Render fillcolor...
  131. if ( mProfile->mOpaque )
  132. GFX->getDrawUtil()->drawRectFill(ctrlRect, mProfile->mFillColor);
  133. // Render caption...
  134. if ( mCaption && mCaption[0] )
  135. {
  136. // Backup current ClipRect
  137. RectI clipBackup = GFX->getClipRect();
  138. RectI clipRect = updateRect;
  139. // The rect within this control in which our caption must fit.
  140. RectI rect( offset + mCaptionRect.point + mProfile->mTextOffset, mCaptionRect.extent + Point2I(1,1) - Point2I(5,0) );
  141. // Now clipRect is the amount of our caption rect that is actually visible.
  142. bool hit = clipRect.intersect( rect );
  143. if ( hit )
  144. {
  145. GFX->setClipRect( clipRect );
  146. GFXDrawUtil *drawer = GFX->getDrawUtil();
  147. // Backup modulation color
  148. ColorI currColor;
  149. drawer->getBitmapModulation( &currColor );
  150. // Draw caption background...
  151. if( !isActive() )
  152. GFX->getDrawUtil()->drawRectFill( clipRect, mProfile->mFillColorNA );
  153. else if ( mHighlighted )
  154. GFX->getDrawUtil()->drawRectFill( clipRect, mProfile->mFillColorHL );
  155. // Draw caption text...
  156. drawer->setBitmapModulation( !isActive() ? mProfile->mFontColorNA : mHighlighted ? mProfile->mFontColorHL : mProfile->mFontColor );
  157. // Clip text with '...' if too long to fit
  158. String clippedText( mCaption );
  159. clipText( clippedText, clipRect.extent.x );
  160. renderJustifiedText( offset + mProfile->mTextOffset, getExtent(), clippedText );
  161. // Restore modulation color
  162. drawer->setBitmapModulation( currColor );
  163. // Restore previous ClipRect
  164. GFX->setClipRect( clipBackup );
  165. }
  166. }
  167. // Render Children...
  168. renderChildControls(offset, updateRect);
  169. // Render border...
  170. if ( mProfile->mBorder )
  171. renderBorder(ctrlRect, mProfile);
  172. // Render divider...
  173. Point2I worldPnt = mEditCtrlRect.point + offset;
  174. GFX->getDrawUtil()->drawLine( worldPnt.x - 5,
  175. worldPnt.y,
  176. worldPnt.x - 5,
  177. worldPnt.y + getHeight(),
  178. !isActive() ? mProfile->mBorderColorNA : mHighlighted ? mProfile->mBorderColorHL : mProfile->mBorderColor );
  179. }
  180. //-----------------------------------------------------------------------------
  181. void GuiInspectorField::setFirstResponder( GuiControl *firstResponder )
  182. {
  183. Parent::setFirstResponder( firstResponder );
  184. if ( firstResponder == this || firstResponder == mEdit )
  185. {
  186. mInspector->setHighlightField( this );
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. void GuiInspectorField::onMouseDown( const GuiEvent &event )
  191. {
  192. if ( mCaptionRect.pointInRect( globalToLocalCoord( event.mousePoint ) ) )
  193. {
  194. if ( mEdit )
  195. //mEdit->onMouseDown( event );
  196. mInspector->setHighlightField( this );
  197. }
  198. else
  199. Parent::onMouseDown( event );
  200. }
  201. //-----------------------------------------------------------------------------
  202. void GuiInspectorField::onRightMouseUp( const GuiEvent &event )
  203. {
  204. if ( mCaptionRect.pointInRect( globalToLocalCoord( event.mousePoint ) ) )
  205. Con::executef( mInspector, "onFieldRightClick", getIdString() );
  206. else
  207. Parent::onMouseDown( event );
  208. }
  209. //-----------------------------------------------------------------------------
  210. void GuiInspectorField::setData( const char* data, bool callbacks )
  211. {
  212. if (mSpecialEditField)
  213. {
  214. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  215. {
  216. mTargetObject->setDataField(mVariableName, NULL, data);
  217. if (mCallbackName != StringTable->EmptyString())
  218. Con::executef(mInspector, mCallbackName, mVariableName, data, mTargetObject);
  219. }
  220. else if (mVariableName != StringTable->EmptyString())
  221. {
  222. Con::setVariable(mVariableName, data);
  223. if (mCallbackName != StringTable->EmptyString())
  224. Con::executef(mInspector, mCallbackName, mVariableName, data);
  225. }
  226. }
  227. if( mField == NULL )
  228. return;
  229. if( verifyData( data ) )
  230. {
  231. String strData = data;
  232. const U32 numTargets = mInspector->getNumInspectObjects();
  233. if( callbacks && numTargets > 1 )
  234. Con::executef( mInspector, "onBeginCompoundEdit" );
  235. for( U32 i = 0; i < numTargets; ++ i )
  236. {
  237. //For now, for simplicity's sake, you can only edit the components in a simple edit
  238. SimObject* target = NULL;
  239. if (numTargets == 1)
  240. {
  241. target = mTargetObject;
  242. if (!target)
  243. target = mInspector->getInspectObject(i);
  244. }
  245. else
  246. {
  247. target = mInspector->getInspectObject(i);
  248. }
  249. String oldValue = target->getDataField( mField->pFieldname, mFieldArrayIndex);
  250. // For numeric fields, allow input expressions.
  251. String newValue = strData;
  252. S32 type= mField->type;
  253. ConsoleValue evaluationResult;
  254. if( type == TypeS8 || type == TypeS32 || type == TypeF32 )
  255. {
  256. char buffer[ 2048 ];
  257. expandEscape( buffer, newValue );
  258. evaluationResult = Con::evaluatef("$f = \"%s\"; return ( %s );", oldValue.c_str(), buffer).value;
  259. newValue = evaluationResult.getString();
  260. Con::evaluatef("$f=0;");
  261. }
  262. else if( type == TypeS32Vector
  263. || type == TypeF32Vector
  264. || type == TypeColorI
  265. || type == TypeColorF
  266. || type == TypePoint2I
  267. || type == TypePoint2F
  268. || type == TypePoint3F
  269. || type == TypePoint4F
  270. || type == TypeRectI
  271. || type == TypeRectF
  272. || type == TypeMatrixPosition
  273. || type == TypeMatrixRotation
  274. || type == TypeBox3F
  275. || type == TypeRectUV
  276. || type == TypeRotationF)
  277. {
  278. //TODO: we should actually take strings into account and not chop things up between quotes
  279. U32 numNewUnits = StringUnit::getUnitCount( newValue, " \t\n\r" );
  280. StringBuilder strNew;
  281. bool isFirst = true;
  282. for( U32 n = 0; n < numNewUnits; ++ n )
  283. {
  284. char oldComponentVal[ 1024 ];
  285. StringUnit::getUnit( oldValue, n, " \t\n\r", oldComponentVal, sizeof( oldComponentVal ) );
  286. char newComponentExpr[ 1024 ];
  287. StringUnit::getUnit( newValue, n, " \t\n\r", newComponentExpr, sizeof( newComponentExpr ) );
  288. char buffer[ 2048 ];
  289. expandEscape( buffer, newComponentExpr );
  290. evaluationResult = Con::evaluatef("$f = \"%s\"; $v = \"%s\"; return ( %s );",
  291. oldComponentVal, oldValue.c_str(), buffer).value;
  292. Con::evaluatef("$f=0;$v=0;");
  293. if( !isFirst )
  294. strNew.append( ' ' );
  295. strNew.append( evaluationResult.getString() );
  296. isFirst = false;
  297. }
  298. newValue = strNew.end();
  299. }
  300. target->inspectPreApply();
  301. // Fire callback single-object undo.
  302. if( callbacks && !mField->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors) )
  303. Con::executef( mInspector, "onInspectorFieldModified",
  304. target->getIdString(),
  305. mField->pFieldname,
  306. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  307. oldValue.c_str(),
  308. newValue.c_str() );
  309. target->setDataField( mField->pFieldname, mFieldArrayIndex, newValue );
  310. // Give the target a chance to validate.
  311. target->inspectPostApply();
  312. }
  313. if( callbacks && numTargets > 1 )
  314. Con::executef( mInspector, "onEndCompoundEdit" );
  315. }
  316. // Force our edit to update
  317. updateValue();
  318. }
  319. //-----------------------------------------------------------------------------
  320. const char* GuiInspectorField::getData( U32 inspectObjectIndex )
  321. {
  322. if (!mSpecialEditField)
  323. {
  324. if (mField == NULL)
  325. return "";
  326. if (mTargetObject)
  327. return mTargetObject->getDataField(mField->pFieldname, mFieldArrayIndex);
  328. return mInspector->getInspectObject(inspectObjectIndex)->getDataField(mField->pFieldname, mFieldArrayIndex);
  329. }
  330. else
  331. {
  332. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  333. {
  334. return mTargetObject->getDataField(mVariableName, NULL);
  335. }
  336. else if (mVariableName != StringTable->EmptyString())
  337. {
  338. return Con::getVariable(mVariableName);
  339. }
  340. else
  341. {
  342. return "";
  343. }
  344. }
  345. }
  346. //-----------------------------------------------------------------------------
  347. void GuiInspectorField::resetData()
  348. {
  349. if( !mField )
  350. return;
  351. SimObject* inspectObject = getInspector()->getInspectObject();
  352. SimObject* tempObject = static_cast< SimObject* >( inspectObject->getClassRep()->create() );
  353. setData( tempObject->getDataField( mField->pFieldname, mFieldArrayIndex ) );
  354. delete tempObject;
  355. }
  356. //-----------------------------------------------------------------------------
  357. void GuiInspectorField::setInspectorField( AbstractClassRep::Field *field, StringTableEntry caption, const char*arrayIndex )
  358. {
  359. mField = field;
  360. if ( arrayIndex != NULL )
  361. mFieldArrayIndex = StringTable->insert( arrayIndex );
  362. if ( !caption || !caption[0] )
  363. mCaption = getFieldName();
  364. else
  365. mCaption = caption;
  366. if ( mField != NULL )
  367. _setFieldDocs( mField->pFieldDocs );
  368. }
  369. //-----------------------------------------------------------------------------
  370. StringTableEntry GuiInspectorField::getRawFieldName()
  371. {
  372. if( !mField )
  373. return StringTable->EmptyString();
  374. return mField->pFieldname;
  375. }
  376. //-----------------------------------------------------------------------------
  377. StringTableEntry GuiInspectorField::getFieldName()
  378. {
  379. // Sanity
  380. if ( mField == NULL )
  381. return StringTable->EmptyString();
  382. // Array element?
  383. if( mFieldArrayIndex != NULL )
  384. {
  385. S32 frameTempSize = dStrlen( mField->pFieldname ) + 32;
  386. FrameTemp<char> valCopy( frameTempSize );
  387. dSprintf( (char *)valCopy, frameTempSize, "%s[%s]", mField->pFieldname, mFieldArrayIndex );
  388. // Return formatted element
  389. return StringTable->insert( valCopy );
  390. }
  391. // Plain field name.
  392. return mField->pFieldname;
  393. }
  394. //-----------------------------------------------------------------------------
  395. StringTableEntry GuiInspectorField::getFieldType()
  396. {
  397. if( !mField )
  398. return StringTable->EmptyString();
  399. return ConsoleBaseType::getType( mField->type )->getTypeName();
  400. }
  401. //-----------------------------------------------------------------------------
  402. GuiControl* GuiInspectorField::constructEditControl()
  403. {
  404. GuiControl* retCtrl = new GuiTextEditCtrl();
  405. static StringTableEntry sProfile = StringTable->insert( "profile" );
  406. retCtrl->setDataField( sProfile, NULL, "GuiInspectorTextEditProfile" );
  407. _registerEditControl( retCtrl );
  408. char szBuffer[512];
  409. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), retCtrl->getId() );
  410. // Suffices to hook on to "validate" as regardless of whether we lose
  411. // focus through the user pressing enter or clicking away on another
  412. // keyboard control, we will see a validate call.
  413. retCtrl->setField("validate", szBuffer );
  414. return retCtrl;
  415. }
  416. //-----------------------------------------------------------------------------
  417. void GuiInspectorField::setInspectorProfile()
  418. {
  419. GuiControlProfile *profile = NULL;
  420. if( mInspector && (mInspector->getNumInspectObjects() > 1) )
  421. {
  422. if( !hasSameValueInAllObjects() )
  423. Sim::findObject( "GuiInspectorMultiFieldDifferentProfile", profile );
  424. else
  425. Sim::findObject( "GuiInspectorMultiFieldProfile", profile );
  426. }
  427. if( !profile )
  428. Sim::findObject( "GuiInspectorFieldProfile", profile );
  429. if( profile )
  430. setControlProfile( profile );
  431. }
  432. //-----------------------------------------------------------------------------
  433. void GuiInspectorField::setValue( StringTableEntry newValue )
  434. {
  435. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  436. if( ctrl != NULL )
  437. ctrl->setText( newValue );
  438. }
  439. //-----------------------------------------------------------------------------
  440. void GuiInspectorField::setEditControl(GuiControl* editCtrl)
  441. {
  442. if (mEdit)
  443. mEdit->deleteObject();
  444. mEdit = editCtrl;
  445. addObject(mEdit);
  446. }
  447. //-----------------------------------------------------------------------------
  448. bool GuiInspectorField::updateRects()
  449. {
  450. S32 dividerPos, dividerMargin;
  451. mInspector->getDivider( dividerPos, dividerMargin );
  452. Point2I fieldExtent = getExtent();
  453. Point2I fieldPos = getPosition();
  454. S32 editWidth = dividerPos - dividerMargin;
  455. mEditCtrlRect.set( fieldExtent.x - dividerPos + dividerMargin, 1, editWidth, fieldExtent.y - 1 );
  456. mCaptionRect.set( 0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y );
  457. if ( !mEdit )
  458. return false;
  459. return mEdit->resize( mEditCtrlRect.point, mEditCtrlRect.extent );
  460. }
  461. //-----------------------------------------------------------------------------
  462. void GuiInspectorField::updateValue()
  463. {
  464. if( mInspector->getNumInspectObjects() > 1 )
  465. {
  466. setInspectorProfile();
  467. if( !hasSameValueInAllObjects() )
  468. setValue( StringTable->EmptyString() );
  469. else
  470. setValue( getData() );
  471. }
  472. else
  473. setValue( getData() );
  474. }
  475. //-----------------------------------------------------------------------------
  476. void GuiInspectorField::setHLEnabled( bool enabled )
  477. {
  478. mHighlighted = enabled;
  479. if ( mHighlighted )
  480. {
  481. if ( mEdit && !mEdit->isFirstResponder() )
  482. {
  483. mEdit->setFirstResponder();
  484. GuiTextEditCtrl *edit = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  485. if ( edit )
  486. {
  487. mouseUnlock();
  488. edit->mouseLock();
  489. edit->setCursorPos(0);
  490. }
  491. }
  492. _executeSelectedCallback();
  493. }
  494. }
  495. //-----------------------------------------------------------------------------
  496. bool GuiInspectorField::hasSameValueInAllObjects()
  497. {
  498. char value1[ 2048 ];
  499. // Get field value from first object.
  500. const char* data1 = getData( 0 );
  501. if( data1 )
  502. {
  503. dStrncpy( value1, data1, sizeof( value1 ) );
  504. value1[ sizeof( value1 ) - 1 ] = 0;
  505. }
  506. else
  507. value1[ 0 ] = 0;
  508. // Check if all other objects have the same value.
  509. const U32 numObjects = mInspector->getNumInspectObjects();
  510. for( U32 i = 1; i < numObjects; ++ i )
  511. {
  512. const char* value2 = getData( i );
  513. if( !value2 )
  514. value2 = "";
  515. if( String::compare( value1, value2 ) != 0 )
  516. return false;
  517. }
  518. return true;
  519. }
  520. //-----------------------------------------------------------------------------
  521. void GuiInspectorField::_executeSelectedCallback()
  522. {
  523. if( mField )
  524. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  525. else if(mSpecialEditField)
  526. Con::executef(mInspector, "onFieldSelected", mVariableName, mVariableType, mFieldDocs.c_str());
  527. }
  528. //-----------------------------------------------------------------------------
  529. void GuiInspectorField::_registerEditControl(GuiControl* ctrl, StringTableEntry suffix)
  530. {
  531. if (ctrl->isProperlyAdded()) return;
  532. ctrl->setInternalName(suffix);
  533. char szName[512];
  534. if (mInspector->getInspectObject() != nullptr)
  535. dSprintf(szName, 512, "IE_%s_%d_%s_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), suffix, mCaption);
  536. else
  537. dSprintf(szName, 512, "IE_%s_%s_%s_Field", ctrl->getClassName(), suffix, mCaption);
  538. // Register the object
  539. ctrl->registerObject( szName );
  540. }
  541. //-----------------------------------------------------------------------------
  542. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  543. {
  544. mFieldDocs = String();
  545. if( docs && docs[ 0 ] )
  546. {
  547. // Only accept first line of docs for brevity.
  548. const char* newline = dStrchr( docs, '\n' );
  549. if( newline )
  550. mFieldDocs = String( docs, newline - docs );
  551. else
  552. mFieldDocs = docs;
  553. }
  554. }
  555. void GuiInspectorField::setHeightOverride(bool useOverride, U32 heightOverride)
  556. {
  557. mUseHeightOverride = useOverride;
  558. if (useOverride)
  559. mHeightOverride = heightOverride;
  560. S32 fieldHeight = 18;
  561. if (mUseHeightOverride)
  562. fieldHeight = mHeightOverride;
  563. RectI bnds = getBounds();
  564. setBounds(bnds.point.x, bnds.point.y, bnds.extent.x, fieldHeight);
  565. // Calculate Caption and EditCtrl Rects
  566. updateRects();
  567. // Force our editField to set it's value
  568. updateValue();
  569. }
  570. //=============================================================================
  571. // Console Methods.
  572. //=============================================================================
  573. // MARK: ---- Console Methods ----
  574. //-----------------------------------------------------------------------------
  575. DefineEngineMethod( GuiInspectorField, getInspector, S32, (), , "() - Return the GuiInspector to which this field belongs." )
  576. {
  577. return object->getInspector()->getId();
  578. }
  579. //-----------------------------------------------------------------------------
  580. DefineEngineMethod( GuiInspectorField, getInspectedFieldName, const char*, (), , "() - Return the name of the field edited by this inspector field." )
  581. {
  582. return object->getFieldName();
  583. }
  584. //-----------------------------------------------------------------------------
  585. DefineEngineMethod( GuiInspectorField, getInspectedFieldType, const char*, (), , "() - Return the type of the field edited by this inspector field." )
  586. {
  587. return object->getFieldType();
  588. }
  589. //-----------------------------------------------------------------------------
  590. DefineEngineMethod( GuiInspectorField, apply, void, ( const char * newValue, bool callbacks ), (true), "( string newValue, bool callbacks=true ) - Set the field's value. Suppress callbacks for undo if callbacks=false." )
  591. {
  592. object->setData( newValue, callbacks );
  593. }
  594. //-----------------------------------------------------------------------------
  595. DefineEngineMethod( GuiInspectorField, applyWithoutUndo, void, (const char * data), , "() - Set field value without recording undo (same as 'apply( value, false )')." )
  596. {
  597. object->setData( data, false );
  598. }
  599. //-----------------------------------------------------------------------------
  600. DefineEngineMethod( GuiInspectorField, getData, const char*, (), , "() - Return the value currently displayed on the field." )
  601. {
  602. return object->getData();
  603. }
  604. //-----------------------------------------------------------------------------
  605. DefineEngineMethod( GuiInspectorField, reset, void, (), , "() - Reset to default value." )
  606. {
  607. object->resetData();
  608. }
  609. DefineEngineMethod(GuiInspectorField, setCaption, void, (String newCaption),, "() - Sets the caption of the field.")
  610. {
  611. object->setCaption(StringTable->insert(newCaption.c_str()));
  612. }
  613. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableName, void, (String newCaption), , "() - Sets the variable name for special edit fields.")
  614. {
  615. object->setSpecialEditVariableName(StringTable->insert(newCaption.c_str()));
  616. }
  617. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableType, void, (String newVariableType), , "() - Sets the variable type for special edit fields.")
  618. {
  619. object->setSpecialEditVariableType(StringTable->insert(newVariableType.c_str()));
  620. }
  621. DefineEngineMethod(GuiInspectorField, setSpecialEditCallbackName, void, (String callbackName), , "() - Sets the callback name for special edit fields.")
  622. {
  623. object->setSpecialEditCallbackName(StringTable->insert(callbackName.c_str()));
  624. }
  625. DefineEngineMethod(GuiInspectorField, setFieldDocs, void, (String documentation), , "() - Sets the field's documentation string.")
  626. {
  627. object->setDocs(documentation);
  628. }
  629. DefineEngineMethod(GuiInspectorField, setHeightOverride, void, (bool useOverride, U32 heightOverride), , "")
  630. {
  631. object->setHeightOverride(useOverride, heightOverride);
  632. }
  633. DefineEngineMethod(GuiInspectorField, setEditControl, void, (GuiControl* editCtrl), (nullAsType<GuiControl*>()), "() - Reset to default value.")
  634. {
  635. object->setEditControl(editCtrl);
  636. }