Files
itgmania212121/stepmania/src/BGAnimation.cpp
T

232 lines
6.6 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-09-29 05:06:18 +00:00
#include "BGAnimation.h"
#include "IniFile.h"
#include "BGAnimationLayer.h"
2003-03-05 02:52:40 +00:00
#include "RageUtil.h"
2003-10-31 01:55:17 +00:00
#include "ActorUtil.h"
#include "Foreach.h"
2005-01-26 11:21:43 +00:00
#include "Command.h"
#include "LuaManager.h"
2002-09-29 05:06:18 +00:00
2005-01-16 19:19:24 +00:00
BGAnimation::BGAnimation()
2002-09-29 05:06:18 +00:00
{
}
BGAnimation::~BGAnimation()
{
Unload();
}
void BGAnimation::Unload()
{
2004-02-01 03:14:37 +00:00
DeleteAllChildren();
2002-09-29 05:06:18 +00:00
}
static bool CompareLayerNames( const CString& s1, const CString& s2 )
{
int i1, i2;
int ret;
ret = sscanf( s1, "Layer%d", &i1 );
ASSERT( ret == 1 );
ret = sscanf( s2, "Layer%d", &i2 );
ASSERT( ret == 1 );
return i1 < i2;
}
2005-01-17 00:58:07 +00:00
void BGAnimation::AddLayersFromAniDir( const CString &_sAniDir, const IniFile& ini, bool bGeneric )
2002-09-29 05:06:18 +00:00
{
2005-01-17 00:58:07 +00:00
const CString& sAniDir = _sAniDir;
2003-10-14 17:05:10 +00:00
2004-02-14 23:22:23 +00:00
{
CString expr;
2004-05-29 20:18:51 +00:00
if( ini.GetValue( "BGAnimation", "Condition", expr ) || ini.GetValue( "BGAnimation", "Cond", expr ) )
2004-02-14 23:22:23 +00:00
{
2005-01-19 22:27:24 +00:00
if( !LUA->RunExpressionB( expr ) )
2004-02-14 23:22:23 +00:00
return;
}
}
2002-09-29 05:06:18 +00:00
{
vector<CString> vsLayerNames;
2005-01-07 14:28:00 +00:00
FOREACH_CONST_Child( &ini, pLayer )
{
2005-01-07 14:28:00 +00:00
if( strncmp(pLayer->m_sName, "Layer", 5) == 0 )
vsLayerNames.push_back( pLayer->m_sName );
}
sort( vsLayerNames.begin(), vsLayerNames.end(), CompareLayerNames );
2003-03-05 02:52:40 +00:00
FOREACH_CONST( CString, vsLayerNames, s )
2003-03-05 02:52:40 +00:00
{
2005-01-07 14:28:00 +00:00
const CString &sLayer = *s;
const XNode* pKey = ini.GetChild( sLayer );
ASSERT( pKey );
CString sImportDir;
2005-01-07 14:28:00 +00:00
if( pKey->GetAttrValue("Import", sImportDir) )
2004-05-03 03:58:41 +00:00
{
CString expr;
2005-01-07 14:28:00 +00:00
if( pKey->GetAttrValue("Condition",expr) )
{
2005-01-19 22:27:24 +00:00
if( !LUA->RunExpressionB( expr ) )
continue;
}
// import a whole BGAnimation
sImportDir = sAniDir + sImportDir;
CollapsePath( sImportDir );
2005-01-17 00:58:07 +00:00
if( sImportDir.Right(1) != "/" )
sImportDir += "/";
ASSERT_M( IsADirectory(sImportDir), sImportDir + " isn't a directory" );
CString sPathToIni = sImportDir + "BGAnimation.ini";
IniFile ini2;
ini2.ReadFile( sPathToIni );
AddLayersFromAniDir( sImportDir, ini2, bGeneric );
}
else
{
// import as a single layer
2005-01-16 19:19:24 +00:00
BGAnimationLayer* pLayer = new BGAnimationLayer( bGeneric );
pLayer->LoadFromNode( sAniDir, pKey );
2005-01-17 00:58:07 +00:00
this->AddChild( pLayer );
2004-05-03 03:58:41 +00:00
}
2003-03-05 02:52:40 +00:00
}
2003-10-14 17:05:10 +00:00
}
}
2005-01-16 19:19:24 +00:00
void BGAnimation::LoadFromAniDir( const CString &_sAniDir, bool bGeneric )
2003-10-14 17:05:10 +00:00
{
Unload();
2005-01-07 22:01:57 +00:00
if( _sAniDir.empty() )
2003-10-14 17:05:10 +00:00
return;
2005-01-07 22:01:57 +00:00
CString sAniDir = _sAniDir;
2003-12-10 09:44:16 +00:00
if( sAniDir.Right(1) != "/" )
sAniDir += "/";
2003-10-14 17:05:10 +00:00
2004-06-16 00:38:31 +00:00
ASSERT_M( IsADirectory(sAniDir), sAniDir + " isn't a directory" );
2003-10-14 17:05:10 +00:00
CString sPathToIni = sAniDir + "BGAnimation.ini";
if( DoesFileExist(sPathToIni) )
{
// This is a new style BGAnimation (using .ini)
2004-05-23 02:27:51 +00:00
IniFile ini;
ini.ReadFile( sPathToIni );
2005-01-07 22:01:57 +00:00
2005-01-17 00:58:07 +00:00
AddLayersFromAniDir( sAniDir, ini, bGeneric ); // TODO: Check for circular load
XNode* pBGAnimation = ini.GetChild( "BGAnimation" );
2005-01-07 22:01:57 +00:00
XNode dummy;
dummy.m_sName = "BGAnimation";
if( pBGAnimation == NULL )
pBGAnimation = &dummy;
// Ugly: Scroller attributes in BGAnimation.ini files are in an element called
// "Scroller", and not in the "BGAnimation" element. Move the attributes from
// Scroller to BGAnimation.
XNode* pScrollerNode = ini.GetChild( "Scroller" );
if( pScrollerNode )
{
FOREACH_Attr( pScrollerNode, pAttr )
pBGAnimation->m_attrs.insert( pair<CString,XAttr*>(pAttr->m_sName, pAttr) );
// Clear the copies in the Scroller node so that we don't double-delete.
pScrollerNode->m_attrs.clear();
}
LoadFromNode( sAniDir, pBGAnimation );
2003-03-05 02:52:40 +00:00
}
else
{
// This is an old style BGAnimation (not using .ini)
// loading a directory of layers
CStringArray asImagePaths;
ASSERT( sAniDir != "" );
GetDirListing( sAniDir+"*.png", asImagePaths, false, true );
GetDirListing( sAniDir+"*.jpg", asImagePaths, false, true );
GetDirListing( sAniDir+"*.gif", asImagePaths, false, true );
GetDirListing( sAniDir+"*.avi", asImagePaths, false, true );
GetDirListing( sAniDir+"*.mpg", asImagePaths, false, true );
GetDirListing( sAniDir+"*.mpeg", asImagePaths, false, true );
GetDirListing( sAniDir+"*.sprite", asImagePaths, false, true );
SortCStringArray( asImagePaths );
for( unsigned i=0; i<asImagePaths.size(); i++ )
{
const CString sPath = asImagePaths[i];
2003-10-29 20:32:29 +00:00
if( Basename(sPath).Left(1) == "_" )
2003-03-05 02:52:40 +00:00
continue; // don't directly load files starting with an underscore
2005-01-16 19:19:24 +00:00
BGAnimationLayer* pLayer = new BGAnimationLayer( bGeneric );
pLayer->LoadFromAniLayerFile( asImagePaths[i] );
2004-02-01 03:14:37 +00:00
AddChild( pLayer );
2003-03-05 02:52:40 +00:00
}
2002-09-29 05:06:18 +00:00
}
}
void BGAnimation::LoadFromNode( const CString& sDir, const XNode* pNode )
2005-01-07 22:01:57 +00:00
{
DEBUG_ASSERT( pNode->m_sName == "BGAnimation" );
2005-01-07 22:01:57 +00:00
CString sInitCommand;
if( pNode->GetAttrValue( "InitCommand", sInitCommand ) )
2005-01-07 22:01:57 +00:00
{
/* There's an InitCommand. Run it now. This can be used to eg. change Z to
* modify draw order between BGAs in a Foreground. Most things should be done
* in metrics.ini commands, not here. */
2005-01-26 11:21:43 +00:00
this->RunCommands( ActorCommands(ParseCommands(sInitCommand)) );
2005-01-07 22:01:57 +00:00
}
ActorScroller::LoadFromNode( sDir, pNode );
2005-01-07 22:01:57 +00:00
2005-01-26 11:21:43 +00:00
this->RunCommandsOnChildren( ActorCommands(ParseCommands("PlayCommand,Init")) );
2005-01-07 22:01:57 +00:00
2005-01-17 00:58:07 +00:00
/* Backwards-compatibility: if a "LengthSeconds" value is present, create a dummy
* actor that sleeps for the given length of time. This will extend GetTweenTimeLeft. */
float fLengthSeconds = 0;
if( pNode->GetAttrValue( "LengthSeconds", fLengthSeconds ) )
2005-01-07 23:56:18 +00:00
{
Actor *pActor = new Actor;
pActor->SetHidden( true );
pActor->BeginTweening( fLengthSeconds );
AddChild( pActor );
2005-01-07 23:56:18 +00:00
}
2005-01-07 22:01:57 +00:00
}
2004-06-01 00:53:06 +00:00
/*
* (c) 2001-2004 Ben Nordstrom, Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/