Skip to content

Commit

Permalink
Change: Unify station/waypoint/roadstop/object build-picker window code.
Browse files Browse the repository at this point in the history
These windows now share a common code base for choosing and display class and types.

An additional text filter is added to search types by name instead of just classes.
  • Loading branch information
PeterN committed May 11, 2024
1 parent d2c8b47 commit cdc356e
Show file tree
Hide file tree
Showing 16 changed files with 1,228 additions and 1,658 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ add_files(
palette_func.h
pbs.cpp
pbs.h
picker_gui.cpp
picker_gui.h
progress.cpp
progress.h
querystring_gui.h
Expand Down
15 changes: 10 additions & 5 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2785,7 +2785,6 @@ STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP :{BLACK}Select r

# Rail waypoint construction window
STR_WAYPOINT_CAPTION :{WHITE}Waypoint
STR_WAYPOINT_GRAPHICS_TOOLTIP :{BLACK}Select waypoint type

# Rail station construction window
STR_STATION_BUILD_RAIL_CAPTION :{WHITE}Rail Station Selection
Expand All @@ -2798,8 +2797,16 @@ STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP :{BLACK}Select l
STR_STATION_BUILD_DRAG_DROP :{BLACK}Drag & Drop
STR_STATION_BUILD_DRAG_DROP_TOOLTIP :{BLACK}Build a station using drag & drop

STR_STATION_BUILD_STATION_CLASS_TOOLTIP :{BLACK}Select a station class to display
STR_STATION_BUILD_STATION_TYPE_TOOLTIP :{BLACK}Select the station type to build
STR_PICKER_STATION_CLASS_TOOLTIP :Select a station class to display
STR_PICKER_STATION_TYPE_TOOLTIP :Select a station type to build. Ctrl+Click to add or remove in saved items
STR_PICKER_WAYPOINT_CLASS_TOOLTIP :Select a waypoint class to display
STR_PICKER_WAYPOINT_TYPE_TOOLTIP :Select a waypoint to build. Ctrl+Click to add or remove in saved items
STR_PICKER_ROADSTOP_BUS_CLASS_TOOLTIP :Select a bus station class to display
STR_PICKER_ROADSTOP_BUS_TYPE_TOOLTIP :Select a bus station type to build. Ctrl+Click to add or remove in saved items
STR_PICKER_ROADSTOP_TRUCK_CLASS_TOOLTIP :Select a lorry station class to display
STR_PICKER_ROADSTOP_TRUCK_TYPE_TOOLTIP :Select a lorry station type to build. Ctrl+Click to add or remove in saved items
STR_PICKER_OBJECT_CLASS_TOOLTIP :Select an object class to display
STR_PICKER_OBJECT_TYPE_TOOLTIP :Select an object type to build. Ctrl+Click to add or remove in saved items. Ctrl+Click+Drag to select the area diagonally. Also press Shift to show cost estimate only

STR_STATION_CLASS_DFLT :Default
STR_STATION_CLASS_DFLT_STATION :Default station
Expand Down Expand Up @@ -2942,8 +2949,6 @@ STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND :{BLACK}Purchase

# Object construction window
STR_OBJECT_BUILD_CAPTION :{WHITE}Object Selection
STR_OBJECT_BUILD_TOOLTIP :{BLACK}Select object to build. Ctrl+Click+Drag to select the area diagonally. Also press Shift to show cost estimate only
STR_OBJECT_BUILD_CLASS_TOOLTIP :{BLACK}Select class of the object to build
STR_OBJECT_BUILD_PREVIEW_TOOLTIP :{BLACK}Preview of the object
STR_OBJECT_BUILD_SIZE :{BLACK}Size: {GOLD}{NUM} x {NUM} tiles

Expand Down
6 changes: 3 additions & 3 deletions src/newgrf_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class NewGRFClass {
static void InsertDefaults();

public:
using spec_type = Tspec;
using index_type = Tindex;

uint32_t global_id; ///< Global ID for class, e.g. 'DFLT', 'WAYP', etc.
StringID name; ///< Name of this class.

Expand All @@ -67,8 +70,6 @@ class NewGRFClass {
uint GetSpecCount() const { return static_cast<uint>(this->spec.size()); }
/** Get the number of potentially user-available specs within the class. */
uint GetUISpecCount() const { return this->ui_count; }
int GetUIFromIndex(int index) const;
int GetIndexFromUI(int ui_index) const;

const Tspec *GetSpec(uint index) const;

Expand All @@ -80,7 +81,6 @@ class NewGRFClass {
static void Assign(Tspec *spec);
static uint GetClassCount();
static uint GetUIClassCount();
static Tindex GetUIClass(uint index);
static NewGRFClass *Get(Tindex class_index);

static const Tspec *GetByGrf(uint32_t grfid, uint16_t local_id);
Expand Down
50 changes: 2 additions & 48 deletions src/newgrf_class_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,6 @@ uint NewGRFClass<Tspec, Tindex, Tmax>::GetUIClassCount()
return std::count_if(std::begin(NewGRFClass::classes), std::end(NewGRFClass::classes), [](const auto &cls) { return cls.GetUISpecCount() > 0; });
}

/**
* Get the nth-class with user available specs.
* @param index UI index of a class.
* @return The class ID of the class.
*/
template <typename Tspec, typename Tindex, Tindex Tmax>
Tindex NewGRFClass<Tspec, Tindex, Tmax>::GetUIClass(uint index)
{
for (const auto &cls : NewGRFClass::classes) {
if (cls.GetUISpecCount() == 0) continue;
if (index-- == 0) return cls.Index();
}
NOT_REACHED();
}

/**
* Get a spec from the class at a given index.
* @param index The index where to find the spec.
Expand All @@ -132,38 +117,6 @@ const Tspec *NewGRFClass<Tspec, Tindex, Tmax>::GetSpec(uint index) const
return index < this->GetSpecCount() ? this->spec[index] : nullptr;
}

/**
* Translate a UI spec index into a spec index.
* @param ui_index UI index of the spec.
* @return index of the spec, or -1 if out of range.
*/
template <typename Tspec, typename Tindex, Tindex Tmax>
int NewGRFClass<Tspec, Tindex, Tmax>::GetIndexFromUI(int ui_index) const
{
if (ui_index < 0) return -1;
for (uint i = 0; i < this->GetSpecCount(); i++) {
if (!this->IsUIAvailable(i)) continue;
if (ui_index-- == 0) return i;
}
return -1;
}

/**
* Translate a spec index into a UI spec index.
* @param index index of the spec.
* @return UI index of the spec, or -1 if out of range.
*/
template <typename Tspec, typename Tindex, Tindex Tmax>
int NewGRFClass<Tspec, Tindex, Tmax>::GetUIFromIndex(int index) const
{
if ((uint)index >= this->GetSpecCount()) return -1;
uint ui_index = 0;
for (int i = 0; i < index; i++) {
if (this->IsUIAvailable(i)) ui_index++;
}
return ui_index;
}

/**
* Retrieve a spec by GRF location.
* @param grfid GRF ID of spec.
Expand All @@ -177,7 +130,8 @@ const Tspec *NewGRFClass<Tspec, Tindex, Tmax>::GetByGrf(uint32_t grfid, uint16_t
for (const auto &cls : NewGRFClass::classes) {
for (const auto &spec : cls.spec) {
if (spec == nullptr) continue;
if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) return spec;
if (spec->grf_prop.local_id != local_id) continue;
if ((spec->grf_prop.grffile == nullptr ? 0 : spec->grf_prop.grffile->grfid) == grfid) return spec;
}
}

Expand Down

0 comments on commit cdc356e

Please sign in to comment.