Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Handling Refactor - For 4.2 #1343

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Next Next commit
init commit
start of attempt 3
  • Loading branch information
marauder2k7 committed Jan 23, 2025
commit eca0820134376a23fce3458598ea0eef9300ccd7
361 changes: 232 additions & 129 deletions Engine/source/T3D/assets/ImageAsset.cpp

Large diffs are not rendered by default.

163 changes: 111 additions & 52 deletions Engine/source/T3D/assets/ImageAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,39 @@ class ImageAsset : public AssetBase
ImageTypeCount = 11
};

class Frame
{
public:
Frame(const S32 pixelOffsetX, const S32 pixelOffsetY,
const U32 pixelWidth, const U32 pixelHeight,
const F32 texelWidthScale, const F32 texelHeightScale,
StringTableEntry inRegionName = StringTable->EmptyString())
: regionName(inRegionName)
{
pixelOffset.set(pixelOffsetY, pixelOffsetY);
pixelSize.set(pixelWidth, pixelHeight);

texelLower.set(pixelOffsetX * texelWidthScale, pixelOffsetY * texelHeightScale);
texelSize.set(pixelWidth * texelWidthScale, pixelHeight * texelHeightScale);
texelUpper.set(texelLower.x + texelSize.x, texelLower.y + texelSize.y);
}

void setFlip(bool flipX, bool flipY)
{
if (flipX) mSwap(texelLower.x, texelUpper.x);
if (flipY) mSwap(texelLower.y, texelUpper.y);
}

Point2I pixelOffset;
Point2I pixelSize;

Point2F texelLower;
Point2F texelUpper;
Point2F texelSize;

StringTableEntry regionName;
};

static StringTableEntry smNoImageAssetFallback;

enum ImageAssetErrCode
Expand All @@ -96,26 +129,16 @@ class ImageAsset : public AssetBase
if (errCode > ImageAssetErrCode::Extended) return "undefined error";
return mErrCodeStrings[errCode - Parent::Extended];
};
private:

protected:
StringTableEntry mImageFileName;
StringTableEntry mImagePath;
NamedTexTargetRef mNamedTarget;

bool mIsValidImage;
bool mUseMips;
bool mIsHDRImage;

ImageTypes mImageType;

StringTableEntry mImageFile;
bool mUseMips;
bool mIsHDRImage;
GFXTexHandle mTextureHandle;
ImageTypes mImageType;
HashMap<GFXTextureProfile*, GFXTexHandle> mResourceMap;

typedef Signal<void()> ImageAssetChanged;
ImageAssetChanged mChangeSignal;

typedef Signal<void(S32 index)> ImageAssetArrayChanged;
ImageAssetArrayChanged mChangeArraySignal;

void generateTexture(void);
public:
ImageAsset();
virtual ~ImageAsset();
Expand All @@ -125,51 +148,86 @@ class ImageAsset : public AssetBase

/// Engine.
static void initPersistFields();

/// Sim
bool onAdd() override;
void onRemove() override;
void copyTo(SimObject* object) override;

/// Declare Console Object.
DECLARE_CONOBJECT(ImageAsset);

void _onResourceChanged(const Torque::Path& path);

ImageAssetChanged& getChangedSignal() { return mChangeSignal; }
ImageAssetArrayChanged& getChangedArraySignal() { return mChangeArraySignal; }

void setImageFileName(StringTableEntry pScriptFile);
inline StringTableEntry getImageFileName(void) const { return mImageFileName; };
// asset Base load
U32 load() override;

inline StringTableEntry getImagePath(void) const { return mImagePath; };
void setImageFile(StringTableEntry pImageFile);
inline StringTableEntry getImageFile(void) const { return mImageFile; };

bool isValid() { return mIsValidImage; }
void setGenMips(const bool pGenMips);
inline bool getGenMips(void) const { return mUseMips; };

GFXTexHandle getTexture(GFXTextureProfile* requestedProfile);
void setTextureHDR(const bool pIsHDR);
inline bool getTextureHDR(void) const { return mIsHDRImage; };

StringTableEntry getImageInfo();
inline GFXTexHandle& getTexture(void) { load(); generateTexture(); return mTextureHandle; }
GFXTexHandle getTexture(GFXTextureProfile* requestedProfile);

static StringTableEntry getImageTypeNameFromType(ImageTypes type);
static ImageTypes getImageTypeFromName(StringTableEntry name);
static ImageTypes getImageTypeFromName(StringTableEntry name);

void setImageType(ImageTypes type) { mImageType = type; }
ImageTypes getImageType() { return mImageType; }
void setImageType(ImageTypes type) { mImageType = type; }
ImageTypes getImageType() { return mImageType; }

inline U32 getTextureWidth(void) const { return mTextureHandle->getWidth(); }
inline U32 getTextureHeight(void) const { return mTextureHandle->getHeight(); }
inline U32 getTextureDepth(void) const { return mTextureHandle->getDepth(); }

inline U32 getTextureBitmapWidth(void) const { return mTextureHandle->getBitmapWidth(); }
inline U32 getTextureBitmapHeight(void) const { return mTextureHandle->getBitmapHeight(); }
inline U32 getTextureBitmapDepth(void) const { return mTextureHandle->getBitmapDepth(); }
bool isAssetValid(void) const override { return !mTextureHandle.isNull(); }

static U32 getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
static U32 getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset);
static U32 getAssetById(String assetId, AssetPtr<ImageAsset>* imageAsset) { return getAssetById(assetId.c_str(), imageAsset); };

U32 load() override;

const char* getImageInfo();

protected:
void initializeAsset(void) override;
void onAssetRefresh(void) override;
// Asset Base callback
void initializeAsset(void) override;
void onAssetRefresh(void) override;
void _onFileChanged(const Torque::Path& path);

/// Taml callbacks.
void onTamlPreWrite(void) override;
void onTamlPostWrite(void) override;

static bool setImageFileName(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ImageAsset*>(obj)->setImageFileName(data); return false; }
static StringTableEntry getImageFileName(void* obj, StringTableEntry data) { return static_cast<ImageAsset*>(obj)->getImageFileName(); }
protected:
// Texture file
static bool setImageFile(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ImageAsset*>(obj)->setImageFile(data); return false; }
static const char* getImageFile(void* obj, StringTableEntry data) { return static_cast<ImageAsset*>(obj)->getImageFile(); }
static bool writeImageFile(void* obj, StringTableEntry pFieldName) { return static_cast<ImageAsset*>(obj)->getImageFile() != StringTable->EmptyString(); }

// Gen mips?
static bool setGenMips(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ImageAsset*>(obj)->setGenMips(dAtob(data)); return false; }
static bool writeGenMips(void* obj, StringTableEntry pFieldName) { return static_cast<ImageAsset*>(obj)->getGenMips() == true; }

// Texture Is Hdr?
static bool setTextureHDR(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ImageAsset*>(obj)->setTextureHDR(dAtob(data)); return false; }
static bool writeTextureHDR(void* obj, StringTableEntry pFieldName) { return static_cast<ImageAsset*>(obj)->getTextureHDR() == true; }
};

DefineConsoleType(TypeImageAssetPtr, ImageAsset)
DefineConsoleType(TypeImageAssetId, String)

DECLARE_STRUCT(AssetPtr<ImageAsset>)
DefineConsoleType(TypeImageAssetPtrRefactor, AssetPtr<ImageAsset> )

typedef ImageAsset::ImageTypes ImageAssetType;
DefineEnumType(ImageAssetType);

Expand All @@ -196,10 +254,6 @@ public: \
{\
if(m##name##AssetId != _in || m##name##Name != _in)\
{\
if (m##name##Asset.notNull())\
{\
m##name##Asset->getChangedSignal().remove(this, &className::changeFunc);\
}\
if (_in == NULL || _in == StringTable->EmptyString())\
{\
m##name##Name = StringTable->EmptyString();\
Expand Down Expand Up @@ -251,10 +305,6 @@ public: \
}\
if (get##name() != StringTable->EmptyString() && m##name##Name != StringTable->insert("texhandle"))\
{\
if (m##name##Asset.notNull())\
{\
m##name##Asset->getChangedSignal().notify(this, &className::changeFunc);\
}\
\
if (get##name()[0] != '$' && get##name()[0] != '#') {\
m##name.set(get##name(), m##name##Profile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));\
Expand Down Expand Up @@ -285,11 +335,8 @@ public: \
\
const StringTableEntry get##name() const\
{\
if (m##name##Asset && (m##name##Asset->getImageFileName() != StringTable->EmptyString()))\
if (m##name##Asset->getImageFileName()[0] == '#' || m##name##Asset->getImageFileName()[0] == '$')\
return m##name##Asset->getImageFileName();\
else\
return Platform::makeRelativePathName(m##name##Asset->getImagePath(), Platform::getMainDotCsDir());\
if (m##name##Asset && (m##name##Asset->getImageFile() != StringTable->EmptyString()))\
return Platform::makeRelativePathName(m##name##Asset->getImageFile(), Platform::getMainDotCsDir());\
else if (m##name##AssetId != StringTable->EmptyString())\
return m##name##AssetId;\
else if (m##name##Name != StringTable->EmptyString())\
Expand Down Expand Up @@ -435,11 +482,8 @@ public: \
\
const StringTableEntry get##name(const U32& index) const\
{\
if (m##name##Asset[index] && (m##name##Asset[index]->getImageFileName() != StringTable->EmptyString()))\
if (m##name##Asset[index]->getImageFileName()[0] == '#' || m##name##Asset[index]->getImageFileName()[0] == '$')\
return m##name##Asset[index]->getImageFileName();\
else\
return Platform::makeRelativePathName(m##name##Asset[index]->getImagePath(), Platform::getMainDotCsDir());\
if (m##name##Asset[index] && (m##name##Asset[index]->getImageFile() != StringTable->EmptyString()))\
return Platform::makeRelativePathName(m##name##Asset[index]->getImageFile(), Platform::getMainDotCsDir());\
else if (m##name##AssetId[index] != StringTable->EmptyString())\
return m##name##AssetId[index];\
else if (m##name##Name[index] != StringTable->EmptyString())\
Expand Down Expand Up @@ -542,4 +586,19 @@ if (m##name##AssetId[index] != StringTable->EmptyString())\

#pragma endregion

#pragma region Refactor Asset Macros

#define DECLARE_IMAGEASSET_REFACTOR(className, name, profile) \
private: \
AssetPtr<ImageAsset> m##name##Asset; \
public: \
void _set##name(StringTableEntry _in); \
inline StringTableEntry _get##name(void) const { return m##name##Asset.getAssetId(); } \
GFXTexHandle get##name() { return m##name##Asset.notNull() ? m##name##Asset->getTexture(&profile) : NULL; } \
AssetPtr<ImageAsset> get##name##Asset(void) { return m##name##Asset; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false;}

#define INITPERSISTFIELD_IMAGEASSET_REFACTOR(name, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeImageAssetPtrRefactor, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.));

#pragma endregion
2 changes: 1 addition & 1 deletion Engine/source/T3D/assets/LevelAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ StringTableEntry LevelAsset::getPreviewImagePath(void) const
{
if (mPreviewImageAsset.notNull() && mPreviewImageAsset->isAssetValid())
{
return mPreviewImageAsset->getImagePath();
return mPreviewImageAsset->getImageFile();
}

return StringTable->EmptyString();
Expand Down
6 changes: 3 additions & 3 deletions Engine/source/T3D/assets/assetImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ void AssetImporter::processMaterialAsset(AssetImportObject* assetItem)
{
//got a match!
ImageAsset* foundImageAsset = AssetDatabase.acquireAsset<ImageAsset>(testAssetId.c_str());
imagePath = foundImageAsset->getImagePath();
imagePath = foundImageAsset->getImageFile();

AssetImportObject* newImageAssetObj = addImportingAsset("ImageAsset", imagePath, assetItem, "");

Expand Down Expand Up @@ -1921,7 +1921,7 @@ void AssetImporter::processMaterialAsset(AssetImportObject* assetItem)
{
//got a match!
ImageAsset* foundImageAsset = AssetDatabase.acquireAsset<ImageAsset>(testAssetId.c_str());
imagePath = foundImageAsset->getImagePath();
imagePath = foundImageAsset->getImageFile();

AssetImportObject* newImageAssetObj = addImportingAsset("ImageAsset", imagePath, assetItem, "");

Expand Down Expand Up @@ -2834,7 +2834,7 @@ Torque::Path AssetImporter::importImageAsset(AssetImportObject* assetItem)
#endif

newAsset->setAssetName(assetName);
newAsset->setImageFileName(imageFileName.c_str());
newAsset->setImageFile(imageFileName.c_str());

//If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
//file path for reimporting support later
Expand Down
5 changes: 0 additions & 5 deletions Engine/source/afx/ce/afxZodiac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,6 @@ void afxZodiacData::onPerformSubstitutions()
{
if (getTexture() != StringTable->EmptyString() && mTextureName != StringTable->insert("texhandle"))
{
if (mTextureAsset.notNull())
{
mTextureAsset->getChangedSignal().notify(this, &afxZodiacData::onImageChanged);
}

mTexture.set(getTexture(), mTextureProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
}
}
Expand Down
10 changes: 5 additions & 5 deletions Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
if( mUseModifiers )
baseName += modifiers[ i ];

mTextures[ i ].mTextureNormal = GFXTexHandle( mBitmapAsset->getImagePath(), &GFXDefaultGUIProfile, avar("%s() - mTextureNormal (line %d)", __FUNCTION__, __LINE__));
mTextures[ i ].mTextureNormal = GFXTexHandle( mBitmapAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureNormal (line %d)", __FUNCTION__, __LINE__));

if( mUseStates )
{
Expand All @@ -323,7 +323,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureNormalAsset->load();
if (mTextures[i].mTextureNormalAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureNormal = GFXTexHandle(mTextures[i].mTextureNormalAsset->getImagePath(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureNormal = GFXTexHandle(mTextures[i].mTextureNormalAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
break;
}
}
Expand All @@ -345,7 +345,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureHilightAsset->load();
if (mTextures[i].mTextureHilightAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureHilight = GFXTexHandle(mTextures[i].mTextureHilightAsset->getImagePath(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureHilight = GFXTexHandle(mTextures[i].mTextureHilightAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
break;
}
}
Expand All @@ -369,7 +369,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureDepressedAsset->load();
if (mTextures[i].mTextureDepressedAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureDepressed = GFXTexHandle(mTextures[i].mTextureDepressedAsset->getImagePath(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureDepressed = GFXTexHandle(mTextures[i].mTextureDepressedAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
break;
}
}
Expand All @@ -393,7 +393,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureInactiveAsset->load();
if (mTextures[i].mTextureInactiveAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureInactive = GFXTexHandle(mTextures[i].mTextureInactiveAsset->getImagePath(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureInactive = GFXTexHandle(mTextures[i].mTextureInactiveAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/source/gui/core/guiTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ bool GuiControlProfile::protectedSetBitmap( void *object, const char *index, con
{
if (profile->mBitmapAsset.notNull() && profile->getBitmap() != StringTable->insert("texHandle"))
{
profile->mBitmap.set(profile->mBitmapAsset->getImagePath(), profile->mBitmapProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
profile->mBitmap.set(profile->mBitmapAsset->getImageFile(), profile->mBitmapProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
}

//verify the bitmap
Expand Down Expand Up @@ -648,7 +648,7 @@ void GuiControlProfile::incLoadCount()
{
if (mBitmapAsset.notNull() && getBitmap() != StringTable->insert("texHandle"))
{
mBitmap.set(mBitmapAsset->getImagePath(), mBitmapProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
mBitmap.set(mBitmapAsset->getImageFile(), mBitmapProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__));
}

//verify the bitmap
Expand Down
Loading