Skip to content

Commit

Permalink
Ability to hide items in Purchase GUI (by jgatkinsn)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeridianOXC committed May 8, 2018
1 parent f4536d3 commit 9c53c21
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
116 changes: 115 additions & 1 deletion src/Basescape/PurchaseState.cpp
Expand Up @@ -136,6 +136,7 @@ PurchaseState::PurchaseState(Base *base) : _base(base), _sel(0), _total(0), _pQt
_lstItems->onMousePress((ActionHandler)&PurchaseState::lstItemsMousePress);

_cats.push_back("STR_ALL_ITEMS");
_cats.push_back("STR_FILTER_HIDDEN");

const std::vector<std::string> &cw = _game->getMod()->getCraftWeaponsList();
for (std::vector<std::string>::const_iterator i = cw.begin(); i != cw.end(); ++i)
Expand Down Expand Up @@ -236,6 +237,7 @@ PurchaseState::PurchaseState(Base *base) : _base(base), _sel(0), _total(0), _pQt
// then use them nicely in order
_cats.clear();
_cats.push_back("STR_ALL_ITEMS");
_cats.push_back("STR_FILTER_HIDDEN");
const std::vector<std::string> &categories = _game->getMod()->getItemCategoriesList();
for (std::vector<std::string>::const_iterator k = categories.begin(); k != categories.end(); ++k)
{
Expand Down Expand Up @@ -344,6 +346,61 @@ bool PurchaseState::belongsToCategory(int sel, const std::string &cat) const
return false;
}

/**
* Determines if a row item is supposed to be hidden
* @param sel Selected row.
* @param cat Category.
* @returns True if row item is hidden
*/
bool PurchaseState::isHidden(int sel) const
{
std::string itemName;
bool isCraft = false;

switch (_items[sel].type)
{
case TRANSFER_SOLDIER:
case TRANSFER_SCIENTIST:
case TRANSFER_ENGINEER:
return false;
case TRANSFER_CRAFT:
isCraft = true; // fall-through
case TRANSFER_ITEM:
if (isCraft)
{
RuleCraft *rule = (RuleCraft*)_items[sel].rule;
if (rule != 0)
{
itemName = rule->getType();
}
}
else
{
RuleItem *rule = (RuleItem*)_items[sel].rule;
if (rule != 0)
{
itemName = rule->getType();
}
}
if (!itemName.empty())
{
std::map<std::string, bool> hiddenMap = _game->getSavedGame()->getHiddenPurchaseItems();
std::map<std::string, bool>::const_iterator iter = hiddenMap.find(itemName);
if (iter != hiddenMap.end())
{
return iter->second;
}
else
{
// not found = not hidden
return false;
}
}
}

return false;
}

/**
* Quick search toggle.
* @param action Pointer to an action.
Expand Down Expand Up @@ -387,7 +444,19 @@ void PurchaseState::updateList()
{
// filter
std::string cat = _cats[_cbxCategory->getSelected()];
if (_game->getMod()->getUseCustomCategories())
bool hidden = isHidden(i);
if (cat == "STR_FILTER_HIDDEN")
{
if (!hidden)
{
continue;
}
}
else if (hidden)
{
continue;
}
else if (_game->getMod()->getUseCustomCategories())
{
if (cat != "STR_ALL_ITEMS" && !belongsToCategory(i, cat))
{
Expand Down Expand Up @@ -631,6 +700,51 @@ void PurchaseState::lstItemsMousePress(Action *action)
}
}
}
else if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
{
if (action->getAbsoluteXMouse() >= _lstItems->getArrowsLeftEdge() &&
action->getAbsoluteXMouse() <= _lstItems->getArrowsRightEdge())
{
return;
}
std::string itemName;
if (getRow().type == TRANSFER_ITEM)
{
RuleItem *rule = (RuleItem*)getRow().rule;
if (rule != 0)
{
itemName = rule->getType();
}
}
else if (getRow().type == TRANSFER_CRAFT)
{
RuleCraft *rule = (RuleCraft*)getRow().rule;
if (rule != 0)
{
itemName = rule->getType();
}
}
if (!itemName.empty())
{
std::map<std::string, bool> hiddenMap = _game->getSavedGame()->getHiddenPurchaseItems();
std::map<std::string, bool>::iterator iter = hiddenMap.find(itemName);
if (iter != hiddenMap.end())
{
// found => flip it
_game->getSavedGame()->setHiddenPurchaseItemsStatus(itemName, !iter->second);
}
else
{
// not found = not hidden yet => hide it
_game->getSavedGame()->setHiddenPurchaseItemsStatus(itemName, true);
}

// update screen
size_t scrollPos = _lstItems->getScroll();
updateList();
_lstItems->scrollTo(scrollPos);
}
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Basescape/PurchaseState.h
Expand Up @@ -63,6 +63,8 @@ class PurchaseState : public State
std::string getCategory(int sel) const;
/// Determines if the current selection belongs to a given category.
bool belongsToCategory(int sel, const std::string &cat) const;
/// Checks for hidden items
bool isHidden(int sel) const;
/// Gets the row of the current selection.
TransferRow &getRow() { return _items[_rows[_sel]]; }
public:
Expand Down
21 changes: 21 additions & 0 deletions src/Savegame/SavedGame.cpp
Expand Up @@ -592,6 +592,7 @@ void SavedGame::load(const std::string &filename, Mod *mod)
_ufopediaRuleStatus = doc["ufopediaRuleStatus"].as< std::map<std::string, int> >(_ufopediaRuleStatus);
_manufactureRuleStatus = doc["manufactureRuleStatus"].as< std::map<std::string, int> >(_manufactureRuleStatus);
_researchRuleStatus = doc["researchRuleStatus"].as< std::map<std::string, int> >(_researchRuleStatus);
_hiddenPurchaseItemsMap = doc["hiddenPurchaseItems"].as< std::map<std::string, bool> >(_hiddenPurchaseItemsMap);

for (YAML::const_iterator i = doc["bases"].begin(); i != doc["bases"].end(); ++i)
{
Expand Down Expand Up @@ -878,6 +879,7 @@ void SavedGame::save(const std::string &filename) const
node["ufopediaRuleStatus"] = _ufopediaRuleStatus;
node["manufactureRuleStatus"] = _manufactureRuleStatus;
node["researchRuleStatus"] = _researchRuleStatus;
node["hiddenPurchaseItems"] = _hiddenPurchaseItemsMap;
node["alienStrategy"] = _alienStrategy->save();
for (std::vector<Soldier*>::const_iterator i = _deadSoldiers.begin(); i != _deadSoldiers.end(); ++i)
{
Expand Down Expand Up @@ -1368,6 +1370,25 @@ void SavedGame::setResearchRuleStatus(const std::string &researchRule, int newSt
_researchRuleStatus[researchRule] = newStatus;
}

/**
* Sets the hidden status of a purchase item
* @param purchase item name
* @param hidden
*/
void SavedGame::setHiddenPurchaseItemsStatus(const std::string &itemName, bool hidden)
{
_hiddenPurchaseItemsMap[itemName] = hidden;
}

/**
* Get the map of hidden items
* @return map
*/
const std::map<std::string, bool> &SavedGame::getHiddenPurchaseItems()
{
return _hiddenPurchaseItemsMap;
}

/*
* Checks for and removes a research project from the "already discovered" list
* @param research is the project we are checking for and removing, if necessary.
Expand Down
5 changes: 5 additions & 0 deletions src/Savegame/SavedGame.h
Expand Up @@ -133,6 +133,7 @@ class SavedGame
std::map<std::string, int> _ufopediaRuleStatus;
std::map<std::string, int> _manufactureRuleStatus;
std::map<std::string, int> _researchRuleStatus;
std::map<std::string, bool> _hiddenPurchaseItemsMap;
std::vector<AlienMission*> _activeMissions;
bool _debug, _warned;
int _monthsPassed;
Expand Down Expand Up @@ -244,6 +245,8 @@ class SavedGame
void setManufactureRuleStatus(const std::string &manufactureRule, int newStatus);
/// Sets the status of a research rule
void setResearchRuleStatus(const std::string &researchRule, int newStatus);
/// Sets the item as hidden or unhidden
void setHiddenPurchaseItemsStatus(const std::string &itemName, bool hidden);
/// Remove a research from the "already discovered" list
void removeDiscoveredResearch(const RuleResearch *research);
/// Add a finished ResearchProject
Expand All @@ -268,6 +271,8 @@ class SavedGame
void getDependableFacilities(std::vector<RuleBaseFacility*> & dependables, const RuleResearch *research, const Mod *mod) const;
/// Gets the status of a ufopedia rule.
int getUfopediaRuleStatus(const std::string &ufopediaRule);
/// Gets the list of hidden items
const std::map<std::string, bool> &getHiddenPurchaseItems();
/// Gets the status of a manufacture rule.
int getManufactureRuleStatus(const std::string &manufactureRule);
/// Is the research new?
Expand Down

0 comments on commit 9c53c21

Please sign in to comment.