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
Prev Previous commit
Next Next commit
gui image asset refactor
bitmap button popup ctrl ex
  • Loading branch information
marauder2k7 committed Jan 23, 2025
commit 4d893f51cff27c03cc1648895b75146997f01f47
12 changes: 2 additions & 10 deletions Engine/source/T3D/assets/ImageAsset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,7 @@ void GuiInspectorTypeImageAssetPtr::updatePreviewImage()
{
if (AssetDatabase.isDeclaredAsset(previewImage))
{
ImageAsset* imgAsset = AssetDatabase.acquireAsset<ImageAsset>(previewImage);
if (imgAsset && imgAsset->isAssetValid())
{
mPreviewImage->_setBitmap(imgAsset->getAssetId());
}
mPreviewImage->_setBitmap(previewImage);
}
}

Expand Down Expand Up @@ -892,11 +888,7 @@ void GuiInspectorTypeImageAssetPtr::setPreviewImage(StringTableEntry assetId)
{
if (AssetDatabase.isDeclaredAsset(assetId))
{
ImageAsset* imgAsset = AssetDatabase.acquireAsset<ImageAsset>(assetId);
if (imgAsset && imgAsset->isAssetValid())
{
mPreviewImage->_setBitmap(imgAsset->getAssetId());
}
mPreviewImage->_setBitmap(assetId);
}
}

Expand Down
35 changes: 35 additions & 0 deletions Engine/source/T3D/assets/ImageAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,39 @@ public:
#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.));


#define DECLARE_IMAGEASSET_ARRAY_REFACTOR(className, name, profile, max) \
private: \
AssetPtr<ImageAsset> m##name##Asset[max]; \
public: \
void _set##name(StringTableEntry _in, const U32& index){ \
if(m##name##Asset[index].getAssetId() == _in) \
return; \
\
if(!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback; \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
imageAssetId = query.mAssetList[0]; \
} \
m##name##Asset[index] = imageAssetId; \
} \
else \
{ \
m##name##Asset[index] = _in; \
} \
}; \
\
inline StringTableEntry _get##name(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
GFXTexHandle get##name(const U32& index) { return m##name##Asset[index].notNull() ? m##name##Asset[index]->getTexture(&profile) : NULL; } \
AssetPtr<ImageAsset> get##name##Asset(const U32& index) { return m##name##Asset[index]; } \
static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data), dAtoi(index)); return false;}

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


#pragma endregion
32 changes: 15 additions & 17 deletions Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ GuiBitmapButtonCtrl::GuiBitmapButtonCtrl()
setExtent( 140, 30 );
mMasked = false;
mColor = ColorI::WHITE;
INIT_ASSET(Bitmap);
mBitmapName = StringTable->EmptyString();
mBitmap = NULL;
mBitmapAsset.registerRefreshNotify(this);
}

//-----------------------------------------------------------------------------
Expand All @@ -139,14 +141,10 @@ void GuiBitmapButtonCtrl::initPersistFields()
docsURL;
addGroup( "Bitmap" );

addProtectedField("Bitmap", TypeImageFilename, Offset(mBitmapName, GuiBitmapButtonCtrl), _setBitmapFieldData, &defaultProtectedGetFn, "Texture file to display on this button.\n"
INITPERSISTFIELD_IMAGEASSET_REFACTOR(Bitmap, GuiBitmapButtonCtrl,"Texture file to display on this button.\n"
"If useStates is false, this will be the file that renders on the control. Otherwise, this will "
"specify the default texture name to which the various state and modifier suffixes are appended "
"to find the per-state and per-modifier (if enabled) textures.", AbstractClassRep::FIELD_HideInInspectors); \
addProtectedField("BitmapAsset", TypeImageAssetId, Offset(mBitmapAssetId, GuiBitmapButtonCtrl), _setBitmapFieldData, &defaultProtectedGetFn, "Texture file to display on this button.\n"
"If useStates is false, this will be the file that renders on the control. Otherwise, this will "
"specify the default texture name to which the various state and modifier suffixes are appended "
"to find the per-state and per-modifier (if enabled) textures.");
"to find the per-state and per-modifier (if enabled) textures.")

addField("color", TypeColorI, Offset(mColor, GuiBitmapButtonCtrl), "color mul");

Expand Down Expand Up @@ -184,7 +182,7 @@ bool GuiBitmapButtonCtrl::onWake()
return false;

setActive( true );
setBitmap( getBitmap() );
setBitmap( mBitmapName );

return true;
}
Expand Down Expand Up @@ -242,7 +240,7 @@ void GuiBitmapButtonCtrl::inspectPostApply()
{
Parent::inspectPostApply();

setBitmap(getBitmap());
setBitmap(mBitmapName);

// if the extent is set to (0,0) in the gui editor and appy hit, this control will
// set it's extent to be exactly the size of the normal bitmap (if present)
Expand Down Expand Up @@ -274,7 +272,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )

if( mBitmapAsset.notNull())
{
if( dStricmp( getBitmap(), "texhandle" ) != 0 )
if( dStricmp( mBitmapName, "texhandle" ) != 0 )
{
const U32 count = mUseModifiers ? NumModifiers : 1;
for( U32 i = 0; i < count; ++ i )
Expand All @@ -292,7 +290,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
static String s_h[2] = { "_h", "_h_image" };
static String s_i[2] = { "_i", "_i_image" };

String baseName = mBitmapAssetId;
String baseName = mBitmapAsset.getAssetId();

//strip any pre-assigned suffix, just in case
baseName = baseName.replace("_n_image", "");
Expand All @@ -301,7 +299,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
if( mUseModifiers )
baseName += modifiers[ i ];

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

if( mUseStates )
{
Expand All @@ -323,7 +321,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureNormalAsset->load();
if (mTextures[i].mTextureNormalAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureNormal = GFXTexHandle(mTextures[i].mTextureNormalAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureNormal = mTextures[i].mTextureNormalAsset->getTexture(&GFXDefaultGUIProfile);
break;
}
}
Expand All @@ -345,7 +343,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureHilightAsset->load();
if (mTextures[i].mTextureHilightAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureHilight = GFXTexHandle(mTextures[i].mTextureHilightAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureHilight = mTextures[i].mTextureHilightAsset->getTexture(&GFXDefaultGUIProfile);
break;
}
}
Expand All @@ -369,7 +367,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureDepressedAsset->load();
if (mTextures[i].mTextureDepressedAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureDepressed = GFXTexHandle(mTextures[i].mTextureDepressedAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureDepressed = mTextures[i].mTextureDepressedAsset->getTexture(&GFXDefaultGUIProfile);
break;
}
}
Expand All @@ -393,7 +391,7 @@ void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
mTextures[i].mTextureInactiveAsset->load();
if (mTextures[i].mTextureInactiveAsset->getStatus() == AssetBase::Ok)
{
mTextures[i].mTextureInactive = GFXTexHandle(mTextures[i].mTextureInactiveAsset->getImageFile(), &GFXDefaultGUIProfile, avar("%s() - mTextureDepressed (line %d)", __FUNCTION__, __LINE__));
mTextures[i].mTextureInactive = mTextures[i].mTextureInactiveAsset->getTexture(&GFXDefaultGUIProfile);
break;
}
}
Expand Down Expand Up @@ -670,4 +668,4 @@ bool GuiBitmapButtonCtrl::pointInControl(const Point2I& parentCoordPoint)
return Parent::pointInControl(parentCoordPoint);
}

DEF_ASSET_BINDS(GuiBitmapButtonCtrl, Bitmap);
DEF_ASSET_BINDS_REFACTOR(GuiBitmapButtonCtrl, Bitmap)
47 changes: 30 additions & 17 deletions Engine/source/gui/buttons/guiBitmapButtonCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/// To implement different handlers for the modifier states, use the "onDefaultClick",
/// "onCtrlClick", "onAltClick", and "onShiftClick" methods.
///
class GuiBitmapButtonCtrl : public GuiButtonCtrl
class GuiBitmapButtonCtrl : public GuiButtonCtrl, protected AssetPtrCallback
{
public:

Expand Down Expand Up @@ -118,9 +118,35 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl
///
BitmapMode mBitmapMode;

DECLARE_IMAGEASSET(GuiBitmapButtonCtrl, Bitmap, onBitmapChange, GFXDefaultGUIProfile);
DECLARE_ASSET_SETGET(GuiBitmapButtonCtrl, Bitmap);

private: AssetPtr<ImageAsset> mBitmapAsset; public: void _setBitmap(StringTableEntry _in) {
if (mBitmapAsset.getAssetId() == _in) return; if (!AssetDatabase.isDeclaredAsset(_in)) {
StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback; AssetQuery query; S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); if (foundAssetcount != 0) {
imageAssetId = query.mAssetList[0];
} mBitmapAsset = imageAssetId;
}
else {
mBitmapAsset = _in;
mBitmapName = _in;
mBitmap = getBitmap();
}
}; inline StringTableEntry _getBitmap(void) const {
return mBitmapAsset.getAssetId();
} GFXTexHandle getBitmap() {
return mBitmapAsset.notNull() ? mBitmapAsset->getTexture(&GFXDefaultGUIProfile) : 0;
} AssetPtr<ImageAsset> getBitmapAsset(void) {
return mBitmapAsset;
} static bool _setBitmapData(void* obj, const char* index, const char* data) {
static_cast<GuiBitmapButtonCtrl*>(obj)->_setBitmap(_getStringTable()->insert(data)); return false;
}
protected:

void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
{
setBitmap(mBitmapName);
}

GFXTexHandle mBitmap;
StringTableEntry mBitmapName;
/// alpha masking
bool mMasked;

Expand Down Expand Up @@ -158,11 +184,6 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl

/// @}

void onBitmapChange()
{
setBitmap(getBitmap());
}

public:

GuiBitmapButtonCtrl();
Expand All @@ -185,14 +206,6 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl
DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
"The individual button states are represented with separate bitmaps." );

//Basically a wrapper function to do our special state handling setup when the fields change
static bool _setBitmapFieldData(void* obj, const char* index, const char* data)
{
GuiBitmapButtonCtrl* object = static_cast<GuiBitmapButtonCtrl*>(obj);
object->setBitmap(StringTable->insert(data));
return false;
}
};

typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
Expand Down
Loading