Skip to content

Commit 773779f

Browse files
authored
Merge pull request #1652 from OutpostUniverse/Move-FileIO-to-GameState
Move file io to game state
2 parents e0cc91c + 4ac2d98 commit 773779f

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

appOPHD/States/GameState.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ NAS2D::Point<int> MOUSE_COORDS; /**< Mouse Coordinates. Used by other states/wra
2020

2121
GameState::GameState(const std::string& savedGameFilename) :
2222
mMainReportsState{std::make_unique<MainReportsUiState>()},
23-
mMapViewState{std::make_unique<MapViewState>(*mMainReportsState.get(), savedGameFilename)}
23+
mMapViewState{std::make_unique<MapViewState>(*this, savedGameFilename)}
2424
{
2525
initializeGameState();
2626
}
2727

2828

2929
GameState::GameState(const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty) :
3030
mMainReportsState{std::make_unique<MainReportsUiState>()},
31-
mMapViewState{std::make_unique<MapViewState>(*mMainReportsState.get(), planetAttributes, selectedDifficulty)}
31+
mMapViewState{std::make_unique<MapViewState>(*this, planetAttributes, selectedDifficulty)}
3232
{
3333
initializeGameState();
3434
}
@@ -48,6 +48,7 @@ GameState::~GameState()
4848

4949
void GameState::initializeGameState()
5050
{
51+
mFileIoDialog.fileLoadSignal().connect({this, &GameState::onLoadGame});
5152
mMainReportsState->initialize();
5253
mMainReportsState->hideReports().connect({this, &GameState::onHideReports});
5354

@@ -74,7 +75,6 @@ void GameState::initializeMapViewState()
7475
mMapViewState->quit().connect({this, &GameState::onQuit});
7576
mMapViewState->showReportsUi().connect({this, &GameState::onShowReports});
7677
mMapViewState->mapChanged().connect({this, &GameState::onMapChange});
77-
mMapViewState->fileLoadSignal().connect({this, &GameState::onLoadGame});
7878
}
7979

8080

@@ -164,7 +164,7 @@ void GameState::onLoadGame(const std::string& saveGameName)
164164
{
165165
throw std::runtime_error("Save game file does not exist: " + saveGamePath);
166166
}
167-
auto newMapViewState = std::make_unique<MapViewState>(*mMainReportsState.get(), saveGamePath);
167+
auto newMapViewState = std::make_unique<MapViewState>(*this, saveGamePath);
168168
newMapViewState->initialize();
169169
mNewMapViewState = std::move(newMapViewState);
170170
}

appOPHD/States/GameState.h

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class GameState : public NAS2D::State
2929

3030
State* update() override;
3131

32+
MainReportsUiState& mainReportsState() { return *mMainReportsState; }
33+
FileIo& fileIoDialog() { return mFileIoDialog; }
34+
3235
protected:
3336
void initializeGameState();
3437
void initializeMapViewState();
@@ -54,4 +57,5 @@ class GameState : public NAS2D::State
5457
Wrapper* mActiveState = nullptr;
5558
NAS2D::State* mReturnState = this;
5659
NAS2D::Fade mFade;
60+
FileIo mFileIoDialog;
5761
};

appOPHD/States/MapViewState.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "MainMenuState.h"
66
#include "MainReportsUiState.h"
7+
#include "GameState.h"
78
#include "Route.h"
89

910
#include "../Constants/Strings.h"
@@ -178,16 +179,17 @@ const std::map<Difficulty, int> MapViewState::ColonyShipDeorbitMoraleLossMultipl
178179
};
179180

180181

181-
MapViewState::MapViewState(MainReportsUiState& mainReportsState, const std::string& savegame) :
182+
MapViewState::MapViewState(GameState& gameState, const std::string& savegame) :
182183
mCrimeRateUpdate{mDifficulty},
183184
mCrimeExecution{mDifficulty, {this, &MapViewState::onCrimeEvent}},
184185
mTechnologyReader{"tech0-1.xml"},
185186
mLoadingExisting{true},
186187
mExistingToLoad{savegame},
187-
mMainReportsState{mainReportsState},
188+
mMainReportsState{gameState.mainReportsState()},
188189
mStructures{"ui/structures.png", constants::StructureIconSize, constants::MarginTight},
189190
mRobots{"ui/robots.png", constants::RobotIconSize, constants::MarginTight},
190191
mConnections{"ui/structures.png", constants::StructureIconSize, constants::MarginTight},
192+
mFileIoDialog{gameState.fileIoDialog()},
191193
mPopulationPanel{mPopulation, mPopulationPool, mMorale},
192194
mResourceInfoBar{mResourcesCount, mPopulation, mMorale, mFood},
193195
mRobotDeploymentSummary{mRobotPool}
@@ -197,18 +199,19 @@ MapViewState::MapViewState(MainReportsUiState& mainReportsState, const std::stri
197199
}
198200

199201

200-
MapViewState::MapViewState(MainReportsUiState& mainReportsState, const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty) :
202+
MapViewState::MapViewState(GameState& gameState, const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty) :
201203
mDifficulty{selectedDifficulty},
202204
mTileMap{std::make_unique<TileMap>(planetAttributes.mapImagePath, planetAttributes.maxDepth, planetAttributes.maxMines, HostilityMineYields.at(planetAttributes.hostility))},
203205
mCrimeRateUpdate{mDifficulty},
204206
mCrimeExecution{mDifficulty, {this, &MapViewState::onCrimeEvent}},
205207
mTechnologyReader{"tech0-1.xml"},
206208
mPlanetAttributes{planetAttributes},
207-
mMainReportsState{mainReportsState},
209+
mMainReportsState{gameState.mainReportsState()},
208210
mMapView{std::make_unique<MapView>(*mTileMap)},
209211
mStructures{"ui/structures.png", constants::StructureIconSize, constants::MarginTight},
210212
mRobots{"ui/robots.png", constants::RobotIconSize, constants::MarginTight},
211213
mConnections{"ui/structures.png", constants::StructureIconSize, constants::MarginTight},
214+
mFileIoDialog{gameState.fileIoDialog()},
212215
mPopulationPanel{mPopulation, mPopulationPool, mMorale},
213216
mPoliceOverlays{static_cast<std::vector<Tile*>::size_type>(mTileMap->maxDepth() + 1)},
214217
mResourceInfoBar{mResourcesCount, mPopulation, mMorale, mFood},
@@ -241,6 +244,7 @@ MapViewState::~MapViewState()
241244
eventHandler.windowResized().disconnect({this, &MapViewState::onWindowResized});
242245

243246
NAS2D::Utility<std::map<class MineFacility*, Route>>::get().clear();
247+
mFileIoDialog.fileSaveSignal().disconnect({this, &MapViewState::onSaveGame});
244248
}
245249

246250

appOPHD/States/MapViewState.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "../UI/NotificationWindow.h"
2020
#include "../UI/DiggerDirection.h"
2121
#include "../UI/FactoryProduction.h"
22-
#include "../UI/FileIo.h"
2322
#include "../UI/GameOverDialog.h"
2423
#include "../UI/GameOptionsDialog.h"
2524
#include "../UI/IconGrid.h"
@@ -79,6 +78,8 @@ class MapView;
7978
class DetailMap;
8079
class NavControl;
8180
class MainReportsUiState;
81+
class GameState;
82+
class FileIo;
8283

8384

8485
enum class InsertMode
@@ -105,19 +106,17 @@ class MapViewState : public Wrapper
105106
using QuitSignal = NAS2D::Signal<>;
106107
using ReportsUiSignal = NAS2D::Signal<>;
107108
using MapChangedSignal = NAS2D::Signal<>;
108-
using FileLoadSignal = NAS2D::Signal<const std::string&>;
109109

110110
public:
111-
MapViewState(MainReportsUiState&, const std::string& savegame);
112-
MapViewState(MainReportsUiState&, const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty);
111+
MapViewState(GameState& gameState, const std::string& savegame);
112+
MapViewState(GameState& gameState, const Planet::Attributes& planetAttributes, Difficulty selectedDifficulty);
113113
~MapViewState() override;
114114

115115
void setPopulationLevel(PopulationLevel popLevel);
116116

117117
ReportsUiSignal::Source& showReportsUi() { return mReportsUiSignal; }
118118
QuitSignal::Source& quit() { return mQuitSignal; }
119119
MapChangedSignal::Source& mapChanged() { return mMapChangedSignal; }
120-
FileLoadSignal::Source& fileLoadSignal() { return mFileIoDialog.fileLoadSignal(); }
121120

122121
void focusOnStructure(const Structure* s);
123122

@@ -368,7 +367,7 @@ class MapViewState : public Wrapper
368367
CheatMenu mCheatMenu;
369368
DiggerDirection mDiggerDirection;
370369
FactoryProduction mFactoryProduction;
371-
FileIo mFileIoDialog;
370+
FileIo& mFileIoDialog;
372371
GameOverDialog mGameOverDialog;
373372
GameOptionsDialog mGameOptionsDialog;
374373
MajorEventAnnouncement mAnnouncement;

0 commit comments

Comments
 (0)