Changeset - r9525:cf291d8eec20
[Not reviewed]
master
0 2 0
skidd13 - 16 years ago 2008-06-15 15:48:33
skidd13@openttd.org
(svn r13521) -Codechange: Use GUIList sorting instead of poll & sort for CompanyLeagueWindow
2 files changed with 55 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/graph_gui.cpp
Show inline comments
 
@@ -16,6 +16,7 @@
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "gfx_func.h"
 
#include "sortlist_type.h"
 

	
 
#include "table/strings.h"
 
#include "table/sprites.h"
 
@@ -758,34 +759,54 @@ static inline StringID GetPerformanceTit
 
	return _performance_titles[minu(value, 1000) >> 6];
 
}
 

	
 
static int CDECL PerfHistComp(const void* elem1, const void* elem2)
 
{
 
	const Player* p1 = *(const Player* const*)elem1;
 
	const Player* p2 = *(const Player* const*)elem2;
 
class CompanyLeagueWindow : public Window {
 
private:
 
	GUIList<const Player*> players;
 

	
 
	/**
 
	 * (Re)Build the company league list
 
	 */
 
	void BuildPlayerList()
 
	{
 
		if (!this->players.NeedRebuild()) return;
 

	
 
		this->players.Clear();
 

	
 
	return p2->old_economy[1].performance_history - p1->old_economy[1].performance_history;
 
}
 
		const Player *p;
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active) {
 
				*this->players.Append() = p;
 
			}
 
		}
 

	
 
struct CompanyLeagueWindow : Window {
 
		this->players.Compact();
 
		this->players.RebuildDone();
 
	}
 

	
 
	/** Sort the company league by performance history */
 
	static int CDECL PerformanceSorter(const Player* const *p1, const Player* const *p2)
 
	{
 
		return (*p2)->old_economy[1].performance_history - (*p1)->old_economy[1].performance_history;
 
	}
 

	
 
public:
 
	CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
 
	{
 
		this->players.ForceRebuild();
 
		this->players.NeedResort();
 

	
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		const Player *plist[MAX_PLAYERS];
 
		const Player *p;
 
		this->BuildPlayerList();
 
		this->players.Sort(&PerformanceSorter);
 

	
 
		this->DrawWidgets();
 

	
 
		uint pl_num = 0;
 
		FOR_ALL_PLAYERS(p) if (p->is_active) plist[pl_num++] = p;
 

	
 
		qsort((void*)plist, pl_num, sizeof(*plist), PerfHistComp);
 

	
 
		for (uint i = 0; i != pl_num; i++) {
 
			p = plist[i];
 
		for (uint i = 0; i != this->players.Length(); i++) {
 
			const Player *p = this->players[i];
 
			SetDParam(0, i + STR_01AC_1ST);
 
			SetDParam(1, p->index);
 
			SetDParam(2, p->index);
 
@@ -795,6 +816,22 @@ struct CompanyLeagueWindow : Window {
 
			DrawPlayerIcon(p->index, 27, 16 + i * 10);
 
		}
 
	}
 

	
 
	virtual void OnTick()
 
	{
 
		if (this->players.NeedResort()) {
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	virtual void OnInvalidateData(int data)
 
	{
 
		if (data == 0) {
 
			this->players.ForceRebuild();
 
		} else {
 
			this->players.ForceResort();
 
		}
 
	}
 
};
 

	
 

	
src/players.cpp
Show inline comments
 
@@ -818,6 +818,8 @@ CommandCost CmdPlayerCtrl(TileIndex tile
 
{
 
	if (flags & DC_EXEC) _current_player = OWNER_NONE;
 

	
 
	InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0);
 

	
 
	switch (p1) {
 
	case 0: { /* Create a new player */
 
		/* Joining Client:
0 comments (0 inline, 0 general)