summaryrefslogtreecommitdiff
path: root/src/graph_gui.cpp
diff options
context:
space:
mode:
authorskidd13 <skidd13@openttd.org>2008-06-15 15:48:33 +0000
committerskidd13 <skidd13@openttd.org>2008-06-15 15:48:33 +0000
commit1ddafcadd23e2521373f6eb875445d6371dc9281 (patch)
treeed69f633720f81a2b3acc93c7739f93ba48af3de /src/graph_gui.cpp
parent704c9be0b8eb7e62d6f52cf772a14c5e17540aae (diff)
downloadopenttd-1ddafcadd23e2521373f6eb875445d6371dc9281.tar.xz
(svn r13521) -Codechange: Use GUIList sorting instead of poll & sort for CompanyLeagueWindow
Diffstat (limited to 'src/graph_gui.cpp')
-rw-r--r--src/graph_gui.cpp69
1 files changed, 53 insertions, 16 deletions
diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp
index 69264ec9b..41a71ef5f 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<const Player*> 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();
+ }
+ }
};