From bc8b631b84e03c7e9f3999ad0d97470b456c5af0 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 20 Apr 2024 19:53:41 +0200 Subject: [PATCH] make choosing a name for new tox profiles possible --- src/main_screen.cpp | 5 +++-- src/main_screen.hpp | 2 +- src/start_screen.cpp | 32 ++++++++++++++++++++++++-------- src/start_screen.hpp | 3 ++- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 79f27dc..6f99b0e 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -12,7 +12,7 @@ #include #include -MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector plugins) : +MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::string new_username, std::vector plugins) : renderer(renderer_), rmm(cr), msnj{cr, {}, {}}, @@ -44,9 +44,10 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri conf.set("tox", "save_file_path", save_path); { // name stuff + // a new profile will not have this set auto name = tc.toxSelfGetName(); if (name.empty()) { - name = "tomato"; + name = new_username; } conf.set("tox", "name", name); tc.setSelfName(name); // TODO: this is ugly diff --git a/src/main_screen.hpp b/src/main_screen.hpp index 5d6f29e..3dd2cc0 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -89,7 +89,7 @@ struct MainScreen final : public Screen { uint64_t _window_hidden_ts {0}; float _time_since_event {0.f}; - MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector plugins); + MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::string new_username, std::vector plugins); ~MainScreen(void); bool handleEvent(SDL_Event& e) override; diff --git a/src/start_screen.cpp b/src/start_screen.cpp index f7ff6a5..16a27d7 100644 --- a/src/start_screen.cpp +++ b/src/start_screen.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -33,13 +34,13 @@ Screen* StartScreen::render(float, bool&) { _fss.requestFile( [](const auto& path) -> bool { return std::filesystem::is_regular_file(path); }, [this](const auto& path) { - tox_profile_path = path.string(); + _tox_profile_path = path.string(); }, [](){} ); } ImGui::SameLine(); - ImGui::TextUnformatted(tox_profile_path.c_str()); + ImGui::TextUnformatted(_tox_profile_path.c_str()); ImGui::TextUnformatted("password:"); ImGui::SameLine(); @@ -56,8 +57,22 @@ Screen* StartScreen::render(float, bool&) { if (ImGui::BeginTabItem("create profile")) { _new_save = true; - ImGui::TextUnformatted("TODO: profile path"); - ImGui::TextUnformatted("TODO: profile name"); + 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(c)) && c != '-' && c != '.') { + c = '_'; + } + } + + if (tmp_copy.empty()) { + tmp_copy = "unnamed-tomato"; + } + + _tox_profile_path = tmp_copy + ".tox"; + } ImGui::TextUnformatted("password:"); ImGui::SameLine(); @@ -69,6 +84,8 @@ Screen* StartScreen::render(float, bool&) { ImGui::SameLine(); ImGui::Checkbox("show password", &_show_password); + ImGui::TextUnformatted("TODO: profile path (current path for now)"); + ImGui::EndTabItem(); } if (ImGui::BeginTabItem("plugins")) { @@ -81,7 +98,6 @@ Screen* StartScreen::render(float, bool&) { continue; } ImGui::SameLine(); - ImGui::TextUnformatted(it->c_str()); ImGui::PopID(); @@ -105,7 +121,7 @@ Screen* StartScreen::render(float, bool&) { ImGui::Separator(); - if (!_new_save && !std::filesystem::is_regular_file(tox_profile_path)) { + if (!_new_save && !std::filesystem::is_regular_file(_tox_profile_path)) { // load but file missing ImGui::BeginDisabled(); @@ -115,7 +131,7 @@ Screen* StartScreen::render(float, bool&) { if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) { ImGui::SetTooltip("file does not exist"); } - } else if (_new_save && std::filesystem::exists(tox_profile_path)) { + } else if (_new_save && std::filesystem::exists(_tox_profile_path)) { // new but file exists ImGui::BeginDisabled(); @@ -127,7 +143,7 @@ Screen* StartScreen::render(float, bool&) { } } else { if (ImGui::Button("load", {60, 25})) { - auto new_screen = std::make_unique(_renderer, tox_profile_path, _password, queued_plugin_paths); + auto new_screen = std::make_unique(_renderer, _tox_profile_path, _password, _user_name, queued_plugin_paths); return new_screen.release(); } } diff --git a/src/start_screen.hpp b/src/start_screen.hpp index 77fc570..0aa2715 100644 --- a/src/start_screen.hpp +++ b/src/start_screen.hpp @@ -17,11 +17,12 @@ struct StartScreen final : public Screen { FileSelector _fss; bool _new_save {false}; + std::string _user_name {"unnamed-tomato"}; bool _show_password {false}; std::string _password; - std::string tox_profile_path {"tomato.tox"}; + std::string _tox_profile_path {"unnamed-tomato.tox"}; std::vector queued_plugin_paths; StartScreen(void) = delete;