diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -18,7 +18,6 @@ #include "window_func.h" #include "date_func.h" #include "gfx_func.h" -#include "sortlist_type.h" #include "core/geometry_func.hpp" #include "currency.h" #include "zoom_func.h" @@ -1100,190 +1099,6 @@ void ShowCargoPaymentRates() AllocateWindowDescFront(&_cargo_payment_rates_desc, 0); } -/************************/ -/* COMPANY LEAGUE TABLE */ -/************************/ - -static const StringID _performance_titles[] = { - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT, - STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON, -}; - -static inline StringID GetPerformanceTitleFromValue(uint value) -{ - return _performance_titles[std::min(value, 1000u) >> 6]; -} - -class CompanyLeagueWindow : public Window { -private: - GUIList companies; - uint ordinal_width; ///< The width of the ordinal number - uint text_width; ///< The width of the actual text - int line_height; ///< Height of the text lines - Dimension icon; ///< Dimenion of the company icon. - - /** - * (Re)Build the company league list - */ - void BuildCompanyList() - { - if (!this->companies.NeedRebuild()) return; - - this->companies.clear(); - - for (const Company *c : Company::Iterate()) { - this->companies.push_back(c); - } - - this->companies.shrink_to_fit(); - this->companies.RebuildDone(); - } - - /** Sort the company league by performance history */ - static bool PerformanceSorter(const Company * const &c1, const Company * const &c2) - { - return c2->old_economy[0].performance_history < c1->old_economy[0].performance_history; - } - -public: - CompanyLeagueWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc) - { - this->InitNested(window_number); - this->companies.ForceRebuild(); - this->companies.NeedResort(); - } - - void OnPaint() override - { - this->BuildCompanyList(); - this->companies.Sort(&PerformanceSorter); - - this->DrawWidgets(); - } - - void DrawWidget(const Rect &r, int widget) const override - { - if (widget != WID_CL_BACKGROUND) return; - - Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); - int icon_y_offset = (this->line_height - this->icon.height) / 2; - int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2; - - bool rtl = _current_text_dir == TD_RTL; - Rect ordinal = ir.WithWidth(this->ordinal_width, rtl); - uint icon_left = ir.Indent(rtl ? this->text_width : this->ordinal_width, rtl).left; - Rect text = ir.WithWidth(this->text_width, !rtl); - - for (uint i = 0; i != this->companies.size(); i++) { - const Company *c = this->companies[i]; - DrawString(ordinal.left, ordinal.right, ir.top + text_y_offset, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW); - - DrawCompanyIcon(c->index, icon_left, ir.top + icon_y_offset); - - SetDParam(0, c->index); - SetDParam(1, c->index); - SetDParam(2, GetPerformanceTitleFromValue(c->old_economy[0].performance_history)); - DrawString(text.left, text.right, ir.top + text_y_offset, STR_COMPANY_LEAGUE_COMPANY_NAME); - ir.top += this->line_height; - } - } - - void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override - { - if (widget != WID_CL_BACKGROUND) return; - - this->ordinal_width = 0; - for (uint i = 0; i < MAX_COMPANIES; i++) { - this->ordinal_width = std::max(this->ordinal_width, GetStringBoundingBox(STR_ORDINAL_NUMBER_1ST + i).width); - } - this->ordinal_width += WidgetDimensions::scaled.hsep_wide; // Keep some extra spacing - - uint widest_width = 0; - uint widest_title = 0; - for (uint i = 0; i < lengthof(_performance_titles); i++) { - uint width = GetStringBoundingBox(_performance_titles[i]).width; - if (width > widest_width) { - widest_title = i; - widest_width = width; - } - } - - this->icon = GetSpriteSize(SPR_COMPANY_ICON); - this->line_height = std::max(this->icon.height + WidgetDimensions::scaled.vsep_normal, FONT_HEIGHT_NORMAL); - - for (const Company *c : Company::Iterate()) { - SetDParam(0, c->index); - SetDParam(1, c->index); - SetDParam(2, _performance_titles[widest_title]); - widest_width = std::max(widest_width, GetStringBoundingBox(STR_COMPANY_LEAGUE_COMPANY_NAME).width); - } - - this->text_width = widest_width + WidgetDimensions::scaled.hsep_indent * 3; // Keep some extra spacing - - size->width = WidgetDimensions::scaled.framerect.Horizontal() + this->ordinal_width + this->icon.width + this->text_width + WidgetDimensions::scaled.framerect.Horizontal(); - size->height = this->line_height * MAX_COMPANIES + WidgetDimensions::scaled.framerect.Vertical(); - } - - - void OnGameTick() override - { - if (this->companies.NeedResort()) { - this->SetDirty(); - } - } - - /** - * Some data on this window has become invalid. - * @param data Information about the changed data. - * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details. - */ - void OnInvalidateData(int data = 0, bool gui_scope = true) override - { - if (data == 0) { - /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */ - this->companies.ForceRebuild(); - } else { - this->companies.ForceResort(); - } - } -}; - -static const NWidgetPart _nested_company_league_widgets[] = { - NWidget(NWID_HORIZONTAL), - NWidget(WWT_CLOSEBOX, COLOUR_BROWN), - NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_COMPANY_LEAGUE_TABLE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS), - NWidget(WWT_SHADEBOX, COLOUR_BROWN), - NWidget(WWT_STICKYBOX, COLOUR_BROWN), - EndContainer(), - NWidget(WWT_PANEL, COLOUR_BROWN, WID_CL_BACKGROUND), SetMinimalSize(400, 0), SetMinimalTextLines(15, WidgetDimensions::unscaled.framerect.Vertical()), -}; - -static WindowDesc _company_league_desc( - WDP_AUTO, "league", 0, 0, - WC_COMPANY_LEAGUE, WC_NONE, - 0, - _nested_company_league_widgets, lengthof(_nested_company_league_widgets) -); - -void ShowCompanyLeagueTable() -{ - AllocateWindowDescFront(&_company_league_desc, 0); -} - /*****************************/ /* PERFORMANCE RATING DETAIL */ /*****************************/