OpenTTD Source: src/help_gui.cpp Source File

OpenTTD Source 15.0-RC2

help_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "gui.h"
12#include "window_gui.h"
13#include "textfile_gui.h"
14#include "fileio_func.h"
15#include "table/control_codes.h"
16#include "string_func.h"
17#include "openttd.h"
18#include "help_gui.h"
19
20#include "widgets/help_widget.h"
21#include "widgets/misc_widget.h"
22
23#include "table/strings.h"
24
25#include "safeguards.h"
26
27static const std::string README_FILENAME = "README.md";
28static const std::string CHANGELOG_FILENAME = "changelog.md";
29static const std::string KNOWN_BUGS_FILENAME = "known-bugs.md";
30static const std::string LICENSE_FILENAME = "COPYING.md";
31static const std::string FONTS_FILENAME = "fonts.md";
32
33static const std::string WEBSITE_LINK = "https://www.openttd.org/";
34static const std::string WIKI_LINK = "https://wiki.openttd.org/";
35static const std::string BUGTRACKER_LINK = "https://bugs.openttd.org/";
36static const std::string COMMUNITY_LINK = "https://community.openttd.org/";
37
39 static constexpr size_t CHANGELOG_VERSIONS_LIMIT = 20;
40
47 static std::optional<std::string> FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
48{
49 static const Searchpath searchpaths[] = {
50 SP_APPLICATION_BUNDLE_DIR, SP_INSTALLATION_DIR, SP_SHARED_DIR, SP_BINARY_DIR, SP_WORKING_DIR
51 };
52
53 for (Searchpath sp : searchpaths) {
54 std::string file_path = FioGetDirectory(sp, subdir);
55 file_path.append(filename);
56 if (FioCheckFileExists(file_path, NO_DIRECTORY)) return file_path;
57 }
58
59 return {};
60}
61
63 struct GameManualTextfileWindow : public TextfileWindow {
64 GameManualTextfileWindow(std::string_view filename, Subdirectory subdir) : TextfileWindow(nullptr, TFT_GAME_MANUAL)
65 {
66 this->ConstructWindow();
67
68 /* Mark the content of these files as trusted. */
69 this->trusted = true;
70
71 auto filepath = FindGameManualFilePath(filename, subdir);
72 /* The user could, in theory, have moved the file. So just show an empty window if that is the case. */
73 if (!filepath.has_value()) {
74 return;
75 }
76
77 this->filepath = filepath.value();
78 this->LoadTextfile(this->filepath, NO_DIRECTORY);
79 this->OnClick({ 0, 0 }, WID_TF_WRAPTEXT, 1);
80 }
81
82 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
83 {
84 if (widget == WID_TF_CAPTION) {
85 return GetString(stringid, this->filename);
86 }
87
88 return this->Window::GetWidgetString(widget, stringid);
89 }
90
91 void AfterLoadText() override
92 {
93 if (this->filename == CHANGELOG_FILENAME) {
94 this->link_anchors.clear();
95 this->AfterLoadChangelog();
96 }
97 this->TextfileWindow::AfterLoadText();
98 }
99
105 void AfterLoadChangelog()
106 {
107 uint versions = 0;
108
109 /* Look for lines beginning with ###, they indicate a release name. */
110 for (size_t line_index = 0; line_index < this->lines.size(); ++line_index) {
111 const Line &line = this->lines[line_index];
112 if (!line.text.starts_with("###")) continue;
113
114 if (versions >= CHANGELOG_VERSIONS_LIMIT) {
115 this->lines.resize(line_index - 2);
116 break;
117 }
118
119 ++versions;
120 }
121 }
122};
123
125 struct HelpWindow : public Window {
126
127 HelpWindow(WindowDesc &desc, WindowNumber number) : Window(desc)
128 {
129 this->InitNested(number);
130
131 this->EnableTextfileButton(README_FILENAME, BASE_DIR, WID_HW_README);
132 this->EnableTextfileButton(CHANGELOG_FILENAME, BASE_DIR, WID_HW_CHANGELOG);
133 this->EnableTextfileButton(KNOWN_BUGS_FILENAME, BASE_DIR, WID_HW_KNOWN_BUGS);
134 this->EnableTextfileButton(LICENSE_FILENAME, BASE_DIR, WID_HW_LICENSE);
135 this->EnableTextfileButton(FONTS_FILENAME, DOCS_DIR, WID_HW_FONTS);
136 }
137
138 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
139 {
140 switch (widget) {
141 case WID_HW_README:
142 new GameManualTextfileWindow(README_FILENAME, BASE_DIR);
143 break;
144 case WID_HW_CHANGELOG:
145 new GameManualTextfileWindow(CHANGELOG_FILENAME, BASE_DIR);
146 break;
147 case WID_HW_KNOWN_BUGS:
148 new GameManualTextfileWindow(KNOWN_BUGS_FILENAME, BASE_DIR);
149 break;
150 case WID_HW_LICENSE:
151 new GameManualTextfileWindow(LICENSE_FILENAME, BASE_DIR);
152 break;
153 case WID_HW_FONTS:
154 new GameManualTextfileWindow(FONTS_FILENAME, DOCS_DIR);
155 break;
156 case WID_HW_WEBSITE:
157 OpenBrowser(WEBSITE_LINK);
158 break;
159 case WID_HW_WIKI:
160 OpenBrowser(WIKI_LINK);
161 break;
162 case WID_HW_BUGTRACKER:
163 OpenBrowser(BUGTRACKER_LINK);
164 break;
165 case WID_HW_COMMUNITY:
166 OpenBrowser(COMMUNITY_LINK);
167 break;
168 }
169 }
170
171private:
172 void EnableTextfileButton(std::string_view filename, Subdirectory subdir, WidgetID button_widget)
173 {
174 this->GetWidget<NWidgetLeaf>(button_widget)->SetDisabled(!FindGameManualFilePath(filename, subdir).has_value());
175 }
176};
177
178static constexpr std::initializer_list<NWidgetPart> _nested_helpwin_widgets = {
179 NWidget(NWID_HORIZONTAL),
180 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
181 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_CAPTION),
182 EndContainer(),
183
184 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
185 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0), SetPadding(WidgetDimensions::unscaled.sparse),
186 NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_WEBSITES),
187 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WEBSITE), SetStringTip(STR_HELP_WINDOW_MAIN_WEBSITE), SetMinimalSize(128, 12), SetFill(1, 0),
188 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WIKI), SetStringTip(STR_HELP_WINDOW_MANUAL_WIKI), SetMinimalSize(128, 12), SetFill(1, 0),
189 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_BUGTRACKER), SetStringTip(STR_HELP_WINDOW_BUGTRACKER), SetMinimalSize(128, 12), SetFill(1, 0),
190 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_COMMUNITY), SetStringTip(STR_HELP_WINDOW_COMMUNITY), SetMinimalSize(128, 12), SetFill(1, 0),
191 EndContainer(),
192
193 NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetStringTip(STR_HELP_WINDOW_DOCUMENTS),
194 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_README), SetStringTip(STR_HELP_WINDOW_README), SetMinimalSize(128, 12), SetFill(1, 0),
195 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_CHANGELOG), SetStringTip(STR_HELP_WINDOW_CHANGELOG), SetMinimalSize(128, 12), SetFill(1, 0),
196 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_KNOWN_BUGS),SetStringTip(STR_HELP_WINDOW_KNOWN_BUGS), SetMinimalSize(128, 12), SetFill(1, 0),
197 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_LICENSE), SetStringTip(STR_HELP_WINDOW_LICENSE), SetMinimalSize(128, 12), SetFill(1, 0),
198 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_FONTS), SetStringTip(STR_HELP_WINDOW_FONTS), SetMinimalSize(128, 12), SetFill(1, 0),
199 EndContainer(),
200 EndContainer(),
201 EndContainer(),
202};
203
204static WindowDesc _helpwin_desc(
205 WDP_CENTER, {}, 0, 0,
206 WC_HELPWIN, WC_NONE,
207 {},
208 _nested_helpwin_widgets
209);
210
211void ShowHelpWindow()
212{
213 AllocateWindowDescFront<HelpWindow>(_helpwin_desc, 0);
214}
WidgetDimensions::unscaled
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:93
control_codes.h
Control codes that are embedded in the translation strings.
FioCheckFileExists
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:121
fileio_func.h
Functions for Standard In/Out file operations.
Searchpath
Searchpath
Types of searchpaths OpenTTD might use.
Definition fileio_type.h:113
SP_SHARED_DIR
@ SP_SHARED_DIR
Search in the shared directory, like 'Shared Files' under Windows.
Definition fileio_type.h:120
SP_INSTALLATION_DIR
@ SP_INSTALLATION_DIR
Search in the installation directory.
Definition fileio_type.h:122
SP_BINARY_DIR
@ SP_BINARY_DIR
Search in the directory where the binary resides.
Definition fileio_type.h:121
SP_WORKING_DIR
@ SP_WORKING_DIR
Search in the working directory.
Definition fileio_type.h:115
SP_APPLICATION_BUNDLE_DIR
@ SP_APPLICATION_BUNDLE_DIR
Search within the application bundle.
Definition fileio_type.h:123
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:107
BASE_DIR
@ BASE_DIR
Base directory for all subdirectories.
Definition fileio_type.h:89
DOCS_DIR
@ DOCS_DIR
Subdirectory for documentation.
Definition fileio_type.h:105
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition widget_type.h:1219
SetPIP
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
Definition widget_type.h:1375
SetPadding
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
Definition widget_type.h:1332
SetStringTip
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
Definition widget_type.h:1240
SetMinimalSize
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition widget_type.h:1156
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition widget_type.h:1229
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Definition widget_type.h:1424
gui.h
GUI functions that shouldn't be here.
CHANGELOG_VERSIONS_LIMIT
static constexpr size_t CHANGELOG_VERSIONS_LIMIT
Only show the first 20 changelog versions in the textfile viewer.
Definition help_gui.cpp:39
FindGameManualFilePath
static std::optional< std::string > FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
Find the path to the game manual file.
Definition help_gui.cpp:47
help_gui.h
GUI to access manuals and related.
help_widget.h
Types related to the help window widgets.
misc_widget.h
Types related to the misc widgets.
WID_TF_WRAPTEXT
@ WID_TF_WRAPTEXT
Whether or not to wrap the text.
Definition misc_widget.h:56
WID_TF_CAPTION
@ WID_TF_CAPTION
The caption of the window.
Definition misc_widget.h:53
openttd.h
Some generic types.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
Coord2D< int >
GameManualTextfileWindow
Window class displaying the game manual textfile viewer.
Definition help_gui.cpp:63
GameManualTextfileWindow::GetWidgetString
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
Definition help_gui.cpp:82
GameManualTextfileWindow::AfterLoadChangelog
void AfterLoadChangelog()
For changelog files, truncate the file after CHANGELOG_VERSIONS_LIMIT versions.
Definition help_gui.cpp:105
GameManualTextfileWindow::AfterLoadText
void AfterLoadText() override
Post-processing after the text is loaded.
Definition help_gui.cpp:91
HelpWindow
Window class displaying the help window.
Definition help_gui.cpp:125
HelpWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition help_gui.cpp:138
TextfileWindow::Line
Definition textfile_gui.h:52
TextfileWindow::Line::text
std::string text
Contents of the line.
Definition textfile_gui.h:57
TextfileWindow
Window for displaying a textfile.
Definition textfile_gui.h:22
TextfileWindow::trusted
bool trusted
Whether the content is trusted (read: not from content like NewGRFs, etc).
Definition textfile_gui.h:84
TextfileWindow::lines
std::vector< Line > lines
#text, split into lines in a table with lines.
Definition textfile_gui.h:78
TextfileWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition textfile_gui.cpp:504
TextfileWindow::AfterLoadText
virtual void AfterLoadText()
Post-processing after the text is loaded.
Definition textfile_gui.cpp:475
TextfileWindow::filepath
std::string filepath
Full path to the filename.
Definition textfile_gui.h:76
TextfileWindow::link_anchors
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
Definition textfile_gui.h:81
TextfileWindow::filename
std::string filename
Filename of the textfile.
Definition textfile_gui.h:75
TextfileWindow::LoadTextfile
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
Definition textfile_gui.cpp:851
WindowDesc
High level window description.
Definition window_gui.h:168
WindowNumber
Number to differentiate different windows of the same class.
Definition window_type.h:760
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::GetWidgetString
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:504
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1820
textfile_gui.h
GUI functions related to textfiles.
TFT_GAME_MANUAL
@ TFT_GAME_MANUAL
Game manual/documentation file.
Definition textfile_type.h:24
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition widget_type.h:104
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
WWT_FRAME
@ WWT_FRAME
Frame.
Definition widget_type.h:51
window_gui.h
Functions, definitions and such used only by the GUI.
WDP_CENTER
@ WDP_CENTER
Center the window.
Definition window_gui.h:145
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50