mountingGroup.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "gui/buttons/guiIconButtonCtrl.h"
  23. #include "gui/editor/guiInspector.h"
  24. #include "gui/editor/inspector/mountingGroup.h"
  25. #include "core/strings/stringUnit.h"
  26. #include "T3D/entity.h"
  27. #include "T3D/components/component.h"
  28. //Need this to get node lists
  29. #include "T3D/components/render/renderComponentInterface.h"
  30. IMPLEMENT_CONOBJECT(GuiInspectorMountingGroup);
  31. ConsoleDocClass( GuiInspectorMountingGroup,
  32. "@brief Used to inspect an object's FieldDictionary (dynamic fields) instead "
  33. "of regular persistent fields.\n\n"
  34. "Editor use only.\n\n"
  35. "@internal"
  36. );
  37. //-----------------------------------------------------------------------------
  38. // GuiInspectorMountingGroup - add custom controls
  39. //-----------------------------------------------------------------------------
  40. GuiInspectorMountingGroup::GuiInspectorMountingGroup( StringTableEntry groupName, SimObjectPtr<GuiInspector> parent )
  41. : GuiInspectorGroup( groupName, parent)
  42. {
  43. mParentInspector = parent;
  44. targetMountCtrl = NULL;
  45. mountCtrl = NULL;
  46. };
  47. bool GuiInspectorMountingGroup::createContent()
  48. {
  49. if(!Parent::createContent())
  50. return false;
  51. //give the necessary padding for the nested controls so it looks nice.
  52. setMargin(RectI(4,0,4,4));
  53. return true;
  54. }
  55. GuiControl* GuiInspectorMountingGroup::buildMenuCtrl()
  56. {
  57. GuiControl* retCtrl = new GuiPopUpMenuCtrl();
  58. // If we couldn't construct the control, bail!
  59. if( retCtrl == NULL )
  60. return retCtrl;
  61. GuiPopUpMenuCtrl *menu = dynamic_cast<GuiPopUpMenuCtrl*>(retCtrl);
  62. // Let's make it look pretty.
  63. retCtrl->setDataField( StringTable->insert("profile"), NULL, "GuiPopUpMenuProfile" );
  64. //GuiInspectorTypeMenuBase::_registerEditControl( retCtrl );
  65. char szName[512];
  66. dSprintf( szName, 512, "IE_%s_%d_%s_Field", retCtrl->getClassName(), mParentInspector->getInspectObject()->getId(), mCaption.c_str());
  67. // Register the object
  68. retCtrl->registerObject( szName );
  69. // Configure it to update our value when the popup is closed
  70. char szBuffer[512];
  71. dSprintf( szBuffer, 512, "%d.apply( %d.getText() );", getId(), menu->getId() );
  72. menu->setField("Command", szBuffer );
  73. return menu;
  74. }
  75. bool GuiInspectorMountingGroup::buildList(Entity* ent, GuiPopUpMenuCtrl* menu)
  76. {
  77. RenderComponentInterface* renderInterface = ent->getComponent<RenderComponentInterface>();
  78. if (renderInterface)
  79. {
  80. TSShape* shape = renderInterface->getShape();
  81. S32 nodeCount = shape ? shape->nodes.size() : 0;
  82. for(U32 i=0; i < nodeCount; i++)
  83. {
  84. menu->addEntry(shape->names[i], i);
  85. }
  86. return true;
  87. }
  88. return false;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // GuiInspectorMountingGroup - inspectGroup override
  92. //-----------------------------------------------------------------------------
  93. bool GuiInspectorMountingGroup::inspectGroup()
  94. {
  95. // We can't inspect a group without a target!
  96. if( !mParent->getNumInspectObjects() )
  97. return false;
  98. // to prevent crazy resizing, we'll just freeze our stack for a sec..
  99. mStack->freeze(true);
  100. bool bNoGroup = false;
  101. // Un-grouped fields are all sorted into the 'general' group
  102. if ( dStricmp( mCaption, "General" ) == 0 )
  103. bNoGroup = true;
  104. // Just delete all fields and recreate them (like the dynamicGroup)
  105. // because that makes creating controls for array fields a lot easier
  106. clearFields();
  107. bool bNewItems = false;
  108. bool bMakingArray = false;
  109. GuiStackControl *pArrayStack = NULL;
  110. GuiRolloutCtrl *pArrayRollout = NULL;
  111. bool bGrabItems = false;
  112. AbstractClassRep* commonAncestorClass = findCommonAncestorClass();
  113. AbstractClassRep::FieldList& fieldList = commonAncestorClass->mFieldList;
  114. for( AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  115. itr != fieldList.end(); ++ itr )
  116. {
  117. AbstractClassRep::Field* field = &( *itr );
  118. if( field->type == AbstractClassRep::StartGroupFieldType )
  119. {
  120. // If we're dealing with general fields, always set grabItems to true (to skip them)
  121. if( bNoGroup == true )
  122. bGrabItems = true;
  123. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  124. bGrabItems = true;
  125. continue;
  126. }
  127. else if ( field->type == AbstractClassRep::EndGroupFieldType )
  128. {
  129. // If we're dealing with general fields, always set grabItems to false (to grab them)
  130. if( bNoGroup == true )
  131. bGrabItems = false;
  132. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  133. bGrabItems = false;
  134. continue;
  135. }
  136. // Skip field if it has the HideInInspectors flag set.
  137. if( field->flag.test( AbstractClassRep::FIELD_HideInInspectors ) )
  138. continue;
  139. if( ( bGrabItems == true || ( bNoGroup == true && bGrabItems == false ) ) && itr->type != AbstractClassRep::DeprecatedFieldType )
  140. {
  141. if( bNoGroup == true && bGrabItems == true )
  142. continue;
  143. // If the field already exists, just update it
  144. GuiInspectorField *fieldGui = findField( field->pFieldname );
  145. if ( fieldGui != NULL )
  146. {
  147. fieldGui->updateValue();
  148. continue;
  149. }
  150. bNewItems = true;
  151. if(field->pFieldname == StringTable->insert("mountNode"))
  152. {
  153. fieldGui = new GuiInspectorNodeListField();
  154. Entity* e = dynamic_cast<Entity*>(mParent->getInspectObject(0));
  155. if(e)
  156. (dynamic_cast<GuiInspectorNodeListField*>(fieldGui))->setTargetEntity(e);
  157. }
  158. else
  159. {
  160. fieldGui = constructField( field->type );
  161. if ( fieldGui == NULL )
  162. fieldGui = new GuiInspectorField();
  163. }
  164. fieldGui->init( mParent, this );
  165. fieldGui->setInspectorField( field );
  166. if( fieldGui->registerObject() )
  167. {
  168. #ifdef DEBUG_SPEW
  169. Platform::outputDebugString( "[GuiInspectorGroup] Adding field '%s'",
  170. field->pFieldname );
  171. #endif
  172. mChildren.push_back( fieldGui );
  173. mStack->addObject( fieldGui );
  174. }
  175. else
  176. {
  177. SAFE_DELETE( fieldGui );
  178. }
  179. }
  180. }
  181. mStack->freeze(false);
  182. mStack->updatePanes();
  183. // If we've no new items, there's no need to resize anything!
  184. if( bNewItems == false && !mChildren.empty() )
  185. return true;
  186. sizeToContents();
  187. setUpdate();
  188. return true;
  189. }
  190. void GuiInspectorMountingGroup::updateAllFields()
  191. {
  192. // We overload this to just reinspect the group.
  193. inspectGroup();
  194. }
  195. void GuiInspectorMountingGroup::onMouseMove(const GuiEvent &event)
  196. {
  197. //mParent->mOverDivider = false;
  198. bool test = false;
  199. }
  200. ConsoleMethod(GuiInspectorMountingGroup, inspectGroup, bool, 2, 2, "Refreshes the dynamic fields in the inspector.")
  201. {
  202. return object->inspectGroup();
  203. }
  204. void GuiInspectorMountingGroup::clearFields()
  205. {
  206. }
  207. bool GuiInspectorMountingGroup::resize( const Point2I &newPosition, const Point2I &newExtent )
  208. {
  209. if ( !Parent::resize( newPosition, newExtent ) )
  210. return false;
  211. //check if we're set up yet
  212. if(!targetMountCtrl || !mountCtrl)
  213. //no? bail
  214. return false;
  215. targetMountCtrl->setExtent(newExtent.x, 18);
  216. mountCtrl->setExtent(newExtent.x, 18);
  217. S32 dividerPos, dividerMargin;
  218. mParentInspector->getDivider( dividerPos, dividerMargin );
  219. Point2I fieldExtent = Point2I(newExtent.x, 18);
  220. Point2I fieldPos = Point2I(newExtent.x, 18);
  221. S32 editWidth = dividerPos - dividerMargin;
  222. targetMountText->setPosition(0,0);
  223. targetMountText->setExtent(fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  224. targetMountNode->setPosition(fieldExtent.x - dividerPos + dividerMargin, 1);
  225. targetMountNode->setExtent(editWidth, fieldExtent.y - 1);
  226. mountText->setPosition(0,0);
  227. mountText->setExtent(fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  228. mountNode->setPosition(fieldExtent.x - dividerPos + dividerMargin, 1);
  229. mountNode->setExtent(editWidth, fieldExtent.y - 1);
  230. return true;
  231. }
  232. SimFieldDictionary::Entry* GuiInspectorMountingGroup::findDynamicFieldInDictionary( StringTableEntry fieldName )
  233. {
  234. SimFieldDictionary * fieldDictionary = mParent->getInspectObject()->getFieldDictionary();
  235. for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  236. {
  237. SimFieldDictionary::Entry * entry = (*ditr);
  238. if( entry->slotName == fieldName )
  239. return entry;
  240. }
  241. return NULL;
  242. }
  243. void GuiInspectorMountingGroup::addDynamicField()
  244. {
  245. }
  246. AbstractClassRep::Field* GuiInspectorMountingGroup::findObjectComponentField(Component* target, String fieldName)
  247. {
  248. AbstractClassRep::FieldList& fieldList = target->getClassRep()->mFieldList;
  249. for( AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  250. itr != fieldList.end(); ++ itr )
  251. {
  252. AbstractClassRep::Field* field = &( *itr );
  253. String fldNm(field->pFieldname);
  254. if(fldNm == fieldName)
  255. return field;
  256. }
  257. return NULL;
  258. }
  259. ConsoleMethod( GuiInspectorMountingGroup, addDynamicField, void, 2, 2, "obj.addDynamicField();" )
  260. {
  261. object->addDynamicField();
  262. }
  263. ConsoleMethod( GuiInspectorMountingGroup, removeDynamicField, void, 3, 3, "" )
  264. {
  265. }
  266. //
  267. IMPLEMENT_CONOBJECT( GuiInspectorNodeListField );
  268. ConsoleDocClass( GuiInspectorNodeListField,
  269. "@brief A control that allows to edit the custom properties (text) of one or more SimObjects.\n\n"
  270. "Editor use only.\n\n"
  271. "@internal"
  272. );
  273. GuiInspectorNodeListField::GuiInspectorNodeListField( GuiInspector *inspector,
  274. GuiInspectorGroup* parent,
  275. SimFieldDictionary::Entry* field,
  276. SimObjectPtr<Entity> target )
  277. {
  278. mInspector = inspector;
  279. mParent = parent;
  280. setBounds(0,0,100,20);
  281. mTargetEntity = target;
  282. }
  283. GuiInspectorNodeListField::GuiInspectorNodeListField()
  284. {
  285. mInspector = NULL;
  286. mParent = NULL;
  287. }
  288. void GuiInspectorNodeListField::setData( const char* data, bool callbacks )
  289. {
  290. mCustomValue = data;
  291. //We aren't updating any mounting info if we're not mounted already
  292. if(mTargetEntity.getObject())
  293. {
  294. Entity* target = dynamic_cast<Entity*>(mTargetEntity->getObjectMount());
  295. if(target)
  296. {
  297. RenderComponentInterface* renderInterface = target->getComponent<RenderComponentInterface>();
  298. if (renderInterface)
  299. {
  300. if (renderInterface->getShape())
  301. {
  302. S32 nodeIdx = renderInterface->getShape()->findNode(data);
  303. target->mountObject(mTargetEntity, nodeIdx, MatrixF::Identity);
  304. mTargetEntity->setMaskBits(Entity::MountedMask);
  305. }
  306. }
  307. }
  308. }
  309. // Force our edit to update
  310. updateValue();
  311. }
  312. const char* GuiInspectorNodeListField::getData( U32 inspectObjectIndex )
  313. {
  314. return mCustomValue;
  315. }
  316. void GuiInspectorNodeListField::updateValue()
  317. {
  318. mMenu->clear();
  319. //mMenu->addEntry("Origin");
  320. //if(mCustomValue.isEmpty())
  321. if(mTargetEntity.getObject())
  322. {
  323. Entity* target = dynamic_cast<Entity*>(mTargetEntity->getObjectMount());
  324. if(target)
  325. {
  326. mMenu->addEntry("Origin");
  327. mMenu->setActive(true);
  328. RenderComponentInterface* renderInterface = target->getComponent<RenderComponentInterface>();
  329. if (renderInterface)
  330. {
  331. TSShape* shape = renderInterface->getShape();
  332. S32 nodeCount = shape ? shape->nodes.size() : 0;
  333. for(U32 i=0; i < nodeCount; i++)
  334. {
  335. mMenu->addEntry(shape->names[i], i);
  336. }
  337. S32 targetNode = mTargetEntity->getMountNode();
  338. if(targetNode != -1)
  339. {
  340. String name = shape->names[targetNode];
  341. mCustomValue = name;
  342. }
  343. else
  344. {
  345. mCustomValue = String("Origin");
  346. }
  347. setValue( mCustomValue );
  348. return;
  349. }
  350. }
  351. }
  352. setValue("Not Mounted");
  353. mMenu->setActive(false);
  354. }
  355. void GuiInspectorNodeListField::setDoc( const char* doc )
  356. {
  357. mDoc = StringTable->insert( doc, true );
  358. }
  359. void GuiInspectorNodeListField::setToolTip( StringTableEntry data )
  360. {
  361. static StringTableEntry sTooltipProfile = StringTable->insert( "tooltipProfile" );
  362. static StringTableEntry sHoverTime = StringTable->insert( "hovertime" );
  363. static StringTableEntry sTooltip = StringTable->insert( "tooltip" );
  364. mEdit->setDataField( sTooltipProfile, NULL, "GuiToolTipProfile" );
  365. mEdit->setDataField( sHoverTime, NULL, "1000" );
  366. mEdit->setDataField( sTooltip, NULL, data );
  367. }
  368. bool GuiInspectorNodeListField::onAdd()
  369. {
  370. if( !Parent::onAdd() )
  371. return false;
  372. return true;
  373. }
  374. void GuiInspectorNodeListField::setInspectorField( AbstractClassRep::Field *field,
  375. StringTableEntry caption,
  376. const char*arrayIndex )
  377. {
  378. // Override the base just to be sure it doesn't get called.
  379. // We don't use an AbstractClassRep::Field...
  380. mField = field;
  381. mCaption = field->pFieldname;
  382. mDoc = field->pFieldDocs;
  383. }
  384. GuiControl* GuiInspectorNodeListField::constructEditControl()
  385. {
  386. GuiControl* retCtrl = new GuiPopUpMenuCtrl();
  387. mMenu = dynamic_cast<GuiPopUpMenuCtrl*>(retCtrl);
  388. static StringTableEntry sProfile = StringTable->insert( "profile" );
  389. retCtrl->setDataField( sProfile, NULL, "ToolsGuiPopUpMenuEditProfile" );
  390. // Register the object
  391. retCtrl->registerObject();
  392. char szBuffer[512];
  393. dSprintf( szBuffer, 512, "%d.apply( %d.getText() );", getId(), mMenu->getId() );
  394. mMenu->setField("Command", szBuffer );
  395. return retCtrl;
  396. }
  397. void GuiInspectorNodeListField::setValue( const char* newValue )
  398. {
  399. GuiPopUpMenuCtrl *ctrl = dynamic_cast<GuiPopUpMenuCtrl*>( mEdit );
  400. if( ctrl != NULL )
  401. ctrl->setText( newValue );
  402. }
  403. void GuiInspectorNodeListField::_executeSelectedCallback()
  404. {
  405. }
  406. void GuiInspectorNodeListField::setTargetEntity(SimObjectPtr<Entity> target)
  407. {
  408. mTargetEntity = target;
  409. }