-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UI): Allow shop items to be custom sorted within categories by c…
…ontent creators, instead of always being alphabetical (#7054) Co-authored-by: Amazinite <[email protected]>
- Loading branch information
Showing
29 changed files
with
530 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* CategoryList.cpp | ||
Copyright (c) 2022 by Amazinite | ||
Endless Sky 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. | ||
Endless Sky 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, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "CategoryList.h" | ||
|
||
#include "DataNode.h" | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
using namespace std; | ||
|
||
|
||
|
||
const bool CategoryList::Category::SortHelper(const CategoryList::Category &a, const CategoryList::Category &b) | ||
{ | ||
if(a.precedence == b.precedence) | ||
return a.name < b.name; | ||
return a.precedence < b.precedence; | ||
} | ||
|
||
|
||
|
||
void CategoryList::Load(const DataNode &node) | ||
{ | ||
for(const DataNode &child : node) | ||
{ | ||
// Use the given precedence. If no precedence is given, use the previous | ||
// precedence + 1. | ||
if(child.Size() > 1) | ||
currentPrecedence = child.Value(1); | ||
Category cat(child.Token(0), currentPrecedence++); | ||
|
||
// If a given category name already exists, its prescedence will be updated. | ||
auto it = find_if(list.begin(), list.end(), | ||
[&cat](const Category &c) noexcept -> bool { return cat.name == c.name; }); | ||
if(it != list.end()) | ||
it->precedence = cat.precedence; | ||
else | ||
{ | ||
list.push_back(cat); | ||
byName.insert(pair<const string, Category>(cat.name, cat)); | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
// Sort the CategoryList. Categories are sorted by precedence. If multiple categories | ||
// share the same precedence then they are sorted alphabetically. | ||
void CategoryList::Sort() | ||
{ | ||
sort(list.begin(), list.end()); | ||
} | ||
|
||
|
||
|
||
// Determine if the CategoryList contains a Category with the given name. | ||
bool CategoryList::Contains(const string &name) const | ||
{ | ||
const auto it = find_if(list.begin(), list.end(), | ||
[&name](const Category &c) noexcept -> bool { return name == c.name; }); | ||
return it != list.end(); | ||
} | ||
|
||
|
||
|
||
const CategoryList::Category CategoryList::GetCategory(const string &name) const | ||
{ | ||
auto it = byName.find(name); | ||
if(it != byName.end()) | ||
return it->second; | ||
return Category("", numeric_limits<int>::max()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* CategoryList.h | ||
Copyright (c) 2022 by Amazinite | ||
Endless Sky 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. | ||
Endless Sky 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, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef CATEGORY_LIST_H_ | ||
#define CATEGORY_LIST_H_ | ||
|
||
#include <iterator> | ||
#include <map> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
class DataNode; | ||
|
||
|
||
|
||
// A CategoryList is a list of names that are associated to a Category of items (e.g. ships | ||
// or outfits). Categories within the list are sorted by the precedence of each Category. | ||
// Any conflicting precedencies are resolved by sorting the names of the Categories | ||
// alphabetically. | ||
class CategoryList { | ||
public: | ||
// A Category is a string with some precedence to it. The precedence is used to sort | ||
// the Category within the CategoryList. Only the CategoryList has access to the | ||
// precedence of each Category. All outside classes can only see the Category's | ||
// name. | ||
class Category { | ||
public: | ||
Category(std::string name, int precedence) : name(name), precedence(precedence) {} | ||
const std::string &Name() const { return name; } | ||
const bool operator<(Category other) const { return SortHelper(*this, other); } | ||
const bool operator()(Category &a, Category &b) const { return SortHelper(a, b); } | ||
|
||
private: | ||
static const bool SortHelper(const Category &a, const Category &b); | ||
|
||
|
||
private: | ||
friend class CategoryList; | ||
std::string name; | ||
int precedence = 0; | ||
}; | ||
|
||
public: | ||
CategoryList() = default; | ||
|
||
void Load(const DataNode &node); | ||
|
||
// Sort the CategoryList. Categories are sorted by precedence. If multiple Categories | ||
// share the same precedence then they are sorted alphabetically. | ||
void Sort(); | ||
|
||
// Determine if the CategoryList contains a Category with the given name. | ||
bool Contains(const std::string &name) const; | ||
|
||
const Category GetCategory(const std::string &name) const; | ||
|
||
typename std::vector<Category>::iterator begin() noexcept { return list.begin(); } | ||
typename std::vector<Category>::const_iterator begin() const noexcept { return list.begin(); } | ||
typename std::vector<Category>::iterator end() noexcept { return list.end(); } | ||
typename std::vector<Category>::const_iterator end() const noexcept { return list.end(); } | ||
|
||
|
||
private: | ||
std::vector<Category> list; | ||
std::map<const std::string, Category> byName; | ||
int currentPrecedence = 0; | ||
}; | ||
|
||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.