mountingGroup.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 bGrabItems = false;
  109. AbstractClassRep* commonAncestorClass = findCommonAncestorClass();
  110. AbstractClassRep::FieldList& fieldList = commonAncestorClass->mFieldList;
  111. for( AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  112. itr != fieldList.end(); ++ itr )
  113. {
  114. AbstractClassRep::Field* field = &( *itr );
  115. if( field->type == AbstractClassRep::StartGroupFieldType )
  116. {
  117. // If we're dealing with general fields, always set grabItems to true (to skip them)
  118. if( bNoGroup == true )
  119. bGrabItems = true;
  120. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  121. bGrabItems = true;
  122. continue;
  123. }
  124. else if ( field->type == AbstractClassRep::EndGroupFieldType )
  125. {
  126. // If we're dealing with general fields, always set grabItems to false (to grab them)
  127. if( bNoGroup == true )
  128. bGrabItems = false;
  129. else if( dStricmp( field->pGroupname, mCaption ) == 0 )
  130. bGrabItems = false;
  131. continue;
  132. }
  133. // Skip field if it has the HideInInspectors flag set.
  134. if( field->flag.test( AbstractClassRep::FIELD_HideInInspectors ) )
  135. continue;
  136. if( ( bGrabItems == true || ( bNoGroup == true && bGrabItems == false ) ) && itr->type != AbstractClassRep::DeprecatedFieldType )
  137. {
  138. if( bNoGroup == true && bGrabItems == true )
  139. continue;
  140. // If the field already exists, just update it
  141. GuiInspectorField *fieldGui = findField( field->pFieldname );
  142. if ( fieldGui != NULL )
  143. {
  144. fieldGui->updateValue();
  145. continue;
  146. }
  147. bNewItems = true;
  148. if(field->pFieldname == StringTable->insert("mountNode"))
  149. {
  150. fieldGui = new GuiInspectorNodeListField();
  151. Entity* e = dynamic_cast<Entity*>(mParent->getInspectObject(0));
  152. if(e)
  153. (dynamic_cast<GuiInspectorNodeListField*>(fieldGui))->setTargetEntity(e);
  154. }
  155. else
  156. {
  157. fieldGui = constructField( field->type );
  158. if ( fieldGui == NULL )
  159. fieldGui = new GuiInspectorField();
  160. }
  161. fieldGui->init( mParent, this );
  162. fieldGui->setInspectorField( field );
  163. if( fieldGui->registerObject() )
  164. {
  165. #ifdef DEBUG_SPEW
  166. Platform::outputDebugString( "[GuiInspectorGroup] Adding field '%s'",
  167. field->pFieldname );
  168. #endif
  169. mChildren.push_back( fieldGui );
  170. mStack->addObject( fieldGui );
  171. }
  172. else
  173. {
  174. SAFE_DELETE( fieldGui );
  175. }
  176. }
  177. }
  178. mStack->freeze(false);
  179. mStack->updatePanes();
  180. // If we've no new items, there's no need to resize anything!
  181. if( bNewItems == false && !mChildren.empty() )
  182. return true;
  183. sizeToContents();
  184. setUpdate();
  185. return true;
  186. }
  187. void GuiInspectorMountingGroup::updateAllFields()
  188. {
  189. // We overload this to just reinspect the group.
  190. inspectGroup();
  191. }
  192. void GuiInspectorMountingGroup::onMouseMove(const GuiEvent &event)
  193. {
  194. //mParent->mOverDivider = false;
  195. }
  196. DefineEngineMethod(GuiInspectorMountingGroup, inspectGroup, bool, (),, "Refreshes the dynamic fields in the inspector.")
  197. {
  198. return object->inspectGroup();
  199. }
  200. void GuiInspectorMountingGroup::clearFields()
  201. {
  202. }
  203. bool GuiInspectorMountingGroup::resize( const Point2I &newPosition, const Point2I &newExtent )
  204. {
  205. if ( !Parent::resize( newPosition, newExtent ) )
  206. return false;
  207. //check if we're set up yet
  208. if(!targetMountCtrl || !mountCtrl)
  209. //no? bail
  210. return false;
  211. targetMountCtrl->setExtent(newExtent.x, 18);
  212. mountCtrl->setExtent(newExtent.x, 18);
  213. S32 dividerPos, dividerMargin;
  214. mParentInspector->getDivider( dividerPos, dividerMargin );
  215. Point2I fieldExtent = Point2I(newExtent.x, 18);
  216. Point2I fieldPos = Point2I(newExtent.x, 18);
  217. S32 editWidth = dividerPos - dividerMargin;
  218. targetMountText->setPosition(0,0);
  219. targetMountText->setExtent(fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  220. targetMountNode->setPosition(fieldExtent.x - dividerPos + dividerMargin, 1);
  221. targetMountNode->setExtent(editWidth, fieldExtent.y - 1);
  222. mountText->setPosition(0,0);
  223. mountText->setExtent(fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  224. mountNode->setPosition(fieldExtent.x - dividerPos + dividerMargin, 1);
  225. mountNode->setExtent(editWidth, fieldExtent.y - 1);
  226. return true;
  227. }
  228. SimFieldDictionary::Entry* GuiInspectorMountingGroup::findDynamicFieldInDictionary( StringTableEntry fieldName )
  229. {
  230. SimFieldDictionary * fieldDictionary = mParent->getInspectObject()->getFieldDictionary();
  231. for(SimFieldDictionaryIterator ditr(fieldDictionary); *ditr; ++ditr)
  232. {
  233. SimFieldDictionary::Entry * entry = (*ditr);
  234. if( entry->slotName == fieldName )
  235. return entry;
  236. }
  237. return NULL;
  238. }
  239. void GuiInspectorMountingGroup::addDynamicField()
  240. {
  241. }
  242. AbstractClassRep::Field* GuiInspectorMountingGroup::findObjectComponentField(Component* target, String fieldName)
  243. {
  244. AbstractClassRep::FieldList& fieldList = target->getClassRep()->mFieldList;
  245. for( AbstractClassRep::FieldList::iterator itr = fieldList.begin();
  246. itr != fieldList.end(); ++ itr )
  247. {
  248. AbstractClassRep::Field* field = &( *itr );
  249. String fldNm(field->pFieldname);
  250. if(fldNm == fieldName)
  251. return field;
  252. }
  253. return NULL;
  254. }
  255. DefineEngineMethod(GuiInspectorMountingGroup, addDynamicField, void, (), , "obj.addDynamicField();")
  256. {
  257. object->addDynamicField();
  258. }
  259. DefineEngineMethod(GuiInspectorMountingGroup, removeDynamicField, void, (), , "")
  260. {
  261. }
  262. //
  263. IMPLEMENT_CONOBJECT( GuiInspectorNodeListField );
  264. ConsoleDocClass( GuiInspectorNodeListField,
  265. "@brief A control that allows to edit the custom properties (text) of one or more SimObjects.\n\n"
  266. "Editor use only.\n\n"
  267. "@internal"
  268. );
  269. GuiInspectorNodeListField::GuiInspectorNodeListField( GuiInspector *inspector,
  270. GuiInspectorGroup* parent,
  271. SimFieldDictionary::Entry* field,
  272. SimObjectPtr<Entity> target )
  273. {
  274. mInspector = inspector;
  275. mParent = parent;
  276. setBounds(0,0,100,20);
  277. mTargetEntity = target;
  278. }
  279. GuiInspectorNodeListField::GuiInspectorNodeListField()
  280. {
  281. mInspector = NULL;
  282. mParent = NULL;
  283. }
  284. void GuiInspectorNodeListField::setData( const char* data, bool callbacks )
  285. {
  286. mCustomValue = data;
  287. //We aren't updating any mounting info if we're not mounted already
  288. if(mTargetEntity.getObject())
  289. {
  290. Entity* target = dynamic_cast<Entity*>(mTargetEntity->getObjectMount());
  291. if(target)
  292. {
  293. RenderComponentInterface* renderInterface = target->getComponent<RenderComponentInterface>();
  294. if (renderInterface)
  295. {
  296. if (renderInterface->getShape())
  297. {
  298. S32 nodeIdx = renderInterface->getShape()->findNode(data);
  299. target->mountObject(mTargetEntity, nodeIdx, MatrixF::Identity);
  300. mTargetEntity->setMaskBits(Entity::MountedMask);
  301. }
  302. }
  303. }
  304. }
  305. // Force our edit to update
  306. updateValue();
  307. }
  308. const char* GuiInspectorNodeListField::getData( U32 inspectObjectIndex )
  309. {
  310. return mCustomValue;
  311. }
  312. void GuiInspectorNodeListField::updateValue()
  313. {
  314. mMenu->clear();
  315. //mMenu->addEntry("Origin");
  316. //if(mCustomValue.isEmpty())
  317. if(mTargetEntity.getObject())
  318. {
  319. Entity* target = dynamic_cast<Entity*>(mTargetEntity->getObjectMount());
  320. if(target)
  321. {
  322. mMenu->addEntry("Origin");
  323. mMenu->setActive(true);
  324. RenderComponentInterface* renderInterface = target->getComponent<RenderComponentInterface>();
  325. if (renderInterface)
  326. {
  327. TSShape* shape = renderInterface->getShape();
  328. S32 nodeCount = shape ? shape->nodes.size() : 0;
  329. for(U32 i=0; i < nodeCount; i++)
  330. {
  331. mMenu->addEntry(shape->names[i], i);
  332. }
  333. S32 targetNode = mTargetEntity->getMountNode();
  334. if(targetNode != -1)
  335. {
  336. String name = shape->names[targetNode];
  337. mCustomValue = name;
  338. }
  339. else
  340. {
  341. mCustomValue = String("Origin");
  342. }
  343. setValue( mCustomValue );
  344. return;
  345. }
  346. }
  347. }
  348. setValue("Not Mounted");
  349. mMenu->setActive(false);
  350. }
  351. void GuiInspectorNodeListField::setDoc( const char* doc )
  352. {
  353. mDoc = StringTable->insert( doc, true );
  354. }
  355. void GuiInspectorNodeListField::setToolTip( StringTableEntry data )
  356. {
  357. static StringTableEntry sTooltipProfile = StringTable->insert( "tooltipProfile" );
  358. static StringTableEntry sHoverTime = StringTable->insert( "hovertime" );
  359. static StringTableEntry sTooltip = StringTable->insert( "tooltip" );
  360. mEdit->setDataField( sTooltipProfile, NULL, "GuiToolTipProfile" );
  361. mEdit->setDataField( sHoverTime, NULL, "1000" );
  362. mEdit->setDataField( sTooltip, NULL, data );
  363. }
  364. bool GuiInspectorNodeListField::onAdd()
  365. {
  366. if( !Parent::onAdd() )
  367. return false;
  368. return true;
  369. }
  370. void GuiInspectorNodeListField::setInspectorField( AbstractClassRep::Field *field,
  371. StringTableEntry caption,
  372. const char*arrayIndex )
  373. {
  374. // Override the base just to be sure it doesn't get called.
  375. // We don't use an AbstractClassRep::Field...
  376. mField = field;
  377. mCaption = field->pFieldname;
  378. mDoc = field->pFieldDocs;
  379. }
  380. GuiControl* GuiInspectorNodeListField::constructEditControl()
  381. {
  382. GuiControl* retCtrl = new GuiPopUpMenuCtrl();
  383. mMenu = dynamic_cast<GuiPopUpMenuCtrl*>(retCtrl);
  384. static StringTableEntry sProfile = StringTable->insert( "profile" );
  385. retCtrl->setDataField( sProfile, NULL, "ToolsGuiPopUpMenuEditProfile" );
  386. // Register the object
  387. retCtrl->registerObject();
  388. char szBuffer[512];
  389. dSprintf( szBuffer, 512, "%d.apply( %d.getText() );", getId(), mMenu->getId() );
  390. mMenu->setField("Command", szBuffer );
  391. return retCtrl;
  392. }
  393. void GuiInspectorNodeListField::setValue( const char* newValue )
  394. {
  395. GuiPopUpMenuCtrl *ctrl = dynamic_cast<GuiPopUpMenuCtrl*>( mEdit );
  396. if( ctrl != NULL )
  397. ctrl->setText( newValue );
  398. }
  399. void GuiInspectorNodeListField::_executeSelectedCallback()
  400. {
  401. }
  402. void GuiInspectorNodeListField::setTargetEntity(SimObjectPtr<Entity> target)
  403. {
  404. mTargetEntity = target;
  405. }