From 3d14ae5bf1b9afffed40ae728e9098c5d2c7342a Mon Sep 17 00:00:00 2001 From: Alayan-stk-2 Date: Thu, 13 Sep 2018 15:14:56 +0200 Subject: [PATCH] Move repeated code to a function --- .../dialogs/select_challenge.cpp | 48 +++++++------------ .../dialogs/select_challenge.hpp | 5 ++ 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/states_screens/dialogs/select_challenge.cpp b/src/states_screens/dialogs/select_challenge.cpp index e9ecd04dc..ed3f09e9d 100644 --- a/src/states_screens/dialogs/select_challenge.cpp +++ b/src/states_screens/dialogs/select_challenge.cpp @@ -111,35 +111,10 @@ SelectChallengeDialog::SelectChallengeDialog(const float percentWidth, const ChallengeStatus* c = PlayerManager::getCurrentPlayer() ->getChallengeStatus(challenge_id); - if (c->isSolved(RaceManager::DIFFICULTY_EASY)) - { - IconButtonWidget* btn = getWidget("novice"); - btn->setImage(file_manager->getAsset(FileManager::GUI, "cup_bronze.png"), - IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE); - } - - if (c->isSolved(RaceManager::DIFFICULTY_MEDIUM)) - { - IconButtonWidget* btn = getWidget("intermediate"); - btn->setImage(file_manager->getAsset(FileManager::GUI,"cup_silver.png"), - IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE); - } - - if (c->isSolved(RaceManager::DIFFICULTY_HARD)) - { - IconButtonWidget* btn = getWidget("expert"); - btn->setImage(file_manager->getAsset(FileManager::GUI,"cup_gold.png"), - IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE); - } - - if (c->isSolved(RaceManager::DIFFICULTY_BEST) - && !PlayerManager::getCurrentPlayer()->isLocked("difficulty_best")) - { - IconButtonWidget* btn = getWidget("supertux"); - btn->setImage(file_manager->getAsset(FileManager::GUI,"cup_platinum.png"), - IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE); - } - + updateSolvedIcon(c, RaceManager::DIFFICULTY_EASY, "novice", "cup_bronze.png"); + updateSolvedIcon(c, RaceManager::DIFFICULTY_MEDIUM, "intermediate", "cup_silver.png"); + updateSolvedIcon(c, RaceManager::DIFFICULTY_HARD, "expert", "cup_gold.png"); + updateSolvedIcon(c, RaceManager::DIFFICULTY_BEST, "supertux", "cup_platinum.png"); LabelWidget* novice_label = getWidget("novice_label"); LabelWidget* medium_label = getWidget("intermediate_label"); @@ -188,6 +163,19 @@ SelectChallengeDialog::~SelectChallengeDialog() // ---------------------------------------------------------------------------- +void SelectChallengeDialog::updateSolvedIcon(const ChallengeStatus* c, RaceManager::Difficulty diff, + const char* widget_name, const char* path) +{ + if (c->isSolved(diff)) + { + IconButtonWidget* btn = getWidget(widget_name); + btn->setImage(file_manager->getAsset(FileManager::GUI, path), + IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE); + } +} //updateSolvedIcon + +// ---------------------------------------------------------------------------- + GUIEngine::EventPropagation SelectChallengeDialog::processEvent(const std::string& eventSourceParam) { std::string eventSource = eventSourceParam; @@ -237,7 +225,7 @@ GUIEngine::EventPropagation SelectChallengeDialog::processEvent(const std::strin // Initialise global data - necessary even in local games to avoid // many if tests in other places (e.g. if network_game call // network_manager else call race_manager). -// network_manager->initCharacterDataStructures(); + // network_manager->initCharacterDataStructures(); // Launch challenge if (eventSource == "novice") diff --git a/src/states_screens/dialogs/select_challenge.hpp b/src/states_screens/dialogs/select_challenge.hpp index e35dcc5e7..e6aaabb8b 100644 --- a/src/states_screens/dialogs/select_challenge.hpp +++ b/src/states_screens/dialogs/select_challenge.hpp @@ -18,8 +18,10 @@ #ifndef SELECT_CHALLENGE_HPP #define SELECT_CHALLENGE_HPP +#include "challenges/challenge_status.hpp" #include "guiengine/event_handler.hpp" #include "guiengine/modaldialog.hpp" +#include "race/race_manager.hpp" /** * \brief Dialog shown when starting a challenge @@ -27,7 +29,10 @@ */ class SelectChallengeDialog : public GUIEngine::ModalDialog { +private: std::string m_challenge_id; + void updateSolvedIcon(const ChallengeStatus* c, RaceManager::Difficulty diff, + const char* widget_name, const char* path); public: SelectChallengeDialog(const float percentWidth, const float percentHeight,