Initial sorting commit.

This is not perfect yet and still needs work.
This commit is contained in:
StackZ
2020-06-19 10:32:17 +02:00
parent c6f0798196
commit e8421ae69f
5 changed files with 349 additions and 32 deletions
+78 -31
View File
@@ -40,6 +40,7 @@ extern bool didAutoboot;
UniStoreV2::UniStoreV2(nlohmann::json &JSON, const std::string sheetPath) {
this->storeJson = JSON;
this->sortedStore = std::make_unique<Store>(this->storeJson);
if (access(sheetPath.c_str(), F_OK) != 0) {
this->iconAmount = 0;
@@ -84,7 +85,6 @@ u32 UniStoreV2::returnTextColor() const {
return this->darkMode ? this->textColorDark : this->textColorLight;
}
// Base draws.
void UniStoreV2::DrawBaseTop(void) const {
Gui::ScreenDraw(Top);
@@ -123,7 +123,7 @@ void UniStoreV2::drawBox(float xPos, float yPos, float width, float height, bool
}
void UniStoreV2::DrawGrid(void) const {
for (int i = 0, i2 = 0 + (this->storePage * STORE_ENTRIES); i2 < STORE_ENTRIES + (this->storePage * STORE_ENTRIES) && i2 < (int)this->storeJson.at("storeContent").size(); i2++, i++) {
for (int i = 0, i2 = 0 + (this->storePage * STORE_ENTRIES); i2 < STORE_ENTRIES + (this->storePage * STORE_ENTRIES) && i2 < this->sortedStore->getSize(); i2++, i++) {
if (i == this->selectedBox) {
this->drawBox(this->StoreBoxesGrid[i].x, this->StoreBoxesGrid[i].y, 100, 50, true);
} else {
@@ -131,9 +131,9 @@ void UniStoreV2::DrawGrid(void) const {
}
if (this->sheetLoaded) {
if (this->storeJson.at("storeContent").at(i + (this->storePage * STORE_ENTRIES)).at("info").contains("icon_index")) {
if ((int)this->storeJson.at("storeContent").at(i + (this->storePage * STORE_ENTRIES)).at("info").at("icon_index") < this->iconAmount) {
Gui::DrawSprite(this->sheet, this->storeJson.at("storeContent").at(i + (this->storePage * STORE_ENTRIES)).at("info").at("icon_index"), this->StoreBoxesGrid[i].x+26, this->StoreBoxesGrid[i].y+1);
if (this->sortedStore->returnIconIndex(i + (this->storePage * STORE_ENTRIES)) != -1) {
if (this->sortedStore->returnIconIndex(i + (this->storePage * STORE_ENTRIES)) < this->iconAmount) {
Gui::DrawSprite(this->sheet, this->sortedStore->returnIconIndex(i + (this->storePage * STORE_ENTRIES)), this->StoreBoxesGrid[i].x+26, this->StoreBoxesGrid[i].y+1);
} else {
GFX::DrawSprite(sprites_noIcon_idx, this->StoreBoxesGrid[i].x+26, this->StoreBoxesGrid[i].y+1);
}
@@ -147,7 +147,7 @@ void UniStoreV2::DrawGrid(void) const {
}
void UniStoreV2::DrawList(void) const {
for (int i = 0, i2 = 0 + (this->storePageList * STORE_ENTRIES_LIST); i2 < STORE_ENTRIES_LIST + (this->storePageList * STORE_ENTRIES_LIST) && i2 < (int)this->storeJson.at("storeContent").size(); i2++, i++) {
for (int i = 0, i2 = 0 + (this->storePageList * STORE_ENTRIES_LIST); i2 < STORE_ENTRIES_LIST + (this->storePageList * STORE_ENTRIES_LIST) && i2 < this->sortedStore->getSize(); i2++, i++) {
if (i == this->selectedBoxList) {
this->drawBox(this->StoreBoxesList[i].x, this->StoreBoxesList[i].y, 360, 50, true);
} else {
@@ -155,9 +155,9 @@ void UniStoreV2::DrawList(void) const {
}
if (this->sheetLoaded) {
if (this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").contains("icon_index")) {
if ((int)this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").at("icon_index") < this->iconAmount) {
Gui::DrawSprite(this->sheet, this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").at("icon_index"), this->StoreBoxesList[i].x+1, this->StoreBoxesList[i].y+1);
if (this->sortedStore->returnIconIndex(i + (this->storePageList * STORE_ENTRIES_LIST)) != -1) {
if (this->sortedStore->returnIconIndex(i + (this->storePageList * STORE_ENTRIES_LIST)) < this->iconAmount) {
Gui::DrawSprite(this->sheet, this->sortedStore->returnIconIndex(i + (this->storePageList * STORE_ENTRIES_LIST)), this->StoreBoxesList[i].x+1, this->StoreBoxesList[i].y+1);
} else {
GFX::DrawSprite(sprites_noIcon_idx, this->StoreBoxesList[i].x+1, this->StoreBoxesList[i].y+1);
}
@@ -169,20 +169,38 @@ void UniStoreV2::DrawList(void) const {
}
// Display Author & App name.
if (this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").contains("title")) {
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+12, 0.45f, this->returnTextColor(), (std::string)this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").at("title"), 300);
} else {
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+12, 0.45f, this->returnTextColor(), "?", 300);
}
if (this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").contains("author")) {
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+28, 0.45f, this->returnTextColor(), (std::string)this->storeJson.at("storeContent").at(i + (this->storePageList * STORE_ENTRIES_LIST)).at("info").at("author"), 300);
} else {
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+28, 0.45f, this->returnTextColor(), "?", 300);
}
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+12, 0.45f, this->returnTextColor(), this->sortedStore->returnTitle(i + (this->storePageList * STORE_ENTRIES_LIST)), 300);
Gui::DrawString(this->StoreBoxesList[i].x+55, this->StoreBoxesList[i].y+28, 0.45f, this->returnTextColor(), this->sortedStore->returnAuthor(i + (this->storePageList * STORE_ENTRIES_LIST)), 300);
}
}
void UniStoreV2::DrawSortingMenu(void) const {
for (int i = 2; i < (int)sortingPos.size(); i++) {
if (i - 2 == this->sortedStore->getSortType()) {
this->drawBox(sortingPos[i].x, sortingPos[i].y, sortingPos[i].w, sortingPos[i].h, true);
} else {
this->drawBox(sortingPos[i].x, sortingPos[i].y, sortingPos[i].w, sortingPos[i].h, false);
}
}
Gui::DrawString(sortingPos[2].x+4, sortingPos[2].y+9, 0.55f, this->returnTextColor(), Lang::get("TITLE_BTN"));
Gui::DrawString(sortingPos[3].x+4, sortingPos[3].y+9, 0.55f, this->returnTextColor(), Lang::get("AUTHOR_BTN"));
Gui::DrawString(sortingPos[4].x+4, sortingPos[4].y+9, 0.55f, this->returnTextColor(), Lang::get("LAST_UPDATED_BTN"));
if (this->sortedStore->getAscending()) {
this->drawBox(sortingPos[0].x, sortingPos[0].y, sortingPos[0].w, sortingPos[0].h, false);
this->drawBox(sortingPos[1].x, sortingPos[1].y, sortingPos[1].w, sortingPos[1].h, true);
} else {
this->drawBox(sortingPos[0].x, sortingPos[0].y, sortingPos[0].w, sortingPos[0].h, true);
this->drawBox(sortingPos[1].x, sortingPos[1].y, sortingPos[1].w, sortingPos[1].h, false);
}
Gui::DrawString(sortingPos[0].x+4, sortingPos[0].y+9, 0.55f, this->returnTextColor(), Lang::get("DESCENDING"));
Gui::DrawString(sortingPos[1].x+4, sortingPos[1].y+9, 0.55f, this->returnTextColor(), Lang::get("ASCENDING"));
}
// Parse the objects from a script.
void UniStoreV2::parseObjects(int selection) {
this->objects.clear();
@@ -304,6 +322,7 @@ void UniStoreV2::Draw(void) const {
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 400, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha));
this->DrawBaseBottom();
this->DrawSortingMenu();
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha));
} else if (this->mode == 1) {
@@ -320,7 +339,7 @@ void UniStoreV2::Draw(void) const {
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 400, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha));
this->DrawBaseBottom();
this->DrawSortingMenu();
if (fadealpha > 0) Gui::Draw_Rect(0, 0, 320, 240, C2D_Color32(fadecolor, fadecolor, fadecolor, fadealpha));
} else if (this->mode == 2) {
this->displaySelectedEntry(this->selection);
@@ -386,12 +405,12 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
if (hDown & KEY_RIGHT) {
// Try to go to next page.
if (this->selectedBox == 2 || this->selectedBox == 5 || this->selectedBox == 8) {
if (STORE_ENTRIES + (this->storePage * STORE_ENTRIES) < (int)this->storeJson.at("storeContent").size()) {
if (STORE_ENTRIES + (this->storePage * STORE_ENTRIES) < (int)this->sortedStore->getSize()) {
this->selectedBox = 0;
this->storePage++;
}
} else {
if ((this->storePage * STORE_ENTRIES) + this->selectedBox + 1 < (int)this->storeJson.at("storeContent").size()) {
if ((this->storePage * STORE_ENTRIES) + this->selectedBox + 1 < (int)this->sortedStore->getSize()) {
if (this->selectedBox < 8 + (this->storePage * STORE_ENTRIES)) this->selectedBox++;
}
}
@@ -414,13 +433,13 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
if (hDown & KEY_DOWN) {
if ((this->storePage * STORE_ENTRIES) + this->selectedBox + 3 < (int)this->storeJson.at("storeContent").size()) {
if ((this->storePage * STORE_ENTRIES) + this->selectedBox + 3 < (int)this->sortedStore->getSize()) {
if (this->selectedBox < 6) this->selectedBox += 3;
}
}
if (hDown & KEY_R) {
if (STORE_ENTRIES + (this->storePage * STORE_ENTRIES) < (int)this->storeJson.at("storeContent").size()) {
if (STORE_ENTRIES + (this->storePage * STORE_ENTRIES) < (int)this->sortedStore->getSize()) {
this->selectedBox = 0;
this->storePage++;
}
@@ -435,8 +454,8 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
if (hDown & KEY_A) {
if (this->selectedBox + (this->storePage * STORE_ENTRIES) < (int)this->storeJson.at("storeContent").size()) {
this->selection = this->selectedBox + (this->storePage * STORE_ENTRIES);
if (this->sortedStore->returnJSONIndex(this->selectedBox + (this->storePage * STORE_ENTRIES)) < (int)this->storeJson.at("storeContent").size()) {
this->selection = this->sortedStore->returnJSONIndex(this->selectedBox + (this->storePage * STORE_ENTRIES));
this->parseObjects(this->selection);
this->canDisplay = true;
this->lastViewMode = this->mode;
@@ -444,6 +463,20 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
}
if (hDown & KEY_TOUCH) {
if (touching(touch, sortingPos[0])) {
this->sortedStore->sorting(false, SortType(this->sortedStore->getSortType()));
} else if (touching(touch, sortingPos[1])) {
this->sortedStore->sorting(true, SortType(this->sortedStore->getSortType()));
} else if (touching(touch, sortingPos[2])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(0));
} else if (touching(touch, sortingPos[3])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(1));
} else if (touching(touch, sortingPos[4])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(2));
}
}
} else if (this->mode == 1) {
if (hDown & KEY_B) {
if (!didAutoboot) didAutoboot = true;
@@ -452,7 +485,7 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
if (hDown & KEY_DOWN) {
if ((this->storePageList * STORE_ENTRIES_LIST) + this->selectedBoxList + 1 < (int)this->storeJson.at("storeContent").size()) {
if ((this->storePageList * STORE_ENTRIES_LIST) + this->selectedBoxList + 1 < (int)this->sortedStore->getSize()) {
if (this->selectedBoxList < STORE_ENTRIES_LIST-1) this->selectedBoxList++;
}
}
@@ -462,7 +495,7 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
if (hDown & KEY_RIGHT || hDown & KEY_R) {
if (STORE_ENTRIES_LIST + (this->storePageList * STORE_ENTRIES_LIST) < (int)this->storeJson.at("storeContent").size()) {
if (STORE_ENTRIES_LIST + (this->storePageList * STORE_ENTRIES_LIST) < (int)this->sortedStore->getSize()) {
this->selectedBoxList = 0;
this->storePageList++;
}
@@ -476,8 +509,8 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
if (hDown & KEY_A) {
if (this->selectedBoxList + (this->storePageList * STORE_ENTRIES_LIST) < (int)this->storeJson.at("storeContent").size()) {
this->selection = this->selectedBoxList + (this->storePageList * STORE_ENTRIES_LIST);
if (this->sortedStore->returnJSONIndex(this->selectedBoxList + (this->storePageList * STORE_ENTRIES_LIST)) < (int)this->storeJson.at("storeContent").size()) {
this->selection = this->sortedStore->returnJSONIndex(this->selectedBoxList + (this->storePageList * STORE_ENTRIES_LIST));
this->parseObjects(this->selection);
this->canDisplay = true;
this->lastViewMode = this->mode;
@@ -485,6 +518,20 @@ void UniStoreV2::Logic(u32 hDown, u32 hHeld, touchPosition touch) {
}
}
if (hDown & KEY_TOUCH) {
if (touching(touch, sortingPos[0])) {
this->sortedStore->sorting(false, SortType(this->sortedStore->getSortType()));
} else if (touching(touch, sortingPos[1])) {
this->sortedStore->sorting(true, SortType(this->sortedStore->getSortType()));
} else if (touching(touch, sortingPos[2])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(0));
} else if (touching(touch, sortingPos[3])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(1));
} else if (touching(touch, sortingPos[4])) {
this->sortedStore->sorting(this->sortedStore->getAscending(), SortType(2));
}
}
} else if (this->mode == 2) {
if (hDown & KEY_TOUCH) {
if (this->objects.size() > 0) {
+170
View File
@@ -0,0 +1,170 @@
/*
* This file is part of Universal-Updater
* Copyright (C) 2019-2020 DeadPhoenix8091, Epicpkmn11, Flame, RocketRobz, StackZ, TotallyNotGuy
*
* This program 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.
*
* This program 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 <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
#include "store.hpp"
Store::Store(nlohmann::json &JS) {
this->storeJson = JS;
for (int i = 0; i < (int)this->storeJson.at("storeContent").size(); i++) {
this->unsortedStore.push_back(this->getData(i));
}
this->sortedStore = this->unsortedStore; // Put that to sorted store as well.
}
// Here we get the data of the UniStore!
UniStoreV2Struct Store::getData(const int index) {
UniStoreV2Struct temp = {"", "", "", "", "" ,"", -1, 0};
if (index > (int)this->storeJson.at("storeContent").size()) return temp; // Empty.
// Here we check.
// Title.
if (this->storeJson.at("storeContent").at(index).at("info").contains("title")) {
temp.title = this->storeJson.at("storeContent").at(index).at("info").at("title");
}
// Author.
if (this->storeJson.at("storeContent").at(index).at("info").contains("author")) {
temp.author = this->storeJson.at("storeContent").at(index).at("info").at("author");
}
// Version.
if (this->storeJson.at("storeContent").at(index).at("info").contains("version")) {
temp.version = this->storeJson.at("storeContent").at(index).at("info").at("version");
}
if (this->storeJson.at("storeContent").at(index).at("info").contains("category")) {
temp.category = this->storeJson.at("storeContent").at(index).at("info").at("category");
}
// Console.
if (this->storeJson.at("storeContent").at(index).at("info").contains("console")) {
temp.console = this->storeJson.at("storeContent").at(index).at("info").at("console");
}
// Last updated.
if (this->storeJson.at("storeContent").at(index).at("info").contains("last_updated")) {
temp.last_updated = this->storeJson.at("storeContent").at(index).at("info").at("last_updated");
}
// Icon index.
if (this->storeJson.at("storeContent").at(index).at("info").contains("icon_index")) {
temp.icon_index = this->storeJson.at("storeContent").at(index).at("info").at("icon_index");
}
// JSON index.
temp.JSONIndex = index;
return temp;
}
// Title.
bool compareTitleDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = a.title.compare(b.title);
if (result > 0) return true;
else return false;
}
bool compareTitleAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = b.title.compare(a.title);
if (result > 0) return true;
else return false;
}
// Author.
bool compareAuthorDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = a.author.compare(b.author);
if (result > 0) return true;
else return false;
}
bool compareAuthorAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = b.author.compare(a.author);
if (result > 0) return true;
else return false;
}
// Last updated.
bool compareUpdateDescending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = a.last_updated.compare(b.last_updated);
if (result > 0) return true;
else return false;
}
bool compareUpdateAscending(const UniStoreV2Struct& a, const UniStoreV2Struct& b) {
int result = b.last_updated.compare(a.last_updated);
if (result > 0) return true;
else return false;
}
void Store::sorting(bool Ascending, SortType sorttype) {
this->ascending = Ascending;
this->sorttype = sorttype;
switch(this->sorttype) {
case SortType::TITLE:
if (Ascending) {
std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareTitleAscending);
} else {
std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareTitleDescending);
}
break;
case SortType::AUTHOR:
if (Ascending) {
std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareAuthorAscending);
} else {
std::sort(this->sortedStore.begin(), this->sortedStore.end(), compareAuthorDescending);
}
break;
case SortType::LAST_UPDATED:
if (Ascending) {
std::sort(this->sortedStore.begin(), sortedStore.end(), compareUpdateAscending);
} else {
std::sort(this->sortedStore.begin(), sortedStore.end(), compareUpdateDescending);
}
break;
}
}
// Some return stuff with checks!
std::string Store::returnTitle(const int index) {
if (index > (int)this->sortedStore.size()) return "?"; // Out of scope.
return this->sortedStore[index].title;
}
std::string Store::returnAuthor(const int index) {
if (index > (int)this->sortedStore.size()) return "?"; // Out of scope.
return this->sortedStore[index].author;
}
int Store::returnIconIndex(const int index) {
if (index > (int)this->sortedStore.size()) return -1; // Out of scope.
return this->sortedStore[index].icon_index;
}
int Store::returnJSONIndex(const int index) {
if (index > (int)this->sortedStore.size()) return -1; // Out of scope.
return this->sortedStore[index].JSONIndex;
}
int Store::getSize() { return (int)this->sortedStore.size(); }