This repository has been archived on 2021-12-25. You can view files and clone it, but cannot push or open issues or pull requests.
Awoo-Installer/source/ui/sdInstPage.cpp
2021-09-05 21:47:36 +02:00

161 lines
6.6 KiB
C++
Executable File

#include <filesystem>
#include "ui/MainApplication.hpp"
#include "ui/mainPage.hpp"
#include "ui/sdInstPage.hpp"
#include "sdInstall.hpp"
#include "util/util.hpp"
#include "util/config.hpp"
#include "util/lang.hpp"
#define COLOR(hex) pu::ui::Color::FromHex(hex)
namespace inst::ui {
extern MainApplication* mainApp;
sdInstPage::sdInstPage() : Layout::Layout() {
this->SetBackgroundColor(COLOR("#670000FF"));
if (std::filesystem::exists(inst::config::appDir + "/background.png")) this->SetBackgroundImage(inst::config::appDir + "/background.png");
else this->SetBackgroundImage("romfs:/images/background.jpg");
this->topRect = Rectangle::New(0, 0, 1280, 94, COLOR("#170909FF"));
this->infoRect = Rectangle::New(0, 95, 1280, 60, COLOR("#17090980"));
this->botRect = Rectangle::New(0, 660, 1280, 60, COLOR("#17090980"));
this->titleImage = Image::New(0, 0, "romfs:/images/logo.png");
this->appVersionText = TextBlock::New(480, 49, "v" + inst::config::appVersion);
this->appVersionText->SetColor(COLOR("#FFFFFFFF"));
this->pageInfoText = TextBlock::New(10, 109, "inst.sd.top_info"_lang);
this->pageInfoText->SetColor(COLOR("#FFFFFFFF"));
this->butText = TextBlock::New(10, 678, "inst.sd.buttons"_lang);
this->butText->SetColor(COLOR("#FFFFFFFF"));
this->menu = pu::ui::elm::Menu::New(0, 156, 1280, COLOR("#FFFFFF00"), 84, (506 / 84));
this->menu->SetOnFocusColor(COLOR("#00000033"));
this->menu->SetScrollbarColor(COLOR("#17090980"));
this->Add(this->topRect);
this->Add(this->infoRect);
this->Add(this->botRect);
this->Add(this->titleImage);
this->Add(this->appVersionText);
this->Add(this->butText);
this->Add(this->pageInfoText);
this->Add(this->menu);
}
void sdInstPage::drawMenuItems(bool clearItems, std::filesystem::path ourPath) {
if (clearItems) this->selectedTitles = {};
if (ourPath == "sdmc:") this->currentDir = std::filesystem::path(ourPath.string() + "/");
else this->currentDir = ourPath;
this->menu->ClearItems();
try {
this->ourDirectories = util::getDirsAtPath(this->currentDir);
this->ourFiles = util::getDirectoryFiles(this->currentDir, { ".nsp", ".nsz", ".xci", ".xcz" });
}
catch (std::exception& e) {
this->drawMenuItems(false, this->currentDir.parent_path());
return;
}
if (this->currentDir != "sdmc:/") {
std::string itm = "..";
auto ourEntry = pu::ui::elm::MenuItem::New(itm);
ourEntry->SetColor(COLOR("#FFFFFFFF"));
ourEntry->SetIcon("romfs:/images/icons/folder-upload.png");
this->menu->AddItem(ourEntry);
}
for (auto& file : this->ourDirectories) {
if (file == "..") break;
std::string itm = file.filename().string();
auto ourEntry = pu::ui::elm::MenuItem::New(itm);
ourEntry->SetColor(COLOR("#FFFFFFFF"));
ourEntry->SetIcon("romfs:/images/icons/folder.png");
this->menu->AddItem(ourEntry);
}
for (auto& file : this->ourFiles) {
std::string itm = file.filename().string();
auto ourEntry = pu::ui::elm::MenuItem::New(itm);
ourEntry->SetColor(COLOR("#FFFFFFFF"));
ourEntry->SetIcon("romfs:/images/icons/checkbox-blank-outline.png");
for (long unsigned int i = 0; i < this->selectedTitles.size(); i++) {
if (this->selectedTitles[i] == file) {
ourEntry->SetIcon("romfs:/images/icons/check-box-outline.png");
}
}
this->menu->AddItem(ourEntry);
}
}
void sdInstPage::followDirectory() {
int selectedIndex = this->menu->GetSelectedIndex();
int dirListSize = this->ourDirectories.size();
if (this->currentDir != "sdmc:/") {
dirListSize++;
selectedIndex--;
}
if (selectedIndex < dirListSize) {
if (this->menu->GetItems()[this->menu->GetSelectedIndex()]->GetName() == ".." && this->menu->GetSelectedIndex() == 0) {
this->drawMenuItems(true, this->currentDir.parent_path());
}
else {
this->drawMenuItems(true, this->ourDirectories[selectedIndex]);
}
this->menu->SetSelectedIndex(0);
}
}
void sdInstPage::selectNsp(int selectedIndex) {
int dirListSize = this->ourDirectories.size();
if (this->currentDir != "sdmc:/") dirListSize++;
if (this->menu->GetItems()[selectedIndex]->GetIcon() == "romfs:/images/icons/check-box-outline.png") {
for (long unsigned int i = 0; i < this->selectedTitles.size(); i++) {
if (this->selectedTitles[i] == this->ourFiles[selectedIndex - dirListSize]) this->selectedTitles.erase(this->selectedTitles.begin() + i);
}
}
else if (this->menu->GetItems()[selectedIndex]->GetIcon() == "romfs:/images/icons/checkbox-blank-outline.png") this->selectedTitles.push_back(this->ourFiles[selectedIndex - dirListSize]);
else {
this->followDirectory();
return;
}
this->drawMenuItems(false, currentDir);
}
void sdInstPage::startInstall() {
int dialogResult = -1;
if (this->selectedTitles.size() == 1) {
dialogResult = mainApp->CreateShowDialog("inst.target.desc0"_lang + inst::util::shortenString(std::filesystem::path(this->selectedTitles[0]).filename().string(), 32, true) + "inst.target.desc1"_lang, "common.cancel_desc"_lang, { "inst.target.opt0"_lang, "inst.target.opt1"_lang }, false);
}
else dialogResult = mainApp->CreateShowDialog("inst.target.desc00"_lang + std::to_string(this->selectedTitles.size()) + "inst.target.desc01"_lang, "common.cancel_desc"_lang, { "inst.target.opt0"_lang, "inst.target.opt1"_lang }, false);
if (dialogResult == -1) return;
nspInstStuff::installNspFromFile(this->selectedTitles, dialogResult);
}
void sdInstPage::onInput(u64 Down, u64 Up, u64 Held, pu::ui::Touch Pos) {
if (Down & HidNpadButton_B) {
mainApp->LoadLayout(mainApp->mainPage);
}
if ((Down & HidNpadButton_A)) {
this->selectNsp(this->menu->GetSelectedIndex());
if (this->ourFiles.size() == 1 && this->selectedTitles.size() == 1) {
this->startInstall();
}
}
if ((Down & HidNpadButton_Y)) {
if (this->selectedTitles.size() == this->ourFiles.size()) this->drawMenuItems(true, currentDir);
else {
int topDir = 0;
if (this->currentDir != "sdmc:/") topDir++;
for (long unsigned int i = this->ourDirectories.size() + topDir; i < this->menu->GetItems().size(); i++) {
if (this->menu->GetItems()[i]->GetIcon() == "romfs:/images/icons/check-box-outline.png") continue;
else this->selectNsp(i);
}
this->drawMenuItems(false, currentDir);
}
}
if ((Down & HidNpadButton_A)) {
inst::ui::mainApp->CreateShowDialog("inst.sd.help.title"_lang, "inst.sd.help.desc"_lang, { "common.ok"_lang }, true);
}
if (Down & HidNpadButton_Plus) {
if (this->selectedTitles.size() == 0 && this->menu->GetItems()[this->menu->GetSelectedIndex()]->GetIcon() == "romfs:/images/icons/checkbox-blank-outline.png") {
this->selectNsp(this->menu->GetSelectedIndex());
}
if (this->selectedTitles.size() > 0) this->startInstall();
}
}
}