22#include "table/strings.h"
79 if (sep != std::string_view::npos) {
86 if (sep == std::string_view::npos) {
91 it =
_badges.
specs.emplace(it, label, index, class_index);
102 if (index.base() >= std::size(
_badges.
specs))
return nullptr;
126 if (class_index.base() >= std::size(
_badges.
classes))
return nullptr;
133 const std::optional<TimerGameCalendar::Date> introduction_date;
142 :
ScopeResolver(
ro), badge(badge), introduction_date(introduction_date) { }
144 uint32_t
GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter,
bool &available)
const override;
151 if (this->introduction_date.has_value())
return this->introduction_date->base();
186 return this->self_scope.badge.
index.base();
199 :
ResolverObject(badge.grf_prop.grffile, callback, callback_param1, callback_param2), self_scope(*this, badge, introduction_date)
201 assert(feature <= GSF_END);
215 if (parameter >= std::size(grffile.
badge_list))
return UINT_MAX;
218 return std::ranges::find(badges, index) != std::end(badges);
227 assert(b !=
nullptr);
240 for (
const BadgeID &index : src) {
242 if (std::ranges::find(dst, index) != std::end(dst))
continue;
246 if (badge ==
nullptr)
continue;
249 dst.push_back(index);
259 assert(class_badge !=
nullptr);
279 if (group ==
nullptr)
return {0, PAL_NONE};
283 return {group->GetResult(), pal};
297 if (badge.class_index != class_index)
continue;
300 if (ps.
sprite == 0)
continue;
332 std::span<const BadgeClassID> Classes()
const {
return this->
classes; }
357 if (size.width == 0)
continue;
361 uint sort_order = UINT_MAX;
365 this->gui_classes.emplace_back(class_index, column, visible, sort_order, size, label);
366 if (visible) max_column = std::max<uint>(max_column, column);
369 std::sort(std::begin(this->gui_classes), std::end(this->gui_classes));
372 this->column_widths.resize(max_column + 1);
373 for (
const auto &el : this->gui_classes) {
374 if (!el.visible)
continue;
379 for (uint &badge_width : this->column_widths) {
380 if (badge_width == 0)
continue;
391 return std::accumulate(std::begin(this->column_widths), std::end(this->column_widths), 0U);
406 if (badge.name == STR_NULL)
continue;
407 if (!badge.features.Test(feature))
continue;
413 auto it = std::ranges::lower_bound(this->badges, badge.index);
414 if (it != std::end(this->badges) && *it == badge.index)
continue;
416 this->badges.insert(it, badge.index);
427 return std::ranges::any_of(badges, [
this](
const BadgeID &badge) {
return std::ranges::binary_search(this->badges, badge); });
439 if (badges.empty())
return r.top;
441 std::set<BadgeClassID> classes;
447 if (class_badge ==
nullptr || class_badge->
name == STR_NULL)
continue;
450 for (
const BadgeID &index : badges) {
452 if (badge ==
nullptr || badge->
name == STR_NULL)
continue;
463 if (s.empty())
continue;
484 for (
const auto &badge_class : badge_classes.GetClasses()) {
485 if (badge_class.column_group != column_group)
continue;
486 if (!badge_class.visible)
continue;
489 for (
const BadgeID &index : badges) {
491 if (badge.
class_index != badge_class.badge_class)
continue;
494 if (ps.
sprite == 0)
continue;
505template <
class TBase,
bool TEnd = true, FontSize TFs = FS_NORMAL>
508 template <
typename... Args>
509 explicit DropDownBadges(
const std::shared_ptr<GUIBadgeClasses> &badge_classes, std::span<const BadgeID> badges,
GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, Args&&... args)
510 : TBase(std::forward<Args>(args)...), badge_classes(badge_classes), badges(badges), feature(feature), introduction_date(introduction_date)
512 for (
const auto &badge_class : badge_classes->GetClasses()) {
513 if (badge_class.column_group != 0)
continue;
515 dim.height = std::max(dim.height, badge_class.size.height);
519 uint
Height()
const override {
return std::max<uint>(this->dim.height, this->TBase::Height()); }
522 void Draw(
const Rect &full,
const Rect &r,
bool sel, Colours bg_colour)
const override
526 DrawBadgeColumn(r.
WithWidth(this->dim.width, rtl), 0, *this->badge_classes, this->badges, this->feature, this->introduction_date, PAL_NONE);
532 std::shared_ptr<GUIBadgeClasses> badge_classes;
534 const std::span<const BadgeID> badges;
536 const std::optional<TimerGameCalendar::Date> introduction_date;
545std::unique_ptr<DropDownListItem> MakeDropDownListBadgeItem(
const std::shared_ptr<GUIBadgeClasses> &badge_classes, std::span<const BadgeID> badges,
GrfSpecFeature feature, std::optional<TimerGameCalendar::Date> introduction_date, std::string &&str,
int value,
bool masked,
bool shaded)
547 return std::make_unique<DropDownListBadgeItem>(badge_classes, badges, feature, introduction_date, std::move(str), value, masked, shaded);
550std::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, std::string &&str,
int value,
bool masked,
bool shaded)
552 return std::make_unique<DropDownListBadgeIconItem>(badge_classes, badges, feature, introduction_date, dim, sprite, palette, std::move(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 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.
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.
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.