field.cpp 20 KB

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