From 59a2147f65e17d406b632c3ec9e8dc66191944d0 Mon Sep 17 00:00:00 2001 From: Alexander Hunt Date: Thu, 8 Feb 2024 09:52:33 -0500 Subject: [PATCH 1/4] Changed PopulationPool methods to const versions. --- OPHD/UI/PopulationPanel.cpp | 7 ++++++- OPHD/UI/PopulationPanel.h | 2 ++ libOPHD/Population/PopulationPool.cpp | 10 +++++----- libOPHD/Population/PopulationPool.h | 8 ++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/OPHD/UI/PopulationPanel.cpp b/OPHD/UI/PopulationPanel.cpp index dfbb49807..9301459bd 100644 --- a/OPHD/UI/PopulationPanel.cpp +++ b/OPHD/UI/PopulationPanel.cpp @@ -66,7 +66,12 @@ PopulationPanel::PopulationPanel() : imageCache.load("ui/skin/window_bottom_right.png") } { - constexpr int linesOfText = 16; + PopulationPanel::init(); +} + +void PopulationPanel::init() +{ + constexpr int linesOfText = 14; constexpr int edgeBuffer = constants::Margin * 2; const int windowHeight = mFontBold.height() + (mFont.height() * linesOfText) + (edgeBuffer * 2 /* Times two to account for both the edge and the divider line. */); diff --git a/OPHD/UI/PopulationPanel.h b/OPHD/UI/PopulationPanel.h index 34acf0a8f..b80186d0c 100644 --- a/OPHD/UI/PopulationPanel.h +++ b/OPHD/UI/PopulationPanel.h @@ -58,4 +58,6 @@ class PopulationPanel : public Control int mResidentialCapacity{0}; int mCrimeRate{0}; int mPopulationPanelWidth{0}; + + void init(); }; diff --git a/libOPHD/Population/PopulationPool.cpp b/libOPHD/Population/PopulationPool.cpp index b51fe425a..b22f86f7c 100644 --- a/libOPHD/Population/PopulationPool.cpp +++ b/libOPHD/Population/PopulationPool.cpp @@ -9,7 +9,7 @@ /** * Sets a pointer to a Population object. - * + * * \note PopulationPool expects a valid object and does no checking * for invalid states. */ @@ -19,13 +19,13 @@ void PopulationPool::population(Population* pop) } -int PopulationPool::availableWorkers() +const int PopulationPool::availableWorkers() const { return mPopulation->getPopulations().worker - workersEmployed(); } -int PopulationPool::availableScientists() +const int PopulationPool::availableScientists() const { return mPopulation->getPopulations().scientist - scientistsEmployed(); } @@ -76,7 +76,7 @@ int PopulationPool::scientistsAsWorkers() /** * Amount of Scientists currently employed. */ -int PopulationPool::scientistsEmployed() +const int PopulationPool::scientistsEmployed() const { return mScientistsUsed; } @@ -85,7 +85,7 @@ int PopulationPool::scientistsEmployed() /** * Amount of Workers currently employed. */ -int PopulationPool::workersEmployed() +const int PopulationPool::workersEmployed() const { return mWorkersUsed; } diff --git a/libOPHD/Population/PopulationPool.h b/libOPHD/Population/PopulationPool.h index ed2ec80a0..547e5e354 100644 --- a/libOPHD/Population/PopulationPool.h +++ b/libOPHD/Population/PopulationPool.h @@ -15,16 +15,16 @@ class PopulationPool public: void population(Population* pop); - int availableWorkers(); - int availableScientists(); + const int availableWorkers() const; + const int availableScientists() const; bool usePopulation(PopulationRequirements populationRequirements); void clear(); int scientistsAsWorkers(); - int scientistsEmployed(); - int workersEmployed(); + const int scientistsEmployed() const; + const int workersEmployed() const; int populationEmployed(); int size() const; From 9174e7d470aa97b315081fb87a1aaf27f37e3fcd Mon Sep 17 00:00:00 2001 From: Alexander Hunt Date: Thu, 8 Feb 2024 10:00:23 -0500 Subject: [PATCH 2/4] Added new constructor to PopulationPanel to initialize mPopulation and mPopulationPool --- OPHD/UI/PopulationPanel.cpp | 19 +++++++++++++++++++ OPHD/UI/PopulationPanel.h | 5 +++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/OPHD/UI/PopulationPanel.cpp b/OPHD/UI/PopulationPanel.cpp index 9301459bd..cf2c41e88 100644 --- a/OPHD/UI/PopulationPanel.cpp +++ b/OPHD/UI/PopulationPanel.cpp @@ -48,6 +48,25 @@ static const std::array moraleStringColor Color{0, 185, 0} }; +PopulationPanel::PopulationPanel(const Population& pop, const PopulationPool& popPool) : + mFont{fontCache.load(constants::FONT_PRIMARY, constants::FontPrimaryNormal)}, + mFontBold{fontCache.load(constants::FONT_PRIMARY_BOLD, constants::FontPrimaryNormal)}, + mIcons{imageCache.load("ui/icons.png")}, + mSkin{ + imageCache.load("ui/skin/window_top_left.png"), + imageCache.load("ui/skin/window_top_middle.png"), + imageCache.load("ui/skin/window_top_right.png"), + imageCache.load("ui/skin/window_middle_left.png"), + imageCache.load("ui/skin/window_middle_middle.png"), + imageCache.load("ui/skin/window_middle_right.png"), + imageCache.load("ui/skin/window_bottom_left.png"), + imageCache.load("ui/skin/window_bottom_middle.png"), + imageCache.load("ui/skin/window_bottom_right.png")} +{ + mPopulation = &pop; + mPopulationPool = &popPool; + PopulationPanel::init(); +} PopulationPanel::PopulationPanel() : mFont{fontCache.load(constants::FONT_PRIMARY, constants::FontPrimaryNormal)}, diff --git a/OPHD/UI/PopulationPanel.h b/OPHD/UI/PopulationPanel.h index b80186d0c..0ca06c4f2 100644 --- a/OPHD/UI/PopulationPanel.h +++ b/OPHD/UI/PopulationPanel.h @@ -22,6 +22,7 @@ class PopulationPanel : public Control { public: PopulationPanel(); + PopulationPanel(const Population& pop, const PopulationPool& popPool); void population(Population* pop); void populationPool(PopulationPool* popPool); @@ -50,8 +51,8 @@ class PopulationPanel : public Control std::vector> mMoraleChangeReasons; - Population* mPopulation = nullptr; - PopulationPool* mPopulationPool = nullptr; + const Population* mPopulation; + const PopulationPool* mPopulationPool; int mMorale{0}; int mPreviousMorale{0}; From be83ef80d6c62908f85ff0966c039c6e02ac3afa Mon Sep 17 00:00:00 2001 From: Alexander Hunt Date: Thu, 8 Feb 2024 10:01:43 -0500 Subject: [PATCH 3/4] Removed method population from PopulationPanel --- OPHD/UI/PopulationPanel.cpp | 10 ---------- OPHD/UI/PopulationPanel.h | 3 --- 2 files changed, 13 deletions(-) diff --git a/OPHD/UI/PopulationPanel.cpp b/OPHD/UI/PopulationPanel.cpp index cf2c41e88..cbb2545cb 100644 --- a/OPHD/UI/PopulationPanel.cpp +++ b/OPHD/UI/PopulationPanel.cpp @@ -113,16 +113,6 @@ void PopulationPanel::init() size({windowWidth, windowHeight}); } -void PopulationPanel::population(Population* pop) -{ - mPopulation = pop; -} - -void PopulationPanel::populationPool(PopulationPool* popPool) -{ - mPopulationPool = popPool; -} - void PopulationPanel::addMoraleReason(const std::string& str, int val) { if (val == 0) { return; } diff --git a/OPHD/UI/PopulationPanel.h b/OPHD/UI/PopulationPanel.h index 0ca06c4f2..9e33108f0 100644 --- a/OPHD/UI/PopulationPanel.h +++ b/OPHD/UI/PopulationPanel.h @@ -24,9 +24,6 @@ class PopulationPanel : public Control PopulationPanel(); PopulationPanel(const Population& pop, const PopulationPool& popPool); - void population(Population* pop); - void populationPool(PopulationPool* popPool); - void morale(int val) { mMorale = val; } void old_morale(int val) { mPreviousMorale = val; } From 53b9b646cc1cd57d3659ba314ea26fbba5ac3b08 Mon Sep 17 00:00:00 2001 From: Alexander Hunt Date: Thu, 8 Feb 2024 10:03:50 -0500 Subject: [PATCH 4/4] Updated MapViewState to use the new constructor and removed Population initialization from MapViewStateUI --- OPHD/States/MapViewState.cpp | 1 + OPHD/States/MapViewStateUi.cpp | 2 -- OPHD/UI/PopulationPanel.cpp | 30 +++------------------------ OPHD/UI/PopulationPanel.h | 1 - libOPHD/Population/PopulationPool.cpp | 12 +++++------ libOPHD/Population/PopulationPool.h | 12 +++++------ 6 files changed, 16 insertions(+), 42 deletions(-) diff --git a/OPHD/States/MapViewState.cpp b/OPHD/States/MapViewState.cpp index 4e1b6d348..60924b129 100644 --- a/OPHD/States/MapViewState.cpp +++ b/OPHD/States/MapViewState.cpp @@ -187,6 +187,7 @@ MapViewState::MapViewState(MainReportsUiState& mainReportsState, const Planet::A mStructures{"ui/structures.png", 46, constants::MarginTight}, mRobots{"ui/robots.png", 46, constants::MarginTight}, mConnections{"ui/structures.png", 46, constants::MarginTight}, + mPopulationPanel{mPopulation, mPopulationPool}, mResourceInfoBar{mResourcesCount, mPopulation, mCurrentMorale, mPreviousMorale, mFood}, mRobotDeploymentSummary{mRobotPool}, mMiniMap{std::make_unique(*mMapView, mTileMap, mRobotList, planetAttributes.mapImagePath)}, diff --git a/OPHD/States/MapViewStateUi.cpp b/OPHD/States/MapViewStateUi.cpp index d91ad0cd0..08a4dff95 100644 --- a/OPHD/States/MapViewStateUi.cpp +++ b/OPHD/States/MapViewStateUi.cpp @@ -75,8 +75,6 @@ void MapViewState::initUi() mFileIoDialog.hide(); mPopulationPanel.position({675, constants::ResourceIconSize + 4 + constants::MarginTight}); - mPopulationPanel.population(&mPopulation); - mPopulationPanel.populationPool(&mPopulationPool); mResourceBreakdownPanel.position({0, 22}); mResourceBreakdownPanel.playerResources(&mResourcesCount); diff --git a/OPHD/UI/PopulationPanel.cpp b/OPHD/UI/PopulationPanel.cpp index cbb2545cb..a71d18bba 100644 --- a/OPHD/UI/PopulationPanel.cpp +++ b/OPHD/UI/PopulationPanel.cpp @@ -48,6 +48,8 @@ static const std::array moraleStringColor Color{0, 185, 0} }; +PopulationPanel::PopulationPanel() : PopulationPanel::PopulationPanel(Population(), PopulationPool()){}; + PopulationPanel::PopulationPanel(const Population& pop, const PopulationPool& popPool) : mFont{fontCache.load(constants::FONT_PRIMARY, constants::FontPrimaryNormal)}, mFontBold{fontCache.load(constants::FONT_PRIMARY_BOLD, constants::FontPrimaryNormal)}, @@ -65,32 +67,7 @@ PopulationPanel::PopulationPanel(const Population& pop, const PopulationPool& po { mPopulation = &pop; mPopulationPool = &popPool; - PopulationPanel::init(); -} - -PopulationPanel::PopulationPanel() : - mFont{fontCache.load(constants::FONT_PRIMARY, constants::FontPrimaryNormal)}, - mFontBold{fontCache.load(constants::FONT_PRIMARY_BOLD, constants::FontPrimaryNormal)}, - mIcons{imageCache.load("ui/icons.png")}, - mSkin - { - imageCache.load("ui/skin/window_top_left.png"), - imageCache.load("ui/skin/window_top_middle.png"), - imageCache.load("ui/skin/window_top_right.png"), - imageCache.load("ui/skin/window_middle_left.png"), - imageCache.load("ui/skin/window_middle_middle.png"), - imageCache.load("ui/skin/window_middle_right.png"), - imageCache.load("ui/skin/window_bottom_left.png"), - imageCache.load("ui/skin/window_bottom_middle.png"), - imageCache.load("ui/skin/window_bottom_right.png") - } -{ - PopulationPanel::init(); -} - -void PopulationPanel::init() -{ - constexpr int linesOfText = 14; + constexpr int linesOfText = 16; constexpr int edgeBuffer = constants::Margin * 2; const int windowHeight = mFontBold.height() + (mFont.height() * linesOfText) + (edgeBuffer * 2 /* Times two to account for both the edge and the divider line. */); @@ -119,7 +96,6 @@ void PopulationPanel::addMoraleReason(const std::string& str, int val) mMoraleChangeReasons.push_back(std::make_pair(str, val)); } - void PopulationPanel::update() { auto& renderer = Utility::get(); diff --git a/OPHD/UI/PopulationPanel.h b/OPHD/UI/PopulationPanel.h index 9e33108f0..656e14e08 100644 --- a/OPHD/UI/PopulationPanel.h +++ b/OPHD/UI/PopulationPanel.h @@ -57,5 +57,4 @@ class PopulationPanel : public Control int mCrimeRate{0}; int mPopulationPanelWidth{0}; - void init(); }; diff --git a/libOPHD/Population/PopulationPool.cpp b/libOPHD/Population/PopulationPool.cpp index b22f86f7c..cc91e0557 100644 --- a/libOPHD/Population/PopulationPool.cpp +++ b/libOPHD/Population/PopulationPool.cpp @@ -19,13 +19,13 @@ void PopulationPool::population(Population* pop) } -const int PopulationPool::availableWorkers() const +int PopulationPool::availableWorkers() const { return mPopulation->getPopulations().worker - workersEmployed(); } -const int PopulationPool::availableScientists() const +int PopulationPool::availableScientists() const { return mPopulation->getPopulations().scientist - scientistsEmployed(); } @@ -67,7 +67,7 @@ void PopulationPool::clear() /** * Amount of Scientists employed as Workers. */ -int PopulationPool::scientistsAsWorkers() +int PopulationPool::scientistsAsWorkers() const { return mScientistsAsWorkers; } @@ -76,7 +76,7 @@ int PopulationPool::scientistsAsWorkers() /** * Amount of Scientists currently employed. */ -const int PopulationPool::scientistsEmployed() const +int PopulationPool::scientistsEmployed() const { return mScientistsUsed; } @@ -85,7 +85,7 @@ const int PopulationPool::scientistsEmployed() const /** * Amount of Workers currently employed. */ -const int PopulationPool::workersEmployed() const +int PopulationPool::workersEmployed() const { return mWorkersUsed; } @@ -94,7 +94,7 @@ const int PopulationPool::workersEmployed() const /** * Amount of population currently employed. */ -int PopulationPool::populationEmployed() +int PopulationPool::populationEmployed() const { return scientistsEmployed() + scientistsAsWorkers() + workersEmployed(); } diff --git a/libOPHD/Population/PopulationPool.h b/libOPHD/Population/PopulationPool.h index 547e5e354..33f7b09aa 100644 --- a/libOPHD/Population/PopulationPool.h +++ b/libOPHD/Population/PopulationPool.h @@ -15,17 +15,17 @@ class PopulationPool public: void population(Population* pop); - const int availableWorkers() const; - const int availableScientists() const; + int availableWorkers() const; + int availableScientists() const; bool usePopulation(PopulationRequirements populationRequirements); void clear(); - int scientistsAsWorkers(); - const int scientistsEmployed() const; - const int workersEmployed() const; - int populationEmployed(); + int scientistsAsWorkers() const; + int scientistsEmployed() const; + int workersEmployed() const; + int populationEmployed() const; int size() const;