field.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 )
  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. _setFieldDocs( mField->pFieldDocs );
  295. }
  296. //-----------------------------------------------------------------------------
  297. StringTableEntry GuiInspectorField::getRawFieldName()
  298. {
  299. if( !mField )
  300. return StringTable->EmptyString();
  301. return mField->pFieldname;
  302. }
  303. //-----------------------------------------------------------------------------
  304. StringTableEntry GuiInspectorField::getFieldName()
  305. {
  306. // Sanity
  307. if ( mField == NULL )
  308. return StringTable->EmptyString();
  309. // Array element?
  310. if( mFieldArrayIndex != NULL )
  311. {
  312. S32 frameTempSize = dStrlen( mField->pFieldname ) + 32;
  313. FrameTemp<char> valCopy( frameTempSize );
  314. dSprintf( (char *)valCopy, frameTempSize, "%s[%s]", mField->pFieldname, mFieldArrayIndex );
  315. // Return formatted element
  316. return StringTable->insert( valCopy );
  317. }
  318. // Plain field name.
  319. return mField->pFieldname;
  320. }
  321. //-----------------------------------------------------------------------------
  322. StringTableEntry GuiInspectorField::getFieldType()
  323. {
  324. if( !mField )
  325. return StringTable->EmptyString();
  326. return ConsoleBaseType::getType( mField->type )->getTypeName();
  327. }
  328. //-----------------------------------------------------------------------------
  329. GuiControl* GuiInspectorField::constructEditControl()
  330. {
  331. GuiControl* retCtrl = new GuiTextEditCtrl();
  332. static StringTableEntry sProfile = StringTable->insert( "profile" );
  333. retCtrl->setDataField( sProfile, NULL, "GuiInspectorTextEditProfile" );
  334. _registerEditControl( retCtrl );
  335. char szBuffer[512];
  336. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), retCtrl->getId() );
  337. // Suffices to hook on to "validate" as regardless of whether we lose
  338. // focus through the user pressing enter or clicking away on another
  339. // keyboard control, we will see a validate call.
  340. retCtrl->setField("validate", szBuffer );
  341. return retCtrl;
  342. }
  343. //-----------------------------------------------------------------------------
  344. void GuiInspectorField::setInspectorProfile()
  345. {
  346. GuiControlProfile *profile = NULL;
  347. if( mInspector->getNumInspectObjects() > 1 )
  348. {
  349. if( !hasSameValueInAllObjects() )
  350. Sim::findObject( "GuiInspectorMultiFieldDifferentProfile", profile );
  351. else
  352. Sim::findObject( "GuiInspectorMultiFieldProfile", profile );
  353. }
  354. if( !profile )
  355. Sim::findObject( "GuiInspectorFieldProfile", profile );
  356. if( profile )
  357. setControlProfile( profile );
  358. }
  359. //-----------------------------------------------------------------------------
  360. void GuiInspectorField::setValue( StringTableEntry newValue )
  361. {
  362. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  363. if( ctrl != NULL )
  364. ctrl->setText( newValue );
  365. }
  366. //-----------------------------------------------------------------------------
  367. bool GuiInspectorField::updateRects()
  368. {
  369. S32 dividerPos, dividerMargin;
  370. mInspector->getDivider( dividerPos, dividerMargin );
  371. Point2I fieldExtent = getExtent();
  372. Point2I fieldPos = getPosition();
  373. S32 editWidth = dividerPos - dividerMargin;
  374. mEditCtrlRect.set( fieldExtent.x - dividerPos + dividerMargin, 1, editWidth, fieldExtent.y - 1 );
  375. mCaptionRect.set( 0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y );
  376. if ( !mEdit )
  377. return false;
  378. return mEdit->resize( mEditCtrlRect.point, mEditCtrlRect.extent );
  379. }
  380. //-----------------------------------------------------------------------------
  381. void GuiInspectorField::updateValue()
  382. {
  383. if( mInspector->getNumInspectObjects() > 1 )
  384. {
  385. setInspectorProfile();
  386. if( !hasSameValueInAllObjects() )
  387. setValue( StringTable->EmptyString() );
  388. else
  389. setValue( getData() );
  390. }
  391. else
  392. setValue( getData() );
  393. }
  394. //-----------------------------------------------------------------------------
  395. void GuiInspectorField::setHLEnabled( bool enabled )
  396. {
  397. mHighlighted = enabled;
  398. if ( mHighlighted )
  399. {
  400. if ( mEdit && !mEdit->isFirstResponder() )
  401. {
  402. mEdit->setFirstResponder();
  403. GuiTextEditCtrl *edit = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  404. if ( edit )
  405. {
  406. mouseUnlock();
  407. edit->mouseLock();
  408. edit->setCursorPos(0);
  409. }
  410. }
  411. _executeSelectedCallback();
  412. }
  413. }
  414. //-----------------------------------------------------------------------------
  415. bool GuiInspectorField::hasSameValueInAllObjects()
  416. {
  417. char value1[ 2048 ];
  418. // Get field value from first object.
  419. const char* data1 = getData( 0 );
  420. if( data1 )
  421. {
  422. dStrncpy( value1, data1, sizeof( value1 ) );
  423. value1[ sizeof( value1 ) - 1 ] = 0;
  424. }
  425. else
  426. value1[ 0 ] = 0;
  427. // Check if all other objects have the same value.
  428. const U32 numObjects = mInspector->getNumInspectObjects();
  429. for( U32 i = 1; i < numObjects; ++ i )
  430. {
  431. const char* value2 = getData( i );
  432. if( !value2 )
  433. value2 = "";
  434. if( dStrcmp( value1, value2 ) != 0 )
  435. return false;
  436. }
  437. return true;
  438. }
  439. //-----------------------------------------------------------------------------
  440. void GuiInspectorField::_executeSelectedCallback()
  441. {
  442. if( mField )
  443. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  444. }
  445. //-----------------------------------------------------------------------------
  446. void GuiInspectorField::_registerEditControl( GuiControl *ctrl )
  447. {
  448. char szName[512];
  449. dSprintf( szName, 512, "IE_%s_%d_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), mCaption);
  450. // Register the object
  451. ctrl->registerObject( szName );
  452. }
  453. //-----------------------------------------------------------------------------
  454. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  455. {
  456. mFieldDocs = String();
  457. if( docs && docs[ 0 ] )
  458. {
  459. // Only accept first line of docs for brevity.
  460. const char* newline = dStrchr( docs, '\n' );
  461. if( newline )
  462. mFieldDocs = String( docs, newline - docs );
  463. else
  464. mFieldDocs = docs;
  465. }
  466. }
  467. //=============================================================================
  468. // Console Methods.
  469. //=============================================================================
  470. // MARK: ---- Console Methods ----
  471. //-----------------------------------------------------------------------------
  472. ConsoleMethod( GuiInspectorField, getInspector, S32, 2, 2, "() - Return the GuiInspector to which this field belongs." )
  473. {
  474. return object->getInspector()->getId();
  475. }
  476. //-----------------------------------------------------------------------------
  477. ConsoleMethod( GuiInspectorField, getInspectedFieldName, const char*, 2, 2, "() - Return the name of the field edited by this inspector field." )
  478. {
  479. return object->getFieldName();
  480. }
  481. //-----------------------------------------------------------------------------
  482. ConsoleMethod( GuiInspectorField, getInspectedFieldType, const char*, 2, 2, "() - Return the type of the field edited by this inspector field." )
  483. {
  484. return object->getFieldType();
  485. }
  486. //-----------------------------------------------------------------------------
  487. ConsoleMethod( GuiInspectorField, apply, void, 3, 4, "( string newValue, bool callbacks=true ) - Set the field's value. Suppress callbacks for undo if callbacks=false." )
  488. {
  489. bool callbacks = true;
  490. if( argc > 3 )
  491. callbacks = dAtob( argv[ 3 ] );
  492. object->setData( argv[ 2 ], callbacks );
  493. }
  494. //-----------------------------------------------------------------------------
  495. ConsoleMethod( GuiInspectorField, applyWithoutUndo, void, 3, 3, "() - Set field value without recording undo (same as 'apply( value, false )')." )
  496. {
  497. object->setData( argv[ 2 ], false );
  498. }
  499. //-----------------------------------------------------------------------------
  500. ConsoleMethod( GuiInspectorField, getData, const char*, 2, 2, "() - Return the value currently displayed on the field." )
  501. {
  502. return object->getData();
  503. }
  504. //-----------------------------------------------------------------------------
  505. ConsoleMethod( GuiInspectorField, reset, void, 2, 2, "() - Reset to default value." )
  506. {
  507. object->resetData();
  508. }