OpenTTD Source 20250311-master-g40ddc03423
newgrf_badge.cpp File Reference

Functionality for NewGRF badges. More...

#include "stdafx.h"
#include "dropdown_type.h"
#include "dropdown_common_type.h"
#include "newgrf.h"
#include "newgrf_badge.h"
#include "newgrf_badge_type.h"
#include "newgrf_spritegroup.h"
#include "stringfilter_type.h"
#include "strings_func.h"
#include "timer/timer_game_calendar.h"
#include "window_gui.h"
#include "zoom_func.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

class  Badges
 Global state for badge definitions. More...
 
struct  BadgeScopeResolver
 Resolver for a badge scope. More...
 
struct  BadgeResolverObject
 Resolver of badges. More...
 
class  UsedBadgeClasses
 Utility class to create a list of badge classes used by a feature. More...
 
class  DropDownBadges< TBase, TEnd, TFs >
 Drop down element that draws a list of badges. More...
 

Typedefs

using DropDownListBadgeItem = DropDownBadges< DropDownListStringItem >
 
using DropDownListBadgeIconItem = DropDownBadges< DropDownListIconItem >
 

Functions

static BadgeClassID GetOrCreateBadgeClass (BadgeID index)
 Assign a BadgeClassID to the given badge.
 
void ResetBadges ()
 Reset badges to the default state.
 
BadgeGetOrCreateBadge (std::string_view label)
 Register a badge label and return its global index.
 
BadgeGetBadge (BadgeID index)
 Get a badge if it exists.
 
BadgeGetBadgeByLabel (std::string_view label)
 Get a badge by label if it exists.
 
BadgeGetClassBadge (BadgeClassID class_index)
 Get the badge class of a badge label.
 
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.
 
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.
 
void ApplyBadgeFeaturesToClassBadges ()
 Apply features from all badges to their badge classes.
 
static PalSpriteID GetBadgeSprite (const Badge &badge, GrfSpecFeature feature, std::optional< TimerGameCalendar::Date > introduction_date, PaletteID remap)
 Get sprite for the given badge.
 
static Dimension GetBadgeMaximalDimension (BadgeClassID class_index, GrfSpecFeature feature)
 Get the largest badge size (within limits) for a badge class.
 
static bool operator< (const GUIBadgeClasses::Element &a, const GUIBadgeClasses::Element &b)
 
int DrawBadgeNameList (Rect r, std::span< const BadgeID > badges, GrfSpecFeature)
 Draw names for a list of badge labels.
 
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.
 
std::unique_ptr< DropDownListItemMakeDropDownListBadgeItem (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)
 
std::unique_ptr< DropDownListItemMakeDropDownListBadgeIconItem (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)
 

Variables

static constexpr char BADGE_CLASS_SEPARATOR = '/'
 Separator to identify badge classes from a label.
 
static Badges _badges = {}
 Static instance of badge state.
 
static constexpr uint MAX_BADGE_HEIGHT = 12
 Maximal height of a badge sprite.
 
static constexpr uint MAX_BADGE_WIDTH = MAX_BADGE_HEIGHT * 2
 Maximal width.
 

Detailed Description

Functionality for NewGRF badges.

Definition in file newgrf_badge.cpp.

Typedef Documentation

◆ DropDownListBadgeIconItem

◆ DropDownListBadgeItem

Function Documentation

◆ AppendCopyableBadgeList()

void AppendCopyableBadgeList ( std::vector< BadgeID > &  dst,
std::span< const BadgeID src,
GrfSpecFeature  feature 
)

Append copyable badges from a list onto another.

Badges must exist and be marked with the Copy flag.

Parameters
dstDestination badge list.
srcSource badge list.
featureFeature of list.

Definition at line 237 of file newgrf_badge.cpp.

References Copy, Badge::features, Badge::flags, GetBadge(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by FinaliseEngineArray().

◆ ApplyBadgeFeaturesToClassBadges()

void ApplyBadgeFeaturesToClassBadges ( )

Apply features from all badges to their badge classes.

Definition at line 254 of file newgrf_badge.cpp.

References _badges, Badge::features, GetClassBadge(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set(), and Badges::specs.

Referenced by FinaliseBadges().

◆ DrawBadgeColumn()

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.

Parameters
rrect to draw within.
column_groupcolumn to draw.
badge_classesbadge classes.
badgesbadges to draw.
featurefeature being used.
introduction_dateintroduction date of item.
remappalette remap to for company-coloured badges.

Definition at line 480 of file newgrf_badge.cpp.

References _current_text_dir, Badge::class_index, GetBadge(), GetBadgeSprite(), Rect::Indent(), PalSpriteID::pal, SA_CENTER, WidgetDimensions::scaled, ScaleGUITrad(), PalSpriteID::sprite, TD_RTL, and Rect::WithWidth().

Referenced by BuildIndustryWindow::DrawWidget(), and PickerWindow::DrawWidget().

◆ DrawBadgeNameList()

int DrawBadgeNameList ( Rect  r,
std::span< const BadgeID badges,
GrfSpecFeature  feature 
)

◆ GetBadge()

Badge * GetBadge ( BadgeID  index)

Get a badge if it exists.

Parameters
indexIndex of badge.
Returns
Badge with specified index, or nullptr if it does not exist.

Definition at line 99 of file newgrf_badge.cpp.

References _badges, and Badges::specs.

Referenced by AppendCopyableBadgeList(), DrawBadgeColumn(), DrawBadgeNameList(), NewGRFInspectWindow::DrawMainPanelWidget(), GetClassBadge(), MarkBadgeSeen(), and UsedBadgeClasses::UsedBadgeClasses().

◆ GetBadgeByLabel()

Badge * GetBadgeByLabel ( std::string_view  label)

Get a badge by label if it exists.

Parameters
labelLabel of badge.
Returns
Badge with specified label, or nullptr if it does not exist.

Definition at line 110 of file newgrf_badge.cpp.

References _badges, Badge::label, and Badges::specs.

Referenced by FinaliseBadges().

◆ GetBadgeMaximalDimension()

static Dimension GetBadgeMaximalDimension ( BadgeClassID  class_index,
GrfSpecFeature  feature 
)
static

Get the largest badge size (within limits) for a badge class.

Parameters
badge_classBadge class.
featureFeature being used.
Returns
Largest base size of the badge class for the feature.

Definition at line 291 of file newgrf_badge.cpp.

References _badges, GetBadgeSprite(), GetSpriteSize(), MAX_BADGE_HEIGHT, MAX_BADGE_WIDTH, Badges::specs, PalSpriteID::sprite, and ZOOM_LVL_NORMAL.

Referenced by GUIBadgeClasses::GUIBadgeClasses().

◆ GetBadgeSprite()

static PalSpriteID GetBadgeSprite ( const Badge badge,
GrfSpecFeature  feature,
std::optional< TimerGameCalendar::Date >  introduction_date,
PaletteID  remap 
)
static

Get sprite for the given badge.

Parameters
badgeBadge being queried.
featureGRF feature being used.
introduction_dateIntroduction date of the item, if it has one.
remapPalette remap to use if the flag is company-coloured.
Returns
Custom sprite to draw, or 0 if not available.

Definition at line 274 of file newgrf_badge.cpp.

References Badge::flags, SpriteGroup::Resolve(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), and UseCompanyColour.

Referenced by DrawBadgeColumn(), and GetBadgeMaximalDimension().

◆ GetBadgeVariableResult()

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.

Parameters
grffileGRF file of the current varaction.
badgesList of badges to test.
parameterGRF-local badge index.
Returns
true iff the badge is present.

Definition at line 212 of file newgrf_badge.cpp.

References GRFFile::badge_list.

Referenced by AirportScopeResolver::GetVariable(), AirportTileScopeResolver::GetVariable(), VehicleScopeResolver::GetVariable(), HouseScopeResolver::GetVariable(), IndustriesScopeResolver::GetVariable(), IndustryTileScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), and StationScopeResolver::GetVariable().

◆ GetClassBadge()

Badge * GetClassBadge ( BadgeClassID  class_index)

Get the badge class of a badge label.

Parameters
labelLabel to get class of.
Returns
Badge class index of label.

Definition at line 123 of file newgrf_badge.cpp.

References _badges, Badges::classes, and GetBadge().

Referenced by ApplyBadgeFeaturesToClassBadges(), DrawBadgeNameList(), GUIBadgeClasses::GUIBadgeClasses(), and UsedBadgeClasses::UsedBadgeClasses().

◆ GetOrCreateBadge()

Badge & GetOrCreateBadge ( std::string_view  label)

Register a badge label and return its global index.

Parameters
labelBadge label to register.
Returns
Global index of the badge.

Definition at line 68 of file newgrf_badge.cpp.

References _badges, BADGE_CLASS_SEPARATOR, Badge::class_index, GetOrCreateBadge(), GetOrCreateBadgeClass(), Badge::label, and Badges::specs.

Referenced by GetOrCreateBadge().

◆ GetOrCreateBadgeClass()

static BadgeClassID GetOrCreateBadgeClass ( BadgeID  index)
static

Assign a BadgeClassID to the given badge.

Parameters
indexBadge ID of badge that should be assigned.
Returns
new or existing BadgeClassID.

Definition at line 45 of file newgrf_badge.cpp.

References _badges, and Badges::classes.

Referenced by GetOrCreateBadge().

◆ MakeDropDownListBadgeIconItem()

std::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 
)

Definition at line 549 of file newgrf_badge.cpp.

◆ MakeDropDownListBadgeItem()

std::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 
)

Definition at line 544 of file newgrf_badge.cpp.

◆ MarkBadgeSeen()

void MarkBadgeSeen ( BadgeID  index,
GrfSpecFeature  feature 
)

Mark a badge a seen (used) by a feature.

Definition at line 223 of file newgrf_badge.cpp.

References Badge::features, GetBadge(), and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set().

Referenced by ReadBadgeList().

◆ operator<()

static bool operator< ( const GUIBadgeClasses::Element a,
const GUIBadgeClasses::Element b 
)
static

Definition at line 337 of file newgrf_badge.cpp.

◆ ResetBadges()

void ResetBadges ( )

Reset badges to the default state.

Definition at line 58 of file newgrf_badge.cpp.

References _badges.

Referenced by ResetNewGRFData().

Variable Documentation

◆ _badges

◆ BADGE_CLASS_SEPARATOR

constexpr char BADGE_CLASS_SEPARATOR = '/'
staticconstexpr

Separator to identify badge classes from a label.

Definition at line 28 of file newgrf_badge.cpp.

Referenced by GetOrCreateBadge().

◆ MAX_BADGE_HEIGHT

constexpr uint MAX_BADGE_HEIGHT = 12
staticconstexpr

Maximal height of a badge sprite.

Definition at line 263 of file newgrf_badge.cpp.

Referenced by GetBadgeMaximalDimension().

◆ MAX_BADGE_WIDTH

constexpr uint MAX_BADGE_WIDTH = MAX_BADGE_HEIGHT * 2
staticconstexpr

Maximal width.

Definition at line 264 of file newgrf_badge.cpp.

Referenced by GetBadgeMaximalDimension().