14 #include "strings_func.h" |
14 #include "strings_func.h" |
15 #include "core/alloc_func.hpp" |
15 #include "core/alloc_func.hpp" |
16 #include "window_func.h" |
16 #include "window_func.h" |
17 #include "date_func.h" |
17 #include "date_func.h" |
18 #include "gfx_func.h" |
18 #include "gfx_func.h" |
|
19 #include "sortlist_type.h" |
19 |
20 |
20 #include "table/strings.h" |
21 #include "table/strings.h" |
21 #include "table/sprites.h" |
22 #include "table/sprites.h" |
22 |
23 |
23 /* Bitmasks of player and cargo indices that shouldn't be drawn. */ |
24 /* Bitmasks of player and cargo indices that shouldn't be drawn. */ |
756 static inline StringID GetPerformanceTitleFromValue(uint value) |
757 static inline StringID GetPerformanceTitleFromValue(uint value) |
757 { |
758 { |
758 return _performance_titles[minu(value, 1000) >> 6]; |
759 return _performance_titles[minu(value, 1000) >> 6]; |
759 } |
760 } |
760 |
761 |
761 static int CDECL PerfHistComp(const void* elem1, const void* elem2) |
762 class CompanyLeagueWindow : public Window { |
762 { |
763 private: |
763 const Player* p1 = *(const Player* const*)elem1; |
764 GUIList<const Player*> players; |
764 const Player* p2 = *(const Player* const*)elem2; |
765 |
765 |
766 /** |
766 return p2->old_economy[1].performance_history - p1->old_economy[1].performance_history; |
767 * (Re)Build the company league list |
767 } |
768 */ |
768 |
769 void BuildPlayerList() |
769 struct CompanyLeagueWindow : Window { |
770 { |
|
771 if (!this->players.NeedRebuild()) return; |
|
772 |
|
773 this->players.Clear(); |
|
774 |
|
775 const Player *p; |
|
776 FOR_ALL_PLAYERS(p) { |
|
777 if (p->is_active) { |
|
778 *this->players.Append() = p; |
|
779 } |
|
780 } |
|
781 |
|
782 this->players.Compact(); |
|
783 this->players.RebuildDone(); |
|
784 } |
|
785 |
|
786 /** Sort the company league by performance history */ |
|
787 static int CDECL PerformanceSorter(const Player* const *p1, const Player* const *p2) |
|
788 { |
|
789 return (*p2)->old_economy[1].performance_history - (*p1)->old_economy[1].performance_history; |
|
790 } |
|
791 |
|
792 public: |
770 CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) |
793 CompanyLeagueWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) |
771 { |
794 { |
|
795 this->players.ForceRebuild(); |
|
796 this->players.NeedResort(); |
|
797 |
772 this->FindWindowPlacementAndResize(desc); |
798 this->FindWindowPlacementAndResize(desc); |
773 } |
799 } |
774 |
800 |
775 virtual void OnPaint() |
801 virtual void OnPaint() |
776 { |
802 { |
777 const Player *plist[MAX_PLAYERS]; |
803 this->BuildPlayerList(); |
778 const Player *p; |
804 this->players.Sort(&PerformanceSorter); |
779 |
805 |
780 this->DrawWidgets(); |
806 this->DrawWidgets(); |
781 |
807 |
782 uint pl_num = 0; |
808 for (uint i = 0; i != this->players.Length(); i++) { |
783 FOR_ALL_PLAYERS(p) if (p->is_active) plist[pl_num++] = p; |
809 const Player *p = this->players[i]; |
784 |
|
785 qsort((void*)plist, pl_num, sizeof(*plist), PerfHistComp); |
|
786 |
|
787 for (uint i = 0; i != pl_num; i++) { |
|
788 p = plist[i]; |
|
789 SetDParam(0, i + STR_01AC_1ST); |
810 SetDParam(0, i + STR_01AC_1ST); |
790 SetDParam(1, p->index); |
811 SetDParam(1, p->index); |
791 SetDParam(2, p->index); |
812 SetDParam(2, p->index); |
792 SetDParam(3, GetPerformanceTitleFromValue(p->old_economy[1].performance_history)); |
813 SetDParam(3, GetPerformanceTitleFromValue(p->old_economy[1].performance_history)); |
793 |
814 |
794 DrawString(2, 15 + i * 10, i == 0 ? STR_7054 : STR_7055, TC_FROMSTRING); |
815 DrawString(2, 15 + i * 10, i == 0 ? STR_7054 : STR_7055, TC_FROMSTRING); |
795 DrawPlayerIcon(p->index, 27, 16 + i * 10); |
816 DrawPlayerIcon(p->index, 27, 16 + i * 10); |
|
817 } |
|
818 } |
|
819 |
|
820 virtual void OnTick() |
|
821 { |
|
822 if (this->players.NeedResort()) { |
|
823 this->SetDirty(); |
|
824 } |
|
825 } |
|
826 |
|
827 virtual void OnInvalidateData(int data) |
|
828 { |
|
829 if (data == 0) { |
|
830 this->players.ForceRebuild(); |
|
831 } else { |
|
832 this->players.ForceResort(); |
796 } |
833 } |
797 } |
834 } |
798 }; |
835 }; |
799 |
836 |
800 |
837 |