OpenTTD
bootstrap_gui.cpp
Go to the documentation of this file.
1 /* $Id: bootstrap_gui.cpp 26538 2014-04-28 21:06:51Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "base_media_base.h"
14 #include "blitter/factory.hpp"
15 
16 #if defined(ENABLE_NETWORK) && defined(WITH_FREETYPE)
17 
18 #include "core/geometry_func.hpp"
19 #include "fontcache.h"
20 #include "gfx_func.h"
21 #include "network/network.h"
23 #include "openttd.h"
24 #include "strings_func.h"
25 #include "video/video_driver.hpp"
26 #include "window_func.h"
27 
29 
30 #include "table/strings.h"
31 
32 #include "safeguards.h"
33 
35 static const struct NWidgetPart _background_widgets[] = {
36  NWidget(WWT_PANEL, COLOUR_DARK_BLUE, WID_BB_BACKGROUND), SetResize(1, 1),
37 };
38 
43  WDP_MANUAL, NULL, 0, 0,
45  0,
46  _background_widgets, lengthof(_background_widgets)
47 );
48 
50 class BootstrapBackground : public Window {
51 public:
52  BootstrapBackground() : Window(&_background_desc)
53  {
54  this->InitNested(0);
56  ResizeWindow(this, _screen.width, _screen.height);
57  }
58 
59  virtual void DrawWidget(const Rect &r, int widget) const
60  {
61  GfxFillRect(r.left, r.top, r.right, r.bottom, 4, FILLRECT_OPAQUE);
62  GfxFillRect(r.left, r.top, r.right, r.bottom, 0, FILLRECT_CHECKER);
63  }
64 };
65 
68  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
69  NWidget(WWT_PANEL, COLOUR_GREY, WID_NCDS_BACKGROUND),
71  EndContainer(),
72 };
73 
76  WDP_CENTER, NULL, 0, 0,
78  WDF_MODAL,
79  _nested_boostrap_download_status_window_widgets, lengthof(_nested_boostrap_download_status_window_widgets)
80 );
81 
82 
85 public:
88  {
89  }
90 
91  virtual void OnDownloadComplete(ContentID cid)
92  {
93  /* We have completed downloading. We can trigger finding the right set now. */
95 
96  /* And continue going into the menu. */
97  _game_mode = GM_MENU;
98 
99  /* _exit_game is used to break out of the outer video driver's MainLoop. */
100  _exit_game = true;
101  delete this;
102  }
103 };
104 
108  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MISSING_GRAPHICS_SET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
109  EndContainer(),
110  NWidget(WWT_PANEL, COLOUR_GREY),
113  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_YES), SetDataTip(STR_MISSING_GRAPHICS_YES_DOWNLOAD, STR_NULL),
114  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BAFD_NO), SetDataTip(STR_MISSING_GRAPHICS_NO_QUIT, STR_NULL),
115  EndContainer(),
116  EndContainer(),
117 };
118 
121  WDP_CENTER, NULL, 0, 0,
123  0,
124  _bootstrap_query_widgets, lengthof(_bootstrap_query_widgets)
125 );
126 
130 
131 public:
133  BootstrapAskForDownloadWindow() : Window(&_bootstrap_query_desc)
134  {
137  }
138 
141  {
143  }
144 
145  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
146  {
147  /* We cache the button size. This is safe as no reinit can happen here. */
148  if (this->button_size.width == 0) {
149  this->button_size = maxdim(GetStringBoundingBox(STR_MISSING_GRAPHICS_YES_DOWNLOAD), GetStringBoundingBox(STR_MISSING_GRAPHICS_NO_QUIT));
150  this->button_size.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
151  this->button_size.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
152  }
153 
154  switch (widget) {
155  case WID_BAFD_QUESTION:
156  /* The question is twice as wide as the buttons, and determine the height based on the width. */
157  size->width = this->button_size.width * 2;
158  size->height = GetStringHeight(STR_MISSING_GRAPHICS_SET_MESSAGE, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT) + WD_FRAMETEXT_BOTTOM + WD_FRAMETEXT_TOP;
159  break;
160 
161  case WID_BAFD_YES:
162  case WID_BAFD_NO:
163  *size = this->button_size;
164  break;
165  }
166  }
167 
168  virtual void DrawWidget(const Rect &r, int widget) const
169  {
170  if (widget != 0) return;
171 
172  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);
173  }
174 
175  virtual void OnClick(Point pt, int widget, int click_count)
176  {
177  switch (widget) {
178  case WID_BAFD_YES:
179  /* We got permission to connect! Yay! */
181  break;
182 
183  case WID_BAFD_NO:
184  _exit_game = true;
185  break;
186 
187  default:
188  break;
189  }
190  }
191 
192  virtual void OnConnect(bool success)
193  {
194  /* Once connected, request the metadata. */
196  }
197 
198  virtual void OnReceiveContentInfo(const ContentInfo *ci)
199  {
200  /* And once the meta data is received, start downloading it. */
203  delete this;
204  }
205 };
206 
207 #endif /* defined(ENABLE_NETWORK) && defined(WITH_FREETYPE) */
208 
216 {
217  if (BaseGraphics::GetUsedSet() != NULL) return true;
218 
219  /* No user interface, bail out with an error. */
220  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) goto failure;
221 
222  /* If there is no network or no freetype, then there is nothing we can do. Go straight to failure. */
223 #if defined(ENABLE_NETWORK) && defined(WITH_FREETYPE) && (defined(WITH_FONTCONFIG) || defined(WIN32) || defined(__APPLE__))
224  if (!_network_available) goto failure;
225 
226  /* First tell the game we're bootstrapping. */
227  _game_mode = GM_BOOTSTRAP;
228 
229  /* Initialise the freetype font code. */
231  /* Next "force" finding a suitable freetype font as the local font is missing. */
232  CheckForMissingGlyphs(false);
233 
234  /* Initialise the palette. The biggest step is 'faking' some recolour sprites.
235  * This way the mauve and gray colours work and we can show the user interface. */
236  GfxInitPalettes();
237  static const int offsets[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0x04, 0x08 };
238  for (uint i = 0; i != 16; i++) {
239  for (int j = 0; j < 8; j++) {
240  _colour_gradient[i][j] = offsets[i] + j;
241  }
242  }
243 
244  /* Finally ask the question. */
245  new BootstrapBackground();
247 
248  /* Process the user events. */
250 
251  /* _exit_game is used to get out of the video driver's main loop.
252  * In case GM_BOOTSTRAP is still set we did not exit it via the
253  * "download complete" event, so it was a manual exit. Obey it. */
254  _exit_game = _game_mode == GM_BOOTSTRAP;
255  if (_exit_game) return false;
256 
257  /* Try to probe the graphics. Should work this time. */
258  if (!BaseGraphics::SetSet(NULL)) goto failure;
259 
260  /* Finally we can continue heading for the menu. */
261  _game_mode = GM_MENU;
262  return true;
263 #endif
264 
265  /* Failure to get enough working to get a graphics set. */
266 failure:
267  usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 4.1 of readme.txt.");
268  return false;
269 }
Functions related to OTTD&#39;s strings.
virtual void DrawWidget(const Rect &r, int widget) const
Draw the contents of a nested widget.
The content consists of base graphics.
Definition: tcp_content.h:27
void CheckForMissingGlyphs(bool base_font, MissingGlyphSearcher *searcher)
Check whether the currently loaded language pack uses characters that the currently loaded font does ...
Definition: strings.cpp:2089
Base of all video drivers.
ResizeInfo resize
Resize information.
Definition: window_gui.h:317
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1843
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen...
Definition: gfx.cpp:112
bool HandleBootstrap()
Handle all procedures for bootstrapping OpenTTD without a base graphics set.
High level window description.
Definition: window_gui.h:168
Bootstrap; Window numbers:
Definition: window_type.h:639
WindowFlags flags
Window flags.
Definition: window_gui.h:305
static uint FindSets()
Do the scan for files.
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:64
Horizontal container.
Definition: widget_type.h:75
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:53
Callbacks for notifying others about incoming data.
void Connect()
Connect with the content server.
BootstrapAskForDownloadWindow()
Start listening to the content client events.
Generic functions for replacing base data (graphics, sounds).
BootstrapContentDownloadStatusWindow()
Simple call the constructor of the superclass.
static const NWidgetPart _bootstrap_query_widgets[]
The widgets for the query.
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition: gfx.cpp:547
virtual void OnConnect(bool success)
Callback for when the connection has finished.
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
static NWidgetPart SetMinimalTextLines(uint8 lines, uint8 spacing, FontSize size=FS_NORMAL)
Widget part function for setting the minimal text lines.
Definition: widget_type.h:965
Dimension button_size
The dimension of the button.
Background of the window.
#define CLRBITS(x, y)
Clears several bits in a variable.
static bool SetSet(const char *name)
Set the set to be used.
ContentID
Unique identifier for the content.
Definition: tcp_content.h:53
static void InitializeUnicodeGlyphMap()
Initialize the glyph map.
Definition: fontcache.h:161
The window for the query.
The question whether to download.
bool _network_available
is network mode available?
Definition: network.cpp:58
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Data structure for an opened window.
Definition: window_gui.h:271
The background for the game.
Functions to read fonts from files and cache them.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1833
Bottom offset of the text of the frame.
Definition: window_gui.h:75
void AddCallback(ContentCallback *cb)
Add a callback to this class.
Invisible widget that takes some space.
Definition: widget_type.h:79
Fill rectangle with a single colour.
Definition: gfx_type.h:283
static WindowDesc _bootstrap_query_desc(WDP_CENTER, NULL, 0, 0, WC_CONFIRM_POPUP_QUERY, WC_NONE, 0, _bootstrap_query_widgets, lengthof(_bootstrap_query_widgets))
The window description for the query.
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
Functions related to the gfx engine.
static WindowDesc _background_desc(WDP_MANUAL, NULL, 0, 0, WC_BOOTSTRAP, WC_NONE, 0, _background_widgets, lengthof(_background_widgets))
Window description for the background window to prevent smearing.
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
Update size and resize step of a widget in the window.
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
Definition of base types and functions in a cross-platform compatible way.
Center both horizontally and vertically.
Definition: gfx_func.h:108
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:90
A number of safeguards to prevent using unsafe methods.
ContentID id
Unique (server side) ID for the content.
Definition: tcp_content.h:70
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Geometry functions.
Query popup confirm for bootstrap.
Definition: window_type.h:27
Simple depressed panel.
Definition: widget_type.h:50
void RequestContentList(ContentType type)
Request the content list for the given type.
Background of the window.
Center the window.
Definition: window_gui.h:157
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:65
Types related to the bootstrap widgets.
Some generic types.
Basic functions/variables used all over the place.
Right offset of the text of the frame.
Definition: window_gui.h:73
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
Top offset of the text of the frame.
Definition: window_gui.h:74
Left offset of the text of the frame.
Definition: window_gui.h:72
static const NWidgetPart _nested_boostrap_download_status_window_widgets[]
Nested widgets for the download window.
The window is a modal child of some other window, meaning the parent is &#39;inactive&#39;.
Definition: window_gui.h:211
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:699
Network status window; Window numbers:
Definition: window_type.h:487
An affirmative answer to the question.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Draw only every second pixel, used for greying-out.
Definition: gfx_type.h:284
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
void Select(ContentID cid)
Select a specific content id.
User interface for downloading files.
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
void RemoveCallback(ContentCallback *cb)
Remove a callback.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
static WindowDesc _bootstrap_download_status_window_desc(WDP_CENTER, NULL, 0, 0, WC_NETWORK_STATUS_WINDOW, WC_NONE, WDF_MODAL, _nested_boostrap_download_status_window_widgets, lengthof(_nested_boostrap_download_status_window_widgets))
Window description for the download window.
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
virtual void OnReceiveContentInfo(const ContentInfo *ci)
We received a content info.
virtual void MainLoop()=0
Perform the actual drawing.
Window for showing the download status of content.
virtual void DrawWidget(const Rect &r, int widget) const
Draw the contents of a nested widget.
Coordinates of a point in 2D.
An negative answer to the question.
Popup with confirm question; Window numbers:
Definition: window_type.h:125
~BootstrapAskForDownloadWindow()
Stop listening to the content client events.
Specification of a rectangle with absolute coordinates of all edges.
Window functions not directly related to making/drawing windows.
Base window for showing the download status of content.
Manually align the window (so no automatic location finding)
Definition: window_gui.h:155
static const struct NWidgetPart _background_widgets[]
Widgets for the background window to prevent smearing.
virtual void OnDownloadComplete(ContentID cid)
We have finished downloading a file.
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2116
Window white border counter bit mask.
Definition: window_gui.h:242
Dimensions (a width and height) of a rectangle in 2D.
Container for all important information about a piece of content.
Definition: tcp_content.h:58
Factory to &#39;query&#39; all available blitters.
static const GraphicsSet * GetUsedSet()
Return the used set.
ClientNetworkContentSocketHandler _network_content_client
The client we use to connect to the server.
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:620