# HG changeset patch # User frosch # Date 2012-04-22 16:28:32 # Node ID 1d5d8ee85137c5785edd0c94a89600539a8fc79c # Parent a55aa4bf5bd3f1022746814d65877202e0335d9a (svn r24170) -Add: Methods for translating between NewGRFClass spec indices and user interface indices. diff --git a/src/newgrf_class.h b/src/newgrf_class.h --- a/src/newgrf_class.h +++ b/src/newgrf_class.h @@ -46,6 +46,8 @@ public: uint GetSpecCount() const { return this->count; } /** 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; @@ -57,6 +59,7 @@ public: static void Assign(Tspec *spec); static uint GetClassCount(); static uint GetUIClassCount(); + static Tid GetUIClass(uint index); static NewGRFClass *Get(Tid cls_id); static const Tspec *GetByGrf(uint32 grfid, byte local_id, int *index); diff --git a/src/newgrf_class_func.h b/src/newgrf_class_func.h --- a/src/newgrf_class_func.h +++ b/src/newgrf_class_func.h @@ -133,6 +133,20 @@ DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIC } /** + * Get the nth-class with user available specs. + * @param index UI index of a class. + * @return The class ID of the class. + */ +DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index) +{ + for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { + if (classes[i].GetUISpecCount() == 0) continue; + if (index-- == 0) return (Tid)i; + } + NOT_REACHED(); +} + +/** * Get a spec from the class at a given index. * @param index The index where to find the spec. * @return The spec at given location. @@ -144,6 +158,36 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec * } /** + * 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. + */ +DEFINE_NEWGRF_CLASS_METHOD(int)::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. + */ +DEFINE_NEWGRF_CLASS_METHOD(int)::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. * @param local_id Index within GRF file of spec. @@ -180,5 +224,8 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec * template NewGRFClass *name::Get(Tid cls_id); \ template uint name::GetClassCount(); \ template uint name::GetUIClassCount(); \ + template Tid name::GetUIClass(uint index); \ template const Tspec *name::GetSpec(uint index) const; \ + template int name::GetUIFromIndex(int index) const; \ + template int name::GetIndexFromUI(int ui_index) const; \ template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);