From 85a18eb03118d8e5117ebf4cbd5c92b8aedd17ab Mon Sep 17 00:00:00 2001 From: StackZ <47382115+SuperSaiyajinStackZ@users.noreply.github.com> Date: Mon, 29 Jun 2020 19:28:52 +0200 Subject: [PATCH] Do Download Speed optional. Cause I cannot properly fix that now. --- include/gui/gfx.hpp | 1 + include/utils/config.hpp | 7 +++++-- source/download/download.cpp | 30 ++++++++++++++---------------- source/gui/gfx.cpp | 10 ++++++++++ source/utils/config.cpp | 8 ++++++++ 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/include/gui/gfx.hpp b/include/gui/gfx.hpp index 1641c20..350ba06 100644 --- a/include/gui/gfx.hpp +++ b/include/gui/gfx.hpp @@ -44,6 +44,7 @@ namespace GFX { void DrawSpriteBlend(int img, int x, int y, float ScaleX = 1, float ScaleY = 1); void DrawButton(int x, int y, std::string ButtonText = "", u32 color = config->buttonColor()); + void TextFormatted(float x, float y, float size, const char *format, ...); } #endif \ No newline at end of file diff --git a/include/utils/config.hpp b/include/utils/config.hpp index fd754c3..3c1f83b 100644 --- a/include/utils/config.hpp +++ b/include/utils/config.hpp @@ -120,7 +120,10 @@ public: // Use ScriptColors. bool useScriptColor() { return this->v_useScriptColor; } void useScriptColor(bool v) { this->v_useScriptColor = v; if (!this->changesMade) this->changesMade = true; } - + // Show Downloadspeed. + bool showSpeed() { return this->v_showSpeed; } + void showSpeed(bool v) { this->v_showSpeed = v; if (!this->changesMade) this->changesMade = true; } + // Mainly helper. bool getBool(const std::string &key); void setBool(const std::string &key, bool v); @@ -138,7 +141,7 @@ private: v_outdatedColor, v_uptodateColor, v_notfoundColor, v_futureColor; std::string v_scriptPath, v_musicPath, v_storePath, v_autobootFile, v_language; int v_langPath, v_viewMode, v_autoboot, v_keyDelay; - bool v_logging, v_useBars, v_screenFade, v_progressDisplay, v_firstStartup, v_useScriptColor; + bool v_logging, v_useBars, v_screenFade, v_progressDisplay, v_firstStartup, v_useScriptColor, v_showSpeed; }; #endif \ No newline at end of file diff --git a/source/download/download.cpp b/source/download/download.cpp index 2fd473e..401c8d2 100644 --- a/source/download/download.cpp +++ b/source/download/download.cpp @@ -851,22 +851,10 @@ void displayProgressBar() { downloadTotal = downloadNow; } - if (!curlResult) { - curl_off_t speed; - curlResult = curl_easy_getinfo(hnd, CURLINFO_SPEED_DOWNLOAD_T, &speed); - if (!curlResult) { - snprintf(str, sizeof(str), "%s / %s (%.2f%%) \n\n\n\n\n %s %lld %s", - formatBytes(downloadNow).c_str(), - formatBytes(downloadTotal).c_str(), - ((float)downloadNow/(float)downloadTotal) * 100.0f, - Lang::get("DOWNLOAD_SPEED").c_str(), (speed / 1000), Lang::get("KB_PER_SECOND").c_str()); - } else { - snprintf(str, sizeof(str), "%s / %s (%.2f%%)", - formatBytes(downloadNow).c_str(), - formatBytes(downloadTotal).c_str(), - ((float)downloadNow/(float)downloadTotal) * 100.0f); - } - } + snprintf(str, sizeof(str), "%s / %s (%.2f%%)", + formatBytes(downloadNow).c_str(), + formatBytes(downloadTotal).c_str(), + ((float)downloadNow/(float)downloadTotal) * 100.0f); break; case ProgressBar::Extracting: snprintf(str, sizeof(str), "%s / %s (%.2f%%)", @@ -893,6 +881,16 @@ void displayProgressBar() { switch(progressbarType) { case ProgressBar::Downloading: Gui::DrawStringCentered(0, 80, 0.6f, isScriptSelected ? TextColor : config->textColor(), str, 400); + + if (!curlResult && hnd != nullptr && config->showSpeed()) { + curl_off_t speed = 0; + curlResult = curl_easy_getinfo(hnd, CURLINFO_SPEED_DOWNLOAD_T, &speed); + if (!curlResult) { + GFX::TextFormatted(0, 170, 0.6f, "%s %lld %s", Lang::get("DOWNLOAD_SPEED").c_str(), (speed / 1000), Lang::get("KB_PER_SECOND").c_str()); + } else { + Gui::DrawStringCentered(0, 170, 0.6f, isScriptSelected ? TextColor : config->textColor(), Lang::get("DOWNLOAD_SPEED") + "?"); + } + } Animation::DrawProgressBar(downloadNow, downloadTotal); break; case ProgressBar::Extracting: diff --git a/source/gui/gfx.cpp b/source/gui/gfx.cpp index bd05d94..6f10064 100644 --- a/source/gui/gfx.cpp +++ b/source/gui/gfx.cpp @@ -98,4 +98,14 @@ void GFX::DrawButton(int x, int y, std::string ButtonText, u32 color) { C2D_SetImageTint(&tint, C2D_BotRight, color, 0.5); C2D_DrawImageAt(C2D_SpriteSheetGetImage(sprites, sprites_button_idx), x, y, 0.5f, &tint); Gui::DrawStringCentered(- (158/2) + x, y + (61/2) - (Gui::GetStringHeight(0.6f, ButtonText) / 2), 0.6f, isScriptSelected ? TextColor : config->textColor(), ButtonText, 145, 30); +} + +void GFX::TextFormatted(float x, float y, float size, const char *format, ...) { + char str[512]; + va_list va; + va_start(va, format); + vsnprintf(str, 512, format, va); + va_end(va); + char * Text = strtok(str, "\n"); + Gui::DrawStringCentered(x, y, size, isScriptSelected ? TextColor : config->textColor(), Text); } \ No newline at end of file diff --git a/source/utils/config.cpp b/source/utils/config.cpp index 91ac7f4..eb2d874 100644 --- a/source/utils/config.cpp +++ b/source/utils/config.cpp @@ -68,6 +68,7 @@ void Config::initialize() { this->setString("LANGUAGE", "en"); this->setBool("FIRST_STARTUP", true); this->setBool("USE_SCRIPT_COLORS", true); + this->setBool("SHOW_SPEED", false); this->setInt("VERSION", this->configVersion); // Write to file. @@ -258,6 +259,12 @@ Config::Config() { this->useScriptColor(this->getBool("USE_SCRIPT_COLORS")); } + if (!this->json.contains("SHOW_SPEED")) { + this->showSpeed(false); + } else { + this->showSpeed(this->getBool("SHOW_SPEED")); + } + this->changesMade = false; // No changes made yet. } @@ -294,6 +301,7 @@ void Config::save() { this->setString("LANGUAGE", this->language()); this->setBool("FIRST_STARTUP", this->firstStartup()); this->setBool("USE_SCRIPT_COLORS", this->useScriptColor()); + this->setBool("SHOW_SPEED", this->showSpeed()); // Write changes to file. fwrite(this->json.dump(1, '\t').c_str(), 1, this->json.dump(1, '\t').size(), file); fclose(file);