2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file industry_gui.cpp GUIs related to industries. */
13 #include "settings_gui.h"
14 #include "sound_func.h"
15 #include "window_func.h"
16 #include "textbuf_gui.h"
17 #include "command_func.h"
18 #include "viewport_func.h"
21 #include "cheat_type.h"
22 #include "newgrf_industries.h"
23 #include "newgrf_text.h"
24 #include "newgrf_debug.h"
25 #include "network/network.h"
26 #include "strings_func.h"
27 #include "company_func.h"
28 #include "tilehighlight_func.h"
29 #include "string_func.h"
30 #include "sortlist_type.h"
31 #include "widgets/dropdown_func.h"
32 #include "company_base.h"
33 #include "core/geometry_func.hpp"
34 #include "core/random_func.hpp"
35 #include "core/backup_type.hpp"
37 #include "smallmap_gui.h"
38 #include "widgets/dropdown_type.h"
39 #include "widgets/industry_widget.h"
40 #include "clear_map.h"
41 #include "zoom_func.h"
42 #include "industry_cmd.h"
43 #include "querystring_gui.h"
44 #include "stringfilter_type.h"
45 #include "timer/timer.h"
46 #include "timer/timer_window.h"
49 #include "table/strings.h"
53 #include "safeguards.h"
55 bool _ignore_restrictions
;
56 std::bitset
<NUM_INDUSTRYTYPES
> _displayed_industries
; ///< Communication from the industry chain window to the smallmap window about what industries to display.
58 /** Cargo suffix type (for which window is it requested) */
59 enum CargoSuffixType
{
60 CST_FUND
, ///< Fund-industry window
61 CST_VIEW
, ///< View-industry window
62 CST_DIR
, ///< Industry-directory window
65 /** Ways of displaying the cargo. */
66 enum CargoSuffixDisplay
{
67 CSD_CARGO
, ///< Display the cargo without sub-type (cb37 result 401).
68 CSD_CARGO_AMOUNT
, ///< Display the cargo and amount (if useful), but no sub-type (cb37 result 400 or fail).
69 CSD_CARGO_TEXT
, ///< Display then cargo and supplied string (cb37 result 800-BFF).
70 CSD_CARGO_AMOUNT_TEXT
, ///< Display then cargo, amount, and string (cb37 result 000-3FF).
73 /** Transfer storage of cargo suffix information. */
75 CargoSuffixDisplay display
; ///< How to display the cargo and text.
76 std::string text
; ///< Cargo suffix text.
79 extern void GenerateIndustries();
80 static void ShowIndustryCargoesWindow(IndustryType id
);
83 * Gets the string to display after the cargo name (using callback 37)
84 * @param cargo the cargo for which the suffix is requested, meaning depends on presence of flag 18 in prop 1A
85 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
86 * @param ind the industry (nullptr if in fund window)
87 * @param ind_type the industry type
88 * @param indspec the industry spec
89 * @param suffix is filled with the string to display
91 static void GetCargoSuffix(uint cargo
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, CargoSuffix
&suffix
)
94 suffix
.display
= CSD_CARGO_AMOUNT
;
96 if (HasBit(indspec
->callback_mask
, CBM_IND_CARGO_SUFFIX
)) {
97 TileIndex t
= (cst
!= CST_FUND
) ? ind
->location
.tile
: INVALID_TILE
;
98 uint16_t callback
= GetIndustryCallback(CBID_INDUSTRY_CARGO_SUFFIX
, 0, (cst
<< 8) | cargo
, const_cast<Industry
*>(ind
), ind_type
, t
);
99 if (callback
== CALLBACK_FAILED
) return;
101 if (indspec
->grf_prop
.grffile
->grf_version
< 8) {
102 if (GB(callback
, 0, 8) == 0xFF) return;
103 if (callback
< 0x400) {
104 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
105 suffix
.text
= GetString(GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 + callback
));
106 StopTextRefStackUsage();
107 suffix
.display
= CSD_CARGO_AMOUNT_TEXT
;
110 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_CARGO_SUFFIX
, callback
);
113 } else { // GRF version 8 or higher.
114 if (callback
== 0x400) return;
115 if (callback
== 0x401) {
116 suffix
.display
= CSD_CARGO
;
119 if (callback
< 0x400) {
120 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
121 suffix
.text
= GetString(GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 + callback
));
122 StopTextRefStackUsage();
123 suffix
.display
= CSD_CARGO_AMOUNT_TEXT
;
126 if (callback
>= 0x800 && callback
< 0xC00) {
127 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
128 suffix
.text
= GetString(GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 - 0x800 + callback
));
129 StopTextRefStackUsage();
130 suffix
.display
= CSD_CARGO_TEXT
;
133 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_CARGO_SUFFIX
, callback
);
139 enum CargoSuffixInOut
{
145 * Gets all strings to display after the cargoes of industries (using callback 37)
146 * @param use_input get suffixes for output cargoes or input cargoes?
147 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
148 * @param ind the industry (nullptr if in fund window)
149 * @param ind_type the industry type
150 * @param indspec the industry spec
151 * @param cargoes array with cargotypes. for INVALID_CARGO no suffix will be determined
152 * @param suffixes is filled with the suffixes
154 template <typename TC
, typename TS
>
155 static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, const TC
&cargoes
, TS
&suffixes
)
157 static_assert(lengthof(cargoes
) <= lengthof(suffixes
));
159 if (indspec
->behaviour
& INDUSTRYBEH_CARGOTYPES_UNLIMITED
) {
160 /* Reworked behaviour with new many-in-many-out scheme */
161 for (uint j
= 0; j
< lengthof(suffixes
); j
++) {
162 if (IsValidCargoID(cargoes
[j
])) {
163 byte local_id
= indspec
->grf_prop
.grffile
->cargo_map
[cargoes
[j
]]; // should we check the value for valid?
164 uint cargotype
= local_id
<< 16 | use_input
;
165 GetCargoSuffix(cargotype
, cst
, ind
, ind_type
, indspec
, suffixes
[j
]);
167 suffixes
[j
].text
[0] = '\0';
168 suffixes
[j
].display
= CSD_CARGO
;
172 /* Compatible behaviour with old 3-in-2-out scheme */
173 for (uint j
= 0; j
< lengthof(suffixes
); j
++) {
174 suffixes
[j
].text
[0] = '\0';
175 suffixes
[j
].display
= CSD_CARGO
;
178 case CARGOSUFFIX_OUT
:
179 if (IsValidCargoID(cargoes
[0])) GetCargoSuffix(3, cst
, ind
, ind_type
, indspec
, suffixes
[0]);
180 if (IsValidCargoID(cargoes
[1])) GetCargoSuffix(4, cst
, ind
, ind_type
, indspec
, suffixes
[1]);
183 if (IsValidCargoID(cargoes
[0])) GetCargoSuffix(0, cst
, ind
, ind_type
, indspec
, suffixes
[0]);
184 if (IsValidCargoID(cargoes
[1])) GetCargoSuffix(1, cst
, ind
, ind_type
, indspec
, suffixes
[1]);
185 if (IsValidCargoID(cargoes
[2])) GetCargoSuffix(2, cst
, ind
, ind_type
, indspec
, suffixes
[2]);
194 * Gets the strings to display after the cargo of industries (using callback 37)
195 * @param use_input get suffixes for output cargo or input cargo?
196 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
197 * @param ind the industry (nullptr if in fund window)
198 * @param ind_type the industry type
199 * @param indspec the industry spec
200 * @param cargo cargotype. for INVALID_CARGO no suffix will be determined
201 * @param slot accepts/produced slot number, used for old-style 3-in/2-out industries.
202 * @param suffix is filled with the suffix
204 void GetCargoSuffix(CargoSuffixInOut use_input
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, CargoID cargo
, uint8_t slot
, CargoSuffix
&suffix
)
206 suffix
.text
[0] = '\0';
207 suffix
.display
= CSD_CARGO
;
208 if (!IsValidCargoID(cargo
)) return;
209 if (indspec
->behaviour
& INDUSTRYBEH_CARGOTYPES_UNLIMITED
) {
210 byte local_id
= indspec
->grf_prop
.grffile
->cargo_map
[cargo
]; // should we check the value for valid?
211 uint cargotype
= local_id
<< 16 | use_input
;
212 GetCargoSuffix(cargotype
, cst
, ind
, ind_type
, indspec
, suffix
);
213 } else if (use_input
== CARGOSUFFIX_IN
) {
214 if (slot
< 3) GetCargoSuffix(slot
, cst
, ind
, ind_type
, indspec
, suffix
);
215 } else if (use_input
== CARGOSUFFIX_OUT
) {
216 if (slot
< 2) GetCargoSuffix(slot
+ 3, cst
, ind
, ind_type
, indspec
, suffix
);
220 std::array
<IndustryType
, NUM_INDUSTRYTYPES
> _sorted_industry_types
; ///< Industry types sorted by name.
222 /** Sort industry types by their name. */
223 static bool IndustryTypeNameSorter(const IndustryType
&a
, const IndustryType
&b
)
225 int r
= StrNaturalCompare(GetString(GetIndustrySpec(a
)->name
), GetString(GetIndustrySpec(b
)->name
)); // Sort by name (natural sorting).
227 /* If the names are equal, sort by industry type. */
228 return (r
!= 0) ? r
< 0 : (a
< b
);
232 * Initialize the list of sorted industry types.
234 void SortIndustryTypes()
236 /* Add each industry type to the list. */
237 for (IndustryType i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
238 _sorted_industry_types
[i
] = i
;
241 /* Sort industry types by name. */
242 std::sort(_sorted_industry_types
.begin(), _sorted_industry_types
.end(), IndustryTypeNameSorter
);
246 * Command callback. In case of failure to build an industry, show an error message.
247 * @param result Result of the command.
248 * @param tile Tile where the industry is placed.
249 * @param indtype Industry type.
251 void CcBuildIndustry(Commands
, const CommandCost
&result
, TileIndex tile
, IndustryType indtype
, uint32_t, bool, uint32_t)
253 if (result
.Succeeded()) return;
255 if (indtype
< NUM_INDUSTRYTYPES
) {
256 const IndustrySpec
*indsp
= GetIndustrySpec(indtype
);
257 if (indsp
->enabled
) {
258 SetDParam(0, indsp
->name
);
259 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, result
.GetErrorMessage(), WL_INFO
, TileX(tile
) * TILE_SIZE
, TileY(tile
) * TILE_SIZE
);
264 static constexpr NWidgetPart _nested_build_industry_widgets
[] = {
265 NWidget(NWID_HORIZONTAL
),
266 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
267 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FUND_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
268 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
269 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
270 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
272 NWidget(NWID_SELECTION
, COLOUR_DARK_GREEN
, WID_DPI_SCENARIO_EDITOR_PANE
),
273 NWidget(NWID_VERTICAL
),
274 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET
), SetMinimalSize(0, 12), SetFill(1, 0), SetResize(1, 0),
275 SetDataTip(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP
),
276 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET
), SetMinimalSize(0, 12), SetFill(1, 0), SetResize(1, 0),
277 SetDataTip(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES
, STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_TOOLTIP
),
280 NWidget(NWID_HORIZONTAL
),
281 NWidget(WWT_MATRIX
, COLOUR_DARK_GREEN
, WID_DPI_MATRIX_WIDGET
), SetMatrixDataTip(1, 0, STR_FUND_INDUSTRY_SELECTION_TOOLTIP
), SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_DPI_SCROLLBAR
),
282 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_DPI_SCROLLBAR
),
284 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_DPI_INFOPANEL
), SetResize(1, 0),
286 NWidget(NWID_HORIZONTAL
),
287 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_DISPLAY_WIDGET
), SetFill(1, 0), SetResize(1, 0),
288 SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
289 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_FUND_WIDGET
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING
, STR_NULL
),
290 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
294 /** Window definition of the dynamic place industries gui */
295 static WindowDesc
_build_industry_desc(__FILE__
, __LINE__
,
296 WDP_AUTO
, "build_industry", 170, 212,
297 WC_BUILD_INDUSTRY
, WC_NONE
,
299 std::begin(_nested_build_industry_widgets
), std::end(_nested_build_industry_widgets
)
302 /** Build (fund or prospect) a new industry, */
303 class BuildIndustryWindow
: public Window
{
304 IndustryType selected_type
; ///< industry corresponding to the above index
305 std::vector
<IndustryType
> list
; ///< List of industries.
306 bool enabled
; ///< Availability state of the selected industry.
308 Dimension legend
; ///< Dimension of the legend 'blob'.
310 /** The largest allowed minimum-width of the window, given in line heights */
311 static const int MAX_MINWIDTH_LINEHEIGHTS
= 20;
313 void UpdateAvailability()
315 this->enabled
= this->selected_type
!= INVALID_INDUSTRYTYPE
&& (_game_mode
== GM_EDITOR
|| GetIndustryProbabilityCallback(this->selected_type
, IACT_USERCREATION
, 1) > 0);
322 /* Fill the arrays with industries.
323 * The tests performed after the enabled allow to load the industries
324 * In the same way they are inserted by grf (if any)
326 for (IndustryType ind
: _sorted_industry_types
) {
327 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
328 if (indsp
->enabled
) {
329 /* Rule is that editor mode loads all industries.
330 * In game mode, all non raw industries are loaded too
331 * and raw ones are loaded only when setting allows it */
332 if (_game_mode
!= GM_EDITOR
&& indsp
->IsRawIndustry() && _settings_game
.construction
.raw_industry_construction
== 0) {
333 /* Unselect if the industry is no longer in the list */
334 if (this->selected_type
== ind
) this->selected_type
= INVALID_INDUSTRYTYPE
;
338 this->list
.push_back(ind
);
342 /* First industry type is selected if the current selection is invalid. */
343 if (this->selected_type
== INVALID_INDUSTRYTYPE
&& !this->list
.empty()) this->selected_type
= this->list
[0];
345 this->UpdateAvailability();
347 this->vscroll
->SetCount(this->list
.size());
350 /** Update status of the fund and display-chain widgets. */
353 this->SetWidgetDisabledState(WID_DPI_FUND_WIDGET
, this->selected_type
!= INVALID_INDUSTRYTYPE
&& !this->enabled
);
354 this->SetWidgetDisabledState(WID_DPI_DISPLAY_WIDGET
, this->selected_type
== INVALID_INDUSTRYTYPE
&& this->enabled
);
358 * Build a string of cargo names with suffixes attached.
359 * This is distinct from the CARGO_LIST string formatting code in two ways:
360 * - This cargo list uses the order defined by the industry, rather than alphabetic.
361 * - NewGRF-supplied suffix strings can be attached to each cargo.
363 * @param cargolist Array of CargoID to display
364 * @param cargo_suffix Array of suffixes to attach to each cargo
365 * @param cargolistlen Length of arrays
366 * @param prefixstr String to use for the first item
367 * @return A formatted raw string
369 std::string
MakeCargoListString(const CargoID
*cargolist
, const CargoSuffix
*cargo_suffix
, int cargolistlen
, StringID prefixstr
) const
371 std::string cargostring
;
375 for (int j
= 0; j
< cargolistlen
; j
++) {
376 if (!IsValidCargoID(cargolist
[j
])) continue;
378 if (firstcargo
< 0) {
382 SetDParam(0, CargoSpec::Get(cargolist
[j
])->name
);
383 SetDParamStr(1, cargo_suffix
[j
].text
);
384 cargostring
+= GetString(STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION
);
388 SetDParam(0, CargoSpec::Get(cargolist
[firstcargo
])->name
);
389 SetDParamStr(1, cargo_suffix
[firstcargo
].text
);
390 cargostring
= GetString(prefixstr
) + cargostring
;
392 SetDParam(0, STR_JUST_NOTHING
);
394 cargostring
= GetString(prefixstr
);
401 BuildIndustryWindow() : Window(&_build_industry_desc
)
403 this->selected_type
= INVALID_INDUSTRYTYPE
;
405 this->CreateNestedTree();
406 this->vscroll
= this->GetScrollbar(WID_DPI_SCROLLBAR
);
407 /* Show scenario editor tools in editor. */
408 if (_game_mode
!= GM_EDITOR
) {
409 this->GetWidget
<NWidgetStacked
>(WID_DPI_SCENARIO_EDITOR_PANE
)->SetDisplayedPlane(SZSP_HORIZONTAL
);
411 this->FinishInitNested(0);
416 void OnInit() override
418 /* Width of the legend blob -- slightly larger than the smallmap legend blob. */
419 this->legend
.height
= GetCharacterHeight(FS_SMALL
);
420 this->legend
.width
= this->legend
.height
* 9 / 6;
425 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
428 case WID_DPI_MATRIX_WIDGET
: {
429 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
);
430 for (const auto &indtype
: this->list
) {
431 d
= maxdim(d
, GetStringBoundingBox(GetIndustrySpec(indtype
)->name
));
433 resize
->height
= std::max
<uint
>(this->legend
.height
, GetCharacterHeight(FS_NORMAL
)) + padding
.height
;
434 d
.width
+= this->legend
.width
+ WidgetDimensions::scaled
.hsep_wide
+ padding
.width
;
435 d
.height
= 5 * resize
->height
;
436 *size
= maxdim(*size
, d
);
440 case WID_DPI_INFOPANEL
: {
441 /* Extra line for cost outside of editor. */
442 int height
= 2 + (_game_mode
== GM_EDITOR
? 0 : 1);
443 uint extra_lines_req
= 0;
444 uint extra_lines_prd
= 0;
445 uint extra_lines_newgrf
= 0;
446 uint max_minwidth
= GetCharacterHeight(FS_NORMAL
) * MAX_MINWIDTH_LINEHEIGHTS
;
447 Dimension d
= {0, 0};
448 for (const auto &indtype
: this->list
) {
449 const IndustrySpec
*indsp
= GetIndustrySpec(indtype
);
450 CargoSuffix cargo_suffix
[lengthof(indsp
->accepts_cargo
)];
452 /* Measure the accepted cargoes, if any. */
453 GetAllCargoSuffixes(CARGOSUFFIX_IN
, CST_FUND
, nullptr, indtype
, indsp
, indsp
->accepts_cargo
, cargo_suffix
);
454 std::string cargostring
= this->MakeCargoListString(indsp
->accepts_cargo
, cargo_suffix
, lengthof(indsp
->accepts_cargo
), STR_INDUSTRY_VIEW_REQUIRES_N_CARGO
);
455 Dimension strdim
= GetStringBoundingBox(cargostring
);
456 if (strdim
.width
> max_minwidth
) {
457 extra_lines_req
= std::max(extra_lines_req
, strdim
.width
/ max_minwidth
+ 1);
458 strdim
.width
= max_minwidth
;
460 d
= maxdim(d
, strdim
);
462 /* Measure the produced cargoes, if any. */
463 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_FUND
, nullptr, indtype
, indsp
, indsp
->produced_cargo
, cargo_suffix
);
464 cargostring
= this->MakeCargoListString(indsp
->produced_cargo
, cargo_suffix
, lengthof(indsp
->produced_cargo
), STR_INDUSTRY_VIEW_PRODUCES_N_CARGO
);
465 strdim
= GetStringBoundingBox(cargostring
);
466 if (strdim
.width
> max_minwidth
) {
467 extra_lines_prd
= std::max(extra_lines_prd
, strdim
.width
/ max_minwidth
+ 1);
468 strdim
.width
= max_minwidth
;
470 d
= maxdim(d
, strdim
);
472 if (indsp
->grf_prop
.grffile
!= nullptr) {
473 /* Reserve a few extra lines for text from an industry NewGRF. */
474 extra_lines_newgrf
= 4;
478 /* Set it to something more sane :) */
479 height
+= extra_lines_prd
+ extra_lines_req
+ extra_lines_newgrf
;
480 size
->height
= height
* GetCharacterHeight(FS_NORMAL
) + padding
.height
;
481 size
->width
= d
.width
+ padding
.width
;
485 case WID_DPI_FUND_WIDGET
: {
486 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
487 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
));
488 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
));
489 d
.width
+= padding
.width
;
490 d
.height
+= padding
.height
;
491 *size
= maxdim(*size
, d
);
497 void SetStringParameters(WidgetID widget
) const override
500 case WID_DPI_FUND_WIDGET
:
501 /* Raw industries might be prospected. Show this fact by changing the string
502 * In Editor, you just build, while ingame, or you fund or you prospect */
503 if (_game_mode
== GM_EDITOR
) {
504 /* We've chosen many random industries but no industries have been specified */
505 SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
507 if (this->selected_type
!= INVALID_INDUSTRYTYPE
) {
508 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
509 SetDParam(0, (_settings_game
.construction
.raw_industry_construction
== 2 && indsp
->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
: STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
);
511 SetDParam(0, STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
);
518 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
521 case WID_DPI_MATRIX_WIDGET
: {
522 bool rtl
= _current_text_dir
== TD_RTL
;
523 Rect text
= r
.WithHeight(this->resize
.step_height
).Shrink(WidgetDimensions::scaled
.matrix
);
524 Rect icon
= text
.WithWidth(this->legend
.width
, rtl
);
525 text
= text
.Indent(this->legend
.width
+ WidgetDimensions::scaled
.hsep_wide
, rtl
);
527 /* Vertical offset for legend icon. */
528 icon
.top
= r
.top
+ (this->resize
.step_height
- this->legend
.height
+ 1) / 2;
529 icon
.bottom
= icon
.top
+ this->legend
.height
- 1;
531 for (uint16_t i
= this->vscroll
->GetPosition(); this->vscroll
->IsVisible(i
) && i
< this->vscroll
->GetCount(); i
++) {
532 IndustryType type
= this->list
[i
];
533 bool selected
= this->selected_type
== type
;
534 const IndustrySpec
*indsp
= GetIndustrySpec(type
);
536 /* Draw the name of the industry in white is selected, otherwise, in orange */
537 DrawString(text
, indsp
->name
, selected
? TC_WHITE
: TC_ORANGE
);
538 GfxFillRect(icon
, selected
? PC_WHITE
: PC_BLACK
);
539 GfxFillRect(icon
.Shrink(WidgetDimensions::scaled
.bevel
), indsp
->map_colour
);
540 SetDParam(0, Industry::GetIndustryTypeCount(type
));
541 DrawString(text
, STR_JUST_COMMA
, TC_BLACK
, SA_RIGHT
, false, FS_SMALL
);
543 text
= text
.Translate(0, this->resize
.step_height
);
544 icon
= icon
.Translate(0, this->resize
.step_height
);
549 case WID_DPI_INFOPANEL
: {
550 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
552 if (this->selected_type
== INVALID_INDUSTRYTYPE
) {
553 DrawStringMultiLine(ir
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP
);
557 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
559 if (_game_mode
!= GM_EDITOR
) {
560 SetDParam(0, indsp
->GetConstructionCost());
561 DrawString(ir
, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST
);
562 ir
.top
+= GetCharacterHeight(FS_NORMAL
);
565 CargoSuffix cargo_suffix
[lengthof(indsp
->accepts_cargo
)];
567 /* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
568 GetAllCargoSuffixes(CARGOSUFFIX_IN
, CST_FUND
, nullptr, this->selected_type
, indsp
, indsp
->accepts_cargo
, cargo_suffix
);
569 std::string cargostring
= this->MakeCargoListString(indsp
->accepts_cargo
, cargo_suffix
, lengthof(indsp
->accepts_cargo
), STR_INDUSTRY_VIEW_REQUIRES_N_CARGO
);
570 ir
.top
= DrawStringMultiLine(ir
, cargostring
);
572 /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
573 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_FUND
, nullptr, this->selected_type
, indsp
, indsp
->produced_cargo
, cargo_suffix
);
574 cargostring
= this->MakeCargoListString(indsp
->produced_cargo
, cargo_suffix
, lengthof(indsp
->produced_cargo
), STR_INDUSTRY_VIEW_PRODUCES_N_CARGO
);
575 ir
.top
= DrawStringMultiLine(ir
, cargostring
);
577 /* Get the additional purchase info text, if it has not already been queried. */
578 if (HasBit(indsp
->callback_mask
, CBM_IND_FUND_MORE_TEXT
)) {
579 uint16_t callback_res
= GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT
, 0, 0, nullptr, this->selected_type
, INVALID_TILE
);
580 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
581 if (callback_res
> 0x400) {
582 ErrorUnknownCallbackResult(indsp
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_FUND_MORE_TEXT
, callback_res
);
584 StringID str
= GetGRFStringID(indsp
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
); // No. here's the new string
585 if (str
!= STR_UNDEFINED
) {
586 StartTextRefStackUsage(indsp
->grf_prop
.grffile
, 6);
587 DrawStringMultiLine(ir
, str
, TC_YELLOW
);
588 StopTextRefStackUsage();
598 static void AskManyRandomIndustriesCallback(Window
*, bool confirmed
)
600 if (!confirmed
) return;
602 if (Town::GetNumItems() == 0) {
603 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
);
605 Backup
<bool> old_generating_world(_generating_world
, true, FILE_LINE
);
606 BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP
);
607 GenerateIndustries();
608 BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP
);
609 old_generating_world
.Restore();
613 static void AskRemoveAllIndustriesCallback(Window
*, bool confirmed
)
615 if (!confirmed
) return;
617 for (Industry
*industry
: Industry::Iterate()) delete industry
;
619 /* Clear farmland. */
620 for (TileIndex tile
= 0; tile
< Map::Size(); tile
++) {
621 if (IsTileType(tile
, MP_CLEAR
) && GetRawClearGround(tile
) == CLEAR_FIELDS
) {
622 MakeClear(tile
, CLEAR_GRASS
, 3);
626 MarkWholeScreenDirty();
629 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
632 case WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET
: {
633 assert(_game_mode
== GM_EDITOR
);
634 this->HandleButtonClick(WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET
);
635 ShowQuery(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_CAPTION
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_QUERY
, nullptr, AskManyRandomIndustriesCallback
);
639 case WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET
: {
640 assert(_game_mode
== GM_EDITOR
);
641 this->HandleButtonClick(WID_DPI_REMOVE_ALL_INDUSTRIES_WIDGET
);
642 ShowQuery(STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_CAPTION
, STR_FUND_INDUSTRY_REMOVE_ALL_INDUSTRIES_QUERY
, nullptr, AskRemoveAllIndustriesCallback
);
646 case WID_DPI_MATRIX_WIDGET
: {
647 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->list
, pt
.y
, this, WID_DPI_MATRIX_WIDGET
);
648 if (it
!= this->list
.end()) { // Is it within the boundaries of available data?
649 this->selected_type
= *it
;
650 this->UpdateAvailability();
652 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
656 if (_thd
.GetCallbackWnd() == this &&
657 ((_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && indsp
!= nullptr && indsp
->IsRawIndustry()) || !this->enabled
)) {
658 /* Reset the button state if going to prospecting or "build many industries" */
659 this->RaiseButtons();
660 ResetObjectToPlace();
664 if (this->enabled
&& click_count
> 1) this->OnClick(pt
, WID_DPI_FUND_WIDGET
, 1);
669 case WID_DPI_DISPLAY_WIDGET
:
670 if (this->selected_type
!= INVALID_INDUSTRYTYPE
) ShowIndustryCargoesWindow(this->selected_type
);
673 case WID_DPI_FUND_WIDGET
: {
674 if (this->selected_type
!= INVALID_INDUSTRYTYPE
) {
675 if (_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && GetIndustrySpec(this->selected_type
)->IsRawIndustry()) {
676 Command
<CMD_BUILD_INDUSTRY
>::Post(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
, 0, this->selected_type
, 0, false, InteractiveRandom());
677 this->HandleButtonClick(WID_DPI_FUND_WIDGET
);
679 HandlePlacePushButton(this, WID_DPI_FUND_WIDGET
, SPR_CURSOR_INDUSTRY
, HT_RECT
);
687 void OnResize() override
689 /* Adjust the number of items in the matrix depending of the resize */
690 this->vscroll
->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET
);
693 void OnPlaceObject([[maybe_unused
]] Point pt
, TileIndex tile
) override
696 /* We do not need to protect ourselves against "Random Many Industries" in this mode */
697 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
698 uint32_t seed
= InteractiveRandom();
699 uint32_t layout_index
= InteractiveRandomRange((uint32_t)indsp
->layouts
.size());
701 if (_game_mode
== GM_EDITOR
) {
702 /* Show error if no town exists at all */
703 if (Town::GetNumItems() == 0) {
704 SetDParam(0, indsp
->name
);
705 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
, pt
.x
, pt
.y
);
709 Backup
<CompanyID
> cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
710 Backup
<bool> old_generating_world(_generating_world
, true, FILE_LINE
);
711 _ignore_restrictions
= true;
713 Command
<CMD_BUILD_INDUSTRY
>::Post(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
, &CcBuildIndustry
, tile
, this->selected_type
, layout_index
, false, seed
);
715 cur_company
.Restore();
716 old_generating_world
.Restore();
717 _ignore_restrictions
= false;
719 success
= Command
<CMD_BUILD_INDUSTRY
>::Post(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
, tile
, this->selected_type
, layout_index
, false, seed
);
722 /* If an industry has been built, just reset the cursor and the system */
723 if (success
&& !_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
726 IntervalTimer
<TimerWindow
> update_interval
= {std::chrono::seconds(3), [this](auto) {
727 if (_game_mode
== GM_EDITOR
) return;
728 if (this->selected_type
== INVALID_INDUSTRYTYPE
) return;
730 bool enabled
= this->enabled
;
731 this->UpdateAvailability();
732 if (enabled
!= this->enabled
) {
738 void OnTimeout() override
740 this->RaiseButtons();
743 void OnPlaceObjectAbort() override
745 this->RaiseButtons();
749 * Some data on this window has become invalid.
750 * @param data Information about the changed data.
751 * @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.
753 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
755 if (!gui_scope
) return;
762 void ShowBuildIndustryWindow()
764 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
765 if (BringWindowToFrontById(WC_BUILD_INDUSTRY
, 0)) return;
766 new BuildIndustryWindow();
769 static void UpdateIndustryProduction(Industry
*i
);
771 static inline bool IsProductionAlterable(const Industry
*i
)
773 const IndustrySpec
*is
= GetIndustrySpec(i
->type
);
774 bool has_prod
= false;
775 for (size_t j
= 0; j
< lengthof(is
->production_rate
); j
++) {
776 if (is
->production_rate
[j
] != 0) {
781 return ((_game_mode
== GM_EDITOR
|| _cheats
.setup_prod
.value
) &&
782 (has_prod
|| is
->IsRawIndustry()) &&
786 class IndustryViewWindow
: public Window
788 /** Modes for changing production */
790 EA_NONE
, ///< Not alterable
791 EA_MULTIPLIER
, ///< Allow changing the production multiplier
792 EA_RATE
, ///< Allow changing the production rates
795 /** Specific lines in the info panel */
797 IL_NONE
, ///< No line
798 IL_MULTIPLIER
, ///< Production multiplier
799 IL_RATE1
, ///< Production rate of cargo 1
800 IL_RATE2
, ///< Production rate of cargo 2
803 Dimension cargo_icon_size
; ///< Largest cargo icon dimension.
804 Editability editable
; ///< Mode for changing production
805 InfoLine editbox_line
; ///< The line clicked to open the edit box
806 InfoLine clicked_line
; ///< The line of the button that has been clicked
807 byte clicked_button
; ///< The button that has been clicked (to raise)
808 int production_offset_y
; ///< The offset of the production texts/buttons
809 int info_height
; ///< Height needed for the #WID_IV_INFO panel
810 int cheat_line_height
; ///< Height of each line for the #WID_IV_INFO panel
813 IndustryViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
815 this->flags
|= WF_DISABLE_VP_SCROLL
;
816 this->editbox_line
= IL_NONE
;
817 this->clicked_line
= IL_NONE
;
818 this->clicked_button
= 0;
819 this->info_height
= WidgetDimensions::scaled
.framerect
.Vertical() + 2 * GetCharacterHeight(FS_NORMAL
); // Info panel has at least two lines text.
821 this->InitNested(window_number
);
822 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
823 nvp
->InitializeViewport(this, Industry::Get(window_number
)->location
.GetCenterTile(), ScaleZoomGUI(ZOOM_LVL_INDUSTRY
));
825 this->InvalidateData();
828 void OnInit() override
830 /* This only used when the cheat to alter industry production is enabled */
831 this->cheat_line_height
= std::max(SETTING_BUTTON_HEIGHT
+ WidgetDimensions::scaled
.vsep_normal
, GetCharacterHeight(FS_NORMAL
));
832 this->cargo_icon_size
= GetLargestCargoIconSize();
835 void OnPaint() override
839 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
841 const Rect r
= this->GetWidget
<NWidgetBase
>(WID_IV_INFO
)->GetCurrentRect();
842 int expected
= this->DrawInfo(r
);
843 if (expected
!= r
.bottom
) {
844 this->info_height
= expected
- r
.top
+ 1;
850 void DrawCargoIcon(const Rect
&r
, CargoID cid
) const
852 bool rtl
= _current_text_dir
== TD_RTL
;
853 SpriteID icon
= CargoSpec::Get(cid
)->GetCargoIcon();
854 Dimension d
= GetSpriteSize(icon
);
855 Rect ir
= r
.WithWidth(this->cargo_icon_size
.width
, rtl
).WithHeight(GetCharacterHeight(FS_NORMAL
));
856 DrawSprite(icon
, PAL_NONE
, CenterBounds(ir
.left
, ir
.right
, d
.width
), CenterBounds(ir
.top
, ir
.bottom
, this->cargo_icon_size
.height
));
860 * Draw the text in the #WID_IV_INFO panel.
861 * @param r Rectangle of the panel.
862 * @return Expected position of the bottom edge of the panel.
864 int DrawInfo(const Rect
&r
)
866 bool rtl
= _current_text_dir
== TD_RTL
;
867 Industry
*i
= Industry::Get(this->window_number
);
868 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
869 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
871 bool has_accept
= false;
873 if (i
->prod_level
== PRODLEVEL_CLOSURE
) {
874 DrawString(ir
, STR_INDUSTRY_VIEW_INDUSTRY_ANNOUNCED_CLOSURE
);
875 ir
.top
+= GetCharacterHeight(FS_NORMAL
) + WidgetDimensions::scaled
.vsep_wide
;
878 const int label_indent
= WidgetDimensions::scaled
.hsep_normal
+ this->cargo_icon_size
.width
;
879 bool stockpiling
= HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_CARGO_ARRIVAL
) || HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_256_TICKS
);
881 for (const auto &a
: i
->accepted
) {
882 if (!IsValidCargoID(a
.cargo
)) continue;
885 DrawString(ir
, STR_INDUSTRY_VIEW_REQUIRES
);
886 ir
.top
+= GetCharacterHeight(FS_NORMAL
);
890 DrawCargoIcon(ir
, a
.cargo
);
893 GetCargoSuffix(CARGOSUFFIX_IN
, CST_VIEW
, i
, i
->type
, ind
, a
.cargo
, &a
- i
->accepted
.data(), suffix
);
895 SetDParam(0, CargoSpec::Get(a
.cargo
)->name
);
896 SetDParam(1, a
.cargo
);
897 SetDParam(2, a
.waiting
);
899 StringID str
= STR_NULL
;
900 switch (suffix
.display
) {
901 case CSD_CARGO_AMOUNT_TEXT
:
902 SetDParamStr(3, suffix
.text
);
904 case CSD_CARGO_AMOUNT
:
905 str
= stockpiling
? STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT
: STR_INDUSTRY_VIEW_ACCEPT_CARGO
;
909 SetDParamStr(3, suffix
.text
);
912 str
= STR_INDUSTRY_VIEW_ACCEPT_CARGO
;
918 DrawString(ir
.Indent(label_indent
, rtl
), str
);
919 ir
.top
+= GetCharacterHeight(FS_NORMAL
);
922 int line_height
= this->editable
== EA_RATE
? this->cheat_line_height
: GetCharacterHeight(FS_NORMAL
);
923 int text_y_offset
= (line_height
- GetCharacterHeight(FS_NORMAL
)) / 2;
924 int button_y_offset
= (line_height
- SETTING_BUTTON_HEIGHT
) / 2;
926 for (const auto &p
: i
->produced
) {
927 if (!IsValidCargoID(p
.cargo
)) continue;
929 if (has_accept
) ir
.top
+= WidgetDimensions::scaled
.vsep_wide
;
930 DrawString(ir
, TimerGameEconomy::UsingWallclockUnits() ? STR_INDUSTRY_VIEW_PRODUCTION_LAST_MINUTE_TITLE
: STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE
);
931 ir
.top
+= GetCharacterHeight(FS_NORMAL
);
932 if (this->editable
== EA_RATE
) this->production_offset_y
= ir
.top
;
936 DrawCargoIcon(ir
, p
.cargo
);
939 GetCargoSuffix(CARGOSUFFIX_OUT
, CST_VIEW
, i
, i
->type
, ind
, p
.cargo
, &p
- i
->produced
.data(), suffix
);
941 SetDParam(0, p
.cargo
);
942 SetDParam(1, p
.history
[LAST_MONTH
].production
);
943 SetDParamStr(2, suffix
.text
);
944 SetDParam(3, ToPercent8(p
.history
[LAST_MONTH
].PctTransported()));
945 DrawString(ir
.Indent(label_indent
+ (this->editable
== EA_RATE
? SETTING_BUTTON_WIDTH
+ WidgetDimensions::scaled
.hsep_normal
: 0), rtl
).Translate(0, text_y_offset
), STR_INDUSTRY_VIEW_TRANSPORTED
);
946 /* Let's put out those buttons.. */
947 if (this->editable
== EA_RATE
) {
948 DrawArrowButtons(ir
.Indent(label_indent
, rtl
).WithWidth(SETTING_BUTTON_WIDTH
, rtl
).left
, ir
.top
+ button_y_offset
, COLOUR_YELLOW
, (this->clicked_line
== IL_RATE1
+ (&p
- i
->produced
.data())) ? this->clicked_button
: 0,
949 p
.rate
> 0, p
.rate
< 255);
951 ir
.top
+= line_height
;
954 /* Display production multiplier if editable */
955 if (this->editable
== EA_MULTIPLIER
) {
956 line_height
= this->cheat_line_height
;
957 text_y_offset
= (line_height
- GetCharacterHeight(FS_NORMAL
)) / 2;
958 button_y_offset
= (line_height
- SETTING_BUTTON_HEIGHT
) / 2;
959 ir
.top
+= WidgetDimensions::scaled
.vsep_wide
;
960 this->production_offset_y
= ir
.top
;
961 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
962 DrawString(ir
.Indent(label_indent
+ SETTING_BUTTON_WIDTH
+ WidgetDimensions::scaled
.hsep_normal
, rtl
).Translate(0, text_y_offset
), STR_INDUSTRY_VIEW_PRODUCTION_LEVEL
);
963 DrawArrowButtons(ir
.Indent(label_indent
, rtl
).WithWidth(SETTING_BUTTON_WIDTH
, rtl
).left
, ir
.top
+ button_y_offset
, COLOUR_YELLOW
, (this->clicked_line
== IL_MULTIPLIER
) ? this->clicked_button
: 0,
964 i
->prod_level
> PRODLEVEL_MINIMUM
, i
->prod_level
< PRODLEVEL_MAXIMUM
);
965 ir
.top
+= line_height
;
968 /* Get the extra message for the GUI */
969 if (HasBit(ind
->callback_mask
, CBM_IND_WINDOW_MORE_TEXT
)) {
970 uint16_t callback_res
= GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT
, 0, 0, i
, i
->type
, i
->location
.tile
);
971 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
972 if (callback_res
> 0x400) {
973 ErrorUnknownCallbackResult(ind
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_WINDOW_MORE_TEXT
, callback_res
);
975 StringID message
= GetGRFStringID(ind
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
);
976 if (message
!= STR_NULL
&& message
!= STR_UNDEFINED
) {
977 ir
.top
+= WidgetDimensions::scaled
.vsep_wide
;
979 StartTextRefStackUsage(ind
->grf_prop
.grffile
, 6);
980 /* Use all the available space left from where we stand up to the
981 * end of the window. We ALSO enlarge the window if needed, so we
982 * can 'go' wild with the bottom of the window. */
983 ir
.top
= DrawStringMultiLine(ir
.left
, ir
.right
, ir
.top
, UINT16_MAX
, message
, TC_BLACK
);
984 StopTextRefStackUsage();
990 if (!i
->text
.empty()) {
991 SetDParamStr(0, i
->text
);
992 ir
.top
+= WidgetDimensions::scaled
.vsep_wide
;
993 ir
.top
= DrawStringMultiLine(ir
.left
, ir
.right
, ir
.top
, UINT16_MAX
, STR_JUST_RAW_STRING
, TC_BLACK
);
996 /* Return required bottom position, the last pixel row plus some padding. */
997 return ir
.top
- 1 + WidgetDimensions::scaled
.framerect
.bottom
;
1000 void SetStringParameters(WidgetID widget
) const override
1002 if (widget
== WID_IV_CAPTION
) SetDParam(0, this->window_number
);
1005 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
1007 if (widget
== WID_IV_INFO
) size
->height
= this->info_height
;
1010 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1014 Industry
*i
= Industry::Get(this->window_number
);
1015 InfoLine line
= IL_NONE
;
1017 switch (this->editable
) {
1018 case EA_NONE
: break;
1021 if (IsInsideBS(pt
.y
, this->production_offset_y
, this->cheat_line_height
)) line
= IL_MULTIPLIER
;
1025 if (pt
.y
>= this->production_offset_y
) {
1026 int row
= (pt
.y
- this->production_offset_y
) / this->cheat_line_height
;
1027 for (auto itp
= std::begin(i
->produced
); itp
!= std::end(i
->produced
); ++itp
) {
1028 if (!IsValidCargoID(itp
->cargo
)) continue;
1031 line
= (InfoLine
)(IL_RATE1
+ (itp
- std::begin(i
->produced
)));
1038 if (line
== IL_NONE
) return;
1040 bool rtl
= _current_text_dir
== TD_RTL
;
1041 Rect r
= this->GetWidget
<NWidgetBase
>(widget
)->GetCurrentRect().Shrink(WidgetDimensions::scaled
.framerect
).Indent(this->cargo_icon_size
.width
+ WidgetDimensions::scaled
.hsep_normal
, rtl
);
1043 if (r
.WithWidth(SETTING_BUTTON_WIDTH
, rtl
).Contains(pt
)) {
1044 /* Clicked buttons, decrease or increase production */
1045 bool decrease
= r
.WithWidth(SETTING_BUTTON_WIDTH
/ 2, rtl
).Contains(pt
);
1046 switch (this->editable
) {
1049 if (i
->prod_level
<= PRODLEVEL_MINIMUM
) return;
1050 i
->prod_level
= static_cast<byte
>(std::max
<uint
>(i
->prod_level
/ 2, PRODLEVEL_MINIMUM
));
1052 if (i
->prod_level
>= PRODLEVEL_MAXIMUM
) return;
1053 i
->prod_level
= static_cast<byte
>(std::min
<uint
>(i
->prod_level
* 2, PRODLEVEL_MAXIMUM
));
1059 if (i
->produced
[line
- IL_RATE1
].rate
<= 0) return;
1060 i
->produced
[line
- IL_RATE1
].rate
= std::max(i
->produced
[line
- IL_RATE1
].rate
/ 2, 0);
1062 if (i
->produced
[line
- IL_RATE1
].rate
>= 255) return;
1063 /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
1064 int new_prod
= i
->produced
[line
- IL_RATE1
].rate
== 0 ? 1 : i
->produced
[line
- IL_RATE1
].rate
* 2;
1065 i
->produced
[line
- IL_RATE1
].rate
= ClampTo
<byte
>(new_prod
);
1069 default: NOT_REACHED();
1072 UpdateIndustryProduction(i
);
1075 this->clicked_line
= line
;
1076 this->clicked_button
= (decrease
^ rtl
) ? 1 : 2;
1077 } else if (r
.Indent(SETTING_BUTTON_WIDTH
+ WidgetDimensions::scaled
.hsep_normal
, rtl
).Contains(pt
)) {
1078 /* clicked the text */
1079 this->editbox_line
= line
;
1080 switch (this->editable
) {
1082 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
1083 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION_LEVEL
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
1087 SetDParam(0, i
->produced
[line
- IL_RATE1
].rate
* 8);
1088 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
1091 default: NOT_REACHED();
1098 Industry
*i
= Industry::Get(this->window_number
);
1099 if (_ctrl_pressed
) {
1100 ShowExtraViewportWindow(i
->location
.GetCenterTile());
1102 ScrollMainWindowToTile(i
->location
.GetCenterTile());
1107 case WID_IV_DISPLAY
: {
1108 Industry
*i
= Industry::Get(this->window_number
);
1109 ShowIndustryCargoesWindow(i
->type
);
1115 void OnTimeout() override
1117 this->clicked_line
= IL_NONE
;
1118 this->clicked_button
= 0;
1122 void OnResize() override
1124 if (this->viewport
!= nullptr) {
1125 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
1126 nvp
->UpdateViewportCoordinates(this);
1128 ScrollWindowToTile(Industry::Get(this->window_number
)->location
.GetCenterTile(), this, true); // Re-center viewport.
1132 void OnQueryTextFinished(char *str
) override
1134 if (StrEmpty(str
)) return;
1136 Industry
*i
= Industry::Get(this->window_number
);
1137 uint value
= atoi(str
);
1138 switch (this->editbox_line
) {
1139 case IL_NONE
: NOT_REACHED();
1142 i
->prod_level
= ClampU(RoundDivSU(value
* PRODLEVEL_DEFAULT
, 100), PRODLEVEL_MINIMUM
, PRODLEVEL_MAXIMUM
);
1146 i
->produced
[this->editbox_line
- IL_RATE1
].rate
= ClampU(RoundDivSU(value
, 8), 0, 255);
1149 UpdateIndustryProduction(i
);
1154 * Some data on this window has become invalid.
1155 * @param data Information about the changed data.
1156 * @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.
1158 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
1160 if (!gui_scope
) return;
1161 const Industry
*i
= Industry::Get(this->window_number
);
1162 if (IsProductionAlterable(i
)) {
1163 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
1164 this->editable
= ind
->UsesOriginalEconomy() ? EA_MULTIPLIER
: EA_RATE
;
1166 this->editable
= EA_NONE
;
1170 bool IsNewGRFInspectable() const override
1172 return ::IsNewGRFInspectable(GSF_INDUSTRIES
, this->window_number
);
1175 void ShowNewGRFInspectWindow() const override
1177 ::ShowNewGRFInspectWindow(GSF_INDUSTRIES
, this->window_number
);
1181 static void UpdateIndustryProduction(Industry
*i
)
1183 const IndustrySpec
*indspec
= GetIndustrySpec(i
->type
);
1184 if (indspec
->UsesOriginalEconomy()) i
->RecomputeProductionMultipliers();
1186 for (auto &p
: i
->produced
) {
1187 if (IsValidCargoID(p
.cargo
)) {
1188 p
.history
[LAST_MONTH
].production
= ScaleByCargoScale(8 * p
.rate
, false);
1193 /** Widget definition of the view industry gui */
1194 static constexpr NWidgetPart _nested_industry_view_widgets
[] = {
1195 NWidget(NWID_HORIZONTAL
),
1196 NWidget(WWT_CLOSEBOX
, COLOUR_CREAM
),
1197 NWidget(WWT_CAPTION
, COLOUR_CREAM
, WID_IV_CAPTION
), SetDataTip(STR_INDUSTRY_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1198 NWidget(WWT_PUSHIMGBTN
, COLOUR_CREAM
, WID_IV_GOTO
), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION
, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP
),
1199 NWidget(WWT_DEBUGBOX
, COLOUR_CREAM
),
1200 NWidget(WWT_SHADEBOX
, COLOUR_CREAM
),
1201 NWidget(WWT_DEFSIZEBOX
, COLOUR_CREAM
),
1202 NWidget(WWT_STICKYBOX
, COLOUR_CREAM
),
1204 NWidget(WWT_PANEL
, COLOUR_CREAM
),
1205 NWidget(WWT_INSET
, COLOUR_CREAM
), SetPadding(2, 2, 2, 2),
1206 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_IV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1),
1209 NWidget(WWT_PANEL
, COLOUR_CREAM
, WID_IV_INFO
), SetMinimalSize(260, 0), SetMinimalTextLines(2, WidgetDimensions::unscaled
.framerect
.Vertical()), SetResize(1, 0),
1211 NWidget(NWID_HORIZONTAL
),
1212 NWidget(WWT_PUSHTXTBTN
, COLOUR_CREAM
, WID_IV_DISPLAY
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
1213 NWidget(WWT_RESIZEBOX
, COLOUR_CREAM
),
1217 /** Window definition of the view industry gui */
1218 static WindowDesc
_industry_view_desc(__FILE__
, __LINE__
,
1219 WDP_AUTO
, "view_industry", 260, 120,
1220 WC_INDUSTRY_VIEW
, WC_NONE
,
1222 std::begin(_nested_industry_view_widgets
), std::end(_nested_industry_view_widgets
)
1225 void ShowIndustryViewWindow(int industry
)
1227 AllocateWindowDescFront
<IndustryViewWindow
>(&_industry_view_desc
, industry
);
1230 /** Widget definition of the industry directory gui */
1231 static constexpr NWidgetPart _nested_industry_directory_widgets
[] = {
1232 NWidget(NWID_HORIZONTAL
),
1233 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1234 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1235 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1236 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1237 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1239 NWidget(NWID_HORIZONTAL
),
1240 NWidget(NWID_VERTICAL
),
1241 NWidget(NWID_HORIZONTAL
),
1242 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_ID_DROPDOWN_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
1243 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_ID_DROPDOWN_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
1244 NWidget(WWT_EDITBOX
, COLOUR_BROWN
, WID_ID_FILTER
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
1246 NWidget(NWID_HORIZONTAL
),
1247 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_ID_FILTER_BY_ACC_CARGO
), SetMinimalSize(225, 12), SetFill(0, 1), SetDataTip(STR_INDUSTRY_DIRECTORY_ACCEPTED_CARGO_FILTER
, STR_TOOLTIP_FILTER_CRITERIA
),
1248 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_ID_FILTER_BY_PROD_CARGO
), SetMinimalSize(225, 12), SetFill(0, 1), SetDataTip(STR_INDUSTRY_DIRECTORY_PRODUCED_CARGO_FILTER
, STR_TOOLTIP_FILTER_CRITERIA
),
1249 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetResize(1, 0), EndContainer(),
1251 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_ID_INDUSTRY_LIST
), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION
), SetResize(1, 1), SetScrollbar(WID_ID_VSCROLLBAR
),
1254 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_ID_VSCROLLBAR
),
1256 NWidget(NWID_HORIZONTAL
),
1257 NWidget(NWID_HSCROLLBAR
, COLOUR_BROWN
, WID_ID_HSCROLLBAR
),
1258 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1262 typedef GUIList
<const Industry
*, const CargoID
&, const std::pair
<CargoID
, CargoID
> &> GUIIndustryList
;
1264 /** Cargo filter functions */
1266 * Check whether an industry accepts and produces a certain cargo pair.
1267 * @param industry The industry whose cargoes will being checked.
1268 * @param cargoes The accepted and produced cargo pair to look for.
1269 * @return bool Whether the given cargoes accepted and produced by the industry.
1271 static bool CDECL
CargoFilter(const Industry
* const *industry
, const std::pair
<CargoID
, CargoID
> &cargoes
)
1273 auto accepted_cargo
= cargoes
.first
;
1274 auto produced_cargo
= cargoes
.second
;
1276 bool accepted_cargo_matches
;
1278 switch (accepted_cargo
) {
1279 case CargoFilterCriteria::CF_ANY
:
1280 accepted_cargo_matches
= true;
1283 case CargoFilterCriteria::CF_NONE
:
1284 accepted_cargo_matches
= !(*industry
)->IsCargoAccepted();
1288 accepted_cargo_matches
= (*industry
)->IsCargoAccepted(accepted_cargo
);
1292 bool produced_cargo_matches
;
1294 switch (produced_cargo
) {
1295 case CargoFilterCriteria::CF_ANY
:
1296 produced_cargo_matches
= true;
1299 case CargoFilterCriteria::CF_NONE
:
1300 produced_cargo_matches
= !(*industry
)->IsCargoProduced();
1304 produced_cargo_matches
= (*industry
)->IsCargoProduced(produced_cargo
);
1308 return accepted_cargo_matches
&& produced_cargo_matches
;
1311 static GUIIndustryList::FilterFunction
* const _filter_funcs
[] = { &CargoFilter
};
1313 /** Enum referring to the Hotkeys in the industry directory window */
1314 enum IndustryDirectoryHotkeys
{
1315 IDHK_FOCUS_FILTER_BOX
, ///< Focus the filter box
1318 * The list of industries.
1320 class IndustryDirectoryWindow
: public Window
{
1322 /* Runtime saved values */
1323 static Listing last_sorting
;
1325 /* Constants for sorting industries */
1326 static const StringID sorter_names
[];
1327 static GUIIndustryList::SortFunction
* const sorter_funcs
[];
1329 GUIIndustryList industries
{IndustryDirectoryWindow::produced_cargo_filter
};
1333 CargoID produced_cargo_filter_criteria
; ///< Selected produced cargo filter index
1334 CargoID accepted_cargo_filter_criteria
; ///< Selected accepted cargo filter index
1335 static CargoID produced_cargo_filter
;
1337 const int MAX_FILTER_LENGTH
= 16; ///< The max length of the filter, in chars
1338 StringFilter string_filter
; ///< Filter for industries
1339 QueryString industry_editbox
; ///< Filter editbox
1341 enum class SorterType
: uint8_t {
1342 ByName
, ///< Sorter type to sort by name
1343 ByType
, ///< Sorter type to sort by type
1344 ByProduction
, ///< Sorter type to sort by production amount
1345 ByTransported
, ///< Sorter type to sort by transported percentage
1349 * Set produced cargo filter for the industry list.
1350 * @param cid The cargo to be set
1352 void SetProducedCargoFilter(CargoID cid
)
1354 if (this->produced_cargo_filter_criteria
!= cid
) {
1355 this->produced_cargo_filter_criteria
= cid
;
1356 /* deactivate filter if criteria is 'Show All', activate it otherwise */
1357 bool is_filtering_necessary
= this->produced_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
|| this->accepted_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
;
1359 this->industries
.SetFilterState(is_filtering_necessary
);
1360 this->industries
.SetFilterType(0);
1361 this->industries
.ForceRebuild();
1366 * Set accepted cargo filter for the industry list.
1367 * @param index The cargo to be set
1369 void SetAcceptedCargoFilter(CargoID cid
)
1371 if (this->accepted_cargo_filter_criteria
!= cid
) {
1372 this->accepted_cargo_filter_criteria
= cid
;
1373 /* deactivate filter if criteria is 'Show All', activate it otherwise */
1374 bool is_filtering_necessary
= this->produced_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
|| this->accepted_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
;
1376 this->industries
.SetFilterState(is_filtering_necessary
);
1377 this->industries
.SetFilterType(0);
1378 this->industries
.ForceRebuild();
1382 StringID
GetCargoFilterLabel(CargoID cid
) const
1385 case CargoFilterCriteria::CF_ANY
: return STR_INDUSTRY_DIRECTORY_FILTER_ALL_TYPES
;
1386 case CargoFilterCriteria::CF_NONE
: return STR_INDUSTRY_DIRECTORY_FILTER_NONE
;
1387 default: return CargoSpec::Get(cid
)->name
;
1392 * Populate the filter list and set the cargo filter criteria.
1394 void SetCargoFilterArray()
1396 this->produced_cargo_filter_criteria
= CargoFilterCriteria::CF_ANY
;
1397 this->accepted_cargo_filter_criteria
= CargoFilterCriteria::CF_ANY
;
1399 this->industries
.SetFilterFuncs(_filter_funcs
);
1401 bool is_filtering_necessary
= this->produced_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
|| this->accepted_cargo_filter_criteria
!= CargoFilterCriteria::CF_ANY
;
1403 this->industries
.SetFilterState(is_filtering_necessary
);
1407 * Get the width needed to draw the longest industry line.
1408 * @return Returns width of the longest industry line, including padding.
1410 uint
GetIndustryListWidth() const
1413 for (const Industry
*i
: this->industries
) {
1414 width
= std::max(width
, GetStringBoundingBox(this->GetIndustryString(i
)).width
);
1416 return width
+ WidgetDimensions::scaled
.framerect
.Horizontal();
1419 /** (Re)Build industries list */
1420 void BuildSortIndustriesList()
1422 if (this->industries
.NeedRebuild()) {
1423 this->industries
.clear();
1425 for (const Industry
*i
: Industry::Iterate()) {
1426 if (this->string_filter
.IsEmpty()) {
1427 this->industries
.push_back(i
);
1430 this->string_filter
.ResetState();
1431 this->string_filter
.AddLine(i
->GetCachedName());
1432 if (this->string_filter
.GetState()) this->industries
.push_back(i
);
1435 this->industries
.shrink_to_fit();
1436 this->industries
.RebuildDone();
1438 auto filter
= std::make_pair(this->accepted_cargo_filter_criteria
, this->produced_cargo_filter_criteria
);
1440 this->industries
.Filter(filter
);
1442 this->hscroll
->SetCount(this->GetIndustryListWidth());
1443 this->vscroll
->SetCount(this->industries
.size()); // Update scrollbar as well.
1446 IndustryDirectoryWindow::produced_cargo_filter
= this->produced_cargo_filter_criteria
;
1447 this->industries
.Sort();
1453 * Returns percents of cargo transported if industry produces this cargo, else -1
1455 * @param i industry to check
1456 * @param id cargo slot
1457 * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot
1459 static inline int GetCargoTransportedPercentsIfValid(const Industry::ProducedCargo
&p
)
1461 if (!IsValidCargoID(p
.cargo
)) return -1;
1462 return ToPercent8(p
.history
[LAST_MONTH
].PctTransported());
1466 * Returns value representing industry's transported cargo
1467 * percentage for industry sorting
1469 * @param i industry to check
1470 * @return value used for sorting
1472 static int GetCargoTransportedSortValue(const Industry
*i
)
1474 CargoID filter
= IndustryDirectoryWindow::produced_cargo_filter
;
1475 if (filter
== CargoFilterCriteria::CF_NONE
) return 0;
1477 int percentage
= 0, produced_cargo_count
= 0;
1478 for (const auto &p
: i
->produced
) {
1479 if (filter
== CargoFilterCriteria::CF_ANY
) {
1480 int transported
= GetCargoTransportedPercentsIfValid(p
);
1481 if (transported
!= -1) {
1482 produced_cargo_count
++;
1483 percentage
+= transported
;
1485 if (produced_cargo_count
== 0 && &p
== &i
->produced
.back() && percentage
== 0) {
1488 } else if (filter
== p
.cargo
) {
1489 return GetCargoTransportedPercentsIfValid(p
);
1493 if (produced_cargo_count
== 0) return percentage
;
1494 return percentage
/ produced_cargo_count
;
1497 /** Sort industries by name */
1498 static bool IndustryNameSorter(const Industry
* const &a
, const Industry
* const &b
, const CargoID
&)
1500 int r
= StrNaturalCompare(a
->GetCachedName(), b
->GetCachedName()); // Sort by name (natural sorting).
1501 if (r
== 0) return a
->index
< b
->index
;
1505 /** Sort industries by type and name */
1506 static bool IndustryTypeSorter(const Industry
* const &a
, const Industry
* const &b
, const CargoID
&filter
)
1509 while (it_a
!= NUM_INDUSTRYTYPES
&& a
->type
!= _sorted_industry_types
[it_a
]) it_a
++;
1511 while (it_b
!= NUM_INDUSTRYTYPES
&& b
->type
!= _sorted_industry_types
[it_b
]) it_b
++;
1512 int r
= it_a
- it_b
;
1513 return (r
== 0) ? IndustryNameSorter(a
, b
, filter
) : r
< 0;
1516 /** Sort industries by production and name */
1517 static bool IndustryProductionSorter(const Industry
* const &a
, const Industry
* const &b
, const CargoID
&filter
)
1519 if (filter
== CargoFilterCriteria::CF_NONE
) return IndustryTypeSorter(a
, b
, filter
);
1521 uint prod_a
= 0, prod_b
= 0;
1522 if (filter
== CargoFilterCriteria::CF_ANY
) {
1523 for (const auto &pa
: a
->produced
) {
1524 if (IsValidCargoID(pa
.cargo
)) prod_a
+= pa
.history
[LAST_MONTH
].production
;
1526 for (const auto &pb
: b
->produced
) {
1527 if (IsValidCargoID(pb
.cargo
)) prod_b
+= pb
.history
[LAST_MONTH
].production
;
1530 if (auto ita
= a
->GetCargoProduced(filter
); ita
!= std::end(a
->produced
)) prod_a
= ita
->history
[LAST_MONTH
].production
;
1531 if (auto itb
= b
->GetCargoProduced(filter
); itb
!= std::end(b
->produced
)) prod_b
= itb
->history
[LAST_MONTH
].production
;
1533 int r
= prod_a
- prod_b
;
1535 return (r
== 0) ? IndustryTypeSorter(a
, b
, filter
) : r
< 0;
1538 /** Sort industries by transported cargo and name */
1539 static bool IndustryTransportedCargoSorter(const Industry
* const &a
, const Industry
* const &b
, const CargoID
&filter
)
1541 int r
= GetCargoTransportedSortValue(a
) - GetCargoTransportedSortValue(b
);
1542 return (r
== 0) ? IndustryNameSorter(a
, b
, filter
) : r
< 0;
1546 * Get the StringID to draw and set the appropriate DParams.
1547 * @param i the industry to get the StringID of.
1548 * @return the StringID.
1550 StringID
GetIndustryString(const Industry
*i
) const
1552 const IndustrySpec
*indsp
= GetIndustrySpec(i
->type
);
1556 SetDParam(p
++, i
->index
);
1558 static CargoSuffix cargo_suffix
[INDUSTRY_NUM_OUTPUTS
];
1560 /* Get industry productions (CargoID, production, suffix, transported) */
1563 uint16_t production
;
1567 std::vector
<CargoInfo
> cargos
;
1569 for (auto itp
= std::begin(i
->produced
); itp
!= std::end(i
->produced
); ++itp
) {
1570 if (!IsValidCargoID(itp
->cargo
)) continue;
1571 GetCargoSuffix(CARGOSUFFIX_OUT
, CST_DIR
, i
, i
->type
, indsp
, itp
->cargo
, itp
- std::begin(i
->produced
), cargo_suffix
[itp
- std::begin(i
->produced
)]);
1572 cargos
.push_back({ itp
->cargo
, itp
->history
[LAST_MONTH
].production
, cargo_suffix
[itp
- std::begin(i
->produced
)].text
.c_str(), ToPercent8(itp
->history
[LAST_MONTH
].PctTransported()) });
1575 switch (static_cast<IndustryDirectoryWindow::SorterType
>(this->industries
.SortType())) {
1576 case IndustryDirectoryWindow::SorterType::ByName
:
1577 case IndustryDirectoryWindow::SorterType::ByType
:
1578 case IndustryDirectoryWindow::SorterType::ByProduction
:
1579 /* Sort by descending production, then descending transported */
1580 std::sort(cargos
.begin(), cargos
.end(), [](const CargoInfo
&a
, const CargoInfo
&b
) {
1581 if (a
.production
!= b
.production
) return a
.production
> b
.production
;
1582 return a
.transported
> b
.transported
;
1586 case IndustryDirectoryWindow::SorterType::ByTransported
:
1587 /* Sort by descending transported, then descending production */
1588 std::sort(cargos
.begin(), cargos
.end(), [](const CargoInfo
&a
, const CargoInfo
&b
) {
1589 if (a
.transported
!= b
.transported
) return a
.transported
> b
.transported
;
1590 return a
.production
> b
.production
;
1595 /* If the produced cargo filter is active then move the filtered cargo to the beginning of the list,
1596 * because this is the one the player interested in, and that way it is not hidden in the 'n' more cargos */
1597 const CargoID cid
= this->produced_cargo_filter_criteria
;
1598 if (cid
!= CargoFilterCriteria::CF_ANY
&& cid
!= CargoFilterCriteria::CF_NONE
) {
1599 auto filtered_ci
= std::find_if(cargos
.begin(), cargos
.end(), [cid
](const CargoInfo
&ci
) -> bool {
1600 return ci
.cargo_id
== cid
;
1602 if (filtered_ci
!= cargos
.end()) {
1603 std::rotate(cargos
.begin(), filtered_ci
, filtered_ci
+ 1);
1607 /* Display first 3 cargos */
1608 for (size_t j
= 0; j
< std::min
<size_t>(3, cargos
.size()); j
++) {
1609 CargoInfo ci
= cargos
[j
];
1610 SetDParam(p
++, STR_INDUSTRY_DIRECTORY_ITEM_INFO
);
1611 SetDParam(p
++, ci
.cargo_id
);
1612 SetDParam(p
++, ci
.production
);
1613 SetDParamStr(p
++, ci
.suffix
);
1614 SetDParam(p
++, ci
.transported
);
1617 /* Undisplayed cargos if any */
1618 SetDParam(p
++, cargos
.size() - 3);
1620 /* Drawing the right string */
1621 switch (cargos
.size()) {
1622 case 0: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD
;
1623 case 1: return STR_INDUSTRY_DIRECTORY_ITEM_PROD1
;
1624 case 2: return STR_INDUSTRY_DIRECTORY_ITEM_PROD2
;
1625 case 3: return STR_INDUSTRY_DIRECTORY_ITEM_PROD3
;
1626 default: return STR_INDUSTRY_DIRECTORY_ITEM_PRODMORE
;
1631 IndustryDirectoryWindow(WindowDesc
*desc
, WindowNumber
) : Window(desc
), industry_editbox(MAX_FILTER_LENGTH
* MAX_CHAR_LENGTH
, MAX_FILTER_LENGTH
)
1633 this->CreateNestedTree();
1634 this->vscroll
= this->GetScrollbar(WID_ID_VSCROLLBAR
);
1635 this->hscroll
= this->GetScrollbar(WID_ID_HSCROLLBAR
);
1637 this->industries
.SetListing(this->last_sorting
);
1638 this->industries
.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs
);
1639 this->industries
.ForceRebuild();
1641 this->FinishInitNested(0);
1643 this->BuildSortIndustriesList();
1645 this->querystrings
[WID_ID_FILTER
] = &this->industry_editbox
;
1646 this->industry_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
1649 ~IndustryDirectoryWindow()
1651 this->last_sorting
= this->industries
.GetListing();
1654 void OnInit() override
1656 this->SetCargoFilterArray();
1659 void SetStringParameters(WidgetID widget
) const override
1662 case WID_ID_DROPDOWN_CRITERIA
:
1663 SetDParam(0, IndustryDirectoryWindow::sorter_names
[this->industries
.SortType()]);
1666 case WID_ID_FILTER_BY_ACC_CARGO
:
1667 SetDParam(0, this->GetCargoFilterLabel(this->accepted_cargo_filter_criteria
));
1670 case WID_ID_FILTER_BY_PROD_CARGO
:
1671 SetDParam(0, this->GetCargoFilterLabel(this->produced_cargo_filter_criteria
));
1676 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
1679 case WID_ID_DROPDOWN_ORDER
:
1680 this->DrawSortButtonState(widget
, this->industries
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
1683 case WID_ID_INDUSTRY_LIST
: {
1684 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
1686 /* Setup a clipping rectangle... */
1687 DrawPixelInfo tmp_dpi
;
1688 if (!FillDrawPixelInfo(&tmp_dpi
, ir
)) return;
1689 /* ...but keep coordinates relative to the window. */
1690 tmp_dpi
.left
+= ir
.left
;
1691 tmp_dpi
.top
+= ir
.top
;
1693 AutoRestoreBackup
dpi_backup(_cur_dpi
, &tmp_dpi
);
1695 ir
.left
-= this->hscroll
->GetPosition();
1696 ir
.right
+= this->hscroll
->GetCapacity() - this->hscroll
->GetPosition();
1698 if (this->industries
.empty()) {
1699 DrawString(ir
, STR_INDUSTRY_DIRECTORY_NONE
);
1703 const CargoID acf_cid
= this->accepted_cargo_filter_criteria
;
1704 for (uint i
= this->vscroll
->GetPosition(); i
< this->industries
.size(); i
++) {
1705 TextColour tc
= TC_FROMSTRING
;
1706 if (acf_cid
!= CargoFilterCriteria::CF_ANY
&& acf_cid
!= CargoFilterCriteria::CF_NONE
) {
1707 Industry
*ind
= const_cast<Industry
*>(this->industries
[i
]);
1708 if (IndustryTemporarilyRefusesCargo(ind
, acf_cid
)) {
1709 tc
= TC_GREY
| TC_FORCED
;
1712 DrawString(ir
, this->GetIndustryString(this->industries
[i
]), tc
);
1714 ir
.top
+= this->resize
.step_height
;
1715 if (++n
== this->vscroll
->GetCapacity()) break; // max number of industries in 1 window
1722 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
1725 case WID_ID_DROPDOWN_ORDER
: {
1726 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
1727 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
1728 d
.height
+= padding
.height
;
1729 *size
= maxdim(*size
, d
);
1733 case WID_ID_DROPDOWN_CRITERIA
: {
1734 Dimension d
= {0, 0};
1735 for (uint i
= 0; IndustryDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
1736 d
= maxdim(d
, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names
[i
]));
1738 d
.width
+= padding
.width
;
1739 d
.height
+= padding
.height
;
1740 *size
= maxdim(*size
, d
);
1744 case WID_ID_INDUSTRY_LIST
: {
1745 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE
);
1746 resize
->height
= d
.height
;
1748 d
.width
+= padding
.width
;
1749 d
.height
+= padding
.height
;
1750 *size
= maxdim(*size
, d
);
1756 DropDownList
BuildCargoDropDownList() const
1760 /* Add item for disabling filtering. */
1761 list
.push_back(std::make_unique
<DropDownListStringItem
>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_ANY
), CargoFilterCriteria::CF_ANY
, false));
1762 /* Add item for industries not producing anything, e.g. power plants */
1763 list
.push_back(std::make_unique
<DropDownListStringItem
>(this->GetCargoFilterLabel(CargoFilterCriteria::CF_NONE
), CargoFilterCriteria::CF_NONE
, false));
1766 Dimension d
= GetLargestCargoIconSize();
1767 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
1768 list
.push_back(std::make_unique
<DropDownListIconItem
>(d
, cs
->GetCargoIcon(), PAL_NONE
, cs
->name
, cs
->Index(), false));
1774 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
1777 case WID_ID_DROPDOWN_ORDER
:
1778 this->industries
.ToggleSortOrder();
1782 case WID_ID_DROPDOWN_CRITERIA
:
1783 ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names
, this->industries
.SortType(), WID_ID_DROPDOWN_CRITERIA
, 0, 0);
1786 case WID_ID_FILTER_BY_ACC_CARGO
: // Cargo filter dropdown
1787 ShowDropDownList(this, this->BuildCargoDropDownList(), this->accepted_cargo_filter_criteria
, widget
);
1790 case WID_ID_FILTER_BY_PROD_CARGO
: // Cargo filter dropdown
1791 ShowDropDownList(this, this->BuildCargoDropDownList(), this->produced_cargo_filter_criteria
, widget
);
1794 case WID_ID_INDUSTRY_LIST
: {
1795 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->industries
, pt
.y
, this, WID_ID_INDUSTRY_LIST
, WidgetDimensions::scaled
.framerect
.top
);
1796 if (it
!= this->industries
.end()) {
1797 if (_ctrl_pressed
) {
1798 ShowExtraViewportWindow((*it
)->location
.tile
);
1800 ScrollMainWindowToTile((*it
)->location
.tile
);
1808 void OnDropdownSelect(WidgetID widget
, int index
) override
1811 case WID_ID_DROPDOWN_CRITERIA
: {
1812 if (this->industries
.SortType() != index
) {
1813 this->industries
.SetSortType(index
);
1814 this->BuildSortIndustriesList();
1819 case WID_ID_FILTER_BY_ACC_CARGO
: {
1820 this->SetAcceptedCargoFilter(index
);
1821 this->BuildSortIndustriesList();
1825 case WID_ID_FILTER_BY_PROD_CARGO
: {
1826 this->SetProducedCargoFilter(index
);
1827 this->BuildSortIndustriesList();
1833 void OnResize() override
1835 this->vscroll
->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST
);
1836 this->hscroll
->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST
);
1839 void OnEditboxChanged(WidgetID wid
) override
1841 if (wid
== WID_ID_FILTER
) {
1842 this->string_filter
.SetFilterTerm(this->industry_editbox
.text
.buf
);
1843 this->InvalidateData(IDIWD_FORCE_REBUILD
);
1847 void OnPaint() override
1849 if (this->industries
.NeedRebuild()) this->BuildSortIndustriesList();
1850 this->DrawWidgets();
1853 /** Rebuild the industry list on a regular interval. */
1854 IntervalTimer
<TimerWindow
> rebuild_interval
= {std::chrono::seconds(3), [this](auto) {
1855 this->industries
.ForceResort();
1856 this->BuildSortIndustriesList();
1860 * Some data on this window has become invalid.
1861 * @param data Information about the changed data.
1862 * @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.
1864 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
1867 case IDIWD_FORCE_REBUILD
:
1868 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1869 this->industries
.ForceRebuild();
1872 case IDIWD_PRODUCTION_CHANGE
:
1873 if (this->industries
.SortType() == 2) this->industries
.ForceResort();
1877 this->industries
.ForceResort();
1882 EventState
OnHotkey(int hotkey
) override
1885 case IDHK_FOCUS_FILTER_BOX
:
1886 this->SetFocusedWidget(WID_ID_FILTER
);
1887 SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
1890 return ES_NOT_HANDLED
;
1895 static inline HotkeyList hotkeys
{"industrydirectory", {
1896 Hotkey('F', "focus_filter_box", IDHK_FOCUS_FILTER_BOX
),
1900 Listing
IndustryDirectoryWindow::last_sorting
= {false, 0};
1902 /* Available station sorting functions. */
1903 GUIIndustryList::SortFunction
* const IndustryDirectoryWindow::sorter_funcs
[] = {
1904 &IndustryNameSorter
,
1905 &IndustryTypeSorter
,
1906 &IndustryProductionSorter
,
1907 &IndustryTransportedCargoSorter
1910 /* Names of the sorting functions */
1911 const StringID
IndustryDirectoryWindow::sorter_names
[] = {
1914 STR_SORT_BY_PRODUCTION
,
1915 STR_SORT_BY_TRANSPORTED
,
1919 CargoID
IndustryDirectoryWindow::produced_cargo_filter
= CargoFilterCriteria::CF_ANY
;
1922 /** Window definition of the industry directory gui */
1923 static WindowDesc
_industry_directory_desc(__FILE__
, __LINE__
,
1924 WDP_AUTO
, "list_industries", 428, 190,
1925 WC_INDUSTRY_DIRECTORY
, WC_NONE
,
1927 std::begin(_nested_industry_directory_widgets
), std::end(_nested_industry_directory_widgets
),
1928 &IndustryDirectoryWindow::hotkeys
1931 void ShowIndustryDirectory()
1933 AllocateWindowDescFront
<IndustryDirectoryWindow
>(&_industry_directory_desc
, 0);
1936 /** Widgets of the industry cargoes window. */
1937 static constexpr NWidgetPart _nested_industry_cargoes_widgets
[] = {
1938 NWidget(NWID_HORIZONTAL
),
1939 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1940 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_IC_CAPTION
), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1941 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1942 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1943 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1945 NWidget(NWID_HORIZONTAL
),
1946 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_IC_PANEL
), SetResize(1, 10), SetScrollbar(WID_IC_SCROLLBAR
), EndContainer(),
1947 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_IC_SCROLLBAR
),
1949 NWidget(NWID_HORIZONTAL
),
1950 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_IC_NOTIFY
),
1951 SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP
, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP
),
1952 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetFill(1, 0), SetResize(0, 0), EndContainer(),
1953 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_IND_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1954 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
, STR_INDUSTRY_CARGOES_SELECT_INDUSTRY_TOOLTIP
),
1955 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_CARGO_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1956 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_CARGO
, STR_INDUSTRY_CARGOES_SELECT_CARGO_TOOLTIP
),
1957 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1961 /** Window description for the industry cargoes window. */
1962 static WindowDesc
_industry_cargoes_desc(__FILE__
, __LINE__
,
1963 WDP_AUTO
, "industry_cargoes", 300, 210,
1964 WC_INDUSTRY_CARGOES
, WC_NONE
,
1966 std::begin(_nested_industry_cargoes_widgets
), std::end(_nested_industry_cargoes_widgets
)
1969 /** Available types of field. */
1970 enum CargoesFieldType
{
1971 CFT_EMPTY
, ///< Empty field.
1972 CFT_SMALL_EMPTY
, ///< Empty small field (for the header).
1973 CFT_INDUSTRY
, ///< Display industry.
1974 CFT_CARGO
, ///< Display cargo connections.
1975 CFT_CARGO_LABEL
, ///< Display cargo labels.
1976 CFT_HEADER
, ///< Header text.
1979 static const uint MAX_CARGOES
= 16; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField.
1981 /** Data about a single field in the #IndustryCargoesWindow panel. */
1982 struct CargoesField
{
1983 static int vert_inter_industry_space
;
1984 static int blob_distance
;
1986 static Dimension legend
;
1987 static Dimension cargo_border
;
1988 static Dimension cargo_line
;
1989 static Dimension cargo_space
;
1990 static Dimension cargo_stub
;
1992 static const int INDUSTRY_LINE_COLOUR
;
1993 static const int CARGO_LINE_COLOUR
;
1995 static int small_height
, normal_height
;
1996 static int cargo_field_width
;
1997 static int industry_width
;
1998 static uint max_cargoes
;
2000 using Cargoes
= uint16_t;
2001 static_assert(std::numeric_limits
<Cargoes
>::digits
>= MAX_CARGOES
);
2003 CargoesFieldType type
; ///< Type of field.
2006 IndustryType ind_type
; ///< Industry type (#NUM_INDUSTRYTYPES means 'houses').
2007 CargoID other_produced
[MAX_CARGOES
]; ///< Cargoes produced but not used in this figure.
2008 CargoID other_accepted
[MAX_CARGOES
]; ///< Cargoes accepted but not used in this figure.
2009 } industry
; ///< Industry data (for #CFT_INDUSTRY).
2011 CargoID vertical_cargoes
[MAX_CARGOES
]; ///< Cargoes running from top to bottom (cargo ID or #INVALID_CARGO).
2012 Cargoes supp_cargoes
; ///< Cargoes in \c vertical_cargoes entering from the left.
2013 Cargoes cust_cargoes
; ///< Cargoes in \c vertical_cargoes leaving to the right.
2014 uint8_t num_cargoes
; ///< Number of cargoes.
2015 uint8_t top_end
; ///< Stop at the top of the vertical cargoes.
2016 uint8_t bottom_end
; ///< Stop at the bottom of the vertical cargoes.
2017 } cargo
; ///< Cargo data (for #CFT_CARGO).
2019 CargoID cargoes
[MAX_CARGOES
]; ///< Cargoes to display (or #INVALID_CARGO).
2020 bool left_align
; ///< Align all cargo texts to the left (else align to the right).
2021 } cargo_label
; ///< Label data (for #CFT_CARGO_LABEL).
2022 StringID header
; ///< Header text (for #CFT_HEADER).
2023 } u
; // Data for each type.
2026 * Make one of the empty fields (#CFT_EMPTY or #CFT_SMALL_EMPTY).
2027 * @param type Type of empty field.
2029 void MakeEmpty(CargoesFieldType type
)
2035 * Make an industry type field.
2036 * @param ind_type Industry type (#NUM_INDUSTRYTYPES means 'houses').
2037 * @note #other_accepted and #other_produced should be filled later.
2039 void MakeIndustry(IndustryType ind_type
)
2041 this->type
= CFT_INDUSTRY
;
2042 this->u
.industry
.ind_type
= ind_type
;
2043 std::fill(std::begin(this->u
.industry
.other_accepted
), std::end(this->u
.industry
.other_accepted
), INVALID_CARGO
);
2044 std::fill(std::begin(this->u
.industry
.other_produced
), std::end(this->u
.industry
.other_produced
), INVALID_CARGO
);
2048 * Connect a cargo from an industry to the #CFT_CARGO column.
2049 * @param cargo Cargo to connect.
2050 * @param producer Cargo is produced (if \c false, cargo is assumed to be accepted).
2051 * @return Horizontal connection index, or \c -1 if not accepted at all.
2053 int ConnectCargo(CargoID cargo
, bool producer
)
2055 assert(this->type
== CFT_CARGO
);
2056 if (!IsValidCargoID(cargo
)) return -1;
2058 /* Find the vertical cargo column carrying the cargo. */
2060 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
2061 if (cargo
== this->u
.cargo
.vertical_cargoes
[i
]) {
2066 if (column
< 0) return -1;
2069 assert(!HasBit(this->u
.cargo
.supp_cargoes
, column
));
2070 SetBit(this->u
.cargo
.supp_cargoes
, column
);
2072 assert(!HasBit(this->u
.cargo
.cust_cargoes
, column
));
2073 SetBit(this->u
.cargo
.cust_cargoes
, column
);
2079 * Does this #CFT_CARGO field have a horizontal connection?
2080 * @return \c true if a horizontal connection exists, \c false otherwise.
2082 bool HasConnection()
2084 assert(this->type
== CFT_CARGO
);
2086 return this->u
.cargo
.supp_cargoes
!= 0 || this->u
.cargo
.cust_cargoes
!= 0;
2090 * Make a piece of cargo column.
2091 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
2092 * @param length Number of cargoes in \a cargoes.
2093 * @param count Number of cargoes to display (should be at least the number of valid cargoes, or \c -1 to let the method compute it).
2094 * @param top_end This is the first cargo field of this column.
2095 * @param bottom_end This is the last cargo field of this column.
2096 * @note #supp_cargoes and #cust_cargoes should be filled in later.
2098 void MakeCargo(const CargoID
*cargoes
, uint length
, int count
= -1, bool top_end
= false, bool bottom_end
= false)
2100 this->type
= CFT_CARGO
;
2101 auto insert
= std::begin(this->u
.cargo
.vertical_cargoes
);
2102 for (uint i
= 0; insert
!= std::end(this->u
.cargo
.vertical_cargoes
) && i
< length
; i
++) {
2103 if (IsValidCargoID(cargoes
[i
])) {
2104 *insert
= cargoes
[i
];
2108 this->u
.cargo
.num_cargoes
= (count
< 0) ? static_cast<uint8_t>(insert
- std::begin(this->u
.cargo
.vertical_cargoes
)) : count
;
2109 CargoIDComparator comparator
;
2110 std::sort(std::begin(this->u
.cargo
.vertical_cargoes
), insert
, comparator
);
2111 std::fill(insert
, std::end(this->u
.cargo
.vertical_cargoes
), INVALID_CARGO
);
2112 this->u
.cargo
.top_end
= top_end
;
2113 this->u
.cargo
.bottom_end
= bottom_end
;
2114 this->u
.cargo
.supp_cargoes
= 0;
2115 this->u
.cargo
.cust_cargoes
= 0;
2119 * Make a field displaying cargo type names.
2120 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
2121 * @param length Number of cargoes in \a cargoes.
2122 * @param left_align ALign texts to the left (else to the right).
2124 void MakeCargoLabel(const CargoID
*cargoes
, uint length
, bool left_align
)
2126 this->type
= CFT_CARGO_LABEL
;
2128 for (i
= 0; i
< MAX_CARGOES
&& i
< length
; i
++) this->u
.cargo_label
.cargoes
[i
] = cargoes
[i
];
2129 for (; i
< MAX_CARGOES
; i
++) this->u
.cargo_label
.cargoes
[i
] = INVALID_CARGO
;
2130 this->u
.cargo_label
.left_align
= left_align
;
2134 * Make a header above an industry column.
2135 * @param textid Text to display.
2137 void MakeHeader(StringID textid
)
2139 this->type
= CFT_HEADER
;
2140 this->u
.header
= textid
;
2144 * For a #CFT_CARGO, compute the left position of the left-most vertical cargo connection.
2145 * @param xpos Left position of the field.
2146 * @return Left position of the left-most vertical cargo column.
2148 int GetCargoBase(int xpos
) const
2150 assert(this->type
== CFT_CARGO
);
2151 int n
= this->u
.cargo
.num_cargoes
;
2153 return xpos
+ cargo_field_width
/ 2 - (CargoesField::cargo_line
.width
* n
+ CargoesField::cargo_space
.width
* (n
- 1)) / 2;
2158 * @param xpos Position of the left edge.
2159 * @param ypos Position of the top edge.
2161 void Draw(int xpos
, int ypos
) const
2163 switch (this->type
) {
2165 case CFT_SMALL_EMPTY
:
2169 ypos
+= (small_height
- GetCharacterHeight(FS_NORMAL
)) / 2;
2170 DrawString(xpos
, xpos
+ industry_width
, ypos
, this->u
.header
, TC_WHITE
, SA_HOR_CENTER
);
2173 case CFT_INDUSTRY
: {
2174 int ypos1
= ypos
+ vert_inter_industry_space
/ 2;
2175 int ypos2
= ypos
+ normal_height
- 1 - vert_inter_industry_space
/ 2;
2176 int xpos2
= xpos
+ industry_width
- 1;
2177 DrawRectOutline({xpos
, ypos1
, xpos2
, ypos2
}, INDUSTRY_LINE_COLOUR
);
2178 ypos
+= (normal_height
- GetCharacterHeight(FS_NORMAL
)) / 2;
2179 if (this->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
2180 const IndustrySpec
*indsp
= GetIndustrySpec(this->u
.industry
.ind_type
);
2181 DrawString(xpos
, xpos2
, ypos
, indsp
->name
, TC_WHITE
, SA_HOR_CENTER
);
2183 /* Draw the industry legend. */
2184 int blob_left
, blob_right
;
2185 if (_current_text_dir
== TD_RTL
) {
2186 blob_right
= xpos2
- blob_distance
;
2187 blob_left
= blob_right
- CargoesField::legend
.width
;
2189 blob_left
= xpos
+ blob_distance
;
2190 blob_right
= blob_left
+ CargoesField::legend
.width
;
2192 GfxFillRect(blob_left
, ypos2
- blob_distance
- CargoesField::legend
.height
, blob_right
, ypos2
- blob_distance
, PC_BLACK
); // Border
2193 GfxFillRect(blob_left
+ 1, ypos2
- blob_distance
- CargoesField::legend
.height
+ 1, blob_right
- 1, ypos2
- blob_distance
- 1, indsp
->map_colour
);
2195 DrawString(xpos
, xpos2
, ypos
, STR_INDUSTRY_CARGOES_HOUSES
, TC_FROMSTRING
, SA_HOR_CENTER
);
2198 /* Draw the other_produced/other_accepted cargoes. */
2199 const CargoID
*other_right
, *other_left
;
2200 if (_current_text_dir
== TD_RTL
) {
2201 other_right
= this->u
.industry
.other_accepted
;
2202 other_left
= this->u
.industry
.other_produced
;
2204 other_right
= this->u
.industry
.other_produced
;
2205 other_left
= this->u
.industry
.other_accepted
;
2207 ypos1
+= CargoesField::cargo_border
.height
+ (GetCharacterHeight(FS_NORMAL
) - CargoesField::cargo_line
.height
) / 2;
2208 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2209 if (IsValidCargoID(other_right
[i
])) {
2210 const CargoSpec
*csp
= CargoSpec::Get(other_right
[i
]);
2211 int xp
= xpos
+ industry_width
+ CargoesField::cargo_stub
.width
;
2212 DrawHorConnection(xpos
+ industry_width
, xp
- 1, ypos1
, csp
);
2213 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ CargoesField::cargo_line
.height
- 1, CARGO_LINE_COLOUR
);
2215 if (IsValidCargoID(other_left
[i
])) {
2216 const CargoSpec
*csp
= CargoSpec::Get(other_left
[i
]);
2217 int xp
= xpos
- CargoesField::cargo_stub
.width
;
2218 DrawHorConnection(xp
+ 1, xpos
- 1, ypos1
, csp
);
2219 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ CargoesField::cargo_line
.height
- 1, CARGO_LINE_COLOUR
);
2221 ypos1
+= GetCharacterHeight(FS_NORMAL
) + CargoesField::cargo_space
.height
;
2227 int cargo_base
= this->GetCargoBase(xpos
);
2228 int top
= ypos
+ (this->u
.cargo
.top_end
? vert_inter_industry_space
/ 2 + 1 : 0);
2229 int bot
= ypos
- (this->u
.cargo
.bottom_end
? vert_inter_industry_space
/ 2 + 1 : 0) + normal_height
- 1;
2230 int colpos
= cargo_base
;
2231 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
2232 if (this->u
.cargo
.top_end
) GfxDrawLine(colpos
, top
- 1, colpos
+ CargoesField::cargo_line
.width
- 1, top
- 1, CARGO_LINE_COLOUR
);
2233 if (this->u
.cargo
.bottom_end
) GfxDrawLine(colpos
, bot
+ 1, colpos
+ CargoesField::cargo_line
.width
- 1, bot
+ 1, CARGO_LINE_COLOUR
);
2234 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
2236 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[i
]);
2237 GfxFillRect(colpos
, top
, colpos
+ CargoesField::cargo_line
.width
- 2, bot
, csp
->legend_colour
, FILLRECT_OPAQUE
);
2238 colpos
+= CargoesField::cargo_line
.width
- 2;
2239 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
2240 colpos
+= 1 + CargoesField::cargo_space
.width
;
2243 Cargoes hor_left
, hor_right
;
2244 if (_current_text_dir
== TD_RTL
) {
2245 hor_left
= this->u
.cargo
.cust_cargoes
;
2246 hor_right
= this->u
.cargo
.supp_cargoes
;
2248 hor_left
= this->u
.cargo
.supp_cargoes
;
2249 hor_right
= this->u
.cargo
.cust_cargoes
;
2251 ypos
+= CargoesField::cargo_border
.height
+ vert_inter_industry_space
/ 2 + (GetCharacterHeight(FS_NORMAL
) - CargoesField::cargo_line
.height
) / 2;
2252 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
2253 if (HasBit(hor_left
, i
)) {
2256 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
2257 for (; col
> 0; col
--) {
2258 int lf
= cargo_base
+ col
* CargoesField::cargo_line
.width
+ (col
- 1) * CargoesField::cargo_space
.width
;
2259 DrawHorConnection(lf
, lf
+ CargoesField::cargo_space
.width
- dx
, ypos
, csp
);
2262 DrawHorConnection(xpos
, cargo_base
- dx
, ypos
, csp
);
2264 if (HasBit(hor_right
, i
)) {
2267 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
2268 for (; col
< this->u
.cargo
.num_cargoes
- 1; col
++) {
2269 int lf
= cargo_base
+ (col
+ 1) * CargoesField::cargo_line
.width
+ col
* CargoesField::cargo_space
.width
;
2270 DrawHorConnection(lf
+ dx
- 1, lf
+ CargoesField::cargo_space
.width
- 1, ypos
, csp
);
2273 DrawHorConnection(cargo_base
+ col
* CargoesField::cargo_space
.width
+ (col
+ 1) * CargoesField::cargo_line
.width
- 1 + dx
, xpos
+ CargoesField::cargo_field_width
- 1, ypos
, csp
);
2275 ypos
+= GetCharacterHeight(FS_NORMAL
) + CargoesField::cargo_space
.height
;
2280 case CFT_CARGO_LABEL
:
2281 ypos
+= CargoesField::cargo_border
.height
+ vert_inter_industry_space
/ 2;
2282 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
2283 if (IsValidCargoID(this->u
.cargo_label
.cargoes
[i
])) {
2284 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo_label
.cargoes
[i
]);
2285 DrawString(xpos
+ WidgetDimensions::scaled
.framerect
.left
, xpos
+ industry_width
- 1 - WidgetDimensions::scaled
.framerect
.right
, ypos
, csp
->name
, TC_WHITE
,
2286 (this->u
.cargo_label
.left_align
) ? SA_LEFT
: SA_RIGHT
);
2288 ypos
+= GetCharacterHeight(FS_NORMAL
) + CargoesField::cargo_space
.height
;
2298 * Decide which cargo was clicked at in a #CFT_CARGO field.
2299 * @param left Left industry neighbour if available (else \c nullptr should be supplied).
2300 * @param right Right industry neighbour if available (else \c nullptr should be supplied).
2301 * @param pt Click position in the cargo field.
2302 * @return Cargo clicked at, or #INVALID_CARGO if none.
2304 CargoID
CargoClickedAt(const CargoesField
*left
, const CargoesField
*right
, Point pt
) const
2306 assert(this->type
== CFT_CARGO
);
2308 /* Vertical matching. */
2309 int cpos
= this->GetCargoBase(0);
2311 for (col
= 0; col
< this->u
.cargo
.num_cargoes
; col
++) {
2312 if (pt
.x
< cpos
) break;
2313 if (pt
.x
< cpos
+ (int)CargoesField::cargo_line
.width
) return this->u
.cargo
.vertical_cargoes
[col
];
2314 cpos
+= CargoesField::cargo_line
.width
+ CargoesField::cargo_space
.width
;
2316 /* col = 0 -> left of first col, 1 -> left of 2nd col, ... this->u.cargo.num_cargoes right of last-col. */
2318 int vpos
= vert_inter_industry_space
/ 2 + CargoesField::cargo_border
.width
;
2320 for (row
= 0; row
< MAX_CARGOES
; row
++) {
2321 if (pt
.y
< vpos
) return INVALID_CARGO
;
2322 if (pt
.y
< vpos
+ GetCharacterHeight(FS_NORMAL
)) break;
2323 vpos
+= GetCharacterHeight(FS_NORMAL
) + CargoesField::cargo_space
.width
;
2325 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
2327 /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
2329 if (HasBit(this->u
.cargo
.supp_cargoes
, row
)) return this->u
.cargo
.vertical_cargoes
[row
];
2330 if (left
!= nullptr) {
2331 if (left
->type
== CFT_INDUSTRY
) return left
->u
.industry
.other_produced
[row
];
2332 if (left
->type
== CFT_CARGO_LABEL
&& !left
->u
.cargo_label
.left_align
) return left
->u
.cargo_label
.cargoes
[row
];
2334 return INVALID_CARGO
;
2336 if (col
== this->u
.cargo
.num_cargoes
) {
2337 if (HasBit(this->u
.cargo
.cust_cargoes
, row
)) return this->u
.cargo
.vertical_cargoes
[row
];
2338 if (right
!= nullptr) {
2339 if (right
->type
== CFT_INDUSTRY
) return right
->u
.industry
.other_accepted
[row
];
2340 if (right
->type
== CFT_CARGO_LABEL
&& right
->u
.cargo_label
.left_align
) return right
->u
.cargo_label
.cargoes
[row
];
2342 return INVALID_CARGO
;
2345 /* Clicked somewhere in-between vertical cargo connection.
2346 * Since the horizontal connection is made in the same order as the vertical list, the above condition
2347 * ensures we are left-below the main diagonal, thus at the supplying side.
2349 if (HasBit(this->u
.cargo
.supp_cargoes
, row
)) return this->u
.cargo
.vertical_cargoes
[row
];
2350 return INVALID_CARGO
;
2352 /* Clicked at a customer connection. */
2353 if (HasBit(this->u
.cargo
.cust_cargoes
, row
)) return this->u
.cargo
.vertical_cargoes
[row
];
2354 return INVALID_CARGO
;
2358 * Decide what cargo the user clicked in the cargo label field.
2359 * @param pt Click position in the cargo label field.
2360 * @return Cargo clicked at, or #INVALID_CARGO if none.
2362 CargoID
CargoLabelClickedAt(Point pt
) const
2364 assert(this->type
== CFT_CARGO_LABEL
);
2366 int vpos
= vert_inter_industry_space
/ 2 + CargoesField::cargo_border
.height
;
2368 for (row
= 0; row
< MAX_CARGOES
; row
++) {
2369 if (pt
.y
< vpos
) return INVALID_CARGO
;
2370 if (pt
.y
< vpos
+ GetCharacterHeight(FS_NORMAL
)) break;
2371 vpos
+= GetCharacterHeight(FS_NORMAL
) + CargoesField::cargo_space
.height
;
2373 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
2374 return this->u
.cargo_label
.cargoes
[row
];
2379 * Draw a horizontal cargo connection.
2380 * @param left Left-most coordinate to draw.
2381 * @param right Right-most coordinate to draw.
2382 * @param top Top coordinate of the cargo connection.
2383 * @param csp Cargo to draw.
2385 static void DrawHorConnection(int left
, int right
, int top
, const CargoSpec
*csp
)
2387 GfxDrawLine(left
, top
, right
, top
, CARGO_LINE_COLOUR
);
2388 GfxFillRect(left
, top
+ 1, right
, top
+ CargoesField::cargo_line
.height
- 2, csp
->legend_colour
, FILLRECT_OPAQUE
);
2389 GfxDrawLine(left
, top
+ CargoesField::cargo_line
.height
- 1, right
, top
+ CargoesField::cargo_line
.height
- 1, CARGO_LINE_COLOUR
);
2393 static_assert(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, produced_cargo
));
2394 static_assert(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, accepts_cargo
));
2396 Dimension
CargoesField::legend
; ///< Dimension of the legend blob.
2397 Dimension
CargoesField::cargo_border
; ///< Dimensions of border between cargo lines and industry boxes.
2398 Dimension
CargoesField::cargo_line
; ///< Dimensions of cargo lines.
2399 Dimension
CargoesField::cargo_space
; ///< Dimensions of space between cargo lines.
2400 Dimension
CargoesField::cargo_stub
; ///< Dimensions of cargo stub (unconnected cargo line.)
2402 int CargoesField::small_height
; ///< Height of the header row.
2403 int CargoesField::normal_height
; ///< Height of the non-header rows.
2404 int CargoesField::industry_width
; ///< Width of an industry field.
2405 int CargoesField::cargo_field_width
; ///< Width of a cargo field.
2406 uint
CargoesField::max_cargoes
; ///< Largest number of cargoes actually on any industry.
2407 int CargoesField::vert_inter_industry_space
; ///< Amount of space between two industries in a column.
2409 int CargoesField::blob_distance
; ///< Distance of the industry legend colour from the edge of the industry box.
2411 const int CargoesField::INDUSTRY_LINE_COLOUR
= PC_YELLOW
; ///< Line colour of the industry type box.
2412 const int CargoesField::CARGO_LINE_COLOUR
= PC_YELLOW
; ///< Line colour around the cargo.
2414 /** A single row of #CargoesField. */
2416 CargoesField columns
[5]; ///< One row of fields.
2419 * Connect industry production cargoes to the cargo column after it.
2420 * @param column Column of the industry.
2422 void ConnectIndustryProduced(int column
)
2424 CargoesField
*ind_fld
= this->columns
+ column
;
2425 CargoesField
*cargo_fld
= this->columns
+ column
+ 1;
2426 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
2428 std::fill(std::begin(ind_fld
->u
.industry
.other_produced
), std::end(ind_fld
->u
.industry
.other_produced
), INVALID_CARGO
);
2430 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
2431 CargoID others
[MAX_CARGOES
]; // Produced cargoes not carried in the cargo column.
2432 int other_count
= 0;
2434 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
2435 assert(CargoesField::max_cargoes
<= lengthof(indsp
->produced_cargo
));
2436 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2437 int col
= cargo_fld
->ConnectCargo(indsp
->produced_cargo
[i
], true);
2438 if (col
< 0) others
[other_count
++] = indsp
->produced_cargo
[i
];
2441 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
2442 for (uint i
= 0; i
< CargoesField::max_cargoes
&& other_count
> 0; i
++) {
2443 if (HasBit(cargo_fld
->u
.cargo
.supp_cargoes
, i
)) ind_fld
->u
.industry
.other_produced
[i
] = others
[--other_count
];
2446 /* Houses only display cargo that towns produce. */
2447 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2448 CargoID cid
= cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
2449 TownProductionEffect tpe
= CargoSpec::Get(cid
)->town_production_effect
;
2450 if (tpe
== TPE_PASSENGERS
|| tpe
== TPE_MAIL
) cargo_fld
->ConnectCargo(cid
, true);
2456 * Construct a #CFT_CARGO_LABEL field.
2457 * @param column Column to create the new field.
2458 * @param accepting Display accepted cargo (if \c false, display produced cargo).
2460 void MakeCargoLabel(int column
, bool accepting
)
2462 CargoID cargoes
[MAX_CARGOES
];
2463 std::fill(std::begin(cargoes
), std::end(cargoes
), INVALID_CARGO
);
2465 CargoesField
*label_fld
= this->columns
+ column
;
2466 CargoesField
*cargo_fld
= this->columns
+ (accepting
? column
- 1 : column
+ 1);
2468 assert(cargo_fld
->type
== CFT_CARGO
&& label_fld
->type
== CFT_EMPTY
);
2469 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2470 int col
= cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], !accepting
);
2471 if (col
>= 0) cargoes
[col
] = cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
2473 label_fld
->MakeCargoLabel(cargoes
, lengthof(cargoes
), accepting
);
2478 * Connect industry accepted cargoes to the cargo column before it.
2479 * @param column Column of the industry.
2481 void ConnectIndustryAccepted(int column
)
2483 CargoesField
*ind_fld
= this->columns
+ column
;
2484 CargoesField
*cargo_fld
= this->columns
+ column
- 1;
2485 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
2487 std::fill(std::begin(ind_fld
->u
.industry
.other_accepted
), std::end(ind_fld
->u
.industry
.other_accepted
), INVALID_CARGO
);
2489 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
2490 CargoID others
[MAX_CARGOES
]; // Accepted cargoes not carried in the cargo column.
2491 int other_count
= 0;
2493 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
2494 assert(CargoesField::max_cargoes
<= lengthof(indsp
->accepts_cargo
));
2495 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2496 int col
= cargo_fld
->ConnectCargo(indsp
->accepts_cargo
[i
], false);
2497 if (col
< 0) others
[other_count
++] = indsp
->accepts_cargo
[i
];
2500 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
2501 for (uint i
= 0; i
< CargoesField::max_cargoes
&& other_count
> 0; i
++) {
2502 if (!HasBit(cargo_fld
->u
.cargo
.cust_cargoes
, i
)) ind_fld
->u
.industry
.other_accepted
[i
] = others
[--other_count
];
2505 /* Houses only display what is demanded. */
2506 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2507 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2508 HouseSpec
*hs
= HouseSpec::Get(h
);
2509 if (!hs
->enabled
) continue;
2511 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2512 if (hs
->cargo_acceptance
[j
] > 0 && cargo_fld
->u
.cargo
.vertical_cargoes
[i
] == hs
->accepts_cargo
[j
]) {
2513 cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], false);
2526 * Window displaying the cargo connections around an industry (or cargo).
2528 * The main display is constructed from 'fields', rectangles that contain an industry, piece of the cargo connection, cargo labels, or headers.
2529 * For a nice display, the following should be kept in mind:
2530 * - A #CFT_HEADER is always at the top of an column of #CFT_INDUSTRY fields.
2531 * - A #CFT_CARGO_LABEL field is also always put in a column of #CFT_INDUSTRY fields.
2532 * - The top row contains #CFT_HEADER and #CFT_SMALL_EMPTY fields.
2533 * - Cargo connections have a column of their own (#CFT_CARGO fields).
2534 * - Cargo accepted or produced by an industry, but not carried in a cargo connection, is drawn in the space of a cargo column attached to the industry.
2535 * The information however is part of the industry.
2537 * This results in the following invariants:
2538 * - Width of a #CFT_INDUSTRY column is large enough to hold all industry type labels, all cargo labels, and all header texts.
2539 * - Height of a #CFT_INDUSTRY is large enough to hold a header line, or a industry type line, \c N cargo labels
2540 * (where \c N is the maximum number of cargoes connected between industries), \c N connections of cargo types, and space
2541 * between two industry types (1/2 above it, and 1/2 underneath it).
2542 * - Width of a cargo field (#CFT_CARGO) is large enough to hold \c N vertical columns (one for each type of cargo).
2543 * Also, space is needed between an industry and the leftmost/rightmost column to draw the non-carried cargoes.
2544 * - Height of a #CFT_CARGO field is equally high as the height of the #CFT_INDUSTRY.
2545 * - A field at the top (#CFT_HEADER or #CFT_SMALL_EMPTY) match the width of the fields below them (#CFT_INDUSTRY respectively
2546 * #CFT_CARGO), the height should be sufficient to display the header text.
2548 * When displaying the cargoes around an industry type, five columns are needed (supplying industries, accepted cargoes, the industry,
2549 * produced cargoes, customer industries). Displaying the industries around a cargo needs three columns (supplying industries, the cargo,
2550 * customer industries). The remaining two columns are set to #CFT_EMPTY with a width equal to the average of a cargo and an industry column.
2552 struct IndustryCargoesWindow
: public Window
{
2553 typedef std::vector
<CargoesRow
> Fields
;
2555 Fields fields
; ///< Fields to display in the #WID_IC_PANEL.
2556 uint ind_cargo
; ///< If less than #NUM_INDUSTRYTYPES, an industry type, else a cargo id + NUM_INDUSTRYTYPES.
2557 Dimension cargo_textsize
; ///< Size to hold any cargo text, as well as STR_INDUSTRY_CARGOES_SELECT_CARGO.
2558 Dimension ind_textsize
; ///< Size to hold any industry type text, as well as STR_INDUSTRY_CARGOES_SELECT_INDUSTRY.
2561 IndustryCargoesWindow(int id
) : Window(&_industry_cargoes_desc
)
2564 this->CreateNestedTree();
2565 this->vscroll
= this->GetScrollbar(WID_IC_SCROLLBAR
);
2566 this->FinishInitNested(0);
2567 this->OnInvalidateData(id
);
2570 void OnInit() override
2572 /* Initialize static CargoesField size variables. */
2573 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS
);
2574 d
= maxdim(d
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS
));
2575 d
.width
+= WidgetDimensions::scaled
.frametext
.Horizontal();
2576 d
.height
+= WidgetDimensions::scaled
.frametext
.Vertical();
2577 CargoesField::small_height
= d
.height
;
2579 /* Size of the legend blob -- slightly larger than the smallmap legend blob. */
2580 CargoesField::legend
.height
= GetCharacterHeight(FS_SMALL
);
2581 CargoesField::legend
.width
= CargoesField::legend
.height
* 9 / 6;
2583 /* Size of cargo lines. */
2584 CargoesField::cargo_line
.width
= ScaleGUITrad(6);
2585 CargoesField::cargo_line
.height
= CargoesField::cargo_line
.width
;
2587 /* Size of border between cargo lines and industry boxes. */
2588 CargoesField::cargo_border
.width
= CargoesField::cargo_line
.width
* 3 / 2;
2589 CargoesField::cargo_border
.height
= CargoesField::cargo_line
.width
/ 2;
2591 /* Size of space between cargo lines. */
2592 CargoesField::cargo_space
.width
= CargoesField::cargo_line
.width
/ 2;
2593 CargoesField::cargo_space
.height
= CargoesField::cargo_line
.height
/ 2;
2595 /* Size of cargo stub (unconnected cargo line.) */
2596 CargoesField::cargo_stub
.width
= CargoesField::cargo_line
.width
/ 2;
2597 CargoesField::cargo_stub
.height
= CargoesField::cargo_line
.height
; /* Unused */
2599 CargoesField::vert_inter_industry_space
= WidgetDimensions::scaled
.vsep_wide
;
2600 CargoesField::blob_distance
= WidgetDimensions::scaled
.hsep_normal
;
2602 /* Decide about the size of the box holding the text of an industry type. */
2603 this->ind_textsize
.width
= 0;
2604 this->ind_textsize
.height
= 0;
2605 CargoesField::max_cargoes
= 0;
2606 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2607 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2608 if (!indsp
->enabled
) continue;
2609 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(indsp
->name
));
2610 CargoesField::max_cargoes
= std::max
<uint
>(CargoesField::max_cargoes
, std::count_if(indsp
->accepts_cargo
, endof(indsp
->accepts_cargo
), IsValidCargoID
));
2611 CargoesField::max_cargoes
= std::max
<uint
>(CargoesField::max_cargoes
, std::count_if(indsp
->produced_cargo
, endof(indsp
->produced_cargo
), IsValidCargoID
));
2613 d
.width
= std::max(d
.width
, this->ind_textsize
.width
);
2614 d
.height
= this->ind_textsize
.height
;
2615 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
));
2617 /* Compute max size of the cargo texts. */
2618 this->cargo_textsize
.width
= 0;
2619 this->cargo_textsize
.height
= 0;
2620 for (const CargoSpec
*csp
: CargoSpec::Iterate()) {
2621 if (!csp
->IsValid()) continue;
2622 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(csp
->name
));
2624 d
= maxdim(d
, this->cargo_textsize
); // Box must also be wide enough to hold any cargo label.
2625 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_CARGO
));
2627 d
.width
+= WidgetDimensions::scaled
.frametext
.Horizontal();
2628 /* Ensure the height is enough for the industry type text, for the horizontal connections, and for the cargo labels. */
2629 uint min_ind_height
= CargoesField::cargo_border
.height
* 2 + CargoesField::max_cargoes
* GetCharacterHeight(FS_NORMAL
) + (CargoesField::max_cargoes
- 1) * CargoesField::cargo_space
.height
;
2630 d
.height
= std::max(d
.height
+ WidgetDimensions::scaled
.frametext
.Vertical(), min_ind_height
);
2632 CargoesField::industry_width
= d
.width
;
2633 CargoesField::normal_height
= d
.height
+ CargoesField::vert_inter_industry_space
;
2635 /* Width of a #CFT_CARGO field. */
2636 CargoesField::cargo_field_width
= CargoesField::cargo_border
.width
* 2 + CargoesField::cargo_line
.width
* CargoesField::max_cargoes
+ CargoesField::cargo_space
.width
* (CargoesField::max_cargoes
- 1);
2639 void UpdateWidgetSize(WidgetID widget
, Dimension
*size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
*fill
, [[maybe_unused
]] Dimension
*resize
) override
2643 resize
->height
= CargoesField::normal_height
;
2644 size
->width
= CargoesField::industry_width
* 3 + CargoesField::cargo_field_width
* 2 + WidgetDimensions::scaled
.frametext
.Horizontal();
2645 size
->height
= CargoesField::small_height
+ 2 * resize
->height
+ WidgetDimensions::scaled
.frametext
.Vertical();
2648 case WID_IC_IND_DROPDOWN
:
2649 size
->width
= std::max(size
->width
, this->ind_textsize
.width
+ padding
.width
);
2652 case WID_IC_CARGO_DROPDOWN
:
2653 size
->width
= std::max(size
->width
, this->cargo_textsize
.width
+ padding
.width
);
2659 CargoesFieldType type
; ///< Type of field.
2660 void SetStringParameters (WidgetID widget
) const override
2662 if (widget
!= WID_IC_CAPTION
) return;
2664 if (this->ind_cargo
< NUM_INDUSTRYTYPES
) {
2665 const IndustrySpec
*indsp
= GetIndustrySpec(this->ind_cargo
);
2666 SetDParam(0, indsp
->name
);
2668 const CargoSpec
*csp
= CargoSpec::Get(this->ind_cargo
- NUM_INDUSTRYTYPES
);
2669 SetDParam(0, csp
->name
);
2674 * Do the two sets of cargoes have a valid cargo in common?
2675 * @param cargoes1 Base address of the first cargo array.
2676 * @param length1 Number of cargoes in the first cargo array.
2677 * @param cargoes2 Base address of the second cargo array.
2678 * @param length2 Number of cargoes in the second cargo array.
2679 * @return Arrays have at least one valid cargo in common.
2681 static bool HasCommonValidCargo(const CargoID
*cargoes1
, uint length1
, const CargoID
*cargoes2
, uint length2
)
2683 while (length1
> 0) {
2684 if (IsValidCargoID(*cargoes1
)) {
2685 for (uint i
= 0; i
< length2
; i
++) if (*cargoes1
== cargoes2
[i
]) return true;
2694 * Can houses be used to supply one of the cargoes?
2695 * @param cargoes Base address of the cargo array.
2696 * @param length Number of cargoes in the array.
2697 * @return Houses can supply at least one of the cargoes.
2699 static bool HousesCanSupply(const CargoID
*cargoes
, uint length
)
2701 for (uint i
= 0; i
< length
; i
++) {
2702 CargoID cid
= cargoes
[i
];
2703 if (!IsValidCargoID(cid
)) continue;
2704 TownProductionEffect tpe
= CargoSpec::Get(cid
)->town_production_effect
;
2705 if (tpe
== TPE_PASSENGERS
|| tpe
== TPE_MAIL
) return true;
2711 * Can houses be used as customers of the produced cargoes?
2712 * @param cargoes Base address of the cargo array.
2713 * @param length Number of cargoes in the array.
2714 * @return Houses can accept at least one of the cargoes.
2716 static bool HousesCanAccept(const CargoID
*cargoes
, uint length
)
2718 HouseZones climate_mask
;
2719 switch (_settings_game
.game_creation
.landscape
) {
2720 case LT_TEMPERATE
: climate_mask
= HZ_TEMP
; break;
2721 case LT_ARCTIC
: climate_mask
= HZ_SUBARTC_ABOVE
| HZ_SUBARTC_BELOW
; break;
2722 case LT_TROPIC
: climate_mask
= HZ_SUBTROPIC
; break;
2723 case LT_TOYLAND
: climate_mask
= HZ_TOYLND
; break;
2724 default: NOT_REACHED();
2726 for (uint i
= 0; i
< length
; i
++) {
2727 if (!IsValidCargoID(cargoes
[i
])) continue;
2729 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2730 HouseSpec
*hs
= HouseSpec::Get(h
);
2731 if (!hs
->enabled
|| !(hs
->building_availability
& climate_mask
)) continue;
2733 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2734 if (hs
->cargo_acceptance
[j
] > 0 && cargoes
[i
] == hs
->accepts_cargo
[j
]) return true;
2742 * Count how many industries have accepted cargoes in common with one of the supplied set.
2743 * @param cargoes Cargoes to search.
2744 * @param length Number of cargoes in \a cargoes.
2745 * @return Number of industries that have an accepted cargo in common with the supplied set.
2747 static int CountMatchingAcceptingIndustries(const CargoID
*cargoes
, uint length
)
2750 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2751 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2752 if (!indsp
->enabled
) continue;
2754 if (HasCommonValidCargo(cargoes
, length
, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) count
++;
2760 * Count how many industries have produced cargoes in common with one of the supplied set.
2761 * @param cargoes Cargoes to search.
2762 * @param length Number of cargoes in \a cargoes.
2763 * @return Number of industries that have a produced cargo in common with the supplied set.
2765 static int CountMatchingProducingIndustries(const CargoID
*cargoes
, uint length
)
2768 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2769 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2770 if (!indsp
->enabled
) continue;
2772 if (HasCommonValidCargo(cargoes
, length
, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) count
++;
2778 * Shorten the cargo column to just the part between industries.
2779 * @param column Column number of the cargo column.
2780 * @param top Current top row.
2781 * @param bottom Current bottom row.
2783 void ShortenCargoColumn(int column
, int top
, int bottom
)
2785 while (top
< bottom
&& !this->fields
[top
].columns
[column
].HasConnection()) {
2786 this->fields
[top
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2789 this->fields
[top
].columns
[column
].u
.cargo
.top_end
= true;
2791 while (bottom
> top
&& !this->fields
[bottom
].columns
[column
].HasConnection()) {
2792 this->fields
[bottom
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2795 this->fields
[bottom
].columns
[column
].u
.cargo
.bottom_end
= true;
2799 * Place an industry in the fields.
2800 * @param row Row of the new industry.
2801 * @param col Column of the new industry.
2802 * @param it Industry to place.
2804 void PlaceIndustry(int row
, int col
, IndustryType it
)
2806 assert(this->fields
[row
].columns
[col
].type
== CFT_EMPTY
);
2807 this->fields
[row
].columns
[col
].MakeIndustry(it
);
2809 this->fields
[row
].ConnectIndustryProduced(col
);
2811 this->fields
[row
].ConnectIndustryAccepted(col
);
2816 * Notify smallmap that new displayed industries have been selected (in #_displayed_industries).
2818 void NotifySmallmap()
2820 if (!this->IsWidgetLowered(WID_IC_NOTIFY
)) return;
2822 /* Only notify the smallmap window if it exists. In particular, do not
2823 * bring it to the front to prevent messing up any nice layout of the user. */
2824 InvalidateWindowClassesData(WC_SMALLMAP
, 0);
2828 * Compute what and where to display for industry type \a it.
2829 * @param displayed_it Industry type to display.
2831 void ComputeIndustryDisplay(IndustryType displayed_it
)
2833 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
;
2834 this->ind_cargo
= displayed_it
;
2835 _displayed_industries
.reset();
2836 _displayed_industries
.set(displayed_it
);
2838 this->fields
.clear();
2839 CargoesRow
&first_row
= this->fields
.emplace_back();
2840 first_row
.columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2841 first_row
.columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2842 first_row
.columns
[2].MakeEmpty(CFT_SMALL_EMPTY
);
2843 first_row
.columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2844 first_row
.columns
[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2846 const IndustrySpec
*central_sp
= GetIndustrySpec(displayed_it
);
2847 bool houses_supply
= HousesCanSupply(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2848 bool houses_accept
= HousesCanAccept(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2849 /* Make a field consisting of two cargo columns. */
2850 int num_supp
= CountMatchingProducingIndustries(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
)) + houses_supply
;
2851 int num_cust
= CountMatchingAcceptingIndustries(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
)) + houses_accept
;
2852 int num_indrows
= std::max(3, std::max(num_supp
, num_cust
)); // One is needed for the 'it' industry, and 2 for the cargo labels.
2853 for (int i
= 0; i
< num_indrows
; i
++) {
2854 CargoesRow
&row
= this->fields
.emplace_back();
2855 row
.columns
[0].MakeEmpty(CFT_EMPTY
);
2856 row
.columns
[1].MakeCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2857 row
.columns
[2].MakeEmpty(CFT_EMPTY
);
2858 row
.columns
[3].MakeCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2859 row
.columns
[4].MakeEmpty(CFT_EMPTY
);
2861 /* Add central industry. */
2862 int central_row
= 1 + num_indrows
/ 2;
2863 this->fields
[central_row
].columns
[2].MakeIndustry(displayed_it
);
2864 this->fields
[central_row
].ConnectIndustryProduced(2);
2865 this->fields
[central_row
].ConnectIndustryAccepted(2);
2867 /* Add cargo labels. */
2868 this->fields
[central_row
- 1].MakeCargoLabel(2, true);
2869 this->fields
[central_row
+ 1].MakeCargoLabel(2, false);
2871 /* Add suppliers and customers of the 'it' industry. */
2874 for (IndustryType it
: _sorted_industry_types
) {
2875 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2876 if (!indsp
->enabled
) continue;
2878 if (HasCommonValidCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
), indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2879 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2880 _displayed_industries
.set(it
);
2883 if (HasCommonValidCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
), indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2884 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, it
);
2885 _displayed_industries
.set(it
);
2889 if (houses_supply
) {
2890 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2893 if (houses_accept
) {
2894 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, NUM_INDUSTRYTYPES
);
2898 this->ShortenCargoColumn(1, 1, num_indrows
);
2899 this->ShortenCargoColumn(3, 1, num_indrows
);
2900 this->vscroll
->SetCount(num_indrows
);
2902 this->NotifySmallmap();
2906 * Compute what and where to display for cargo id \a cid.
2907 * @param cid Cargo id to display.
2909 void ComputeCargoDisplay(CargoID cid
)
2911 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_CARGO_CAPTION
;
2912 this->ind_cargo
= cid
+ NUM_INDUSTRYTYPES
;
2913 _displayed_industries
.reset();
2915 this->fields
.clear();
2916 CargoesRow
&first_row
= this->fields
.emplace_back();
2917 first_row
.columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2918 first_row
.columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2919 first_row
.columns
[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2920 first_row
.columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2921 first_row
.columns
[4].MakeEmpty(CFT_SMALL_EMPTY
);
2923 bool houses_supply
= HousesCanSupply(&cid
, 1);
2924 bool houses_accept
= HousesCanAccept(&cid
, 1);
2925 int num_supp
= CountMatchingProducingIndustries(&cid
, 1) + houses_supply
+ 1; // Ensure room for the cargo label.
2926 int num_cust
= CountMatchingAcceptingIndustries(&cid
, 1) + houses_accept
;
2927 int num_indrows
= std::max(num_supp
, num_cust
);
2928 for (int i
= 0; i
< num_indrows
; i
++) {
2929 CargoesRow
&row
= this->fields
.emplace_back();
2930 row
.columns
[0].MakeEmpty(CFT_EMPTY
);
2931 row
.columns
[1].MakeCargo(&cid
, 1);
2932 row
.columns
[2].MakeEmpty(CFT_EMPTY
);
2933 row
.columns
[3].MakeEmpty(CFT_EMPTY
);
2934 row
.columns
[4].MakeEmpty(CFT_EMPTY
);
2937 this->fields
[num_indrows
].MakeCargoLabel(0, false); // Add cargo labels at the left bottom.
2939 /* Add suppliers and customers of the cargo. */
2942 for (IndustryType it
: _sorted_industry_types
) {
2943 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2944 if (!indsp
->enabled
) continue;
2946 if (HasCommonValidCargo(&cid
, 1, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2947 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2948 _displayed_industries
.set(it
);
2951 if (HasCommonValidCargo(&cid
, 1, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2952 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, it
);
2953 _displayed_industries
.set(it
);
2957 if (houses_supply
) {
2958 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2961 if (houses_accept
) {
2962 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, NUM_INDUSTRYTYPES
);
2966 this->ShortenCargoColumn(1, 1, num_indrows
);
2967 this->vscroll
->SetCount(num_indrows
);
2969 this->NotifySmallmap();
2973 * Some data on this window has become invalid.
2974 * @param data Information about the changed data.
2975 * - data = 0 .. NUM_INDUSTRYTYPES - 1: Display the chain around the given industry.
2976 * - data = NUM_INDUSTRYTYPES: Stop sending updates to the smallmap window.
2977 * @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.
2979 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
2981 if (!gui_scope
) return;
2982 if (data
== NUM_INDUSTRYTYPES
) {
2983 this->RaiseWidgetWhenLowered(WID_IC_NOTIFY
);
2987 assert(data
>= 0 && data
< NUM_INDUSTRYTYPES
);
2988 this->ComputeIndustryDisplay(data
);
2991 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
2993 if (widget
!= WID_IC_PANEL
) return;
2995 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.bevel
);
2996 DrawPixelInfo tmp_dpi
;
2997 if (!FillDrawPixelInfo(&tmp_dpi
, ir
)) return;
2998 AutoRestoreBackup
dpi_backup(_cur_dpi
, &tmp_dpi
);
3000 int left_pos
= WidgetDimensions::scaled
.frametext
.left
- WidgetDimensions::scaled
.bevel
.left
;
3001 if (this->ind_cargo
>= NUM_INDUSTRYTYPES
) left_pos
+= (CargoesField::industry_width
+ CargoesField::cargo_field_width
) / 2;
3002 int last_column
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
3004 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
3005 int vpos
= WidgetDimensions::scaled
.frametext
.top
- WidgetDimensions::scaled
.bevel
.top
- this->vscroll
->GetPosition() * nwp
->resize_y
;
3006 int row_height
= CargoesField::small_height
;
3007 for (const auto &field
: this->fields
) {
3008 if (vpos
+ row_height
>= 0) {
3009 int xpos
= left_pos
;
3011 if (_current_text_dir
== TD_RTL
) {
3018 while (col
>= 0 && col
<= last_column
) {
3019 field
.columns
[col
].Draw(xpos
, vpos
);
3020 xpos
+= (col
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
;
3025 if (vpos
>= height
) break;
3026 row_height
= CargoesField::normal_height
;
3031 * Calculate in which field was clicked, and within the field, at what position.
3032 * @param pt Clicked position in the #WID_IC_PANEL widget.
3033 * @param fieldxy If \c true is returned, field x/y coordinate of \a pt.
3034 * @param xy If \c true is returned, x/y coordinate with in the field.
3035 * @return Clicked at a valid position.
3037 bool CalculatePositionInWidget(Point pt
, Point
*fieldxy
, Point
*xy
)
3039 const NWidgetBase
*nw
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
3043 int vpos
= WidgetDimensions::scaled
.framerect
.top
+ CargoesField::small_height
- this->vscroll
->GetPosition() * nw
->resize_y
;
3044 if (pt
.y
< vpos
) return false;
3046 int row
= (pt
.y
- vpos
) / CargoesField::normal_height
; // row is relative to row 1.
3047 if (row
+ 1 >= (int)this->fields
.size()) return false;
3048 vpos
= pt
.y
- vpos
- row
* CargoesField::normal_height
; // Position in the row + 1 field
3049 row
++; // rebase row to match index of this->fields.
3051 int xpos
= 2 * WidgetDimensions::scaled
.framerect
.left
+ ((this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 0 : (CargoesField::industry_width
+ CargoesField::cargo_field_width
) / 2);
3052 if (pt
.x
< xpos
) return false;
3054 for (column
= 0; column
<= 5; column
++) {
3055 int width
= (column
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
;
3056 if (pt
.x
< xpos
+ width
) break;
3059 int num_columns
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
3060 if (column
> num_columns
) return false;
3063 /* Return both positions, compensating for RTL languages (which works due to the equal symmetry in both displays). */
3066 if (_current_text_dir
== TD_RTL
) {
3067 fieldxy
->x
= num_columns
- column
;
3068 xy
->x
= ((column
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
) - xpos
;
3070 fieldxy
->x
= column
;
3076 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
3079 case WID_IC_PANEL
: {
3081 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return;
3083 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
3084 switch (fld
->type
) {
3086 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) this->ComputeIndustryDisplay(fld
->u
.industry
.ind_type
);
3090 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : nullptr;
3091 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : nullptr;
3092 CargoID cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
3093 if (IsValidCargoID(cid
)) this->ComputeCargoDisplay(cid
);
3097 case CFT_CARGO_LABEL
: {
3098 CargoID cid
= fld
->CargoLabelClickedAt(xy
);
3099 if (IsValidCargoID(cid
)) this->ComputeCargoDisplay(cid
);
3110 this->ToggleWidgetLoweredState(WID_IC_NOTIFY
);
3111 this->SetWidgetDirty(WID_IC_NOTIFY
);
3112 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
3114 if (this->IsWidgetLowered(WID_IC_NOTIFY
)) {
3115 if (FindWindowByClass(WC_SMALLMAP
) == nullptr) ShowSmallMap();
3116 this->NotifySmallmap();
3120 case WID_IC_CARGO_DROPDOWN
: {
3122 Dimension d
= GetLargestCargoIconSize();
3123 for (const CargoSpec
*cs
: _sorted_standard_cargo_specs
) {
3124 lst
.push_back(std::make_unique
<DropDownListIconItem
>(d
, cs
->GetCargoIcon(), PAL_NONE
, cs
->name
, cs
->Index(), false));
3127 int selected
= (this->ind_cargo
>= NUM_INDUSTRYTYPES
) ? (int)(this->ind_cargo
- NUM_INDUSTRYTYPES
) : -1;
3128 ShowDropDownList(this, std::move(lst
), selected
, WID_IC_CARGO_DROPDOWN
);
3133 case WID_IC_IND_DROPDOWN
: {
3135 for (IndustryType ind
: _sorted_industry_types
) {
3136 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
3137 if (!indsp
->enabled
) continue;
3138 lst
.push_back(std::make_unique
<DropDownListStringItem
>(indsp
->name
, ind
, false));
3141 int selected
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? (int)this->ind_cargo
: -1;
3142 ShowDropDownList(this, std::move(lst
), selected
, WID_IC_IND_DROPDOWN
);
3149 void OnDropdownSelect(WidgetID widget
, int index
) override
3151 if (index
< 0) return;
3154 case WID_IC_CARGO_DROPDOWN
:
3155 this->ComputeCargoDisplay(index
);
3158 case WID_IC_IND_DROPDOWN
:
3159 this->ComputeIndustryDisplay(index
);
3164 bool OnTooltip([[maybe_unused
]] Point pt
, WidgetID widget
, TooltipCloseCondition close_cond
) override
3166 if (widget
!= WID_IC_PANEL
) return false;
3169 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return false;
3171 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
3172 CargoID cid
= INVALID_CARGO
;
3173 switch (fld
->type
) {
3175 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : nullptr;
3176 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : nullptr;
3177 cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
3181 case CFT_CARGO_LABEL
: {
3182 cid
= fld
->CargoLabelClickedAt(xy
);
3187 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
&& (this->ind_cargo
>= NUM_INDUSTRYTYPES
|| fieldxy
.x
!= 2)) {
3188 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP
, close_cond
);
3195 if (IsValidCargoID(cid
) && (this->ind_cargo
< NUM_INDUSTRYTYPES
|| cid
!= this->ind_cargo
- NUM_INDUSTRYTYPES
)) {
3196 const CargoSpec
*csp
= CargoSpec::Get(cid
);
3197 SetDParam(0, csp
->name
);
3198 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP
, close_cond
, 1);
3205 void OnResize() override
3207 this->vscroll
->SetCapacityFromWidget(this, WID_IC_PANEL
, WidgetDimensions::scaled
.framerect
.top
+ CargoesField::small_height
);
3212 * Open the industry and cargoes window.
3213 * @param id Industry type to display, \c NUM_INDUSTRYTYPES selects a default industry type.
3215 static void ShowIndustryCargoesWindow(IndustryType id
)
3217 if (id
>= NUM_INDUSTRYTYPES
) {
3218 for (IndustryType ind
: _sorted_industry_types
) {
3219 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
3220 if (indsp
->enabled
) {
3225 if (id
>= NUM_INDUSTRYTYPES
) return;
3228 Window
*w
= BringWindowToFrontById(WC_INDUSTRY_CARGOES
, 0);
3230 w
->InvalidateData(id
);
3233 new IndustryCargoesWindow(id
);
3236 /** Open the industry and cargoes window with an industry. */
3237 void ShowIndustryCargoesWindow()
3239 ShowIndustryCargoesWindow(NUM_INDUSTRYTYPES
);