field.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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->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. 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 == TypeS16 || 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. if (isProperlyAdded())
  687. _executeSelectedCallback();
  688. }
  689. }
  690. //-----------------------------------------------------------------------------
  691. bool GuiInspectorField::hasSameValueInAllObjects()
  692. {
  693. char value1[ 2048 ];
  694. // Get field value from first object.
  695. const char* data1 = getData( 0 );
  696. if( data1 )
  697. {
  698. dStrncpy( value1, data1, sizeof( value1 ) );
  699. value1[ sizeof( value1 ) - 1 ] = 0;
  700. }
  701. else
  702. value1[ 0 ] = 0;
  703. // Check if all other objects have the same value.
  704. const U32 numObjects = mInspector->getNumInspectObjects();
  705. for( U32 i = 1; i < numObjects; ++ i )
  706. {
  707. const char* value2 = getData( i );
  708. if( !value2 )
  709. value2 = "";
  710. if( String::compare( value1, value2 ) != 0 )
  711. return false;
  712. }
  713. return true;
  714. }
  715. //-----------------------------------------------------------------------------
  716. void GuiInspectorField::_executeSelectedCallback()
  717. {
  718. if( mField )
  719. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  720. else if(mSpecialEditField)
  721. Con::executef(mInspector, "onFieldSelected", mVariableName, mVariableType, mFieldDocs.c_str());
  722. }
  723. //-----------------------------------------------------------------------------
  724. void GuiInspectorField::_registerEditControl(GuiControl* ctrl, StringTableEntry suffix)
  725. {
  726. if (ctrl->isProperlyAdded()) return;
  727. ctrl->setInternalName(suffix);
  728. char szName[512];
  729. if (mInspector->getInspectObject() != nullptr)
  730. dSprintf(szName, 512, "IE_%s_%d_%s_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), suffix, mCaption);
  731. else
  732. dSprintf(szName, 512, "IE_%s_%s_%s_Field", ctrl->getClassName(), suffix, mCaption);
  733. // Register the object
  734. ctrl->registerObject( szName );
  735. }
  736. //-----------------------------------------------------------------------------
  737. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  738. {
  739. mFieldDocs = String();
  740. if( docs && docs[ 0 ] )
  741. {
  742. // Only accept first line of docs for brevity.
  743. const char* newline = dStrchr( docs, '\n' );
  744. if( newline )
  745. mFieldDocs = String( docs, newline - docs );
  746. else
  747. mFieldDocs = docs;
  748. }
  749. }
  750. void GuiInspectorField::setHeightOverride(bool useOverride, U32 heightOverride)
  751. {
  752. mUseHeightOverride = useOverride;
  753. if (useOverride)
  754. mHeightOverride = heightOverride;
  755. S32 fieldHeight = 18;
  756. if (mUseHeightOverride)
  757. fieldHeight = mHeightOverride;
  758. RectI bnds = getBounds();
  759. setBounds(bnds.point.x, bnds.point.y, bnds.extent.x, fieldHeight);
  760. // Calculate Caption and EditCtrl Rects
  761. updateRects();
  762. // Force our editField to set it's value
  763. updateValue();
  764. }
  765. //=============================================================================
  766. // Console Methods.
  767. //=============================================================================
  768. // MARK: ---- Console Methods ----
  769. //-----------------------------------------------------------------------------
  770. DefineEngineMethod( GuiInspectorField, getInspector, S32, (), , "() - Return the GuiInspector to which this field belongs." )
  771. {
  772. return object->getInspector()->getId();
  773. }
  774. //-----------------------------------------------------------------------------
  775. DefineEngineMethod( GuiInspectorField, getInspectedFieldName, const char*, (), , "() - Return the name of the field edited by this inspector field." )
  776. {
  777. return object->getFieldName();
  778. }
  779. //-----------------------------------------------------------------------------
  780. DefineEngineMethod( GuiInspectorField, getInspectedFieldType, const char*, (), , "() - Return the type of the field edited by this inspector field." )
  781. {
  782. return object->getFieldType();
  783. }
  784. //-----------------------------------------------------------------------------
  785. 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." )
  786. {
  787. object->setData( newValue, callbacks );
  788. }
  789. //-----------------------------------------------------------------------------
  790. 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.")
  791. {
  792. object->setWordData(wordIndex, newValue, callbacks);
  793. }
  794. //-----------------------------------------------------------------------------
  795. DefineEngineMethod( GuiInspectorField, applyWithoutUndo, void, (const char * data), , "() - Set field value without recording undo (same as 'apply( value, false )')." )
  796. {
  797. object->setData( data, false );
  798. }
  799. //-----------------------------------------------------------------------------
  800. DefineEngineMethod( GuiInspectorField, getData, const char*, (), , "() - Return the value currently displayed on the field." )
  801. {
  802. return object->getData();
  803. }
  804. //-----------------------------------------------------------------------------
  805. DefineEngineMethod( GuiInspectorField, reset, void, (), , "() - Reset to default value." )
  806. {
  807. object->resetData();
  808. }
  809. DefineEngineMethod(GuiInspectorField, setCaption, void, (String newCaption),, "() - Sets the caption of the field.")
  810. {
  811. object->setCaption(StringTable->insert(newCaption.c_str()));
  812. }
  813. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableName, void, (String newCaption), , "() - Sets the variable name for special edit fields.")
  814. {
  815. object->setSpecialEditVariableName(StringTable->insert(newCaption.c_str()));
  816. }
  817. DefineEngineMethod(GuiInspectorField, setSpecialEditVariableType, void, (String newVariableType), , "() - Sets the variable type for special edit fields.")
  818. {
  819. object->setSpecialEditVariableType(StringTable->insert(newVariableType.c_str()));
  820. }
  821. DefineEngineMethod(GuiInspectorField, setSpecialEditCallbackName, void, (String callbackName), , "() - Sets the callback name for special edit fields.")
  822. {
  823. object->setSpecialEditCallbackName(StringTable->insert(callbackName.c_str()));
  824. }
  825. DefineEngineMethod(GuiInspectorField, setFieldDocs, void, (String documentation), , "() - Sets the field's documentation string.")
  826. {
  827. object->setDocs(documentation);
  828. }
  829. DefineEngineMethod(GuiInspectorField, setHeightOverride, void, (bool useOverride, U32 heightOverride), , "")
  830. {
  831. object->setHeightOverride(useOverride, heightOverride);
  832. }
  833. DefineEngineMethod(GuiInspectorField, setEditControl, void, (GuiControl* editCtrl), (nullAsType<GuiControl*>()), "() - Reset to default value.")
  834. {
  835. object->setEditControl(editCtrl);
  836. }