From 477f22a6fd0fc30786f9fe251b7ce2d8b3adcba2 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Sun, 15 Jun 2008 15:48:33 +0000 Subject: [PATCH] (svn r13521) -Codechange: Use GUIList sorting instead of poll & sort for CompanyLeagueWindow --- src/graph_gui.cpp | 69 ++++++++++++++++++++++++++++++++++++----------- src/players.cpp | 2 ++ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 69264ec9bf..41a71ef5fd 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -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 GetPerformanceTitleFromValue(uint value) 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 players; - return p2->old_economy[1].performance_history - p1->old_economy[1].performance_history; -} + /** + * (Re)Build the company league list + */ + void BuildPlayerList() + { + if (!this->players.NeedRebuild()) return; + + this->players.Clear(); + + const Player *p; + FOR_ALL_PLAYERS(p) { + if (p->is_active) { + *this->players.Append() = p; + } + } + + this->players.Compact(); + this->players.RebuildDone(); + } -struct CompanyLeagueWindow : Window { + /** 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(); + } + } }; diff --git a/src/players.cpp b/src/players.cpp index a024cd47ae..c568b603de 100644 --- a/src/players.cpp +++ b/src/players.cpp @@ -818,6 +818,8 @@ CommandCost CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { if (flags & DC_EXEC) _current_player = OWNER_NONE; + InvalidateWindowData(WC_COMPANY_LEAGUE, 0, 0); + switch (p1) { case 0: { /* Create a new player */ /* Joining Client: