field.cpp 20 KB

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