From f913346852f428ec4647ebbd552e364e4f08e35b Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 20 Feb 2005 04:20:46 +0000 Subject: [PATCH] MoveMap for IPreference* --- stepmania/src/ScreenOptionsMasterPrefs.cpp | 61 ++++++++++++++++------ 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/stepmania/src/ScreenOptionsMasterPrefs.cpp b/stepmania/src/ScreenOptionsMasterPrefs.cpp index 4fa9243baa..89087279c3 100644 --- a/stepmania/src/ScreenOptionsMasterPrefs.cpp +++ b/stepmania/src/ScreenOptionsMasterPrefs.cpp @@ -33,33 +33,64 @@ static void SetDefaultModifiers( const PlayerOptions &po, const SongOptions &so PREFSMAN->m_sDefaultModifiers = join(", ",as); } +template +int FindClosestEntry( T value, const T *mapping, unsigned cnt ) +{ + int iBestIndex = 0; + T best_dist = T(); + bool have_best = false; + + for( unsigned i = 0; i < cnt; ++i ) + { + const T val = mapping[i]; + T dist = value < val? (T)(val-value):(T)(value-val); + if( have_best && best_dist < dist ) + continue; + + have_best = true; + best_dist = dist; + + iBestIndex = i; + } + + if( have_best ) + return iBestIndex; + else + return 0; +} + template static void MoveMap( int &sel, T &opt, bool ToSel, const T *mapping, unsigned cnt ) { if( ToSel ) { /* opt -> sel. Find the closest entry in mapping. */ - T best_dist = T(); - bool have_best = false; - - for( unsigned i = 0; i < cnt; ++i ) - { - const T val = mapping[i]; - T dist = opt < val? (T)(val-opt):(T)(opt-val); - if( have_best && best_dist < dist ) - continue; - - have_best = true; - best_dist = dist; - - sel = i; - } + sel = FindClosestEntry( opt, mapping, cnt ); } else { /* sel -> opt */ opt = mapping[sel]; } } +/* + * MoveMap for a generic preference. We don't know its type, but MoveMap is only used on numeric + * values, so pretend it's a float. + */ +static void MoveMap( int &sel, IPreference *pOption, bool ToSel, const float *mapping, unsigned cnt ) +{ + if( ToSel ) + { + /* opt -> sel. Find the closest entry in mapping. */ + CString sStr = pOption->ToString(); + float fValue = strtof( sStr, NULL ); + + sel = FindClosestEntry( fValue, mapping, cnt ); + } else { + /* sel -> opt */ + CString sStr = ssprintf( "%f", mapping[sel] ); + pOption->FromString( sStr ); + } +} /* "sel" is the selection in the menu. */ static void MoveData( int &sel, int &opt, bool ToSel )