more intellegent track adding and removing

This commit is contained in:
konstin 2014-05-27 20:49:02 +02:00
parent 0d959a72aa
commit feeab733e4
4 changed files with 44 additions and 16 deletions

View File

@ -55,6 +55,18 @@ GrandPrixData::GrandPrixData(const unsigned int number_of_tracks,
m_name = L"Random";
m_editable = false;
m_tracks.reserve(number_of_tracks);
m_laps.reserve(number_of_tracks);
m_reversed.reserve(number_of_tracks);
changeTrackNumber(number_of_tracks, track_group, use_reverse);
}
// ----------------------------------------------------------------------------
void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks,
const std::string& track_group,
const bool use_reverse)
{
// The problem with the track groups is that "all" isn't a track group
// TODO: Add "all" to the track groups and rewrite this more elegant
std::vector<int> track_indices;
@ -70,20 +82,32 @@ GrandPrixData::GrandPrixData(const unsigned int number_of_tracks,
}
assert(number_of_tracks <= available_tracks);
m_tracks.reserve(number_of_tracks);
m_laps.reserve(number_of_tracks);
m_reversed.reserve(number_of_tracks);
for (unsigned int i = 0; i < number_of_tracks; i++)
// add or remove the right number of tracks
if (m_tracks.size() < number_of_tracks)
{
int index = (track_group == "all") ?
rand() % available_tracks :
track_indices[rand() % available_tracks];
while (m_tracks.size() < number_of_tracks)
{
int index = (track_group == "all") ?
rand() % available_tracks :
track_indices[rand() % available_tracks];
m_tracks.push_back(track_manager->getTrack(index)->getIdent());
m_laps.push_back(3);
m_reversed.push_back(rand() % 2);
m_tracks.push_back(track_manager->getTrack(index)->getIdent());
m_laps.push_back(3); // TODO: Take the default number from the track
m_reversed.push_back(rand() % 2);
}
}
else if (m_tracks.size() > number_of_tracks)
{
while (m_tracks.size() > number_of_tracks)
{
m_tracks.pop_back();
m_laps.pop_back();
m_reversed.pop_back();
}
}
assert(m_tracks.size() == m_laps.size() );
assert(m_laps.size() == m_reversed.size());
}
// ----------------------------------------------------------------------------

View File

@ -80,6 +80,9 @@ public:
GrandPrixData(const unsigned int number_of_tracks,
const std::string& m_track_group,
const bool use_reverse);
void changeTrackNumber(const unsigned int number_of_tracks,
const std::string& track_group,
const bool use_reverse);
// Methods for the GP editor
void setId(const std::string& id);

View File

@ -54,10 +54,10 @@ public:
const std::string& gp_ident);
/** \brief display all the tracks according to the current gp
* For a normal gp info dialog, it just creates a label with the name for
* every track. But in a random gp info dialog, it tries to reuse as many
* labels as possible by just changing their labels. If there are less than
* need, some are added, if there are to many, some become deleted. */
* For a normal gp info dialog, it just creates a label for every track.
* With its name. But in a random gp info dialog, it tries to reuse as many
* labels as possible by just changing their text. If there are less than
* need, some are added, if there are to many, some are deleted. */
void displayTracks(const int y1, const int y2);
void onEnterPressedInternal();

View File

@ -90,7 +90,8 @@ GUIEngine::EventPropagation randomGPInfoDialog::processEvent(
if (eventSource == "Number of tracks")
{
m_number_of_tracks = getWidget<GUIEngine::SpinnerWidget>("Number of tracks")->getValue();
updateGP();
m_gp->changeTrackNumber(m_number_of_tracks, m_trackgroup, m_use_reverse);
displayTracks(m_area.getHeight()/7, m_area.getHeight()*6/7);
}
else if (eventSource == "Trackgroup")
{