00001 #include "MercuryTheme.h"
00002 #include "MercuryUtil.h"
00003 #include "MercuryLog.h"
00004 #include "MercuryMessages.h"
00005
00006 using namespace std;
00007
00008 MercuryThemeManager THEME;
00009
00010 REGISTER_STATEMENT_TO_MESSAGE( ReloadTheme, ReloadTheme, THEME.ReloadAllThemes(); )
00011
00012 bool MercuryTheme::Load( const MString &sThemeName )
00013 {
00014 m_sName = sThemeName;
00015 return Metrics.Open( "Themes/"+sThemeName+"/metrics.ini" );
00016
00017 }
00018
00019 void MercuryThemeManager::ReloadAllThemes( )
00020 {
00021 if ( m_vThemes == NULL )
00022 {
00023 m_vThemes = new MVector < MercuryTheme >;
00024 }
00025
00026 CheckPREFSMANSetup();
00027 MString ThemeList = PREFSMAN->GetValueS( "Options", "Themes", "default", true );
00028
00029 MVector < MString > vsThemes;
00030 SplitStrings( ThemeList, vsThemes, ",;"," ",2,1 );
00031 m_vThemes->resize( 0 );
00032 m_vThemes->resize( vsThemes.size() );
00033 for ( unsigned int i = 0; i < vsThemes.size(); i++ )
00034 {
00035 (*m_vThemes)[i].Load( vsThemes[i] );
00036 }
00037 }
00038
00039 bool MercuryThemeManager::GetMetric( const MString & sKey, const MString & sValue,
00040 MString & sData, int ttl )
00041 {
00042 MString tempData;
00043 if ( m_vThemes == NULL )
00044 ReloadAllThemes();
00045
00046 unsigned int i;
00047
00048
00049 for ( i = 0; i < m_vThemes->size(); i++ )
00050 {
00051 if ( (*m_vThemes)[i].GetMetricS( sKey, sValue, tempData ) )
00052 {
00053 sData = tempData;
00054 return true;
00055 }
00056
00057 if ( m_vThemes->at(i).GetMetricS( sKey, "Fallback", tempData ) )
00058 {
00059 MVector < MString > vFallbacks;
00060 SplitStrings( tempData, vFallbacks, ",;", " ",2,1 );
00061 for ( unsigned int p = 0; p < vFallbacks.size(); p++ )
00062 {
00063 if ( GetMetric( vFallbacks[p], sValue, tempData, ttl - 1 ) )
00064 {
00065 sData = tempData;
00066 return true;
00067 }
00068 }
00069 }
00070 }
00071 LOG.Warn( ssprintf( "Failed to find theme metric: [%s]:%s!",sKey.c_str(), sValue.c_str() ) );
00072 return false;
00073 }
00074
00075 MString MercuryThemeManager::GetMetricS( const MString & sKey, const MString & sValue, const MString & Default )
00076 {
00077 MString sData;
00078 bool Got = GetMetric( sKey, sValue, sData );
00079 if (!Got)
00080 return Default;
00081 else
00082 return sData;
00083 }
00084
00085 int MercuryThemeManager::GetMetricI( const MString & sKey, const MString & sValue, int Default )
00086 {
00087 return atoi(GetMetricS(sKey, sValue, ssprintf( "%d", Default ) ) );
00088 }
00089
00090 float MercuryThemeManager::GetMetricF( const MString & sKey, const MString & sValue, float Default )
00091 {
00092 return float(atof(GetMetricS(sKey, sValue, ssprintf( "%f", Default ) ) ) );
00093 }
00094
00095 bool MercuryThemeManager::GetMetricB( const MString & sKey, const MString & sValue, bool Default )
00096 {
00097 return atoi(GetMetricS(sKey, sValue, ssprintf( "%d", Default ) ) ) != 0;
00098 }
00099
00100 bool MercuryThemeManager::GetPathToFile( const MString & File, MString & Path)
00101 {
00102 if ( m_vThemes == NULL )
00103 ReloadAllThemes();
00104
00105 unsigned int i;
00106
00107 for ( i = 0; i < m_vThemes->size(); i++ )
00108 {
00109 MString addy = "Themes/" + m_vThemes->at(i).GetName() + "/" + File;
00110 if ( FileExists( addy ) )
00111 {
00112 Path = addy;
00113 return true;
00114 }
00115 }
00116 return false;
00117 }
00118
00119 MString MercuryThemeManager::GetPathToFile( const MString & Name )
00120 {
00121 MString Ret;
00122 MString In = "Files/" + Name;
00123 if ( !GetPathToFile( In, Ret ) )
00124 Ret = "";
00125 return Ret;
00126 }
00127
00128 MString MercuryThemeManager::GetPathToGraphic( const MString & Name )
00129 {
00130 MString Ret;
00131 MString In = "Graphics/" + Name;
00132 if ( !GetPathToFile( In, Ret ) )
00133 {
00134 LOG.Log( "\"" + In + "\" not found" );
00135 Ret = "Themes/_missing.png";
00136 }
00137 return Ret;
00138 }
00139
00140 MString MercuryThemeManager::GetPathToModel( const MString &Name )
00141 {
00142 MString Ret;
00143 MString In = "Models/" + Name;
00144 if ( !GetPathToFile( In, Ret ) )
00145 {
00146 LOG.Log( "\"" + In + "\" not found" );
00147 FAIL( "\"" + In + "\" not found" );
00148 }
00149 return Ret;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178