OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_badge_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11
12#include "core/flatset_type.hpp"
13#include "dropdown_type.h"
14#include "dropdown_func.h"
15#include "newgrf.h"
16#include "newgrf_badge.h"
17#include "newgrf_badge_config.h"
18#include "newgrf_badge_gui.h"
19#include "newgrf_badge_type.h"
20#include "settings_gui.h"
21#include "spritecache.h"
22#include "strings_func.h"
24#include "window_gui.h"
25#include "window_type.h"
26#include "zoom_func.h"
27
28#include "table/strings.h"
29
31
32#include "safeguards.h"
33
34static constexpr uint MAX_BADGE_HEIGHT = 12;
35static constexpr uint MAX_BADGE_WIDTH = MAX_BADGE_HEIGHT * 2;
36
43static Dimension GetBadgeMaximalDimension(BadgeClassID class_index, GrfSpecFeature feature)
44{
46
47 for (const auto &badge : GetBadges()) {
48 if (badge.class_index != class_index) continue;
49
50 PalSpriteID ps = GetBadgeSprite(badge, feature, std::nullopt, PAL_NONE);
51 if (ps.sprite == 0) continue;
52
53 /* Get unscaled sprite size ignoring offsets. Don't use GetSpriteSize as it applies offsets,
54 * and GetScaledSpriteSize uses interface scale. */
55 const Sprite *sprite = GetSprite(ps.sprite, SpriteType::Normal);
56 if (sprite == nullptr) continue;
57
58 uint width = UnScaleByZoom(sprite->width, ZoomLevel::Normal);
59 d.width = std::max(d.width, width);
60 if (d.width > MAX_BADGE_WIDTH) break;
61 }
62
63 d.width = std::min(d.width, MAX_BADGE_WIDTH);
64 return d;
65}
66
73static auto operator<=>(const GUIBadgeClasses::Element &a, const GUIBadgeClasses::Element &b) noexcept
74{
75 if (a.column_group != b.column_group) return a.column_group <=> b.column_group;
76 if (a.sort_order != b.sort_order) return a.sort_order <=> b.sort_order;
77 return a.label <=> b.label;
78}
79
84GUIBadgeClasses::GUIBadgeClasses(GrfSpecFeature feature) : UsedBadgeClasses(feature)
85{
86 /* Get list of classes used by feature. */
87 uint max_column = 0;
88 for (BadgeClassID class_index : this->Classes()) {
89 const Badge *class_badge = GetClassBadge(class_index);
90 if (class_badge->name == STR_NULL) continue;
91
92 Dimension size = GetBadgeMaximalDimension(class_index, feature);
93 if (size.width == 0) continue;
94
95 const auto [config, sort_order] = GetBadgeClassConfigItem(feature, class_badge->label);
96
97 this->gui_classes.emplace_back(class_index, config.column, config.show_icon, sort_order, size, class_badge->label);
98 if (size.width != 0 && config.show_icon) max_column = std::max(max_column, config.column);
99 }
100
101 std::sort(std::begin(this->gui_classes), std::end(this->gui_classes));
102
103 /* Determine total width of visible badge columns. */
104 this->column_widths.resize(max_column + 1);
105 for (const auto &el : this->gui_classes) {
106 if (!el.visible || el.size.width == 0) continue;
107 this->column_widths[el.column_group] += ScaleGUITrad(el.size.width) + WidgetDimensions::scaled.hsep_normal;
108 }
109
110 /* Replace trailing `hsep_normal` spacer with wider `hsep_wide` spacer. */
111 for (uint &badge_width : this->column_widths) {
112 if (badge_width == 0) continue;
113 badge_width = badge_width - WidgetDimensions::scaled.hsep_normal + WidgetDimensions::scaled.hsep_wide;
114 }
115}
116
122{
123 return std::accumulate(std::begin(this->column_widths), std::end(this->column_widths), 0U);
124}
125
133int DrawBadgeNameList(Rect r, std::span<const BadgeID> badges, GrfSpecFeature)
134{
135 if (badges.empty()) return r.top;
136
137 FlatSet<BadgeClassID> class_indexes;
138 for (const BadgeID &index : badges) class_indexes.insert(GetBadge(index)->class_index);
139
140 std::string_view list_separator = GetListSeparator();
141 for (const BadgeClassID &class_index : class_indexes) {
142 const Badge *class_badge = GetClassBadge(class_index);
143 if (class_badge == nullptr || class_badge->name == STR_NULL) continue;
144
145 std::string s;
146 for (const BadgeID &index : badges) {
147 const Badge *badge = GetBadge(index);
148 if (badge == nullptr || badge->name == STR_NULL) continue;
149 if (badge->class_index != class_index) continue;
150 if (badge->flags.Test(BadgeFlag::NameListSkip)) continue;
151
152 if (!s.empty()) {
153 if (badge->flags.Test(BadgeFlag::NameListFirstOnly)) continue;
154 s += list_separator;
155 }
156 AppendStringInPlace(s, badge->name);
157 if (badge->flags.Test(BadgeFlag::NameListStop)) break;
158 }
159
160 if (s.empty()) continue;
161
162 r.top = DrawStringMultiLine(r, GetString(STR_BADGE_NAME_LIST, class_badge->name, std::move(s)), TextColour::Black);
163 }
164
165 return r.top;
166}
167
178void DrawBadgeColumn(Rect r, int column_group, const GUIBadgeClasses &gui_classes, std::span<const BadgeID> badges, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, PaletteID remap)
179{
180 bool rtl = _current_text_dir == TD_RTL;
181 for (const auto &gc : gui_classes.GetClasses()) {
182 if (gc.column_group != column_group) continue;
183 if (!gc.visible) continue;
184
185 int width = ScaleGUITrad(gc.size.width);
186 for (const BadgeID &index : badges) {
187 const Badge &badge = *GetBadge(index);
188 if (badge.class_index != gc.class_index) continue;
189
190 PalSpriteID ps = GetBadgeSprite(badge, feature, introduction_date, remap);
191 if (ps.sprite == 0) continue;
192
193 DrawSpriteIgnorePadding(ps.sprite, ps.pal, r.WithWidth(width, rtl), {AlignmentH::Centre, AlignmentV::Middle});
194 break;
195 }
196
197 r = r.Indent(width + WidgetDimensions::scaled.hsep_normal, rtl);
198 }
199}
200
202template <class TBase, bool TEnd = true, FontSize TFs = FontSize::Normal>
203class DropDownBadges : public TBase {
204public:
205 template <typename... Args>
206 explicit DropDownBadges(const std::shared_ptr<GUIBadgeClasses> &gui_classes, std::span<const BadgeID> badges, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, Args &&...args) :
207 TBase(std::forward<Args>(args)...), gui_classes(gui_classes), badges(badges), feature(feature), introduction_date(introduction_date)
208 {
209 for (const auto &gc : gui_classes->GetClasses()) {
210 if (gc.column_group != 0) continue;
211 dim.width += ScaleGUITrad(gc.size.width) + WidgetDimensions::scaled.hsep_normal;
212 dim.height = std::max<uint>(dim.height, ScaleGUITrad(gc.size.height));
213 }
214
215 /* Remove trailing `hsep_normal` spacer. */
216 if (dim.width > 0) dim.width -= WidgetDimensions::scaled.hsep_normal;
217 }
218
220 void FilterText(StringFilter &string_filter) const override
221 {
222 for (const BadgeID &badge_index : this->badges) {
223 const Badge *badge = GetBadge(badge_index);
224 if (badge->name == STR_NULL) continue;
225 string_filter.AddLine(GetString(badge->name));
226 }
227 this->TBase::FilterText(string_filter);
228 }
229
230 uint Height() const override
231 {
232 return std::max<uint>(this->dim.height, this->TBase::Height());
233 }
234
235 uint Width() const override
236 {
237 if (this->dim.width == 0) return this->TBase::Width();
238 return this->dim.width + WidgetDimensions::scaled.hsep_normal + this->TBase::Width();
239 }
240
241 int OnClick(const Rect &r, const Point &pt) const override
242 {
243 if (this->dim.width == 0) {
244 return this->TBase::OnClick(r, pt);
245 } else {
246 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
247 return this->TBase::OnClick(r.Indent(this->dim.width + WidgetDimensions::scaled.hsep_normal, rtl), pt);
248 }
249 }
250
251 void Draw(const Rect &full, const Rect &r, bool sel, int click_result, Colours bg_colour) const override
252 {
253 if (this->dim.width == 0) {
254 this->TBase::Draw(full, r, sel, click_result, bg_colour);
255 } else {
256 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
257 DrawBadgeColumn(r.WithWidth(this->dim.width, rtl), 0, *this->gui_classes, this->badges, this->feature, this->introduction_date, PAL_NONE);
258 this->TBase::Draw(full, r.Indent(this->dim.width + WidgetDimensions::scaled.hsep_normal, rtl), sel, click_result, bg_colour);
259 }
260 }
261
262private:
263 std::shared_ptr<GUIBadgeClasses> gui_classes;
264
265 const std::span<const BadgeID> badges;
266 const GrfSpecFeature feature;
267 const std::optional<TimerGameCalendar::Date> introduction_date;
268
269 Dimension dim{};
270};
271
274
275std::unique_ptr<DropDownListItem> MakeDropDownListBadgeItem(const std::shared_ptr<GUIBadgeClasses> &gui_classes, std::span<const BadgeID> badges, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, std::string &&str, int value, bool masked, bool shaded)
276{
277 return std::make_unique<DropDownListBadgeItem>(gui_classes, badges, feature, introduction_date, "", std::move(str), value, masked, shaded);
278}
279
280std::unique_ptr<DropDownListItem> MakeDropDownListBadgeItem(const std::shared_ptr<GUIBadgeClasses> &gui_classes, std::span<const BadgeID> badges, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, Money cost, std::string &&str, int value, bool masked, bool shaded)
281{
282 return std::make_unique<DropDownListBadgeItem>(gui_classes, badges, feature, introduction_date, GetString(STR_JUST_CURRENCY_SHORT, cost), std::move(str), value, masked, shaded);
283}
284
285std::unique_ptr<DropDownListItem> MakeDropDownListBadgeIconItem(const std::shared_ptr<GUIBadgeClasses> &gui_classes, std::span<const BadgeID> badges, GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, Money cost, const Dimension &dim, SpriteID sprite, PaletteID palette, std::string &&str, int value, bool masked, bool shaded)
286{
287 return std::make_unique<DropDownListBadgeIconItem>(gui_classes, badges, feature, introduction_date, GetString(STR_JUST_CURRENCY_SHORT, cost), dim, sprite, palette, std::move(str), value, masked, shaded);
288}
289
293template <class TBase, bool TEnd = true, FontSize TFs = FontSize::Normal>
294class DropDownMover : public TBase {
295public:
296 template <typename... Args>
297 explicit DropDownMover(int click_up, int click_down, Colours button_colour, Args &&...args)
298 : TBase(std::forward<Args>(args)...), click_up(click_up), click_down(click_down), button_colour(button_colour)
299 {
300 }
301
302 uint Height() const override
303 {
304 return std::max<uint>(SETTING_BUTTON_HEIGHT, this->TBase::Height());
305 }
306
307 uint Width() const override
308 {
309 return SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_wide + this->TBase::Width();
310 }
311
312 int OnClick(const Rect &r, const Point &pt) const override
313 {
314 bool rtl = (_current_text_dir == TD_RTL);
315 int w = SETTING_BUTTON_WIDTH;
316
317 Rect br = r.WithWidth(w, TEnd ^ rtl).CentreToHeight(SETTING_BUTTON_HEIGHT);
318 if (br.WithWidth(w / 2, rtl).Contains(pt)) return this->click_up;
319 if (br.WithWidth(w / 2, !rtl).Contains(pt)) return this->click_down;
320
321 return this->TBase::OnClick(r.Indent(w + WidgetDimensions::scaled.hsep_wide, TEnd ^ rtl), pt);
322 }
323
324 void Draw(const Rect &full, const Rect &r, bool sel, int click_result, Colours bg_colour) const override
325 {
326 bool rtl = (_current_text_dir == TD_RTL);
327 int w = SETTING_BUTTON_WIDTH;
328
329 int state = 0;
330 if (sel && click_result != 0) {
331 if (click_result == this->click_up) state = 1;
332 if (click_result == this->click_down) state = 2;
333 }
334
335 Rect br = r.WithWidth(w, TEnd ^ rtl).CentreToHeight(SETTING_BUTTON_HEIGHT);
336 DrawUpDownButtons(br.left, br.top, this->button_colour, state, this->click_up != 0, this->click_down != 0);
337
338 this->TBase::Draw(full, r.Indent(w + WidgetDimensions::scaled.hsep_wide, TEnd ^ rtl), sel, click_result, bg_colour);
339 }
340
341private:
345};
346
347using DropDownListToggleMoverItem = DropDownMover<DropDownToggle<DropDownString<DropDownListItem>>>;
348using DropDownListToggleItem = DropDownToggle<DropDownString<DropDownListItem>>;
349
350enum BadgeClick : int {
351 BADGE_CLICK_NONE,
352 BADGE_CLICK_MOVE_UP,
353 BADGE_CLICK_MOVE_DOWN,
354 BADGE_CLICK_TOGGLE_ICON,
355 BADGE_CLICK_TOGGLE_FILTER,
356};
357
358DropDownList BuildBadgeClassConfigurationList(const GUIBadgeClasses &gui_classes, uint columns, std::span<const StringID> column_separators, Colours bg_colour)
359{
360 DropDownList list;
361
362 list.push_back(MakeDropDownListStringItem(STR_BADGE_CONFIG_RESET, INT_MAX));
363 if (gui_classes.GetClasses().empty()) return list;
364 list.push_back(MakeDropDownListDividerItem());
365 list.push_back(std::make_unique<DropDownUnselectable<DropDownListStringItem>>(GetString(STR_BADGE_CONFIG_ICONS), -1));
366
367 const BadgeClassID front = gui_classes.GetClasses().front().class_index;
368 const BadgeClassID back = gui_classes.GetClasses().back().class_index;
369
370 for (uint i = 0; i < columns; ++i) {
371 for (const auto &gc : gui_classes.GetClasses()) {
372 if (gc.column_group != i) continue;
373 if (gc.size.width == 0) continue;
374
375 bool first = (i == 0 && gc.class_index == front);
376 bool last = (i == columns - 1 && gc.class_index == back);
377 list.push_back(std::make_unique<DropDownListToggleMoverItem>(first ? 0 : BADGE_CLICK_MOVE_UP, last ? 0 : BADGE_CLICK_MOVE_DOWN, Colours::Yellow, gc.visible, BADGE_CLICK_TOGGLE_ICON, Colours::Yellow, bg_colour, GetString(GetClassBadge(gc.class_index)->name), gc.class_index.base()));
378 }
379
380 if (i >= column_separators.size()) continue;
381
382 if (column_separators[i] == STR_NULL) {
383 list.push_back(MakeDropDownListDividerItem());
384 } else {
385 list.push_back(MakeDropDownListStringItem(column_separators[i], INT_MIN + i, false, true));
386 }
387 }
388
389 list.push_back(MakeDropDownListDividerItem());
390 list.push_back(std::make_unique<DropDownUnselectable<DropDownListStringItem>>(GetString(STR_BADGE_CONFIG_FILTERS), -1));
391
392 for (const BadgeClassID &badge_class_index : gui_classes.Classes()) {
393 const Badge *badge = GetClassBadge(badge_class_index);
394 if (!badge->flags.Test(BadgeFlag::HasText)) continue;
395
396 const auto [config, _] = GetBadgeClassConfigItem(gui_classes.GetFeature(), badge->label);
397 list.push_back(std::make_unique<DropDownListToggleItem>(config.show_filter, BADGE_CLICK_TOGGLE_FILTER, Colours::Yellow, bg_colour, GetString(badge->name), badge_class_index.base()));
398 }
399
400 return list;
401}
402
410static void BadgeClassToggleVisibility(GrfSpecFeature feature, Badge &class_badge, int click_result, BadgeFilterChoices &choices)
411{
412 auto config = GetBadgeClassConfiguration(feature);
413 auto it = std::ranges::find(config, class_badge.label, &BadgeClassConfigItem::label);
414 if (it == std::end(config)) return;
415
416 if (click_result == BADGE_CLICK_TOGGLE_ICON) it->show_icon = !it->show_icon;
417 if (click_result == BADGE_CLICK_TOGGLE_FILTER) {
418 it->show_filter = !it->show_filter;
419 if (!it->show_filter) ResetBadgeFilter(choices, class_badge.class_index);
420 }
421}
422
428static void BadgeClassMovePrevious(GrfSpecFeature feature, Badge &class_badge)
429{
430 GUIBadgeClasses gui_classes(feature);
431 if (gui_classes.GetClasses().empty()) return;
432
433 auto config = GetBadgeClassConfiguration(feature);
434 auto it = std::ranges::find(config, class_badge.label, &BadgeClassConfigItem::label);
435 if (it == std::end(config)) return;
436
437 auto pos_cur = std::ranges::find(gui_classes.GetClasses(), class_badge.class_index, &GUIBadgeClasses::Element::class_index);
438 if (pos_cur == std::begin(gui_classes.GetClasses())) {
439 if (it->column > 0) --it->column;
440 return;
441 }
442
443 auto pos_prev = std::ranges::find(config, std::prev(pos_cur)->label, &BadgeClassConfigItem::label);
444 if (it->column > pos_prev->column) {
445 --it->column;
446 } else {
447 /* Rotate elements right so that it is placed before pos_prev, maintaining order of non-visible elements. */
448 std::rotate(pos_prev, it, std::next(it));
449 }
450}
451
458static void BadgeClassMoveNext(GrfSpecFeature feature, Badge &class_badge, uint columns)
459{
460 GUIBadgeClasses gui_classes(feature);
461 if (gui_classes.GetClasses().empty()) return;
462
463 auto config = GetBadgeClassConfiguration(feature);
464 auto it = std::ranges::find(config, class_badge.label, &BadgeClassConfigItem::label);
465 if (it == std::end(config)) return;
466
467 auto pos_cur = std::ranges::find(gui_classes.GetClasses(), class_badge.class_index, &GUIBadgeClasses::Element::class_index);
468 if (std::next(pos_cur) == std::end(gui_classes.GetClasses())) {
469 if (it->column < columns - 1) ++it->column;
470 return;
471 }
472
473 auto pos_next = std::ranges::find(config, std::next(pos_cur)->label, &BadgeClassConfigItem::label);
474 if (it->column < pos_next->column) {
475 ++it->column;
476 } else {
477 /* Rotate elements left so that it is placed after pos_next, maintaining order of non-visible elements. */
478 std::rotate(it, std::next(it), std::next(pos_next));
479 }
480}
481
491bool HandleBadgeConfigurationDropDownClick(GrfSpecFeature feature, uint columns, int result, int click_result, BadgeFilterChoices &choices)
492{
493 if (result == INT_MAX) {
495 return true;
496 }
497
498 Badge *class_badge = GetClassBadge(static_cast<BadgeClassID>(result));
499 if (class_badge == nullptr) return false;
500
501 switch (click_result) {
502 case BADGE_CLICK_MOVE_DOWN: // Move down button.
503 BadgeClassMoveNext(feature, *class_badge, columns);
504 break;
505 case BADGE_CLICK_MOVE_UP: // Move up button.
506 BadgeClassMovePrevious(feature, *class_badge);
507 break;
508 case BADGE_CLICK_TOGGLE_ICON:
509 case BADGE_CLICK_TOGGLE_FILTER:
510 BadgeClassToggleVisibility(feature, *class_badge, click_result, choices);
511 break;
512 default:
513 break;
514 }
515
516 return true;
517}
518
519NWidgetBadgeFilter::NWidgetBadgeFilter(Colours colour, WidgetID index, GrfSpecFeature feature, BadgeClassID badge_class)
520 : NWidgetLeaf(WWT_DROPDOWN, colour, index, WidgetData{ .string = STR_JUST_STRING }, STR_NULL)
521 , feature(feature), badge_class(badge_class)
522{
523 this->SetFill(1, 0);
524 this->SetResize(1, 0);
525}
526
527std::string NWidgetBadgeFilter::GetStringParameter(const BadgeFilterChoices &choices) const
528{
529 auto it = choices.find(this->badge_class);
530 if (it == std::end(choices)) {
531 return ::GetString(STR_BADGE_FILTER_ANY_LABEL, GetClassBadge(this->badge_class)->name);
532 }
533
534 return ::GetString(GetBadge(it->second)->name);
535}
536
543{
544 DropDownList list;
545
546 /* Add item for disabling filtering. */
547 list.push_back(MakeDropDownListStringItem(::GetString(STR_BADGE_FILTER_ANY_LABEL, GetClassBadge(this->badge_class)->name), -1));
548 list.push_back(MakeDropDownListDividerItem());
549
550 /* Add badges */
551 Dimension d = GetBadgeMaximalDimension(this->badge_class, this->feature);
552 d.width = ScaleGUITrad(d.width);
554
555 auto start = list.size();
556
557 const auto *bc = GetClassBadge(this->badge_class);
558
559 for (const Badge &badge : GetBadges()) {
560 if (badge.class_index != this->badge_class) continue;
561 if (badge.index == bc->index) continue;
562 if (badge.name == STR_NULL) continue;
563 if (!badge.features.Test(this->feature)) continue;
564
565 PalSpriteID ps = GetBadgeSprite(badge, this->feature, std::nullopt, palette);
566 if (ps.sprite == 0) {
567 list.push_back(MakeDropDownListStringItem(badge.name, badge.index.base()));
568 } else {
569 list.push_back(MakeDropDownListIconItem(d, ps.sprite, ps.pal, badge.name, badge.index.base()));
570 }
571 }
572
573 std::sort(std::begin(list) + start, std::end(list), DropDownListStringItem::NatSortFunc);
574
575 return list;
576}
577
587std::pair<WidgetID, WidgetID> AddBadgeDropdownFilters(Window *window, WidgetID container_id, WidgetID widget, Colours colour, GrfSpecFeature feature)
588{
589 auto container = window->GetWidget<NWidgetContainer>(container_id);
590 container->Clear(window);
591 WidgetID first = ++widget;
592
593 /* Get list of classes used by feature. */
594 UsedBadgeClasses used(feature);
595
596 for (BadgeClassID class_index : used.Classes()) {
597 const auto [config, _] = GetBadgeClassConfigItem(feature, GetClassBadge(class_index)->label);
598 if (!config.show_filter) continue;
599
600 container->Add(std::make_unique<NWidgetBadgeFilter>(colour, widget, feature, class_index));
601 ++widget;
602 }
603
604 return {first, widget};
605}
606
612void ResetBadgeFilter(BadgeFilterChoices &choices, BadgeClassID badge_class_index)
613{
614 choices.erase(badge_class_index);
615}
616
623void SetBadgeFilter(BadgeFilterChoices &choices, BadgeID badge_index)
624{
625 const Badge *badge = GetBadge(badge_index);
626 assert(badge != nullptr);
627
628 choices[badge->class_index] = badge_index;
629}
std::string label
Class label.
BadgeClassID class_index
Index of class this badge belongs to.
std::string label
Label of badge.
BadgeID index
Index assigned to badge.
BadgeFlags flags
Display flags.
GrfSpecFeatures features
Bitmask of which features use this badge.
StringID name
Short name.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Drop down element that draws a list of badges.
void FilterText(StringFilter &string_filter) const override
Add text from this dropdown item to a string filter.
Drop down component that shows extra buttons to indicate that the item can be moved up or down.
int click_down
Click result for down button. Button is inactive if 0.
Colours button_colour
Colour of buttons.
int click_up
Click result for up button. Button is inactive if 0.
static bool NatSortFunc(std::unique_ptr< const DropDownListItem > const &first, std::unique_ptr< const DropDownListItem > const &second)
Drop down boolean toggle component.
Drop down component that makes the item unselectable.
Flat set implementation that uses a sorted vector for storage.
std::pair< const_iterator, bool > insert(const Tkey &key)
Insert a key into the set, if it does not already exist.
uint GetTotalColumnsWidth() const
Get total width of all columns.
BadgeClassID badge_class
Badge class of this dropdown.
DropDownList GetDropDownList(PaletteID palette=PAL_NONE) const
Get the drop down list of badges for this filter.
Baseclass for container widgets.
void Clear(Window *parent_window)
Clears the container, deleting all widgets that were contained.
StringID GetString() const
Get the string that has been set for this nested widget.
Definition widget.cpp:1287
Leaf widget.
Utility class to create a list of badge classes used by a feature.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:62
std::unique_ptr< DropDownListItem > MakeDropDownListDividerItem()
Creates new DropDownListDividerItem.
Definition dropdown.cpp:36
std::unique_ptr< DropDownListItem > MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
Creates new DropDownListIconItem.
Definition dropdown.cpp:70
std::unique_ptr< DropDownListItem > MakeDropDownListStringItem(StringID str, int value, bool masked, bool shaded)
Creates new DropDownListStringItem.
Definition dropdown.cpp:49
Common drop down list components.
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.
Flat set container implementation.
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour, Alignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:782
void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, Alignment align)
Draw a sprite within a Rect, ignoring the sprite's padding.
Definition widget.cpp:350
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Normal
The most basic (normal) sprite.
Definition gfx_type.h:405
@ Small
Index of the small font in the font tables.
Definition gfx_type.h:250
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Yellow
Yellow.
Definition gfx_type.h:288
@ Black
Black colour.
Definition gfx_type.h:334
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
Base for the NewGRF implementation.
GrfSpecFeature
Definition newgrf.h:81
Badge * GetBadge(BadgeID index)
Get a badge if it exists.
std::span< const Badge > GetBadges()
Get a read-only view of badges.
PalSpriteID GetBadgeSprite(const Badge &badge, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
Get sprite for the given badge.
Badge * GetClassBadge(BadgeClassID class_index)
Get the badge for a badge class index.
Functions related to NewGRF badges.
void ResetBadgeClassConfiguration(GrfSpecFeature feature)
Reset badge class configuration for a feature.
std::pair< const BadgeClassConfigItem &, int > GetBadgeClassConfigItem(GrfSpecFeature feature, std::string_view label)
Get configuration for a badge class.
std::span< BadgeClassConfigItem > GetBadgeClassConfiguration(GrfSpecFeature feature)
Get the badge user configuration for a feature.
Functions related to NewGRF badge configuration.
int DrawBadgeNameList(Rect r, std::span< const BadgeID > badges, GrfSpecFeature)
Draw names for a list of badge labels.
static void BadgeClassMoveNext(GrfSpecFeature feature, Badge &class_badge, uint columns)
Move the badge class to the next position.
void DrawBadgeColumn(Rect r, int column_group, const GUIBadgeClasses &gui_classes, std::span< const BadgeID > badges, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
Draw a badge column group.
static auto operator<=>(const GUIBadgeClasses::Element &a, const GUIBadgeClasses::Element &b) noexcept
Compare two GUIBadgeClasses::Elements.
static void BadgeClassToggleVisibility(GrfSpecFeature feature, Badge &class_badge, int click_result, BadgeFilterChoices &choices)
Toggle badge class visibility.
static constexpr uint MAX_BADGE_HEIGHT
Maximal height of a badge sprite.
static constexpr uint MAX_BADGE_WIDTH
Maximal width.
std::pair< WidgetID, WidgetID > AddBadgeDropdownFilters(Window *window, WidgetID container_id, WidgetID widget, Colours colour, GrfSpecFeature feature)
Add badge drop down filter widgets.
bool HandleBadgeConfigurationDropDownClick(GrfSpecFeature feature, uint columns, int result, int click_result, BadgeFilterChoices &choices)
Handle the badge configuration drop down selection.
static Dimension GetBadgeMaximalDimension(BadgeClassID class_index, GrfSpecFeature feature)
Get the largest badge size (within limits) for a badge class.
void SetBadgeFilter(BadgeFilterChoices &choices, BadgeID badge_index)
Set badge filter choice for a class.
static void BadgeClassMovePrevious(GrfSpecFeature feature, Badge &class_badge)
Move the badge class to the previous position.
void ResetBadgeFilter(BadgeFilterChoices &choices, BadgeClassID badge_class_index)
Reset badge filter choice for a class.
GUI functions related to NewGRF badges.
Types related to NewGRF badges.
@ NameListStop
Stop adding names to the name list after this badge.
@ NameListFirstOnly
Don't add this name to the name list if not first.
@ HasText
Internal flag set if the badge has text.
@ NameListSkip
Don't show name in name list at all.
A number of safeguards to prevent using unsafe methods.
void DrawUpDownButtons(int x, int y, Colours button_colour, uint8_t state, bool clickable_up, bool clickable_down)
Draw [^][v] buttons.
Functions for setting GUIs.
#define SETTING_BUTTON_WIDTH
Width of setting buttons.
#define SETTING_BUTTON_HEIGHT
Height of setting buttons.
Functions to cache sprites in memory.
Definition of base types and functions in a cross-platform compatible way.
std::string_view GetListSeparator()
Get the list separator string for the current language.
Definition strings.cpp:298
void AppendStringInPlace(std::string &result, StringID string)
Resolve the given StringID and append in place into an existing std::string with formatting but no pa...
Definition strings.cpp:433
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:55
Functions related to OTTD's strings.
@ TD_RTL
Text is written right-to-left by default.
Dimensions (a width and height) of a rectangle in 2D.
value_type height
The height of the rectangle.
value_type width
The width of the rectangle.
BadgeClassID class_index
Badge class index.
Combination of a palette sprite and a 'real' sprite.
Definition gfx_type.h:22
SpriteID sprite
The 'real' sprite.
Definition gfx_type.h:23
PaletteID pal
The palette (use PAL_NONE) if not needed).
Definition gfx_type.h:24
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Rect CentreToHeight(int height) const
Centre a vertical dimension within this Rect.
bool Contains(const Point &pt) const
Test if a point falls inside this Rect.
Data structure describing a sprite.
uint16_t width
Width of the sprite.
String filter and state.
Container with the data associated to a single widget.
Data structure for an opened window.
Definition window_gui.h:272
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
int16_t Height
Fixed point type for heights.
Definition tgp.cpp:152
Definition of the game-calendar-timer.
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:49
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:61
Functions, definitions and such used only by the GUI.
Types related to windows.
int WidgetID
Widget ID.
Definition window_type.h:21
Functions related to zooming.
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34
@ Normal
The normal zoom level.
Definition zoom_type.h:26