LevelAsset.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. //-----------------------------------------------------------------------------
  41. IMPLEMENT_CONOBJECT(LevelAsset);
  42. ConsoleType(LevelAssetPtr, TypeLevelAssetPtr, const char*, "")
  43. //-----------------------------------------------------------------------------
  44. ConsoleGetType(TypeLevelAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. return *((const char**)(dptr));
  48. }
  49. //-----------------------------------------------------------------------------
  50. ConsoleSetType(TypeLevelAssetPtr)
  51. {
  52. // Was a single argument specified?
  53. if (argc == 1)
  54. {
  55. // Yes, so fetch field value.
  56. *((const char**)dptr) = StringTable->insert(argv[0]);
  57. return;
  58. }
  59. // Warn.
  60. Con::warnf("(TypeLevelAssetPtr) - Cannot set multiple args to a single asset.");
  61. }
  62. //-----------------------------------------------------------------------------
  63. ConsoleType(assetIdString, TypeLevelAssetId, const char*, "")
  64. ConsoleGetType(TypeLevelAssetId)
  65. {
  66. // Fetch asset Id.
  67. return *((const char**)(dptr));
  68. }
  69. ConsoleSetType(TypeLevelAssetId)
  70. {
  71. // Was a single argument specified?
  72. if (argc == 1)
  73. {
  74. *((const char**)dptr) = StringTable->insert(argv[0]);
  75. return;
  76. }
  77. // Warn.
  78. Con::warnf("(TypeLevelAssetId) - Cannot set multiple args to a single asset.");
  79. }
  80. //-----------------------------------------------------------------------------
  81. LevelAsset::LevelAsset() : AssetBase(), mIsSubLevel(false)
  82. {
  83. mLevelName = StringTable->EmptyString();
  84. mLevelFile = StringTable->EmptyString();
  85. mPostFXPresetFile = StringTable->EmptyString();
  86. mDecalsFile = StringTable->EmptyString();
  87. mForestFile = StringTable->EmptyString();
  88. mNavmeshFile = StringTable->EmptyString();
  89. mLevelPath = StringTable->EmptyString();
  90. mPostFXPresetPath = StringTable->EmptyString();
  91. mDecalsPath = StringTable->EmptyString();
  92. mForestPath = StringTable->EmptyString();
  93. mNavmeshPath = StringTable->EmptyString();
  94. mGameModesNames = StringTable->EmptyString();
  95. mMainLevelAsset = 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("isSubScene", TypeBool, Offset(mIsSubLevel, LevelAsset), "Is this a sublevel to another Scene");
  127. addField("gameModesNames", TypeString, Offset(mGameModesNames, LevelAsset), "Name of the Game Mode to be used with this level");
  128. }
  129. //------------------------------------------------------------------------------
  130. void LevelAsset::copyTo(SimObject* object)
  131. {
  132. // Call to parent.
  133. Parent::copyTo(object);
  134. }
  135. //
  136. void LevelAsset::initializeAsset()
  137. {
  138. // Call parent.
  139. Parent::initializeAsset();
  140. loadAsset();
  141. }
  142. void LevelAsset::onAssetRefresh(void)
  143. {
  144. loadAsset();
  145. }
  146. void LevelAsset::loadAsset()
  147. {
  148. // Ensure the image-file is expanded.
  149. mLevelPath = getOwned() ? expandAssetFilePath(mLevelFile) : mLevelPath;
  150. mPostFXPresetPath = getOwned() ? expandAssetFilePath(mPostFXPresetFile) : mPostFXPresetPath;
  151. mDecalsPath = getOwned() ? expandAssetFilePath(mDecalsFile) : mDecalsPath;
  152. mForestPath = getOwned() ? expandAssetFilePath(mForestFile) : mForestPath;
  153. mNavmeshPath = getOwned() ? expandAssetFilePath(mNavmeshFile) : mNavmeshPath;
  154. StringTableEntry previewImageAssetId = getAssetDependencyField("previewImageAsset");
  155. if (previewImageAssetId != StringTable->EmptyString())
  156. {
  157. mPreviewImageAssetId = previewImageAssetId;
  158. mPreviewImageAsset = mPreviewImageAssetId;
  159. }
  160. }
  161. //
  162. void LevelAsset::setLevelFile(const char* pLevelFile)
  163. {
  164. // Sanity!
  165. AssertFatal(pLevelFile != NULL, "Cannot use a NULL level file.");
  166. // Fetch image file.
  167. pLevelFile = StringTable->insert(pLevelFile, true);
  168. // Ignore no change,
  169. if (pLevelFile == mLevelFile)
  170. return;
  171. // Update.
  172. mLevelFile = getOwned() ? expandAssetFilePath(pLevelFile) : pLevelFile;
  173. // Refresh the asset.
  174. refreshAsset();
  175. }
  176. StringTableEntry LevelAsset::getPreviewImageAsset() const
  177. {
  178. return mPreviewImageAssetId;
  179. }
  180. StringTableEntry LevelAsset::getPreviewImagePath(void) const
  181. {
  182. if (mPreviewImageAsset.notNull() && mPreviewImageAsset->isAssetValid())
  183. {
  184. return mPreviewImageAsset->getImagePath();
  185. }
  186. return StringTable->EmptyString();
  187. }
  188. void LevelAsset::setEditorFile(const char* pEditorFile)
  189. {
  190. // Sanity!
  191. AssertFatal(pEditorFile != NULL, "Cannot use a NULL level file.");
  192. // Fetch image file.
  193. pEditorFile = StringTable->insert(pEditorFile, true);
  194. // Ignore no change,
  195. if (pEditorFile == mEditorFile)
  196. return;
  197. // Update.
  198. mEditorFile = getOwned() ? expandAssetFilePath(pEditorFile) : pEditorFile;
  199. // Refresh the asset.
  200. refreshAsset();
  201. }
  202. void LevelAsset::setBakedSceneFile(const char* pBakedSceneFile)
  203. {
  204. // Sanity!
  205. AssertFatal(pBakedSceneFile != NULL, "Cannot use a NULL level file.");
  206. // Fetch image file.
  207. pBakedSceneFile = StringTable->insert(pBakedSceneFile, true);
  208. // Ignore no change,
  209. if (pBakedSceneFile == mBakedSceneFile)
  210. return;
  211. // Update.
  212. mBakedSceneFile = getOwned() ? expandAssetFilePath(pBakedSceneFile) : pBakedSceneFile;
  213. // Refresh the asset.
  214. refreshAsset();
  215. }
  216. void LevelAsset::setPostFXPresetFile(const char* pPostFXPresetFile)
  217. {
  218. // Sanity!
  219. AssertFatal(pPostFXPresetFile != NULL, "Cannot use a NULL postFX preset file.");
  220. // Fetch file.
  221. pPostFXPresetFile = StringTable->insert(pPostFXPresetFile, true);
  222. // Ignore no change,
  223. if (pPostFXPresetFile == mPostFXPresetFile)
  224. return;
  225. // Update.
  226. mPostFXPresetFile = getOwned() ? expandAssetFilePath(pPostFXPresetFile) : pPostFXPresetFile;
  227. // Refresh the asset.
  228. refreshAsset();
  229. }
  230. void LevelAsset::setDecalsFile(const char* pDecalsFile)
  231. {
  232. // Sanity!
  233. AssertFatal(pDecalsFile != NULL, "Cannot use a NULL decals file.");
  234. // Fetch file.
  235. pDecalsFile = StringTable->insert(pDecalsFile, true);
  236. // Ignore no change,
  237. if (pDecalsFile == mDecalsFile)
  238. return;
  239. // Update.
  240. mDecalsFile = getOwned() ? expandAssetFilePath(pDecalsFile) : pDecalsFile;
  241. // Refresh the asset.
  242. refreshAsset();
  243. }
  244. void LevelAsset::setForestFile(const char* pForestFile)
  245. {
  246. // Sanity!
  247. AssertFatal(pForestFile != NULL, "Cannot use a NULL decals file.");
  248. // Fetch file.
  249. pForestFile = StringTable->insert(pForestFile, true);
  250. // Ignore no change,
  251. if (pForestFile == mForestFile)
  252. return;
  253. // Update.
  254. mForestFile = getOwned() ? expandAssetFilePath(pForestFile) : pForestFile;
  255. // Refresh the asset.
  256. refreshAsset();
  257. }
  258. void LevelAsset::setNavmeshFile(const char* pNavmeshFile)
  259. {
  260. // Sanity!
  261. AssertFatal(pNavmeshFile != NULL, "Cannot use a NULL Navmesh file.");
  262. // Fetch file.
  263. pNavmeshFile = StringTable->insert(pNavmeshFile, true);
  264. // Ignore no change,
  265. if (pNavmeshFile == mNavmeshFile)
  266. return;
  267. // Update.
  268. mNavmeshFile = getOwned() ? expandAssetFilePath(pNavmeshFile) : pNavmeshFile;
  269. // Refresh the asset.
  270. refreshAsset();
  271. }
  272. void LevelAsset::loadDependencies()
  273. {
  274. //First, load any material, animation, etc assets we may be referencing in our asset
  275. // Find any asset dependencies.
  276. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  277. // Does the asset have any dependencies?
  278. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  279. {
  280. // Iterate all dependencies.
  281. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  282. {
  283. //Force it to be loaded by acquiring it
  284. StringTableEntry assetId = assetDependenciesItr->value;
  285. mAssetDependencies.push_back(AssetDatabase.acquireAsset<AssetBase>(assetId));
  286. // Next dependency.
  287. assetDependenciesItr++;
  288. }
  289. }
  290. }
  291. void LevelAsset::unloadDependencies()
  292. {
  293. for (U32 i = 0; i < mAssetDependencies.size(); i++)
  294. {
  295. AssetBase* assetDef = mAssetDependencies[i];
  296. AssetDatabase.releaseAsset(assetDef->getAssetId());
  297. }
  298. }
  299. DefineEngineMethod(LevelAsset, getLevelPath, const char*, (), ,
  300. "Gets the full path of the asset's defined level file.\n"
  301. "@return The string result of the level path")
  302. {
  303. return object->getLevelPath();
  304. }
  305. DefineEngineMethod(LevelAsset, getPreviewImageAsset, const char*, (), ,
  306. "Gets the full path of the asset's defined preview image file.\n"
  307. "@return The string result of the level preview image path")
  308. {
  309. return object->getPreviewImageAsset();
  310. }
  311. DefineEngineMethod(LevelAsset, getPreviewImagePath, const char*, (), ,
  312. "Gets the full path of the asset's defined preview image file.\n"
  313. "@return The string result of the level preview image path")
  314. {
  315. return object->getPreviewImagePath();
  316. }
  317. DefineEngineMethod(LevelAsset, getPostFXPresetPath, const char*, (), ,
  318. "Gets the full path of the asset's defined postFX preset file.\n"
  319. "@return The string result of the postFX preset path")
  320. {
  321. return object->getPostFXPresetPath();
  322. }
  323. DefineEngineMethod(LevelAsset, getDecalsPath, const char*, (), ,
  324. "Gets the full path of the asset's defined decal file.\n"
  325. "@return The string result of the decal path")
  326. {
  327. return object->getDecalsPath();
  328. }
  329. DefineEngineMethod(LevelAsset, getForestPath, const char*, (), ,
  330. "Gets the full path of the asset's defined forest file.\n"
  331. "@return The string result of the forest path")
  332. {
  333. return object->getForestPath();
  334. }
  335. DefineEngineMethod(LevelAsset, getNavmeshPath, const char*, (), ,
  336. "Gets the full path of the asset's defined navmesh file.\n"
  337. "@return The string result of the navmesh path")
  338. {
  339. return object->getNavmeshPath();
  340. }
  341. DefineEngineMethod(LevelAsset, loadDependencies, void, (), ,
  342. "Initiates the loading of asset dependencies for this level.")
  343. {
  344. return object->loadDependencies();
  345. }
  346. DefineEngineMethod(LevelAsset, unloadDependencies, void, (), ,
  347. "Initiates the unloading of previously loaded asset dependencies for this level.")
  348. {
  349. return object->unloadDependencies();
  350. }
  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. }