18#include "newgrf_badge_gui.h"
37#include "table/strings.h"
43static std::vector<PickerCallbacks *> &GetPickerCallbacks()
45 static std::vector<PickerCallbacks *> callbacks;
49PickerCallbacks::PickerCallbacks(
const std::string &ini_group) : ini_group(ini_group)
51 GetPickerCallbacks().push_back(
this);
54PickerCallbacks::~PickerCallbacks()
56 auto &callbacks = GetPickerCallbacks();
57 callbacks.erase(std::ranges::find(callbacks,
this));
68 if (group ==
nullptr)
return;
70 callbacks.
saved.clear();
72 std::array<uint8_t, 4> grfid_buf;
74 std::string_view str = item.name;
77 auto grfid_pos = str.find(
'|');
78 if (grfid_pos == std::string_view::npos)
continue;
80 std::string_view grfid_str = str.substr(0, grfid_pos);
83 str = str.substr(grfid_pos + 1);
84 uint32_t grfid = grfid_buf[0] | (grfid_buf[1] << 8) | (grfid_buf[2] << 16) | (grfid_buf[3] << 24);
86 auto [ptr, err] = std::from_chars(str.data(), str.data() + str.size(), localid);
88 if (err == std::errc{} && ptr == str.data() + str.size()) {
89 callbacks.
saved.insert({grfid, localid, 0, 0});
105 std::string key = fmt::format(
"{:08X}|{}", std::byteswap(item.grfid), item.local_id);
145 int r = a.class_index - b.class_index;
146 if (r == 0) r = a.index - b.index;
153 if (filter.btf.has_value() && filter.btf->Filter(filter.
callbacks->
GetTypeBadges(item->class_index, item->index)))
return true;
169 this->window_number = window_number;
175void PickerWindow::ConstructWindow()
180 bool is_active = this->callbacks.
IsActive();
191 if (
auto *nwid = this->GetWidget<NWidgetStacked>(
WID_PW_CLASS_SEL); nwid !=
nullptr) {
193 bool is_vertical = (nwid->parent->parent->type ==
NWID_VERTICAL);
200 this->class_string_filter.
callbacks = &this->callbacks;
211 this->callbacks.
used.clear();
226 if (
auto *nwid = this->GetWidget<NWidgetStacked>(
WID_PW_TYPE_SEL); nwid !=
nullptr) {
228 bool is_vertical = (nwid->parent->parent->type ==
NWID_VERTICAL);
235 this->type_string_filter.
callbacks = &this->callbacks;
244 this->InvalidateData(PICKER_INVALIDATION_ALL);
254 this->callbacks.Close(data);
264 size.height = 5 *
resize.height;
270 size.width +=
resize.width;
271 fill.width =
resize.width;
286 if (this->badge_classes.GetClasses().empty()) size = {0, 0};
299 const int y_step = this->GetWidget<NWidgetResizeBase>(widget)->resize_y;
300 auto [first, last] = vscroll->GetVisibleRangeIterators(this->
classes);
301 for (
auto it = first; it != last; ++it) {
310 assert(this->GetWidget<NWidgetBase>(widget)->GetParentWidget<NWidgetMatrix>()->GetCurrentElement() <
static_cast<int>(this->
types.size()));
311 const auto &item = this->
types[this->GetWidget<NWidgetBase>(widget)->GetParentWidget<
NWidgetMatrix>()->GetCurrentElement()];
320 this->callbacks.
DrawType(x, y, item.class_index, item.index);
325 DrawBadgeColumn({0, by, ir.
Width() - 1, ir.
Height() - 1}, 0, this->badge_classes, this->callbacks.
GetTypeBadges(item.class_index, item.index), feature, std::nullopt, PAL_NONE);
327 if (this->callbacks.
saved.contains(item)) {
328 DrawSprite(SPR_BLOT, PALETTE_TO_YELLOW, 0, 0);
330 if (this->callbacks.
used.contains(item)) {
361 if (it == this->
classes.end())
return;
386 int sel = this->GetWidget<NWidgetBase>(widget)->GetParentWidget<
NWidgetMatrix>()->GetCurrentElement();
387 assert(sel < (
int)this->
types.size());
388 const auto &item = this->
types[sel];
391 auto it = this->callbacks.
saved.find(item);
392 if (it == std::end(this->callbacks.
saved)) {
393 this->callbacks.
saved.insert(item);
395 this->callbacks.
saved.erase(it);
412 if (this->badge_classes.GetClasses().empty())
break;
413 ShowDropDownList(
this, BuildBadgeClassConfigurationList(this->badge_classes, 1, {}), -1, widget, 0,
false,
true);
425 bool reopen = HandleBadgeConfigurationDropDownClick(this->callbacks.GetFeature(), 1, index, click_result);
430 ReplaceDropDownList(
this, BuildBadgeClassConfigurationList(this->badge_classes, 1, {}), -1);
444 if (!gui_scope)
return;
494 if (!type_string_filter.
IsEmpty()) {
495 this->type_string_filter.btf.emplace(this->type_string_filter, this->callbacks.GetFeature());
497 this->type_string_filter.btf.reset();
520 for (
int i = 0; i < count; i++) {
522 if (filter_used && std::none_of(std::begin(this->callbacks.
used), std::end(this->callbacks.used), [i](
const PickerItem &item) { return item.class_index == i; }))
continue;
523 if (filter_saved && std::none_of(std::begin(this->callbacks.
saved), std::end(this->callbacks.saved), [i](
const PickerItem &item) { return item.class_index == i; }))
continue;
535void PickerWindow::EnsureSelectedClassIsValid()
538 if (std::binary_search(std::begin(this->
classes), std::end(this->
classes), class_index))
return;
541 class_index = this->
classes.front();
545 for (
int i = 0; i < count; i++) {
556void PickerWindow::EnsureSelectedClassIsVisible()
559 if (this->
classes.empty())
return;
562 if (it == std::end(this->
classes))
return;
564 int pos =
static_cast<int>(std::distance(std::begin(this->
classes), it));
568void PickerWindow::RefreshUsedTypeList()
572 this->callbacks.
used.clear();
591 this->
types.reserve(this->callbacks.
used.size());
593 if (!show_all && item.class_index != cls_id)
continue;
595 this->
types.emplace_back(item);
597 }
else if (filter_saved) {
599 this->
types.reserve(this->callbacks.
saved.size());
602 if (item.class_index == -1)
continue;
603 if (!show_all && item.class_index != cls_id)
continue;
605 this->
types.emplace_back(item);
607 }
else if (show_all) {
611 this->
types.reserve(total);
613 for (
int class_index : this->
classes) {
615 for (
int i = 0; i < count; i++) {
622 if (cls_id >= 0 && cls_id < this->callbacks.
GetClassCount()) {
624 this->
types.reserve(count);
625 for (
int i = 0; i < count; i++) {
640void PickerWindow::EnsureSelectedTypeIsValid()
644 if (std::any_of(std::begin(this->
types), std::end(this->
types), [class_index, index](
const auto &item) {
return item.class_index == class_index && item.index == index; }))
return;
646 if (!this->
types.empty()) {
647 class_index = this->
types.front().class_index;
648 index = this->
types.front().index;
652 for (
int i = 0; i < count; i++) {
662void PickerWindow::EnsureSelectedTypeIsVisible()
665 if (this->
types.empty()) {
673 auto it = std::ranges::find_if(this->
types, [class_index, index](
const auto &item) {
return item.class_index == class_index && item.index == index; });
674 if (it == std::end(this->
types))
return;
676 int pos =
static_cast<int>(std::distance(std::begin(this->
types), it));
683 static constexpr NWidgetPart picker_class_widgets[] = {
687 NWidget(
WWT_EDITBOX, COLOUR_DARK_GREEN,
WID_PW_CLASS_FILTER),
SetMinimalSize(144, 0),
SetPadding(2),
SetFill(1, 0),
SetStringTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
706 static constexpr NWidgetPart picker_type_widgets[] = {
711 NWidget(
WWT_EDITBOX, COLOUR_DARK_GREEN,
WID_PW_TYPE_FILTER),
SetPadding(2),
SetResize(1, 0),
SetFill(1, 0),
SetStringTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
713 NWidget(
WWT_IMGBTN, COLOUR_DARK_GREEN,
WID_PW_CONFIGURE_BADGES),
SetAspect(WidgetDimensions::ASPECT_UP_DOWN_BUTTON),
SetResize(0, 0),
SetFill(0, 1),
SetSpriteTip(SPR_EXTRA_MENU, STR_BADGE_CONFIG_MENU_TOOLTIP),
731 NWidget(
WWT_EMPTY, INVALID_COLOUR,
WID_PW_TYPE_NAME),
SetPadding(
WidgetDimensions::unscaled.framerect),
SetResize(1, 0),
SetFill(1, 0),
SetMinimalTextLines(1, 0),
742void InvalidateAllPickerWindows()
Class for backupping variables and making sure they are restored later.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
void RebuildDone()
Notify the sortlist that the rebuild is done.
void SetFiltering(Filtering f)
Import filter conditions.
void SetListing(Listing l)
Import sort conditions.
void SetFilterState(bool state)
Enable or disable the filter.
void SetFilterFuncs(std::span< FilterFunction *const > n_funcs)
Hand the filter function pointers to the GUIList.
bool NeedRebuild() const
Check if a rebuild is needed.
void ForceRebuild()
Force that a rebuild is needed.
bool Sort(Comp compare)
Sort the list.
void SetSortFuncs(std::span< SortFunction *const > n_funcs)
Hand the sort function pointers to the GUIList.
Class for PickerClassWindow to collect information and retain state.
virtual int GetSelectedClass() const =0
Get the index of the selected class.
Filtering type_last_filtering
Default filtering of PickerTypeList.
virtual PickerItem GetPickerItem(int cls_id, int id) const =0
Get data about an item.
const std::string ini_group
Ini Group for saving favourites.
virtual void SetSelectedClass(int id) const =0
Set the selected class.
virtual bool IsActive() const =0
Should picker class/type selection be enabled?
virtual StringID GetTypeName(int cls_id, int id) const =0
Get the item of a type.
virtual bool IsTypeAvailable(int cls_id, int id) const =0
Test if an item is currently buildable.
virtual void FillUsedItems(std::set< PickerItem > &items)=0
Fill a set with all items that are used by the current player.
virtual StringID GetTypeTooltip() const =0
Get the tooltip string for the type grid.
virtual bool HasClassChoice() const =0
Are there multiple classes to chose from?
Listing type_last_sorting
Default sorting of PickerTypeList.
std::set< PickerItem > used
Set of items used in the current game by the current company.
virtual StringID GetClassTooltip() const =0
Get the tooltip string for the class list.
virtual int GetTypeCount(int cls_id) const =0
Get the number of types in a class.
virtual int GetClassCount() const =0
Get the number of classes.
virtual void SetSelectedType(int id) const =0
Set the selected type.
virtual StringID GetClassName(int id) const =0
Get the name of a class.
virtual std::set< PickerItem > UpdateSavedItems(const std::set< PickerItem > &src)=0
Update link between grfid/localidx and class_index/index in saved items.
virtual void DrawType(int x, int y, int cls_id, int id) const =0
Draw preview image of an item.
virtual std::span< const BadgeID > GetTypeBadges(int cls_id, int id) const =0
Get the item of a type.
Filtering class_last_filtering
Default filtering of PickerClassList.
virtual int GetSelectedType() const =0
Get the selected type.
uint8_t mode
Bitmask of PickerFilterModes.
std::set< PickerItem > saved
Set of saved favourite items.
Listing class_last_sorting
Default sorting of PickerClassList.
Base class for windows opened from a toolbar.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
bool has_class_picker
Set if this window has a class picker 'component'.
static const int PREVIEW_LEFT
Offset from left edge to draw preview.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
@ Position
Update scroll positions.
@ Class
Refresh the class list.
@ Type
Refresh the type list.
@ Validate
Validate selected item.
PickerTypeList types
List of types.
QueryString class_editbox
Filter editbox.
bool has_type_picker
Set if this window has a type picker 'component'.
void BuildPickerClassList()
Builds the filter list of classes.
@ PFM_USED
Show used types.
@ PFM_ALL
Show all classes.
@ PFM_SAVED
Show saved types.
void OnEditboxChanged(WidgetID wid) override
The text in an editbox has been edited.
void BuildPickerTypeList()
Builds the filter list of types.
void OnDropdownSelect(WidgetID widget, int index, int click_result) override
A dropdown option associated to this window has been selected.
void OnResize() override
Called after the window got resized.
@ PCWHK_FOCUS_FILTER_BOX
Focus the edit box for editing the filter string.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
static const int PREVIEW_WIDTH
Width of each preview button.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
QueryString type_editbox
Filter editbox.
static const int PREVIEW_BOTTOM
Offset from bottom edge to draw preview.
EventState OnHotkey(int hotkey) override
A hotkey has been pressed.
void OnInit() override
Notification that the nested widget tree gets initialized.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
PickerClassList classes
List of classes.
static const int PREVIEW_HEIGHT
Height of each preview button.
Functions related to companies.
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width, bool instant_close, bool persist)
Show a drop down list.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
bool _ctrl_pressed
Is Ctrl pressed?
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
@ FS_NORMAL
Index of the normal font in the font tables.
@ SA_CENTER
Center both horizontally and vertically.
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
GUI functions that shouldn't be here.
Hotkey related functions.
Types related to reading/writing '*.ini' files.
Functions related to NewGRF badges.
Functions related to NewGRF badge configuration.
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
static const std::initializer_list< PickerClassList::FilterFunction *const > _class_filter_funcs
Filter functions of the PickerClassList.
static bool TypeIDSorter(PickerItem const &a, PickerItem const &b)
Sort types by id.
static void PickerLoadConfig(const IniFile &ini, PickerCallbacks &callbacks)
Load favourites of a picker from config.
static const std::initializer_list< PickerTypeList::SortFunction *const > _type_sorter_funcs
Sort functions of the PickerTypeList.
static bool ClassIDSorter(int const &a, int const &b)
Sort classes by id.
static const std::initializer_list< PickerClassList::SortFunction *const > _class_sorter_funcs
Sort functions of the PickerClassList.
std::unique_ptr< NWidgetBase > MakePickerClassWidgets()
Create nested widgets for the class picker widgets.
static bool TypeTagNameFilter(PickerItem const *item, PickerFilterData &filter)
Filter types by class name.
static const std::initializer_list< PickerTypeList::FilterFunction *const > _type_filter_funcs
Filter functions of the PickerTypeList.
static void PickerSaveConfig(IniFile &ini, const PickerCallbacks &callbacks)
Save favourites of a picker to config.
std::unique_ptr< NWidgetBase > MakePickerTypeWidgets()
Create nested widgets for the type picker widgets.
static bool ClassTagNameFilter(int const *item, PickerFilterData &filter)
Filter classes by class name.
Base for the GUIs that have an edit box in them.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Types related to global configuration settings.
Base types for having sorted lists in GUIs.
Functions related to sound.
@ SND_15_BEEP
19 == 0x13 GUI button click
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Functions related to low-level strings.
Searching and filtering using a stringterm.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Functions related to OTTD's strings.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
SoundSettings sound
sound effect settings
Dimensions (a width and height) of a rectangle in 2D.
Data about how and where to blit pixels.
Ini file that supports both loading and saving.
A group within an ini file.
void Clear()
Clear all items in the group.
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
std::list< IniItem > items
all items in the group
A single "line" in an ini file.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.
const PickerCallbacks * callbacks
Callbacks for filter functions to access to callbacks.
Coordinates of a point in 2D.
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
static const int ACTION_CLEAR
Clear editbox.
constexpr uint Horizontal() const
Get total horizontal padding of RectPadding.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
int Height() const
Get height of Rect.
bool click_beep
Beep on a random selection of buttons.
bool IsEmpty() const
Check whether any filter words were entered.
void SetFilterTerm(std::string_view str)
Set the term to filter on.
void ResetState()
Reset the matching state to process a new item.
bool GetState() const
Get the matching state of the current item.
std::string_view GetText() const
Get the current text.
High level window description.
Data structure for an opened window.
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
ResizeInfo resize
Resize information.
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
bool IsWidgetDisabled(WidgetID widget_index) const
Gets the enabled/disabled status of a widget.
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
const NWidgetCore * nested_focus
Currently focused nested widget, or nullptr if no nested widget has focus.
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
int width
width of the window (number of pixels to the right in x direction)
WindowNumber window_number
Window number within the window class.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
void SetFocusedWindow(Window *w)
Set the window that has the focus.
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
Types related to windows.
EventState
State of handling an event.
@ ES_HANDLED
The passed event is handled.
@ ES_NOT_HANDLED
The passed event is not handled.
@ WC_BUILD_OBJECT
Build object; Window numbers:
@ WC_BUILD_HOUSE
Build house; Window numbers:
@ WC_SELECT_STATION
Select station (when joining stations); Window numbers:
@ WC_TRUCK_STATION
Build truck station; Window numbers:
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
@ WC_BUS_STATION
Build bus station; Window numbers:
@ WC_BUILD_WAYPOINT
Build waypoint; Window numbers:
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.