add a reverse spinner

This commit is contained in:
konstin
2014-06-02 16:56:59 +02:00
parent 03768db82a
commit ce661a4dd4
7 changed files with 84 additions and 39 deletions

View File

@@ -3,6 +3,8 @@
#include "race/race_manager.hpp"
#include "utils/log.hpp"
#include <stdexcept>
/** \brief Gets the element with the highest count in a std::map<S,int>.
* \param histogram : A pointer to the histogram.
* \return The key of type S that has the highest second value.

View File

@@ -24,11 +24,13 @@
#include "config/player_manager.hpp"
#include "io/file_manager.hpp"
#include "io/utf_writer.hpp"
#include "states_screens/dialogs/random_gp_dialog.hpp"
#include "tracks/track_manager.hpp"
#include "tracks/track.hpp"
#include "utils/string_utils.hpp"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <memory>
#include <stdexcept>
@@ -46,25 +48,25 @@ GrandPrixData::GrandPrixData(const std::string& filename)
// ----------------------------------------------------------------------------
GrandPrixData::GrandPrixData(const unsigned int number_of_tracks,
const std::string& track_group,
const bool use_reverse)
const std::string& track_group,
const RandomGPInfoDialog::REVERSED use_reverse)
{
m_filename = "Random GP - Not loaded from a file!";
m_id = "random";
m_name = L"Random Grand Prix";
m_name = "Random Grand Prix";
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);
changeTrackNumber(number_of_tracks, track_group);
changeReverse(use_reverse);
}
// ----------------------------------------------------------------------------
void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks,
const std::string& track_group,
const bool use_reverse)
const std::string& track_group)
{
// 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
@@ -97,7 +99,7 @@ void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks,
m_tracks.push_back(id);
m_laps.push_back(3); // TODO: Take the default number from the track
m_reversed.push_back(rand() % 2);
m_reversed.push_back(false); // This will be changed later
}
}
else if (m_tracks.size() > number_of_tracks)
@@ -114,6 +116,24 @@ void GrandPrixData::changeTrackNumber(const unsigned int number_of_tracks,
assert(m_laps.size() == m_reversed.size());
}
// ----------------------------------------------------------------------------
void GrandPrixData::changeReverse(const RandomGPInfoDialog::REVERSED use_reverse)
{
for (unsigned int i = 0; i < m_tracks.size(); i++)
{
if (use_reverse == RandomGPInfoDialog::NO_REVERSE)
m_reversed[i] = false;
else if (use_reverse == RandomGPInfoDialog::MIXED)
if (track_manager->getTrack(m_tracks[i])->reverseAvailable())
m_reversed[i] = rand() % 2;
else
m_reversed[i] = false;
else // all reversed
m_reversed[i] = true;
}
}
// ----------------------------------------------------------------------------
void GrandPrixData::setId(const std::string& id)
{

View File

@@ -20,14 +20,15 @@
#ifndef HEADER_GRAND_PRIX_DATA_HPP
#define HEADER_GRAND_PRIX_DATA_HPP
#include <irrString.h>
#include <string>
#include <vector>
#include <cassert>
#include <irrString.h>
#include <stdexcept>
#include "states_screens/dialogs/random_gp_dialog.hpp"
#include "utils/translation.hpp"
using irr::core::stringw;
class Track;
/** Simple class that hold the data relevant to a 'grand_prix', aka. a number
@@ -78,11 +79,11 @@ public:
GrandPrixData() {};
/** TODO */
GrandPrixData(const unsigned int number_of_tracks,
const std::string& m_track_group,
const bool use_reverse);
const std::string& track_group,
const RandomGPInfoDialog::REVERSED use_reverse);
void changeTrackNumber(const unsigned int number_of_tracks,
const std::string& track_group,
const bool use_reverse);
const std::string& track_group);
void changeReverse(const RandomGPInfoDialog::REVERSED use_reverse);
// Methods for the GP editor
void setId(const std::string& id);

View File

@@ -27,8 +27,6 @@
#include <set>
#include <sstream>
#include <iostream> // remove later
GrandPrixManager *grand_prix_manager = NULL;
const char* GrandPrixManager::SUFFIX = ".grandprix";
@@ -159,7 +157,6 @@ GrandPrixData* GrandPrixManager::getGrandPrix(const std::string& s) const
// ----------------------------------------------------------------------------
GrandPrixData* GrandPrixManager::editGrandPrix(const std::string& s) const
{
std::cout << s << std::endl;
if (s == "random")
return m_random_gp;

View File

@@ -20,7 +20,10 @@
#define HEADER_GP_INFO_DIALOG_HPP
#include "guiengine/modaldialog.hpp"
#include "race/grand_prix_data.hpp"
// Don't include grand_prix_data.hpp here or the compilation will fail
class GrandPrixData;
namespace GUIEngine
{

View File

@@ -36,7 +36,7 @@ RandomGPInfoDialog::RandomGPInfoDialog()
// Defaults - loading selection from last time frrom a file would be better
m_number_of_tracks = 2;
m_trackgroup = "all";
m_use_reverse = true;
m_use_reverse = NO_REVERSE;
doInit();
m_curr_time = 0.0f;
@@ -65,13 +65,16 @@ RandomGPInfoDialog::RandomGPInfoDialog()
void RandomGPInfoDialog::addSpinners()
{
const int left = (m_area.getWidth() - 250 - 150 - 250)/2;
// Trackgroup chooser
Spinner* spinner = new Spinner(false);
spinner->m_properties[GUIEngine::PROP_ID] = "Trackgroup";
spinner->m_properties[GUIEngine::PROP_WRAP_AROUND] = "true";
spinner->setParent(m_irrlicht_window);
m_widgets.push_back(spinner);
spinner->add();
spinner->move(10, m_under_title, 300, SPINNER_HEIGHT);
spinner->move(left + 0, m_under_title, 250, SPINNER_HEIGHT);
// Fill it with with all the track group names
spinner->addLabel("all");
const std::vector<std::string>& groups = track_manager->getAllTrackGroups();
@@ -85,9 +88,22 @@ void RandomGPInfoDialog::addSpinners()
spinner->setMax(track_manager->getNumberOfTracks()); // default is "all"
spinner->setParent(m_irrlicht_window);
spinner->m_properties[GUIEngine::PROP_ID] = "Number of tracks";
spinner->m_properties[GUIEngine::PROP_WRAP_AROUND] = "true";
m_widgets.push_back(spinner);
spinner->add();
spinner->move(310, m_under_title, 200, SPINNER_HEIGHT);
spinner->move(left + 260, m_under_title, 150, SPINNER_HEIGHT);
// reverse choose
spinner = new Spinner(false);
spinner->setParent(m_irrlicht_window);
spinner->m_properties[GUIEngine::PROP_ID] = "reverse";
spinner->m_properties[GUIEngine::PROP_WRAP_AROUND] = "true";
m_widgets.push_back(spinner);
spinner->add();
spinner->move(left + 410, m_under_title, 250, SPINNER_HEIGHT);
spinner->addLabel("no reverse");
spinner->addLabel("all reverse");
spinner->addLabel("mixed");
}
// ----------------------------------------------------------------------------
@@ -106,7 +122,7 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
{
// The old gp can be reused because there's only track deletion/adding
m_number_of_tracks = getWidget<Spinner>("Number of tracks")->getValue();
m_gp->changeTrackNumber(m_number_of_tracks, m_trackgroup, m_use_reverse);
m_gp->changeTrackNumber(m_number_of_tracks, m_trackgroup);
addTracks();
}
else if (eventSource == "Trackgroup")
@@ -128,20 +144,18 @@ GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
if (s->getValue() > (signed)max)
s->setValue(max);
updateGP();
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
addTracks();
}
else if (eventSource == "reverse")
{
Spinner* r = getWidget<Spinner>("reverse");
m_use_reverse = static_cast<REVERSED>(r->getValue());
m_gp->changeReverse(m_use_reverse);
}
return GUIEngine::EVENT_LET;
}
// ----------------------------------------------------------------------------
void RandomGPInfoDialog::updateGP()
{
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
grand_prix_manager->m_random_gp = m_gp;
addTracks();
}

View File

@@ -24,23 +24,31 @@
class RandomGPInfoDialog : public GPInfoDialog
{
public:
// the enum has to be declared here because you can't forward declare it
// without c++11
enum REVERSED
{
NO_REVERSE = 0,
ALL_REVERSE = 1,
MIXED = 2
};
private:
unsigned int m_number_of_tracks;
std::string m_trackgroup;
bool m_use_reverse;
REVERSED m_use_reverse;
public:
static const int SPINNER_HEIGHT = 40;
RandomGPInfoDialog();
/** Adds a SpinnerWidgets to choose the track groups and one to choose the
* number of tracks */
/** Adds a SpinnerWidgets to choose the track groups, one to choose the
* number of tracks and one to choose if the tracks should be raced in
* reverse. The Spinners are centered. */
void addSpinners();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);
/** Constructs a new gp from the members */
void updateGP();
};
#endif