Cleans up ShapeAsset of some unnecessary/redundant elements like extra material and animations tracking

Removed the old SHAPE_ASSET macros
Implements AssetRef struct that acts as a universal wrapper for an templated AssetPtr and AssetId pair
Adds Type handling for AssetRef for ShapeAsset to unify handling in classes that utilize a shapeAsset, so assigning an assetPtr or an assetId will keep a record of the assignment in the event the assetPtr is invalid.
Update all classes that utilized the old SHAPE_ASSET macros to utilize the AssetRef struct and updated the class code to utilize it to provide much more clean and concise code that isn't blocked behind macro definitions
Added a new example class: shapeDatablockExample which allows render of a simple shape object utilizing a simple example datablock.
This commit is contained in:
JeffR 2026-05-31 01:19:26 -05:00
parent c2c5674fe9
commit b44158cb89
52 changed files with 1860 additions and 1086 deletions

View file

@ -55,16 +55,12 @@
#include "gfx/bitmap/imageUtils.h"
StringTableEntry ShapeAsset::smNoShapeAssetFallback = NULL;
AssetPtr<ShapeAsset> ShapeAsset::smNoShapeAssetFallbackAssetPtr = NULL;
//-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(ShapeAsset);
//-----------------------------------------------------------------------------
// REFACTOR
//-----------------------------------------------------------------------------
IMPLEMENT_STRUCT(AssetPtr<ShapeAsset>, AssetPtrShapeAsset, , "")
END_IMPLEMENT_STRUCT
@ -131,8 +127,50 @@ ConsoleSetType(TypeShapeAssetId)
}
//-----------------------------------------------------------------------------
// REFACTOR END
//-----------------------------------------------------------------------------
IMPLEMENT_STRUCT(AssetRef<ShapeAsset>, AssetRefShapeAsset, , "")
END_IMPLEMENT_STRUCT
ConsoleType(ShapeAssetRef, TypeShapeAssetRef, AssetRef<ShapeAsset>, ASSET_ID_FIELD_PREFIX)
ConsoleGetType(TypeShapeAssetRef)
{
AssetRef<ShapeAsset>& ref = *((AssetRef<ShapeAsset>*)dptr);
if (ref.assetPtr.isNull())
return ref.assetId;
else
return ref.assetPtr.getAssetId();
}
ConsoleSetType(TypeShapeAssetRef)
{
// Was a single argument specified?
if (argc == 1)
{
// Yes, so fetch field value.
const char* pFieldValue = argv[0];
// Fetch asset pointer.
AssetRef<ShapeAsset>* pAssetRef = (AssetRef<ShapeAsset>*)(dptr);
// Is the asset pointer the correct type?
if (pAssetRef == NULL)
{
Con::warnf("(TypeShapeAssetRef) - Failed to set asset Id '%d'.", pFieldValue);
return;
}
// Set asset.
*pAssetRef = pFieldValue;
return;
}
// Warn.
Con::warnf("(TypeShapeAssetRef) - Cannot set multiple args to a single asset.");
}
const String ShapeAsset::mErrCodeStrings[] =
{
@ -170,7 +208,7 @@ void ShapeAsset::consoleInit()
Con::addVariable("$Core::NoShapeAssetFallback", TypeString, &smNoShapeAssetFallback,
"The assetId of the shape to display when the requested shape asset is missing.\n"
"@ingroup GFX\n");
smNoShapeAssetFallback = StringTable->insert(Con::getVariable("$Core::NoShapeAssetFallback"));
}
@ -194,20 +232,6 @@ void ShapeAsset::initPersistFields()
}
void ShapeAsset::setDataField(StringTableEntry slotName, StringTableEntry array, StringTableEntry value)
{
Parent::setDataField(slotName, array, value);
//Now, if it's a material slot of some fashion, set it up
StringTableEntry matSlotName = StringTable->insert("materialAsset");
if (String(slotName).startsWith(matSlotName))
{
StringTableEntry matId = StringTable->insert(value);
mMaterialAssetIds.push_back(matId);
}
}
void ShapeAsset::initializeAsset()
{
// Call parent.
@ -239,6 +263,16 @@ void ShapeAsset::initializeAsset()
String normalPath = String(mShapeFile) + "_imposter_normals.dds";
mNormalImposterFileName = StringTable->insert(normalPath.c_str());
}
//Make sure our fallback is valid
if (smNoShapeAssetFallbackAssetPtr.isNull())
{
smNoShapeAssetFallbackAssetPtr = smNoShapeAssetFallback;
if (smNoShapeAssetFallbackAssetPtr.isNull())
Con::errorf("ShapeAsset::initializeAsset could not find fallback asset %s!", smNoShapeAssetFallback);
else
smNoShapeAssetFallbackAssetPtr->load();
}
}
void ShapeAsset::setShapeFile(const char* pShapeFile)
@ -313,9 +347,9 @@ void ShapeAsset::setNormalImposterFile(const char* pImageFile)
refreshAsset();
}
void ShapeAsset::_onResourceChanged(const Torque::Path &path)
void ShapeAsset::_onResourceChanged(const Torque::Path& path)
{
if (path != Torque::Path(mShapeFile) )
if (path != Torque::Path(mShapeFile))
return;
refreshAsset();
@ -325,117 +359,64 @@ U32 ShapeAsset::load()
{
if (mLoadedState == AssetErrCode::Ok) return mLoadedState;
mMaterialAssets.clear();
mMaterialAssetIds.clear();
//First, load any material, animation, etc assets we may be referencing in our asset
// Find any asset dependencies.
AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
// Does the asset have any dependencies?
if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
{
// Iterate all dependencies.
while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
{
StringTableEntry assetType = mpOwningAssetManager->getAssetType(assetDependenciesItr->value);
if (assetType == StringTable->insert("MaterialAsset"))
{
mMaterialAssetIds.push_front(assetDependenciesItr->value);
//Force the asset to become initialized if it hasn't been already
AssetPtr<MaterialAsset> matAsset = assetDependenciesItr->value;
mMaterialAssets.push_front(matAsset);
}
else if (assetType == StringTable->insert("ShapeAnimationAsset"))
{
mAnimationAssetIds.push_back(assetDependenciesItr->value);
//Force the asset to become initialized if it hasn't been already
AssetPtr<ShapeAnimationAsset> animAsset = assetDependenciesItr->value;
mAnimationAssets.push_back(animAsset);
}
// Next dependency.
assetDependenciesItr++;
}
}
mShape = ResourceManager::get().load(mShapeFile);
if (!mShape)
{
mLoadedState = BadFileReference;
return mLoadedState; //if it failed to load, bail out
}
// Construct billboards if not done already
if (GFXDevice::devicePresent())
mShape->setupBillboardDetails(mShapeFile, mDiffuseImposterFileName, mNormalImposterFileName);
//If they exist, grab our imposters here and bind them to our shapeAsset
bool hasBlends = false;
//Now that we've successfully loaded our shape and have any materials and animations loaded
//we need to set up the animations we're using on our shape
for (S32 i = mAnimationAssets.size()-1; i >= 0; --i)
{
String srcName = mAnimationAssets[i]->getAnimationName();
String srcPath(mAnimationAssets[i]->getAnimationFilename());
//SplitSequencePathAndName(srcPath, srcName);
if (!mShape->addSequence(srcPath, mAnimationAssets[i]->getAssetId(), srcName, srcName,
mAnimationAssets[i]->getStartFrame(), mAnimationAssets[i]->getEndFrame(), mAnimationAssets[i]->getPadRotation(), mAnimationAssets[i]->getPadTransforms()))
{
mLoadedState = MissingAnimatons;
return mLoadedState;
}
if (mAnimationAssets[i]->isBlend())
hasBlends = true;
}
//if any of our animations are blends, set those up now
if (hasBlends)
{
for (U32 i=0; i < mAnimationAssets.size(); ++i)
{
if (mAnimationAssets[i]->isBlend() && mAnimationAssets[i]->getBlendAnimationName() != StringTable->EmptyString())
{
//gotta do a bit of logic here.
//First, we need to make sure the anim asset we depend on for our blend is loaded
AssetPtr<ShapeAnimationAsset> blendAnimAsset = mAnimationAssets[i]->getBlendAnimationName();
U32 assetStatus = ShapeAnimationAsset::getAssetErrCode(blendAnimAsset);
if (assetStatus != AssetBase::Ok)
{
Con::errorf("ShapeAsset::initializeAsset - Unable to acquire reference animation asset %s for asset %s to blend!", mAnimationAssets[i]->getBlendAnimationName(), mAnimationAssets[i]->getAssetName());
{
mLoadedState = MissingAnimatons;
return mLoadedState;
}
}
String refAnimName = blendAnimAsset->getAnimationName();
if (!mShape->setSequenceBlend(mAnimationAssets[i]->getAnimationName(), true, blendAnimAsset->getAnimationName(), mAnimationAssets[i]->getBlendFrame()))
{
Con::errorf("ShapeAnimationAsset::initializeAsset - Unable to set animation clip %s for asset %s to blend!", mAnimationAssets[i]->getAnimationName(), mAnimationAssets[i]->getAssetName());
{
mLoadedState = MissingAnimatons;
return mLoadedState;
}
}
}
}
}
mLoadedState = Ok;
return mLoadedState;
}
bool ShapeAsset::preloadMaterialList()
{
if (!mShape)
return false;
return mShape->preloadMaterialList(getShapeFile());
}
TSShape* ShapeAsset::getShape()
{
AssetErrCode result = static_cast<AssetErrCode>(load());
if (mShape)
{
return mShape;
}
else if (smNoShapeAssetFallbackAssetPtr.notNull())
{
return smNoShapeAssetFallbackAssetPtr->getShape();
}
return NULL;
}
Resource<TSShape> ShapeAsset::getShapeResource()
{
AssetErrCode result = static_cast<AssetErrCode>(load());
if (mShape)
{
return mShape;
}
else if (smNoShapeAssetFallbackAssetPtr.notNull())
{
return smNoShapeAssetFallbackAssetPtr->getShapeResource();
}
return Resource<TSShape>(NULL);
}
//------------------------------------------------------------------------------
//Utility function to 'fill out' bindings and resources with a matching asset if one exists
U32 ShapeAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset)
@ -560,6 +541,44 @@ U32 ShapeAsset::getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* sha
return AssetErrCode::UsingFallback;
}
}
StringTableEntry ShapeAsset::getMaterial(const S32& index)
{
if (!mShape)
return StringTable->EmptyString();
if (index < 0 || index >= mShape->materialList->size())
return StringTable->EmptyString();
return mShape->materialList->getMaterialName(index);
}
U32 ShapeAsset::getMaterialCount()
{
if (!mShape)
return 0;
return mShape->materialList->size();
}
StringTableEntry ShapeAsset::getAnimation(const S32& index)
{
if (!mShape)
return StringTable->EmptyString();
if (index < 0 || index >= mShape->sequences.size())
return StringTable->EmptyString();
return mShape->getName(mShape->sequences[index].nameIndex);
}
U32 ShapeAsset::getAnimationCount()
{
if (!mShape)
return 0;
return mShape->sequences.size();
}
//------------------------------------------------------------------------------
void ShapeAsset::copyTo(SimObject* object)
@ -589,10 +608,10 @@ void ShapeAsset::onTamlPreWrite(void)
Parent::onTamlPreWrite();
// ensure paths are collapsed.
mShapeFile = collapseAssetFilePath(mShapeFile);
mConstructorFileName = collapseAssetFilePath(mConstructorFileName);
mDiffuseImposterFileName = collapseAssetFilePath(mDiffuseImposterFileName);
mNormalImposterFileName = collapseAssetFilePath(mNormalImposterFileName);
mShapeFile = collapseAssetFilePath(mShapeFile);
mConstructorFileName = collapseAssetFilePath(mConstructorFileName);
mDiffuseImposterFileName = collapseAssetFilePath(mDiffuseImposterFileName);
mNormalImposterFileName = collapseAssetFilePath(mNormalImposterFileName);
}
void ShapeAsset::onTamlPostWrite(void)
@ -601,42 +620,10 @@ void ShapeAsset::onTamlPostWrite(void)
Parent::onTamlPostWrite();
// ensure paths are expanded.
mShapeFile = expandAssetFilePath(mShapeFile);
mConstructorFileName = expandAssetFilePath(mConstructorFileName);
mDiffuseImposterFileName = expandAssetFilePath(mDiffuseImposterFileName);
mNormalImposterFileName = expandAssetFilePath(mNormalImposterFileName);
}
void ShapeAsset::SplitSequencePathAndName(String& srcPath, String& srcName)
{
srcName = "";
// Determine if there is a sequence name at the end of the source string, and
// if so, split the filename from the sequence name
S32 split = srcPath.find(' ', 0, String::Right);
S32 split2 = srcPath.find('\t', 0, String::Right);
if ((split == String::NPos) || (split2 > split))
split = split2;
if (split != String::NPos)
{
split2 = split + 1;
while ((srcPath[split2] != '\0') && dIsspace(srcPath[split2]))
split2++;
// now 'split' is at the end of the path, and 'split2' is at the start of the sequence name
srcName = srcPath.substr(split2);
srcPath = srcPath.erase(split, srcPath.length() - split);
}
}
ShapeAnimationAsset* ShapeAsset::getAnimation(S32 index)
{
if (index < mAnimationAssets.size())
{
return mAnimationAssets[index];
}
return NULL;
mShapeFile = expandAssetFilePath(mShapeFile);
mConstructorFileName = expandAssetFilePath(mConstructorFileName);
mDiffuseImposterFileName = expandAssetFilePath(mDiffuseImposterFileName);
mNormalImposterFileName = expandAssetFilePath(mNormalImposterFileName);
}
#ifdef TORQUE_TOOLS
@ -655,7 +642,7 @@ const char* ShapeAsset::generateCachedPreviewImage(S32 resolution, String overri
if (overrideMaterial.isNotEmpty())
{
Material *tMat = dynamic_cast<Material*>(Sim::findObject(overrideMaterial));
Material* tMat = dynamic_cast<Material*>(Sim::findObject(overrideMaterial));
if (tMat)
shape->reSkin(tMat->mMapTo, mShape->materialList->getMaterialName(0));
}
@ -702,7 +689,7 @@ const char* ShapeAsset::generateCachedPreviewImage(S32 resolution, String overri
dSprintf(returnBuffer, 128, "%s", dumpPath.c_str());
imposter->writeBitmap("png", dumpPath);
delete imposter;
delete imposterNrml;
@ -714,6 +701,13 @@ const char* ShapeAsset::generateCachedPreviewImage(S32 resolution, String overri
}
#endif
DefineEngineMethod(ShapeAsset, getMaterial, const char*, (S32 index), (0),
"Gets the number of materials for this shape asset.\n"
"@return Material count.\n")
{
return object->getMaterial(index);
}
DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
"Gets the number of materials for this shape asset.\n"
"@return Material count.\n")
@ -721,14 +715,7 @@ DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
return object->getMaterialCount();
}
DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
"Gets the number of animations for this shape asset.\n"
"@return Animation count.\n")
{
return object->getAnimationCount();
}
DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index), (0),
DefineEngineMethod(ShapeAsset, getAnimation, const char*, (S32 index), (0),
"Gets a particular shape animation asset for this shape.\n"
"@param animation asset index.\n"
"@return Shape Animation Asset.\n")
@ -736,6 +723,13 @@ DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index),
return object->getAnimation(index);
}
DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
"Gets the number of animations for this shape asset.\n"
"@return Animation count.\n")
{
return object->getAnimationCount();
}
DefineEngineMethod(ShapeAsset, getShapePath, const char*, (), ,
"Gets the shape's file path\n"
"@return The filename of the shape file")
@ -784,7 +778,7 @@ ConsoleDocClass(GuiInspectorTypeShapeAssetPtr,
"@brief Inspector field type for Shapes\n\n"
"Editor use only.\n\n"
"@internal"
);
);
void GuiInspectorTypeShapeAssetPtr::consoleInit()
{
@ -940,7 +934,7 @@ void GuiInspectorTypeShapeAssetPtr::updatePreviewImage()
//if what we're working with isn't even a valid asset, don't present like we found a good one
if (!AssetDatabase.isDeclaredAsset(previewImage))
{
mPreviewImage->_setBitmap(StringTable->EmptyString());
mPreviewImage->setBitmap(StringTable->EmptyString());
return;
}
@ -953,7 +947,7 @@ void GuiInspectorTypeShapeAssetPtr::updatePreviewImage()
}
if (mPreviewImage->getBitmapAsset().isNull())
mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
mPreviewImage->setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
}
void GuiInspectorTypeShapeAssetPtr::setPreviewImage(StringTableEntry assetId)
@ -961,7 +955,7 @@ void GuiInspectorTypeShapeAssetPtr::setPreviewImage(StringTableEntry assetId)
//if what we're working with isn't even a valid asset, don't present like we found a good one
if (!AssetDatabase.isDeclaredAsset(assetId))
{
mPreviewImage->_setBitmap(StringTable->EmptyString());
mPreviewImage->setBitmap(StringTable->EmptyString());
return;
}
@ -974,7 +968,7 @@ void GuiInspectorTypeShapeAssetPtr::setPreviewImage(StringTableEntry assetId)
}
if (mPreviewImage->getBitmapAsset().isNull())
mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
mPreviewImage->setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
}
IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetId);
@ -991,4 +985,204 @@ void GuiInspectorTypeShapeAssetId::consoleInit()
ConsoleBaseType::getType(TypeShapeAssetId)->setInspectorFieldType("GuiInspectorTypeShapeAssetId");
}
//-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetRef);
ConsoleDocClass(GuiInspectorTypeShapeAssetRef,
"@brief Inspector field type for Shapes\n\n"
"Editor use only.\n\n"
"@internal"
);
void GuiInspectorTypeShapeAssetRef::consoleInit()
{
Parent::consoleInit();
ConsoleBaseType::getType(TypeShapeAssetRef)->setInspectorFieldType("GuiInspectorTypeShapeAssetRef");
}
GuiControl* GuiInspectorTypeShapeAssetRef::constructEditControl()
{
// Create base filename edit controls
GuiControl* retCtrl = Parent::constructEditControl();
if (retCtrl == NULL)
return retCtrl;
// Change filespec
char szBuffer[512];
const char* previewImage;
if (mInspector->getInspectObject() != NULL)
{
StringBuilder varNameStr;
varNameStr.append(mCaption);
if (mFieldArrayIndex != NULL)
{
varNameStr.append("[");
varNameStr.append(mFieldArrayIndex);
varNameStr.append("]");
}
dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
mInspector->getIdString(), varNameStr.end().c_str());
mBrowseButton->setField("Command", szBuffer);
setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
previewImage = mInspector->getInspectObject()->getDataField(varNameStr.end().c_str(), NULL);
}
else
{
//if we don't have a target object, we'll be manipulating the desination value directly
dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
mInspector->getIdString(), mVariableName);
mBrowseButton->setField("Command", szBuffer);
previewImage = Con::getVariable(mVariableName);
}
mLabel = new GuiTextCtrl();
mLabel->registerObject();
mLabel->setControlProfile(mProfile);
mLabel->setText(mCaption);
addObject(mLabel);
//
GuiTextEditCtrl* editTextCtrl = static_cast<GuiTextEditCtrl*>(retCtrl);
GuiControlProfile* toolEditProfile;
if (Sim::findObject("ToolsGuiTextEditProfile", toolEditProfile))
editTextCtrl->setControlProfile(toolEditProfile);
GuiControlProfile* toolDefaultProfile = NULL;
Sim::findObject("ToolsGuiDefaultProfile", toolDefaultProfile);
//
mPreviewImage = new GuiBitmapCtrl();
mPreviewImage->registerObject();
if (toolDefaultProfile)
mPreviewImage->setControlProfile(toolDefaultProfile);
updatePreviewImage();
addObject(mPreviewImage);
//
mPreviewBorderButton = new GuiBitmapButtonCtrl();
mPreviewBorderButton->registerObject();
if (toolDefaultProfile)
mPreviewBorderButton->setControlProfile(toolDefaultProfile);
mPreviewBorderButton->_setBitmap(StringTable->insert("ToolsModule:cubemapBtnBorder_n_image"));
mPreviewBorderButton->setField("Command", szBuffer); //clicking the preview does the same thing as the edit button, for simplicity
addObject(mPreviewBorderButton);
//
// Create "Open in Editor" button
mEditButton = new GuiBitmapButtonCtrl();
dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAssetId(%d.getText());", retCtrl->getId());
mEditButton->setField("Command", szBuffer);
mEditButton->setText("Edit");
mEditButton->setSizing(horizResizeLeft, vertResizeAspectTop);
mEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile");
mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Shape Editor");
mEditButton->registerObject();
addObject(mEditButton);
//
mUseHeightOverride = true;
mHeightOverride = 72;
return retCtrl;
}
bool GuiInspectorTypeShapeAssetRef::updateRects()
{
S32 rowSize = 18;
S32 dividerPos, dividerMargin;
mInspector->getDivider(dividerPos, dividerMargin);
Point2I fieldExtent = getExtent();
Point2I fieldPos = getPosition();
mEditCtrlRect.set(0, 0, fieldExtent.x, fieldExtent.y);
mLabel->resize(Point2I(mProfile->mTextOffset.x, 0), Point2I(fieldExtent.x, rowSize));
RectI previewRect = RectI(Point2I(mProfile->mTextOffset.x, rowSize), Point2I(50, 50));
mPreviewBorderButton->resize(previewRect.point, previewRect.extent);
mPreviewImage->resize(previewRect.point, previewRect.extent);
S32 editPos = previewRect.point.x + previewRect.extent.x + 10;
mEdit->resize(Point2I(editPos, rowSize * 1.5), Point2I(fieldExtent.x - editPos - 5, rowSize));
mEditButton->resize(Point2I(fieldExtent.x - 105, previewRect.point.y + previewRect.extent.y - rowSize), Point2I(100, rowSize));
mBrowseButton->setHidden(true);
return true;
}
void GuiInspectorTypeShapeAssetRef::updateValue()
{
Parent::updateValue();
updatePreviewImage();
}
void GuiInspectorTypeShapeAssetRef::updatePreviewImage()
{
const char* previewImage;
if (mInspector->getInspectObject() != NULL)
previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
else
previewImage = Con::getVariable(mVariableName);
//if what we're working with isn't even a valid asset, don't present like we found a good one
if (!AssetDatabase.isDeclaredAsset(previewImage))
{
mPreviewImage->setBitmap(StringTable->EmptyString());
return;
}
String shpPreviewAssetId = String(previewImage) + "_PreviewImage";
shpPreviewAssetId.replace(":", "_");
shpPreviewAssetId = "ToolsModule:" + shpPreviewAssetId;
if (AssetDatabase.isDeclaredAsset(shpPreviewAssetId.c_str()))
{
mPreviewImage->setBitmap(StringTable->insert(shpPreviewAssetId.c_str()));
}
if (mPreviewImage->getBitmapAsset().isNull())
mPreviewImage->setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
}
void GuiInspectorTypeShapeAssetRef::setPreviewImage(StringTableEntry assetId)
{
//if what we're working with isn't even a valid asset, don't present like we found a good one
if (!AssetDatabase.isDeclaredAsset(assetId))
{
mPreviewImage->setBitmap(StringTable->EmptyString());
return;
}
String shpPreviewAssetId = String(assetId) + "_PreviewImage";
shpPreviewAssetId.replace(":", "_");
shpPreviewAssetId = "ToolsModule:" + shpPreviewAssetId;
if (AssetDatabase.isDeclaredAsset(shpPreviewAssetId.c_str()))
{
mPreviewImage->setBitmap(StringTable->insert(shpPreviewAssetId.c_str()));
}
if (mPreviewImage->getBitmapAsset().isNull())
mPreviewImage->setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
}
#endif

View file

@ -77,9 +77,10 @@ public:
};
static StringTableEntry smNoShapeAssetFallback;
static AssetPtr<ShapeAsset> smNoShapeAssetFallbackAssetPtr;
static const String mErrCodeStrings[U32(ShapeAssetErrCode::Extended) - U32(Parent::Extended) + 1];
static U32 getAssetErrCode(ConcreteAssetPtr checkAsset) { if (checkAsset) return checkAsset->mLoadedState; else return 0; }
static U32 getAssetErrCode(const ConcreteAssetPtr& checkAsset) { if (checkAsset) return checkAsset->mLoadedState; else return 0; }
static String getAssetErrstrn(U32 errCode)
{
@ -94,14 +95,6 @@ private:
StringTableEntry mDiffuseImposterFileName;
StringTableEntry mNormalImposterFileName;
//Material assets we're dependent on and use
Vector<StringTableEntry> mMaterialAssetIds;
Vector<AssetPtr<MaterialAsset>> mMaterialAssets;
//Animation assets we're dependent on and use
Vector<StringTableEntry> mAnimationAssetIds;
Vector<AssetPtr<ShapeAnimationAsset>> mAnimationAssets;
Resource<TSShape> mShape;
public:
@ -115,39 +108,17 @@ public:
static void initPersistFields();
void copyTo(SimObject* object) override;
virtual void setDataField(StringTableEntry slotName, StringTableEntry array, StringTableEntry value);
/// Declare Console Object.
DECLARE_CONOBJECT(ShapeAsset);
U32 load() override;
bool preloadMaterialList();
TSShape* getShape() { load(); return mShape; }
Resource<TSShape> getShapeResource() { load(); return mShape; }
void SplitSequencePathAndName(String& srcPath, String& srcName);
TSShape* getShape();
Resource<TSShape> getShapeResource();
U32 getShapeFilenameHash() { return _StringTable::hashString(mShapeFile); }
Vector<AssetPtr<MaterialAsset>> getMaterialAssets() { return mMaterialAssets; }
inline AssetPtr<MaterialAsset> getMaterialAsset(U32 matId)
{
if (matId >= mMaterialAssets.size())
return NULL;
else
return mMaterialAssets[matId];
}
void clearMaterialAssets() { mMaterialAssets.clear(); }
void addMaterialAssets(AssetPtr<MaterialAsset> matPtr) { mMaterialAssets.push_back(matPtr); }
S32 getMaterialCount() { return mMaterialAssets.size(); }
S32 getAnimationCount() { return mAnimationAssets.size(); }
ShapeAnimationAsset* getAnimation(S32 index);
void _onResourceChanged(const Torque::Path& path);
void setShapeFile(const char* pScriptFile);
@ -163,11 +134,18 @@ public:
void setNormalImposterFile(const char* pImageFile);
inline StringTableEntry getNormalImposterFile(void) const { return mNormalImposterFileName; };
static U32 getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset);
static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
static U32 getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset);
StringTableEntry getMaterial(const S32& index);
U32 getMaterialCount();
StringTableEntry getAnimation(const S32& index);
U32 getAnimationCount();
#ifdef TORQUE_TOOLS
const char* generateCachedPreviewImage(S32 resolution, String overrideMaterial = "");
#endif
@ -201,6 +179,9 @@ DefineConsoleType(TypeShapeAssetId, String)
DECLARE_STRUCT(AssetPtr<ShapeAsset>)
DefineConsoleType(TypeShapeAssetPtr, AssetPtr<ShapeAsset>)
DECLARE_STRUCT(AssetRef<ShapeAsset>)
DefineConsoleType(TypeShapeAssetRef, AssetRef<ShapeAsset>)
#ifdef TORQUE_TOOLS
//-----------------------------------------------------------------------------
// TypeAssetId GuiInspectorField Class
@ -236,255 +217,29 @@ public:
static void consoleInit();
};
//-----------------------------------------------------------------------------
class GuiInspectorTypeShapeAssetRef : public GuiInspectorTypeFileName
{
typedef GuiInspectorTypeFileName Parent;
public:
GuiTextCtrl* mLabel;
GuiBitmapButtonCtrl* mPreviewBorderButton;
GuiBitmapCtrl* mPreviewImage;
GuiButtonCtrl* mEditButton;
DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetRef);
static void consoleInit();
GuiControl* constructEditControl() override;
bool updateRects() override;
void updateValue() override;
void updatePreviewImage();
void setPreviewImage(StringTableEntry assetId);
};
#endif
//-----------------------------------------------------------------------------
// REFACTOR
//-----------------------------------------------------------------------------
#pragma region Refactor Asset Macros
#define DECLARE_SHAPEASSET_REFACTOR(className, name) \
private: \
AssetPtr<ShapeAsset> m##name##Asset; \
StringTableEntry m##name##File = StringTable->EmptyString(); \
public: \
void _set##name(StringTableEntry _in) { \
if (m##name##Asset.getAssetId() == _in) \
return; \
if(get##name##File() == _in) \
return; \
if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset = NULL; \
m##name##File = ""; \
return; \
} \
if (!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry shapeAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
shapeAssetId = query.mAssetList[0]; \
} \
else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
{ \
shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
{ \
ShapeAsset* privateShape = new ShapeAsset(); \
privateShape->setShapeFile(_in); \
shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
} \
m##name##Asset = shapeAssetId; \
m##name##File = _in; \
} \
else \
{ \
m##name##Asset = _in; \
m##name##File = get##name##File(); \
} \
}; \
\
inline StringTableEntry _get##name##AssetId(void) const { return m##name##Asset.getAssetId(); } \
TSShape* get##name() { if (m##name##Asset.notNull()) return m##name##Asset->getShape(); else return NULL; } \
AssetPtr<ShapeAsset> 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; } \
StringTableEntry get##name##File() { return m##name##Asset.notNull() ? m##name##Asset->getShapeFile() : ""; }
#define DECLARE_SHAPEASSET_NET_REFACTOR(className, name, mask) \
private: \
AssetPtr<ShapeAsset> m##name##Asset; \
StringTableEntry m##name##File = StringTable->EmptyString(); \
public: \
void _set##name(StringTableEntry _in) { \
if (m##name##Asset.getAssetId() == _in) \
return; \
if(get##name##File() == _in) \
return; \
if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset = NULL; \
m##name##File = ""; \
setMaskBits(mask); \
return; \
} \
if (!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry shapeAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
shapeAssetId = query.mAssetList[0]; \
} \
else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
{ \
shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
{ \
ShapeAsset* privateShape = new ShapeAsset(); \
privateShape->setShapeFile(_in); \
shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
} \
m##name##Asset = shapeAssetId; \
m##name##File = _in; \
} \
else \
{ \
m##name##Asset = _in; \
m##name##File = get##name##File(); \
} \
setMaskBits(mask); \
}; \
\
inline StringTableEntry _get##name##AssetId(void) const { return m##name##Asset.getAssetId(); } \
TSShape* get##name() { if (m##name##Asset.notNull()) return m##name##Asset->getShape(); else return NULL; } \
AssetPtr<ShapeAsset> 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; } \
StringTableEntry get##name##File() { return m##name##Asset.notNull() ? m##name##Asset->getShapeFile() : ""; }
#define INITPERSISTFIELD_SHAPEASSET_REFACTOR(name, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeShapeAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.)); \
addProtectedField(assetText(name, File), TypeFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, file docs.), AbstractClassRep::FIELD_HideInInspectors);
#define DECLARE_SHAPEASSET_ARRAY_REFACTOR(className, name, max) \
private: \
AssetPtr<ShapeAsset> m##name##Asset[max]; \
StringTableEntry m##name##File[max] = {StringTable->EmptyString() }; \
public: \
void _set##name(StringTableEntry _in, const U32& index){ \
if (m##name##Asset[index].getAssetId() == _in) \
return; \
if(get##name##File(index) == _in) \
return; \
if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset[index] = NULL; \
m##name##File[index] = ""; \
return; \
} \
if (!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry shapeAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
shapeAssetId = query.mAssetList[0]; \
} \
else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
{ \
shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
{ \
ShapeAsset* privateShape = new ShapeAsset(); \
privateShape->setShapeFile(_in); \
shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
} \
m##name##Asset[index] = shapeAssetId; \
m##name##File[index] = _in; \
} \
else \
{ \
m##name##Asset[index] = _in; \
m##name##File[index] = get##name##File(index); \
} \
}; \
\
inline StringTableEntry _get##name##AssetId(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
TSShape* get##name(const U32& index) { if (m##name##Asset[index].notNull()) return m##name##Asset[index]->getShape(); else return NULL; } \
AssetPtr<ShapeAsset> 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;}\
StringTableEntry get##name##File(const U32& idx) { return m##name##Asset[idx].notNull() ? m##name##Asset[idx]->getShapeFile() : ""; }
#define DECLARE_SHAPEASSET_ARRAY_NET_REFACTOR(className, name, max, mask) \
private: \
AssetPtr<ShapeAsset> m##name##Asset[max]; \
StringTableEntry m##name##File[max] = {StringTable->EmptyString() }; \
public: \
void _set##name(StringTableEntry _in, const U32& index){ \
if (m##name##Asset[index].getAssetId() == _in) \
return; \
if(get##name##File(index) == _in) \
return; \
if (_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
{ \
m##name##Asset[index] = NULL; \
m##name##File[index] = ""; \
setMaskBits(mask); \
return; \
} \
if (!AssetDatabase.isDeclaredAsset(_in)) \
{ \
StringTableEntry shapeAssetId = StringTable->EmptyString(); \
AssetQuery query; \
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
if (foundAssetcount != 0) \
{ \
shapeAssetId = query.mAssetList[0]; \
} \
else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
{ \
shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
{ \
ShapeAsset* privateShape = new ShapeAsset(); \
privateShape->setShapeFile(_in); \
shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
} \
} \
else \
{ \
Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
} \
m##name##Asset[index] = shapeAssetId; \
m##name##File[index] = _in; \
} \
else \
{ \
m##name##Asset[index] = _in; \
m##name##File[index] = get##name##File(index); \
} \
setMaskBits(mask); \
}; \
\
inline StringTableEntry _get##name##AssetId(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
TSShape* get##name(const U32& index) { if (m##name##Asset[index].notNull()) return m##name##Asset[index]->getShape(); else return NULL; } \
AssetPtr<ShapeAsset> 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;}\
StringTableEntry get##name##File(const U32& idx) { return m##name##Asset[idx].notNull() ? m##name##Asset[idx]->getShapeFile() : ""; }
#define INITPERSISTFIELD_SHAPEASSET_ARRAY_REFACTOR(name, arraySize, consoleClass, docs) \
addProtectedField(assetText(name, Asset), TypeShapeAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));\
addProtectedField(assetText(name, File), TypeFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
#pragma endregion
//-----------------------------------------------------------------------------
// REFACTOR END
//-----------------------------------------------------------------------------
#endif