Refactoring, renaming and cleaning-up

This commit is contained in:
konstin 2014-05-30 20:26:44 +02:00
parent feeab733e4
commit 02677b38c2
6 changed files with 164 additions and 153 deletions

View File

@ -52,7 +52,7 @@ GrandPrixData::GrandPrixData(const unsigned int number_of_tracks,
{
m_filename = "Random GP - Not loaded from a file!";
m_id = "random";
m_name = L"Random";
m_name = L"Random Grand Prix";
m_editable = false;
m_tracks.reserve(number_of_tracks);

View File

@ -39,57 +39,60 @@
#include <IGUIEnvironment.h>
#include <IGUIStaticText.h>
using namespace irr::gui;
using namespace irr::video;
using namespace irr::core;
using namespace GUIEngine;
using irr::gui::IGUIStaticText;
using GUIEngine::PROP_ID;
// ----------------------------------------------------------------------------
typedef GUIEngine::LabelWidget Label;
GPInfoDialog::GPInfoDialog(const std::string& gp_ident) :
ModalDialog(PERCENT_WIDTH, PERCENT_HEIGHT)
{
doInit();
m_curr_time = 0.0f;
m_gp_ident = gp_ident;
m_gp = grand_prix_manager->getGrandPrix(gp_ident);
assert (m_gp != NULL);
m_gp->checkConsistency();
// ---- GP Name
core::rect< s32 > area_top(0, 0, m_area.getWidth(), m_area.getHeight()/7);
IGUIStaticText* title = GUIEngine::getGUIEnv()->addStaticText( translations->fribidize(m_gp->getName()),
area_top, false, true, // border, word wrap
m_irrlicht_window);
title->setTabStop(false);
title->setTextAlignment(EGUIA_CENTER, EGUIA_CENTER);
m_under_title = m_area.getHeight()/7;
m_over_body = m_area.getHeight()/7;
m_lower_bound = m_area.getHeight()*6/7;
displayTracks(m_area.getHeight()/7, m_area.getHeight()*6/7);
InitAfterDrawingTheHeader(m_area.getHeight()/7, m_area.getHeight()*6/7, gp_ident);
addTitle();
addTracks();
addScreenshot();
addButtons();
}
// ----------------------------------------------------------------------------
GPInfoDialog::~GPInfoDialog()
{
// Place focus back on selected GP, in case the dialog was cancelled and we're back to
// the track selection screen after
Screen* curr_screen = GUIEngine::getCurrentScreen();
GUIEngine::Screen* curr_screen = GUIEngine::getCurrentScreen();
if (curr_screen->getName() == "tracks.stkgui")
{
((TracksScreen*)curr_screen)->setFocusOnGP(m_gp_ident);
}
static_cast<TracksScreen*>(curr_screen)->setFocusOnGP(m_gp->getId());
}
// ----------------------------------------------------------------------------
void GPInfoDialog::displayTracks(const int upper_bound,
const int lower_bound)
void GPInfoDialog::addTitle()
{
core::rect< s32 > area_top(0, 0, m_area.getWidth(), m_under_title);
IGUIStaticText* title = GUIEngine::getGUIEnv()->addStaticText(
translations->fribidize(m_gp->getName()),
area_top, false, true, // border, word wrap
m_irrlicht_window);
title->setTabStop(false);
title->setTextAlignment(irr::gui::EGUIA_CENTER, irr::gui::EGUIA_CENTER);
}
// ----------------------------------------------------------------------------
void GPInfoDialog::addTracks()
{
const std::vector<std::string> tracks = m_gp->getTrackNames();
const unsigned int track_amount = tracks.size();
int height_of_one_line = std::min((lower_bound - upper_bound)/(track_amount+1),
int height_of_one_line = std::min((m_lower_bound - m_over_body)/(track_amount+1),
(unsigned int)(GUIEngine::getFontHeight()*1.5f));
// Count the number of label already existing labels representing a track
@ -111,9 +114,9 @@ void GPInfoDialog::displayTracks(const int upper_bound,
while (m_widgets.get(widgets_iter)->m_properties[PROP_ID] != "Track label")
widgets_iter++;
LabelWidget* widget = dynamic_cast<LabelWidget*>(m_widgets.get(widgets_iter));
Label* widget = dynamic_cast<Label*>(m_widgets.get(widgets_iter));
widget->setText(translations->fribidize(track->getName()), false);
widget->move(20, upper_bound + height_of_one_line*(i+1),
widget->move(20, m_over_body + height_of_one_line*(i+1),
m_area.getWidth()/2 - 20, height_of_one_line);
widgets_iter++;
@ -128,14 +131,14 @@ void GPInfoDialog::displayTracks(const int upper_bound,
Track* track = track_manager->getTrack(tracks[i]);
assert(track != NULL);
LabelWidget* widget = new LabelWidget();
Label* widget = new Label();
widget->m_properties[PROP_ID] = "Track label";
widget->setText(translations->fribidize(track->getName()), false);
widget->setParent(m_irrlicht_window);
m_widgets.push_back(widget);
widget->add();
widget->move(20, upper_bound + height_of_one_line*(i+1),
widget->move(20, m_over_body + height_of_one_line*(i+1),
m_area.getWidth()/2 - 20, height_of_one_line);
}
}
@ -156,26 +159,22 @@ void GPInfoDialog::displayTracks(const int upper_bound,
// ----------------------------------------------------------------------------
void GPInfoDialog::InitAfterDrawingTheHeader(const int upper_bound,
const int lower_bound,
const std::string& gp_ident)
void GPInfoDialog::addScreenshot()
{
const std::vector<std::string> tracks = m_gp->getTrackNames();
// ---- Track screenshot
m_screenshot_widget = new IconButtonWidget(IconButtonWidget::SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO,
false /* tab stop */, false /* focusable */,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE /* Track gives us absolute paths */);
m_screenshot_widget = new GUIEngine::IconButtonWidget(
GUIEngine::IconButtonWidget::SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO,
false, false, GUIEngine::IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
// images are saved squared, but must be stretched to 4:3
m_screenshot_widget->setCustomAspectRatio(4.0f / 3.0f);
m_screenshot_widget->m_x = m_area.getWidth()/2-20;
m_screenshot_widget->m_y = upper_bound + 10;
m_screenshot_widget->m_y = m_over_body + 10;
// Scale the picture to the biggest possible size without an overflow
if (lower_bound - upper_bound - 20 < m_area.getWidth()/2*3/4)
if (m_lower_bound - m_over_body - 20 < m_area.getWidth()/2*3/4)
{
m_screenshot_widget->m_w = (lower_bound - upper_bound - 30)*4/3;
m_screenshot_widget->m_h = lower_bound - upper_bound - 30;
m_screenshot_widget->m_w = (m_lower_bound - m_over_body - 30)*4/3;
m_screenshot_widget->m_h = m_lower_bound - m_over_body - 30;
}
else
{
@ -183,48 +182,41 @@ void GPInfoDialog::InitAfterDrawingTheHeader(const int upper_bound,
m_screenshot_widget->m_h = m_area.getWidth()*3/8; // *(3/4)*(1/2)
}
Track* track = (tracks.size() == 0 ? NULL : track_manager->getTrack(tracks[0]));
m_screenshot_widget->m_properties[PROP_ICON] = (track != NULL ?
track->getScreenshotFile().c_str() :
file_manager->getAsset(FileManager::GUI,"main_help.png"));
Track* track = track_manager->getTrack(m_gp->getTrackNames()[0]);
m_screenshot_widget->m_properties[GUIEngine::PROP_ICON] = (track->getScreenshotFile().c_str());
m_screenshot_widget->setParent(m_irrlicht_window);
m_screenshot_widget->add();
m_widgets.push_back(m_screenshot_widget);
}
// ----------------------------------------------------------------------------
void GPInfoDialog::addButtons()
{
// ---- Start button
ButtonWidget* okBtn = new ButtonWidget();
ButtonWidget* continueBtn = new ButtonWidget();
GUIEngine::ButtonWidget* okBtn = new GUIEngine::ButtonWidget();
GUIEngine::ButtonWidget* continueBtn = new GUIEngine::ButtonWidget();
SavedGrandPrix* saved_gp = SavedGrandPrix::getSavedGP( StateManager::get()
->getActivePlayerProfile(0)
->getUniqueID(),
gp_ident,
m_gp->getId(),
race_manager->getDifficulty(),
race_manager->getNumberOfKarts(),
race_manager->getNumLocalPlayers());
if (tracks.size() == 0)
{
okBtn->m_properties[PROP_ID] = "cannot_start";
okBtn->setText(_("Sorry, no tracks available"));
}
else
{
okBtn->m_properties[PROP_ID] = "start";
okBtn->setText(_("Start Grand Prix"));
okBtn->m_properties[PROP_ID] = "start";
okBtn->setText(_("Start Grand Prix"));
continueBtn->m_properties[PROP_ID] = "continue";
continueBtn->setText(_("Continue"));
}
continueBtn->m_properties[PROP_ID] = "continue";
continueBtn->setText(_("Continue"));
if (saved_gp)
{
continueBtn->m_x = m_area.getWidth()/2 + 110;
continueBtn->m_y = lower_bound;
continueBtn->m_y = m_lower_bound;
continueBtn->m_w = 200;
continueBtn->m_h = m_area.getHeight() - lower_bound - 15;
continueBtn->m_h = m_area.getHeight() - m_lower_bound - 15;
continueBtn->setParent(m_irrlicht_window);
m_widgets.push_back(continueBtn);
continueBtn->add();
@ -238,9 +230,9 @@ void GPInfoDialog::InitAfterDrawingTheHeader(const int upper_bound,
okBtn->m_x = m_area.getWidth()/2 - 200;
}
okBtn->m_y = lower_bound;
okBtn->m_y = m_lower_bound;
okBtn->m_w = 400;
okBtn->m_h = m_area.getHeight() - lower_bound - 15;
okBtn->m_h = m_area.getHeight() - m_lower_bound - 15;
okBtn->setParent(m_irrlicht_window);
m_widgets.push_back(okBtn);
okBtn->add();
@ -250,36 +242,30 @@ void GPInfoDialog::InitAfterDrawingTheHeader(const int upper_bound,
okBtn->setFocusForPlayer( PLAYER_ID_GAME_MASTER );
}
// ----------------------------------------------------------------------------
void GPInfoDialog::onEnterPressedInternal()
{
std::string gp_id = m_gp->getId();
ModalDialog::dismiss();
// Disable accidentally unlocking of a challenge
PlayerManager::getCurrentPlayer()->setCurrentChallenge("");
race_manager->startGP(m_gp, false, false);
race_manager->startGP(grand_prix_manager->getGrandPrix(gp_id), false, false);
}
// ----------------------------------------------------------------------------
GUIEngine::EventPropagation GPInfoDialog::processEvent(const std::string& eventSource)
{
if (eventSource == "start")
{
ModalDialog::dismiss();
race_manager->startGP(m_gp, false, false);
return GUIEngine::EVENT_BLOCK;
}
if (eventSource == "continue")
if (eventSource == "start" || eventSource == "continue")
{
// Save GP identifier, since dismiss will delete this object.
std::string gp_id = m_gp_ident;
std::string gp_id = m_gp->getId();
ModalDialog::dismiss();
race_manager->startGP(grand_prix_manager->getGrandPrix(gp_id), false, true);
race_manager->startGP(grand_prix_manager->getGrandPrix(gp_id), false,
(eventSource == "continue"));
return GUIEngine::EVENT_BLOCK;
}
else if (eventSource == "cannot_start")
{
sfx_manager->quickSound( "anvil" );
}
return GUIEngine::EVENT_LET;
}
@ -288,12 +274,12 @@ GUIEngine::EventPropagation GPInfoDialog::processEvent(const std::string& eventS
void GPInfoDialog::onUpdate(float dt)
{
const int frameBefore = (int)(m_curr_time / 1.5f);
if (dt == 0)
return; // if nothing changed, return right now
m_curr_time += dt;
int frameAfter = (int)(m_curr_time / 1.5f);
if (frameAfter == frameBefore) return; // if nothing changed, return right now
const std::vector<std::string> tracks = m_gp->getTrackNames();
if (frameAfter >= (int)tracks.size())
{
@ -301,11 +287,9 @@ void GPInfoDialog::onUpdate(float dt)
m_curr_time = 0;
}
Track* track = (tracks.size() == 0 ? NULL :
track_manager->getTrack(tracks[frameAfter]));
std::string fn = track ? track->getScreenshotFile()
: file_manager->getAsset(FileManager::GUI, "main_help.png");
m_screenshot_widget->setImage(fn.c_str(), IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
Track* track = track_manager->getTrack(tracks[frameAfter]);
std::string file = track->getScreenshotFile();
typedef GUIEngine::IconButtonWidget Icon;
m_screenshot_widget->setImage(file.c_str(), Icon::ICON_PATH_TYPE_ABSOLUTE);
}
// ----------------------------------------------------------------------------

View File

@ -33,33 +33,41 @@ namespace GUIEngine
*/
class GPInfoDialog : public GUIEngine::ModalDialog
{
protected: // Necessary for randomGPInfoDialog
std::string m_gp_ident;
protected: // Necessary for RandomGPInfoDialog
GUIEngine::IconButtonWidget* m_screenshot_widget;
float m_curr_time;
GrandPrixData* m_gp;
/** height of the separator over the body */
int m_over_body;
/** height of the separator under the titlebar, which is equal to
* m_over_body in a normal GPInfoDialo and lower in RandomGPInfoDialog. */
int m_under_title;
/** height of the seperator over the buttons */
int m_lower_bound;
void addTitle();
/** \brief display all the tracks according to the current gp
* For a normal gp info dialog, it just creates a label for every track.
* But with a random gp info dialog, it tries to reuse as many
* labels as possible by just changing their text. */
void addTracks();
void addScreenshot();
/** display a ok-button and eventually a continue-button */
void addButtons();
/** only used for track_screen.cpp */
GPInfoDialog() : ModalDialog(PERCENT_WIDTH, PERCENT_HEIGHT) {}
public:
static const float PERCENT_WIDTH = 0.8f;
static const float PERCENT_HEIGHT = 0.7f;
/**
* Creates a modal dialog with given percentage of screen width and height
* atm only used in track_screen.cpp
*/
GPInfoDialog() : ModalDialog(PERCENT_WIDTH, PERCENT_HEIGHT) {}
GPInfoDialog(const std::string& gpIdent);
/** Places the focus back on the selected GP, in the case that the dialog
* was cancelled and we're returning to the track selection screen */
virtual ~GPInfoDialog();
void InitAfterDrawingTheHeader(const int y1, const int y2,
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 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();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);

View File

@ -17,20 +17,21 @@
#include "guiengine/engine.hpp"
#include "guiengine/widgets/spinner_widget.hpp"
#include "guiengine/widget.hpp"
#include "race/grand_prix_manager.hpp"
#include "race/race_manager.hpp"
#include "states_screens/dialogs/random_gp_dialog.hpp"
#include "tracks/track_manager.hpp"
#include <IGUIEnvironment.h>
#include <IGUIStaticText.h>
using namespace irr::gui;
using namespace irr::video;
using namespace irr::core;
using namespace GUIEngine;
using irr::core::stringc;
using irr::core::stringw;
using irr::gui::IGUIStaticText;
randomGPInfoDialog::randomGPInfoDialog()
typedef GUIEngine::SpinnerWidget Spinner;
RandomGPInfoDialog::RandomGPInfoDialog()
{
// Defaults - loading selection from last time frrom a file would be better
m_number_of_tracks = 2;
@ -40,27 +41,30 @@ randomGPInfoDialog::randomGPInfoDialog()
doInit();
m_curr_time = 0.0f;
int y1 = m_area.getHeight()/7;
m_under_title = m_area.getHeight()/7;
m_over_body = m_area.getHeight()/7 + SPINNER_HEIGHT + 10; // 10px space
m_lower_bound = m_area.getHeight()*6/7;
m_gp_ident = "random";
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
// ---- GP Name
core::rect< s32 > area_top(0, 0, m_area.getWidth(), y1);
IGUIStaticText* title = GUIEngine::getGUIEnv()->addStaticText(translations->fribidize("Random Grand Prix"),
area_top, false, true, // border, word wrap
m_irrlicht_window);
title->setTabStop(false);
title->setTextAlignment(EGUIA_CENTER, EGUIA_CENTER);
addTitle();
addSpinners();
addTracks();
addScreenshot();
addButtons();
}
// ---- Spinners
// ----------------------------------------------------------------------------
void RandomGPInfoDialog::addSpinners()
{
// Trackgroup chooser
GUIEngine::SpinnerWidget* spinner = new GUIEngine::SpinnerWidget(false);
spinner->m_properties[PROP_ID] = "Trackgroup";
Spinner* spinner = new Spinner(false);
spinner->m_properties[GUIEngine::PROP_ID] = "Trackgroup";
spinner->setParent(m_irrlicht_window);
m_widgets.push_back(spinner);
spinner->add();
spinner->move(10, m_area.getHeight()/7, 300, 40);
spinner->move(10, m_under_title, 300, SPINNER_HEIGHT);
// Fill it with with all the track group names
spinner->addLabel("all");
const std::vector<std::string>& groups = track_manager->getAllTrackGroups();
@ -68,35 +72,41 @@ randomGPInfoDialog::randomGPInfoDialog()
spinner->addLabel(stringw(groups[i].c_str()));
// Number of laps chooser
spinner = new GUIEngine::SpinnerWidget(false);
spinner = new Spinner(false);
spinner->setValue(m_number_of_tracks);
spinner->setMin(1);
spinner->setMax(track_manager->getNumberOfTracks()); // default is "all"
spinner->setParent(m_irrlicht_window);
spinner->m_properties[PROP_ID] = "Number of tracks";
spinner->m_properties[GUIEngine::PROP_ID] = "Number of tracks";
m_widgets.push_back(spinner);
spinner->add();
spinner->move(310, m_area.getHeight()/7, 200, 40);
displayTracks(m_area.getHeight()/7 + 50, m_area.getHeight()*6/7);
InitAfterDrawingTheHeader(m_area.getHeight()/7 + 50,
m_area.getHeight()*6/7,
"random");
spinner->move(310, m_under_title, 200, SPINNER_HEIGHT);
}
GUIEngine::EventPropagation randomGPInfoDialog::processEvent(
// ----------------------------------------------------------------------------
GUIEngine::EventPropagation RandomGPInfoDialog::processEvent(
const std::string& eventSource)
{
if (eventSource == "Number of tracks")
if (eventSource == "start")
{
m_number_of_tracks = getWidget<GUIEngine::SpinnerWidget>("Number of tracks")->getValue();
// Save the gp since dismiss deletes it otherwise
GrandPrixData buff = *m_gp;
ModalDialog::dismiss();
race_manager->startGP(&buff, false, false);
return GUIEngine::EVENT_BLOCK;
}
else if (eventSource == "Number of tracks")
{
// 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);
displayTracks(m_area.getHeight()/7, m_area.getHeight()*6/7);
addTracks();
}
else if (eventSource == "Trackgroup")
{
GUIEngine::SpinnerWidget* t = getWidget<GUIEngine::SpinnerWidget>("Trackgroup");
GUIEngine::SpinnerWidget* s = getWidget<GUIEngine::SpinnerWidget>("Number of tracks");
Spinner* t = getWidget<Spinner>("Trackgroup");
Spinner* s = getWidget<Spinner>("Number of tracks");
m_trackgroup = stringc(t->getStringValue()).c_str();
@ -115,15 +125,17 @@ GUIEngine::EventPropagation randomGPInfoDialog::processEvent(
updateGP();
}
return GPInfoDialog::processEvent(eventSource);
return GUIEngine::EVENT_LET;
}
void randomGPInfoDialog::updateGP()
// ----------------------------------------------------------------------------
void RandomGPInfoDialog::updateGP()
{
if (m_gp != NULL)
delete m_gp;
m_gp = new GrandPrixData(m_number_of_tracks, m_trackgroup, m_use_reverse);
displayTracks(m_area.getHeight()/7, m_area.getHeight()*6/7);
addTracks();
}

View File

@ -18,12 +18,11 @@
#ifndef HEADER_RANDOM_GP_INFO_DIALOG_HPP
#define HEADER_RANDOM_GP_INFO_DIALOG_HPP
#include "guiengine/widgets/spinner_widget.hpp"
#include "states_screens/dialogs/gp_info_dialog.hpp"
#include <string>
class randomGPInfoDialog : public GPInfoDialog
class RandomGPInfoDialog : public GPInfoDialog
{
private:
unsigned int m_number_of_tracks;
@ -31,10 +30,17 @@ private:
bool m_use_reverse;
public:
randomGPInfoDialog();
~randomGPInfoDialog() { delete m_gp; }
static const int SPINNER_HEIGHT = 40;
RandomGPInfoDialog();
~RandomGPInfoDialog() { delete m_gp; }
/** Adds a SpinnerWidgets to choose the track groups and one to choose the
* number of tracks */
void addSpinners();
GUIEngine::EventPropagation processEvent(const std::string& eventSource);
/** Constructs a new gp from the members */
void updateGP();
};

View File

@ -119,10 +119,10 @@ void TracksScreen::eventCallback(Widget* widget, const std::string& name,
}
else
{
if (selection != "Random")
new GPInfoDialog(selection);
if (selection == "Random Grand Prix")
new RandomGPInfoDialog();
else
new randomGPInfoDialog();
new GPInfoDialog(selection);
}
}
else if (name == "trackgroups")
@ -214,10 +214,11 @@ void TracksScreen::init()
}
}
// Random GP - not finished yet
// Random GP
std::vector<std::string> screenshots;
screenshots.push_back("gui/main_help.png");
gps_widget->addAnimatedItem(translations->fribidize("Random"), "Random",
gps_widget->addAnimatedItem(translations->fribidize("Random Grand Prix"),
"Random Grand Prix",
screenshots, 1.5f, 0,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);