(svn r24170) -Add: Methods for translating between NewGRFClass spec indices and user interface indices.

This commit is contained in:
frosch 2012-04-22 16:28:32 +00:00
parent ed1fb53859
commit 14bd0dad91
2 changed files with 50 additions and 0 deletions

View File

@ -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);

View File

@ -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<Tspec, Tid, Tmax> *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);