field.cpp 33 KB

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