(svn r23240) -Codechange: rework the code of the download status window

This commit is contained in:
rubidium 2011-11-17 21:06:41 +00:00
parent 1de7127d62
commit cc85ef493a
7 changed files with 126 additions and 63 deletions

View File

@ -465,6 +465,7 @@
<ClInclude Include="..\src\network\network_base.h" />
<ClInclude Include="..\src\network\network_client.h" />
<ClInclude Include="..\src\network\network_content.h" />
<ClInclude Include="..\src\network\network_content_gui.h" />
<ClInclude Include="..\src\network\network_func.h" />
<ClInclude Include="..\src\network\network_gamelist.h" />
<ClInclude Include="..\src\network\network_gui.h" />

View File

@ -615,6 +615,9 @@
<ClInclude Include="..\src\network\network_content.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\network\network_content_gui.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\network\network_func.h">
<Filter>Header Files</Filter>
</ClInclude>

View File

@ -1134,6 +1134,10 @@
RelativePath=".\..\src\network\network_content.h"
>
</File>
<File
RelativePath=".\..\src\network\network_content_gui.h"
>
</File>
<File
RelativePath=".\..\src\network\network_func.h"
>

View File

@ -1131,6 +1131,10 @@
RelativePath=".\..\src\network\network_content.h"
>
</File>
<File
RelativePath=".\..\src\network\network_content_gui.h"
>
</File>
<File
RelativePath=".\..\src\network\network_func.h"
>

View File

@ -198,6 +198,7 @@ network/network_admin.h
network/network_base.h
network/network_client.h
network/network_content.h
network/network_content_gui.h
network/network_func.h
network/network_gamelist.h
network/network_gui.h

View File

@ -20,17 +20,11 @@
#include "../sortlist_type.h"
#include "../querystring_gui.h"
#include "../core/geometry_func.hpp"
#include "network_content.h"
#include "network_content_gui.h"
#include "table/strings.h"
#include "../table/sprites.h"
/** Widgets used by this window */
enum DownloadStatusWindowWidgets {
NCDSWW_BACKGROUND, ///< Background
NCDSWW_CANCELOK, ///< Cancel/OK button
};
/** Nested widgets for the download window. */
static const NWidgetPart _nested_network_content_download_status_window_widgets[] = {
NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
@ -53,34 +47,75 @@ static const WindowDesc _network_content_download_status_window_desc(
_nested_network_content_download_status_window_widgets, lengthof(_nested_network_content_download_status_window_widgets)
);
BaseNetworkContentDownloadStatusWindow::BaseNetworkContentDownloadStatusWindow(const WindowDesc *desc) :
cur_id(UINT32_MAX)
{
_network_content_client.AddCallback(this);
_network_content_client.DownloadSelectedContent(this->total_files, this->total_bytes);
this->InitNested(desc, 0);
}
BaseNetworkContentDownloadStatusWindow::~BaseNetworkContentDownloadStatusWindow()
{
_network_content_client.RemoveCallback(this);
}
/* virtual */ void BaseNetworkContentDownloadStatusWindow::DrawWidget(const Rect &r, int widget) const
{
if (widget != NCDSWW_BACKGROUND) return;
/* Draw nice progress bar :) */
DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE);
int y = r.top + 20;
SetDParam(0, this->downloaded_bytes);
SetDParam(1, this->total_bytes);
SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes);
DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_HOR_CENTER);
StringID str;
if (this->downloaded_bytes == this->total_bytes) {
str = STR_CONTENT_DOWNLOAD_COMPLETE;
} else if (!StrEmpty(this->name)) {
SetDParamStr(0, this->name);
SetDParam(1, this->downloaded_files);
SetDParam(2, this->total_files);
str = STR_CONTENT_DOWNLOAD_FILE;
} else {
str = STR_CONTENT_DOWNLOAD_INITIALISE;
}
y += FONT_HEIGHT_NORMAL + 5;
DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
}
/* virtual */ void BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(const ContentInfo *ci, int bytes)
{
if (ci->id != this->cur_id) {
strecpy(this->name, ci->filename, lastof(this->name));
this->cur_id = ci->id;
this->downloaded_files++;
}
this->downloaded_bytes += bytes;
this->SetDirty();
}
/** Window for showing the download status of content */
struct NetworkContentDownloadStatusWindow : public Window, ContentCallback {
struct NetworkContentDownloadStatusWindow : public BaseNetworkContentDownloadStatusWindow {
private:
ClientNetworkContentSocketHandler *connection; ///< Our connection with the content server
SmallVector<ContentType, 4> receivedTypes; ///< Types we received so we can update their cache
uint total_files; ///< Number of files to download
uint downloaded_files; ///< Number of files downloaded
uint total_bytes; ///< Number of bytes to download
uint downloaded_bytes; ///< Number of bytes downloaded
uint32 cur_id; ///< The current ID of the downloaded file
char name[48]; ///< The current name of the downloaded file
public:
/**
* Create a new download window based on a list of content information
* with flags whether to download them or not.
*/
NetworkContentDownloadStatusWindow() :
cur_id(UINT32_MAX)
NetworkContentDownloadStatusWindow() : BaseNetworkContentDownloadStatusWindow(&_network_content_download_status_window_desc)
{
this->parent = FindWindowById(WC_NETWORK_WINDOW, 1);
_network_content_client.AddCallback(this);
_network_content_client.DownloadSelectedContent(this->total_files, this->total_bytes);
this->InitNested(&_network_content_download_status_window_desc, 0);
}
/** Free whatever we've allocated */
@ -157,36 +192,6 @@ public:
/* Always invalidate the download window; tell it we are going to be gone */
InvalidateWindowData(WC_NETWORK_WINDOW, 1, 2);
_network_content_client.RemoveCallback(this);
}
virtual void DrawWidget(const Rect &r, int widget) const
{
if (widget != NCDSWW_BACKGROUND) return;
/* Draw nice progress bar :) */
DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE);
int y = r.top + 20;
SetDParam(0, this->downloaded_bytes);
SetDParam(1, this->total_bytes);
SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes);
DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_HOR_CENTER);
StringID str;
if (this->downloaded_bytes == this->total_bytes) {
str = STR_CONTENT_DOWNLOAD_COMPLETE;
} else if (!StrEmpty(this->name)) {
SetDParamStr(0, this->name);
SetDParam(1, this->downloaded_files);
SetDParam(2, this->total_files);
str = STR_CONTENT_DOWNLOAD_FILE;
} else {
str = STR_CONTENT_DOWNLOAD_INITIALISE;
}
y += FONT_HEIGHT_NORMAL + 5;
DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
}
virtual void OnClick(Point pt, int widget, int click_count)
@ -199,20 +204,14 @@ public:
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
{
if (ci->id != this->cur_id) {
strecpy(this->name, ci->filename, lastof(this->name));
this->cur_id = ci->id;
this->downloaded_files++;
this->receivedTypes.Include(ci->type);
}
this->downloaded_bytes += bytes;
BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(ci, bytes);
if (ci->id != this->cur_id) this->receivedTypes.Include(ci->type);
/* When downloading is finished change cancel in ok */
if (this->downloaded_bytes == this->total_bytes) {
this->GetWidget<NWidgetCore>(NCDSWW_CANCELOK)->widget_data = STR_BUTTON_OK;
}
this->SetDirty();
}
};

View File

@ -0,0 +1,51 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD 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, version 2.
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file network_content_gui.h User interface for downloading files. */
#ifndef NETWORK_CONTENT_GUI_H
#define NETWORK_CONTENT_GUI_H
#include "network_content.h"
#include "../window_gui.h"
/** Widgets used by this window */
enum NetworkContentDownloadStatusWindowWidgets {
NCDSWW_BACKGROUND, ///< Background
NCDSWW_CANCELOK, ///< (Optional) Cancel/OK button
};
/** Base window for showing the download status of content */
class BaseNetworkContentDownloadStatusWindow : public Window, ContentCallback {
protected:
uint total_bytes; ///< Number of bytes to download
uint downloaded_bytes; ///< Number of bytes downloaded
uint total_files; ///< Number of files to download
uint downloaded_files; ///< Number of files downloaded
uint32 cur_id; ///< The current ID of the downloaded file
char name[48]; ///< The current name of the downloaded file
public:
/**
* Create the window with the given description.
* @param desc The description of the window.
*/
BaseNetworkContentDownloadStatusWindow(const WindowDesc *desc);
/**
* Free everything associated with this window.
*/
~BaseNetworkContentDownloadStatusWindow();
virtual void DrawWidget(const Rect &r, int widget) const;
virtual void OnDownloadProgress(const ContentInfo *ci, int bytes);
};
#endif /* NETWORK_CONTENT_GUI_H */