field.cpp 25 KB

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