LevelAsset.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef LEVEL_ASSET_H
  23. #include "LevelAsset.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assets/assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. #ifndef _ASSET_PTR_H_
  35. #include "assets/assetPtr.h"
  36. #endif
  37. // Debug Profiling.
  38. #include "platform/profiler.h"
  39. #include "gfx/gfxDrawUtil.h"
  40. #include "T3D/SubScene.h"
  41. //-----------------------------------------------------------------------------
  42. IMPLEMENT_CONOBJECT(LevelAsset);
  43. ConsoleType(LevelAssetPtr, TypeLevelAssetPtr, const char*, "")
  44. //-----------------------------------------------------------------------------
  45. ConsoleGetType(TypeLevelAssetPtr)
  46. {
  47. // Fetch asset Id.
  48. return *((const char**)(dptr));
  49. }
  50. //-----------------------------------------------------------------------------
  51. ConsoleSetType(TypeLevelAssetPtr)
  52. {
  53. // Was a single argument specified?
  54. if (argc == 1)
  55. {
  56. // Yes, so fetch field value.
  57. *((const char**)dptr) = StringTable->insert(argv[0]);
  58. return;
  59. }
  60. // Warn.
  61. Con::warnf("(TypeLevelAssetPtr) - Cannot set multiple args to a single asset.");
  62. }
  63. //-----------------------------------------------------------------------------
  64. ConsoleType(assetIdString, TypeLevelAssetId, const char*, "")
  65. ConsoleGetType(TypeLevelAssetId)
  66. {
  67. // Fetch asset Id.
  68. return *((const char**)(dptr));
  69. }
  70. ConsoleSetType(TypeLevelAssetId)
  71. {
  72. // Was a single argument specified?
  73. if (argc == 1)
  74. {
  75. *((const char**)dptr) = StringTable->insert(argv[0]);
  76. return;
  77. }
  78. // Warn.
  79. Con::warnf("(TypeLevelAssetId) - Cannot set multiple args to a single asset.");
  80. }
  81. //-----------------------------------------------------------------------------
  82. LevelAsset::LevelAsset() : AssetBase()
  83. {
  84. mLevelName = StringTable->EmptyString();
  85. mLevelFile = StringTable->EmptyString();
  86. mPostFXPresetFile = StringTable->EmptyString();
  87. mDecalsFile = StringTable->EmptyString();
  88. mForestFile = StringTable->EmptyString();
  89. mNavmeshFile = StringTable->EmptyString();
  90. mLevelPath = StringTable->EmptyString();
  91. mPostFXPresetPath = StringTable->EmptyString();
  92. mDecalsPath = StringTable->EmptyString();
  93. mForestPath = StringTable->EmptyString();
  94. mNavmeshPath = StringTable->EmptyString();
  95. mGameModesNames = StringTable->EmptyString();
  96. mEditorFile = StringTable->EmptyString();
  97. mBakedSceneFile = StringTable->EmptyString();
  98. mPreviewImageAssetId = StringTable->EmptyString();
  99. mPreviewImageAsset = StringTable->EmptyString();
  100. }
  101. //-----------------------------------------------------------------------------
  102. LevelAsset::~LevelAsset()
  103. {
  104. }
  105. //-----------------------------------------------------------------------------
  106. void LevelAsset::initPersistFields()
  107. {
  108. docsURL;
  109. // Call parent.
  110. Parent::initPersistFields();
  111. addProtectedField("LevelFile", TypeAssetLooseFilePath, Offset(mLevelFile, LevelAsset),
  112. &setLevelFile, &getLevelFile, "Path to the actual level file.");
  113. addField("LevelName", TypeString, Offset(mLevelName, LevelAsset), "Human-friendly name for the level.");
  114. addProtectedField("PostFXPresetFile", TypeAssetLooseFilePath, Offset(mPostFXPresetFile, LevelAsset),
  115. &setPostFXPresetFile, &getPostFXPresetFile, "Path to the level's postFXPreset.");
  116. addProtectedField("DecalsFile", TypeAssetLooseFilePath, Offset(mDecalsFile, LevelAsset),
  117. &setDecalsFile, &getDecalsFile, "Path to the decals cache file.");
  118. addProtectedField("ForestFile", TypeAssetLooseFilePath, Offset(mForestFile, LevelAsset),
  119. &setForestFile, &getForestFile, "Path to the Forest cache file.");
  120. addProtectedField("NavmeshFile", TypeAssetLooseFilePath, Offset(mNavmeshFile, LevelAsset),
  121. &setNavmeshFile, &getNavmeshFile, "Path to the navmesh file.");
  122. addProtectedField("EditorFile", TypeAssetLooseFilePath, Offset(mEditorFile, LevelAsset),
  123. &setEditorFile, &getEditorFile, "Path to the level file with objects that were removed as part of the baking process. Loaded when the editor is loaded for ease of editing.");
  124. addProtectedField("BakedSceneFile", TypeAssetLooseFilePath, Offset(mBakedSceneFile, LevelAsset),
  125. &setBakedSceneFile, &getBakedSceneFile, "Path to the level file with the objects generated as part of the baking process");
  126. addField("gameModesNames", TypeString, Offset(mGameModesNames, LevelAsset), "Name of the Game Mode to be used with this level");
  127. }
  128. //------------------------------------------------------------------------------
  129. void LevelAsset::copyTo(SimObject* object)
  130. {
  131. // Call to parent.
  132. Parent::copyTo(object);
  133. }
  134. //
  135. void LevelAsset::initializeAsset()
  136. {
  137. // Call parent.
  138. Parent::initializeAsset();
  139. loadAsset();
  140. }
  141. void LevelAsset::onAssetRefresh(void)
  142. {
  143. loadAsset();
  144. }
  145. void LevelAsset::loadAsset()
  146. {
  147. // Ensure the image-file is expanded.
  148. mLevelPath = getOwned() ? expandAssetFilePath(mLevelFile) : mLevelPath;
  149. mPostFXPresetPath = getOwned() ? expandAssetFilePath(mPostFXPresetFile) : mPostFXPresetPath;
  150. mDecalsPath = getOwned() ? expandAssetFilePath(mDecalsFile) : mDecalsPath;
  151. mForestPath = getOwned() ? expandAssetFilePath(mForestFile) : mForestPath;
  152. mNavmeshPath = getOwned() ? expandAssetFilePath(mNavmeshFile) : mNavmeshPath;
  153. StringTableEntry previewImageAssetId = getAssetDependencyField("previewImageAsset");
  154. if (previewImageAssetId != StringTable->EmptyString())
  155. {
  156. mPreviewImageAssetId = previewImageAssetId;
  157. mPreviewImageAsset = mPreviewImageAssetId;
  158. }
  159. }
  160. //
  161. void LevelAsset::setLevelFile(const char* pLevelFile)
  162. {
  163. // Sanity!
  164. AssertFatal(pLevelFile != NULL, "Cannot use a NULL level file.");
  165. // Fetch image file.
  166. pLevelFile = StringTable->insert(pLevelFile, true);
  167. // Ignore no change,
  168. if (pLevelFile == mLevelFile)
  169. return;
  170. // Update.
  171. mLevelFile = getOwned() ? expandAssetFilePath(pLevelFile) : pLevelFile;
  172. // Refresh the asset.
  173. refreshAsset();
  174. }
  175. StringTableEntry LevelAsset::getPreviewImageAsset() const
  176. {
  177. return mPreviewImageAssetId;
  178. }
  179. StringTableEntry LevelAsset::getPreviewImagePath(void) const
  180. {
  181. if (mPreviewImageAsset.notNull())
  182. {
  183. return mPreviewImageAsset->getImageFile();
  184. }
  185. return StringTable->EmptyString();
  186. }
  187. void LevelAsset::setEditorFile(const char* pEditorFile)
  188. {
  189. // Sanity!
  190. AssertFatal(pEditorFile != NULL, "Cannot use a NULL level file.");
  191. // Fetch image file.
  192. pEditorFile = StringTable->insert(pEditorFile, true);
  193. // Ignore no change,
  194. if (pEditorFile == mEditorFile)
  195. return;
  196. // Update.
  197. mEditorFile = getOwned() ? expandAssetFilePath(pEditorFile) : pEditorFile;
  198. // Refresh the asset.
  199. refreshAsset();
  200. }
  201. void LevelAsset::setBakedSceneFile(const char* pBakedSceneFile)
  202. {
  203. // Sanity!
  204. AssertFatal(pBakedSceneFile != NULL, "Cannot use a NULL level file.");
  205. // Fetch image file.
  206. pBakedSceneFile = StringTable->insert(pBakedSceneFile, true);
  207. // Ignore no change,
  208. if (pBakedSceneFile == mBakedSceneFile)
  209. return;
  210. // Update.
  211. mBakedSceneFile = getOwned() ? expandAssetFilePath(pBakedSceneFile) : pBakedSceneFile;
  212. // Refresh the asset.
  213. refreshAsset();
  214. }
  215. void LevelAsset::setPostFXPresetFile(const char* pPostFXPresetFile)
  216. {
  217. // Sanity!
  218. AssertFatal(pPostFXPresetFile != NULL, "Cannot use a NULL postFX preset file.");
  219. // Fetch file.
  220. pPostFXPresetFile = StringTable->insert(pPostFXPresetFile, true);
  221. // Ignore no change,
  222. if (pPostFXPresetFile == mPostFXPresetFile)
  223. return;
  224. // Update.
  225. mPostFXPresetFile = getOwned() ? expandAssetFilePath(pPostFXPresetFile) : pPostFXPresetFile;
  226. // Refresh the asset.
  227. refreshAsset();
  228. }
  229. void LevelAsset::setDecalsFile(const char* pDecalsFile)
  230. {
  231. // Sanity!
  232. AssertFatal(pDecalsFile != NULL, "Cannot use a NULL decals file.");
  233. // Fetch file.
  234. pDecalsFile = StringTable->insert(pDecalsFile, true);
  235. // Ignore no change,
  236. if (pDecalsFile == mDecalsFile)
  237. return;
  238. // Update.
  239. mDecalsFile = getOwned() ? expandAssetFilePath(pDecalsFile) : pDecalsFile;
  240. // Refresh the asset.
  241. refreshAsset();
  242. }
  243. void LevelAsset::setForestFile(const char* pForestFile)
  244. {
  245. // Sanity!
  246. AssertFatal(pForestFile != NULL, "Cannot use a NULL decals file.");
  247. // Fetch file.
  248. pForestFile = StringTable->insert(pForestFile, true);
  249. // Ignore no change,
  250. if (pForestFile == mForestFile)
  251. return;
  252. // Update.
  253. mForestFile = getOwned() ? expandAssetFilePath(pForestFile) : pForestFile;
  254. // Refresh the asset.
  255. refreshAsset();
  256. }
  257. void LevelAsset::setNavmeshFile(const char* pNavmeshFile)
  258. {
  259. // Sanity!
  260. AssertFatal(pNavmeshFile != NULL, "Cannot use a NULL Navmesh file.");
  261. // Fetch file.
  262. pNavmeshFile = StringTable->insert(pNavmeshFile, true);
  263. // Ignore no change,
  264. if (pNavmeshFile == mNavmeshFile)
  265. return;
  266. // Update.
  267. mNavmeshFile = getOwned() ? expandAssetFilePath(pNavmeshFile) : pNavmeshFile;
  268. // Refresh the asset.
  269. refreshAsset();
  270. }
  271. void LevelAsset::loadDependencies()
  272. {
  273. //First, load any material, animation, etc assets we may be referencing in our asset
  274. // Find any asset dependencies.
  275. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  276. // Does the asset have any dependencies?
  277. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  278. {
  279. // Iterate all dependencies.
  280. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  281. {
  282. //Force it to be loaded by acquiring it
  283. StringTableEntry assetId = assetDependenciesItr->value;
  284. mAssetDependencies.push_back(AssetDatabase.acquireAsset<AssetBase>(assetId));
  285. // Next dependency.
  286. assetDependenciesItr++;
  287. }
  288. }
  289. }
  290. void LevelAsset::unloadDependencies()
  291. {
  292. for (U32 i = 0; i < mAssetDependencies.size(); i++)
  293. {
  294. AssetBase* assetDef = mAssetDependencies[i];
  295. AssetDatabase.releaseAsset(assetDef->getAssetId());
  296. }
  297. }
  298. DefineEngineMethod(LevelAsset, getLevelPath, const char*, (), ,
  299. "Gets the full path of the asset's defined level file.\n"
  300. "@return The string result of the level path")
  301. {
  302. return object->getLevelPath();
  303. }
  304. DefineEngineMethod(LevelAsset, getPreviewImageAsset, const char*, (), ,
  305. "Gets the full path of the asset's defined preview image file.\n"
  306. "@return The string result of the level preview image path")
  307. {
  308. return object->getPreviewImageAsset();
  309. }
  310. DefineEngineMethod(LevelAsset, getPreviewImagePath, const char*, (), ,
  311. "Gets the full path of the asset's defined preview image file.\n"
  312. "@return The string result of the level preview image path")
  313. {
  314. return object->getPreviewImagePath();
  315. }
  316. DefineEngineMethod(LevelAsset, getPostFXPresetPath, const char*, (), ,
  317. "Gets the full path of the asset's defined postFX preset file.\n"
  318. "@return The string result of the postFX preset path")
  319. {
  320. return object->getPostFXPresetPath();
  321. }
  322. DefineEngineMethod(LevelAsset, getDecalsPath, const char*, (), ,
  323. "Gets the full path of the asset's defined decal file.\n"
  324. "@return The string result of the decal path")
  325. {
  326. return object->getDecalsPath();
  327. }
  328. DefineEngineMethod(LevelAsset, getForestPath, const char*, (), ,
  329. "Gets the full path of the asset's defined forest file.\n"
  330. "@return The string result of the forest path")
  331. {
  332. return object->getForestPath();
  333. }
  334. DefineEngineMethod(LevelAsset, getNavmeshPath, const char*, (), ,
  335. "Gets the full path of the asset's defined navmesh file.\n"
  336. "@return The string result of the navmesh path")
  337. {
  338. return object->getNavmeshPath();
  339. }
  340. DefineEngineMethod(LevelAsset, loadDependencies, void, (), ,
  341. "Initiates the loading of asset dependencies for this level.")
  342. {
  343. return object->loadDependencies();
  344. }
  345. DefineEngineMethod(LevelAsset, unloadDependencies, void, (), ,
  346. "Initiates the unloading of previously loaded asset dependencies for this level.")
  347. {
  348. return object->unloadDependencies();
  349. }
  350. #ifdef TORQUE_TOOLS
  351. //-----------------------------------------------------------------------------
  352. // GuiInspectorTypeAssetId
  353. //-----------------------------------------------------------------------------
  354. IMPLEMENT_CONOBJECT(GuiInspectorTypeLevelAssetPtr);
  355. ConsoleDocClass(GuiInspectorTypeLevelAssetPtr,
  356. "@brief Inspector field type for Shapes\n\n"
  357. "Editor use only.\n\n"
  358. "@internal"
  359. );
  360. void GuiInspectorTypeLevelAssetPtr::consoleInit()
  361. {
  362. Parent::consoleInit();
  363. ConsoleBaseType::getType(TypeLevelAssetPtr)->setInspectorFieldType("GuiInspectorTypeLevelAssetPtr");
  364. }
  365. GuiControl* GuiInspectorTypeLevelAssetPtr::constructEditControl()
  366. {
  367. // Create base filename edit controls
  368. GuiControl* retCtrl = Parent::constructEditControl();
  369. if (retCtrl == NULL)
  370. return retCtrl;
  371. // Change filespec
  372. char szBuffer[512];
  373. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"LevelAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  374. getIdString());
  375. mBrowseButton->setField("Command", szBuffer);
  376. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  377. // Create "Open in Editor" button
  378. mEditButton = new GuiBitmapButtonCtrl();
  379. dSprintf(szBuffer, sizeof(szBuffer), "$createAndAssignField = %s; AssetBrowser.setupCreateNewAsset(\"LevelAsset\", AssetBrowser.selectedModule, \"createAndAssignLevelAsset\");",
  380. getIdString());
  381. mEditButton->setField("Command", szBuffer);
  382. char bitmapName[512] = "ToolsModule:iconAdd_image";
  383. mEditButton->setBitmap(StringTable->insert(bitmapName));
  384. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  385. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  386. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  387. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Test play this sound");
  388. mEditButton->registerObject();
  389. addObject(mEditButton);
  390. return retCtrl;
  391. }
  392. bool GuiInspectorTypeLevelAssetPtr::updateRects()
  393. {
  394. S32 dividerPos, dividerMargin;
  395. mInspector->getDivider(dividerPos, dividerMargin);
  396. Point2I fieldExtent = getExtent();
  397. Point2I fieldPos = getPosition();
  398. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  399. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  400. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  401. if (mBrowseButton != NULL)
  402. {
  403. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  404. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  405. }
  406. if (mEditButton != NULL)
  407. {
  408. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  409. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  410. }
  411. return resized;
  412. }
  413. IMPLEMENT_CONOBJECT(GuiInspectorTypeLevelAssetId);
  414. ConsoleDocClass(GuiInspectorTypeLevelAssetId,
  415. "@brief Inspector field type for Levels\n\n"
  416. "Editor use only.\n\n"
  417. "@internal"
  418. );
  419. void GuiInspectorTypeLevelAssetId::consoleInit()
  420. {
  421. Parent::consoleInit();
  422. ConsoleBaseType::getType(TypeLevelAssetId)->setInspectorFieldType("GuiInspectorTypeLevelAssetId");
  423. }
  424. #endif