23#include "table/strings.h"
78 if (sep != std::string_view::npos) {
85 if (sep == std::string_view::npos) {
90 it =
_badges.
specs.emplace(it, label, index, class_index);
101 if (index.base() >= std::size(
_badges.
specs))
return nullptr;
125 if (class_index.base() >= std::size(
_badges.
classes))
return nullptr;
132 const std::optional<TimerGameCalendar::Date> introduction_date;
141 :
ScopeResolver(
ro), badge(badge), introduction_date(introduction_date) { }
143 uint32_t
GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter,
bool &available)
const override;
150 if (this->introduction_date.has_value())
return this->introduction_date->base();
185 return this->self_scope.badge.
index.base();
198 :
ResolverObject(badge.grf_prop.grffile, callback, callback_param1, callback_param2), self_scope(*this, badge, introduction_date)
200 assert(feature <= GSF_END);
214 if (parameter >= std::size(grffile.
badge_list))
return UINT_MAX;
217 return std::ranges::find(badges, index) != std::end(badges);
226 assert(b !=
nullptr);
239 for (
const BadgeID &index : src) {
241 if (std::ranges::find(dst, index) != std::end(dst))
continue;
245 if (badge ==
nullptr)
continue;
248 dst.push_back(index);
258 assert(class_badge !=
nullptr);
278 if (group ==
nullptr)
return {0, PAL_NONE};
282 return {group->GetResult(), pal};
296 if (badge.class_index != class_index)
continue;
299 if (ps.
sprite == 0)
continue;
331 std::span<const BadgeClassID> Classes()
const {
return this->
classes; }
356 if (size.width == 0)
continue;
360 uint sort_order = UINT_MAX;
364 this->gui_classes.emplace_back(class_index, column, visible, sort_order, size, label);
365 if (visible) max_column = std::max<uint>(max_column, column);
368 std::sort(std::begin(this->gui_classes), std::end(this->gui_classes));
371 this->column_widths.resize(max_column + 1);
372 for (
const auto &el : this->gui_classes) {
373 if (!el.visible)
continue;
378 for (uint &badge_width : this->column_widths) {
379 if (badge_width == 0)
continue;
390 return std::accumulate(std::begin(this->column_widths), std::end(this->column_widths), 0U);
405 if (badge.name == STR_NULL)
continue;
406 if (!badge.features.Test(feature))
continue;
412 auto it = std::ranges::lower_bound(this->badges, badge.index);
413 if (it != std::end(this->badges) && *it == badge.index)
continue;
415 this->badges.insert(it, badge.index);
426 return std::ranges::any_of(badges, [
this](
const BadgeID &badge) {
return std::ranges::binary_search(this->badges, badge); });
438 if (badges.empty())
return r.top;
440 std::set<BadgeClassID> classes;
446 if (class_badge ==
nullptr || class_badge->
name == STR_NULL)
continue;
449 for (
const BadgeID &index : badges) {
451 if (badge ==
nullptr || badge->
name == STR_NULL)
continue;
462 if (s.empty())
continue;
485 for (
const auto &badge_class : badge_classes.GetClasses()) {
486 if (badge_class.column_group != column_group)
continue;
487 if (!badge_class.visible)
continue;
490 for (
const BadgeID &index : badges) {
492 if (badge.
class_index != badge_class.badge_class)
continue;
495 if (ps.
sprite == 0)
continue;
506template <
class TBase,
bool TEnd = true, FontSize TFs = FS_NORMAL>
509 template <
typename... Args>
510 explicit DropDownBadges(
const std::shared_ptr<GUIBadgeClasses> &badge_classes, std::span<const BadgeID> badges,
GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, Args&&... args)
511 : TBase(std::forward<Args>(args)...), badge_classes(badge_classes), badges(badges), feature(feature), introduction_date(introduction_date)
513 for (
const auto &badge_class : badge_classes->GetClasses()) {
514 if (badge_class.column_group != 0)
continue;
516 dim.height = std::max(dim.height, badge_class.size.height);
520 uint
Height()
const override {
return std::max<uint>(this->dim.height, this->TBase::Height()); }
523 void Draw(
const Rect &full,
const Rect &r,
bool sel, Colours bg_colour)
const override
527 DrawBadgeColumn(r.
WithWidth(this->dim.width, rtl), 0, *this->badge_classes, this->badges, this->feature, this->introduction_date, PAL_NONE);
533 std::shared_ptr<GUIBadgeClasses> badge_classes;
535 const std::span<const BadgeID> badges;
537 const std::optional<TimerGameCalendar::Date> introduction_date;
546std::unique_ptr<DropDownListItem> MakeDropDownListBadgeItem(
const std::shared_ptr<GUIBadgeClasses> &badge_classes, std::span<const BadgeID> badges,
GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date,
StringID str,
int value,
bool masked,
bool shaded)
548 return std::make_unique<DropDownListBadgeItem>(badge_classes, badges, feature, introduction_date, str, value, masked, shaded);
551std::unique_ptr<DropDownListItem> MakeDropDownListBadgeIconItem(
const std::shared_ptr<GUIBadgeClasses> &badge_classes, std::span<const BadgeID> badges,
GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date,
const Dimension &dim,
SpriteID sprite,
PaletteID palette,
StringID str,
int value,
bool masked,
bool shaded)
553 return std::make_unique<DropDownListBadgeIconItem>(badge_classes, badges, feature, introduction_date, dim, sprite, palette, str, value, masked, shaded);
bool Filter(std::span< const BadgeID > badges) const
Test if any of the given badges matches the filtered badge list.
BadgeTextFilter(struct StringFilter &filter, GrfSpecFeature feature)
Construct a badge text filter.
VariableGRFFileProps grf_prop
Sprite information.
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.
Global state for badge definitions.
std::vector< BadgeID > classes
List of known badge classes.
std::vector< Badge > specs
List of known badges.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
Drop down element that draws a list of badges.
uint GetTotalColumnsWidth() const
Get total width of all columns.
static Date date
Current date in days (day counter).
Utility class to create a list of badge classes used by a feature.
std::vector< BadgeClassID > classes
List of badge classes.
UsedBadgeClasses(GrfSpecFeature feature)
Create a list of used badge classes for a feature.
Common drop down list components.
Types related to the drop down widget.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
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.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
@ SA_CENTER
Center both horizontally and vertically.
uint32_t PaletteID
The number of the palette.
Base for the NewGRF implementation.
int DrawBadgeNameList(Rect r, std::span< const BadgeID > badges, GrfSpecFeature)
Draw names for a list of badge labels.
static Badges _badges
Static instance of badge state.
Badge * GetBadge(BadgeID index)
Get a badge if it exists.
void ApplyBadgeFeaturesToClassBadges()
Apply features from all badges to their badge classes.
void MarkBadgeSeen(BadgeID index, GrfSpecFeature feature)
Mark a badge a seen (used) by a feature.
void AppendCopyableBadgeList(std::vector< BadgeID > &dst, std::span< const BadgeID > src, GrfSpecFeature feature)
Append copyable badges from a list onto another.
Badge * GetClassBadge(BadgeClassID class_index)
Get the badge class of a badge label.
static constexpr char BADGE_CLASS_SEPARATOR
Separator to identify badge classes from a label.
static constexpr uint MAX_BADGE_HEIGHT
Maximal height of a badge sprite.
static BadgeClassID GetOrCreateBadgeClass(BadgeID index)
Assign a BadgeClassID to the given badge.
static constexpr uint MAX_BADGE_WIDTH
Maximal width.
Badge & GetOrCreateBadge(std::string_view label)
Register a badge label and return its global index.
uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
Test for a matching badge in a list of badges, returning the number of matching bits.
static Dimension GetBadgeMaximalDimension(BadgeClassID class_index, GrfSpecFeature feature)
Get the largest badge size (within limits) for a badge class.
Badge * GetBadgeByLabel(std::string_view label)
Get a badge by label if it exists.
void DrawBadgeColumn(Rect r, int column_group, const GUIBadgeClasses &badge_classes, std::span< const BadgeID > badges, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
Draw a badge column group.
static PalSpriteID GetBadgeSprite(const Badge &badge, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
Get sprite for the given badge.
void ResetBadges()
Reset badges to the default state.
Functions related to NewGRF badges.
Badge * GetClassBadge(BadgeClassID class_index)
Get the badge class of a badge label.
Types related to NewGRF badges.
@ NameListStop
Stop adding names to the name list after this badge.
@ UseCompanyColour
Apply company colour palette to this badge.
@ NameListFirstOnly
Don't add this name to the name list if not first.
@ Copy
Copy badge to related things.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
@ VSG_SCOPE_SELF
Resolved object itself.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Searching and filtering using a stringterm.
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 all the associated D...
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
TextDirection _current_text_dir
Text direction of the currently selected language.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
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.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
BadgeResolverObject(const Badge &badge, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Constructor of the badge resolver.
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Resolver for a badge scope.
BadgeScopeResolver(ResolverObject &ro, const Badge &badge, const std::optional< TimerGameCalendar::Date > introduction_date)
Scope resolver of a badge.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Dimensions (a width and height) of a rectangle in 2D.
Dynamic data of a loaded NewGRF.
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index)
uint sort_order
Order of element.
std::string_view label
Class label (string owned by the class badge)
uint8_t column_group
Column group in UI. 0 = left, 1 = centre, 2 = right.
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.
Interface for SpriteGroup-s to access the gamestate.
uint32_t callback_param2
Second parameter (var 18) of the callback.
CallbackID callback
Callback being resolved.
uint32_t callback_param1
First parameter (var 10) of the callback.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
ResolverObject & ro
Surrounding resolver object.
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
bool IsEmpty() const
Check whether any filter words were entered.
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.
const struct SpriteGroup * GetSpriteGroup(size_t index) const
Get the SpriteGroup at the specified index.
int16_t Height
Fixed point type for heights.
Definition of the game-calendar-timer.
Functions, definitions and such used only by the GUI.
Functions related to zooming.
@ ZOOM_LVL_NORMAL
The normal zoom level.