Files
itgmania212121/stepmania/src/Preference.cpp
T

151 lines
4.1 KiB
C++
Raw Normal View History

2004-12-04 07:15:22 +00:00
#include "global.h"
#include "Preference.h"
#include "XmlFile.h"
#include "RageLog.h"
#include "LuaFunctions.h"
2005-02-25 02:53:20 +00:00
#include "LuaManager.h"
2005-05-06 20:41:05 +00:00
#include "MessageManager.h"
#include "SubscriptionManager.h"
2006-01-06 20:19:30 +00:00
static SubscriptionManager<IPreference> m_Subscribers;
2004-12-04 07:15:22 +00:00
2006-01-22 01:00:06 +00:00
IPreference::IPreference( const RString& sName ):
m_sName( sName )
2004-12-04 07:15:22 +00:00
{
m_Subscribers.Subscribe( this );
2004-12-04 07:15:22 +00:00
}
IPreference::~IPreference()
2004-12-04 07:15:22 +00:00
{
m_Subscribers.Unsubscribe( this );
}
2006-01-22 01:00:06 +00:00
IPreference *IPreference::GetPreferenceByName( const RString &sName )
{
FOREACHS( IPreference*, *m_Subscribers.m_pSubscribers, p )
{
if( !(*p)->GetName().CompareNoCase( sName ) )
return *p;
}
return NULL;
}
void IPreference::LoadAllDefaults()
{
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->LoadDefault();
}
void IPreference::ReadAllPrefsFromNode( const XNode* pNode )
{
if( pNode == NULL )
return;
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->ReadFrom( pNode );
}
void IPreference::SavePrefsToNode( XNode* pNode )
{
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->WriteTo( pNode );
}
void IPreference::ReadAllDefaultsFromNode( const XNode* pNode )
{
if( pNode == NULL )
return;
FOREACHS_CONST( IPreference*, *m_Subscribers.m_pSubscribers, p )
(*p)->ReadDefaultFrom( pNode );
}
void IPreference::PushValue( lua_State *L ) const
{
if( LOG )
LOG->Trace( "The preference value \"%s\" is of a type not supported by Lua", m_sName.c_str() );
lua_pushnil( L );
}
2005-02-22 04:22:24 +00:00
void IPreference::SetFromStack( lua_State *L )
{
if( LOG )
LOG->Trace( "The preference value \"%s\" is of a type not supported by Lua", m_sName.c_str() );
lua_pop( L, 1 );
}
#define READFROM_AND_WRITETO( type ) \
2006-01-22 01:00:06 +00:00
template<> RString PrefToString( const type &v ) \
2004-12-04 07:15:22 +00:00
{ \
return ::ToString( v ); \
2004-12-04 07:15:22 +00:00
} \
2006-01-22 01:00:06 +00:00
template<> void PrefFromString( const RString &s, type &v ) \
2004-12-04 07:15:22 +00:00
{ \
::FromString( s, v ); \
2004-12-04 07:15:22 +00:00
} \
template<> void PrefSetFromStack( lua_State *L, type &v ) \
{ \
LuaHelpers::PopStack( v, L ); \
2005-02-22 04:22:24 +00:00
} \
template<> void PrefPushValue( lua_State *L, const type &v ) \
2005-02-22 04:22:24 +00:00
{ \
LuaHelpers::PushStack( v, L ); \
}
2004-12-04 07:15:22 +00:00
READFROM_AND_WRITETO( int )
READFROM_AND_WRITETO( float )
READFROM_AND_WRITETO( bool )
2006-01-22 01:00:06 +00:00
READFROM_AND_WRITETO( RString )
2004-12-04 07:15:22 +00:00
void IPreference::ReadFrom( const XNode* pNode )
{
2006-01-22 01:00:06 +00:00
RString sVal;
if( pNode->GetAttrValue(m_sName, sVal) )
FromString( sVal );
}
void IPreference::WriteTo( XNode* pNode ) const
{
pNode->AppendAttr( m_sName, ToString() );
}
/* Load our value from the node, and make it the new default. */
void IPreference::ReadDefaultFrom( const XNode* pNode )
{
2006-01-22 01:00:06 +00:00
RString sVal;
if( !pNode->GetAttrValue(m_sName, sVal) )
return;
SetDefaultFromString( sVal );
}
2006-01-22 01:00:06 +00:00
void BroadcastPreferenceChanged( const RString& sPreferenceName )
2005-05-06 20:41:05 +00:00
{
if( MESSAGEMAN )
MESSAGEMAN->Broadcast( sPreferenceName+"Changed" );
}
2004-12-04 07:15:22 +00:00
/*
* (c) 2001-2004 Chris Danford, Chris Gomez
* 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.
*/