MaterialAsset.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 MATERIALASSET_H
  23. #include "MaterialAsset.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. //-----------------------------------------------------------------------------
  38. IMPLEMENT_CONOBJECT(MaterialAsset);
  39. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  40. //-----------------------------------------------------------------------------
  41. ConsoleGetType(TypeMaterialAssetPtr)
  42. {
  43. // Fetch asset Id.
  44. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  45. }
  46. //-----------------------------------------------------------------------------
  47. ConsoleSetType(TypeMaterialAssetPtr)
  48. {
  49. // Was a single argument specified?
  50. if (argc == 1)
  51. {
  52. // Yes, so fetch field value.
  53. const char* pFieldValue = argv[0];
  54. // Fetch asset pointer.
  55. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  56. // Is the asset pointer the correct type?
  57. if (pAssetPtr == NULL)
  58. {
  59. // No, so fail.
  60. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  61. return;
  62. }
  63. // Set asset.
  64. pAssetPtr->setAssetId(pFieldValue);
  65. return;
  66. }
  67. // Warn.
  68. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  69. }
  70. //-----------------------------------------------------------------------------
  71. MaterialAsset::MaterialAsset()
  72. {
  73. mShaderGraphFile = "";
  74. mScriptFile = StringTable->EmptyString();
  75. mMatDefinitionName = StringTable->EmptyString();
  76. }
  77. //-----------------------------------------------------------------------------
  78. MaterialAsset::~MaterialAsset()
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. void MaterialAsset::initPersistFields()
  83. {
  84. // Call parent.
  85. Parent::initPersistFields();
  86. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  87. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  88. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  89. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  90. }
  91. void MaterialAsset::initializeAsset()
  92. {
  93. // Call parent.
  94. Parent::initializeAsset();
  95. compileShader();
  96. if (!Platform::isFullPath(mScriptFile))
  97. mScriptFile = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptFile;
  98. if (Platform::isFile(mScriptFile))
  99. Con::executeFile(mScriptFile, false, false);
  100. }
  101. void MaterialAsset::onAssetRefresh()
  102. {
  103. mScriptFile = expandAssetFilePath(mScriptFile);
  104. if (Platform::isFile(mScriptFile))
  105. Con::executeFile(mScriptFile, false, false);
  106. if (mMatDefinitionName != StringTable->EmptyString())
  107. {
  108. Material* matDef;
  109. if (!Sim::findObject(mMatDefinitionName, matDef))
  110. {
  111. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  112. return;
  113. }
  114. matDef->reload();
  115. }
  116. }
  117. void MaterialAsset::setScriptFile(const char* pScriptFile)
  118. {
  119. // Sanity!
  120. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  121. // Fetch image file.
  122. pScriptFile = StringTable->insert(pScriptFile);
  123. // Update.
  124. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  125. // Refresh the asset.
  126. refreshAsset();
  127. }
  128. //------------------------------------------------------------------------------
  129. void MaterialAsset::compileShader()
  130. {
  131. }
  132. void MaterialAsset::copyTo(SimObject* object)
  133. {
  134. // Call to parent.
  135. Parent::copyTo(object);
  136. }
  137. DefineEngineMethod(MaterialAsset, compileShader, void, (), , "Compiles the material's generated shader, if any. Not yet implemented\n")
  138. {
  139. object->compileShader();
  140. }
  141. //-----------------------------------------------------------------------------
  142. // GuiInspectorTypeAssetId
  143. //-----------------------------------------------------------------------------
  144. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  145. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  146. "@brief Inspector field type for Material Asset Objects\n\n"
  147. "Editor use only.\n\n"
  148. "@internal"
  149. );
  150. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  151. {
  152. Parent::consoleInit();
  153. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  154. }
  155. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  156. {
  157. // Create base filename edit controls
  158. mUseHeightOverride = true;
  159. mHeightOverride = 100;
  160. mMatEdContainer = new GuiControl();
  161. mMatEdContainer->registerObject();
  162. addObject(mMatEdContainer);
  163. // Create "Open in ShapeEditor" button
  164. mMatPreviewButton = new GuiBitmapButtonCtrl();
  165. const char* matAssetId = getData();
  166. MaterialAsset* matAsset = AssetDatabase.acquireAsset< MaterialAsset>(matAssetId);
  167. Material* materialDef = nullptr;
  168. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  169. StringTableEntry matDefName = matAsset ? matAsset->getMaterialDefinitionName() : matAssetId;
  170. if (!Sim::findObject(matDefName, materialDef))
  171. {
  172. Con::errorf("GuiInspectorTypeMaterialAssetPtr::constructEditControl() - unable to find material in asset");
  173. }
  174. else
  175. {
  176. mMatPreviewButton->setBitmap(materialDef->mDiffuseMapFilename[0]);
  177. }
  178. mMatPreviewButton->setPosition(0, 0);
  179. mMatPreviewButton->setExtent(100,100);
  180. // Change filespec
  181. char szBuffer[512];
  182. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %d, %s);",
  183. mInspector->getComponentGroupTargetId(), mCaption);
  184. mMatPreviewButton->setField("Command", szBuffer);
  185. mMatPreviewButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  186. mMatPreviewButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  187. mMatPreviewButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  188. StringBuilder strbld;
  189. strbld.append(matDefName);
  190. strbld.append("\n");
  191. strbld.append("Open this file in the Material Editor");
  192. mMatPreviewButton->setDataField(StringTable->insert("tooltip"), NULL, strbld.data());
  193. _registerEditControl(mMatPreviewButton);
  194. //mMatPreviewButton->registerObject();
  195. mMatEdContainer->addObject(mMatPreviewButton);
  196. mMatAssetIdTxt = new GuiTextEditCtrl();
  197. mMatAssetIdTxt->registerObject();
  198. mMatAssetIdTxt->setActive(false);
  199. mMatAssetIdTxt->setText(matAssetId);
  200. mMatAssetIdTxt->setBounds(100, 0, 150, 18);
  201. mMatEdContainer->addObject(mMatAssetIdTxt);
  202. return mMatEdContainer;
  203. }
  204. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  205. {
  206. S32 dividerPos, dividerMargin;
  207. mInspector->getDivider(dividerPos, dividerMargin);
  208. Point2I fieldExtent = getExtent();
  209. Point2I fieldPos = getPosition();
  210. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  211. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  212. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  213. if (mMatEdContainer != nullptr)
  214. {
  215. mMatPreviewButton->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  216. }
  217. if (mMatPreviewButton != nullptr)
  218. {
  219. mMatPreviewButton->resize(Point2I::Zero, Point2I(100, 100));
  220. }
  221. if (mMatAssetIdTxt != nullptr)
  222. {
  223. mMatAssetIdTxt->resize(Point2I(100, 0), Point2I(mEditCtrlRect.extent.x - 100, 18));
  224. }
  225. return resized;
  226. }
  227. void GuiInspectorTypeMaterialAssetPtr::setMaterialAsset(String assetId)
  228. {
  229. mTargetObject->setDataField(mCaption, "", assetId);
  230. //force a refresh
  231. SimObject* obj = mInspector->getInspectObject();
  232. mInspector->inspectObject(obj);
  233. }
  234. DefineEngineMethod(GuiInspectorTypeMaterialAssetPtr, setMaterialAsset, void, (String assetId), (""),
  235. "Gets a particular shape animation asset for this shape.\n"
  236. "@param animation asset index.\n"
  237. "@return Shape Animation Asset.\n")
  238. {
  239. if (assetId == String::EmptyString)
  240. return;
  241. return object->setMaterialAsset(assetId);
  242. }