OpenTTD Source 20250312-master-gcdcc6b491d
newgrf_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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "error.h"
12#include "settings_gui.h"
13#include "newgrf.h"
14#include "strings_func.h"
15#include "window_func.h"
16#include "gamelog.h"
17#include "settings_type.h"
18#include "settings_func.h"
19#include "dropdown_type.h"
20#include "dropdown_func.h"
21#include "network/network.h"
23#include "sortlist_type.h"
24#include "stringfilter_type.h"
25#include "querystring_gui.h"
27#include "newgrf_text.h"
28#include "textfile_gui.h"
29#include "tilehighlight_func.h"
30#include "fios.h"
31#include "timer/timer.h"
32#include "timer/timer_window.h"
33#include "zoom_func.h"
34
36#include "widgets/misc_widget.h"
37
38#include "table/sprites.h"
39
40#include "safeguards.h"
41
46{
47 /* Do not show errors when entering the main screen */
48 if (_game_mode == GM_MENU) return;
49
50 for (const auto &c : _grfconfig) {
51 /* Only show Fatal and Error level messages */
52 if (!c->error.has_value() || (c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL && c->error->severity != STR_NEWGRF_ERROR_MSG_ERROR)) continue;
53
54 std::vector<StringParameter> params;
55 params.emplace_back(c->GetName());
56 params.emplace_back(c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING);
57 params.emplace_back(c->error->custom_message);
58 params.emplace_back(c->filename);
59 params.emplace_back(c->error->data);
60 for (const uint32_t &value : c->error->param_value) params.emplace_back(value);
61
62 if (c->error->severity == STR_NEWGRF_ERROR_MSG_FATAL) {
63 ShowErrorMessage(GetEncodedStringWithArgs(STR_NEWGRF_ERROR_FATAL_POPUP, params), {}, WL_CRITICAL);
64 } else {
65 ShowErrorMessage(GetEncodedStringWithArgs(STR_NEWGRF_ERROR_POPUP, params), {}, WL_ERROR);
66 }
67 break;
68 }
69}
70
71static StringID GetGRFPaletteString(uint8_t palette)
72{
73 if (palette & GRFP_BLT_32BPP) {
74 return (palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY_32BPP : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT_32BPP;
75 }
76 return (palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT;
77}
78
79static void ShowNewGRFInfo(const GRFConfig &c, const Rect &r, bool show_params)
80{
81 Rect tr = r.Shrink(WidgetDimensions::scaled.frametext);
82 if (c.error.has_value()) {
83 std::array<StringParameter, 3 + std::tuple_size_v<decltype(c.error->param_value)>> params{};
84 auto it = params.begin();
85 *it++ = c.error->custom_message; // is skipped by built-in messages
86 *it++ = c.filename;
87 *it++ = c.error->data;
88 for (const uint32_t &value : c.error->param_value) *it++ = value;
89
90 tr.top = DrawStringMultiLine(tr, GetString(c.error->severity, GetStringWithArgs(c.error->message != STR_NULL ? c.error->message : STR_JUST_RAW_STRING, {params.begin(), it})));
91 }
92
93 /* Draw filename or not if it is not known (GRF sent over internet) */
94 if (!c.filename.empty()) {
95 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_FILENAME, c.filename));
96 }
97
98 /* Prepare and draw GRF ID */
99 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_GRF_ID, fmt::format("{:08X}", std::byteswap(c.ident.grfid))));
100
102 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_VERSION, c.version));
103 }
105 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_MIN_VERSION, c.min_loadable_version));
106 }
107
108 /* Prepare and draw MD5 sum */
109 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_MD5SUM, FormatArrayAsHex(c.ident.md5sum)));
110
111 /* Show GRF parameter list */
112 if (show_params) {
113 if (!c.param.empty()) {
114 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_PARAMETER, STR_JUST_RAW_STRING, GRFBuildParamList(c)));
115 } else {
116 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_PARAMETER, STR_NEWGRF_SETTINGS_PARAMETER_NONE, std::monostate{}));
117 }
118
119 /* Draw the palette of the NewGRF */
120 tr.top = DrawStringMultiLine(tr, GetString(STR_NEWGRF_SETTINGS_PALETTE, GetGRFPaletteString(c.palette)));
121 }
122
123 /* Show flags */
124 if (c.status == GCS_NOT_FOUND) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_NOT_FOUND);
125 if (c.status == GCS_DISABLED) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_DISABLED);
126 if (c.flags.Test(GRFConfigFlag::Invalid)) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
127 if (c.flags.Test(GRFConfigFlag::Compatible)) tr.top = DrawStringMultiLine(tr, STR_NEWGRF_COMPATIBLE_LOADED);
128
129 /* Draw GRF info if it exists */
130 if (!StrEmpty(c.GetDescription())) {
131 tr.top = DrawStringMultiLine(tr, GetString(STR_JUST_RAW_STRING, c.GetDescription()), TC_BLACK);
132 } else {
133 tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_NO_INFO);
134 }
135}
136
144 bool clicked_increase = false;
145 bool clicked_dropdown = false;
146 bool closing_dropdown = false;
148 int line_height = 0;
149 Scrollbar *vscroll = nullptr;
150 bool action14present = false;
151 bool editable = false;
152
154 grf_config(c),
156 {
157 this->action14present = (this->grf_config.num_valid_params != this->grf_config.param.size() || !this->grf_config.param_info.empty());
158
159 this->CreateNestedTree();
161 this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
162 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
163 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
164 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
165
166 this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
167
168 this->InvalidateData();
169 }
170
181
187 bool HasParameterInfo(uint nr) const
188 {
189 return nr < this->grf_config.param_info.size() && this->grf_config.param_info[nr].has_value();
190 }
191
199 {
200 return this->HasParameterInfo(nr) ? this->grf_config.param_info[nr].value() : GetDummyParameterInfo(nr);
201 }
202
204 {
205 switch (widget) {
207 case WID_NP_NUMPAR_INC: {
208 size.width = std::max(SETTING_BUTTON_WIDTH / 2, GetCharacterHeight(FS_NORMAL));
209 size.height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL));
210 break;
211 }
212
213 case WID_NP_NUMPAR: {
214 Dimension d = GetStringBoundingBox(GetString(this->GetWidget<NWidgetCore>(widget)->GetString(), GetParamMaxValue(GRFConfig::MAX_NUM_PARAMS)));
215 d.width += padding.width;
216 d.height += padding.height;
217 size = maxdim(size, d);
218 break;
219 }
220
222 this->line_height = std::max(SETTING_BUTTON_HEIGHT, GetCharacterHeight(FS_NORMAL)) + padding.height;
223
224 resize.width = 1;
225 resize.height = this->line_height;
226 size.height = 5 * this->line_height;
227 break;
228
230 /* Minimum size of 4 lines. The 500 is the default size of the window. */
232 for (const auto &par_info : this->grf_config.param_info) {
233 if (!par_info.has_value()) continue;
234 const char *desc = GetGRFStringFromGRFText(par_info->desc);
235 if (desc == nullptr) continue;
239 }
240 size.height = suggestion.height;
241 break;
242 }
243 }
244
245 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
246 {
247 switch (widget) {
248 case WID_NP_NUMPAR:
249 return GetString(STR_NEWGRF_PARAMETERS_NUM_PARAM, this->vscroll->GetCount());
250
251 default:
252 return this->Window::GetWidgetString(widget, stringid);
253 }
254 }
255
256 std::pair<StringParameter, StringParameter> GetValueParams(const GRFParameterInfo &par_info, uint32_t value) const
257 {
258 if (par_info.type == PTYPE_BOOL) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}};
259
260 auto it = std::ranges::lower_bound(par_info.value_names, value, std::less{}, &GRFParameterInfo::ValueName::first);
261 if (it != std::end(par_info.value_names) && it->first == value) {
262 if (const char *label = GetGRFStringFromGRFText(it->second); label != nullptr) return {STR_JUST_RAW_STRING, label};
263 }
264
265 return {STR_JUST_INT, value};
266 }
267
268 std::string GetSettingString(const GRFParameterInfo &par_info, int i, uint32_t value) const
269 {
270 auto [param1, param2] = this->GetValueParams(par_info, value);
271 const char *name = GetGRFStringFromGRFText(par_info.name);
272 return name == nullptr
275 }
276
277 void DrawWidget(const Rect &r, WidgetID widget) const override
278 {
279 if (widget == WID_NP_DESCRIPTION) {
280 if (!this->HasParameterInfo(this->clicked_row)) return;
281 const GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
282 const char *desc = GetGRFStringFromGRFText(par_info.desc);
283 if (desc == nullptr) return;
285 return;
286 } else if (widget != WID_NP_BACKGROUND) {
287 return;
288 }
289
290 Rect ir = r.Shrink(WidgetDimensions::scaled.frametext, RectPadding::zero);
291 bool rtl = _current_text_dir == TD_RTL;
292 uint buttons_left = rtl ? ir.right - SETTING_BUTTON_WIDTH : ir.left;
294
295 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
296 int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
297 for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
299 uint32_t current_value = this->grf_config.GetValue(par_info);
300 bool selected = (i == this->clicked_row);
301
302 if (par_info.type == PTYPE_BOOL) {
304 } else if (par_info.type == PTYPE_UINT_ENUM) {
305 if (par_info.complete_labels) {
306 DrawDropDownButton(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
307 } else {
308 DrawArrowButtons(buttons_left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, this->editable && current_value > par_info.min_value, this->editable && current_value < par_info.max_value);
309 }
310 }
311
312 DrawString(tr.left, tr.right, ir.top + text_y_offset, this->GetSettingString(par_info, i, current_value), selected ? TC_WHITE : TC_LIGHT_BLUE);
313 ir.top += this->line_height;
314 }
315 }
316
317 void OnPaint() override
318 {
319 if (this->closing_dropdown) {
320 this->closing_dropdown = false;
321 this->clicked_dropdown = false;
322 }
323 this->DrawWidgets();
324 }
325
326 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
327 {
328 switch (widget) {
330 if (this->editable && !this->action14present && !this->grf_config.param.empty()) {
331 this->grf_config.param.pop_back();
332 this->InvalidateData();
334 }
335 break;
336
338 if (this->editable && !this->action14present && this->grf_config.param.size() < this->grf_config.num_valid_params) {
339 this->grf_config.param.emplace_back(0);
340 this->InvalidateData();
342 }
343 break;
344
345 case WID_NP_BACKGROUND: {
346 if (!this->editable) break;
347 int32_t num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
348 if (num >= this->vscroll->GetCount()) break;
349
350 if (this->clicked_row != num) {
353 this->clicked_row = num;
354 this->clicked_dropdown = false;
355 }
356
357 Rect r = this->GetWidget<NWidgetBase>(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.frametext, RectPadding::zero);
358 int x = pt.x - r.left;
359 if (_current_text_dir == TD_RTL) x = r.Width() - 1 - x;
360
362
363 /* One of the arrows is clicked */
364 uint32_t old_val = this->grf_config.GetValue(par_info);
365 if (par_info.type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info.complete_labels) {
366 if (this->clicked_dropdown) {
367 /* unclick the dropdown */
369 this->clicked_dropdown = false;
370 this->closing_dropdown = false;
371 } else {
372 int rel_y = (pt.y - r.top) % this->line_height;
373
374 Rect wi_rect;
375 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
376 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
377 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
378 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
379
380 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
381 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
382 this->clicked_dropdown = true;
383 this->closing_dropdown = false;
384
385 DropDownList list;
386 for (const auto &[value, name] : par_info.value_names) {
387 list.push_back(MakeDropDownListStringItem(GetString(STR_JUST_RAW_STRING, GetGRFStringFromGRFText(name)), value));
388 }
389
390 ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE);
391 }
392 }
393 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
395 if (par_info.type == PTYPE_BOOL) {
396 val = !val;
397 } else {
398 if (x >= SETTING_BUTTON_WIDTH / 2) {
399 /* Increase button clicked */
400 if (val < par_info.max_value) val++;
401 this->clicked_increase = true;
402 } else {
403 /* Decrease button clicked */
404 if (val > par_info.min_value) val--;
405 this->clicked_increase = false;
406 }
407 }
408 if (val != old_val) {
409 this->grf_config.SetValue(par_info, val);
410
411 this->clicked_button = num;
412 this->unclick_timeout.Reset();
413 }
414 } else if (par_info.type == PTYPE_UINT_ENUM && !par_info.complete_labels && click_count >= 2) {
415 /* Display a query box so users can enter a custom value. */
417 }
418 this->SetDirty();
419 break;
420 }
421
422 case WID_NP_RESET:
423 if (!this->editable) break;
424 this->grf_config.SetParameterDefaults();
425 this->InvalidateData();
427 break;
428
429 case WID_NP_ACCEPT:
430 this->Close();
431 break;
432 }
433 }
434
435 void OnQueryTextFinished(std::optional<std::string> str) override
436 {
437 if (!str.has_value() || str->empty()) return;
438 int32_t value = atoi(str->c_str());
439 GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
440 this->grf_config.SetValue(par_info, value);
441 this->SetDirty();
442 }
443
444 void OnDropdownSelect(WidgetID widget, int index) override
445 {
446 if (widget != WID_NP_SETTING_DROPDOWN) return;
447 assert(this->clicked_dropdown);
448 GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row);
449 this->grf_config.SetValue(par_info, index);
450 this->SetDirty();
451 }
452
453 void OnDropdownClose(Point, WidgetID widget, int, bool) override
454 {
455 if (widget != WID_NP_SETTING_DROPDOWN) return;
456 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
457 * the same dropdown button was clicked again, and then not open the dropdown again.
458 * So, we only remember that it was closed, and process it on the next OnPaint, which is
459 * after OnClick. */
460 assert(this->clicked_dropdown);
461 this->closing_dropdown = true;
462 this->SetDirty();
463 }
464
465 void OnResize() override
466 {
467 this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
468 }
469
475 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
476 {
477 if (!gui_scope) return;
478 if (!this->action14present) {
479 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config.param.empty());
480 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || std::size(this->grf_config.param) >= this->grf_config.num_valid_params);
481 }
482
483 this->vscroll->SetCount(this->action14present ? this->grf_config.num_valid_params : GRFConfig::MAX_NUM_PARAMS);
484 if (this->clicked_row != INT32_MAX && this->clicked_row >= this->vscroll->GetCount()) {
485 this->clicked_row = INT32_MAX;
487 }
488 }
489
491 TimeoutTimer<TimerWindow> unclick_timeout = {std::chrono::milliseconds(150), [this]() {
492 this->clicked_button = INT32_MAX;
493 this->SetDirty();
494 }};
495};
497
498
499static constexpr NWidgetPart _nested_newgrf_parameter_widgets[] = {
501 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
502 NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_NP_CAPTION),
503 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
504 EndContainer(),
505 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
506 NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
507 NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
510 NWidget(WWT_TEXT, INVALID_COLOUR, WID_NP_NUMPAR), SetResize(1, 0), SetFill(1, 0), SetPadding(0, 0, 0, 4),
511 EndContainer(),
512 EndContainer(),
513 EndContainer(),
517 EndContainer(),
519 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
520 EndContainer(),
521 EndContainer(),
524 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NEWGRF_PARAMETERS_CLOSE),
525 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetStringTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
526 EndContainer(),
527 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
528 EndContainer(),
529};
530
533 WDP_CENTER, "settings_newgrf_config", 500, 208,
535 {},
536 _nested_newgrf_parameter_widgets
537);
538
539void OpenGRFParameterWindow(bool is_baseset, GRFConfig &c, bool editable)
540{
542 new NewGRFParametersWindow(_newgrf_parameters_desc, is_baseset, c, editable);
543}
544
547 const GRFConfig *grf_config = nullptr;
548
550 {
551 this->ConstructWindow();
552
553 auto textfile = this->grf_config->GetTextfile(file_type);
554 this->LoadTextfile(textfile.value(), NEWGRF_DIR);
555 }
556
557 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
558 {
559 if (widget == WID_TF_CAPTION) {
560 return GetString(stringid, STR_CONTENT_TYPE_NEWGRF, this->grf_config->GetName());
561 }
562
563 return this->Window::GetWidgetString(widget, stringid);
564 }
565};
566
567void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
568{
569 CloseWindowById(WC_TEXTFILE, file_type);
570 new NewGRFTextfileWindow(file_type, c);
571}
572
573typedef std::map<uint32_t, const GRFConfig *> GrfIdMap;
574
580static void FillGrfidMap(const GRFConfigList &lst, GrfIdMap &grfid_map)
581{
582 for (const auto &c : lst) {
583 grfid_map.emplace(c->ident.grfid, c.get());
584 }
585}
586
587static void NewGRFConfirmationCallback(Window *w, bool confirmed);
588static void ShowSavePresetWindow(const char *initial_text);
589
595
596 static const uint EDITBOX_MAX_SIZE = 50;
597
598 static Listing last_sorting;
600 static const std::initializer_list<GUIGRFConfigList::SortFunction * const> sorter_funcs;
601 static const std::initializer_list<GUIGRFConfigList::FilterFunction * const> filter_funcs;
602
604 const GRFConfig *avail_sel = nullptr;
605 int avail_pos = -1;
608
610
611 GRFConfigList actives{};
613
614 GRFConfigList &orig_list;
615 bool editable = false;
616 bool show_params = false;
617 bool execute = false;
618 int preset = -1;
619 int active_over = -1;
620 bool modified = false;
621
622 Scrollbar *vscroll = nullptr;
623 Scrollbar *vscroll2 = nullptr;
624
625 NewGRFWindow(WindowDesc &desc, bool editable, bool show_params, bool execute, GRFConfigList &orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE), orig_list(orig_list)
626 {
627 this->editable = editable;
628 this->execute = execute;
629 this->show_params = show_params;
630
631 CopyGRFConfigList(this->actives, orig_list, false);
633
634 this->CreateNestedTree();
635 this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
636 this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
637
638 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
639 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
641
643 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
644 if (editable) {
646 } else {
648 }
649
650 this->avails.SetListing(this->last_sorting);
651 this->avails.SetFiltering(this->last_filtering);
652 this->avails.SetSortFuncs(this->sorter_funcs);
653 this->avails.SetFilterFuncs(this->filter_funcs);
654 this->avails.ForceRebuild();
655
657 }
658
659 void Close([[maybe_unused]] int data = 0) override
660 {
664
665 if (this->editable && this->modified && !this->execute && !_exit_game) {
666 CopyGRFConfigList(this->orig_list, this->actives, true);
667 ResetGRFConfig(false);
669 }
670
671 this->Window::Close();
672 }
673
674 int GetCurrentActivePosition() const
675 {
676 if (this->active_sel != nullptr) {
677 auto it = std::ranges::find_if(this->actives, [this](const auto &c) { return c.get() == this->active_sel; });
678 if (it != std::end(this->actives)) return static_cast<int>(std::distance(std::begin(this->actives), it));
679 }
680 return -1;
681 }
682
688 {
690 FillGrfidMap(this->actives, grfid_map);
691
692 for (const auto &a : _all_grfs) {
693 GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
694 if (iter != grfid_map.end() && a->version > iter->second->version) return true;
695 }
696 return false;
697 }
698
701 {
703 FillGrfidMap(this->actives, grfid_map);
704
705 for (const auto &a : _all_grfs) {
706 GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
707 if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
708
709 auto c = std::ranges::find_if(this->actives, [&iter](const auto &grfconfig) { return grfconfig.get() == iter->second; });
710 assert(c != std::end(this->actives));
711 auto d = std::make_unique<GRFConfig>(*iter->second);
712 if (d->IsCompatible((*c)->version)) {
713 d->CopyParams(**c);
714 } else {
715 d->SetParameterDefaults();
716 }
717 if (this->active_sel == c->get()) {
720 this->active_sel = nullptr;
721 }
722 *c = std::move(d);
723 iter->second = c->get();
724 }
725 }
726
728 {
729 switch (widget) {
730 case WID_NS_FILE_LIST:
731 {
732 Dimension d = maxdim(GetScaledSpriteSize(SPR_SQUARE), GetScaledSpriteSize(SPR_WARNING_SIGN));
733 resize.height = std::max<uint>(d.height + 2U, GetCharacterHeight(FS_NORMAL));
734 size.height = std::max(size.height, padding.height + 6 * resize.height);
735 break;
736 }
737
739 {
740 Dimension d = maxdim(GetScaledSpriteSize(SPR_SQUARE), GetScaledSpriteSize(SPR_WARNING_SIGN));
741 resize.height = std::max<uint>(d.height + 2U, GetCharacterHeight(FS_NORMAL));
742 size.height = std::max(size.height, padding.height + 8 * resize.height);
743 break;
744 }
745
748 size.height = std::max(size.height, dim.height + WidgetDimensions::scaled.frametext.Vertical());
749 size.width = std::max(size.width, dim.width + WidgetDimensions::scaled.frametext.Horizontal());
750 break;
751 }
752
754 size.height = std::max<uint>(size.height, WidgetDimensions::scaled.framerect.Vertical() + 10 * GetCharacterHeight(FS_NORMAL));
755 break;
756
757 case WID_NS_PRESET_LIST: {
759 for (const auto &i : this->grf_presets) {
761 }
762 d.width += padding.width;
763 size = maxdim(d, size);
764 break;
765 }
766
771 size.width += padding.width;
772 size.height += padding.height;
773 break;
774 }
775 }
776 }
777
778 void OnResize() override
779 {
780 this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST, WidgetDimensions::scaled.framerect.Vertical());
781 this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST, WidgetDimensions::scaled.framerect.Vertical());
782 }
783
784 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
785 {
786 switch (widget) {
788 if (this->preset == -1) return GetString(STR_NUM_CUSTOM);
789
790 return this->grf_presets[this->preset];
791
792 default:
793 return this->Window::GetWidgetString(widget, stringid);
794 }
795 }
796
802 inline PaletteID GetPalette(const GRFConfig &c) const
803 {
804 PaletteID pal;
805
806 /* Pick a colour */
807 switch (c.status) {
808 case GCS_NOT_FOUND:
809 case GCS_DISABLED:
810 pal = PALETTE_TO_RED;
811 break;
812 case GCS_ACTIVATED:
813 pal = PALETTE_TO_GREEN;
814 break;
815 default:
816 pal = PALETTE_TO_BLUE;
817 break;
818 }
819
820 /* Do not show a "not-failure" colour when it actually failed to load */
821 if (pal != PALETTE_TO_RED) {
823 pal = PALETTE_TO_GREY;
824 } else if (c.flags.Test(GRFConfigFlag::Compatible)) {
825 pal = PALETTE_TO_ORANGE;
826 }
827 }
828
829 return pal;
830 }
831
832 void DrawWidget(const Rect &r, WidgetID widget) const override
833 {
834 switch (widget) {
835 case WID_NS_FILE_LIST: {
836 const Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
838
839 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
840 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
841 Dimension square = GetSpriteSize(SPR_SQUARE);
842 Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
843 int square_offset_y = (step_height - square.height) / 2;
844 int warning_offset_y = (step_height - warning.height) / 2;
845 int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2;
846
847 bool rtl = _current_text_dir == TD_RTL;
848 uint text_left = rtl ? tr.left : tr.left + square.width + 13;
849 uint text_right = rtl ? tr.right - square.width - 13 : tr.right;
850 uint square_left = rtl ? tr.right - square.width - 3 : tr.left + 3;
851 uint warning_left = rtl ? tr.right - square.width - warning.width - 8 : tr.left + square.width + 8;
852
853 int i = 0;
854 for (const auto &c : this->actives) {
855 if (this->vscroll->IsVisible(i)) {
856 const char *text = c->GetName();
857 bool h = (this->active_sel == c.get());
858 PaletteID pal = this->GetPalette(*c);
859
860 if (h) {
861 GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE);
862 } else if (i == this->active_over) {
863 /* Get index of current selection. */
864 int active_sel_pos = this->GetCurrentActivePosition();
865 if (active_sel_pos != this->active_over) {
866 uint top = this->active_over < active_sel_pos ? tr.top + 1 : tr.top + step_height - 2;
867 GfxFillRect(tr.left, top - 1, tr.right, top + 1, PC_GREY);
868 }
869 }
870 DrawSprite(SPR_SQUARE, pal, square_left, tr.top + square_offset_y);
871 if (c->error.has_value()) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y);
872 uint txtoffset = !c->error.has_value() ? 0 : warning.width;
873 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, text, h ? TC_WHITE : TC_ORANGE);
874 tr.top += step_height;
875 }
876 i++;
877 }
878 if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
879 GfxFillRect(tr.left, tr.top, tr.right, tr.top + 2, PC_GREY);
880 }
881 break;
882 }
883
884 case WID_NS_AVAIL_LIST: {
885 const Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
886 GfxFillRect(br, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
887
888 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
889 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
890 int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2;
891
892 auto [first, last] = this->vscroll2->GetVisibleRangeIterators(this->avails);
893 for (auto it = first; it != last; ++it) {
894 const GRFConfig *c = *it;
895 bool h = (c == this->avail_sel);
896 const char *text = c->GetName();
897
898 if (h) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE);
899 DrawString(tr.left, tr.right, tr.top + offset_y, text, h ? TC_WHITE : TC_SILVER);
900 tr.top += step_height;
901 }
902 break;
903 }
904
906 /* Create the nice grayish rectangle at the details top. */
908 DrawString(r.left, r.right, CenterBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL)), STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
909 break;
910 }
911
912 case WID_NS_NEWGRF_INFO: {
913 const GRFConfig *selected = this->active_sel;
914 if (selected == nullptr) selected = this->avail_sel;
915 if (selected != nullptr) {
916 ShowNewGRFInfo(*selected, r, this->show_params);
917 }
918 break;
919 }
920 }
921 }
922
923 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
924 {
925 if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_CONTENT_END) {
926 if (this->active_sel == nullptr && this->avail_sel == nullptr) return;
927
928 ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != nullptr ? this->active_sel : this->avail_sel);
929 return;
930 }
931
932 switch (widget) {
933 case WID_NS_PRESET_LIST: {
934 DropDownList list;
935
936 /* Add 'None' option for clearing list */
937 list.push_back(MakeDropDownListStringItem(STR_NONE, -1));
938
939 for (uint i = 0; i < this->grf_presets.size(); i++) {
940 list.push_back(MakeDropDownListStringItem(std::string{this->grf_presets[i]}, i));
941 }
942
943 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
944 ShowDropDownList(this, std::move(list), this->preset, WID_NS_PRESET_LIST);
945 break;
946 }
947
948 case WID_NS_OPEN_URL: {
949 const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
950
951 OpenBrowser(c->GetURL());
952 break;
953 }
954
956 ShowSavePresetWindow((this->preset == -1) ? nullptr : this->grf_presets[this->preset].c_str());
957 break;
958
960 if (this->preset == -1) return;
961
962 DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
964 this->preset = -1;
965 this->InvalidateData();
966 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
967 break;
968
969 case WID_NS_MOVE_UP: { // Move GRF up
970 if (this->active_sel == nullptr || !this->editable) break;
971
972 int pos = this->GetCurrentActivePosition();
973 if (pos <= 0) break;
974
975 std::swap(this->actives[pos - 1], this->actives[pos]);
976
977 this->vscroll->ScrollTowards(pos - 1);
978 this->preset = -1;
980 break;
981 }
982
983 case WID_NS_MOVE_DOWN: { // Move GRF down
984 if (this->active_sel == nullptr || !this->editable) break;
985
986 int pos = this->GetCurrentActivePosition();
987 if (pos == -1 || static_cast<size_t>(pos) >= this->actives.size() - 1) break;
988
989 std::swap(this->actives[pos], this->actives[pos + 1]);
990
991 this->vscroll->ScrollTowards(pos + 1);
992 this->preset = -1;
994 break;
995 }
996
997 case WID_NS_FILE_LIST: { // Select an active GRF.
999
1000 const GRFConfig *old_sel = this->active_sel;
1001 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST, WidgetDimensions::scaled.framerect.top);
1002 if (i < this->actives.size()) {
1003 this->active_sel = this->actives[i].get();
1004 } else {
1005 this->active_sel = nullptr;
1006 }
1007 if (this->active_sel != old_sel) {
1010 }
1011 this->avail_sel = nullptr;
1012 this->avail_pos = -1;
1013
1014 this->InvalidateData();
1015 if (click_count == 1) {
1016 if (this->editable && this->active_sel != nullptr) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1017 break;
1018 }
1019 /* With double click, continue */
1020 [[fallthrough]];
1021 }
1022
1023 case WID_NS_REMOVE: { // Remove GRF
1024 if (this->active_sel == nullptr || !this->editable) break;
1027
1028 /* Choose the next GRF file to be the selected file. */
1029 int pos = this->GetCurrentActivePosition();
1030 if (pos < 0) break;
1031
1032 auto it = std::next(std::begin(this->actives), pos);
1033 it = this->actives.erase(it);
1034 if (this->actives.empty()) {
1035 this->active_sel = nullptr;
1036 } else if (it == std::end(this->actives)) {
1037 this->active_sel = this->actives.back().get();
1038 } else {
1039 this->active_sel = it->get();
1040 }
1041 this->preset = -1;
1042 this->avail_pos = -1;
1043 this->avail_sel = nullptr;
1044 this->avails.ForceRebuild();
1046 break;
1047 }
1048
1049 case WID_NS_UPGRADE: { // Upgrade GRF.
1050 if (!this->editable || this->actives.empty()) break;
1053 break;
1054 }
1055
1056 case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1058
1059 auto it = this->vscroll2->GetScrolledItemFromWidget(this->avails, pt.y, this, WID_NS_AVAIL_LIST, WidgetDimensions::scaled.framerect.top);
1060 this->active_sel = nullptr;
1062 if (it != std::end(this->avails)) {
1063 if (this->avail_sel != *it) CloseWindowByClass(WC_TEXTFILE);
1064 this->avail_sel = *it;
1065 this->avail_pos = static_cast<int>(std::distance(std::begin(this->avails), it));
1066 }
1067 this->InvalidateData();
1068 if (click_count == 1) {
1069 if (this->editable && this->avail_sel != nullptr && !this->avail_sel->flags.Test(GRFConfigFlag::Invalid)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1070 break;
1071 }
1072 /* With double click, continue */
1073 [[fallthrough]];
1074 }
1075
1076 case WID_NS_ADD:
1077 if (this->avail_sel == nullptr || !this->editable || this->avail_sel->flags.Test(GRFConfigFlag::Invalid)) break;
1078
1079 this->AddGRFToActive();
1080 break;
1081
1082 case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1083 if (!this->editable) break;
1084 if (this->execute) {
1085 ShowQuery(
1088 this,
1090 );
1091 } else {
1092 CopyGRFConfigList(this->orig_list, this->actives, true);
1093 ResetGRFConfig(false);
1096 }
1097 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1098 break;
1099
1101 case WID_NS_SET_PARAMETERS: { // Edit parameters
1102 if (this->active_sel == nullptr || !this->show_params || this->active_sel->num_valid_params == 0) break;
1103
1104 OpenGRFParameterWindow(false, *this->active_sel, this->editable);
1106 break;
1107 }
1108
1110 if (this->active_sel != nullptr && this->editable) {
1111 this->active_sel->palette ^= GRFP_USE_MASK;
1112 this->SetDirty();
1114 }
1115 break;
1116
1119 if (!_network_available) {
1121 } else {
1122 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1123
1125 }
1126 break;
1127
1130 RequestNewGRFScan(this);
1131 break;
1132 }
1133 }
1134
1135 void OnNewGRFsScanned() override
1136 {
1137 if (this->active_sel == nullptr) CloseWindowByClass(WC_TEXTFILE);
1138 this->avail_sel = nullptr;
1139 this->avail_pos = -1;
1140 this->avails.ForceRebuild();
1141 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1142 }
1143
1144 void OnDropdownSelect(WidgetID widget, int index) override
1145 {
1146 if (widget != WID_NS_PRESET_LIST) return;
1147 if (!this->editable) return;
1148
1150 this->preset = index;
1151
1152 if (index != -1) {
1153 this->actives = LoadGRFPresetFromConfig(this->grf_presets[index].c_str());
1154 }
1155 this->avails.ForceRebuild();
1156
1160 this->active_sel = nullptr;
1162 }
1163
1164 void OnQueryTextFinished(std::optional<std::string> str) override
1165 {
1166 if (!str.has_value()) return;
1167
1168 SaveGRFPresetToConfig(str->c_str(), this->actives);
1169 this->grf_presets = GetGRFPresetList();
1170
1171 /* Switch to this preset */
1172 for (uint i = 0; i < this->grf_presets.size(); i++) {
1173 if (this->grf_presets[i] == str) {
1174 this->preset = i;
1175 break;
1176 }
1177 }
1178
1179 this->InvalidateData();
1180 }
1181
1186 {
1187 /* Update scrollbars */
1188 this->vscroll->SetCount(this->actives.size() + 1); // Reserve empty space for drag and drop handling.
1189
1190 if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1191 }
1192
1198 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1199 {
1200 if (!gui_scope) return;
1201 switch (data) {
1202 default:
1203 /* Nothing important to do */
1204 break;
1205
1207 /* Search the list for items that are now found and mark them as such. */
1208 for (auto &c : this->actives) {
1209 bool compatible = c->flags.Test(GRFConfigFlag::Compatible);
1210 if (c->status != GCS_NOT_FOUND && !compatible) continue;
1211
1212 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? &c->original_md5sum : &c->ident.md5sum);
1213 if (f == nullptr || f->flags.Test(GRFConfigFlag::Invalid)) continue;
1214
1215 c = std::make_unique<GRFConfig>(*f);
1216 }
1217
1218 this->avails.ForceRebuild();
1219 [[fallthrough]];
1220
1222 this->modified = false;
1224 break;
1225
1227 this->preset = -1;
1228 [[fallthrough]];
1229
1232
1233 /* Changes have been made to the list of active NewGRFs */
1234 this->modified = true;
1235
1236 break;
1237
1239 /* No changes have been made to the list of active NewGRFs since the last time the changes got applied */
1240 this->modified = false;
1241 break;
1242 }
1243
1244 this->BuildAvailables();
1245
1246 this->SetWidgetDisabledState(WID_NS_APPLY_CHANGES, !((this->editable && this->modified) || _settings_client.gui.newgrf_developer_tools));
1247 this->SetWidgetsDisabledState(!this->editable,
1250 );
1251 this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == nullptr || this->avail_sel->flags.Test(GRFConfigFlag::Invalid));
1252 this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives.empty() || !this->CanUpgradeCurrent());
1253
1254 bool disable_all = this->active_sel == nullptr || !this->editable;
1255 this->SetWidgetsDisabledState(disable_all,
1259 );
1260
1261 const GRFConfig *selected_config = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
1262 for (TextfileType tft = TFT_CONTENT_BEGIN; tft < TFT_CONTENT_END; tft++) {
1263 this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, selected_config == nullptr || !selected_config->GetTextfile(tft).has_value());
1264 }
1265 this->SetWidgetDisabledState(WID_NS_OPEN_URL, selected_config == nullptr || StrEmpty(selected_config->GetURL()));
1266
1267 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1268 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1269 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1271
1272 if (!disable_all) {
1273 /* All widgets are now enabled, so disable widgets we can't use */
1274 if (this->active_sel == this->actives.front().get()) this->DisableWidget(WID_NS_MOVE_UP);
1275 if (this->active_sel == this->actives.back().get()) this->DisableWidget(WID_NS_MOVE_DOWN);
1276 }
1277
1278 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1279
1280 bool has_missing = false;
1281 bool has_compatible = false;
1282 for (const auto &c : this->actives) {
1283 has_missing |= c->status == GCS_NOT_FOUND;
1285 }
1286 StringID text;
1287 StringID tool_tip;
1288 if (has_missing || has_compatible) {
1291 } else {
1294 }
1295 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->SetStringTip(text, tool_tip);
1296 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->SetStringTip(text, tool_tip);
1297
1298 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1299 }
1300
1301 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
1302 {
1303 if (!this->editable) return ES_NOT_HANDLED;
1304
1305 if (this->vscroll2->UpdateListPositionOnKeyPress(this->avail_pos, keycode) == ES_NOT_HANDLED) return ES_NOT_HANDLED;
1306
1307 if (this->avail_pos >= 0) {
1308 this->active_sel = nullptr;
1310 if (this->avail_sel != this->avails[this->avail_pos]) CloseWindowByClass(WC_TEXTFILE);
1311 this->avail_sel = this->avails[this->avail_pos];
1312 this->vscroll2->ScrollTowards(this->avail_pos);
1313 this->InvalidateData(0);
1314 }
1315
1316 return ES_HANDLED;
1317 }
1318
1319 void OnEditboxChanged(WidgetID widget) override
1320 {
1321 if (!this->editable) return;
1322
1323 if (widget == WID_NS_FILTER) {
1324 string_filter.SetFilterTerm(this->filter_editbox.text.GetText());
1326 this->avails.ForceRebuild();
1327 this->InvalidateData(0);
1328 }
1329 }
1330
1331 void OnDragDrop(Point pt, WidgetID widget) override
1332 {
1333 if (!this->editable) return;
1334
1335 if (widget == WID_NS_FILE_LIST) {
1336 if (this->active_sel != nullptr) {
1337 int from_pos = this->GetCurrentActivePosition();
1338
1339 /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1340 int to_pos = std::min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST, WidgetDimensions::scaled.framerect.top), this->vscroll->GetCount() - 2);
1341 if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1342 if (to_pos > from_pos) ++to_pos;
1343
1344 auto from = std::next(std::begin(this->actives), from_pos);
1345 auto to = std::next(std::begin(this->actives), to_pos);
1346 Slide(from, std::next(from), to);
1347
1348 this->vscroll->ScrollTowards(to_pos);
1349 this->preset = -1;
1350 this->InvalidateData();
1351 }
1352 } else if (this->avail_sel != nullptr) {
1353 int to_pos = std::min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST, WidgetDimensions::scaled.framerect.top), this->vscroll->GetCount() - 1);
1354 this->AddGRFToActive(to_pos);
1355 }
1356 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1357 /* Remove active NewGRF file by dragging it over available list. */
1358 Point dummy = {-1, -1};
1359 this->OnClick(dummy, WID_NS_REMOVE, 1);
1360 }
1361
1363
1364 if (this->active_over != -1) {
1365 /* End of drag-and-drop, hide dragged destination highlight. */
1366 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1367 this->active_over = -1;
1368 }
1369 }
1370
1371 void OnMouseDrag(Point pt, WidgetID widget) override
1372 {
1373 if (!this->editable) return;
1374
1375 if (widget == WID_NS_FILE_LIST && (this->active_sel != nullptr || this->avail_sel != nullptr)) {
1376 /* An NewGRF file is dragged over the active list. */
1378 /* Skip the last dummy line if the source is from the active list. */
1379 to_pos = std::min(to_pos, this->vscroll->GetCount() - (this->active_sel != nullptr ? 2 : 1));
1380
1381 if (to_pos != this->active_over) {
1382 this->active_over = to_pos;
1384 }
1385 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1386 this->active_over = -2;
1388 } else if (this->active_over != -1) {
1389 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1390 this->active_over = -1;
1391 }
1392 }
1393
1394private:
1396 static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b)
1397 {
1398 std::string name_a = StrMakeValid(a->GetName(), SVS_NONE); // Make a copy without control codes.
1399 std::string name_b = StrMakeValid(b->GetName(), SVS_NONE); // Make a copy without control codes.
1400 int i = StrNaturalCompare(name_a, name_b, true); // Sort by name (natural sorting).
1401 if (i != 0) return i < 0;
1402
1403 i = a->version - b->version;
1404 if (i != 0) return i < 0;
1405
1406 return a->ident.md5sum < b->ident.md5sum;
1407 }
1408
1410 static bool TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
1411 {
1412 filter.ResetState();
1413 filter.AddLine((*a)->GetName());
1414 filter.AddLine((*a)->filename);
1415 filter.AddLine((*a)->GetDescription());
1416 return filter.GetState();;
1417 }
1418
1419 void BuildAvailables()
1420 {
1421 if (!this->avails.NeedRebuild()) return;
1422
1423 this->avails.clear();
1424
1425 for (const auto &c : _all_grfs) {
1426 if (std::ranges::any_of(this->actives, [&c](const auto &gc) { return gc->ident.HasGrfIdentifier(c->ident.grfid, &c->ident.md5sum); })) continue;
1427
1429 this->avails.push_back(c.get());
1430 } else {
1432 /* Never triggers; FindGRFConfig returns either c, or a newer version of c. */
1433 assert(best != nullptr);
1434
1435 /*
1436 * If the best version is 0, then all NewGRF with this GRF ID
1437 * have version 0, so for backward compatibility reasons we
1438 * want to show them all.
1439 * If we are the best version, then we definitely want to
1440 * show that NewGRF!.
1441 */
1442 if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, &c->ident.md5sum)) {
1443 this->avails.push_back(c.get());
1444 }
1445 }
1446 }
1447
1448 this->avails.Filter(this->string_filter);
1449 this->avails.RebuildDone();
1450 this->avails.Sort();
1451
1452 if (this->avail_sel != nullptr) {
1453 this->avail_pos = find_index(this->avails, this->avail_sel);
1454 if (this->avail_pos == -1) {
1455 this->avail_sel = nullptr;
1456 }
1457 }
1458
1459 this->vscroll2->SetCount(this->avails.size()); // Update the scrollbar
1460 }
1461
1468 {
1469 if (this->avail_sel == nullptr || !this->editable || this->avail_sel->flags.Test(GRFConfigFlag::Invalid)) return false;
1470
1472
1473 /* Get number of non-static NewGRFs. */
1474 size_t count = std::ranges::count_if(this->actives, [](const auto &gc) { return !gc->flags.Test(GRFConfigFlag::Static); });
1475 if (count >= NETWORK_MAX_GRF_COUNT) {
1477 return false;
1478 }
1479
1480 /* Check for duplicate GRF ID. */
1481 if (std::ranges::any_of(this->actives, [&grfid = this->avail_sel->ident.grfid](const auto &gc) { return gc->ident.grfid == grfid; })) {
1483 return false;
1484 }
1485
1486 auto entry = (ins_pos >= 0 && static_cast<size_t>(ins_pos) < std::size(this->actives))
1487 ? std::next(std::begin(this->actives), ins_pos)
1488 : std::end(this->actives);
1489
1490 /* Copy GRF details from scanned list. */
1491 entry = this->actives.insert(entry, std::make_unique<GRFConfig>(*this->avail_sel));
1492 (*entry)->SetParameterDefaults();
1493
1494 /* Select next (or previous, if last one) item in the list. */
1495 int new_pos = this->avail_pos + 1;
1496 if (new_pos >= (int)this->avails.size()) new_pos = this->avail_pos - 1;
1497 this->avail_pos = new_pos;
1498 if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1499
1500 this->avails.ForceRebuild();
1502 return true;
1503 }
1504};
1505
1510void ShowMissingContentWindow(const GRFConfigList &list)
1511{
1512 /* Only show the things in the current list, or everything when nothing's selected */
1513 ContentVector cv;
1514 for (const auto &c : list) {
1515 if (c->status != GCS_NOT_FOUND && !c->flags.Test(GRFConfigFlag::Compatible)) continue;
1516
1517 ContentInfo *ci = new ContentInfo();
1520 ci->name = c->GetName();
1521 ci->unique_id = std::byteswap(c->ident.grfid);
1523 cv.push_back(ci);
1524 }
1525 ShowNetworkContentListWindow(cv.empty() ? nullptr : &cv, CONTENT_TYPE_NEWGRF);
1526}
1527
1530
1531const std::initializer_list<NewGRFWindow::GUIGRFConfigList::SortFunction * const> NewGRFWindow::sorter_funcs = {
1532 &NameSorter,
1533};
1534
1535const std::initializer_list<NewGRFWindow::GUIGRFConfigList::FilterFunction * const> NewGRFWindow::filter_funcs = {
1536 &TagNameFilter,
1537};
1538
1546public:
1547 static const uint MAX_EXTRA_INFO_WIDTH;
1548 static const uint MIN_EXTRA_FOR_3_COLUMNS;
1549
1550 std::unique_ptr<NWidgetBase> avs{};
1551 std::unique_ptr<NWidgetBase> acs{};
1552 std::unique_ptr<NWidgetBase> inf{};
1553 bool editable = true;
1554
1555 NWidgetNewGRFDisplay(std::unique_ptr<NWidgetBase> &&avs, std::unique_ptr<NWidgetBase> &&acs, std::unique_ptr<NWidgetBase> &&inf) : NWidgetBase(NWID_CUSTOM)
1556 , avs(std::move(avs))
1557 , acs(std::move(acs))
1558 , inf(std::move(inf))
1559 {
1560 }
1561
1562 void SetupSmallestSize(Window *w) override
1563 {
1564 /* Copy state flag from the window. */
1565 assert(dynamic_cast<NewGRFWindow *>(w) != nullptr);
1566 NewGRFWindow *ngw = (NewGRFWindow *)w;
1567 this->editable = ngw->editable;
1568
1569 this->avs->SetupSmallestSize(w);
1570 this->acs->SetupSmallestSize(w);
1571 this->inf->SetupSmallestSize(w);
1572
1573 uint min_avs_width = this->avs->smallest_x + this->avs->padding.Horizontal();
1574 uint min_acs_width = this->acs->smallest_x + this->acs->padding.Horizontal();
1575 uint min_inf_width = this->inf->smallest_x + this->inf->padding.Horizontal();
1576
1577 uint min_avs_height = this->avs->smallest_y + this->avs->padding.Vertical();
1578 uint min_acs_height = this->acs->smallest_y + this->acs->padding.Vertical();
1579 uint min_inf_height = this->inf->smallest_y + this->inf->padding.Vertical();
1580
1581 /* Smallest window is in two column mode. */
1582 this->smallest_x = std::max(min_avs_width, min_acs_width) + WidgetDimensions::scaled.hsep_wide + min_inf_width;
1583 this->smallest_y = std::max(min_inf_height, min_acs_height + WidgetDimensions::scaled.vsep_wide + min_avs_height);
1584
1585 /* Filling. */
1586 this->fill_x = std::lcm(this->avs->fill_x, this->acs->fill_x);
1587 if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1588
1589 this->fill_y = this->avs->fill_y;
1590 if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1591 this->fill_y = std::lcm(this->fill_y, this->inf->fill_y);
1592
1593 /* Resizing. */
1594 this->resize_x = std::lcm(this->avs->resize_x, this->acs->resize_x);
1595 if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1596
1597 this->resize_y = this->avs->resize_y;
1598 if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1599 this->resize_y = std::lcm(this->resize_y, this->inf->resize_y);
1600
1601 /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1602 this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1603 }
1604
1605 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
1606 {
1607 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1608
1609 uint min_avs_width = this->avs->smallest_x + this->avs->padding.Horizontal();
1610 uint min_acs_width = this->acs->smallest_x + this->acs->padding.Horizontal();
1611 uint min_inf_width = this->inf->smallest_x + this->inf->padding.Horizontal();
1612
1613 uint min_list_width = std::max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1614 uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1615 uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1616
1617 /* Use 2 or 3 columns? */
1618 uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * WidgetDimensions::scaled.hsep_wide;
1619 uint min_two_columns = min_list_width + min_inf_width + WidgetDimensions::scaled.hsep_wide;
1620 bool use_three_columns = this->editable && (min_three_columns + ScaleGUITrad(MIN_EXTRA_FOR_3_COLUMNS) <= given_width);
1621
1622 /* Info panel is a separate column in both modes. Compute its width first. */
1623 uint extra_width, inf_width;
1624 if (use_three_columns) {
1625 extra_width = given_width - min_three_columns;
1626 inf_width = std::min<uint>(ScaleGUITrad(MAX_EXTRA_INFO_WIDTH), extra_width / 2);
1627 } else {
1628 extra_width = given_width - min_two_columns;
1629 inf_width = std::min<uint>(ScaleGUITrad(MAX_EXTRA_INFO_WIDTH), extra_width / 2);
1630 }
1631 inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1632 extra_width -= inf_width - this->inf->smallest_x;
1633
1634 uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1635
1636 if (use_three_columns) {
1637 /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1638 * Only keep track of what avs gets, all other space goes to acs. */
1639 uint avs_width = std::min(avs_extra_width, extra_width);
1640 extra_width -= avs_width;
1641 extra_width -= std::min(acs_extra_width, extra_width);
1642 avs_width += extra_width / 2;
1643
1644 avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1645
1646 uint acs_width = given_width - // Remaining space, including horizontal padding.
1647 inf_width - this->inf->padding.Horizontal() -
1648 avs_width - this->avs->padding.Horizontal() - 2 * WidgetDimensions::scaled.hsep_wide;
1649 acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1650 this->acs->padding.Horizontal();
1651
1652 /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1653 uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1654 uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1655
1656 /* Assign size and position to the children. */
1657 if (rtl) {
1658 x += this->inf->padding.left;
1659 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding.top, inf_width, inf_height, rtl);
1660 x += inf_width + this->inf->padding.right + WidgetDimensions::scaled.hsep_wide;
1661 } else {
1662 x += this->avs->padding.left;
1663 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding.top, avs_width, avs_height, rtl);
1664 x += avs_width + this->avs->padding.right + WidgetDimensions::scaled.hsep_wide;
1665 }
1666
1667 x += this->acs->padding.left;
1668 this->acs->AssignSizePosition(sizing, x, y + this->acs->padding.top, acs_width, acs_height, rtl);
1669 x += acs_width + this->acs->padding.right + WidgetDimensions::scaled.hsep_wide;
1670
1671 if (rtl) {
1672 x += this->avs->padding.left;
1673 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding.top, avs_width, avs_height, rtl);
1674 } else {
1675 x += this->inf->padding.left;
1676 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding.top, inf_width, inf_height, rtl);
1677 }
1678 } else {
1679 /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1680 * the column is min_list_width wide at least. */
1681 uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1682 this->avs->GetHorizontalStepSize(sizing));
1683 uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1684 this->acs->GetHorizontalStepSize(sizing));
1685
1686 uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding.Vertical() + WidgetDimensions::scaled.vsep_wide;
1687 uint min_acs_height = this->acs->smallest_y + this->acs->padding.Vertical();
1688 uint extra_height = given_height - min_acs_height - min_avs_height;
1689
1690 /* Never use fill_y on these; instead use WidgetDimensions::scaled.vsep_wide as filler */
1691 uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1692 if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1693 uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1694
1695 /* Assign size and position to the children. */
1696 if (rtl) {
1697 x += this->inf->padding.left;
1698 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding.top, inf_width, inf_height, rtl);
1699 x += inf_width + this->inf->padding.right + WidgetDimensions::scaled.hsep_wide;
1700
1701 this->acs->AssignSizePosition(sizing, x + this->acs->padding.left, y + this->acs->padding.top, acs_width, acs_height, rtl);
1702 if (this->editable) {
1703 this->avs->AssignSizePosition(sizing, x + this->avs->padding.left, y + given_height - avs_height - this->avs->padding.bottom, avs_width, avs_height, rtl);
1704 } else {
1705 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1706 }
1707 } else {
1708 this->acs->AssignSizePosition(sizing, x + this->acs->padding.left, y + this->acs->padding.top, acs_width, acs_height, rtl);
1709 if (this->editable) {
1710 this->avs->AssignSizePosition(sizing, x + this->avs->padding.left, y + given_height - avs_height - this->avs->padding.bottom, avs_width, avs_height, rtl);
1711 } else {
1712 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1713 }
1714 uint dx = this->acs->current_x + this->acs->padding.Horizontal();
1715 if (this->editable) {
1716 dx = std::max(dx, this->avs->current_x + this->avs->padding.Horizontal());
1717 }
1718 x += dx + WidgetDimensions::scaled.hsep_wide + this->inf->padding.left;
1719 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding.top, inf_width, inf_height, rtl);
1720 }
1721 }
1722 }
1723
1724 void FillWidgetLookup(WidgetLookup &widget_lookup) override
1725 {
1726 this->avs->FillWidgetLookup(widget_lookup);
1727 this->acs->FillWidgetLookup(widget_lookup);
1728 this->inf->FillWidgetLookup(widget_lookup);
1729 }
1730
1731 NWidgetCore *GetWidgetFromPos(int x, int y) override
1732 {
1733 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1734
1735 NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : nullptr;
1736 if (nw == nullptr) nw = this->acs->GetWidgetFromPos(x, y);
1737 if (nw == nullptr) nw = this->inf->GetWidgetFromPos(x, y);
1738 return nw;
1739 }
1740
1741 void Draw(const Window *w) override
1742 {
1743 if (this->editable) this->avs->Draw(w);
1744 this->acs->Draw(w);
1745 this->inf->Draw(w);
1746 }
1747};
1748
1751
1752static constexpr NWidgetPart _nested_newgrf_actives_widgets[] = {
1754 /* Left side, presets. */
1757 NWidget(WWT_TEXT, INVALID_COLOUR), SetStringTip(STR_NEWGRF_SETTINGS_SELECT_PRESET),
1758 SetPadding(0, WidgetDimensions::unscaled.hsep_wide, 0, 0),
1759 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1760 SetToolTip(STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1761 EndContainer(),
1763 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1764 SetStringTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1765 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1766 SetStringTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1767 EndContainer(),
1768 EndContainer(),
1769
1770 NWidget(WWT_FRAME, COLOUR_MAUVE), SetStringTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0),
1771 /* Left side, active grfs. */
1773 NWidget(WWT_PANEL, COLOUR_MAUVE),
1774 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2),
1775 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetToolTip(STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1776 EndContainer(),
1777 EndContainer(),
1779 EndContainer(),
1780
1781 /* Buttons. */
1782 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1784 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1785 SetStringTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1787 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1788 SetStringTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1789 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1790 SetStringTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1791 EndContainer(),
1792 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1793 SetStringTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1794 EndContainer(),
1795
1797 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1798 SetStringTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1799 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1800 SetStringTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1801 EndContainer(),
1802 EndContainer(),
1803 EndContainer(),
1804 EndContainer(),
1805};
1806
1807static constexpr NWidgetPart _nested_newgrf_availables_widgets[] = {
1808 NWidget(WWT_FRAME, COLOUR_MAUVE), SetStringTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0),
1809 /* Left side, available grfs, filter edit box. */
1811 NWidget(WWT_TEXT, INVALID_COLOUR), SetFill(0, 1), SetStringTip(STR_NEWGRF_FILTER_TITLE),
1812 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetResize(1, 0),
1813 SetStringTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1814 EndContainer(),
1815
1816 /* Left side, available grfs. */
1818 NWidget(WWT_PANEL, COLOUR_MAUVE),
1819 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2),
1821 EndContainer(),
1822 EndContainer(),
1824 EndContainer(),
1825
1826 /* Left side, available grfs, buttons. */
1828 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1829 SetStringTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1831 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1832 SetStringTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1833 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1834 SetStringTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1835 EndContainer(),
1836 EndContainer(),
1837 EndContainer(),
1838};
1839
1840static constexpr NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1842 /* Right side, info panel. */
1843 NWidget(WWT_PANEL, COLOUR_MAUVE),
1844 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1845 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1846 EndContainer(),
1847
1848 /* Right side, info buttons. */
1851 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1852 SetStringTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1854 SetStringTip(STR_TEXTFILE_VIEW_README, STR_TEXTFILE_VIEW_README_TOOLTIP),
1855 EndContainer(),
1858 SetStringTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_TEXTFILE_VIEW_CHANGELOG_TOOLTIP),
1860 SetStringTip(STR_TEXTFILE_VIEW_LICENCE, STR_TEXTFILE_VIEW_LICENCE_TOOLTIP),
1861 EndContainer(),
1862 EndContainer(),
1863
1864 /* Right side, config buttons. */
1865 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1868 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1869 SetStringTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS),
1870 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1871 SetStringTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1872 EndContainer(),
1873 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1874 SetStringTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES),
1875 EndContainer(),
1876 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1877 SetStringTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS),
1878 EndContainer(),
1879 EndContainer(),
1880};
1881
1883std::unique_ptr<NWidgetBase> NewGRFDisplay()
1884{
1885 std::unique_ptr<NWidgetBase> avs = MakeNWidgets(_nested_newgrf_availables_widgets, nullptr);
1886 std::unique_ptr<NWidgetBase> acs = MakeNWidgets(_nested_newgrf_actives_widgets, nullptr);
1887 std::unique_ptr<NWidgetBase> inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, nullptr);
1888
1889 return std::make_unique<NWidgetNewGRFDisplay>(std::move(avs), std::move(acs), std::move(inf));
1890}
1891
1892/* Widget definition of the manage newgrfs window */
1893static constexpr NWidgetPart _nested_newgrf_widgets[] = {
1895 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1896 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetStringTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1897 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1898 EndContainer(),
1899 NWidget(WWT_PANEL, COLOUR_MAUVE),
1901 /* Resize button. */
1903 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1904 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1905 EndContainer(),
1906 EndContainer(),
1907};
1908
1909/* Window definition of the manage newgrfs window */
1910static WindowDesc _newgrf_desc(
1911 WDP_CENTER, "settings_newgrf", 300, 263,
1913 {},
1914 _nested_newgrf_widgets
1915);
1916
1922static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1923{
1924 if (confirmed) {
1927 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1928 assert(nw != nullptr);
1929
1931 _gamelog.GRFUpdate(_grfconfig, nw->actives); // log GRF changes
1932 CopyGRFConfigList(nw->orig_list, nw->actives, false);
1935
1936 /* Show new, updated list */
1937 int pos = nw->GetCurrentActivePosition();
1938
1939 CopyGRFConfigList(nw->actives, nw->orig_list, false);
1940
1941 if (nw->active_sel != nullptr) {
1942 /* Set current selection from position */
1943 if (static_cast<size_t>(pos) >= nw->actives.size()) {
1944 nw->active_sel = nw->actives.back().get();
1945 } else {
1946 auto it = std::next(std::begin(nw->actives), pos);
1947 nw->active_sel = it->get();
1948 }
1949 }
1950 nw->avails.ForceRebuild();
1951 nw->modified = false;
1952
1953 w->InvalidateData();
1954
1955 ReInitAllWindows(false);
1957 }
1958}
1959
1960
1961
1970void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfigList &config)
1971{
1973 new NewGRFWindow(_newgrf_desc, editable, show_params, exec_changes, config);
1974}
1975
1979 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1980 NWidget(WWT_CAPTION, COLOUR_GREY), SetStringTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1981 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1982 EndContainer(),
1983 NWidget(WWT_PANEL, COLOUR_GREY),
1985 NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 2, 2),
1986 SetToolTip(STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
1988 EndContainer(),
1989 EndContainer(),
1990 NWidget(WWT_PANEL, COLOUR_GREY),
1991 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(2, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
1992 SetStringTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
1993 EndContainer(),
1995 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetStringTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1996 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetStringTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1997 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1998 EndContainer(),
1999};
2000
2003 WDP_CENTER, "save_preset", 140, 110,
2007);
2008
2010struct SavePresetWindow : public Window {
2013 Scrollbar *vscroll = nullptr;
2014 int selected = -1;
2015
2021 {
2022 this->presets = GetGRFPresetList();
2023 if (initial_text != nullptr) {
2024 for (uint i = 0; i < this->presets.size(); i++) {
2025 if (this->presets[i] == initial_text) {
2026 this->selected = i;
2027 break;
2028 }
2029 }
2030 }
2031
2033 this->presetname_editbox.ok_button = WID_SVP_SAVE;
2034 this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2035
2036 this->CreateNestedTree();
2037 this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2038 this->FinishInitNested(0);
2039
2040 this->vscroll->SetCount(this->presets.size());
2042 if (initial_text != nullptr) this->presetname_editbox.text.Assign(initial_text);
2043 }
2044
2046 {
2047 }
2048
2050 {
2051 switch (widget) {
2052 case WID_SVP_PRESET_LIST: {
2054 size.height = 0;
2055 for (uint i = 0; i < this->presets.size(); i++) {
2057 size.width = std::max(size.width, d.width + padding.width);
2058 resize.height = std::max(resize.height, d.height);
2059 }
2060 size.height = ClampU((uint)this->presets.size(), 5, 20) * resize.height + padding.height;
2061 break;
2062 }
2063 }
2064 }
2065
2066 void DrawWidget(const Rect &r, WidgetID widget) const override
2067 {
2068 switch (widget) {
2069 case WID_SVP_PRESET_LIST: {
2070 const Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
2072
2073 uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2074 int offset_y = (step_height - GetCharacterHeight(FS_NORMAL)) / 2;
2075 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
2076
2077 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->presets);
2078 for (auto it = first; it != last; ++it) {
2079 int row = static_cast<int>(std::distance(std::begin(this->presets), it));
2080 if (row == this->selected) GfxFillRect(br.left, tr.top, br.right, tr.top + step_height - 1, PC_DARK_BLUE);
2081
2082 DrawString(tr.left, tr.right, tr.top + offset_y, *it, (row == this->selected) ? TC_WHITE : TC_SILVER);
2083 tr.top += step_height;
2084 }
2085 break;
2086 }
2087 }
2088 }
2089
2090 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
2091 {
2092 switch (widget) {
2093 case WID_SVP_PRESET_LIST: {
2094 auto it = this->vscroll->GetScrolledItemFromWidget(this->presets, pt.y, this, WID_SVP_PRESET_LIST);
2095 if (it != this->presets.end()) {
2096 this->selected = it - this->presets.begin();
2097 this->presetname_editbox.text.Assign(*it);
2100 }
2101 break;
2102 }
2103
2104 case WID_SVP_CANCEL:
2105 this->Close();
2106 break;
2107
2108 case WID_SVP_SAVE: {
2110 if (w != nullptr && !StrEmpty(this->presetname_editbox.text.GetText())) w->OnQueryTextFinished(this->presetname_editbox.text.GetText());
2111 this->Close();
2112 break;
2113 }
2114 }
2115 }
2116
2117 void OnResize() override
2118 {
2119 this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST, WidgetDimensions::scaled.framerect.Vertical());
2120 }
2121};
2122
2127static void ShowSavePresetWindow(const char *initial_text)
2128{
2130 new SavePresetWindow(initial_text);
2131}
2132
2135 NWidget(WWT_CAPTION, COLOUR_GREY), SetStringTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2136 NWidget(WWT_PANEL, COLOUR_GREY),
2138 NWidget(WWT_LABEL, INVALID_COLOUR), SetStringTip(STR_NEWGRF_SCAN_MESSAGE), SetFill(1, 0),
2139 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2140 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0), SetMinimalSize(400, 0),
2141 EndContainer(),
2142 EndContainer(),
2143};
2144
2147 WDP_CENTER, nullptr, 0, 0,
2149 {},
2151);
2152
2155 std::string last_name{};
2156 int scanned = 0;
2157
2160 {
2161 this->InitNested(1);
2162 }
2163
2165 {
2166 switch (widget) {
2169 /* We need some spacing for the 'border' */
2172 break;
2173
2174 case WID_SP_PROGRESS_TEXT: {
2176 /* We really don't know the width. We could determine it by scanning the NewGRFs,
2177 * but this is the status window for scanning them... */
2178 size.width = std::max<uint>(size.width, GetStringBoundingBox(GetString(STR_NEWGRF_SCAN_STATUS, max_digits, max_digits)).width + padding.width);
2180 break;
2181 }
2182 }
2183 }
2184
2185 void DrawWidget(const Rect &r, WidgetID widget) const override
2186 {
2187 switch (widget) {
2188 case WID_SP_PROGRESS_BAR: {
2189 /* Draw the % complete with a bar and a text */
2192 uint percent = scanned * 100 / std::max(1U, _settings_client.gui.last_newgrf_count);
2193 DrawFrameRect(ir.WithWidth(ir.Width() * percent / 100, _current_text_dir == TD_RTL), COLOUR_MAUVE, {});
2194 DrawString(ir.left, ir.right, CenterBounds(ir.top, ir.bottom, GetCharacterHeight(FS_NORMAL)), GetString(STR_GENERATION_PROGRESS, percent), TC_FROMSTRING, SA_HOR_CENTER);
2195 break;
2196 }
2197
2199 DrawString(r.left, r.right, r.top, GetString(STR_NEWGRF_SCAN_STATUS, this->scanned, _settings_client.gui.last_newgrf_count), TC_FROMSTRING, SA_HOR_CENTER);
2200
2201 DrawString(r.left, r.right, r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal, this->last_name, TC_BLACK, SA_HOR_CENTER);
2202 break;
2203 }
2204 }
2205
2211 void UpdateNewGRFScanStatus(uint num, const char *name)
2212 {
2213 if (name == nullptr) {
2215 } else {
2216 this->last_name = name;
2217 }
2218 this->scanned = num;
2220
2221 this->SetDirty();
2222 }
2223};
2224
2230void UpdateNewGRFScanStatus(uint num, const char *name)
2231{
2233 if (w == nullptr) w = new ScanProgressWindow();
2234 w->UpdateNewGRFScanStatus(num, name);
2235}
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
List template of 'things' T to sort in a GUI.
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
void RebuildDone()
Notify the sortlist that the rebuild is done.
void SetFiltering(Filtering f)
Import filter conditions.
void SetListing(Listing l)
Import sort conditions.
void SetFilterState(bool state)
Enable or disable the filter.
void SetFilterFuncs(std::span< FilterFunction *const > n_funcs)
Hand the filter function pointers to the GUIList.
bool NeedRebuild() const
Check if a rebuild is needed.
void ForceRebuild()
Force that a rebuild is needed.
bool Sort(Comp compare)
Sort the list.
void SetSortFuncs(std::span< SortFunction *const > n_funcs)
Hand the sort function pointers to the GUIList.
void StartAction(GamelogActionType at)
Stores information about new action, but doesn't allocate it Action is allocated only when there is a...
Definition gamelog.cpp:65
void StopAction()
Stops logging of any changes.
Definition gamelog.cpp:74
void GRFUpdate(const GRFConfigList &oldg, const GRFConfigList &newg)
Compares two NewGRF lists and logs any change.
Definition gamelog.cpp:606
Baseclass for nested widgets.
void StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height)
Store size and position.
uint resize_x
Horizontal resize step (0 means not resizable).
uint fill_x
Horizontal fill stepsize (from initial size, 0 means not resizable).
uint smallest_x
Smallest horizontal size of the widget in a filled window.
uint current_x
Current horizontal size (after resizing).
int pos_y
Vertical position of top-left corner of the widget in the window.
int pos_x
Horizontal position of top-left corner of the widget in the window.
uint smallest_y
Smallest vertical size of the widget in a filled window.
uint fill_y
Vertical fill stepsize (from initial size, 0 means not resizable).
uint resize_y
Vertical resize step (0 means not resizable).
uint current_y
Current vertical size (after resizing).
Base class for a 'real' widget.
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:1253
Custom nested widget container for the NewGRF gui.
bool editable
Editable status of the parent NewGRF window (if false, drop all widgets that make the window editable...
void FillWidgetLookup(WidgetLookup &widget_lookup) override
Fill the Window::widget_lookup with pointers to nested widgets in the tree.
std::unique_ptr< NWidgetBase > acs
Widget with the active grfs list and buttons.
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
static const uint MAX_EXTRA_INFO_WIDTH
Maximal additional width given to the panel.
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
void Draw(const Window *w) override
Draw the widgets of the tree.
std::unique_ptr< NWidgetBase > inf
Info panel.
static const uint MIN_EXTRA_FOR_3_COLUMNS
Minimal additional width needed before switching to 3 columns.
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
std::unique_ptr< NWidgetBase > avs
Widget with the available grfs list and buttons.
Scrollbar data structure.
bool IsVisible(size_type item) const
Checks whether given current item is visible in the list.
void SetCount(size_t num)
Sets the number of elements in the list.
auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Return an iterator pointing to the element of a scrolled widget that a user clicked in.
size_type GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition widget.cpp:2447
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2521
size_type GetCount() const
Gets the number of elements in the list.
EventState UpdateListPositionOnKeyPress(int &list_position, uint16_t keycode) const
Update the given list position as if it were on this scroll bar when the given keycode was pressed.
Definition widget.cpp:2468
void ScrollTowards(size_type position)
Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown...
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
size_type GetPosition() const
Gets the position of the first visible element in the list.
A timeout timer will fire once after the interval.
Definition timer.h:116
void Reset()
Reset the timer, so it will fire again after the timeout.
Definition timer.h:140
RectPadding framerect
Standard padding inside many panels.
Definition window_gui.h:40
RectPadding frametext
Padding inside frame with text.
Definition window_gui.h:41
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
int vsep_normal
Normal vertical spacing.
Definition window_gui.h:58
int vsep_wide
Wide vertical spacing.
Definition window_gui.h:60
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:62
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition config.h:90
int find_index(Container const &container, typename Container::const_reference item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
auto Slide(TIter first, TIter last, TIter position) -> std::pair< TIter, TIter >
Move elements between first and last to a new position, rotating elements in between as necessary.
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close, bool persist)
Show a drop down list.
Definition dropdown.cpp:404
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, bool instant_close, bool persist)
Show a drop down list.
Definition dropdown.cpp:386
Functions related to the drop down widget.
Types related to the drop down widget.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
Functions related to errors.
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition error.h:26
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition error.h:27
@ WL_INFO
Used for DoCommand-like (and some non-fatal AI GUI) errors/information.
Definition error.h:24
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc)
Display an error message in a window.
@ NEWGRF_DIR
Subdirectory for all NewGRFs.
Declarations for savegames operations.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Gamelog _gamelog
Gamelog instance.
Definition gamelog.cpp:31
Functions to be called to log fundamental changes to the game.
@ GLAT_GRF
GRF changed.
Definition gamelog.h:19
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:923
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:658
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:115
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:989
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition gfx.cpp:741
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:775
Dimension GetScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI.
Definition widget.cpp:67
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:243
@ SA_HOR_CENTER
Horizontally center the text.
Definition gfx_type.h:376
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
constexpr NWidgetPart SetMatrixDataTip(uint32_t cols, uint32_t rows, StringID tip={})
Widget part function for setting the data and tooltip of WWT_MATRIX widgets.
constexpr NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
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 SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
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 SetResizeWidgetTypeTip(ResizeWidgetValues widget_type, StringID tip)
Widget part function for setting the resize widget type and tooltip.
std::unique_ptr< NWidgetBase > MakeNWidgets(std::span< const NWidgetPart > nwid_parts, std::unique_ptr< NWidgetBase > &&container)
Construct a nested widget tree from an array of parts.
Definition widget.cpp:3359
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetArrowWidgetTypeTip(ArrowWidgetValues widget_type, StringID tip={})
Widget part function for setting the arrow widget type and tooltip.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:943
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
void ShowQuery(EncodedString &&caption, EncodedString &&message, Window *parent, QueryCallbackProc *callback, bool focus)
Show a confirmation window with standard 'yes' and 'no' buttons The window is aligned to the centre o...
void ShowQueryString(std::string_view str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
Types related to the misc widgets.
@ WID_TF_CAPTION
The caption of the window.
Definition misc_widget.h:50
bool _network_available
is network mode available?
Definition network.cpp:67
Basic functions/variables used all over the place.
Part of the network protocol handling content distribution.
void ShowNetworkContentListWindow(ContentVector *cv=nullptr, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
std::vector< ContentInfo * > ContentVector
Vector with content info.
Base for the NewGRF implementation.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
GRFConfigList _grfconfig
First item in list of current GRF set up.
void CopyGRFConfigList(GRFConfigList &dst, const GRFConfigList &src, bool init_only)
Copy a GRF Config list.
std::string GRFBuildParamList(const GRFConfig &c)
Build a string containing space separated parameter values, and terminate.
const GRFConfig * FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5Hash *md5sum, uint32_t desired_version)
Find a NewGRF in the scanned list.
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
GRFConfigList _all_grfs
First item in list of all scanned NewGRFs.
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
@ GCS_DISABLED
GRF file is disabled.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
@ GCS_ACTIVATED
GRF file has been activated.
@ Compatible
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches)
@ Static
GRF file is used statically (can be used in any MP game)
@ Invalid
GRF is unusable with this version of OpenTTD.
@ PTYPE_UINT_ENUM
The parameter allows a range of numbers, each of which can have a special name.
@ PTYPE_BOOL
The parameter is either 0 or 1.
@ FGCM_NEWEST
Find newest Grf.
@ FGCM_NEWEST_VALID
Find newest Grf, ignoring Grfs with GRFConfigFlag::Invalid set.
@ FGCM_EXACT
Only find Grfs matching md5sum.
@ GRFP_GRF_UNSET
The NewGRF provided no information.
@ GRFP_USE_WINDOWS
The palette state is set to use the Windows palette.
@ GRFP_BLT_32BPP
The NewGRF prefers a 32 bpp blitter.
@ GRFP_USE_MASK
Bitmask to get only the use palette use states.
@ GRFP_GRF_MASK
Bitmask to get only the NewGRF supplied information.
std::map< uint32_t, const GRFConfig * > GrfIdMap
Map of grfid to the grf config.
static WindowDesc _save_preset_desc(WDP_CENTER, "save_preset", 140, 110, WC_SAVE_PRESET, WC_GAME_OPTIONS, WindowDefaultFlag::Modal, _nested_save_preset_widgets)
Window description of the preset save window.
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
static void NewGRFConfirmationCallback(Window *w, bool confirmed)
Callback function for the newgrf 'apply changes' confirmation window.
static WindowDesc _scan_progress_desc(WDP_CENTER, nullptr, 0, 0, WC_MODAL_PROGRESS, WC_NONE, {}, _nested_scan_progress_widgets)
Description of the widgets and other settings of the window.
static void ShowSavePresetWindow(const char *initial_text)
Open the window for saving a preset.
static void FillGrfidMap(const GRFConfigList &lst, GrfIdMap &grfid_map)
Add all grf configs from c into the map.
static constexpr NWidgetPart _nested_scan_progress_widgets[]
Widgets for the progress window.
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfigList &config)
Setup the NewGRF gui.
static constexpr NWidgetPart _nested_save_preset_widgets[]
Widget parts of the save preset window.
std::unique_ptr< NWidgetBase > NewGRFDisplay()
Construct nested container widget for managing the lists and the info panel of the NewGRF GUI.
static WindowDesc _newgrf_parameters_desc(WDP_CENTER, "settings_newgrf_config", 500, 208, WC_GRF_PARAMETERS, WC_NONE, {}, _nested_newgrf_parameter_widgets)
Window definition for the change grf parameters window.
void ShowNewGRFError()
Show the first NewGRF error we can find.
void ShowMissingContentWindow(const GRFConfigList &list)
Show the content list window with all missing grfs from the given list.
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Header of Action 04 "universal holder" structure and functions.
Types related to the newgrf widgets.
@ WID_NS_VIEW_PARAMETERS
Open Parameters Window for selected NewGRF for viewing parameters.
@ WID_NS_APPLY_CHANGES
Apply changes to NewGRF config.
@ WID_NS_OPEN_URL
Open URL of NewGRF.
@ WID_NS_CONTENT_DOWNLOAD2
Open content download (active NewGRFs).
@ WID_NS_RESCAN_FILES
Rescan files (available NewGRFs).
@ WID_NS_CONTENT_DOWNLOAD
Open content download (available NewGRFs).
@ WID_NS_MOVE_UP
Move NewGRF up in active list.
@ WID_NS_SHOW_REMOVE
Select active list buttons (0 = normal, 1 = simple layout).
@ WID_NS_NEWGRF_INFO
Panel for Info on selected NewGRF.
@ WID_NS_NEWGRF_TEXTFILE
Open NewGRF readme, changelog (+1) or license (+2).
@ WID_NS_SET_PARAMETERS
Open Parameters Window for selected NewGRF for editing parameters.
@ WID_NS_ADD
Add NewGRF to active list.
@ WID_NS_MOVE_DOWN
Move NewGRF down in active list.
@ WID_NS_SCROLL2BAR
Scrollbar for available NewGRF list.
@ WID_NS_UPGRADE
Upgrade NewGRFs that have a newer version available.
@ WID_NS_PRESET_SAVE
Save list of active NewGRFs as presets.
@ WID_NS_FILTER
Filter list of available NewGRFs.
@ WID_NS_FILE_LIST
List window of active NewGRFs.
@ WID_NS_SHOW_APPLY
Select display of the buttons below the 'details'.
@ WID_NS_SCROLLBAR
Scrollbar for active NewGRF list.
@ WID_NS_PRESET_DELETE
Delete active preset.
@ WID_NS_TOGGLE_PALETTE
Toggle Palette of selected, active NewGRF.
@ WID_NS_RESCAN_FILES2
Rescan files (active NewGRFs).
@ WID_NS_NEWGRF_INFO_TITLE
Title for Info on selected NewGRF.
@ WID_NS_REMOVE
Remove NewGRF from active list.
@ WID_NS_PRESET_LIST
Active NewGRF preset.
@ WID_NS_AVAIL_LIST
List window of available NewGRFs.
@ WID_SP_PROGRESS_BAR
Simple progress bar.
@ WID_SP_PROGRESS_TEXT
Text explaining what is happening.
@ WID_NP_NUMPAR_INC
Button to increase number of parameters.
@ WID_NP_RESET
Reset button.
@ WID_NP_NUMPAR_DEC
Button to decrease number of parameters.
@ WID_NP_SCROLLBAR
Scrollbar to scroll through all settings.
@ WID_NP_CAPTION
Caption of the window.
@ WID_NP_SETTING_DROPDOWN
Dynamically created dropdown for changing setting value.
@ WID_NP_BACKGROUND
Panel to draw the settings on.
@ WID_NP_ACCEPT
Accept button.
@ WID_NP_NUMPAR
Optional number of parameters.
@ WID_NP_SHOW_NUMPAR
NWID_SELECTION to optionally display WID_NP_NUMPAR.
@ WID_NP_SHOW_DESCRIPTION
NWID_SELECTION to optionally display parameter descriptions.
@ WID_NP_DESCRIPTION
Multi-line description of a parameter.
@ WID_SVP_SAVE
Button to save the preset.
@ WID_SVP_PRESET_LIST
List with available preset names.
@ WID_SVP_SCROLLBAR
Scrollbar for the list available preset names.
@ WID_SVP_CANCEL
Button to cancel saving the preset.
@ WID_SVP_EDITBOX
Edit box for changing the preset name.
bool RequestNewGRFScan(NewGRFScanCallback *callback)
Request a new NewGRF scan.
Definition openttd.cpp:1325
static const uint8_t PC_GREY
Grey palette colour.
static const uint8_t PC_DARK_GREY
Dark grey palette colour.
static const uint8_t PC_DARK_BLUE
Dark blue palette colour.
static const uint8_t PC_BLACK
Black palette colour.
Base for the GUIs that have an edit box in them.
A number of safeguards to prevent using unsafe methods.
StringList GetGRFPresetList()
Get the list of known NewGrf presets.
void SaveGRFPresetToConfig(const char *config_name, GRFConfigList &config)
Save a NewGRF configuration with a preset name.
void DeleteGRFPresetFromConfig(const char *config_name)
Delete a NewGRF configuration by preset name.
GRFConfigList LoadGRFPresetFromConfig(const char *config_name)
Load a NewGRF configuration by preset-name.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:57
Functions related to setting/changing the settings.
void DrawArrowButtons(int x, int y, Colours button_colour, uint8_t state, bool clickable_left, bool clickable_right)
Draw [<][>] boxes.
void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable)
Draw a dropdown button.
void DrawBoolButton(int x, int y, bool state, bool clickable)
Draw a toggle button.
Functions for setting GUIs.
#define SETTING_BUTTON_WIDTH
Width of setting buttons.
#define SETTING_BUTTON_HEIGHT
Height of setting buttons.
Types related to global configuration settings.
Base types for having sorted lists in GUIs.
This file contains all sprite-related enums and defines.
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition sprites.h:1394
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition string.cpp:125
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:80
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:607
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition string_func.h:57
@ SVS_NONE
Allow nothing and replace nothing.
Definition string_type.h:45
@ CS_NUMERAL
Only numeric ones.
Definition string_type.h:26
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
Searching and filtering using a stringterm.
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:248
EncodedString GetEncodedStringWithArgs(StringID str, std::span< const StringParameter > params)
Encode a string with its parameters into an encoded string.
Definition strings.cpp:102
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:338
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
uint64_t GetParamMaxDigits(uint count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:230
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
GUISettings gui
settings related to the GUI
Container for all important information about a piece of content.
uint32_t unique_id
Unique ID; either GRF ID or shortname.
MD5Hash md5sum
The MD5 checksum.
State state
Whether the content info is selected (for download)
std::string name
Name of the content.
ContentType type
Type of content.
@ DOES_NOT_EXIST
The content does not exist in the content system.
Dimensions (a width and height) of a rectangle in 2D.
Data structure describing what to show in the list (filter criteria).
Information about GRF, used in the game and (part of it) in savegames.
MD5Hash original_md5sum
MD5 checksum of original file if only a 'compatible' file was loaded.
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
const char * GetURL() const
Get the grf url.
uint8_t palette
GRFPalette, bitset.
const char * GetDescription() const
Get the grf info.
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
std::vector< uint32_t > param
GRF parameters.
GRFStatus status
NOSAVE: GRFStatus, enum.
void SetValue(const GRFParameterInfo &info, uint32_t value)
Set the value of the given user-changeable parameter.
std::optional< GRFError > error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
GRFConfigFlags flags
NOSAVE: GCF_Flags, bitset.
uint8_t num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
std::string filename
Filename - either with or without full path.
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
const char * GetName() const
Get the name of this grf.
uint32_t min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
uint32_t GetValue(const GRFParameterInfo &info) const
Get the value of the given user-changeable parameter.
bool HasGrfIdentifier(uint32_t grfid, const MD5Hash *md5sum) const
Does the identification match the provided values?
uint32_t grfid
GRF ID (defined by Action 0x08)
MD5Hash md5sum
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Information about one grf parameter.
uint8_t param_nr
GRF parameter to store content in.
GRFParameterType type
The type of this parameter.
uint32_t last_newgrf_count
the numbers of NewGRFs we found during the last scan
bool newgrf_show_old_versions
whether to show old versions in the NewGRF list
bool scenario_developer
activate scenario developer: allow modifying NewGRFs in an existing game
bool newgrf_developer_tools
activate NewGRF developer tools and allow modifying NewGRFs in an existing game
Data structure describing how to show the list (what sort direction and criteria).
Partial widget specification to allow NWidgets to be written nested.
Window for setting the parameters of a NewGRF.
bool clicked_dropdown
Whether the dropdown is open.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
void OnPaint() override
The window must be repainted.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
bool editable
Allow editing parameters.
bool HasParameterInfo(uint nr) const
Test if GRF Parameter Info exists for a given parameter index.
static GRFParameterInfo dummy_parameter_info
Dummy info in case a newgrf didn't provide info about some parameter.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
TimeoutTimer< TimerWindow > unclick_timeout
When reset, unclick the button after a small timeout.
bool action14present
True if action14 information is present.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
int line_height
Height of a row in the matrix widget.
bool clicked_increase
True if the increase button was clicked, false for the decrease button.
void OnDropdownSelect(WidgetID widget, int index) override
A dropdown option associated to this window has been selected.
int32_t clicked_row
The selected parameter, or INT32_MAX when none is selected.
bool closing_dropdown
True, if the dropdown list is currently closing.
void OnDropdownClose(Point, WidgetID widget, int, bool) override
A dropdown window associated to this window has been closed.
GRFConfig & grf_config
Set the parameters of this GRFConfig.
int32_t clicked_button
The row in which a button was clicked or INT32_MAX when none is selected.
static GRFParameterInfo & GetDummyParameterInfo(uint nr)
Get a dummy parameter-info object with default information.
GRFParameterInfo & GetParameterInfo(uint nr) const
Get GRF Parameter Info exists for a given parameter index.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnResize() override
Called after the window got resized.
Callback for NewGRF scanning.
Window for displaying the textfile of a NewGRF.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
const GRFConfig * grf_config
View the textfile of this GRFConfig.
Window for showing NewGRF files.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
bool editable
Is the window editable?
void OnEditboxChanged(WidgetID widget) override
The text in an editbox has been edited.
bool CanUpgradeCurrent()
Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs.
GRFConfig * active_sel
Selected active grf item.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
bool show_params
Are the grf-parameters shown in the info-panel?
void UpgradeCurrent()
Upgrade the currently active set of NewGRFs.
void OnResize() override
Called after the window got resized.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
bool AddGRFToActive(int ins_pos=-1)
Insert a GRF into the active list.
GRFConfigList actives
Temporary active grf list to which changes are made.
int avail_pos
Index of avail_sel if existing, else -1.
int active_over
Active GRF item over which another one is dragged, -1 if none.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnMouseDrag(Point pt, WidgetID widget) override
An 'object' is being dragged at the provided position, highlight the target if possible.
bool modified
The list of active NewGRFs has been modified since the last time they got saved.
bool execute
On pressing 'apply changes' are grf changes applied immediately, or only list is updated.
GRFConfigList & orig_list
List active grfs in the game. Used as initial value, may be updated by the window.
void OnDropdownSelect(WidgetID widget, int index) override
A dropdown option associated to this window has been selected.
EventState OnKeyPress(char32_t key, uint16_t keycode) override
A key has been pressed.
int preset
Selected preset or -1 if none selected.
PaletteID GetPalette(const GRFConfig &c) const
Pick the palette for the sprite of the grf to display.
StringList grf_presets
List of known NewGRF presets.
const GRFConfig * avail_sel
Currently selected available grf. nullptr is none is selected.
void UpdateScrollBars()
Updates the scroll bars for the active and inactive NewGRF lists.
QueryString filter_editbox
Filter editbox;.
static bool TagNameFilter(const GRFConfig *const *a, StringFilter &filter)
Filter grfs by tags/name.
void OnDragDrop(Point pt, WidgetID widget) override
A dragged 'object' has been released.
static Filtering last_filtering
Default filtering of GUIGRFConfigList.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
static const std::initializer_list< GUIGRFConfigList::SortFunction *const > sorter_funcs
Sort functions of the GUIGRFConfigList.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
static const std::initializer_list< GUIGRFConfigList::FilterFunction *const > filter_funcs
Filter functions of the GUIGRFConfigList.
void OnNewGRFsScanned() override
Called whenever the NewGRF scan completed.
GUIGRFConfigList avails
Available (non-active) grfs.
StringFilter string_filter
Filter for available grf.
static Listing last_sorting
Default sorting of GUIGRFConfigList.
static bool NameSorter(const GRFConfig *const &a, const GRFConfig *const &b)
Sort grfs by name.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Coordinates of a point in 2D.
Data stored about a string that can be modified in the GUI.
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
static const int ACTION_CLEAR
Clear editbox.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Class for the save preset window.
QueryString presetname_editbox
Edit box of the save preset.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnResize() override
Called after the window got resized.
SavePresetWindow(const char *initial_text)
Constructor of the save preset window.
int selected
Selected entry in the preset list, or -1 if none selected.
Scrollbar * vscroll
Pointer to the scrollbar widget.
StringList presets
Available presets.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Window for showing the progress of NewGRF scanning.
std::string last_name
The name of the last 'seen' NewGRF.
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
int scanned
The number of NewGRFs that we have seen.
ScanProgressWindow()
Create the window.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
String filter and state.
bool IsEmpty() const
Check whether any filter words were entered.
void SetFilterTerm(std::string_view str)
Set the term to filter on.
void ResetState()
Reset the matching state to process a new item.
void AddLine(const char *str)
Pass another text line from the current item to the filter.
bool GetState() const
Get the matching state of the current item.
The data required to format and validate a single parameter of a string.
void Assign(const std::string_view text)
Copy a string into the textbuffer.
Definition textbuf.cpp:422
const char * GetText() const
Get the current text.
Definition textbuf.cpp:286
Window for displaying a textfile.
TextfileType file_type
Type of textfile to view.
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:168
Data structure for an opened window.
Definition window_gui.h:274
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1050
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1736
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition window_gui.h:321
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:744
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
Definition window.cpp:3164
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:554
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:502
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1038
ResizeInfo resize
Resize information.
Definition window_gui.h:315
void DisableWidget(WidgetID widget_index)
Sets a widget to disabled.
Definition window_gui.h:392
void SetWidgetsDisabledState(bool disab_stat, Args... widgets)
Sets the enabled/disabled status of a list of widgets.
Definition window_gui.h:516
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1726
virtual void OnQueryTextFinished(std::optional< std::string > str)
The query window opened from this window has closed.
Definition window_gui.h:776
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
Definition window.cpp:483
int top
y position of top edge of the window
Definition window_gui.h:311
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1749
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:311
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:382
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
@ CONTENT_TYPE_NEWGRF
The content consists of a NewGRF.
GUI functions related to textfiles.
TextfileType
Additional text files accompanying Tar archives.
@ TFT_LICENSE
Content license.
@ TFT_README
Content readme.
@ TFT_CHANGELOG
Content changelog.
Functions related to tile highlights.
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
Change the cursor and mouse click/drag handling to a mode for performing special operations like tile...
@ HT_DRAG
dragging items in the depot windows
Definition of Interval and OneShot timers.
Definition of the Window system.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:296
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:48
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition widget_type.h:41
@ NWID_CUSTOM
General Custom widget.
Definition widget_type.h:76
@ WWT_PUSHARROWBTN
Normal push-button (no toggle button) with arrow caption.
@ WWT_LABEL
Centered label.
Definition widget_type.h:47
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:69
@ WWT_EDITBOX
a textbox for typing
Definition widget_type.h:61
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_MATRIX
Grid of rows and columns.
Definition widget_type.h:49
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:75
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:67
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_FRAME
Frame.
Definition widget_type.h:50
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:38
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:58
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:55
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:60
@ WWT_TEXT
Pure simple text.
Definition widget_type.h:48
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:70
uint ComputeMaxSize(uint base, uint max_space, uint step)
Return the biggest possible size of a nested widget.
@ SZSP_HORIZONTAL
Display plane with zero size vertically, and filling and resizing horizontally.
@ EqualSize
Containers should keep all their (resizing) children equally large.
SizingType
Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition()
@ AWV_DECREASE
Arrow to the left or in case of RTL to the right.
Definition widget_type.h:21
@ AWV_INCREASE
Arrow to the right or in case of RTL to the left.
Definition widget_type.h:22
std::map< WidgetID, class NWidgetBase * > WidgetLookup
Lookup between widget IDs and NWidget objects.
@ RWV_HIDE_BEVEL
Bevel of resize box is hidden.
Definition widget_type.h:30
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1143
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition window.cpp:1116
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3340
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1155
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1101
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition window.cpp:3106
Window functions not directly related to making/drawing windows.
@ Modal
The window is a modal child of some other window, meaning the parent is 'inactive'.
@ BorderOnly
Draw border only, no background.
@ Lowered
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
@ WDP_CENTER
Center the window.
Definition window_gui.h:146
int WidgetID
Widget ID.
Definition window_type.h:20
@ WN_GAME_OPTIONS_NEWGRF_STATE
NewGRF settings.
Definition window_type.h:27
EventState
State of handling an event.
@ ES_HANDLED
The passed event is handled.
@ ES_NOT_HANDLED
The passed event is not handled.
@ GOID_NEWGRF_CURRENT_LOADED
The current list of active NewGRF has been loaded.
@ GOID_NEWGRF_RESCANNED
NewGRFs were just rescanned.
@ GOID_NEWGRF_CHANGES_APPLIED
The active NewGRF list changes have been applied.
@ GOID_NEWGRF_CHANGES_MADE
Changes have been made to a given NewGRF either through the palette or its parameters.
@ GOID_NEWGRF_LIST_EDITED
List of active NewGRFs is being edited.
@ WC_BUILD_OBJECT
Build object; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
@ WC_SAVE_PRESET
Save preset; Window numbers:
@ WC_GAME_OPTIONS
Game options window; Window numbers:
@ WC_TEXTFILE
textfile; Window numbers:
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
@ WC_QUERY_STRING
Query string window; Window numbers:
@ WC_GRF_PARAMETERS
NewGRF parameters; Window numbers:
@ WC_MODAL_PROGRESS
Progress report of landscape generation; Window numbers:
Functions related to zooming.