2011-11-17 21:17:17 +00:00
|
|
|
/*
|
|
|
|
* 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 bootstrap_gui.cpp Barely used user interface for bootstrapping OpenTTD, i.e. downloading the required content. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "base_media_base.h"
|
|
|
|
#include "blitter/factory.hpp"
|
2011-11-17 21:29:15 +00:00
|
|
|
|
2021-02-13 21:51:18 +00:00
|
|
|
#if defined(WITH_FREETYPE) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
|
2011-11-17 21:29:15 +00:00
|
|
|
|
2011-11-17 21:17:17 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2021-03-13 11:26:06 +00:00
|
|
|
#include "error.h"
|
2011-11-17 21:17:17 +00:00
|
|
|
#include "fontcache.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
#include "network/network_content_gui.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "video/video_driver.hpp"
|
|
|
|
#include "window_func.h"
|
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/bootstrap_widget.h"
|
|
|
|
|
2011-11-17 21:17:17 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-11-17 21:17:17 +00:00
|
|
|
/** Widgets for the background window to prevent smearing. */
|
|
|
|
static const struct NWidgetPart _background_widgets[] = {
|
2011-12-19 20:46:17 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_BLUE, WID_BB_BACKGROUND), SetResize(1, 1),
|
2011-11-17 21:17:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Window description for the background window to prevent smearing.
|
|
|
|
*/
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _background_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_MANUAL, nullptr, 0, 0,
|
2011-11-17 21:17:17 +00:00
|
|
|
WC_BOOTSTRAP, WC_NONE,
|
|
|
|
0,
|
|
|
|
_background_widgets, lengthof(_background_widgets)
|
|
|
|
);
|
|
|
|
|
|
|
|
/** The background for the game. */
|
|
|
|
class BootstrapBackground : public Window {
|
|
|
|
public:
|
2013-05-26 19:23:42 +00:00
|
|
|
BootstrapBackground() : Window(&_background_desc)
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(0);
|
2011-12-15 19:54:23 +00:00
|
|
|
CLRBITS(this->flags, WF_WHITE_BORDER);
|
2011-11-17 21:17:17 +00:00
|
|
|
ResizeWindow(this, _screen.width, _screen.height);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
GfxFillRect(r.left, r.top, r.right, r.bottom, 4, FILLRECT_OPAQUE);
|
|
|
|
GfxFillRect(r.left, r.top, r.right, r.bottom, 0, FILLRECT_CHECKER);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-13 11:26:06 +00:00
|
|
|
/** Nested widgets for the error window. */
|
|
|
|
static const NWidgetPart _nested_bootstrap_errmsg_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_GREY, WID_BEM_CAPTION), SetDataTip(STR_MISSING_GRAPHICS_ERROR_TITLE, STR_NULL),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BEM_MESSAGE), EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BEM_QUIT), SetDataTip(STR_MISSING_GRAPHICS_ERROR_QUIT, STR_NULL), SetFill(1, 0),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Window description for the error window. */
|
|
|
|
static WindowDesc _bootstrap_errmsg_desc(
|
|
|
|
WDP_CENTER, nullptr, 0, 0,
|
|
|
|
WC_BOOTSTRAP, WC_NONE,
|
|
|
|
WDF_MODAL,
|
|
|
|
_nested_bootstrap_errmsg_widgets, lengthof(_nested_bootstrap_errmsg_widgets)
|
|
|
|
);
|
|
|
|
|
|
|
|
/** The window for a failed bootstrap. */
|
|
|
|
class BootstrapErrorWindow : public Window {
|
|
|
|
public:
|
|
|
|
BootstrapErrorWindow() : Window(&_bootstrap_errmsg_desc)
|
|
|
|
{
|
|
|
|
this->InitNested(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BootstrapErrorWindow()
|
|
|
|
{
|
|
|
|
_exit_game = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
|
|
|
{
|
|
|
|
if (widget == WID_BEM_MESSAGE) {
|
|
|
|
*size = GetStringBoundingBox(STR_MISSING_GRAPHICS_ERROR);
|
|
|
|
size->height = GetStringHeight(STR_MISSING_GRAPHICS_ERROR, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT) + WD_FRAMETEXT_BOTTOM + WD_FRAMETEXT_TOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
|
|
|
{
|
|
|
|
if (widget == WID_BEM_MESSAGE) {
|
|
|
|
DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMETEXT_TOP, r.bottom - WD_FRAMETEXT_BOTTOM, STR_MISSING_GRAPHICS_ERROR, TC_FROMSTRING, SA_CENTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
|
|
|
{
|
|
|
|
if (widget == WID_BEM_QUIT) {
|
|
|
|
_exit_game = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-17 21:17:17 +00:00
|
|
|
/** Nested widgets for the download window. */
|
2021-03-13 14:09:09 +00:00
|
|
|
static const NWidgetPart _nested_bootstrap_download_status_window_widgets[] = {
|
2011-11-17 21:17:17 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2011-12-16 18:02:27 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_NCDS_BACKGROUND),
|
2011-11-17 21:17:17 +00:00
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(350, 0), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 30),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Window description for the download window */
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _bootstrap_download_status_window_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_CENTER, nullptr, 0, 0,
|
2011-11-17 21:17:17 +00:00
|
|
|
WC_NETWORK_STATUS_WINDOW, WC_NONE,
|
|
|
|
WDF_MODAL,
|
2021-03-13 14:09:09 +00:00
|
|
|
_nested_bootstrap_download_status_window_widgets, lengthof(_nested_bootstrap_download_status_window_widgets)
|
2011-11-17 21:17:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/** Window for showing the download status of content */
|
|
|
|
struct BootstrapContentDownloadStatusWindow : public BaseNetworkContentDownloadStatusWindow {
|
|
|
|
public:
|
|
|
|
/** Simple call the constructor of the superclass. */
|
|
|
|
BootstrapContentDownloadStatusWindow() : BaseNetworkContentDownloadStatusWindow(&_bootstrap_download_status_window_desc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-13 11:26:06 +00:00
|
|
|
~BootstrapContentDownloadStatusWindow()
|
|
|
|
{
|
|
|
|
/* If we are not set to exit the game, it means the bootstrap failed. */
|
|
|
|
if (!_exit_game) {
|
|
|
|
new BootstrapErrorWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnDownloadComplete(ContentID cid) override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
/* We have completed downloading. We can trigger finding the right set now. */
|
|
|
|
BaseGraphics::FindSets();
|
|
|
|
|
|
|
|
/* And continue going into the menu. */
|
|
|
|
_game_mode = GM_MENU;
|
|
|
|
|
|
|
|
/* _exit_game is used to break out of the outer video driver's MainLoop. */
|
|
|
|
_exit_game = true;
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** The widgets for the query. It has no close box as that sprite does not exist yet. */
|
|
|
|
static const NWidgetPart _bootstrap_query_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MISSING_GRAPHICS_SET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BAFD_QUESTION), EndContainer(),
|
2011-11-17 21:17:17 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_YES), SetDataTip(STR_MISSING_GRAPHICS_YES_DOWNLOAD, STR_NULL),
|
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_NO), SetDataTip(STR_MISSING_GRAPHICS_NO_QUIT, STR_NULL),
|
2011-11-17 21:17:17 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** The window description for the query. */
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _bootstrap_query_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_CENTER, nullptr, 0, 0,
|
2011-11-17 21:17:17 +00:00
|
|
|
WC_CONFIRM_POPUP_QUERY, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
0,
|
2011-11-17 21:17:17 +00:00
|
|
|
_bootstrap_query_widgets, lengthof(_bootstrap_query_widgets)
|
|
|
|
);
|
|
|
|
|
|
|
|
/** The window for the query. It can't use the generic query window as that uses sprites that don't exist yet. */
|
|
|
|
class BootstrapAskForDownloadWindow : public Window, ContentCallback {
|
|
|
|
Dimension button_size; ///< The dimension of the button
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Start listening to the content client events. */
|
2013-05-26 19:23:42 +00:00
|
|
|
BootstrapAskForDownloadWindow() : Window(&_bootstrap_query_desc)
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(WN_CONFIRM_POPUP_QUERY_BOOTSTRAP);
|
2011-11-17 21:17:17 +00:00
|
|
|
_network_content_client.AddCallback(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Stop listening to the content client events. */
|
|
|
|
~BootstrapAskForDownloadWindow()
|
|
|
|
{
|
|
|
|
_network_content_client.RemoveCallback(this);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
/* We cache the button size. This is safe as no reinit can happen here. */
|
|
|
|
if (this->button_size.width == 0) {
|
|
|
|
this->button_size = maxdim(GetStringBoundingBox(STR_MISSING_GRAPHICS_YES_DOWNLOAD), GetStringBoundingBox(STR_MISSING_GRAPHICS_NO_QUIT));
|
|
|
|
this->button_size.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
|
|
|
|
this->button_size.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BAFD_QUESTION:
|
2011-11-17 21:17:17 +00:00
|
|
|
/* The question is twice as wide as the buttons, and determine the height based on the width. */
|
|
|
|
size->width = this->button_size.width * 2;
|
|
|
|
size->height = GetStringHeight(STR_MISSING_GRAPHICS_SET_MESSAGE, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT) + WD_FRAMETEXT_BOTTOM + WD_FRAMETEXT_TOP;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BAFD_YES:
|
|
|
|
case WID_BAFD_NO:
|
2011-11-17 21:17:17 +00:00
|
|
|
*size = this->button_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
if (widget != 0) return;
|
|
|
|
|
|
|
|
DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMETEXT_TOP, r.bottom - WD_FRAMETEXT_BOTTOM, STR_MISSING_GRAPHICS_SET_MESSAGE, TC_FROMSTRING, SA_CENTER);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BAFD_YES:
|
2011-11-17 21:17:17 +00:00
|
|
|
/* We got permission to connect! Yay! */
|
|
|
|
_network_content_client.Connect();
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BAFD_NO:
|
2011-11-17 21:17:17 +00:00
|
|
|
_exit_game = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnConnect(bool success) override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
/* Once connected, request the metadata. */
|
|
|
|
_network_content_client.RequestContentList(CONTENT_TYPE_BASE_GRAPHICS);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnReceiveContentInfo(const ContentInfo *ci) override
|
2011-11-17 21:17:17 +00:00
|
|
|
{
|
|
|
|
/* And once the meta data is received, start downloading it. */
|
|
|
|
_network_content_client.Select(ci->id);
|
|
|
|
new BootstrapContentDownloadStatusWindow();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Remove: ENABLE_NETWORK switch
This switch has been a pain for years. Often disabling broke
compilation, as no developer compiles OpenTTD without, neither do
any of our official binaries.
Additionaly, it has grown so hugely in our codebase, that it
clearly shows that the current solution was a poor one. 350+
instances of "#ifdef ENABLE_NETWORK" were in the code, of which
only ~30 in the networking code itself. The rest were all around
the code to do the right thing, from GUI to NewGRF.
A more proper solution would be to stub all the functions, and
make sure the rest of the code can simply assume network is
available. This was also partially done, and most variables were
correct if networking was disabled. Despite that, often the #ifdefs
were still used.
With the recent removal of DOS, there is also no platform anymore
which we support where networking isn't working out-of-the-box.
All in all, it is time to remove the ENABLE_NETWORK switch. No
replacement is planned, but if you feel we really need this option,
we welcome any Pull Request which implements this in a way that
doesn't crawl through the code like this diff shows we used to.
2019-03-20 16:01:13 +00:00
|
|
|
#endif /* defined(WITH_FREETYPE) */
|
2011-11-17 21:29:15 +00:00
|
|
|
|
2011-11-17 21:17:17 +00:00
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Handle all procedures for bootstrapping OpenTTD without a base graphics set.
|
2011-11-17 21:17:17 +00:00
|
|
|
* This requires all kinds of trickery that is needed to avoid the use of
|
|
|
|
* sprites from the base graphics set which are pretty interwoven.
|
|
|
|
* @return True if a base set exists, otherwise false.
|
|
|
|
*/
|
|
|
|
bool HandleBootstrap()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (BaseGraphics::GetUsedSet() != nullptr) return true;
|
2011-11-17 21:17:17 +00:00
|
|
|
|
|
|
|
/* No user interface, bail out with an error. */
|
2014-01-02 22:41:58 +00:00
|
|
|
if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) goto failure;
|
2011-11-17 21:17:17 +00:00
|
|
|
|
|
|
|
/* If there is no network or no freetype, then there is nothing we can do. Go straight to failure. */
|
2021-02-13 21:51:18 +00:00
|
|
|
#if (defined(_WIN32) && defined(WITH_UNISCRIBE)) || (defined(WITH_FREETYPE) && (defined(WITH_FONTCONFIG) || defined(__APPLE__))) || defined(WITH_COCOA)
|
2011-11-17 21:17:17 +00:00
|
|
|
if (!_network_available) goto failure;
|
|
|
|
|
|
|
|
/* First tell the game we're bootstrapping. */
|
|
|
|
_game_mode = GM_BOOTSTRAP;
|
|
|
|
|
|
|
|
/* Initialise the freetype font code. */
|
|
|
|
InitializeUnicodeGlyphMap();
|
|
|
|
/* Next "force" finding a suitable freetype font as the local font is missing. */
|
2011-11-20 11:52:11 +00:00
|
|
|
CheckForMissingGlyphs(false);
|
2011-11-17 21:17:17 +00:00
|
|
|
|
|
|
|
/* Initialise the palette. The biggest step is 'faking' some recolour sprites.
|
|
|
|
* This way the mauve and gray colours work and we can show the user interface. */
|
|
|
|
GfxInitPalettes();
|
|
|
|
static const int offsets[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0x04, 0x08 };
|
|
|
|
for (uint i = 0; i != 16; i++) {
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
_colour_gradient[i][j] = offsets[i] + j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally ask the question. */
|
|
|
|
new BootstrapBackground();
|
|
|
|
new BootstrapAskForDownloadWindow();
|
|
|
|
|
|
|
|
/* Process the user events. */
|
2014-04-28 21:06:51 +00:00
|
|
|
VideoDriver::GetInstance()->MainLoop();
|
2011-11-17 21:17:17 +00:00
|
|
|
|
|
|
|
/* _exit_game is used to get out of the video driver's main loop.
|
|
|
|
* In case GM_BOOTSTRAP is still set we did not exit it via the
|
|
|
|
* "download complete" event, so it was a manual exit. Obey it. */
|
|
|
|
_exit_game = _game_mode == GM_BOOTSTRAP;
|
|
|
|
if (_exit_game) return false;
|
|
|
|
|
|
|
|
/* Try to probe the graphics. Should work this time. */
|
2020-12-05 14:03:16 +00:00
|
|
|
if (!BaseGraphics::SetSet({})) goto failure;
|
2011-11-17 21:17:17 +00:00
|
|
|
|
|
|
|
/* Finally we can continue heading for the menu. */
|
|
|
|
_game_mode = GM_MENU;
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Failure to get enough working to get a graphics set. */
|
|
|
|
failure:
|
2019-12-26 18:36:53 +00:00
|
|
|
usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 1.4 of README.md.");
|
2011-11-17 21:17:17 +00:00
|
|
|
return false;
|
|
|
|
}
|