28#include "table/strings.h"
48 if (badge.class_index != class_index)
continue;
51 if (ps.
sprite == 0)
continue;
56 if (sprite ==
nullptr)
continue;
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;
84GUIBadgeClasses::GUIBadgeClasses(
GrfSpecFeature feature) : UsedBadgeClasses(feature)
88 for (BadgeClassID class_index : this->Classes()) {
90 if (class_badge->
name == STR_NULL)
continue;
93 if (size.
width == 0)
continue;
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);
101 std::sort(std::begin(this->gui_classes), std::end(this->gui_classes));
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;
111 for (uint &badge_width : this->column_widths) {
112 if (badge_width == 0)
continue;
123 return std::accumulate(std::begin(this->column_widths), std::end(this->column_widths), 0U);
135 if (badges.empty())
return r.top;
138 for (
const BadgeID &index : badges) class_indexes.
insert(
GetBadge(index)->class_index);
141 for (
const BadgeClassID &class_index : class_indexes) {
143 if (class_badge ==
nullptr || class_badge->
name == STR_NULL)
continue;
146 for (
const BadgeID &index : badges) {
148 if (badge ==
nullptr || badge->
name == STR_NULL)
continue;
160 if (s.empty())
continue;
181 for (
const auto &gc : gui_classes.GetClasses()) {
182 if (gc.column_group != column_group)
continue;
183 if (!gc.visible)
continue;
186 for (
const BadgeID &index : badges) {
191 if (ps.
sprite == 0)
continue;
202template <
class TBase,
bool TEnd = true, FontSize TFs = FontSize::Normal>
203class DropDownBadges :
public TBase {
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)
209 for (
const auto &gc : gui_classes->GetClasses()) {
210 if (gc.column_group != 0)
continue;
212 dim.height = std::max<uint>(dim.height,
ScaleGUITrad(gc.size.height));
222 for (
const BadgeID &badge_index : this->badges) {
224 if (badge->
name == STR_NULL)
continue;
227 this->TBase::FilterText(string_filter);
230 uint
Height()
const override
232 return std::max<uint>(this->dim.
height, this->TBase::Height());
235 uint Width()
const override
237 if (this->dim.width == 0)
return this->TBase::Width();
241 int OnClick(
const Rect &r,
const Point &pt)
const override
243 if (this->dim.width == 0) {
244 return this->TBase::OnClick(r, pt);
251 void Draw(
const Rect &full,
const Rect &r,
bool sel,
int click_result,
Colours bg_colour)
const override
253 if (this->dim.width == 0) {
254 this->TBase::Draw(full, r, sel, click_result, bg_colour);
257 DrawBadgeColumn(r.
WithWidth(this->dim.width, rtl), 0, *this->gui_classes, this->badges, this->feature, this->introduction_date, PAL_NONE);
263 std::shared_ptr<GUIBadgeClasses> gui_classes;
265 const std::span<const BadgeID> badges;
267 const std::optional<TimerGameCalendar::Date> introduction_date;
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)
277 return std::make_unique<DropDownListBadgeItem>(gui_classes, badges, feature, introduction_date,
"", std::move(str), value, masked, shaded);
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)
282 return std::make_unique<DropDownListBadgeItem>(gui_classes, badges, feature, introduction_date,
GetString(STR_JUST_CURRENCY_SHORT, cost), std::move(str), value, masked, shaded);
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)
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);
293template <
class TBase,
bool TEnd = true, FontSize TFs = FontSize::Normal>
294class DropDownMover :
public TBase {
296 template <
typename... Args>
302 uint
Height()
const override
307 uint Width()
const override
312 int OnClick(
const Rect &r,
const Point &pt)
const override
324 void Draw(
const Rect &full,
const Rect &r,
bool sel,
int click_result,
Colours bg_colour)
const override
330 if (sel && click_result != 0) {
331 if (click_result == this->click_up) state = 1;
332 if (click_result == this->click_down) state = 2;
336 DrawUpDownButtons(br.left, br.top, this->button_colour, state, this->click_up != 0, this->click_down != 0);
350enum BadgeClick :
int {
353 BADGE_CLICK_MOVE_DOWN,
354 BADGE_CLICK_TOGGLE_ICON,
355 BADGE_CLICK_TOGGLE_FILTER,
363 if (gui_classes.GetClasses().empty())
return list;
367 const BadgeClassID front = gui_classes.GetClasses().front().class_index;
368 const BadgeClassID back = gui_classes.GetClasses().back().class_index;
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;
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()));
380 if (i >= column_separators.size())
continue;
382 if (column_separators[i] == STR_NULL) {
392 for (
const BadgeClassID &badge_class_index : gui_classes.Classes()) {
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()));
414 if (it == std::end(config))
return;
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;
431 if (gui_classes.GetClasses().empty())
return;
435 if (it == std::end(config))
return;
438 if (pos_cur == std::begin(gui_classes.GetClasses())) {
439 if (it->column > 0) --it->column;
444 if (it->column > pos_prev->column) {
448 std::rotate(pos_prev, it, std::next(it));
461 if (gui_classes.GetClasses().empty())
return;
465 if (it == std::end(config))
return;
468 if (std::next(pos_cur) == std::end(gui_classes.GetClasses())) {
469 if (it->column < columns - 1) ++it->column;
474 if (it->column < pos_next->column) {
478 std::rotate(it, std::next(it), std::next(pos_next));
493 if (result == INT_MAX) {
499 if (class_badge ==
nullptr)
return false;
501 switch (click_result) {
502 case BADGE_CLICK_MOVE_DOWN:
505 case BADGE_CLICK_MOVE_UP:
508 case BADGE_CLICK_TOGGLE_ICON:
509 case BADGE_CLICK_TOGGLE_FILTER:
521 , feature(feature), badge_class(badge_class)
527std::string NWidgetBadgeFilter::GetStringParameter(
const BadgeFilterChoices &choices)
const
530 if (it == std::end(choices)) {
555 auto start = list.size();
560 if (badge.
class_index != this->badge_class)
continue;
561 if (badge.
index == bc->index)
continue;
562 if (badge.
name == STR_NULL)
continue;
590 container->
Clear(window);
596 for (BadgeClassID class_index : used.Classes()) {
598 if (!config.show_filter)
continue;
600 container->Add(std::make_unique<NWidgetBadgeFilter>(colour, widget, feature, class_index));
604 return {first, widget};
614 choices.erase(badge_class_index);
626 assert(badge !=
nullptr);
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.
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.
Utility class to create a list of badge classes used by a feature.
std::unique_ptr< DropDownListItem > MakeDropDownListDividerItem()
Creates new DropDownListDividerItem.
std::unique_ptr< DropDownListItem > MakeDropDownListIconItem(SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
Creates new DropDownListIconItem.
std::unique_ptr< DropDownListItem > MakeDropDownListStringItem(StringID str, int value, bool masked, bool shaded)
Creates new DropDownListStringItem.
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.
void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, Alignment align)
Draw a sprite within a Rect, ignoring the sprite's padding.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
@ Normal
The most basic (normal) sprite.
@ Small
Index of the small font in the font tables.
uint32_t PaletteID
The number of the palette.
Colours
One of 16 base colours used for companies and windows/widgets.
#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.
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.
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...
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
TextDirection _current_text_dir
Text direction of the currently selected language.
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.
SpriteID sprite
The 'real' sprite.
PaletteID pal
The palette (use PAL_NONE) if not needed).
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.
Data structure for an opened window.
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 of the game-calendar-timer.
Functions, definitions and such used only by the GUI.
Types related to windows.
Functions related to zooming.
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
@ Normal
The normal zoom level.