Changeset - r11029:bf0816bd43ea
[Not reviewed]
master
0 1 0
rubidium - 16 years ago 2009-02-06 10:38:57
rubidium@openttd.org
(svn r15369) -Codechange: generalise the GUIList a bit so peter can write filters for cargo type, speed and mass ;)
1 file changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/sortlist_type.h
Show inline comments
 
@@ -11,53 +11,53 @@
 
#include "core/sort_func.hpp"
 
#include "core/smallvec_type.hpp"
 
#include "date_type.h"
 

	
 
enum SortListFlags {
 
	VL_NONE       = 0,      ///< no sort
 
	VL_DESC       = 1 << 0, ///< sort descending or ascending
 
	VL_RESORT     = 1 << 1, ///< instruct the code to resort the list in the next loop
 
	VL_REBUILD    = 1 << 2, ///< rebuild the sort list
 
	VL_FIRST_SORT = 1 << 3, ///< sort with qsort first
 
	VL_FILTER     = 1 << 4, ///< filter disabled/enabled
 
	VL_END        = 1 << 5,
 
};
 
DECLARE_ENUM_AS_BIT_SET(SortListFlags);
 

	
 
struct Listing {
 
	bool order;    ///< Ascending/descending
 
	byte criteria; ///< Sorting criteria
 
};
 
struct Filtering {
 
	bool state;    ///< Filter on/off
 
	byte criteria; ///< Filtering criteria
 
};
 

	
 
template <typename T, typename F = char>
 
template <typename T, typename F = const char*>
 
class GUIList : public SmallVector<T, 32> {
 
public:
 
	typedef int CDECL SortFunction(const T*, const T*);
 
	typedef bool CDECL FilterFunction(const T*, const F*);
 
	typedef bool CDECL FilterFunction(const T*, F);
 

	
 
protected:
 
	SortFunction * const *sort_func_list;     ///< the sort criteria functions
 
	FilterFunction * const *filter_func_list; ///< the filter criteria functions
 
	SortListFlags flags;                      ///< used to control sorting/resorting/etc.
 
	uint8 sort_type;                          ///< what criteria to sort on
 
	uint8 filter_type;                        ///< what criteria to filter on
 
	uint16 resort_timer;                      ///< resort list after a given amount of ticks if set
 

	
 
	/**
 
	 * Check if the list is sortable
 
	 *
 
	 * @return true if we can sort the list
 
	 */
 
	bool IsSortable() const
 
	{
 
		return (this->data != NULL && this->items >= 2);
 
	}
 

	
 
	/**
 
	 * Reset the resort timer
 
	 */
 
	void ResetResortTimer()
 
	{
 
@@ -295,82 +295,82 @@ public:
 
		return HASBITS(this->flags, VL_FILTER);
 
	}
 

	
 
	/**
 
	 * Enable or disable the filter
 
	 *
 
	 * @param state If filtering should be enabled or disabled
 
	 */
 
	void SetFilterState(bool state)
 
	{
 
		if (state) {
 
			SETBITS(this->flags, VL_FILTER);
 
		} else {
 
			CLRBITS(this->flags, VL_FILTER);
 
		}
 
	}
 

	
 
	/**
 
	 * Filter the list.
 
	 *
 
	 * @param decide The function to decide about an item
 
	 * @param filter_data Additional data passed to the filter function
 
	 * @return true if the list has been altered by filtering
 
	 */
 
	bool Filter(FilterFunction *decide, const F *filter_data)
 
	bool Filter(FilterFunction *decide, F filter_data)
 
	{
 
		/* Do not filter if the filter bit is not set */
 
		if (!HASBITS(this->flags, VL_FILTER)) return false;
 

	
 
		for (uint iter = 0; iter < this->items;) {
 
			T *item = &this->data[iter];
 
			if (!decide(item, filter_data)) {
 
				this->Erase(item);
 
			} else {
 
				iter++;
 
			}
 
		}
 

	
 
		return true;
 
	}
 

	
 
	/**
 
	 * Hand the array of filter function pointers to the sort list
 
	 *
 
	 * @param n_funcs The pointer to the first filter func
 
	 */
 
	void SetFilterFuncs(FilterFunction * const *n_funcs)
 
	{
 
		this->filter_func_list = n_funcs;
 
	}
 

	
 
	/**
 
	 * Filter the data with the currently selected filter.
 
	 *
 
	 * @param filter_data Additional data passed to the filter function.
 
	 * @return true if the list has been altered by filtering
 
	 */
 
	bool Filter(const F *filter_data)
 
	bool Filter(F filter_data)
 
	{
 
		if (this->filter_func_list == NULL) return false;
 
		return this->Filter(this->filter_func_list[this->filter_type], filter_data);
 
	}
 

	
 
	/**
 
	 * Check if a rebuild is needed
 
	 * @return true if a rebuild is needed
 
	 */
 
	bool NeedRebuild() const
 
	{
 
		return HASBITS(this->flags, VL_REBUILD);
 
	}
 

	
 
	/**
 
	 * Force that a rebuild is needed
 
	 */
 
	void ForceRebuild()
 
	{
 
		SETBITS(this->flags, VL_REBUILD);
 
	}
 

	
 
	/**
 
	 * Notify the sortlist that the rebuild is done
0 comments (0 inline, 0 general)