Changeset - r19267:1d5d8ee85137
[Not reviewed]
master
0 2 0
frosch - 12 years ago 2012-04-22 16:28:32
frosch@openttd.org
(svn r24170) -Add: Methods for translating between NewGRFClass spec indices and user interface indices.
2 files changed with 50 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/newgrf_class.h
Show inline comments
 
@@ -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);
src/newgrf_class_func.h
Show inline comments
 
@@ -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<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);
0 comments (0 inline, 0 general)