field.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. : mParent( parent ),
  43. mInspector( inspector ),
  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. : mParent( NULL ),
  60. mInspector( NULL ),
  61. mField( NULL ),
  62. mEdit( NULL ),
  63. mCaption( StringTable->EmptyString() ),
  64. mFieldArrayIndex( NULL ),
  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. || type == TypeRotationF)
  228. {
  229. //TODO: we should actually take strings into account and not chop things up between quotes
  230. U32 numNewUnits = StringUnit::getUnitCount( newValue, " \t\n\r" );
  231. StringBuilder strNew;
  232. bool isFirst = true;
  233. for( U32 n = 0; n < numNewUnits; ++ n )
  234. {
  235. char oldComponentVal[ 1024 ];
  236. StringUnit::getUnit( oldValue, n, " \t\n\r", oldComponentVal, sizeof( oldComponentVal ) );
  237. char newComponentExpr[ 1024 ];
  238. StringUnit::getUnit( newValue, n, " \t\n\r", newComponentExpr, sizeof( newComponentExpr ) );
  239. char buffer[ 2048 ];
  240. expandEscape( buffer, newComponentExpr );
  241. const char* newComponentVal = Con::evaluatef( "%%f = \"%s\"; %%v = \"%s\"; return ( %s );",
  242. oldComponentVal, oldValue.c_str(), buffer );
  243. if( !isFirst )
  244. strNew.append( ' ' );
  245. strNew.append( newComponentVal );
  246. isFirst = false;
  247. }
  248. newValue = strNew.end();
  249. }
  250. target->inspectPreApply();
  251. // Fire callback single-object undo.
  252. if( callbacks )
  253. Con::executef( mInspector, "onInspectorFieldModified",
  254. target->getIdString(),
  255. mField->pFieldname,
  256. mFieldArrayIndex ? mFieldArrayIndex : "(null)",
  257. oldValue.c_str(),
  258. newValue.c_str() );
  259. target->setDataField( mField->pFieldname, mFieldArrayIndex, newValue );
  260. // Give the target a chance to validate.
  261. target->inspectPostApply();
  262. }
  263. if( callbacks && numTargets > 1 )
  264. Con::executef( mInspector, "onEndCompoundEdit" );
  265. }
  266. // Force our edit to update
  267. updateValue();
  268. }
  269. //-----------------------------------------------------------------------------
  270. const char* GuiInspectorField::getData( U32 inspectObjectIndex )
  271. {
  272. if( mField == NULL )
  273. return "";
  274. return mInspector->getInspectObject( inspectObjectIndex )->getDataField( mField->pFieldname, mFieldArrayIndex );
  275. }
  276. //-----------------------------------------------------------------------------
  277. void GuiInspectorField::resetData()
  278. {
  279. if( !mField )
  280. return;
  281. SimObject* inspectObject = getInspector()->getInspectObject();
  282. SimObject* tempObject = static_cast< SimObject* >( inspectObject->getClassRep()->create() );
  283. setData( tempObject->getDataField( mField->pFieldname, mFieldArrayIndex ) );
  284. delete tempObject;
  285. }
  286. //-----------------------------------------------------------------------------
  287. void GuiInspectorField::setInspectorField( AbstractClassRep::Field *field, StringTableEntry caption, const char*arrayIndex )
  288. {
  289. mField = field;
  290. if ( arrayIndex != NULL )
  291. mFieldArrayIndex = StringTable->insert( arrayIndex );
  292. if ( !caption || !caption[0] )
  293. mCaption = getFieldName();
  294. else
  295. mCaption = caption;
  296. if ( mField != NULL )
  297. _setFieldDocs( mField->pFieldDocs );
  298. }
  299. //-----------------------------------------------------------------------------
  300. StringTableEntry GuiInspectorField::getRawFieldName()
  301. {
  302. if( !mField )
  303. return StringTable->EmptyString();
  304. return mField->pFieldname;
  305. }
  306. //-----------------------------------------------------------------------------
  307. StringTableEntry GuiInspectorField::getFieldName()
  308. {
  309. // Sanity
  310. if ( mField == NULL )
  311. return StringTable->EmptyString();
  312. // Array element?
  313. if( mFieldArrayIndex != NULL )
  314. {
  315. S32 frameTempSize = dStrlen( mField->pFieldname ) + 32;
  316. FrameTemp<char> valCopy( frameTempSize );
  317. dSprintf( (char *)valCopy, frameTempSize, "%s[%s]", mField->pFieldname, mFieldArrayIndex );
  318. // Return formatted element
  319. return StringTable->insert( valCopy );
  320. }
  321. // Plain field name.
  322. return mField->pFieldname;
  323. }
  324. //-----------------------------------------------------------------------------
  325. StringTableEntry GuiInspectorField::getFieldType()
  326. {
  327. if( !mField )
  328. return StringTable->EmptyString();
  329. return ConsoleBaseType::getType( mField->type )->getTypeName();
  330. }
  331. //-----------------------------------------------------------------------------
  332. GuiControl* GuiInspectorField::constructEditControl()
  333. {
  334. GuiControl* retCtrl = new GuiTextEditCtrl();
  335. static StringTableEntry sProfile = StringTable->insert( "profile" );
  336. retCtrl->setDataField( sProfile, NULL, "GuiInspectorTextEditProfile" );
  337. _registerEditControl( retCtrl );
  338. char szBuffer[512];
  339. dSprintf( szBuffer, 512, "%d.apply(%d.getText());",getId(), retCtrl->getId() );
  340. // Suffices to hook on to "validate" as regardless of whether we lose
  341. // focus through the user pressing enter or clicking away on another
  342. // keyboard control, we will see a validate call.
  343. retCtrl->setField("validate", szBuffer );
  344. return retCtrl;
  345. }
  346. //-----------------------------------------------------------------------------
  347. void GuiInspectorField::setInspectorProfile()
  348. {
  349. GuiControlProfile *profile = NULL;
  350. if( mInspector && (mInspector->getNumInspectObjects() > 1) )
  351. {
  352. if( !hasSameValueInAllObjects() )
  353. Sim::findObject( "GuiInspectorMultiFieldDifferentProfile", profile );
  354. else
  355. Sim::findObject( "GuiInspectorMultiFieldProfile", profile );
  356. }
  357. if( !profile )
  358. Sim::findObject( "GuiInspectorFieldProfile", profile );
  359. if( profile )
  360. setControlProfile( profile );
  361. }
  362. //-----------------------------------------------------------------------------
  363. void GuiInspectorField::setValue( StringTableEntry newValue )
  364. {
  365. GuiTextEditCtrl *ctrl = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  366. if( ctrl != NULL )
  367. ctrl->setText( newValue );
  368. }
  369. //-----------------------------------------------------------------------------
  370. bool GuiInspectorField::updateRects()
  371. {
  372. S32 dividerPos, dividerMargin;
  373. mInspector->getDivider( dividerPos, dividerMargin );
  374. Point2I fieldExtent = getExtent();
  375. Point2I fieldPos = getPosition();
  376. S32 editWidth = dividerPos - dividerMargin;
  377. mEditCtrlRect.set( fieldExtent.x - dividerPos + dividerMargin, 1, editWidth, fieldExtent.y - 1 );
  378. mCaptionRect.set( 0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y );
  379. if ( !mEdit )
  380. return false;
  381. return mEdit->resize( mEditCtrlRect.point, mEditCtrlRect.extent );
  382. }
  383. //-----------------------------------------------------------------------------
  384. void GuiInspectorField::updateValue()
  385. {
  386. if( mInspector->getNumInspectObjects() > 1 )
  387. {
  388. setInspectorProfile();
  389. if( !hasSameValueInAllObjects() )
  390. setValue( StringTable->EmptyString() );
  391. else
  392. setValue( getData() );
  393. }
  394. else
  395. setValue( getData() );
  396. }
  397. //-----------------------------------------------------------------------------
  398. void GuiInspectorField::setHLEnabled( bool enabled )
  399. {
  400. mHighlighted = enabled;
  401. if ( mHighlighted )
  402. {
  403. if ( mEdit && !mEdit->isFirstResponder() )
  404. {
  405. mEdit->setFirstResponder();
  406. GuiTextEditCtrl *edit = dynamic_cast<GuiTextEditCtrl*>( mEdit );
  407. if ( edit )
  408. {
  409. mouseUnlock();
  410. edit->mouseLock();
  411. edit->setCursorPos(0);
  412. }
  413. }
  414. _executeSelectedCallback();
  415. }
  416. }
  417. //-----------------------------------------------------------------------------
  418. bool GuiInspectorField::hasSameValueInAllObjects()
  419. {
  420. char value1[ 2048 ];
  421. // Get field value from first object.
  422. const char* data1 = getData( 0 );
  423. if( data1 )
  424. {
  425. dStrncpy( value1, data1, sizeof( value1 ) );
  426. value1[ sizeof( value1 ) - 1 ] = 0;
  427. }
  428. else
  429. value1[ 0 ] = 0;
  430. // Check if all other objects have the same value.
  431. const U32 numObjects = mInspector->getNumInspectObjects();
  432. for( U32 i = 1; i < numObjects; ++ i )
  433. {
  434. const char* value2 = getData( i );
  435. if( !value2 )
  436. value2 = "";
  437. if( dStrcmp( value1, value2 ) != 0 )
  438. return false;
  439. }
  440. return true;
  441. }
  442. //-----------------------------------------------------------------------------
  443. void GuiInspectorField::_executeSelectedCallback()
  444. {
  445. if( mField )
  446. Con::executef( mInspector, "onFieldSelected", mField->pFieldname, ConsoleBaseType::getType(mField->type)->getTypeName(), mFieldDocs.c_str() );
  447. }
  448. //-----------------------------------------------------------------------------
  449. void GuiInspectorField::_registerEditControl( GuiControl *ctrl )
  450. {
  451. char szName[512];
  452. dSprintf( szName, 512, "IE_%s_%d_%s_Field", ctrl->getClassName(), mInspector->getInspectObject()->getId(), mCaption);
  453. // Register the object
  454. ctrl->registerObject( szName );
  455. }
  456. //-----------------------------------------------------------------------------
  457. void GuiInspectorField::_setFieldDocs( StringTableEntry docs )
  458. {
  459. mFieldDocs = String();
  460. if( docs && docs[ 0 ] )
  461. {
  462. // Only accept first line of docs for brevity.
  463. const char* newline = dStrchr( docs, '\n' );
  464. if( newline )
  465. mFieldDocs = String( docs, newline - docs );
  466. else
  467. mFieldDocs = docs;
  468. }
  469. }
  470. //=============================================================================
  471. // Console Methods.
  472. //=============================================================================
  473. // MARK: ---- Console Methods ----
  474. //-----------------------------------------------------------------------------
  475. DefineConsoleMethod( GuiInspectorField, getInspector, S32, (), , "() - Return the GuiInspector to which this field belongs." )
  476. {
  477. return object->getInspector()->getId();
  478. }
  479. //-----------------------------------------------------------------------------
  480. DefineConsoleMethod( GuiInspectorField, getInspectedFieldName, const char*, (), , "() - Return the name of the field edited by this inspector field." )
  481. {
  482. return object->getFieldName();
  483. }
  484. //-----------------------------------------------------------------------------
  485. DefineConsoleMethod( GuiInspectorField, getInspectedFieldType, const char*, (), , "() - Return the type of the field edited by this inspector field." )
  486. {
  487. return object->getFieldType();
  488. }
  489. //-----------------------------------------------------------------------------
  490. 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." )
  491. {
  492. object->setData( newValue, callbacks );
  493. }
  494. //-----------------------------------------------------------------------------
  495. DefineConsoleMethod( GuiInspectorField, applyWithoutUndo, void, (const char * data), , "() - Set field value without recording undo (same as 'apply( value, false )')." )
  496. {
  497. object->setData( data, false );
  498. }
  499. //-----------------------------------------------------------------------------
  500. DefineConsoleMethod( GuiInspectorField, getData, const char*, (), , "() - Return the value currently displayed on the field." )
  501. {
  502. return object->getData();
  503. }
  504. //-----------------------------------------------------------------------------
  505. DefineConsoleMethod( GuiInspectorField, reset, void, (), , "() - Reset to default value." )
  506. {
  507. object->resetData();
  508. }