Files
itgmania212121/stepmania/src/ActorUtil.cpp
T

357 lines
11 KiB
C++
Raw Normal View History

2004-06-08 00:47:53 +00:00
#include "global.h"
#include "ActorUtil.h"
#include "Sprite.h"
#include "BitmapText.h"
#include "Model.h"
2003-10-31 00:50:06 +00:00
#include "BGAnimation.h"
#include "IniFile.h"
#include "ThemeManager.h"
#include "RageDisplay.h"
#include "RageLog.h"
#include "arch/ArchHooks/ArchHooks.h"
2003-12-07 04:16:10 +00:00
#include "RageFileManager.h"
2004-01-25 01:59:21 +00:00
#include "SongCreditDisplay.h"
2004-01-25 22:22:40 +00:00
#include "song.h"
#include "GameState.h"
#include "RageTextureManager.h"
2004-02-01 03:14:37 +00:00
#include "SongManager.h"
2004-02-01 23:06:43 +00:00
#include "Course.h"
#include "arch/Dialog/Dialog.h"
2004-11-06 03:00:10 +00:00
Actor* LoadFromActorFile( const CString &sIniPath, const CString &sLayer )
{
// TODO: Check for recursive loading
IniFile ini;
2004-05-23 02:27:51 +00:00
if( !ini.ReadFile( sIniPath ) )
RageException::Throw( "%s", ini.GetError().c_str() );
2004-01-25 22:22:40 +00:00
if( !ini.GetKey(sLayer) )
RageException::Throw( "The file '%s' doesn't have layer '%s'.", sIniPath.c_str(), sLayer.c_str() );
2004-01-25 22:22:40 +00:00
Actor* pActor = NULL; // fill this in before we return;
CString sType;
ini.GetValue( sLayer, "Type", sType );
CString sFile;
ini.GetValue( sLayer, "File", sFile );
FixSlashesInPlace( sFile );
2004-01-25 22:22:40 +00:00
CString sDir = Dirname( sIniPath );
if( sType == "SongCreditDisplay" )
{
2004-01-25 22:22:40 +00:00
pActor = new SongCreditDisplay;
}
else
{
2003-12-22 10:47:09 +00:00
2004-01-25 22:22:40 +00:00
/* XXX: How to handle translations? Maybe we should have one metrics section,
* "Text", eg:
*
* [Text]
* SoundVolume=Sound Volume
* TextItem=Hello
*
* and allow "$TextItem$" in .actors to reference that.
*/
2004-02-14 10:38:40 +00:00
/* Be careful: if sFile is "", and we don't check it, then we can end up recursively
* loading the BGAnimationLayer that we're in. */
if( sFile == "" )
RageException::Throw( "The actor file '%s' layer %s is missing File",
sIniPath.c_str(), sLayer.c_str() );
2004-01-25 22:22:40 +00:00
CString text;
if( ini.GetValue ( sLayer, "Text", text ) )
{
/* It's a BitmapText. Note that we could do the actual text setting with metrics,
* by adding "text" and "alttext" commands, but right now metrics can't contain
* commas or semicolons. It's useful to be able to refer to fonts in the real
* theme font dirs, too. */
CString alttext;
ini.GetValue ( sLayer, "AltText", alttext );
text.Replace( "::", "\n" );
alttext.Replace( "::", "\n" );
BitmapText* pBitmapText = new BitmapText;
pBitmapText->LoadFromFont( THEME->GetPathToF( sFile ) );
pBitmapText->SetText( text, alttext );
pActor = pBitmapText;
}
else
{
if( sFile.CompareNoCase("songbackground")==0 )
{
Song *pSong = GAMESTATE->m_pCurSong;
if( pSong && pSong->HasBackground() )
sFile = pSong->GetBackgroundPath();
else
sFile = THEME->GetPathToG("Common fallback background");
2004-03-26 08:08:02 +00:00
/* Always load song backgrounds with SongBGTexture. It sets texture properties;
2004-03-26 07:41:04 +00:00
* if we load a background without setting those properties, we'll end up
2004-03-26 08:08:02 +00:00
* with duplicates. */
2004-03-26 07:41:04 +00:00
Sprite* pSprite = new Sprite;
pSprite->LoadBG( sFile );
pActor = pSprite;
2004-01-25 22:22:40 +00:00
}
else if( sFile.CompareNoCase("songbanner")==0 )
{
2004-02-01 03:14:37 +00:00
Song *pSong = GAMESTATE->m_pCurSong;
if( pSong == NULL )
{
// probe for a random banner
for( int i=0; i<300; i++ )
{
pSong = SONGMAN->GetRandomSong();
if( pSong == NULL )
break;
if( !pSong->ShowInDemonstrationAndRanking() )
continue;
break;
}
}
2004-02-01 03:14:37 +00:00
2004-01-25 22:22:40 +00:00
if( pSong && pSong->HasBanner() )
sFile = pSong->GetBannerPath();
else
sFile = THEME->GetPathToG("Common fallback banner");
TEXTUREMAN->DisableOddDimensionWarning();
2004-03-26 08:08:02 +00:00
/* Always load banners with BannerTex. It sets texture properties;
* if we load a background without setting those properties, we'll end up
* with duplicates. */
Sprite* pSprite = new Sprite;
pSprite->Load( Sprite::SongBannerTexture(sFile) );
pActor = pSprite;
2004-01-25 22:22:40 +00:00
TEXTUREMAN->EnableOddDimensionWarning();
}
2004-02-01 23:06:43 +00:00
else if( sFile.CompareNoCase("coursebanner")==0 )
{
Course *pCourse = GAMESTATE->m_pCurCourse;
if( pCourse == NULL )
{
// probe for a random banner
for( int i=0; i<300; i++ )
{
pCourse = SONGMAN->GetRandomCourse();
if( pCourse == NULL )
break;
if( !pCourse->ShowInDemonstrationAndRanking() )
continue;
if( pCourse->m_bIsAutogen )
continue;
break;
}
}
2004-02-01 23:06:43 +00:00
if( pCourse && pCourse->HasBanner() )
sFile = pCourse->m_sBannerPath;
else
sFile = THEME->GetPathToG("Common fallback banner");
TEXTUREMAN->DisableOddDimensionWarning();
2004-03-26 08:08:02 +00:00
Sprite* pSprite = new Sprite;
pSprite->Load( Sprite::SongBannerTexture(sFile) );
pActor = pSprite;
2004-02-01 23:06:43 +00:00
TEXTUREMAN->EnableOddDimensionWarning();
}
2004-04-06 01:04:04 +00:00
else
2004-01-26 02:31:04 +00:00
{
retry:
2004-04-06 01:04:04 +00:00
/* XXX: We need to do a theme search, since the file we're loading might
* be overridden by the theme. */
CString sNewPath = sDir+sFile;
// If we know this is an exact match, don't bother with the GetDirListing;
// it's causing problems with partial matching BGAnimation directory names.
if( !IsAFile(sNewPath) && !IsADirectory(sNewPath) )
{
CStringArray asPaths;
GetDirListing( sNewPath + "*", asPaths, false, true ); // return path too
if( asPaths.empty() )
{
CString sError = ssprintf( "The actor file '%s' references a file '%s' which doesn't exist.", sIniPath.c_str(), sFile.c_str() );
2004-08-12 23:06:06 +00:00
switch( Dialog::AbortRetryIgnore( sError, "BROKEN_ACTOR_REFERENCE" ) )
{
2004-08-12 23:06:06 +00:00
case Dialog::abort:
RageException::Throw( sError );
break;
case Dialog::retry:
FlushDirCache();
goto retry;
2004-08-12 23:06:06 +00:00
case Dialog::ignore:
asPaths.push_back( sNewPath );
if( GetExtension(asPaths[0]) == "" )
asPaths[0] = SetExtension( asPaths[0], "png" );
break;
default:
ASSERT(0);
}
}
2004-04-06 01:04:04 +00:00
else if( asPaths.size() > 1 )
{
CString sError = ssprintf( "The actor file '%s' references a file '%s' which has multiple matches.", sIniPath.c_str(), sFile.c_str() );
2004-08-12 23:06:06 +00:00
switch( Dialog::AbortRetryIgnore( sError, "DUPLICATE_ACTOR_REFERENCE" ) )
{
2004-08-12 23:06:06 +00:00
case Dialog::abort:
RageException::Throw( sError );
break;
case Dialog::retry:
FlushDirCache();
goto retry;
2004-08-12 23:06:06 +00:00
case Dialog::ignore:
asPaths.erase( asPaths.begin()+1, asPaths.end() );
break;
default:
ASSERT(0);
}
}
2004-04-06 01:04:04 +00:00
sNewPath = asPaths[0];
}
2004-01-25 22:22:40 +00:00
2004-04-06 01:04:04 +00:00
sNewPath = DerefRedir( sNewPath );
2004-01-25 22:22:40 +00:00
pActor = MakeActor( sNewPath );
}
}
}
2003-09-23 05:39:47 +00:00
float f;
2004-01-25 22:22:40 +00:00
if( ini.GetValue ( sLayer, "BaseRotationXDegrees", f ) ) pActor->SetBaseRotationX( f );
if( ini.GetValue ( sLayer, "BaseRotationYDegrees", f ) ) pActor->SetBaseRotationY( f );
if( ini.GetValue ( sLayer, "BaseRotationZDegrees", f ) ) pActor->SetBaseRotationZ( f );
if( ini.GetValue ( sLayer, "BaseZoomX", f ) ) pActor->SetBaseZoomX( f );
if( ini.GetValue ( sLayer, "BaseZoomY", f ) ) pActor->SetBaseZoomY( f );
if( ini.GetValue ( sLayer, "BaseZoomZ", f ) ) pActor->SetBaseZoomZ( f );
ASSERT( pActor ); // we should have filled this in above
return pActor;
}
2004-11-06 03:00:10 +00:00
Actor* MakeActor( const RageTextureID &ID )
{
CString sExt = GetExtension( ID.filename );
sExt.MakeLower();
2004-01-25 22:22:40 +00:00
if( sExt=="actor" )
{
2004-01-25 22:22:40 +00:00
return LoadFromActorFile( ID.filename );
}
else if( sExt=="png" ||
sExt=="jpg" ||
sExt=="gif" ||
sExt=="bmp" ||
sExt=="avi" ||
sExt=="mpeg" ||
sExt=="mpg" ||
sExt=="sprite" )
{
Sprite* pSprite = new Sprite;
pSprite->Load( ID );
return pSprite;
}
2004-02-08 11:17:03 +00:00
else if( sExt=="txt" ||
sExt=="model" )
{
Model* pModel = new Model;
2004-02-08 11:17:03 +00:00
pModel->Load( ID.filename );
return pModel;
}
2003-10-31 00:50:06 +00:00
/* Do this last, to avoid the IsADirectory in most cases. */
else if( IsADirectory(ID.filename) )
{
BGAnimation *pBGA = new BGAnimation( true );
2003-10-31 00:50:06 +00:00
pBGA->LoadFromAniDir( ID.filename );
return pBGA;
}
else
2003-09-30 19:29:20 +00:00
RageException::Throw("File \"%s\" has unknown type, \"%s\"",
ID.filename.c_str(), sExt.c_str() );
}
2004-11-06 03:00:10 +00:00
void UtilSetXY( Actor& actor, const CString &sClassName )
2003-10-31 00:50:06 +00:00
{
2003-11-14 23:01:35 +00:00
ASSERT( !actor.GetID().empty() );
actor.SetXY( THEME->GetMetricF(sClassName,actor.GetID()+"X"), THEME->GetMetricF(sClassName,actor.GetID()+"Y") );
2003-10-31 00:50:06 +00:00
}
2004-11-06 03:00:10 +00:00
void UtilCommand( Actor& actor, const CString &sClassName, const CString &sCommandName )
2003-10-31 00:50:06 +00:00
{
2003-12-28 08:20:48 +00:00
// If Actor is hidden, it won't get updated or drawn, so don't bother tweening.
2004-01-11 05:32:40 +00:00
/* ... but we might be unhiding it, or setting state for when we unhide it later */
// if( actor.GetHidden() )
// return 0;
2003-12-28 08:20:48 +00:00
ActorCommand ac;
ac.Set("playcommand,"+sCommandName);
actor.HandleCommand( ac );
2003-10-31 00:50:06 +00:00
// HACK: It's very often that we command things to TweenOffScreen
// that we aren't drawing. We know that an Actor is not being
2003-11-05 06:43:26 +00:00
// used if its name is blank. So, do nothing on Actors with a blank name.
2003-10-31 02:01:16 +00:00
// (Do "playcommand" anyway; BGAs often have no name.)
if( sCommandName=="Off" )
{
2003-11-14 23:01:35 +00:00
if( actor.GetID().empty() )
2004-02-01 03:14:37 +00:00
return;
2004-11-06 03:09:29 +00:00
}
else
{
2004-06-16 00:38:31 +00:00
ASSERT_M( !actor.GetID().empty(), ssprintf("!actor.GetID().empty() ('%s', '%s')",
sClassName.c_str(), sCommandName.c_str()) );
2004-11-06 03:09:29 +00:00
}
2003-10-31 01:02:50 +00:00
actor.Command( THEME->GetMetricA(sClassName,actor.GetID()+sCommandName+"Command") );
2003-10-31 00:50:06 +00:00
}
2004-11-06 03:00:10 +00:00
void AutoActor::Load( const CString &sPath )
{
Unload();
m_pActor = MakeActor( sPath );
}
2004-11-06 03:00:10 +00:00
void AutoActor::LoadAndSetName( const CString &sScreenName, const CString &sActorName )
{
Load( THEME->GetPathG(sScreenName,sActorName) );
m_pActor->SetName( sActorName );
}
2004-06-08 00:47:53 +00:00
2004-11-23 22:06:27 +00:00
void UtilSetQuadInit( Actor& actor, const CString &sClassName )
{
UtilSetXYAndOnCommand( actor, sClassName );
actor.SetDiffuse( THEME->GetMetricC( sClassName, actor.m_sName + "Color" ) );
actor.SetWidth( THEME->GetMetricF( sClassName, actor.m_sName + "Width" ) );
actor.SetHeight( THEME->GetMetricF( sClassName, actor.m_sName + "Height" ) );
}
2004-06-08 00:47:53 +00:00
/*
* (c) 2003-2004 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.
*/