Skip to content

Commit

Permalink
Remove code duplication in the help screens
Browse files Browse the repository at this point in the history
Same approach as with the options screen: managing all the generic includes needed by all help screens in one place and adding a new standalone 'switchTab function
  • Loading branch information
Alayan-stk-2 committed May 5, 2024
1 parent fa421b4 commit 7859157
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 203 deletions.
44 changes: 44 additions & 0 deletions src/states_screens/help/help_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2009-2015 Marianne Gagnon
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_common.hpp"

#include "guiengine/screen.hpp"

namespace HelpCommon
{
void switchTab(std::string selected_tab)
{
GUIEngine::Screen *screen = NULL;
if (selected_tab == "page1")
screen = HelpScreen1::getInstance();
else if (selected_tab == "page2")
screen = HelpScreen2::getInstance();
else if (selected_tab == "page3")
screen = HelpScreen3::getInstance();
else if (selected_tab == "page4")
screen = HelpScreen4::getInstance();
else if (selected_tab == "page5")
screen = HelpScreen5::getInstance();
else if (selected_tab == "page6")
screen = HelpScreen6::getInstance();
else if (selected_tab == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
}
}
45 changes: 45 additions & 0 deletions src/states_screens/help/help_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2009-2015 Marianne Gagnon
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#ifndef __HEADER_HELP_COMMON_HPP__
#define __HEADER_HELP_COMMON_HPP__

// This file contains include headers that are used by all or most help screens.
// It also contains a standalone function to switch between the help screens.
// This simplifies maintenance.

// Frequent widgets used by multiple option screens
#include "guiengine/widgets/ribbon_widget.hpp"

// Other help screens, for navigation between them
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"

// GUI management
#include "states_screens/state_manager.hpp"

namespace HelpCommon
{
void switchTab(std::string selected_tab);
}

#endif
37 changes: 4 additions & 33 deletions src/states_screens/help/help_screen_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,19 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_1.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

#include "challenges/unlock_manager.hpp"
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "guiengine/widget.hpp"
#include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/list_widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "input/device_manager.hpp"
#include "input/input_manager.hpp"
#include "input/keyboard_device.hpp"
#include "karts/kart_properties_manager.hpp"
#include "race/race_manager.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"

using namespace GUIEngine;

// FIXME : it's hugely repetitive to have one class per help screen when
// THEY ALL DO THE SAME THING
// (the specialized test of this first screen is a tiny exception)
// -----------------------------------------------------------------------------

HelpScreen1::HelpScreen1() : Screen("help/help1.stkgui")
Expand Down Expand Up @@ -97,23 +83,8 @@ void HelpScreen1::eventCallback(Widget* widget, const std::string& name, const i
{
std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
//if (selection == "page1")
// screen = HelpScreen1::getInstance();
if (selection == "page2")
screen = HelpScreen2::getInstance();
else if (selection == "page3")
screen = HelpScreen3::getInstance();
else if (selection == "page4")
screen = HelpScreen4::getInstance();
else if (selection == "page5")
screen = HelpScreen5::getInstance();
else if (selection == "page6")
screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page1")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
32 changes: 4 additions & 28 deletions src/states_screens/help/help_screen_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_2.hpp"

#include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

using namespace GUIEngine;

Expand All @@ -49,23 +40,8 @@ void HelpScreen2::eventCallback(Widget* widget, const std::string& name, const i
{
std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
if (selection == "page1")
screen = HelpScreen1::getInstance();
//else if (selection == "page2")
// screen = HelpScreen2::getInstance();
else if (selection == "page3")
screen = HelpScreen3::getInstance();
else if (selection == "page4")
screen = HelpScreen4::getInstance();
else if (selection == "page5")
screen = HelpScreen5::getInstance();
else if (selection == "page6")
screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page2")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
33 changes: 4 additions & 29 deletions src/states_screens/help/help_screen_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_3.hpp"

#include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "modes/world.hpp"
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

using namespace GUIEngine;

Expand All @@ -51,23 +41,8 @@ void HelpScreen3::eventCallback(Widget* widget, const std::string& name, const i

std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
if (selection == "page1")
screen = HelpScreen1::getInstance();
else if (selection == "page2")
screen = HelpScreen2::getInstance();
//else if (selection == "page3")
// screen = HelpScreen3::getInstance();
else if (selection == "page4")
screen = HelpScreen4::getInstance();
else if (selection == "page5")
screen = HelpScreen5::getInstance();
else if (selection == "page6")
screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page3")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
33 changes: 4 additions & 29 deletions src/states_screens/help/help_screen_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_4.hpp"

#include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "modes/world.hpp"
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

using namespace GUIEngine;

Expand All @@ -51,23 +41,8 @@ void HelpScreen4::eventCallback(Widget* widget, const std::string& name, const i

std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
if (selection == "page1")
screen = HelpScreen1::getInstance();
else if (selection == "page2")
screen = HelpScreen2::getInstance();
else if (selection == "page3")
screen = HelpScreen3::getInstance();
//else if (selection == "page4")
// screen = HelpScreen4::getInstance();
else if (selection == "page5")
screen = HelpScreen5::getInstance();
else if (selection == "page6")
screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page4")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
32 changes: 4 additions & 28 deletions src/states_screens/help/help_screen_5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_5.hpp"

#include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_6.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

using namespace GUIEngine;

Expand All @@ -50,23 +41,8 @@ void HelpScreen5::eventCallback(Widget* widget, const std::string& name, const i

std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
if (selection == "page1")
screen = HelpScreen1::getInstance();
else if (selection == "page2")
screen = HelpScreen2::getInstance();
else if (selection == "page3")
screen = HelpScreen3::getInstance();
else if (selection == "page4")
screen = HelpScreen4::getInstance();
//else if (selection == "page5")
// screen = HelpScreen5::getInstance();
else if (selection == "page6")
screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page5")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
32 changes: 4 additions & 28 deletions src/states_screens/help/help_screen_6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "states_screens/help/help_screen_6.hpp"

#include "guiengine/widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "states_screens/help/help_screen_1.hpp"
#include "states_screens/help/help_screen_2.hpp"
#include "states_screens/help/help_screen_3.hpp"
#include "states_screens/help/help_screen_4.hpp"
#include "states_screens/help/help_screen_5.hpp"
#include "states_screens/help/help_screen_7.hpp"
#include "states_screens/state_manager.hpp"
// Manages includes common to all help screens
#include "states_screens/help/help_common.hpp"

using namespace GUIEngine;

Expand All @@ -50,23 +41,8 @@ void HelpScreen6::eventCallback(Widget* widget, const std::string& name, const i

std::string selection = ((RibbonWidget*)widget)->getSelectionIDString(PLAYER_ID_GAME_MASTER);

Screen *screen = NULL;
if (selection == "page1")
screen = HelpScreen1::getInstance();
else if (selection == "page2")
screen = HelpScreen2::getInstance();
else if (selection == "page3")
screen = HelpScreen3::getInstance();
else if (selection == "page4")
screen = HelpScreen4::getInstance();
else if (selection == "page5")
screen = HelpScreen5::getInstance();
//else if (selection == "page6")
// screen = HelpScreen6::getInstance();
else if (selection == "page7")
screen = HelpScreen7::getInstance();
if(screen)
StateManager::get()->replaceTopMostScreen(screen);
if (selection != "page6")
HelpCommon::switchTab(selection);
}
else if (name == "back")
{
Expand Down
Loading

0 comments on commit 7859157

Please sign in to comment.