From e65362e3f5854b4e68ea10ef7abe87528ab0f0f9 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Mon, 7 Mar 2005 05:23:18 +0000 Subject: [PATCH] give new edits unique names --- stepmania/src/ScreenEditMenu.cpp | 39 ++++++++++---------------- stepmania/src/Song.cpp | 47 ++++++++++++++++++++++++++++++-- stepmania/src/Steps.cpp | 15 +++++++--- stepmania/src/Steps.h | 4 +++ stepmania/src/StepsUtil.cpp | 4 +-- stepmania/src/song.h | 4 ++- 6 files changed, 79 insertions(+), 34 deletions(-) diff --git a/stepmania/src/ScreenEditMenu.cpp b/stepmania/src/ScreenEditMenu.cpp index 493d884faf..580e42c736 100644 --- a/stepmania/src/ScreenEditMenu.cpp +++ b/stepmania/src/ScreenEditMenu.cpp @@ -149,27 +149,6 @@ void ScreenEditMenu::MenuStart( PlayerNumber pn ) m_Selector.RefreshAll(); return; case EditMenu::ACTION_COPY: - ASSERT( !pSteps ); - ASSERT( pSourceSteps ); - { - // Yuck. Doing the memory allocation doesn't seem right since - // Song allocates all of the other Steps. - Steps* pNewSteps = new Steps; - pNewSteps->CopyFrom( pSourceSteps, st ); - pNewSteps->SetDifficulty( dc ); - pNewSteps->SetDescription( GetCopyDescription(pSourceSteps) ); - pSong->AddSteps( pNewSteps ); - - SCREENMAN->SystemMessage( "Steps created from copy." ); - SOUND->PlayOnce( THEME->GetPathS(m_sName,"create") ); - pSong->Save(); - - GAMESTATE->m_pCurSong.Set( pSong ); - GAMESTATE->m_pCurSteps[0].Set( pNewSteps ); - - m_Selector.RefreshAll(); - } - return; case EditMenu::ACTION_AUTOGEN: ASSERT( !pSteps ); ASSERT( pSourceSteps ); @@ -177,10 +156,22 @@ void ScreenEditMenu::MenuStart( PlayerNumber pn ) // Yuck. Doing the memory allocation doesn't seem right since // Song allocates all of the other Steps. Steps* pNewSteps = new Steps; - pNewSteps->AutogenFrom( pSourceSteps, st ); - pNewSteps->DeAutogen(); + switch( action ) + { + case EditMenu::ACTION_COPY: + pNewSteps->CopyFrom( pSourceSteps, st ); + break; + case EditMenu::ACTION_AUTOGEN: + pNewSteps->AutogenFrom( pSourceSteps, st ); + pNewSteps->DeAutogen(); + break; + default: + ASSERT(0); + } pNewSteps->SetDifficulty( dc ); // override difficulty with the user's choice - pNewSteps->SetDescription( GetCopyDescription(pSourceSteps) ); + CString sPreferredEditName = GetCopyDescription(pSourceSteps); + pSong->MakeUniqueEditDescription( st, sPreferredEditName ); + pNewSteps->SetDescription( sPreferredEditName ); pSong->AddSteps( pNewSteps ); SCREENMAN->SystemMessage( "Steps created from AutoGen." ); diff --git a/stepmania/src/Song.cpp b/stepmania/src/Song.cpp index be520a089c..ae32378f3f 100644 --- a/stepmania/src/Song.cpp +++ b/stepmania/src/Song.cpp @@ -855,7 +855,7 @@ void Song::GetSteps( } } -Steps* Song::GetSteps( +Steps* Song::GetOneSteps( StepsType st, Difficulty dc, int iMeterLow, @@ -952,12 +952,12 @@ bool Song::SongCompleteForStyle( const Style *st ) const bool Song::HasStepsType( StepsType st ) const { - return GetSteps( st ) != NULL; + return GetOneSteps( st ) != NULL; } bool Song::HasStepsTypeAndDifficulty( StepsType st, Difficulty dc ) const { - return GetSteps( st, dc ) != NULL; + return GetOneSteps( st, dc ) != NULL; } void Song::Save() @@ -1404,6 +1404,47 @@ bool Song::HasSignificantBpmChangesOrStops() const return m_Timing.HasBpmChangesOrStops(); } +bool IsEditDescriptionUnique( vector vSteps, StepsType st, CString sPreferredDescription ) +{ + FOREACH( Steps*, vSteps, s ) + { + if( (*s)->GetDifficulty() != DIFFICULTY_EDIT ) + continue; + + if( (*s)->m_StepsType != st ) + continue; + + if( (*s)->GetDescription() == sPreferredDescription ) + return false; + } + return true; +} + +void Song::MakeUniqueEditDescription( StepsType st, CString &sPreferredDescriptionInOut ) +{ + if( IsEditDescriptionUnique( m_vpSteps, st, sPreferredDescriptionInOut ) ) + return; + + int i=0; + CString sTemp; + + for( int i=0; i<1000; i++ ) + { + // make name "My Edit" -> "My Edit" + CString sNum = ssprintf("%d", i+1); + sTemp = sPreferredDescriptionInOut.Left( MAX_DESCRIPTION_LENGTH - sNum.size() ) + sNum; + + if( IsEditDescriptionUnique(m_vpSteps, st, sTemp) ) + { + sPreferredDescriptionInOut = sTemp; + return; + } + } + + // Edit limit guards should keep us from ever having more than 1000 edits per song. + return; +} + // lua start #include "LuaBinding.h" diff --git a/stepmania/src/Steps.cpp b/stepmania/src/Steps.cpp index df879ca9b5..483d000ea5 100644 --- a/stepmania/src/Steps.cpp +++ b/stepmania/src/Steps.cpp @@ -31,8 +31,6 @@ #include "PrefsManager.h" #include "NotesLoaderSM.h" -const int MAX_DESCRIPTION_LENGTH = 12; - Steps::Steps() { m_StepsType = STEPS_TYPE_INVALID; @@ -303,8 +301,17 @@ void Steps::SetDescription(CString desc) { DeAutogen(); m_sDescription = desc; - if( int(m_sDescription.size()) > MAX_DESCRIPTION_LENGTH ) - m_sDescription = m_sDescription.Left( MAX_DESCRIPTION_LENGTH ); + MakeValidDescription( m_sDescription ); +} + +bool Steps::MakeValidDescription( CString &sPreferredDescription ) +{ + if( int(sPreferredDescription.size()) > MAX_DESCRIPTION_LENGTH ) + { + sPreferredDescription = sPreferredDescription.Left( MAX_DESCRIPTION_LENGTH ); + return true; + } + return false; } void Steps::SetDifficulty(Difficulty d) diff --git a/stepmania/src/Steps.h b/stepmania/src/Steps.h index 0e5454d1f6..484b746e51 100644 --- a/stepmania/src/Steps.h +++ b/stepmania/src/Steps.h @@ -12,6 +12,8 @@ class NoteData; class Profile; struct lua_State; +const int MAX_DESCRIPTION_LENGTH = 12; + class Steps { public: @@ -40,6 +42,8 @@ public: void SetFile( CString fn ); void SetDescription(CString desc); + static bool MakeValidDescription( CString &sPreferredDescription ); // return true if was modified + void SetDifficulty(Difficulty d); void SetLoadedFromProfile( ProfileSlot slot ) { m_LoadedFromProfile = slot; } void SetMeter(int meter); diff --git a/stepmania/src/StepsUtil.cpp b/stepmania/src/StepsUtil.cpp index c8d9c21141..60b7ca4c82 100644 --- a/stepmania/src/StepsUtil.cpp +++ b/stepmania/src/StepsUtil.cpp @@ -176,11 +176,11 @@ Steps *StepsID::ToSteps( const Song *p, bool bAllowNull, bool bUseCache ) const Steps *ret = NULL; if( dc == DIFFICULTY_EDIT ) { - ret = p->GetSteps( st, dc, -1, -1, sDescription, uHash, true ); + ret = p->GetOneSteps( st, dc, -1, -1, sDescription, uHash, true ); } else { - ret = p->GetSteps( st, dc, -1, -1, "", 0, true ); + ret = p->GetOneSteps( st, dc, -1, -1, "", 0, true ); } if( !bAllowNull && ret == NULL ) diff --git a/stepmania/src/song.h b/stepmania/src/song.h index cd06e63ba2..457be50613 100644 --- a/stepmania/src/song.h +++ b/stepmania/src/song.h @@ -189,7 +189,7 @@ public: unsigned uHash = 0, int iMaxToGet = -1 ) const; - Steps* GetSteps( + Steps* GetOneSteps( StepsType st = STEPS_TYPE_INVALID, Difficulty dc = DIFFICULTY_INVALID, int iMeterLow = -1, @@ -219,6 +219,8 @@ public: int GetNumStepsLoadedFromProfile( ProfileSlot slot ) const; bool IsEditAlreadyLoaded( Steps* pSteps ) const; + void MakeUniqueEditDescription( StepsType st, CString &sPreferredDescriptionInOut ); + // An array of keysound file names (e.g. "beep.wav"). // The index in this array corresponds to the index in TapNote. If you // change the index in here, you must change all NoteData too.