OpenTTD Source 20261112-master-gbcbec5e63f
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
9
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 "openttd.h"
16#include "help_gui.h"
17
18#include "widgets/help_widget.h"
19#include "widgets/misc_widget.h"
20
21#include "table/strings.h"
22
23#include "safeguards.h"
24
25static const std::string README_FILENAME = "README.md";
26static const std::string CHANGELOG_FILENAME = "changelog.md";
27static const std::string KNOWN_BUGS_FILENAME = "known-bugs.md";
28static const std::string LICENSE_FILENAME = "COPYING.md";
29static const std::string FONTS_FILENAME = "fonts.md";
30
31static const std::string WEBSITE_LINK = "https://www.openttd.org/";
32static const std::string WIKI_LINK = "https://wiki.openttd.org/";
33static const std::string BUGTRACKER_LINK = "https://bugs.openttd.org/";
34static const std::string COMMUNITY_LINK = "https://community.openttd.org/";
35
37static constexpr size_t CHANGELOG_VERSIONS_LIMIT = 20;
38
46static std::optional<std::string> FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
47{
48 static const Searchpath searchpaths[] = {
50 };
51
52 for (Searchpath sp : searchpaths) {
53 std::string file_path = FioGetDirectory(sp, subdir);
54 file_path.append(filename);
55 if (FioCheckFileExists(file_path, Subdirectory::None)) return file_path;
56 }
57
58 return {};
59}
60
62struct GameManualTextfileWindow : public TextfileWindow {
63 GameManualTextfileWindow(std::string_view filename, Subdirectory subdir) : TextfileWindow(nullptr, TextfileType::GameManual)
64 {
65 this->ConstructWindow();
66
67 /* Mark the content of these files as trusted. */
68 this->trusted = true;
69
71 /* The user could, in theory, have moved the file. So just show an empty window if that is the case. */
72 if (!filepath.has_value()) {
73 return;
74 }
75
76 this->filepath = filepath.value();
78 this->OnClick({ 0, 0 }, WID_TF_WRAPTEXT, 1);
79 }
80
81 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
82 {
83 if (widget == WID_TF_CAPTION) {
84 return GetString(stringid, this->filename);
85 }
86
87 return this->Window::GetWidgetString(widget, stringid);
88 }
89
90 void AfterLoadText() override
91 {
92 if (this->filename == CHANGELOG_FILENAME) {
93 this->link_anchors.clear();
94 this->AfterLoadChangelog();
95 }
97 }
98
105 {
106 uint versions = 0;
107
108 /* Look for lines beginning with ###, they indicate a release name. */
109 for (size_t line_index = 0; line_index < this->lines.size(); ++line_index) {
110 const Line &line = this->lines[line_index];
111 if (!line.text.starts_with("###")) continue;
112
113 if (versions >= CHANGELOG_VERSIONS_LIMIT) {
114 this->lines.resize(line_index - 2);
115 break;
116 }
117
118 ++versions;
119 }
120 }
121};
122
124struct HelpWindow : public Window {
125
126 HelpWindow(WindowDesc &desc, WindowNumber number) : Window(desc)
127 {
128 this->InitNested(number);
129
130 this->EnableTextfileButton(README_FILENAME, Subdirectory::Base, WID_HW_README);
131 this->EnableTextfileButton(CHANGELOG_FILENAME, Subdirectory::Base, WID_HW_CHANGELOG);
132 this->EnableTextfileButton(KNOWN_BUGS_FILENAME, Subdirectory::Base, WID_HW_KNOWN_BUGS);
133 this->EnableTextfileButton(LICENSE_FILENAME, Subdirectory::Base, WID_HW_LICENSE);
134 this->EnableTextfileButton(FONTS_FILENAME, Subdirectory::Docs, WID_HW_FONTS);
135 }
136
137 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
138 {
139 switch (widget) {
140 case WID_HW_README:
141 new GameManualTextfileWindow(README_FILENAME, Subdirectory::Base);
142 break;
143 case WID_HW_CHANGELOG:
144 new GameManualTextfileWindow(CHANGELOG_FILENAME, Subdirectory::Base);
145 break;
146 case WID_HW_KNOWN_BUGS:
147 new GameManualTextfileWindow(KNOWN_BUGS_FILENAME, Subdirectory::Base);
148 break;
149 case WID_HW_LICENSE:
150 new GameManualTextfileWindow(LICENSE_FILENAME, Subdirectory::Base);
151 break;
152 case WID_HW_FONTS:
153 new GameManualTextfileWindow(FONTS_FILENAME, Subdirectory::Docs);
154 break;
155 case WID_HW_WEBSITE:
156 OpenBrowser(WEBSITE_LINK);
157 break;
158 case WID_HW_WIKI:
159 OpenBrowser(WIKI_LINK);
160 break;
161 case WID_HW_BUGTRACKER:
162 OpenBrowser(BUGTRACKER_LINK);
163 break;
164 case WID_HW_COMMUNITY:
165 OpenBrowser(COMMUNITY_LINK);
166 break;
167 }
168 }
169
170private:
171 void EnableTextfileButton(std::string_view filename, Subdirectory subdir, WidgetID button_widget)
172 {
173 this->GetWidget<NWidgetLeaf>(button_widget)->SetDisabled(!FindGameManualFilePath(filename, subdir).has_value());
174 }
175};
176
177static constexpr std::initializer_list<NWidgetPart> _nested_helpwin_widgets = {
180 NWidget(WWT_CAPTION, Colours::DarkGreen), SetStringTip(STR_HELP_WINDOW_CAPTION),
181 EndContainer(),
182
185 NWidget(WWT_FRAME, Colours::DarkGreen), SetStringTip(STR_HELP_WINDOW_WEBSITES),
186 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_WEBSITE), SetStringTip(STR_HELP_WINDOW_MAIN_WEBSITE), SetMinimalSize(128, 12), SetFill(1, 0),
187 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_WIKI), SetStringTip(STR_HELP_WINDOW_MANUAL_WIKI), SetMinimalSize(128, 12), SetFill(1, 0),
188 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_BUGTRACKER), SetStringTip(STR_HELP_WINDOW_BUGTRACKER), SetMinimalSize(128, 12), SetFill(1, 0),
189 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_COMMUNITY), SetStringTip(STR_HELP_WINDOW_COMMUNITY), SetMinimalSize(128, 12), SetFill(1, 0),
190 EndContainer(),
191
192 NWidget(WWT_FRAME, Colours::DarkGreen), SetStringTip(STR_HELP_WINDOW_DOCUMENTS),
193 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_README), SetStringTip(STR_HELP_WINDOW_README), SetMinimalSize(128, 12), SetFill(1, 0),
194 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_CHANGELOG), SetStringTip(STR_HELP_WINDOW_CHANGELOG), SetMinimalSize(128, 12), SetFill(1, 0),
195 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_KNOWN_BUGS),SetStringTip(STR_HELP_WINDOW_KNOWN_BUGS), SetMinimalSize(128, 12), SetFill(1, 0),
196 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_LICENSE), SetStringTip(STR_HELP_WINDOW_LICENSE), SetMinimalSize(128, 12), SetFill(1, 0),
197 NWidget(WWT_PUSHTXTBTN, Colours::Green, WID_HW_FONTS), SetStringTip(STR_HELP_WINDOW_FONTS), SetMinimalSize(128, 12), SetFill(1, 0),
198 EndContainer(),
199 EndContainer(),
200 EndContainer(),
201};
202
205 WindowPosition::Center, {}, 0, 0,
206 WindowClass::Help, WindowClass::None,
207 {},
208 _nested_helpwin_widgets
209);
210
211void ShowHelpWindow()
212{
214}
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
bool FioCheckFileExists(std::string_view filename, Subdirectory subdir)
Check whether the given file exists.
Definition fileio.cpp:122
Functions for standard in/out file operations.
Searchpath
Types of searchpaths OpenTTD might use.
@ WorkingDir
Search in the working directory.
@ ApplicationBundleDir
Search within the application bundle.
@ InstallationDir
Search in the installation directory.
@ SharedDir
Search in the shared directory, like 'Shared Files' under Windows.
@ BinaryDir
Search in the directory where the binary resides.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ Base
Base directory for all subdirectories.
Definition fileio_type.h:88
@ None
A path without any base directory.
@ Docs
Subdirectory for documentation.
@ Green
Green.
Definition gfx_type.h:291
@ DarkGreen
Dark green.
Definition gfx_type.h:292
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
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.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
GUI functions that shouldn't be here.
static constexpr size_t CHANGELOG_VERSIONS_LIMIT
Only show the first 20 changelog versions in the textfile viewer.
Definition help_gui.cpp:37
static WindowDesc _helpwin_desc(WindowPosition::Center, {}, 0, 0, WindowClass::Help, WindowClass::None, {}, _nested_helpwin_widgets)
Window definition for the help window.
static std::optional< std::string > FindGameManualFilePath(std::string_view filename, Subdirectory subdir)
Find the path to the game manual file.
Definition help_gui.cpp:46
GUI to access manuals and related.
Types related to the help window widgets.
#define Point
Macro that prevents name conflicts between included headers.
Types related to the misc widgets.
@ WID_TF_WRAPTEXT
Whether or not to wrap the text.
Definition misc_widget.h:56
@ WID_TF_CAPTION
The caption of the window.
Definition misc_widget.h:53
Some generic types.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Window class displaying the game manual textfile viewer.
Definition help_gui.cpp:62
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
Definition help_gui.cpp:81
void AfterLoadChangelog()
For changelog files, truncate the file after CHANGELOG_VERSIONS_LIMIT versions.
Definition help_gui.cpp:104
void AfterLoadText() override
Post-processing after the text is loaded.
Definition help_gui.cpp:90
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:137
std::string text
Contents of the line.
bool trusted
Whether the content is trusted (read: not from content like NewGRFs, etc).
std::vector< Line > lines
text, split into lines in a table with lines.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
virtual void AfterLoadText()
Post-processing after the text is loaded.
std::string filepath
Full path to the filename.
std::vector< Hyperlink > link_anchors
Anchor names of headings that can be linked to.
std::string filename
Filename of the textfile.
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir)
Loads the textfile text from file and setup lines.
High level window description.
Definition window_gui.h:171
Number to differentiate different windows of the same class.
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:513
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1879
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1869
GUI functions related to textfiles.
@ GameManual
Game manual/documentation file.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ WWT_CLOSEBOX
Close box (at top-left of a window).
Definition widget_type.h:60
@ WWT_FRAME
Frame.
Definition widget_type.h:51
Functions, definitions and such used only by the GUI.
Twindow * AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number, Targs... extra_arguments)
Open a new window.
@ Center
Center the window.
Definition window_gui.h:146
int WidgetID
Widget ID.
Definition window_type.h:21