Files
@ r28319:f00779c148c1
Branch filter:
Location: cpp/openttd-patchpack/source/src/help_gui.cpp
r28319:f00779c148c1
7.4 KiB
text/x-c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | /*
* 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 help_gui.cpp GUI to access manuals and related. */
#include "stdafx.h"
#include "gui.h"
#include "window_gui.h"
#include "textfile_gui.h"
#include "fileio_func.h"
#include "table/control_codes.h"
#include "string_func.h"
#include "openttd.h"
#include "help_gui.h"
#include "widgets/help_widget.h"
#include "widgets/misc_widget.h"
#include "safeguards.h"
static const std::string README_FILENAME = "README.md";
static const std::string CHANGELOG_FILENAME = "changelog.txt";
static const std::string KNOWN_BUGS_FILENAME = "known-bugs.txt";
static const std::string LICENSE_FILENAME = "COPYING.md";
static const std::string WEBSITE_LINK = "https://www.openttd.org/";
static const std::string WIKI_LINK = "https://wiki.openttd.org/";
static const std::string BUGTRACKER_LINK = "https://bugs.openttd.org/";
static const std::string COMMUNITY_LINK = "https://community.openttd.org/";
/** Only show the first 20 changelog versions in the textfile viewer. */
static constexpr size_t CHANGELOG_VERSIONS_LIMIT = 20;
/**
* Find the path to the game manual file.
*
* @param filename The filename to find.
* @return std::string The path to the filename if found.
*/
static std::optional<std::string> FindGameManualFilePath(std::string_view filename)
{
static const Searchpath searchpaths[] = {
SP_APPLICATION_BUNDLE_DIR, SP_INSTALLATION_DIR, SP_SHARED_DIR, SP_BINARY_DIR, SP_WORKING_DIR
};
for (Searchpath sp : searchpaths) {
auto file_path = FioGetDirectory(sp, BASE_DIR) + filename.data();
if (FioCheckFileExists(file_path, NO_DIRECTORY)) return file_path;
}
return {};
}
/** Window class displaying the game manual textfile viewer. */
struct GameManualTextfileWindow : public TextfileWindow {
GameManualTextfileWindow(std::string_view filename) : TextfileWindow(TFT_GAME_MANUAL)
{
/* Mark the content of these files as trusted. */
this->trusted = true;
auto filepath = FindGameManualFilePath(filename);
/* The user could, in theory, have moved the file. So just show an empty window if that is the case. */
if (!filepath.has_value()) {
return;
}
this->filepath = filepath.value();
this->LoadTextfile(this->filepath, NO_DIRECTORY);
this->OnClick({ 0, 0 }, WID_TF_WRAPTEXT, 1);
}
void SetStringParameters(int widget) const override
{
if (widget == WID_TF_CAPTION) {
SetDParamStr(0, this->filename);
}
}
void AfterLoadText() override
{
if (this->filename == CHANGELOG_FILENAME) {
this->link_anchors.clear();
this->AfterLoadChangelog();
this->GetWidget<NWidgetStacked>(WID_TF_SEL_JUMPLIST)->SetDisplayedPlane(this->jumplist.empty() ? SZSP_HORIZONTAL : 0);
} else {
this->TextfileWindow::AfterLoadText();
}
}
/**
* For changelog files, add a jumplist entry for each version.
*
* This is hardcoded and assumes "---" are used to separate versions.
*/
void AfterLoadChangelog()
{
/* Look for lines beginning with ---, they indicate that the previous line was a release name. */
for (size_t line_index = 0; line_index < this->lines.size(); ++line_index) {
const Line &line = this->lines[line_index];
if (line.text.find("---", 0) != 0) continue;
if (this->jumplist.size() >= CHANGELOG_VERSIONS_LIMIT) {
this->lines.resize(line_index - 2);
break;
}
/* Mark the version header with a colour, and add it to the jumplist. */
this->lines[line_index - 1].colour = TC_GOLD;
this->lines[line_index].colour = TC_GOLD;
this->jumplist.push_back(line_index - 1);
}
}
};
/** Window class displaying the help window. */
struct HelpWindow : public Window {
HelpWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
{
this->InitNested(number);
this->EnableTextfileButton(README_FILENAME, WID_HW_README);
this->EnableTextfileButton(CHANGELOG_FILENAME, WID_HW_CHANGELOG);
this->EnableTextfileButton(KNOWN_BUGS_FILENAME, WID_HW_KNOWN_BUGS);
this->EnableTextfileButton(LICENSE_FILENAME, WID_HW_LICENSE);
}
void OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count) override
{
switch (widget) {
case WID_HW_README:
new GameManualTextfileWindow(README_FILENAME);
break;
case WID_HW_CHANGELOG:
new GameManualTextfileWindow(CHANGELOG_FILENAME);
break;
case WID_HW_KNOWN_BUGS:
new GameManualTextfileWindow(KNOWN_BUGS_FILENAME);
break;
case WID_HW_LICENSE:
new GameManualTextfileWindow(LICENSE_FILENAME);
break;
case WID_HW_WEBSITE:
OpenBrowser(WEBSITE_LINK);
break;
case WID_HW_WIKI:
OpenBrowser(WIKI_LINK);
break;
case WID_HW_BUGTRACKER:
OpenBrowser(BUGTRACKER_LINK);
break;
case WID_HW_COMMUNITY:
OpenBrowser(COMMUNITY_LINK);
break;
}
}
private:
void EnableTextfileButton(std::string_view filename, int button_widget)
{
this->GetWidget<NWidgetLeaf>(button_widget)->SetDisabled(!FindGameManualFilePath(filename).has_value());
}
};
static const NWidgetPart _nested_helpwin_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_HELP_WINDOW_CAPTION, STR_NULL),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0), SetPadding(WidgetDimensions::unscaled.sparse),
NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetDataTip(STR_HELP_WINDOW_WEBSITES, STR_NULL),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WEBSITE), SetDataTip(STR_HELP_WINDOW_MAIN_WEBSITE, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_WIKI), SetDataTip(STR_HELP_WINDOW_MANUAL_WIKI, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_BUGTRACKER), SetDataTip(STR_HELP_WINDOW_BUGTRACKER, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_COMMUNITY), SetDataTip(STR_HELP_WINDOW_COMMUNITY, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
EndContainer(),
NWidget(WWT_FRAME, COLOUR_DARK_GREEN), SetDataTip(STR_HELP_WINDOW_DOCUMENTS, STR_NULL),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_README), SetDataTip(STR_HELP_WINDOW_README, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_CHANGELOG), SetDataTip(STR_HELP_WINDOW_CHANGELOG, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_KNOWN_BUGS),SetDataTip(STR_HELP_WINDOW_KNOWN_BUGS, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_HW_LICENSE), SetDataTip(STR_HELP_WINDOW_LICENSE, STR_NULL), SetMinimalSize(128, 12), SetFill(1, 0),
EndContainer(),
EndContainer(),
EndContainer(),
};
static WindowDesc _helpwin_desc(__FILE__, __LINE__,
WDP_CENTER, nullptr, 0, 0,
WC_HELPWIN, WC_NONE,
0,
std::begin(_nested_helpwin_widgets), std::end(_nested_helpwin_widgets)
);
void ShowHelpWindow()
{
AllocateWindowDescFront<HelpWindow>(&_helpwin_desc, 0);
}
|