ImageAsset.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. //-----------------------------------------------------------------------------
  3. // Copyright (c) 2013 GarageGames, LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #ifndef IMAGE_ASSET_H
  24. #define IMAGE_ASSET_H
  25. #ifndef _ASSET_BASE_H_
  26. #include "assets/assetBase.h"
  27. #endif
  28. #ifndef _ASSET_DEFINITION_H_
  29. #include "assets/assetDefinition.h"
  30. #endif
  31. #ifndef _STRINGUNIT_H_
  32. #include "string/stringUnit.h"
  33. #endif
  34. #ifndef _ASSET_FIELD_TYPES_H_
  35. #include "assets/assetFieldTypes.h"
  36. #endif
  37. #ifndef _ASSET_PTR_H_
  38. #include "assets/assetPtr.h"
  39. #endif
  40. #include "gfx/bitmap/gBitmap.h"
  41. #include "gfx/gfxTextureHandle.h"
  42. #include "gui/editor/guiInspectorTypes.h"
  43. //-----------------------------------------------------------------------------
  44. class ImageAsset : public AssetBase
  45. {
  46. typedef AssetBase Parent;
  47. public:
  48. /// The different types of image use cases
  49. enum ImageTypes
  50. {
  51. Albedo = 0,
  52. Normal = 1,
  53. ORMConfig = 2,
  54. GUI = 3,
  55. Roughness = 4,
  56. AO = 5,
  57. Metalness = 6,
  58. Glow = 7,
  59. Particle = 8,
  60. Decal = 9,
  61. Cubemap = 10,
  62. ImageTypeCount = 11
  63. };
  64. protected:
  65. StringTableEntry mImageFileName;
  66. StringTableEntry mImagePath;
  67. GFXTexHandle mImage;
  68. bool mIsValidImage;
  69. bool mUseMips;
  70. bool mIsHDRImage;
  71. ImageTypes mImageType;
  72. Map<GFXTextureProfile, GFXTexHandle> mResourceMap;
  73. public:
  74. ImageAsset();
  75. virtual ~ImageAsset();
  76. /// Engine.
  77. static void initPersistFields();
  78. virtual void copyTo(SimObject* object);
  79. /// Declare Console Object.
  80. DECLARE_CONOBJECT(ImageAsset);
  81. void setImageFileName(const char* pScriptFile);
  82. inline StringTableEntry getImageFileName(void) const { return mImageFileName; };
  83. inline StringTableEntry getImagePath(void) const { return mImagePath; };
  84. bool isValid() { return mIsValidImage; }
  85. GFXTexHandle getImage(GFXTextureProfile requestedProfile);
  86. const char* getImageInfo();
  87. static const char* getImageTypeNameFromType(ImageTypes type);
  88. static ImageTypes getImageTypeFromName(const char* name);
  89. void setImageType(ImageTypes type) { mImageType = type; }
  90. static bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
  91. static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
  92. static bool getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset);
  93. protected:
  94. virtual void initializeAsset(void);
  95. virtual void onAssetRefresh(void);
  96. static bool setImageFileName(void* obj, const char* index, const char* data) { static_cast<ImageAsset*>(obj)->setImageFileName(data); return false; }
  97. static const char* getImageFileName(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFileName(); }
  98. void loadImage();
  99. };
  100. DefineConsoleType(TypeImageAssetPtr, ImageAsset)
  101. DefineConsoleType(TypeImageAssetId, String)
  102. typedef ImageAsset::ImageTypes ImageAssetType;
  103. DefineEnumType(ImageAssetType);
  104. class GuiInspectorTypeImageAssetPtr : public GuiInspectorTypeFileName
  105. {
  106. typedef GuiInspectorTypeFileName Parent;
  107. public:
  108. GuiBitmapButtonCtrl* mImageEdButton;
  109. DECLARE_CONOBJECT(GuiInspectorTypeImageAssetPtr);
  110. static void consoleInit();
  111. virtual GuiControl* constructEditControl();
  112. virtual bool updateRects();
  113. };
  114. class GuiInspectorTypeImageAssetId : public GuiInspectorTypeImageAssetPtr
  115. {
  116. typedef GuiInspectorTypeImageAssetPtr Parent;
  117. public:
  118. DECLARE_CONOBJECT(GuiInspectorTypeImageAssetId);
  119. static void consoleInit();
  120. };
  121. #define assetText(x,suff) std::string(std::string(#x) + std::string(#suff)).c_str()
  122. #define initMapSlot(name) m##name##Filename = String::EmptyString; m##name##AssetId = StringTable->EmptyString(); m##name##Asset = NULL;
  123. #define bindMapSlot(name) if (m##name##AssetId != String::EmptyString) m##name##Asset = m##name##AssetId;
  124. #define scriptBindMapSlot(name, consoleClass, docs) addField(#name, TypeImageFilename, Offset(m##name##Filename, consoleClass), assetText(name, docs)); \
  125. addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & defaultProtectedGetFn, assetText(name, asset reference.));
  126. #define initMapArraySlot(name,id) m##name##Filename[id] = String::EmptyString; m##name##AssetId[id] = StringTable->EmptyString(); m##name##Asset[id] = NULL;
  127. #define bindMapArraySlot(name,id) if (m##name##AssetId[id] != String::EmptyString) m##name##Asset[id] = m##name##AssetId[id];
  128. #define scriptBindMapArraySlot(name, arraySize, consoleClass, docs) addField(#name, TypeImageFilename, Offset(m##name##Filename, consoleClass), arraySize, assetText(name, docs)); \
  129. addProtectedField(assetText(name,Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##AssetSlot, &defaultProtectedGetFn, arraySize, assetText(name,asset reference.));
  130. #define DECLARE_TEXTUREMAP(className,name) protected: \
  131. FileName m##name##Filename;\
  132. StringTableEntry m##name##AssetId;\
  133. AssetPtr<ImageAsset> m##name##Asset;\
  134. public: \
  135. const String& get##name() const { return m##name##Filename; }\
  136. void set##name(FileName _in) { m##name##Filename = _in; }\
  137. const AssetPtr<ImageAsset> & get##name##Asset() const { return m##name##Asset; }\
  138. void set##name##Asset(AssetPtr<ImageAsset>_in) { m##name##Asset = _in; }\
  139. static bool _set##name##Asset(void* obj, const char* index, const char* data)\
  140. {\
  141. className* mat = static_cast<className*>(obj);\
  142. mat->m##name##AssetId = StringTable->insert(data);\
  143. if (ImageAsset::getAssetById(mat->m##name##AssetId, &mat->m##name##Asset))\
  144. {\
  145. if (mat->m##name##Asset.getAssetId() != StringTable->insert("Core_Rendering:noMaterial"))\
  146. mat->m##name##Filename = StringTable->EmptyString();\
  147. return true;\
  148. }\
  149. return true;\
  150. }
  151. #define GET_TEXTUREMAP(name) get##name()
  152. #define SET_TEXTUREMAP(name,_in) set##name(_in)
  153. #define GET_TEXTUREASSET(name) get##name##Asset()
  154. #define SET_TEXTUREASSET(name,_in) set##name##Asset(_in)
  155. #define DECLARE_TEXTUREARRAY(className,name,max) FileName m##name##Filename[max];\
  156. StringTableEntry m##name##AssetId[max];\
  157. AssetPtr<ImageAsset> m##name##Asset[max];\
  158. static bool _set##name##AssetSlot(void* obj, const char* index, const char* data)\
  159. {\
  160. className* mat = static_cast<className*>(obj);\
  161. if (!index) return false;\
  162. U32 idx = dAtoi(index);\
  163. if (idx >= max)\
  164. return false;\
  165. mat->m##name##AssetId[idx] = StringTable->insert(data);\
  166. if (ImageAsset::getAssetById(mat->m##name##AssetId[idx], &mat->m##name##Asset[idx]))\
  167. {\
  168. if (mat->m##name##Asset[idx].getAssetId() != StringTable->insert("Core_Rendering:noMaterial"))\
  169. {\
  170. mat->m##name##Filename[idx] = StringTable->EmptyString();\
  171. }\
  172. return true;\
  173. }\
  174. return true;\
  175. }
  176. #endif