AeAsset.c++ 5.92 KB
#include <assert.h>
#include <string.h>
#include <stdio.h>

#include "GList.h"
#include "AeAsset.h"
#include "AeAssetBase.h"
#include "AeAssetBin.h"


AeAsset::AeAsset (const char * name, TAssetType type)
{
    assert (strlen(name) < sizeof(TString63));

    strncpy (fName, name, sizeof(TString63)-1);
    fType = type;
    fIsSelected = False;
    fIsOnline = False;
    fClientDataList = new GList<void *> (1);
    fParentList = new GList<AeAsset *> (1);    
}

AeAsset::~AeAsset (void)
{
    if (fClientDataList)
	delete fClientDataList;

    if (fParentList)
    {
	AeAsset *	parent;
	int		i;

	// remove the asset from any child lists
	fParentList->Start (i, kReverse);
	while (fParentList->Prev (i, parent))
	    if (parent->CanContain ())
		((AeContainerAsset *)parent)->RemoveChild (this);
	
	assert (fParentList->GetNum () == 0);
	delete fParentList;
    }
}


Boolean
AeAsset::IsBankComponent (void)
{
    switch (fType)
    {
	case kBankFileType:
	case kBankType:
	case kInstType:
	case kSoundType:
	case kEnvType:
	case kKeymapType:
	case kWaveType:
	case kLoopType:
	case kADPCMLoopType:
	case kADPCMBookType:
	    return True;
    }

    return False;
}


Boolean
AeAsset::IsFxComponent (void)
{
    switch (fType)
    {
	case kFxType:
	case kFxSxnType:
	    return True;
    }

    return False;
}


Boolean
AeAsset::HasParent (void)
{
    assert (fParentList);
    return fParentList->GetNum () > 0;
}

void
AeAsset::GetClassName (char * name)
{
    strcpy (name, "asset");
}


Pixmap
AeAsset::GetIcon (void)
{
    assert (False);		// pure virtual called
    return NULL;
}

void
AeAsset::SetName (const char * name)
{
    strcpy (fName, name);
}

char *
AeAsset::GetNameProc (AeAsset * asset)
{
   assert (asset);
   return asset->GetName ();
}

void
AeAsset::SetNameProc (AeAsset * asset, char * name)
{
    assert (asset);
    asset->SetName (name);
}


Boolean
AeAsset::IsParent (AeAsset * parent)
{
    return False;
}

void
AeAsset::AppendParent (AeAsset * parent)
{
    assert (parent);
    assert (fParentList);

    fParentList->Append (parent);
}

void
AeAsset::RemoveParent (AeAsset * parent)
{
    int i;

    assert (parent);
    assert (fParentList);
    assert (fParentList->Find (parent, i));

    fParentList->Remove (parent);
}


void
AeAsset::SetClientData (AeClient * client, void * data)
{
    assert (client && client->GetRef () != kInvalidRef);

    if (fClientDataList)
    {
	// grow the list to fit the ref index (a hack)
	if (client->GetRef() >= fClientDataList->GetNum())
	{
	    while (fClientDataList->GetNum() <= client->GetRef())
		fClientDataList->Append (NULL);
	}

	(*fClientDataList)[client->GetRef ()] = data;
    }
}


//
// Return the client's asset data.
// If the list does not exist or the reference
// is not in the data list, then return NULL.
//
Boolean
AeAsset::GetClientData (AeClient * client, void ** pData)
{
    assert (client && client->GetRef () != kInvalidRef);

    *pData = NULL;	// help catch bugs

    if (!fClientDataList)
	return False;

    if (client->GetRef () >= fClientDataList->GetNum ())
	return False;

    *pData = (*fClientDataList)[client->GetRef ()];
    
    return True;
}


int
AeAsset::GetMaxDelay (void)
{
    assert (False);	// pure virtual called
    return -1;
}


/////////////////////////////////////////////////////////


AeContainerAsset::AeContainerAsset (const char * name, TAssetType type)
    : AeAsset (name, type)
{
    fChildList = new GList<AeAsset *>;
}


AeContainerAsset::~AeContainerAsset (void)
{
    if (fChildList)
    {
	AeAsset *	child;
	int		i;

	fChildList->Start (i, kReverse);

	while (fChildList->Prev (i, child))
	    child->RemoveParent (this);

	delete fChildList;
    }
}


TAssetType
AeContainerAsset::GetChildType (void)
{
    assert (False);	// pure virtual function called
    return kUnknownType;
}


int
AeContainerAsset::GetNumChildrenProc (AeAsset * asset)
{
    assert (asset != NULL);
    return ((AeContainerAsset *)asset)->GetNumChildren ();
}


int
AeContainerAsset::GetNumChildren (void)
{
    assert (CanContain ());
    return fChildList->GetNum ();
}


void
AeContainerAsset::AppendChild (AeAsset * child)
{
    assert (CanContain ());
    assert (child);

    fChildList->Append (child);
    child->AppendParent (this);
}


void
AeContainerAsset::InsertChild (AeAsset * child, int before)
{
    assert (CanContain ());
    assert (child);

    fChildList->Insert (child, before);
    child->AppendParent (this);
}


void
AeContainerAsset::RemoveChild (AeAsset * child)
{
    int i;

    assert (CanContain ());
    assert (child);
    assert (fChildList->Find (child, i));

    child->RemoveParent (this);
    fChildList->Remove (child);
}


AeAsset *
AeContainerAsset::GetIndexedChild (int index)
{
    assert (CanContain ());
    return (* fChildList)[index];
}



AeAsset *
AeAsset::NewTypedAsset (TAssetType type, AeAssetBase * assetBase)
{
    AeAsset *	asset;

    switch (type)
    {
	case kBankFileType:
	    asset = (AeAsset *)new AeBankFileAsset ("");
	break;

	case kBankType:
	    asset = (AeAsset *)new AeBankAsset ("");
	break;

	case kInstType:
	    asset = (AeAsset *)new AeInstAsset ("");
	break;

	case kSoundType:
	    asset = (AeAsset *)new AeSoundAsset ("");
	break;

	case kEnvType:
	    asset = (AeAsset *)new AeEnvAsset ("");
	break;

	case kKeymapType:
	    asset = (AeAsset *)new AeKeymapAsset ("");
	break;

	case kFxType:
	    asset = (AeAsset *)new AeFxAsset ("");
	break;

	case kFxSxnType:
	    asset = (AeAsset *)new AeFxSxnAsset ("");
	break;

	default:
	    asset = NULL;
    }

    if (asset)
	asset->FindUniqueName (assetBase);

    return asset;
}


void
AeAsset::FindUniqueName (AeAssetBase * assetBase)
{
    AeAssetBin *	bin;
    AeAsset *		asset;
    TString63		name;
    TString63		className;
    int			i = 0;
    
    GetClassName (className);
 
    if (assetBase->FindTypedBin (fType, bin))
    {
	do {
	    sprintf (name, "%s%d", className, i++);    
	} while (bin->FindNamedAsset (name, asset) && i>0);
    }
    else
	strcpy (name, className);

    SetName (name);
}