Awoo-Installer/source/ui/usbInstPage.cpp

130 lines
No EOL
6 KiB
C++
Executable file

#include "ui/usbInstPage.hpp"
#include "ui/MainApplication.hpp"
#include "util/util.hpp"
#include "util/config.hpp"
#include "usbInstall.hpp"
#define COLOR(hex) pu::ui::Color::FromHex(hex)
namespace inst::ui {
extern MainApplication *mainApp;
usbInstPage::usbInstPage() : Layout::Layout() {
this->SetBackgroundColor(COLOR("#670000FF"));
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, 22);
this->appVersionText->SetColor(COLOR("#FFFFFFFF"));
this->pageInfoText = TextBlock::New(10, 109, "", 30);
this->pageInfoText->SetColor(COLOR("#FFFFFFFF"));
this->butText = TextBlock::New(10, 678, "", 24);
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->infoImage = Image::New(460, 332, "romfs:/images/icons/usb-connection-waiting.png");
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);
this->Add(this->infoImage);
}
void usbInstPage::drawMenuItems(bool clearItems) {
if (clearItems) this->selectedTitles = {};
this->menu->ClearItems();
for (auto& url: this->ourTitles) {
pu::String itm = inst::util::shortenString(inst::util::formatUrlString(url), 56, true);
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] == url) {
ourEntry->SetIcon("romfs:/images/icons/check-box-outline.png");
}
}
this->menu->AddItem(ourEntry);
}
}
void usbInstPage::selectTitle(int selectedIndex) {
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->ourTitles[selectedIndex]) this->selectedTitles.erase(this->selectedTitles.begin() + i);
}
} else this->selectedTitles.push_back(this->ourTitles[selectedIndex]);
this->drawMenuItems(false);
}
void usbInstPage::startUsb() {
this->pageInfoText->SetText("USB connection successful! Waiting for list of files to be sent...");
this->butText->SetText("\ue0e2 (Hold) Help \ue0e1 (Hold) Cancel ");
this->menu->SetVisible(false);
this->menu->ClearItems();
this->infoImage->SetVisible(true);
mainApp->LoadLayout(mainApp->usbinstPage);
mainApp->CallForRender();
this->ourTitles = usbInstStuff::OnSelected();
if (!this->ourTitles.size()) {
mainApp->LoadLayout(mainApp->mainPage);
return;
} else {
mainApp->CallForRender(); // If we re-render a few times during this process the main screen won't flicker
this->pageInfoText->SetText("Select what files you want to install over USB, then press the Plus button!");
this->butText->SetText("\ue0e0 Select File \ue0e3 Select All \ue0ef Install File(s) \ue0e1 Cancel ");
this->drawMenuItems(true);
this->menu->SetSelectedIndex(0);
mainApp->CallForRender();
this->infoImage->SetVisible(false);
this->menu->SetVisible(true);
}
return;
}
void usbInstPage::startInstall() {
int dialogResult = -1;
if (this->selectedTitles.size() == 1) dialogResult = mainApp->CreateShowDialog("Where should " + inst::util::shortenString(inst::util::formatUrlString(this->selectedTitles[0]), 32, true) + " be installed to?", "Press B to cancel", {"SD Card", "Internal Storage"}, false);
else dialogResult = mainApp->CreateShowDialog("Where should the selected " + std::to_string(this->selectedTitles.size()) + " files be installed to?", "Press B to cancel", {"SD Card", "Internal Storage"}, false);
if (dialogResult == -1) return;
usbInstStuff::installTitleUsb(this->selectedTitles, dialogResult);
return;
}
void usbInstPage::onInput(u64 Down, u64 Up, u64 Held, pu::ui::Touch Pos) {
if (Down & KEY_B) {
mainApp->LoadLayout(mainApp->mainPage);
}
if ((Down & KEY_A) || (Up & KEY_TOUCH)) {
this->selectTitle(this->menu->GetSelectedIndex());
if (this->menu->GetItems().size() == 1 && this->selectedTitles.size() == 1) {
this->startInstall();
}
}
if ((Down & KEY_Y)) {
if (this->selectedTitles.size() == this->menu->GetItems().size()) this->drawMenuItems(true);
else {
for (long unsigned int i = 0; i < this->menu->GetItems().size(); i++) {
if (this->menu->GetItems()[i]->GetIcon() == "romfs:/images/icons/check-box-outline.png") continue;
else this->selectTitle(i);
}
this->drawMenuItems(false);
}
}
if (Down & KEY_PLUS) {
if (this->selectedTitles.size() == 0) {
this->selectTitle(this->menu->GetSelectedIndex());
this->startInstall();
return;
}
this->startInstall();
}
}
}