ShapeAsset.h

Engine/source/T3D/assets/ShapeAsset.h

More...

Classes:

Public Defines

define
assetText(x, suff) std::string(std::string(#x) + std::string(#suff)).c_str()
define
bindShapeAsset(name)  (m##name##AssetId != ->EmptyString()) m##name##Asset = m##name##AssetId;
define
cloneShapeAsset(name) m##name##Name = other.m##name##Name; m##name##AssetId = other.m##name##AssetId; m##name##Asset = other.m##name##Asset;
define
DECLARE_SHAPEASSET(className, name) 
define
initShapeAsset(name) m##name##Name = ->EmptyString(); m##name##AssetId = ->EmptyString(); m##name##Asset = ;
define
scriptBindShapeAsset(name, consoleClass, docs)                                       addProtectedField((name, ), TypeShapeFilename, (m##name##Name, consoleClass), consoleClass::_set##name##Filename,  & , (name, docs)); \
                                      addProtectedField((name, Asset), TypeShapeAssetId, (m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & , (name, asset reference.));

Detailed Description

Public Defines

assetText(x, suff) std::string(std::string(#x) + std::string(#suff)).c_str()
bindShapeAsset(name)  (m##name##AssetId != ->EmptyString()) m##name##Asset = m##name##AssetId;
cloneShapeAsset(name) m##name##Name = other.m##name##Name; m##name##AssetId = other.m##name##AssetId; m##name##Asset = other.m##name##Asset;
DECLARE_SHAPEASSET(className, name) 
initShapeAsset(name) m##name##Name = ->EmptyString(); m##name##AssetId = ->EmptyString(); m##name##Asset = ;
scriptBindShapeAsset(name, consoleClass, docs)                                       addProtectedField((name, ), TypeShapeFilename, (m##name##Name, consoleClass), consoleClass::_set##name##Filename,  & , (name, docs)); \
                                      addProtectedField((name, Asset), TypeShapeAssetId, (m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & , (name, asset reference.));
  1
  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 SHAPE_ASSET_H
 24#define SHAPE_ASSET_H
 25
 26#ifndef _ASSET_BASE_H_
 27#include "assets/assetBase.h"
 28#endif
 29
 30#ifndef _ASSET_DEFINITION_H_
 31#include "assets/assetDefinition.h"
 32#endif
 33
 34#ifndef _STRINGUNIT_H_
 35#include "string/stringUnit.h"
 36#endif
 37
 38#ifndef _ASSET_FIELD_TYPES_H_
 39#include "assets/assetFieldTypes.h"
 40#endif
 41
 42#ifndef _TSSHAPE_H_
 43#include "ts/tsShape.h"
 44#endif
 45#ifndef __RESOURCE_H__
 46#include "core/resource.h"
 47#endif
 48#ifndef _ASSET_PTR_H_
 49#include "assets/assetPtr.h"
 50#endif 
 51#ifndef MATERIALASSET_H
 52#include "MaterialAsset.h"
 53#endif
 54#ifndef SHAPE_ANIMATION_ASSET_H
 55#include "ShapeAnimationAsset.h"
 56#endif
 57
 58#ifdef TORQUE_TOOLS
 59#include "gui/editor/guiInspectorTypes.h"
 60#endif
 61#ifndef _BITSTREAM_H_
 62#include "core/stream/bitStream.h"
 63#endif
 64//-----------------------------------------------------------------------------
 65class ShapeAsset : public AssetBase
 66{
 67   typedef AssetBase Parent;
 68
 69protected:
 70   StringTableEntry   mFileName;
 71   StringTableEntry   mConstructorFileName;
 72   StringTableEntry   mFilePath;
 73   StringTableEntry   mConstructorFilePath;
 74   Resource<TSShape>  mShape;
 75
 76   //Material assets we're dependent on and use
 77   Vector<StringTableEntry> mMaterialAssetIds;
 78   Vector<AssetPtr<MaterialAsset>> mMaterialAssets;
 79
 80   //Animation assets we're dependent on and use
 81   Vector<StringTableEntry> mAnimationAssetIds;
 82   Vector<AssetPtr<ShapeAnimationAsset>> mAnimationAssets;
 83
 84   typedef Signal<void()> ShapeAssetChanged;
 85
 86   ShapeAssetChanged mChangeSignal;
 87
 88public:
 89   enum ShapeAssetErrCode
 90   {
 91      TooManyVerts = AssetErrCode::Extended,
 92      TooManyBones,
 93      MissingAnimatons,
 94      Extended
 95   };
 96
 97   static const String mErrCodeStrings[ShapeAssetErrCode::Extended - Parent::Extended + 1];
 98   static String getAssetErrstrn(U32 errCode)
 99   {
100      if (errCode < Parent::Extended) return Parent::getAssetErrstrn(errCode);
101      if (errCode > ShapeAssetErrCode::Extended) return "undefined error";
102      return mErrCodeStrings[errCode];
103   };
104
105   ShapeAsset();
106   virtual ~ShapeAsset();
107
108   /// Engine.
109   static void initPersistFields();
110   virtual void copyTo(SimObject* object);
111
112   virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
113
114   virtual void initializeAsset();
115
116   /// Declare Console Object.
117   DECLARE_CONOBJECT(ShapeAsset);
118
119   bool loadShape();
120   U32 mLoadedState;
121
122   TSShape* getShape() { return mShape; }
123
124   Resource<TSShape> getShapeResource() { return mShape; }
125
126   void SplitSequencePathAndName(String& srcPath, String& srcName);
127   StringTableEntry getShapeFilename() { return mFilePath; }
128   
129   U32 getShapeFilenameHash() { return _StringTable::hashString(mFilePath); }
130
131   Vector<AssetPtr<MaterialAsset>> getMaterialAssets() { return mMaterialAssets; }
132
133   inline AssetPtr<MaterialAsset> getMaterialAsset(U32 matId) 
134   { 
135      if(matId >= mMaterialAssets.size()) 
136          return nullptr; 
137      else 
138         return mMaterialAssets[matId]; 
139   }
140
141   void clearMaterialAssets() { mMaterialAssets.clear(); }
142
143   void addMaterialAssets(AssetPtr<MaterialAsset> matPtr) { mMaterialAssets.push_back(matPtr); }
144
145   S32 getMaterialCount() { return mMaterialAssets.size(); }
146   S32 getAnimationCount() { return mAnimationAssets.size(); }
147   ShapeAnimationAsset* getAnimation(S32 index);
148
149   void _onResourceChanged(const Torque::Path &path);
150
151   ShapeAssetChanged& getChangedSignal() { return mChangeSignal; }
152
153   void                    setShapeFile(const char* pScriptFile);
154   inline StringTableEntry getShapeFile(void) const { return mFileName; };
155
156   void                    setShapeConstructorFile(const char* pScriptFile);
157   inline StringTableEntry getShapeConstructorFile(void) const { return mConstructorFileName; };
158
159   inline StringTableEntry getShapeFilePath(void) const { return mFilePath; };
160   inline StringTableEntry getShapeConstructorFilePath(void) const { return mConstructorFilePath; };
161
162   static bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset);
163
164   static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
165   static U32 getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset);
166
167   static StringTableEntry getNoShapeAssetId() { return StringTable->insert("Core_Rendering:noshape"); }
168
169#ifdef TORQUE_TOOLS
170   const char* generateCachedPreviewImage(S32 resolution);
171#endif
172
173protected:
174   virtual void            onAssetRefresh(void);
175
176   static bool setShapeFile(void *obj, const char *index, const char *data) { static_cast<ShapeAsset*>(obj)->setShapeFile(data); return false; }
177   static const char* getShapeFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeFile(); }
178
179   static bool setShapeConstructorFile(void* obj, const char* index, const char* data) { static_cast<ShapeAsset*>(obj)->setShapeConstructorFile(data); return false; }
180   static const char* getShapeConstructorFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeConstructorFile(); }
181
182};
183
184#ifdef TORQUE_TOOLS
185DefineConsoleType(TypeShapeAssetPtr, S32)
186DefineConsoleType(TypeShapeAssetId, String)
187
188//-----------------------------------------------------------------------------
189// TypeAssetId GuiInspectorField Class
190//-----------------------------------------------------------------------------
191class GuiInspectorTypeShapeAssetPtr : public GuiInspectorTypeFileName
192{
193   typedef GuiInspectorTypeFileName Parent;
194public:
195
196   GuiBitmapButtonCtrl  *mShapeEdButton;
197
198   DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
199   static void consoleInit();
200
201   virtual GuiControl* constructEditControl();
202   virtual bool updateRects();
203};
204
205class GuiInspectorTypeShapeAssetId : public GuiInspectorTypeShapeAssetPtr
206{
207   typedef GuiInspectorTypeShapeAssetPtr Parent;
208public:
209
210   DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetId);
211   static void consoleInit();
212};
213#endif
214
215#define assetText(x,suff) std::string(std::string(#x) + std::string(#suff)).c_str()
216
217#define initShapeAsset(name) m##name##Name = StringTable->EmptyString(); m##name##AssetId = StringTable->EmptyString(); m##name##Asset = NULL;
218#define cloneShapeAsset(name) m##name##Name = other.m##name##Name; m##name##AssetId = other.m##name##AssetId; m##name##Asset = other.m##name##Asset;
219#define bindShapeAsset(name) if (m##name##AssetId != StringTable->EmptyString()) m##name##Asset = m##name##AssetId;
220
221#define scriptBindShapeAsset(name, consoleClass, docs) addProtectedField(assetText(name, File), TypeShapeFilename, Offset(m##name##Name, consoleClass), consoleClass::_set##name##Filename,  & defaultProtectedGetFn, assetText(name, docs)); \
222                                      addProtectedField(assetText(name, Asset), TypeShapeAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & defaultProtectedGetFn, assetText(name, asset reference.));
223
224#define DECLARE_SHAPEASSET(className,name)\
225                                      StringTableEntry m##name##Name;\
226                                      StringTableEntry m##name##AssetId;\
227                                      AssetPtr<ShapeAsset>  m##name##Asset;\
228                                      const StringTableEntry& get##name() const { return m##name##Name; }\
229                                      void set##name(FileName _in) { m##name##Name = _in; }\
230                                      const AssetPtr<ShapeAsset> & get##name##Asset() const { return m##name##Asset; }\
231                                      void set##name##Asset(AssetPtr<ShapeAsset>_in) { m##name##Asset = _in; }\
232static bool _set##name##Filename(void* obj, const char* index, const char* data)\
233{\
234   className* shape = static_cast<className*>(obj);\
235   \
236   StringTableEntry assetId = ShapeAsset::getAssetIdByFilename(StringTable->insert(data));\
237   if (assetId != StringTable->EmptyString())\
238   {\
239      if (shape->_set##name##Asset(obj, index, assetId))\
240      {\
241         if (assetId == StringTable->insert("Core_Rendering:noShape"))\
242         {\
243            shape->m##name##Name = data;\
244            shape->m##name##AssetId = StringTable->EmptyString();\
245            \
246            return true;\
247         }\
248         else\
249         {\
250            shape->m##name##AssetId = assetId;\
251            shape->m##name##Name = StringTable->EmptyString();\
252            \
253            return false;\
254         }\
255      }\
256   }\
257   else\
258   {\
259      shape->m##name##Asset = StringTable->EmptyString();\
260   }\
261   \
262   return true;\
263}\
264\
265static bool _set##name##Asset(void* obj, const char* index, const char* data)\
266{\
267   className* shape = static_cast<className*>(obj);\
268   shape->m##name##AssetId = StringTable->insert(data);\
269   if (ShapeAsset::getAssetById(shape->m##name##AssetId, &shape->m##name##Asset))\
270   {\
271      if (shape->m##name##Asset.getAssetId() != StringTable->insert("Core_Rendering:noShape"))\
272         shape->m##name##Name = StringTable->EmptyString();\
273      return true;\
274   }\
275   return false;\
276}\
277void pack##name##Asset(BitStream *stream)\
278{\
279   if (stream->writeFlag(m##name##Asset.notNull()))\
280      stream->writeString(m##name##Asset.getAssetId());\
281   else\
282      stream->writeString(m##name##Name);\
283}\
284void unpack##name##Asset(BitStream *stream)\
285{\
286   if (stream->readFlag())\
287   {\
288      m##name##AssetId = stream->readSTString();\
289      ShapeAsset::getAssetById(m##name##AssetId, &m##name##Asset);\
290      m##name##Name = m##name##Asset->getShapeFilename(); \
291   }\
292   else\
293      m##name##Name = stream->readSTString();\
294}
295
296#endif
297
298