Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StructureInspector #1619

Merged
merged 14 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appOPHD/MapObjects/Structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Structure : public MapObject
const StorableResources& production() const { return mProductionPool; }

const PopulationRequirements& populationRequirements() const;
const PopulationRequirements& populationAvailable() const { return mPopulationAvailable; }
PopulationRequirements& populationAvailable() { return mPopulationAvailable; }

// ATTRIBUTES
Expand Down
189 changes: 98 additions & 91 deletions appOPHD/UI/StructureInspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,86 @@ namespace
{
return idleReadonTable.at(idleReason);
}


std::string getDisabledReason(const Structure& structure)
{
if (structure.disabled())
{
return disabledReasonToString(structure.disabledReason());
}
else if (structure.isIdle())
{
return idleReasonToString(structure.idleReason());
}

return "";
}


std::string formatAge(const Structure& structure)
{
return structure.ages() ? std::to_string(structure.age()) + " of " + std::to_string(structure.maxAge()) : "N/A";
}


StringTable buildGenericStructureAttributesStringTable(const Structure& structure)
{
StringTable stringTable{4, 6};

stringTable[{0, 0}].text = "Type:";
stringTable[{1, 0}].text = structure.classDescription();

if (structure.underConstruction())
{
stringTable[{2, 0}].text = "Turns Remaining:";
stringTable[{3, 0}].text = std::to_string(structure.turnsToBuild() - structure.age());
}
else
{
stringTable[{2, 0}].text = "Age:";
stringTable[{3, 0}].text = formatAge(structure);
}

stringTable[{0, 1}].text = "Power Required:";
stringTable[{1, 1}].text = std::to_string(structure.energyRequirement());

stringTable[{2, 1}].text = "State:";
stringTable[{3, 1}].text = structure.stateDescription(structure.state());

stringTable[{3, 2}].text = getDisabledReason(structure);

if (!structure.underConstruction() && !structure.destroyed())
{
stringTable[{0, 2}].text = "Integrity:";
stringTable[{1, 2}].text = std::to_string(structure.integrity());
}

const auto& populationAvailable = structure.populationAvailable();
const auto& populationRequirements = structure.populationRequirements();

if (populationRequirements.workers > 0)
{
stringTable[{0, 3}].text = "Workers:";
stringTable[{1, 3}].text = std::to_string(populationAvailable.workers) + " / " + std::to_string(populationRequirements.workers);
stringTable[{1, 3}].textColor = populationAvailable.workers >= populationRequirements.workers ? Color::White : Color::Red;
}

if (populationRequirements.scientists > 0)
{
stringTable[{0, 4}].text = "Scientists:";
stringTable[{1, 4}].text = std::to_string(populationAvailable.scientists) + " / " + std::to_string(populationRequirements.scientists);
stringTable[{1, 4}].textColor = populationAvailable.scientists >= populationRequirements.scientists ? Color::White : Color::Red;
}

if (structure.hasCrime())
{
stringTable[{0, 5}].text = "Crime Rate:";
stringTable[{1, 5}].text = std::to_string(structure.crimeRate()) + "%";
}

return stringTable;
}
}


Expand All @@ -75,7 +155,9 @@ void StructureInspector::structure(Structure* structure)

if (!mStructure) { return; }

auto stringTable = buildStringTable();
title(mStructure->name());

auto stringTable = buildGenericStringTable();

auto windowWidth = stringTable.screenRect().size.x + 10;
size({windowWidth < 350 ? 350 : windowWidth, rect().size.y});
Expand All @@ -90,66 +172,22 @@ void StructureInspector::onClose()
}


StringTable StructureInspector::buildStringTable() const
StringTable StructureInspector::buildGenericStringTable() const
{
StringTable stringTable(4, 6);
auto stringTable = buildGenericStructureAttributesStringTable(*mStructure);
stringTable.position(mRect.position + NAS2D::Vector{5, 25});
stringTable.setVerticalPadding(5);
stringTable.setColumnFont(2, stringTable.GetDefaultTitleFont());
stringTable.computeRelativeCellPositions();
return stringTable;
}

stringTable[{0, 0}].text = "Type:";
stringTable[{1, 0}].text = mStructure->classDescription();

if (mStructure->underConstruction())
{
stringTable[{2, 0}].text = "Turns Remaining:";
stringTable[{3, 0}].text = std::to_string(mStructure->turnsToBuild() - mStructure->age());
}
else
{
stringTable[{2, 0}].text = "Age:";
stringTable[{3, 0}].text = formatAge();
}

stringTable[{0, 1}].text = "Power Required:";
stringTable[{1, 1}].text = std::to_string(mStructure->energyRequirement());

stringTable[{2, 1}].text = "State:";
stringTable[{3, 1}].text = mStructure->stateDescription(mStructure->state());

stringTable[{3, 2}].text = getDisabledReason();

if (!mStructure->underConstruction() && !mStructure->destroyed())
{
stringTable[{0, 2}].text = "Integrity:";
stringTable[{1, 2}].text = std::to_string(mStructure->integrity());
}

const auto& populationAvailable = mStructure->populationAvailable();
const auto& populationRequirements = mStructure->populationRequirements();

if (populationRequirements.workers > 0)
{
stringTable[{0, 3}].text = "Workers:";
stringTable[{1, 3}].text = std::to_string(populationAvailable.workers) + " / " + std::to_string(populationRequirements.workers);
stringTable[{1, 3}].textColor = populationAvailable.workers >= populationRequirements.workers ? Color::White : Color::Red;
}

if (populationRequirements.scientists > 0)
{
stringTable[{0, 4}].text = "Scientists:";
stringTable[{1, 4}].text = std::to_string(populationAvailable.scientists) + " / " + std::to_string(populationRequirements.scientists);
stringTable[{1, 4}].textColor = populationAvailable.scientists >= populationRequirements.scientists ? Color::White : Color::Red;
}

if (mStructure->hasCrime())
{
stringTable[{0, 5}].text = "Crime Rate:";
stringTable[{1, 5}].text = std::to_string(mStructure->crimeRate()) + "%";
}

StringTable StructureInspector::buildSpecificStringTable(NAS2D::Point<int> position) const
{
auto stringTable = mStructure->createInspectorViewTable();
stringTable.computeRelativeCellPositions();

stringTable.position(position);
return stringTable;
}

Expand All @@ -159,47 +197,16 @@ void StructureInspector::update()
if (!visible()) { return; }
Window::update();

auto& renderer = Utility<Renderer>::get();

if (mStructure == nullptr)
{
throw std::runtime_error("Null pointer to structure within StructureInspector");
}
title(mStructure->name());

auto stringTable = buildStringTable();
stringTable.draw(renderer);
const auto genericStructureAttributes = buildGenericStringTable();
const auto specificAttributeTablePosition = genericStructureAttributes.screenRect().crossYPoint() + NAS2D::Vector{0, 25};
const auto specificStructureAttributes = buildSpecificStringTable(specificAttributeTablePosition);

drawStructureSpecificTable({stringTable.position().x, stringTable.screenRect().endPoint().y + 25}, renderer);
}

void StructureInspector::drawStructureSpecificTable(NAS2D::Point<int> position, NAS2D::Renderer& renderer)
{
StringTable stringTable = mStructure->createInspectorViewTable();
stringTable.computeRelativeCellPositions();
stringTable.position(position);
stringTable.draw(renderer);
}

std::string StructureInspector::getDisabledReason() const
{
if (mStructure->disabled())
{
return disabledReasonToString(mStructure->disabledReason());
}
else if (mStructure->isIdle())
{
return idleReasonToString(mStructure->idleReason());
}

return "";
}

std::string StructureInspector::formatAge() const
{
if (mStructure->ages()) {
return std::to_string(mStructure->age()) + " of " + std::to_string(mStructure->maxAge());
}

return "N/A";
auto& renderer = Utility<Renderer>::get();
genericStructureAttributes.draw(renderer);
specificStructureAttributes.draw(renderer);
}
9 changes: 4 additions & 5 deletions appOPHD/UI/StructureInspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ class StructureInspector : public Window

void update() override;

private:
protected:
void onClose();
std::string getDisabledReason() const;
void drawStructureSpecificTable(NAS2D::Point<int> position, NAS2D::Renderer& renderer);
std::string formatAge() const;

StringTable buildStringTable() const;
StringTable buildGenericStringTable() const;
StringTable buildSpecificStringTable(NAS2D::Point<int> position) const;

private:
Button btnClose;
const NAS2D::Image& mIcons;
Structure* mStructure = nullptr;
Expand Down