From afa26ccf97609b4a53c747d487aa487b64b9a23a Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 26 Jul 2019 01:19:34 +0800 Subject: [PATCH] Show download size for each option of game assets --- src/states_screens/download_assets.cpp | 25 +++++++++++++++-- src/states_screens/download_assets.hpp | 2 ++ src/utils/download_assets_size.hpp | 38 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/utils/download_assets_size.hpp diff --git a/src/states_screens/download_assets.cpp b/src/states_screens/download_assets.cpp index 6fef24fbc..04455b953 100644 --- a/src/states_screens/download_assets.cpp +++ b/src/states_screens/download_assets.cpp @@ -26,6 +26,7 @@ #include "io/file_manager.hpp" #include "online/http_request.hpp" #include "states_screens/state_manager.hpp" +#include "utils/download_assets_size.hpp" #include "utils/string_utils.hpp" #include "utils/translation.hpp" #include "main_loop.hpp" @@ -45,11 +46,26 @@ void DownloadAssets::beforeAddingWidget() } // beforeAddingWidget +// ----------------------------------------------------------------------------- +void DownloadAssets::updateDownloadSize() +{ + m_progress->setValue(0); + core::stringw unit=""; + unsigned n = getDownloadAssetsSize(m_all_tracks->getState(), + m_hd_textures->getState()); + float f = ((int)(n/1024.0f/1024.0f*10.0f+0.5f))/10.0f; + char s[32]; + sprintf(s, "%.1f", f); + unit = _("%s MB", s); + // I18N: File size of game assets or addons downloading + core::stringw size = _("Size: %s", unit.c_str()); + m_progress->setText(size); +} // updateDownloadSize + // ----------------------------------------------------------------------------- void DownloadAssets::init() { m_progress = getWidget("progress"); - m_progress->setVisible(false); m_all_tracks = getWidget("all-tracks"); m_all_tracks->setActive(true); m_all_tracks->setVisible(true); @@ -58,6 +74,7 @@ void DownloadAssets::init() m_hd_textures->setActive(true); m_hd_textures->setVisible(true); m_hd_textures->setState(false); + updateDownloadSize(); m_ok = getWidget("ok"); m_ok->setActive(true); m_ok->setVisible(true); @@ -89,7 +106,9 @@ void DownloadAssets::eventCallback(Widget* widget, const std::string& name, { if (m_downloading_now) return; - if (name == "buttons") + if (name == "all-tracks" || name == "hd-textures") + updateDownloadSize(); + else if (name == "buttons") { const std::string& button = getWidget("buttons") ->getSelectionIDString(PLAYER_ID_GAME_MASTER); @@ -97,8 +116,8 @@ void DownloadAssets::eventCallback(Widget* widget, const std::string& name, { m_downloading_now = true; m_ok->setActive(false); + m_progress->setText(""); m_progress->setValue(0); - m_progress->setVisible(true); m_all_tracks->setActive(false); m_hd_textures->setActive(false); std::string download_url = stk_config->m_assets_download_url; diff --git a/src/states_screens/download_assets.hpp b/src/states_screens/download_assets.hpp index 2dbb2d858..b01a1aaac 100644 --- a/src/states_screens/download_assets.hpp +++ b/src/states_screens/download_assets.hpp @@ -60,6 +60,8 @@ private: Online::HTTPRequest* m_download_request; std::thread m_download_thread; + + void updateDownloadSize(); public: friend class GUIEngine::ScreenSingleton; diff --git a/src/utils/download_assets_size.hpp b/src/utils/download_assets_size.hpp new file mode 100644 index 000000000..feb2ba934 --- /dev/null +++ b/src/utils/download_assets_size.hpp @@ -0,0 +1,38 @@ +// SuperTuxKart - a fun racing game with go-kart +// +// Copyright (C) 2019 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_DOWNLOAD_ASSETS_SIZE_HPP +#define HEADER_DOWNLOAD_ASSETS_SIZE_HPP +/* Return .zip file size in bytes (as in ls -l) + */ +inline unsigned getDownloadAssetsSize(bool is_full, bool is_hd) +{ + // Todo: generated from some sed script + unsigned full_hd = 188442091; + unsigned full_nonhd = 83487825; + unsigned nonfull_hd = 166310030; + unsigned nonfull_nonhd = 69282428; + if (is_full && is_hd) + return full_hd; + if (is_full && !is_hd) + return full_nonhd; + if (!is_full && is_hd) + return nonfull_hd; + return nonfull_nonhd; +} +#endif