field.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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::setWordData(const S32& wordIndex, const char* data, bool callbacks)
  211. {
  212. if (mSpecialEditField)
  213. {
  214. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  215. {
  216. const char* fieldData = mTargetObject->getDataField(mVariableName, NULL);
  217. StringBuilder newFieldData;
  218. const U32 wordCount = StringUnit::getUnitCount(fieldData, " \t\n");
  219. for (U32 i = 0; i < wordCount; i++)
  220. {
  221. if (i != 0)
  222. newFieldData.append(" ");
  223. if (i == wordIndex)
  224. newFieldData.append(data);
  225. else
  226. {
  227. newFieldData.append(StringUnit::getUnit(fieldData, i, " \t\n"));
  228. }
  229. }
  230. mTargetObject->setDataField(mVariableName, NULL, newFieldData.end());
  231. if (mCallbackName != StringTable->EmptyString())
  232. Con::executef(mInspector, mCallbackName, mVariableName, newFieldData.end(), mTargetObject);
  233. }
  234. else if (mVariableName != StringTable->EmptyString())
  235. {
  236. const char* fieldData = Con::getVariable(mVariableName, "");
  237. StringBuilder newFieldData;
  238. const U32 wordCount = StringUnit::getUnitCount(fieldData, " \t\n");
  239. for (U32 i = 0; i < wordCount; i++)
  240. {
  241. if (i != 0)
  242. newFieldData.append(" ");
  243. if (i == wordIndex)
  244. newFieldData.append(data);
  245. else
  246. {
  247. newFieldData.append(StringUnit::getUnit(fieldData, i, " \t\n"));
  248. }
  249. }
  250. Con::setVariable(mVariableName, newFieldData.end());
  251. if (mCallbackName != StringTable->EmptyString())
  252. Con::executef(mInspector, mCallbackName, mVariableName, newFieldData.end());
  253. }
  254. }
  255. if (mField == NULL)
  256. return;
  257. if (verifyData(data))
  258. {
  259. String strData = data;
  260. const U32 numTargets = mInspector->getNumInspectObjects();
  261. if (callbacks && numTargets > 1)
  262. Con::executef(mInspector, "onBeginCompoundEdit");
  263. for (U32 i = 0; i < numTargets; ++i)
  264. {
  265. //For now, for simplicity's sake, you can only edit the components in a simple edit
  266. SimObject* target = NULL;
  267. if (numTargets == 1)
  268. {
  269. target = mTargetObject;
  270. if (!target)
  271. target = mInspector->getInspectObject(i);
  272. }
  273. else
  274. {
  275. target = mInspector->getInspectObject(i);
  276. }
  277. const char* fieldData = target->getDataField(mField->pFieldname, mFieldArrayIndex);
  278. StringBuilder newFieldData;
  279. const U32 wordCount = StringUnit::getUnitCount(fieldData, " \t\n");
  280. for (U32 i = 0; i < wordCount; i++)
  281. {
  282. if (i != 0)
  283. newFieldData.append(" ");
  284. if (i == wordIndex)
  285. newFieldData.append(data);
  286. else
  287. {
  288. newFieldData.append(StringUnit::getUnit(fieldData, i, " \t\n"));
  289. }
  290. }
  291. target->inspectPreApply();
  292. // Fire callback single-object undo.
  293. if (callbacks && !mField->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors))
  294. Con::executef(mInspector, "onInspectorFieldModified",
  295. target->getIdString(),
  296. mField->pFieldname,
  297. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  298. fieldData,
  299. newFieldData.end());
  300. target->setDataField(mField->pFieldname, mFieldArrayIndex, newFieldData.end());
  301. // Give the target a chance to validate.
  302. target->inspectPostApply();
  303. }
  304. if (callbacks && numTargets > 1)
  305. Con::executef(mInspector, "onEndCompoundEdit");
  306. }
  307. // Force our edit to update
  308. updateValue();
  309. }
  310. void GuiInspectorField::setData( const char* data, bool callbacks )
  311. {
  312. if (mSpecialEditField)
  313. {
  314. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  315. {
  316. mTargetObject->setDataField(mVariableName, NULL, data);
  317. if (mCallbackName != StringTable->EmptyString())
  318. Con::executef(mInspector, mCallbackName, mVariableName, data, mTargetObject);
  319. }
  320. else if (mVariableName != StringTable->EmptyString())
  321. {
  322. Con::setVariable(mVariableName, data);
  323. if (mCallbackName != StringTable->EmptyString())
  324. Con::executef(mInspector, mCallbackName, mVariableName, data);
  325. }
  326. }
  327. if( mField == NULL )
  328. return;
  329. if( verifyData( data ) )
  330. {
  331. String strData = data;
  332. const U32 numTargets = mInspector->getNumInspectObjects();
  333. if( callbacks && numTargets > 1 )
  334. Con::executef( mInspector, "onBeginCompoundEdit" );
  335. for( U32 i = 0; i < numTargets; ++ i )
  336. {
  337. //For now, for simplicity's sake, you can only edit the components in a simple edit
  338. SimObject* target = NULL;
  339. if (numTargets == 1)
  340. {
  341. target = mTargetObject;
  342. if (!target)
  343. target = mInspector->getInspectObject(i);
  344. }
  345. else
  346. {
  347. target = mInspector->getInspectObject(i);
  348. }
  349. String oldValue = target->getDataField( mField->pFieldname, mFieldArrayIndex);
  350. // For numeric fields, allow input expressions.
  351. String newValue = strData;
  352. S32 type= mField->type;
  353. ConsoleValue evaluationResult;
  354. if( type == TypeS8 || type == TypeS32 || type == TypeF32 )
  355. {
  356. char buffer[ 2048 ];
  357. expandEscape( buffer, newValue );
  358. evaluationResult = Con::evaluatef("$f = \"%s\"; return ( %s );", oldValue.c_str(), buffer).value;
  359. newValue = evaluationResult.getString();
  360. Con::evaluatef("$f=0;");
  361. }
  362. else if( type == TypeS32Vector
  363. || type == TypeF32Vector
  364. || type == TypeColorI
  365. || type == TypeColorF
  366. || type == TypePoint2I
  367. || type == TypePoint2F
  368. || type == TypePoint3F
  369. || type == TypePoint4F
  370. || type == TypeRectI
  371. || type == TypeRectF
  372. || type == TypeMatrixPosition
  373. || type == TypeMatrixRotation
  374. || type == TypeBox3F
  375. || type == TypeRectUV
  376. || type == TypeRotationF)
  377. {
  378. //TODO: we should actually take strings into account and not chop things up between quotes
  379. U32 numNewUnits = StringUnit::getUnitCount( newValue, " \t\n\r" );
  380. StringBuilder strNew;
  381. bool isFirst = true;
  382. for( U32 n = 0; n < numNewUnits; ++ n )
  383. {
  384. char oldComponentVal[ 1024 ];
  385. StringUnit::getUnit( oldValue, n, " \t\n\r", oldComponentVal, sizeof( oldComponentVal ) );
  386. char newComponentExpr[ 1024 ];
  387. StringUnit::getUnit( newValue, n, " \t\n\r", newComponentExpr, sizeof( newComponentExpr ) );
  388. char buffer[ 2048 ];
  389. expandEscape( buffer, newComponentExpr );
  390. evaluationResult = Con::evaluatef("$f = \"%s\"; $v = \"%s\"; return ( %s );",
  391. oldComponentVal, oldValue.c_str(), buffer).value;
  392. Con::evaluatef("$f=0;$v=0;");
  393. if( !isFirst )
  394. strNew.append( ' ' );
  395. strNew.append( evaluationResult.getString() );
  396. isFirst = false;
  397. }
  398. newValue = strNew.end();
  399. }
  400. target->inspectPreApply();
  401. // Fire callback single-object undo.
  402. if( callbacks && !mField->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors) )
  403. Con::executef( mInspector, "onInspectorFieldModified",
  404. target->getIdString(),
  405. mField->pFieldname,
  406. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  407. oldValue.c_str(),
  408. newValue.c_str() );
  409. target->setDataField( mField->pFieldname, mFieldArrayIndex, newValue );
  410. // Give the target a chance to validate.
  411. target->inspectPostApply();
  412. }
  413. if( callbacks && numTargets > 1 )
  414. Con::executef( mInspector, "onEndCompoundEdit" );
  415. }
  416. // Force our edit to update
  417. updateValue();
  418. }
  419. //-----------------------------------------------------------------------------
  420. const char* GuiInspectorField::getData( U32 inspectObjectIndex )
  421. {
  422. if (!mSpecialEditField)
  423. {
  424. if (mField == NULL)
  425. return "";
  426. if (mTargetObject)
  427. return mTargetObject->getDataField(mField->pFieldname, mFieldArrayIndex);
  428. return mInspector->getInspectObject(inspectObjectIndex)->getDataField(mField->pFieldname, mFieldArrayIndex);
  429. }
  430. else
  431. {
  432. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  433. {
  434. return mTargetObject->getDataField(mVariableName, NULL);
  435. }
  436. else if (mVariableName != StringTable->EmptyString())
  437. {
  438. return Con::getVariable(mVariableName);
  439. }
  440. else
  441. {
  442. return "";
  443. }
  444. }
  445. }
  446. //-----------------------------------------------------------------------------
  447. void GuiInspectorField::resetData()
  448. {
  449. if( !mField )
  450. return;
  451. SimObject* inspectObject = getInspector()->getInspectObject();
  452. SimObject* tempObject = static_cast< SimObject* >( inspectObject->getClassRep()->create() );
  453. setData( tempObject->getDataField( mField->pFieldname, mFieldArrayIndex ) );
  454. delete tempObject;
  455. }
  456. //-----------------------------------------------------------------------------
  457. void GuiInspectorField::setInspectorField( AbstractClassRep::Field *field, StringTableEntry caption, const char*arrayIndex )
  458. {
  459. mField = field;
  460. if ( arrayIndex != NULL )
  461. mFieldArrayIndex = StringTable->insert( arrayIndex );
  462. if ( !caption || !caption[0] )
  463. mCaption = getFieldName();
  464. else
  465. mCaption = caption;
  466. if ( mField != NULL )
  467. _setFieldDocs( mField->pFieldDocs );
  468. }
  469. //-----------------------------------------------------------------------------
  470. StringTableEntry GuiInspectorField::getRawFieldName()
  471. {
  472. if( !mField )
  473. return StringTable->EmptyString();
  474. return mField->pFieldname;
  475. }
  476. //-----------------------------------------------------------------------------
  477. StringTableEntry GuiInspectorField::getFieldName()
  478. {
  479. // Sanity
  480. if ( mField == NULL )
  481. return StringTable->EmptyString();
  482. // Array element?
  483. if( mFieldArrayIndex != NULL )
  484. {
  485. S32 frameTempSize = dStrlen( mField->pFieldname ) + 32;
  486. FrameTemp<char> valCopy( frameTempSize );
  487. dSprintf( (char *)valCopy, frameTempSize, "%s[%s]", mField->pFieldname, mFieldArrayIndex );
  488. // Return formatted element
  489. return StringTable->insert( valCopy );
  490. }
  491. // Plain field name.
  492. return mField->pFieldname;
  493. }
  494. //-----------------------------------------------------------------------------
  495. StringTableEntry GuiInspectorField::getFieldType()
  496. {
  497. if( !mField )
  498. return StringTable->EmptyString();
  499. return ConsoleBaseType::getType( mField->type )->getTypeName();
  500. }
  501. //-----------------------------------------------------------------------------
  502. GuiControl* GuiInspectorField::constructEditControl()
  503. {
  504. GuiControl* retCtrl = new GuiTextEditCtrl();
  505. static StringTableEntry sProfile = StringTable->insert( "profile" );
  506. retCtrl->setDataField( sProfile, NULL, "GuiInspectorTextEditProfile" );
  507. _registerEditControl( retCtrl );
  508. char szBuffer[512];
  509. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), retCtrl->getId() );
  510. // Suffices to hook on to "validate" as regardless of whether we lose
  511. // focus through the user pressing enter or clicking away on another
  512. // keyboard control, we will see a validate call.
  513. retCtrl->setField("validate", szBuffer );
  514. return retCtrl;
  515. }
  516. //-----------------------------------------------------------------------------
  517. void GuiInspectorField::setInspectorProfile()
  518. {
  519. GuiControlProfile *profile = NULL;
  520. if( mInspector && (mInspector->getNumInspectObjects() > 1) )
  521. {
  522. if( !hasSameValueInAllObjects() )
  523. Sim::findObject( "GuiInspectorMultiFieldDifferentProfile", profile );
  524. else
  525. Sim::findObject( "GuiInspectorMultiFieldProfile", profile );
  526. }
  527. if( !profile )
  528. Sim::findObject( "GuiInspectorFieldProfile", profile );
  529. if( profile )
  530. setControlProfile( profile );
  531. }
  532. //-----------------------------------------------------------------------------
  533. void GuiInspectorField::setValue( StringTableEntry newValue )
  534. {
  535. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  536. if( ctrl != NULL )
  537. ctrl->setText( newValue );
  538. }
  539. //-----------------------------------------------------------------------------
  540. void GuiInspectorField::setEditControl(GuiControl* editCtrl)
  541. {
  542. if (mEdit)
  543. mEdit->deleteObject();
  544. mEdit = editCtrl;
  545. addObject(mEdit);
  546. }
  547. //-----------------------------------------------------------------------------
  548. bool GuiInspectorField::updateRects()
  549. {
  550. S32 dividerPos, dividerMargin;
  551. mInspector->getDivider( dividerPos, dividerMargin );
  552. Point2I fieldExtent = getExtent();
  553. Point2I fieldPos = getPosition();
  554. S32 editWidth = dividerPos - dividerMargin;
  555. mEditCtrlRect.set( fieldExtent.x - dividerPos + dividerMargin, 1, editWidth, fieldExtent.y - 1 );
  556. mCaptionRect.set( 0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y );
  557. if ( !mEdit )
  558. return false;
  559. return mEdit->resize( mEditCtrlRect.point, mEditCtrlRect.extent );
  560. }
  561. //-----------------------------------------------------------------------------
  562. void GuiInspectorField::updateValue()
  563. {
  564. if( mInspector->getNumInspectObjects() > 1 )
  565. {
  566. setInspectorProfile();
  567. if( !hasSameValueInAllObjects() )
  568. setValue( StringTable->EmptyString() );
  569. else
  570. setValue( getData() );
  571. }
  572. else
  573. setValue( getData() );
  574. }
  575. //-----------------------------------------------------------------------------
  576. void GuiInspectorField::setHLEnabled( bool enabled )
  577. {
  578. mHighlighted = enabled;
  579. if ( mHighlighted )
  580. {
  581. if ( mEdit && !mEdit->isFirstResponder() )
  582. {
  583. mEdit->setFirstResponder();
  584. GuiTextEditCtrl *edit = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  585. if ( edit )
  586. {
  587. mouseUnlock();
  588. edit->mouseLock();
  589. edit->setCursorPos(0);
  590. }
  591. }
  592. _executeSelectedCallback();
  593. }
  594. }
  595. //-----------------------------------------------------------------------------
  596. bool GuiInspectorField::hasSameValueInAllObjects()
  597. {
  598. char value1[ 2048 ];
  599. // Get field value from first object.
  600. const char* data1 = getData( 0 );
  601. if( data1 )
  602. {
  603. dStrncpy( value1, data1, sizeof( value1 ) );
  604. value1[ sizeof( value1 ) - 1 ] = 0;
  605. }
  606. else
  607. value1[ 0 ] = 0;
  608. // Check if all other objects have the same value.
  609. const U32 numObjects = mInspector->getNumInspectObjects();
  610. for( U32 i = 1; i < numObjects; ++ i )
  611. {
  612. const char* value2 = getData( i );
  613. if( !value2 )
  614. value2 = "";
  615. if( String::compare( value1, value2 ) != 0 )
  616. return false;
  617. }
  618. return true;
  619. }
  620. //-----------------------------------------------------------------------------
  621. void GuiInspectorField::_executeSelectedCallback()
  622. {
  623. if( mField )
  624. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  625. else if(mSpecialEditField)
  626. Con::executef(mInspector, "onFieldSelected", mVariableName, mVariableType, mFieldDocs.c_str());
  627. }
  628. //-----------------------------------------------------------------------------
  629. void GuiInspectorField::_registerEditControl(GuiControl* ctrl, StringTableEntry suffix)
  630. {
  631. if (ctrl->isProperlyAdded()) return;
  632. ctrl->setInternalName(suffix);
  633. char szName[512];
  634. if (mInspector->getInspectObject() != nullptr)
  635. dSprintf(szName, 512, "IE_%s_%d_%s_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), suffix, mCaption);
  636. else
  637. dSprintf(szName, 512, "IE_%s_%s_%s_Field", ctrl->getClassName(), suffix, mCaption);
  638. // Register the object
  639. ctrl->registerObject( szName );
  640. }
  641. //-----------------------------------------------------------------------------
  642. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  643. {
  644. mFieldDocs = String();
  645. if( docs && docs[ 0 ] )
  646. {
  647. // Only accept first line of docs for brevity.
  648. const char* newline = dStrchr( docs, '\n' );
  649. if( newline )
  650. mFieldDocs = String( docs, newline - docs );
  651. else
  652. mFieldDocs = docs;
  653. }
  654. }
  655. void GuiInspectorField::setHeightOverride(bool useOverride, U32 heightOverride)
  656. {
  657. mUseHeightOverride = useOverride;
  658. if (useOverride)
  659. mHeightOverride = heightOverride;
  660. S32 fieldHeight = 18;
  661. if (mUseHeightOverride)
  662. fieldHeight = mHeightOverride;
  663. RectI bnds = getBounds();
  664. setBounds(bnds.point.x, bnds.point.y, bnds.extent.x, fieldHeight);
  665. // Calculate Caption and EditCtrl Rects
  666. updateRects();
  667. // Force our editField to set it's value
  668. updateValue();
  669. }
  670. //=============================================================================
  671. // Console Methods.
  672. //=============================================================================
  673. // MARK: ---- Console Methods ----
  674. //-----------------------------------------------------------------------------
  675. DefineEngineMethod( GuiInspectorField, getInspector, S32, (), , "() - Return the GuiInspector to which this field belongs." )
  676. {
  677. return object->getInspector()->getId();
  678. }
  679. //-----------------------------------------------------------------------------
  680. DefineEngineMethod( GuiInspectorField, getInspectedFieldName, const char*, (), , "() - Return the name of the field edited by this inspector field." )
  681. {
  682. return object->getFieldName();
  683. }
  684. //-----------------------------------------------------------------------------
  685. DefineEngineMethod( GuiInspectorField, getInspectedFieldType, const char*, (), , "() - Return the type of the field edited by this inspector field." )
  686. {
  687. return object->getFieldType();
  688. }
  689. //-----------------------------------------------------------------------------
  690. 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." )
  691. {
  692. object->setData( newValue, callbacks );
  693. }
  694. //-----------------------------------------------------------------------------
  695. DefineEngineMethod(GuiInspectorField, applyWord, void, (S32 wordIndex, const char* newValue, bool callbacks), (true), "( string newValue, bool callbacks=true ) - Set the field's value. Suppress callbacks for undo if callbacks=false.")
  696. {
  697. object->setWordData(wordIndex, newValue, callbacks);
  698. }
  699. //-----------------------------------------------------------------------------
  700. DefineEngineMethod( GuiInspectorField, applyWithoutUndo, void, (const char * data), , "() - Set field value without recording undo (same as 'apply( value, false )')." )
  701. {
  702. object->setData( data, false );
  703. }
  704. //-----------------------------------------------------------------------------
  705. DefineEngineMethod( GuiInspectorField, getData, const char*, (), , "() - Return the value currently displayed on the field." )
  706. {
  707. return object->getData();
  708. }
  709. //-----------------------------------------------------------------------------
  710. DefineEngineMethod( GuiInspectorField, reset, void, (), , "() - Reset to default value." )
  711. {
  712. object->resetData();
  713. }
  714. DefineEngineMethod(GuiInspectorField, setCaption, void, (String newCaption),, "() - Sets the caption of the field.")
  715. {
  716. object->setCaption(StringTable->insert(newCaption.c_str()));
  717. }
  718. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableName, void, (String newCaption), , "() - Sets the variable name for special edit fields.")
  719. {
  720. object->setSpecialEditVariableName(StringTable->insert(newCaption.c_str()));
  721. }
  722. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableType, void, (String newVariableType), , "() - Sets the variable type for special edit fields.")
  723. {
  724. object->setSpecialEditVariableType(StringTable->insert(newVariableType.c_str()));
  725. }
  726. DefineEngineMethod(GuiInspectorField, setSpecialEditCallbackName, void, (String callbackName), , "() - Sets the callback name for special edit fields.")
  727. {
  728. object->setSpecialEditCallbackName(StringTable->insert(callbackName.c_str()));
  729. }
  730. DefineEngineMethod(GuiInspectorField, setFieldDocs, void, (String documentation), , "() - Sets the field's documentation string.")
  731. {
  732. object->setDocs(documentation);
  733. }
  734. DefineEngineMethod(GuiInspectorField, setHeightOverride, void, (bool useOverride, U32 heightOverride), , "")
  735. {
  736. object->setHeightOverride(useOverride, heightOverride);
  737. }
  738. DefineEngineMethod(GuiInspectorField, setEditControl, void, (GuiControl* editCtrl), (nullAsType<GuiControl*>()), "() - Reset to default value.")
  739. {
  740. object->setEditControl(editCtrl);
  741. }