2023-07-26 12:24:18 +02:00
|
|
|
#include "./start_screen.hpp"
|
|
|
|
|
|
|
|
#include "./main_screen.hpp"
|
|
|
|
|
2024-05-30 11:46:58 +02:00
|
|
|
#include "./json_to_config.hpp"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-08-03 13:05:19 +02:00
|
|
|
#include <imgui/imgui.h>
|
2023-08-28 15:33:36 +02:00
|
|
|
#include <imgui/misc/cpp/imgui_stdlib.h>
|
2023-08-03 13:05:19 +02:00
|
|
|
|
2024-04-20 19:53:41 +02:00
|
|
|
#include <cctype>
|
2023-07-26 12:24:18 +02:00
|
|
|
#include <memory>
|
2023-08-03 13:05:19 +02:00
|
|
|
#include <filesystem>
|
2024-05-30 11:46:58 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
StartScreen::StartScreen(const std::vector<std::string_view>& args, SDL_Renderer* renderer, Theme& theme) : _renderer(renderer), _theme(theme) {
|
|
|
|
for (size_t ai = 0; ai < args.size(); ai++) {
|
|
|
|
if (args.at(ai) == "--config" || args.at(ai) == "-c") {
|
|
|
|
if (args.size() == ai+1) {
|
|
|
|
std::cerr << "TOMATO error: argument '" << args.at(ai) << "' missing parameter!\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai++;
|
|
|
|
|
|
|
|
const auto& config_path = args.at(ai);
|
|
|
|
auto config_file = std::ifstream(static_cast<std::string>(config_path));
|
|
|
|
if (!config_file.is_open()) {
|
|
|
|
std::cerr << "TOMATO error: failed to open config file '" << config_path << "'\n";
|
|
|
|
break;
|
|
|
|
}
|
2023-07-26 12:24:18 +02:00
|
|
|
|
2024-05-30 11:46:58 +02:00
|
|
|
auto config_json = nlohmann::ordered_json::parse(config_file);
|
|
|
|
if (!load_json_into_config(config_json, _conf)) {
|
|
|
|
std::cerr << "TOMATO error in config json, exiting...\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (args.at(ai) == "--plugin" || args.at(ai) == "-p") {
|
|
|
|
if (args.size() == ai+1) {
|
|
|
|
std::cerr << "TOMATO error: argument '" << args.at(ai) << "' missing parameter!\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai++;
|
|
|
|
|
|
|
|
const auto& plugin_path = args.at(ai);
|
|
|
|
// TODO: check for dups
|
|
|
|
queued_plugin_paths.push_back(static_cast<std::string>(plugin_path));
|
|
|
|
} else {
|
|
|
|
std::cerr << "TOMATO error: unknown cli arg: '" << args.at(ai) << "'\n";
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 12:24:18 +02:00
|
|
|
}
|
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
Screen* StartScreen::render(float, bool&) {
|
2023-07-26 12:24:18 +02:00
|
|
|
|
|
|
|
// TODO: imgui tox profile selector?
|
2023-08-03 13:05:19 +02:00
|
|
|
|
2023-07-26 12:55:50 +02:00
|
|
|
// +----------------------------
|
|
|
|
// | |*tox profile*| plugins |
|
2023-07-26 12:24:18 +02:00
|
|
|
// | +------+ +--------
|
|
|
|
// | | ICON | | fileselector/dropdown?
|
|
|
|
// | | | | password input
|
|
|
|
// | +------+ +--------
|
2023-07-26 12:55:50 +02:00
|
|
|
// +----------------------------
|
2023-07-26 12:24:18 +02:00
|
|
|
|
2023-08-03 13:05:19 +02:00
|
|
|
if (ImGui::BeginTabBar("view")) {
|
|
|
|
if (ImGui::BeginTabItem("load profile")) {
|
2023-08-28 15:33:36 +02:00
|
|
|
_new_save = false;
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("profile :");
|
|
|
|
ImGui::SameLine();
|
2023-08-03 15:03:12 +02:00
|
|
|
if (ImGui::Button("select")) {
|
|
|
|
_fss.requestFile(
|
|
|
|
[](const auto& path) -> bool { return std::filesystem::is_regular_file(path); },
|
|
|
|
[this](const auto& path) {
|
2024-04-20 19:53:41 +02:00
|
|
|
_tox_profile_path = path.string();
|
2023-08-03 15:03:12 +02:00
|
|
|
},
|
|
|
|
[](){}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
2024-04-20 19:53:41 +02:00
|
|
|
ImGui::TextUnformatted(_tox_profile_path.c_str());
|
2023-08-03 15:03:12 +02:00
|
|
|
|
2023-08-28 15:33:36 +02:00
|
|
|
ImGui::TextUnformatted("password:");
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (_show_password) {
|
|
|
|
ImGui::InputText("##password", &_password);
|
|
|
|
} else {
|
|
|
|
ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password);
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::Checkbox("show password", &_show_password);
|
2023-08-03 15:03:12 +02:00
|
|
|
|
2023-08-03 13:05:19 +02:00
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("create profile")) {
|
2023-08-28 15:33:36 +02:00
|
|
|
_new_save = true;
|
|
|
|
|
2024-04-20 19:53:41 +02:00
|
|
|
ImGui::TextUnformatted("username:");
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::InputText("##user_name", &_user_name)) {
|
|
|
|
std::string tmp_copy = _user_name;
|
|
|
|
for (auto& c : tmp_copy) {
|
|
|
|
if (!std::isalnum(static_cast<unsigned char>(c)) && c != '-' && c != '.') {
|
|
|
|
c = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_copy.empty()) {
|
|
|
|
tmp_copy = "unnamed-tomato";
|
|
|
|
}
|
|
|
|
|
|
|
|
_tox_profile_path = tmp_copy + ".tox";
|
|
|
|
}
|
2023-08-28 15:33:36 +02:00
|
|
|
|
|
|
|
ImGui::TextUnformatted("password:");
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (_show_password) {
|
|
|
|
ImGui::InputText("##password", &_password);
|
|
|
|
} else {
|
|
|
|
ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password);
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::Checkbox("show password", &_show_password);
|
|
|
|
|
2024-04-20 19:53:41 +02:00
|
|
|
ImGui::TextUnformatted("TODO: profile path (current path for now)");
|
|
|
|
|
2023-08-03 13:05:19 +02:00
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
if (ImGui::BeginTabItem("plugins")) {
|
|
|
|
// list of selected plugins (in order)
|
|
|
|
for (auto it = queued_plugin_paths.begin(); it != queued_plugin_paths.end();) {
|
2023-08-20 12:50:58 +02:00
|
|
|
ImGui::PushID(it->c_str());
|
2023-08-03 13:05:19 +02:00
|
|
|
if (ImGui::SmallButton("-")) {
|
|
|
|
it = queued_plugin_paths.erase(it);
|
2023-08-20 12:50:58 +02:00
|
|
|
ImGui::PopID();
|
2023-08-03 13:05:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextUnformatted(it->c_str());
|
|
|
|
|
2023-08-20 12:50:58 +02:00
|
|
|
ImGui::PopID();
|
2023-08-03 13:05:19 +02:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::Button("+")) {
|
|
|
|
_fss.requestFile(
|
|
|
|
[](const auto& path) -> bool { return std::filesystem::is_regular_file(path); },
|
|
|
|
[this](const auto& path) {
|
|
|
|
queued_plugin_paths.push_back(path.string());
|
|
|
|
},
|
|
|
|
[](){}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
|
|
|
ImGui::EndTabBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
2024-04-20 19:53:41 +02:00
|
|
|
if (!_new_save && !std::filesystem::is_regular_file(_tox_profile_path)) {
|
2023-08-28 15:33:36 +02:00
|
|
|
// load but file missing
|
|
|
|
|
|
|
|
ImGui::BeginDisabled();
|
|
|
|
ImGui::Button("load", {60, 25});
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) {
|
|
|
|
ImGui::SetTooltip("file does not exist");
|
|
|
|
}
|
2024-04-20 19:53:41 +02:00
|
|
|
} else if (_new_save && std::filesystem::exists(_tox_profile_path)) {
|
2023-08-28 15:33:36 +02:00
|
|
|
// new but file exists
|
|
|
|
|
|
|
|
ImGui::BeginDisabled();
|
|
|
|
ImGui::Button("load", {60, 25});
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
|
|
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) {
|
|
|
|
ImGui::SetTooltip("file already exists");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ImGui::Button("load", {60, 25})) {
|
2024-05-30 11:46:58 +02:00
|
|
|
auto new_screen = std::make_unique<MainScreen>(std::move(_conf), _renderer, _theme, _tox_profile_path, _password, _user_name, queued_plugin_paths);
|
2023-08-28 15:33:36 +02:00
|
|
|
return new_screen.release();
|
|
|
|
}
|
2023-08-03 13:05:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_fss.render();
|
|
|
|
|
|
|
|
return nullptr;
|
2023-07-26 12:24:18 +02:00
|
|
|
}
|
|
|
|
|
2024-01-05 14:47:08 +01:00
|
|
|
Screen* StartScreen::tick(float, bool&) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|