MercuryTheme.cpp

Go to the documentation of this file.
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     //Check all themes for fallback(s)
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  * Copyright (c) 2005-2006, Charles Lohr
00154  * All rights reserved.
00155  *
00156  * Redistribution and use in source and binary forms, with or
00157  * without modification, are permitted provided that the following
00158  * conditions are met:
00159  *  -   Redistributions of source code must retain the above
00160  *      copyright notice, this list of conditions and the following disclaimer.
00161  *  -   Redistributions in binary form must reproduce the above copyright
00162  *      notice, this list of conditions and the following disclaimer in
00163  *      the documentation and/or other materials provided with the distribution.
00164  *  -   Neither the name of the Mercury Engine nor the names of its
00165  *      contributors may be used to endorse or promote products derived from
00166  *      this software without specific prior written permission.
00167  *
00168  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00169  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00170  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00171  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00172  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00173  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00174  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00175  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00176  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00177  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00178  */

Hosted by SourceForge.net Logo