field.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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 ) && (firstResponder && firstResponder->isProperlyAdded()))
  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 == TypeS16 || 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 == TypeS16 || 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 == TypeS16 || 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. && (dStrcmp(fieldData, newFieldData.end().c_str()) != 0))
  389. Con::executef(mInspector, "onInspectorFieldModified",
  390. target->getIdString(),
  391. mField->pFieldname,
  392. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  393. fieldData,
  394. newFieldData.end());
  395. target->setDataField(mField->pFieldname, mFieldArrayIndex, newFieldData.end());
  396. // Give the target a chance to validate.
  397. target->inspectPostApply();
  398. }
  399. if (callbacks && numTargets > 1)
  400. Con::executef(mInspector, "onEndCompoundEdit");
  401. }
  402. // Force our edit to update
  403. updateValue();
  404. }
  405. void GuiInspectorField::setData( const char* data, bool callbacks )
  406. {
  407. if (mSpecialEditField)
  408. {
  409. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  410. {
  411. mTargetObject->setDataField(mVariableName, NULL, data);
  412. if (mCallbackName != StringTable->EmptyString())
  413. Con::executef(mInspector, mCallbackName, mVariableName, data, mTargetObject);
  414. }
  415. else if (mVariableName != StringTable->EmptyString())
  416. {
  417. Con::setVariable(mVariableName, data);
  418. if (mCallbackName != StringTable->EmptyString())
  419. Con::executef(mInspector, mCallbackName, mVariableName, data);
  420. }
  421. }
  422. if( mField == NULL )
  423. return;
  424. if( verifyData( data ) )
  425. {
  426. String strData = data;
  427. const U32 numTargets = mInspector->getNumInspectObjects();
  428. if( callbacks && numTargets > 1 )
  429. Con::executef( mInspector, "onBeginCompoundEdit" );
  430. for( U32 i = 0; i < numTargets; ++ i )
  431. {
  432. //For now, for simplicity's sake, you can only edit the components in a simple edit
  433. SimObject* target = NULL;
  434. if (numTargets == 1)
  435. {
  436. target = mTargetObject;
  437. if (!target)
  438. target = mInspector->getInspectObject(i);
  439. }
  440. else
  441. {
  442. target = mInspector->getInspectObject(i);
  443. }
  444. String oldValue = target->getDataField( mField->pFieldname, mFieldArrayIndex);
  445. // For numeric fields, allow input expressions.
  446. String newValue = strData;
  447. S32 type= mField->type;
  448. ConsoleValue evaluationResult;
  449. if( type == TypeS8 || type == TypeS16 || type == TypeS32 || type == TypeF32 )
  450. {
  451. char buffer[ 2048 ];
  452. expandEscape( buffer, newValue );
  453. evaluationResult = Con::evaluatef("$f = \"%s\"; return ( %s );", oldValue.c_str(), buffer).value;
  454. newValue = evaluationResult.getString();
  455. Con::evaluatef("$f=0;");
  456. }
  457. else if( type == TypeS32Vector
  458. || type == TypeF32Vector
  459. || type == TypeColorI
  460. || type == TypeColorF
  461. || type == TypePoint2I
  462. || type == TypePoint2F
  463. || type == TypePoint3F
  464. || type == TypePoint4F
  465. || type == TypeRectI
  466. || type == TypeRectF
  467. || type == TypeMatrixPosition
  468. || type == TypeMatrixRotation
  469. || type == TypeBox3F
  470. || type == TypeRectUV
  471. || type == TypeRotationF)
  472. {
  473. //TODO: we should actually take strings into account and not chop things up between quotes
  474. U32 numNewUnits = StringUnit::getUnitCount( newValue, " \t\n\r" );
  475. StringBuilder strNew;
  476. bool isFirst = true;
  477. for( U32 n = 0; n < numNewUnits; ++ n )
  478. {
  479. char oldComponentVal[ 1024 ];
  480. StringUnit::getUnit( oldValue, n, " \t\n\r", oldComponentVal, sizeof( oldComponentVal ) );
  481. char newComponentExpr[ 1024 ];
  482. StringUnit::getUnit( newValue, n, " \t\n\r", newComponentExpr, sizeof( newComponentExpr ) );
  483. char buffer[ 2048 ];
  484. expandEscape( buffer, newComponentExpr );
  485. evaluationResult = Con::evaluatef("$f = \"%s\"; $v = \"%s\"; return ( %s );",
  486. oldComponentVal, oldValue.c_str(), buffer).value;
  487. Con::evaluatef("$f=0;$v=0;");
  488. if( !isFirst )
  489. strNew.append( ' ' );
  490. strNew.append( evaluationResult.getString() );
  491. isFirst = false;
  492. }
  493. newValue = strNew.end();
  494. }
  495. target->inspectPreApply();
  496. // Fire callback single-object undo.
  497. if( callbacks && !mField->flag.test(AbstractClassRep::FieldFlags::FIELD_ComponentInspectors)
  498. && (dStrcmp(oldValue.c_str(), newValue.c_str()) != 0))
  499. Con::executef( mInspector, "onInspectorFieldModified",
  500. target->getIdString(),
  501. mField->pFieldname,
  502. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  503. oldValue.c_str(),
  504. newValue.c_str() );
  505. target->setDataField( mField->pFieldname, mFieldArrayIndex, newValue );
  506. // Give the target a chance to validate.
  507. target->inspectPostApply();
  508. if (String::compare(oldValue.c_str(), newValue.c_str()) != 0)
  509. Con::executef(mInspector, "onPostInspectorFieldModified", mInspector->getIdString(), target->getIdString());
  510. }
  511. if( callbacks && numTargets > 1 )
  512. Con::executef( mInspector, "onEndCompoundEdit" );
  513. }
  514. // Force our edit to update
  515. updateValue();
  516. }
  517. //-----------------------------------------------------------------------------
  518. const char* GuiInspectorField::getData( U32 inspectObjectIndex )
  519. {
  520. if (!mSpecialEditField)
  521. {
  522. if (mField == NULL)
  523. return "";
  524. if (mTargetObject)
  525. return mTargetObject->getDataField(mField->pFieldname, mFieldArrayIndex);
  526. return mInspector->getInspectObject(inspectObjectIndex)->getDataField(mField->pFieldname, mFieldArrayIndex);
  527. }
  528. else
  529. {
  530. if (mTargetObject != nullptr && mVariableName != StringTable->EmptyString())
  531. {
  532. return mTargetObject->getDataField(mVariableName, NULL);
  533. }
  534. else if (mVariableName != StringTable->EmptyString())
  535. {
  536. return Con::getVariable(mVariableName);
  537. }
  538. else
  539. {
  540. return "";
  541. }
  542. }
  543. }
  544. //-----------------------------------------------------------------------------
  545. void GuiInspectorField::resetData()
  546. {
  547. if( !mField )
  548. return;
  549. SimObject* inspectObject = getInspector()->getInspectObject();
  550. SimObject* tempObject = static_cast< SimObject* >( inspectObject->getClassRep()->create() );
  551. setData( tempObject->getDataField( mField->pFieldname, mFieldArrayIndex ) );
  552. delete tempObject;
  553. }
  554. //-----------------------------------------------------------------------------
  555. void GuiInspectorField::setInspectorField( AbstractClassRep::Field *field, StringTableEntry caption, const char*arrayIndex )
  556. {
  557. mField = field;
  558. if ( arrayIndex != NULL )
  559. mFieldArrayIndex = StringTable->insert( arrayIndex );
  560. if ( !caption || !caption[0] )
  561. mCaption = getFieldName();
  562. else
  563. mCaption = caption;
  564. if ( mField != NULL )
  565. _setFieldDocs( mField->pFieldDocs );
  566. }
  567. //-----------------------------------------------------------------------------
  568. StringTableEntry GuiInspectorField::getRawFieldName()
  569. {
  570. if( !mField )
  571. return StringTable->EmptyString();
  572. return mField->pFieldname;
  573. }
  574. //-----------------------------------------------------------------------------
  575. StringTableEntry GuiInspectorField::getFieldName()
  576. {
  577. // Sanity
  578. if ( mField == NULL )
  579. return StringTable->EmptyString();
  580. // Array element?
  581. if( mFieldArrayIndex != NULL )
  582. {
  583. S32 frameTempSize = dStrlen( mField->pFieldname ) + 32;
  584. FrameTemp<char> valCopy( frameTempSize );
  585. dSprintf( (char *)valCopy, frameTempSize, "%s[%s]", mField->pFieldname, mFieldArrayIndex );
  586. // Return formatted element
  587. return StringTable->insert( valCopy );
  588. }
  589. // Plain field name.
  590. return mField->pFieldname;
  591. }
  592. //-----------------------------------------------------------------------------
  593. StringTableEntry GuiInspectorField::getFieldType()
  594. {
  595. if( !mField )
  596. return StringTable->EmptyString();
  597. return ConsoleBaseType::getType( mField->type )->getTypeName();
  598. }
  599. //-----------------------------------------------------------------------------
  600. GuiControl* GuiInspectorField::constructEditControl()
  601. {
  602. GuiControl* retCtrl = new GuiTextEditCtrl();
  603. static StringTableEntry sProfile = StringTable->insert( "profile" );
  604. retCtrl->setDataField( sProfile, NULL, "GuiInspectorTextEditProfile" );
  605. _registerEditControl( retCtrl );
  606. char szBuffer[512];
  607. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), retCtrl->getId() );
  608. // Suffices to hook on to "validate" as regardless of whether we lose
  609. // focus through the user pressing enter or clicking away on another
  610. // keyboard control, we will see a validate call.
  611. retCtrl->setField("validate", szBuffer );
  612. return retCtrl;
  613. }
  614. //-----------------------------------------------------------------------------
  615. void GuiInspectorField::setInspectorProfile()
  616. {
  617. GuiControlProfile *profile = NULL;
  618. if( mInspector && (mInspector->getNumInspectObjects() > 1) )
  619. {
  620. if( !hasSameValueInAllObjects() )
  621. Sim::findObject( "GuiInspectorMultiFieldDifferentProfile", profile );
  622. else
  623. Sim::findObject( "GuiInspectorMultiFieldProfile", profile );
  624. }
  625. if( !profile )
  626. Sim::findObject( "GuiInspectorFieldProfile", profile );
  627. if( profile )
  628. setControlProfile( profile );
  629. }
  630. //-----------------------------------------------------------------------------
  631. void GuiInspectorField::setValue( StringTableEntry newValue )
  632. {
  633. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  634. if( ctrl != NULL )
  635. ctrl->setText( newValue );
  636. }
  637. //-----------------------------------------------------------------------------
  638. void GuiInspectorField::setEditControl(GuiControl* editCtrl)
  639. {
  640. if (mEdit)
  641. mEdit->deleteObject();
  642. mEdit = editCtrl;
  643. addObject(mEdit);
  644. }
  645. //-----------------------------------------------------------------------------
  646. bool GuiInspectorField::updateRects()
  647. {
  648. S32 dividerPos, dividerMargin;
  649. mInspector->getDivider( dividerPos, dividerMargin );
  650. Point2I fieldExtent = getExtent();
  651. Point2I fieldPos = getPosition();
  652. S32 editWidth = dividerPos - dividerMargin;
  653. mEditCtrlRect.set( fieldExtent.x - dividerPos + dividerMargin, 1, editWidth, fieldExtent.y - 1 );
  654. mCaptionRect.set( 0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y );
  655. if ( !mEdit )
  656. return false;
  657. return mEdit->resize( mEditCtrlRect.point, mEditCtrlRect.extent );
  658. }
  659. //-----------------------------------------------------------------------------
  660. void GuiInspectorField::updateValue()
  661. {
  662. if( mInspector->getNumInspectObjects() > 1 )
  663. {
  664. setInspectorProfile();
  665. if( !hasSameValueInAllObjects() )
  666. setValue( StringTable->EmptyString() );
  667. else
  668. setValue( getData() );
  669. }
  670. else
  671. setValue( getData() );
  672. }
  673. //-----------------------------------------------------------------------------
  674. void GuiInspectorField::setHLEnabled( bool enabled )
  675. {
  676. mHighlighted = enabled;
  677. if ( mHighlighted )
  678. {
  679. if ( mEdit && !mEdit->isFirstResponder() )
  680. {
  681. mEdit->setFirstResponder();
  682. GuiTextEditCtrl *edit = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  683. if ( edit )
  684. {
  685. mouseUnlock();
  686. edit->mouseLock();
  687. edit->setCursorPos(0);
  688. }
  689. }
  690. if (isProperlyAdded())
  691. _executeSelectedCallback();
  692. }
  693. }
  694. //-----------------------------------------------------------------------------
  695. bool GuiInspectorField::hasSameValueInAllObjects()
  696. {
  697. char value1[ 2048 ];
  698. // Get field value from first object.
  699. const char* data1 = getData( 0 );
  700. if( data1 )
  701. {
  702. dStrncpy( value1, data1, sizeof( value1 ) );
  703. value1[ sizeof( value1 ) - 1 ] = 0;
  704. }
  705. else
  706. value1[ 0 ] = 0;
  707. // Check if all other objects have the same value.
  708. const U32 numObjects = mInspector->getNumInspectObjects();
  709. for( U32 i = 1; i < numObjects; ++ i )
  710. {
  711. const char* value2 = getData( i );
  712. if( !value2 )
  713. value2 = "";
  714. if( String::compare( value1, value2 ) != 0 )
  715. return false;
  716. }
  717. return true;
  718. }
  719. //-----------------------------------------------------------------------------
  720. void GuiInspectorField::_executeSelectedCallback()
  721. {
  722. if( mField )
  723. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  724. else if(mSpecialEditField)
  725. Con::executef(mInspector, "onFieldSelected", mVariableName, mVariableType, mFieldDocs.c_str());
  726. }
  727. //-----------------------------------------------------------------------------
  728. void GuiInspectorField::_registerEditControl(GuiControl* ctrl, StringTableEntry suffix)
  729. {
  730. if (ctrl->isProperlyAdded()) return;
  731. ctrl->setInternalName(suffix);
  732. char szName[512];
  733. if (mInspector->getInspectObject() != nullptr)
  734. dSprintf(szName, 512, "IE_%s_%d_%s_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), suffix, mCaption);
  735. else
  736. dSprintf(szName, 512, "IE_%s_%s_%s_Field", ctrl->getClassName(), suffix, mCaption);
  737. // Register the object
  738. ctrl->registerObject( szName );
  739. }
  740. //-----------------------------------------------------------------------------
  741. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  742. {
  743. mFieldDocs = String();
  744. if( docs && docs[ 0 ] )
  745. {
  746. // Only accept first line of docs for brevity.
  747. const char* newline = dStrchr( docs, '\n' );
  748. if( newline )
  749. mFieldDocs = String( docs, newline - docs );
  750. else
  751. mFieldDocs = docs;
  752. }
  753. String inDocs(docs);
  754. String outDocs("");
  755. String outLine("");
  756. S32 newline = inDocs.find('\n');
  757. if (newline == -1)
  758. outDocs = docs;
  759. else
  760. {
  761. U32 uCount = StringUnit::getUnitCount(inDocs, " ");
  762. for (U32 i = 0; i < uCount; i++)
  763. {
  764. String docWord = StringUnit::getUnit(inDocs, i, " ");
  765. if (!docWord.isEmpty())
  766. outLine += docWord;
  767. if (outLine.length() > 80)
  768. {
  769. outLine += "\n";
  770. outDocs += outLine;
  771. outLine.clear();
  772. }
  773. else
  774. outLine += " ";
  775. }
  776. }
  777. outDocs += String("\n") + outLine;
  778. mTooltip = outDocs;
  779. }
  780. void GuiInspectorField::setHeightOverride(bool useOverride, U32 heightOverride)
  781. {
  782. mUseHeightOverride = useOverride;
  783. if (useOverride)
  784. mHeightOverride = heightOverride;
  785. S32 fieldHeight = 18;
  786. if (mUseHeightOverride)
  787. fieldHeight = mHeightOverride;
  788. RectI bnds = getBounds();
  789. setBounds(bnds.point.x, bnds.point.y, bnds.extent.x, fieldHeight);
  790. // Calculate Caption and EditCtrl Rects
  791. updateRects();
  792. // Force our editField to set it's value
  793. updateValue();
  794. }
  795. //=============================================================================
  796. // Console Methods.
  797. //=============================================================================
  798. // MARK: ---- Console Methods ----
  799. //-----------------------------------------------------------------------------
  800. DefineEngineMethod( GuiInspectorField, getInspector, S32, (), , "() - Return the GuiInspector to which this field belongs." )
  801. {
  802. return object->getInspector()->getId();
  803. }
  804. //-----------------------------------------------------------------------------
  805. DefineEngineMethod( GuiInspectorField, getInspectedFieldName, const char*, (), , "() - Return the name of the field edited by this inspector field." )
  806. {
  807. return object->getFieldName();
  808. }
  809. //-----------------------------------------------------------------------------
  810. DefineEngineMethod( GuiInspectorField, getInspectedFieldType, const char*, (), , "() - Return the type of the field edited by this inspector field." )
  811. {
  812. return object->getFieldType();
  813. }
  814. //-----------------------------------------------------------------------------
  815. 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." )
  816. {
  817. object->setData( newValue, callbacks );
  818. }
  819. //-----------------------------------------------------------------------------
  820. 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.")
  821. {
  822. object->setWordData(wordIndex, newValue, callbacks);
  823. }
  824. //-----------------------------------------------------------------------------
  825. DefineEngineMethod( GuiInspectorField, applyWithoutUndo, void, (const char * data), , "() - Set field value without recording undo (same as 'apply( value, false )')." )
  826. {
  827. object->setData( data, false );
  828. }
  829. //-----------------------------------------------------------------------------
  830. DefineEngineMethod( GuiInspectorField, getData, const char*, (), , "() - Return the value currently displayed on the field." )
  831. {
  832. return object->getData();
  833. }
  834. //-----------------------------------------------------------------------------
  835. DefineEngineMethod( GuiInspectorField, reset, void, (), , "() - Reset to default value." )
  836. {
  837. object->resetData();
  838. }
  839. DefineEngineMethod(GuiInspectorField, setCaption, void, (String newCaption),, "() - Sets the caption of the field.")
  840. {
  841. object->setCaption(StringTable->insert(newCaption.c_str()));
  842. }
  843. DefineEngineMethod(GuiInspectorField, getFieldName, const char*, (), , "() - Gets the fieldName of the field.")
  844. {
  845. constexpr U32 bufSize = 128;
  846. char* retBuffer = Con::getReturnBuffer(bufSize);
  847. dSprintf(retBuffer, bufSize, "%s", object->getFieldName());
  848. return retBuffer;
  849. }
  850. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableName, void, (String newCaption), , "() - Sets the variable name for special edit fields.")
  851. {
  852. object->setSpecialEditVariableName(StringTable->insert(newCaption.c_str()));
  853. }
  854. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableType, void, (String newVariableType), , "() - Sets the variable type for special edit fields.")
  855. {
  856. object->setSpecialEditVariableType(StringTable->insert(newVariableType.c_str()));
  857. }
  858. DefineEngineMethod(GuiInspectorField, setSpecialEditCallbackName, void, (String callbackName), , "() - Sets the callback name for special edit fields.")
  859. {
  860. object->setSpecialEditCallbackName(StringTable->insert(callbackName.c_str()));
  861. }
  862. DefineEngineMethod(GuiInspectorField, setFieldDocs, void, (String documentation), , "() - Sets the field's documentation string.")
  863. {
  864. object->setDocs(documentation);
  865. }
  866. DefineEngineMethod(GuiInspectorField, setHeightOverride, void, (bool useOverride, U32 heightOverride), , "")
  867. {
  868. object->setHeightOverride(useOverride, heightOverride);
  869. }
  870. DefineEngineMethod(GuiInspectorField, setEditControl, void, (GuiControl* editCtrl), (nullAsType<GuiControl*>()), "() - Reset to default value.")
  871. {
  872. object->setEditControl(editCtrl);
  873. }