datablockField.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/simBase.h"
  23. #include "console/simDatablock.h"
  24. #include "gui/editor/guiInspector.h"
  25. #include "gui/editor/inspector/datablockField.h"
  26. #include "gui/editor/inspector/group.h"
  27. #include "gui/buttons/guiIconButtonCtrl.h"
  28. #include "gui/editor/inspector/datablockField.h"
  29. #include "sfx/sfxTypes.h"
  30. #include "sfx/sfxDescription.h"
  31. #include "sfx/sfxEnvironment.h"
  32. #include "sfx/sfxAmbience.h"
  33. #include "sfx/sfxTrack.h"
  34. #include "T3D/gameBase/gameBase.h"
  35. //-----------------------------------------------------------------------------
  36. // GuiInspectorDatablockField
  37. // Field construction for datablock types
  38. //-----------------------------------------------------------------------------
  39. IMPLEMENT_CONOBJECT(GuiInspectorDatablockField);
  40. ConsoleDocClass( GuiInspectorDatablockField,
  41. "@brief Custom field type for datablock enumeration.\n\n"
  42. "Editor use only.\n\n"
  43. "@internal"
  44. );
  45. GuiInspectorDatablockField::GuiInspectorDatablockField( StringTableEntry className )
  46. {
  47. setClassName( className );
  48. }
  49. void GuiInspectorDatablockField::setClassName( StringTableEntry className )
  50. {
  51. if( !className || !className[ 0 ] )
  52. mDesiredClass = NULL;
  53. else
  54. {
  55. mDesiredClass = AbstractClassRep::findClassRep( className );
  56. if( !mDesiredClass )
  57. Con::errorf( "GuiInspectorDatablockField::setClassName - no class '%s' found!", className );
  58. }
  59. }
  60. void GuiInspectorDatablockField::_populateMenu( GuiPopUpMenuCtrlEx* menu )
  61. {
  62. menu->setCanSearch(true);
  63. menu->addScheme( 1, ColorI( 80, 0, 0, 255 ), ColorI( 80, 0, 0, 255 ), ColorI( 80, 0, 0, 255 ) ); // For client-only coloring.
  64. menu->addEntry( "", 0 ); // For unsetting.
  65. SimSet* set = _getDatablockSet();
  66. U32 id = 1;
  67. //We can do some special filtering here if it's derived from GameBase a la categories
  68. if(mDesiredClass->isSubclassOf(AbstractClassRep::findClassRep("GameBaseData")))
  69. {
  70. //First, do categories
  71. Vector<String> categories;
  72. for (SimSet::iterator iter = set->begin(); iter != set->end(); ++iter)
  73. {
  74. SimDataBlock* datablock = dynamic_cast<SimDataBlock*>(*iter);
  75. // Skip non-datablocks if we somehow encounter them.
  76. if (!datablock)
  77. continue;
  78. if (datablock && (!mDesiredClass || datablock->getClassRep()->isClass(mDesiredClass)))
  79. {
  80. GameBaseData *data = dynamic_cast<GameBaseData*>(datablock);
  81. if(data)
  82. {
  83. String category = data->mCategory;
  84. if(category.isNotEmpty() && (categories.empty() || categories.find_next(category) == -1))
  85. categories.push_back(category);
  86. }
  87. }
  88. }
  89. if (categories.size() > 0)
  90. {
  91. categories.push_back("No Category");
  92. //Now that we have our categories, lets populate our list
  93. for (Vector<String>::iterator catIter = categories.begin(); catIter != categories.end(); ++catIter)
  94. {
  95. String categoryName = String::ToLower(catIter->c_str());
  96. if (categoryName != String::EmptyString)
  97. {
  98. menu->addCategory(catIter->c_str());
  99. id++;
  100. }
  101. for (SimSet::iterator iter = set->begin(); iter != set->end(); ++iter)
  102. {
  103. GameBaseData* datablock = dynamic_cast<GameBaseData*>(*iter);
  104. // Skip non-datablocks if we somehow encounter them.
  105. if (!datablock)
  106. continue;
  107. String dbCategory = String(datablock->mCategory).isEmpty() ? String("no category") : String::ToLower(datablock->mCategory);
  108. if (datablock && (!mDesiredClass || datablock->getClassRep()->isClass(mDesiredClass)) && (dbCategory == categoryName))
  109. {
  110. menu->addEntry(datablock->getName(), id++, datablock->isClientOnly() ? 1 : 0, true);
  111. }
  112. }
  113. }
  114. return;
  115. }
  116. }
  117. for (SimSet::iterator iter = set->begin(); iter != set->end(); ++iter)
  118. {
  119. SimDataBlock* datablock = dynamic_cast<SimDataBlock*>(*iter);
  120. // Skip non-datablocks if we somehow encounter them.
  121. if (!datablock)
  122. continue;
  123. // Ok, now we have to figure inheritance info.
  124. if (datablock && (!mDesiredClass || datablock->getClassRep()->isClass(mDesiredClass)))
  125. menu->addEntry(datablock->getName(), id++, datablock->isClientOnly() ? 1 : 0);
  126. }
  127. menu->sort();
  128. }
  129. GuiControl* GuiInspectorDatablockField::constructEditControl()
  130. {
  131. // Create base filename edit controls
  132. GuiControl* retCtrl = Parent::constructEditControl();
  133. if (retCtrl == NULL)
  134. return retCtrl;
  135. char szBuffer[512];
  136. // Create "Open in Editor" button
  137. mEditButton = new GuiButtonCtrl();
  138. dSprintf(szBuffer, sizeof(szBuffer), "DatablockEditorPlugin.openDatablock(%d.getText());", retCtrl->getId());
  139. mEditButton->setField("Command", szBuffer);
  140. mEditButton->setText("...");
  141. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiInspectorButtonProfile");
  142. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  143. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  144. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Edit this datablock");
  145. mEditButton->registerObject();
  146. addObject(mEditButton);
  147. //Add add button
  148. mAddButton = new GuiBitmapButtonCtrl();
  149. if(mDesiredClass == NULL)
  150. return retCtrl;
  151. dSprintf(szBuffer, sizeof(szBuffer), "DatablockEditorPlugin.createNewDatablockOfType(%s, %d.getText());", mDesiredClass->getClassName(), retCtrl->getId());
  152. mAddButton->setField("Command", szBuffer);
  153. char addBtnBitmapName[512] = "ToolsModule:iconAdd_Image";
  154. mAddButton->setBitmap(StringTable->insert(addBtnBitmapName));
  155. mAddButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  156. mAddButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  157. mAddButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  158. mAddButton->setDataField(StringTable->insert("tooltip"), NULL, "Create new datablock");
  159. mAddButton->registerObject();
  160. addObject(mAddButton);
  161. return retCtrl;
  162. }
  163. bool GuiInspectorDatablockField::updateRects()
  164. {
  165. S32 dividerPos, dividerMargin;
  166. mInspector->getDivider(dividerPos, dividerMargin);
  167. Point2I fieldExtent = getExtent();
  168. Point2I fieldPos = getPosition();
  169. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  170. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  171. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  172. if (mEditButton != NULL)
  173. {
  174. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  175. resized |= mEditButton->resize(mBrowseRect.point, mBrowseRect.extent);
  176. }
  177. if (mAddButton != NULL)
  178. {
  179. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  180. resized |= mAddButton->resize(shapeEdRect.point, shapeEdRect.extent);
  181. }
  182. return resized;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // GuiInspectorTypeSFXDescriptionName
  186. //-----------------------------------------------------------------------------
  187. IMPLEMENT_CONOBJECT(GuiInspectorTypeSFXDescriptionName);
  188. ConsoleDocClass( GuiInspectorTypeSFXDescriptionName,
  189. "@brief Inspector field type for SFXDescriptionName\n\n"
  190. "Editor use only.\n\n"
  191. "@internal"
  192. );
  193. void GuiInspectorTypeSFXDescriptionName::consoleInit()
  194. {
  195. Parent::consoleInit();
  196. ConsoleBaseType::getType( TypeSFXDescriptionName )->setInspectorFieldType( "GuiInspectorTypeSFXDescriptionName" );
  197. }
  198. //-----------------------------------------------------------------------------
  199. // GuiInspectorTypeSFXTrackName
  200. //-----------------------------------------------------------------------------
  201. IMPLEMENT_CONOBJECT(GuiInspectorTypeSFXTrackName);
  202. ConsoleDocClass( GuiInspectorTypeSFXTrackName,
  203. "@brief Inspector field type for SFXTrackName\n\n"
  204. "Editor use only.\n\n"
  205. "@internal"
  206. );
  207. void GuiInspectorTypeSFXTrackName::consoleInit()
  208. {
  209. Parent::consoleInit();
  210. ConsoleBaseType::getType( TypeSFXTrackName )->setInspectorFieldType( "GuiInspectorTypeSFXTrackName" );
  211. }
  212. //-----------------------------------------------------------------------------
  213. // GuiInspectorTypeSFXEnvironmentName
  214. //-----------------------------------------------------------------------------
  215. IMPLEMENT_CONOBJECT(GuiInspectorTypeSFXEnvironmentName);
  216. ConsoleDocClass( GuiInspectorTypeSFXEnvironmentName,
  217. "@brief Inspector field type for SFXEnvironment\n\n"
  218. "Editor use only.\n\n"
  219. "@internal"
  220. );
  221. void GuiInspectorTypeSFXEnvironmentName::consoleInit()
  222. {
  223. Parent::consoleInit();
  224. ConsoleBaseType::getType( TypeSFXEnvironmentName )->setInspectorFieldType( "GuiInspectorTypeSFXEnvironmentName" );
  225. }
  226. //-----------------------------------------------------------------------------
  227. // GuiInspectorTypeSFXAmbienceName
  228. //-----------------------------------------------------------------------------
  229. IMPLEMENT_CONOBJECT(GuiInspectorTypeSFXAmbienceName);
  230. ConsoleDocClass( GuiInspectorTypeSFXAmbienceName,
  231. "@brief Inspector field type for SFXAmbience\n\n"
  232. "Editor use only.\n\n"
  233. "@internal"
  234. );
  235. void GuiInspectorTypeSFXAmbienceName::consoleInit()
  236. {
  237. Parent::consoleInit();
  238. ConsoleBaseType::getType( TypeSFXAmbienceName )->setInspectorFieldType( "GuiInspectorTypeSFXAmbienceName" );
  239. }