From 00af887048a1b6d3d424dcf237843838ba31e2da Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 22 Apr 2012 16:28:32 +0000 Subject: [PATCH] (svn r24170) -Add: Methods for translating between NewGRFClass spec indices and user interface indices. --- src/newgrf_class.h | 3 +++ src/newgrf_class_func.h | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/newgrf_class.h b/src/newgrf_class.h index 2dfeceba34..71b5608d84 100644 --- 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 index b5af35fdbe..c2a30992ae 100644 --- a/src/newgrf_class_func.h +++ b/src/newgrf_class_func.h @@ -132,6 +132,20 @@ DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount() return cnt; } +/** + * 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. @@ -143,6 +157,36 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const return index < this->GetSpecCount() ? this->spec[index] : NULL; } +/** + * 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. @@ -180,5 +224,8 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, 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);