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"
41 #include "table/strings.h"
45 #include "safeguards.h"
47 bool _ignore_restrictions
;
48 std::bitset
<NUM_INDUSTRYTYPES
> _displayed_industries
; ///< Communication from the industry chain window to the smallmap window about what industries to display.
50 /** Cargo suffix type (for which window is it requested) */
51 enum CargoSuffixType
{
52 CST_FUND
, ///< Fund-industry window
53 CST_VIEW
, ///< View-industry window
54 CST_DIR
, ///< Industry-directory window
57 /** Ways of displaying the cargo. */
58 enum CargoSuffixDisplay
{
59 CSD_CARGO
, ///< Display the cargo without sub-type (cb37 result 401).
60 CSD_CARGO_AMOUNT
, ///< Display the cargo and amount (if useful), but no sub-type (cb37 result 400 or fail).
61 CSD_CARGO_TEXT
, ///< Display then cargo and supplied string (cb37 result 800-BFF).
62 CSD_CARGO_AMOUNT_TEXT
, ///< Display then cargo, amount, and string (cb37 result 000-3FF).
65 /** Transfer storage of cargo suffix information. */
67 CargoSuffixDisplay display
; ///< How to display the cargo and text.
68 char text
[512]; ///< Cargo suffix text.
71 static void ShowIndustryCargoesWindow(IndustryType id
);
74 * Gets the string to display after the cargo name (using callback 37)
75 * @param cargo the cargo for which the suffix is requested, meaning depends on presence of flag 18 in prop 1A
76 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
77 * @param ind the industry (nullptr if in fund window)
78 * @param ind_type the industry type
79 * @param indspec the industry spec
80 * @param suffix is filled with the string to display
82 static void GetCargoSuffix(uint cargo
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, CargoSuffix
&suffix
)
84 suffix
.text
[0] = '\0';
85 suffix
.display
= CSD_CARGO_AMOUNT
;
87 if (HasBit(indspec
->callback_mask
, CBM_IND_CARGO_SUFFIX
)) {
88 TileIndex t
= (cst
!= CST_FUND
) ? ind
->location
.tile
: INVALID_TILE
;
89 uint16 callback
= GetIndustryCallback(CBID_INDUSTRY_CARGO_SUFFIX
, 0, (cst
<< 8) | cargo
, const_cast<Industry
*>(ind
), ind_type
, t
);
90 if (callback
== CALLBACK_FAILED
) return;
92 if (indspec
->grf_prop
.grffile
->grf_version
< 8) {
93 if (GB(callback
, 0, 8) == 0xFF) return;
94 if (callback
< 0x400) {
95 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
96 GetString(suffix
.text
, GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 + callback
), lastof(suffix
.text
));
97 StopTextRefStackUsage();
98 suffix
.display
= CSD_CARGO_AMOUNT_TEXT
;
101 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_CARGO_SUFFIX
, callback
);
104 } else { // GRF version 8 or higher.
105 if (callback
== 0x400) return;
106 if (callback
== 0x401) {
107 suffix
.display
= CSD_CARGO
;
110 if (callback
< 0x400) {
111 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
112 GetString(suffix
.text
, GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 + callback
), lastof(suffix
.text
));
113 StopTextRefStackUsage();
114 suffix
.display
= CSD_CARGO_AMOUNT_TEXT
;
117 if (callback
>= 0x800 && callback
< 0xC00) {
118 StartTextRefStackUsage(indspec
->grf_prop
.grffile
, 6);
119 GetString(suffix
.text
, GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 - 0x800 + callback
), lastof(suffix
.text
));
120 StopTextRefStackUsage();
121 suffix
.display
= CSD_CARGO_TEXT
;
124 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_CARGO_SUFFIX
, callback
);
130 enum CargoSuffixInOut
{
136 * Gets all strings to display after the cargoes of industries (using callback 37)
137 * @param use_input get suffixes for output cargoes or input cargoes?
138 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
139 * @param ind the industry (nullptr if in fund window)
140 * @param ind_type the industry type
141 * @param indspec the industry spec
142 * @param cargoes array with cargotypes. for CT_INVALID no suffix will be determined
143 * @param suffixes is filled with the suffixes
145 template <typename TC
, typename TS
>
146 static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, const TC
&cargoes
, TS
&suffixes
)
148 assert_compile(lengthof(cargoes
) <= lengthof(suffixes
));
150 if (indspec
->behaviour
& INDUSTRYBEH_CARGOTYPES_UNLIMITED
) {
151 /* Reworked behaviour with new many-in-many-out scheme */
152 for (uint j
= 0; j
< lengthof(suffixes
); j
++) {
153 if (cargoes
[j
] != CT_INVALID
) {
154 byte local_id
= indspec
->grf_prop
.grffile
->cargo_map
[cargoes
[j
]]; // should we check the value for valid?
155 uint cargotype
= local_id
<< 16 | use_input
;
156 GetCargoSuffix(cargotype
, cst
, ind
, ind_type
, indspec
, suffixes
[j
]);
158 suffixes
[j
].text
[0] = '\0';
159 suffixes
[j
].display
= CSD_CARGO
;
163 /* Compatible behaviour with old 3-in-2-out scheme */
164 for (uint j
= 0; j
< lengthof(suffixes
); j
++) {
165 suffixes
[j
].text
[0] = '\0';
166 suffixes
[j
].display
= CSD_CARGO
;
169 case CARGOSUFFIX_OUT
:
170 if (cargoes
[0] != CT_INVALID
) GetCargoSuffix(3, cst
, ind
, ind_type
, indspec
, suffixes
[0]);
171 if (cargoes
[1] != CT_INVALID
) GetCargoSuffix(4, cst
, ind
, ind_type
, indspec
, suffixes
[1]);
174 if (cargoes
[0] != CT_INVALID
) GetCargoSuffix(0, cst
, ind
, ind_type
, indspec
, suffixes
[0]);
175 if (cargoes
[1] != CT_INVALID
) GetCargoSuffix(1, cst
, ind
, ind_type
, indspec
, suffixes
[1]);
176 if (cargoes
[2] != CT_INVALID
) GetCargoSuffix(2, cst
, ind
, ind_type
, indspec
, suffixes
[2]);
184 std::array
<IndustryType
, NUM_INDUSTRYTYPES
> _sorted_industry_types
; ///< Industry types sorted by name.
186 /** Sort industry types by their name. */
187 static bool IndustryTypeNameSorter(const IndustryType
&a
, const IndustryType
&b
)
189 static char industry_name
[2][64];
191 const IndustrySpec
*indsp1
= GetIndustrySpec(a
);
192 GetString(industry_name
[0], indsp1
->name
, lastof(industry_name
[0]));
194 const IndustrySpec
*indsp2
= GetIndustrySpec(b
);
195 GetString(industry_name
[1], indsp2
->name
, lastof(industry_name
[1]));
197 int r
= strnatcmp(industry_name
[0], industry_name
[1]); // Sort by name (natural sorting).
199 /* If the names are equal, sort by industry type. */
200 return (r
!= 0) ? r
< 0 : (a
< b
);
204 * Initialize the list of sorted industry types.
206 void SortIndustryTypes()
208 /* Add each industry type to the list. */
209 for (IndustryType i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
210 _sorted_industry_types
[i
] = i
;
213 /* Sort industry types by name. */
214 std::sort(_sorted_industry_types
.begin(), _sorted_industry_types
.end(), IndustryTypeNameSorter
);
218 * Command callback. In case of failure to build an industry, show an error message.
219 * @param result Result of the command.
220 * @param tile Tile where the industry is placed.
221 * @param p1 Additional data of the #CMD_BUILD_INDUSTRY command.
222 * @param p2 Additional data of the #CMD_BUILD_INDUSTRY command.
225 void CcBuildIndustry(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
227 if (result
.Succeeded()) return;
229 uint8 indtype
= GB(p1
, 0, 8);
230 if (indtype
< NUM_INDUSTRYTYPES
) {
231 const IndustrySpec
*indsp
= GetIndustrySpec(indtype
);
232 if (indsp
->enabled
) {
233 SetDParam(0, indsp
->name
);
234 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, result
.GetErrorMessage(), WL_INFO
, TileX(tile
) * TILE_SIZE
, TileY(tile
) * TILE_SIZE
);
239 static const NWidgetPart _nested_build_industry_widgets
[] = {
240 NWidget(NWID_HORIZONTAL
),
241 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
242 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FUND_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
243 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
244 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
245 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
247 NWidget(NWID_HORIZONTAL
),
248 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
),
249 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_DPI_SCROLLBAR
),
251 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_DPI_INFOPANEL
), SetResize(1, 0),
253 NWidget(NWID_HORIZONTAL
),
254 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_DISPLAY_WIDGET
), SetFill(1, 0), SetResize(1, 0),
255 SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
256 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_FUND_WIDGET
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING
, STR_NULL
),
257 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
261 /** Window definition of the dynamic place industries gui */
262 static WindowDesc
_build_industry_desc(
263 WDP_AUTO
, "build_industry", 170, 212,
264 WC_BUILD_INDUSTRY
, WC_NONE
,
266 _nested_build_industry_widgets
, lengthof(_nested_build_industry_widgets
)
269 /** Build (fund or prospect) a new industry, */
270 class BuildIndustryWindow
: public Window
{
271 int selected_index
; ///< index of the element in the matrix
272 IndustryType selected_type
; ///< industry corresponding to the above index
273 uint16 count
; ///< How many industries are loaded
274 IndustryType index
[NUM_INDUSTRYTYPES
+ 1]; ///< Type of industry, in the order it was loaded
275 bool enabled
[NUM_INDUSTRYTYPES
+ 1]; ///< availability state, coming from CBID_INDUSTRY_PROBABILITY (if ever)
278 /** The offset for the text in the matrix. */
279 static const int MATRIX_TEXT_OFFSET
= 17;
280 /** The largest allowed minimum-width of the window, given in line heights */
281 static const int MAX_MINWIDTH_LINEHEIGHTS
= 20;
287 for (uint i
= 0; i
< lengthof(this->index
); i
++) {
288 this->index
[i
] = INVALID_INDUSTRYTYPE
;
289 this->enabled
[i
] = false;
292 if (_game_mode
== GM_EDITOR
) { // give room for the Many Random "button"
293 this->index
[this->count
] = INVALID_INDUSTRYTYPE
;
294 this->enabled
[this->count
] = true;
297 /* Fill the arrays with industries.
298 * The tests performed after the enabled allow to load the industries
299 * In the same way they are inserted by grf (if any)
301 for (IndustryType ind
: _sorted_industry_types
) {
302 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
303 if (indsp
->enabled
) {
304 /* Rule is that editor mode loads all industries.
305 * In game mode, all non raw industries are loaded too
306 * and raw ones are loaded only when setting allows it */
307 if (_game_mode
!= GM_EDITOR
&& indsp
->IsRawIndustry() && _settings_game
.construction
.raw_industry_construction
== 0) {
308 /* Unselect if the industry is no longer in the list */
309 if (this->selected_type
== ind
) this->selected_index
= -1;
312 this->index
[this->count
] = ind
;
313 this->enabled
[this->count
] = (_game_mode
== GM_EDITOR
) || GetIndustryProbabilityCallback(ind
, IACT_USERCREATION
, 1) > 0;
314 /* Keep the selection to the correct line */
315 if (this->selected_type
== ind
) this->selected_index
= this->count
;
320 /* first industry type is selected if the current selection is invalid.
321 * I'll be damned if there are none available ;) */
322 if (this->selected_index
== -1) {
323 this->selected_index
= 0;
324 this->selected_type
= this->index
[0];
327 this->vscroll
->SetCount(this->count
);
330 /** Update status of the fund and display-chain widgets. */
333 this->SetWidgetDisabledState(WID_DPI_FUND_WIDGET
, this->selected_type
!= INVALID_INDUSTRYTYPE
&& !this->enabled
[this->selected_index
]);
334 this->SetWidgetDisabledState(WID_DPI_DISPLAY_WIDGET
, this->selected_type
== INVALID_INDUSTRYTYPE
&& this->enabled
[this->selected_index
]);
338 * Build a string of cargo names with suffixes attached.
339 * This is distinct from the CARGO_LIST string formatting code in two ways:
340 * - This cargo list uses the order defined by the industry, rather than alphabetic.
341 * - NewGRF-supplied suffix strings can be attached to each cargo.
343 * @param cargolist Array of CargoID to display
344 * @param cargo_suffix Array of suffixes to attach to each cargo
345 * @param cargolistlen Length of arrays
346 * @param prefixstr String to use for the first item
347 * @return A formatted raw string
349 std::string
MakeCargoListString(const CargoID
*cargolist
, const CargoSuffix
*cargo_suffix
, int cargolistlen
, StringID prefixstr
) const
351 std::string cargostring
;
356 for (byte j
= 0; j
< cargolistlen
; j
++) {
357 if (cargolist
[j
] == CT_INVALID
) continue;
359 if (firstcargo
< 0) {
363 SetDParam(0, CargoSpec::Get(cargolist
[j
])->name
);
364 SetDParamStr(1, cargo_suffix
[j
].text
);
365 GetString(buf
, STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION
, lastof(buf
));
370 SetDParam(0, CargoSpec::Get(cargolist
[firstcargo
])->name
);
371 SetDParamStr(1, cargo_suffix
[firstcargo
].text
);
372 GetString(buf
, prefixstr
, lastof(buf
));
373 cargostring
= std::string(buf
) + cargostring
;
375 SetDParam(0, STR_JUST_NOTHING
);
377 GetString(buf
, prefixstr
, lastof(buf
));
378 cargostring
= std::string(buf
);
385 BuildIndustryWindow() : Window(&_build_industry_desc
)
387 this->selected_index
= -1;
388 this->selected_type
= INVALID_INDUSTRYTYPE
;
390 this->CreateNestedTree();
391 this->vscroll
= this->GetScrollbar(WID_DPI_SCROLLBAR
);
392 this->FinishInitNested(0);
397 void OnInit() override
402 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
405 case WID_DPI_MATRIX_WIDGET
: {
406 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
);
407 for (byte i
= 0; i
< this->count
; i
++) {
408 if (this->index
[i
] == INVALID_INDUSTRYTYPE
) continue;
409 d
= maxdim(d
, GetStringBoundingBox(GetIndustrySpec(this->index
[i
])->name
));
411 resize
->height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
412 d
.width
+= MATRIX_TEXT_OFFSET
+ padding
.width
;
413 d
.height
= 5 * resize
->height
;
414 *size
= maxdim(*size
, d
);
418 case WID_DPI_INFOPANEL
: {
419 /* Extra line for cost outside of editor. */
420 int height
= 2 + (_game_mode
== GM_EDITOR
? 0 : 1);
421 uint extra_lines_req
= 0;
422 uint extra_lines_prd
= 0;
423 uint extra_lines_newgrf
= 0;
424 uint max_minwidth
= FONT_HEIGHT_NORMAL
* MAX_MINWIDTH_LINEHEIGHTS
;
425 Dimension d
= {0, 0};
426 for (byte i
= 0; i
< this->count
; i
++) {
427 if (this->index
[i
] == INVALID_INDUSTRYTYPE
) continue;
429 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[i
]);
430 CargoSuffix cargo_suffix
[lengthof(indsp
->accepts_cargo
)];
432 /* Measure the accepted cargoes, if any. */
433 GetAllCargoSuffixes(CARGOSUFFIX_IN
, CST_FUND
, nullptr, this->index
[i
], indsp
, indsp
->accepts_cargo
, cargo_suffix
);
434 std::string cargostring
= this->MakeCargoListString(indsp
->accepts_cargo
, cargo_suffix
, lengthof(indsp
->accepts_cargo
), STR_INDUSTRY_VIEW_REQUIRES_N_CARGO
);
435 Dimension strdim
= GetStringBoundingBox(cargostring
.c_str());
436 if (strdim
.width
> max_minwidth
) {
437 extra_lines_req
= max(extra_lines_req
, strdim
.width
/ max_minwidth
+ 1);
438 strdim
.width
= max_minwidth
;
440 d
= maxdim(d
, strdim
);
442 /* Measure the produced cargoes, if any. */
443 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_FUND
, nullptr, this->index
[i
], indsp
, indsp
->produced_cargo
, cargo_suffix
);
444 cargostring
= this->MakeCargoListString(indsp
->produced_cargo
, cargo_suffix
, lengthof(indsp
->produced_cargo
), STR_INDUSTRY_VIEW_PRODUCES_N_CARGO
);
445 strdim
= GetStringBoundingBox(cargostring
.c_str());
446 if (strdim
.width
> max_minwidth
) {
447 extra_lines_prd
= max(extra_lines_prd
, strdim
.width
/ max_minwidth
+ 1);
448 strdim
.width
= max_minwidth
;
450 d
= maxdim(d
, strdim
);
452 if (indsp
->grf_prop
.grffile
!= nullptr) {
453 /* Reserve a few extra lines for text from an industry NewGRF. */
454 extra_lines_newgrf
= 4;
458 /* Set it to something more sane :) */
459 height
+= extra_lines_prd
+ extra_lines_req
+ extra_lines_newgrf
;
460 size
->height
= height
* FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
461 size
->width
= d
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
465 case WID_DPI_FUND_WIDGET
: {
466 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
467 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
));
468 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
));
469 d
.width
+= padding
.width
;
470 d
.height
+= padding
.height
;
471 *size
= maxdim(*size
, d
);
477 void SetStringParameters(int widget
) const override
480 case WID_DPI_FUND_WIDGET
:
481 /* Raw industries might be prospected. Show this fact by changing the string
482 * In Editor, you just build, while ingame, or you fund or you prospect */
483 if (_game_mode
== GM_EDITOR
) {
484 /* We've chosen many random industries but no industries have been specified */
485 SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
487 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[this->selected_index
]);
488 SetDParam(0, (_settings_game
.construction
.raw_industry_construction
== 2 && indsp
->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
: STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
);
494 void DrawWidget(const Rect
&r
, int widget
) const override
497 case WID_DPI_MATRIX_WIDGET
: {
498 uint text_left
, text_right
, icon_left
, icon_right
;
499 if (_current_text_dir
== TD_RTL
) {
500 icon_right
= r
.right
- WD_MATRIX_RIGHT
;
501 icon_left
= icon_right
- 10;
502 text_right
= icon_right
- BuildIndustryWindow::MATRIX_TEXT_OFFSET
;
503 text_left
= r
.left
+ WD_MATRIX_LEFT
;
505 icon_left
= r
.left
+ WD_MATRIX_LEFT
;
506 icon_right
= icon_left
+ 10;
507 text_left
= icon_left
+ BuildIndustryWindow::MATRIX_TEXT_OFFSET
;
508 text_right
= r
.right
- WD_MATRIX_RIGHT
;
511 for (byte i
= 0; i
< this->vscroll
->GetCapacity() && i
+ this->vscroll
->GetPosition() < this->count
; i
++) {
512 int y
= r
.top
+ WD_MATRIX_TOP
+ i
* this->resize
.step_height
;
513 bool selected
= this->selected_index
== i
+ this->vscroll
->GetPosition();
515 if (this->index
[i
+ this->vscroll
->GetPosition()] == INVALID_INDUSTRYTYPE
) {
516 DrawString(text_left
, text_right
, y
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
, selected
? TC_WHITE
: TC_ORANGE
);
519 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[i
+ this->vscroll
->GetPosition()]);
521 /* Draw the name of the industry in white is selected, otherwise, in orange */
522 DrawString(text_left
, text_right
, y
, indsp
->name
, selected
? TC_WHITE
: TC_ORANGE
);
523 GfxFillRect(icon_left
, y
+ 1, icon_right
, y
+ 7, selected
? PC_WHITE
: PC_BLACK
);
524 GfxFillRect(icon_left
+ 1, y
+ 2, icon_right
- 1, y
+ 6, indsp
->map_colour
);
529 case WID_DPI_INFOPANEL
: {
530 int y
= r
.top
+ WD_FRAMERECT_TOP
;
531 int bottom
= r
.bottom
- WD_FRAMERECT_BOTTOM
;
532 int left
= r
.left
+ WD_FRAMERECT_LEFT
;
533 int right
= r
.right
- WD_FRAMERECT_RIGHT
;
535 if (this->selected_type
== INVALID_INDUSTRYTYPE
) {
536 DrawStringMultiLine(left
, right
, y
, bottom
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP
);
540 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
542 if (_game_mode
!= GM_EDITOR
) {
543 SetDParam(0, indsp
->GetConstructionCost());
544 DrawString(left
, right
, y
, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST
);
545 y
+= FONT_HEIGHT_NORMAL
;
548 CargoSuffix cargo_suffix
[lengthof(indsp
->accepts_cargo
)];
550 /* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
551 GetAllCargoSuffixes(CARGOSUFFIX_IN
, CST_FUND
, nullptr, this->selected_type
, indsp
, indsp
->accepts_cargo
, cargo_suffix
);
552 std::string cargostring
= this->MakeCargoListString(indsp
->accepts_cargo
, cargo_suffix
, lengthof(indsp
->accepts_cargo
), STR_INDUSTRY_VIEW_REQUIRES_N_CARGO
);
553 y
= DrawStringMultiLine(left
, right
, y
, bottom
, cargostring
.c_str());
555 /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
556 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_FUND
, nullptr, this->selected_type
, indsp
, indsp
->produced_cargo
, cargo_suffix
);
557 cargostring
= this->MakeCargoListString(indsp
->produced_cargo
, cargo_suffix
, lengthof(indsp
->produced_cargo
), STR_INDUSTRY_VIEW_PRODUCES_N_CARGO
);
558 y
= DrawStringMultiLine(left
, right
, y
, bottom
, cargostring
.c_str());
560 /* Get the additional purchase info text, if it has not already been queried. */
561 if (HasBit(indsp
->callback_mask
, CBM_IND_FUND_MORE_TEXT
)) {
562 uint16 callback_res
= GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT
, 0, 0, nullptr, this->selected_type
, INVALID_TILE
);
563 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
564 if (callback_res
> 0x400) {
565 ErrorUnknownCallbackResult(indsp
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_FUND_MORE_TEXT
, callback_res
);
567 StringID str
= GetGRFStringID(indsp
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
); // No. here's the new string
568 if (str
!= STR_UNDEFINED
) {
569 StartTextRefStackUsage(indsp
->grf_prop
.grffile
, 6);
570 DrawStringMultiLine(left
, right
, y
, bottom
, str
, TC_YELLOW
);
571 StopTextRefStackUsage();
581 void OnClick(Point pt
, int widget
, int click_count
) override
584 case WID_DPI_MATRIX_WIDGET
: {
585 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_DPI_MATRIX_WIDGET
);
586 if (y
< this->count
) { // Is it within the boundaries of available data?
587 this->selected_index
= y
;
588 this->selected_type
= this->index
[y
];
589 const IndustrySpec
*indsp
= (this->selected_type
== INVALID_INDUSTRYTYPE
) ? nullptr : GetIndustrySpec(this->selected_type
);
593 if (_thd
.GetCallbackWnd() == this &&
594 ((_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && indsp
!= nullptr && indsp
->IsRawIndustry()) ||
595 this->selected_type
== INVALID_INDUSTRYTYPE
||
596 !this->enabled
[this->selected_index
])) {
597 /* Reset the button state if going to prospecting or "build many industries" */
598 this->RaiseButtons();
599 ResetObjectToPlace();
603 if (this->enabled
[this->selected_index
] && click_count
> 1) this->OnClick(pt
, WID_DPI_FUND_WIDGET
, 1);
608 case WID_DPI_DISPLAY_WIDGET
:
609 if (this->selected_type
!= INVALID_INDUSTRYTYPE
) ShowIndustryCargoesWindow(this->selected_type
);
612 case WID_DPI_FUND_WIDGET
: {
613 if (this->selected_type
== INVALID_INDUSTRYTYPE
) {
614 this->HandleButtonClick(WID_DPI_FUND_WIDGET
);
616 if (Town::GetNumItems() == 0) {
617 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
);
619 extern void GenerateIndustries();
620 _generating_world
= true;
621 GenerateIndustries();
622 _generating_world
= false;
624 } else if (_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && GetIndustrySpec(this->selected_type
)->IsRawIndustry()) {
625 DoCommandP(0, this->selected_type
, InteractiveRandom(), CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
));
626 this->HandleButtonClick(WID_DPI_FUND_WIDGET
);
628 HandlePlacePushButton(this, WID_DPI_FUND_WIDGET
, SPR_CURSOR_INDUSTRY
, HT_RECT
);
635 void OnResize() override
637 /* Adjust the number of items in the matrix depending of the resize */
638 this->vscroll
->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET
);
641 void OnPlaceObject(Point pt
, TileIndex tile
) override
644 /* We do not need to protect ourselves against "Random Many Industries" in this mode */
645 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
646 uint32 seed
= InteractiveRandom();
647 uint32 layout_index
= InteractiveRandomRange((uint32
)indsp
->layouts
.size());
649 if (_game_mode
== GM_EDITOR
) {
650 /* Show error if no town exists at all */
651 if (Town::GetNumItems() == 0) {
652 SetDParam(0, indsp
->name
);
653 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
, pt
.x
, pt
.y
);
657 Backup
<CompanyID
> cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
658 _generating_world
= true;
659 _ignore_restrictions
= true;
661 DoCommandP(tile
, (layout_index
<< 8) | this->selected_type
, seed
,
662 CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
), &CcBuildIndustry
);
664 cur_company
.Restore();
665 _ignore_restrictions
= false;
666 _generating_world
= false;
668 success
= DoCommandP(tile
, (layout_index
<< 8) | this->selected_type
, seed
, CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
));
671 /* If an industry has been built, just reset the cursor and the system */
672 if (success
&& !_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
675 void OnHundredthTick() override
677 if (_game_mode
== GM_EDITOR
) return;
678 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
680 if (indsp
->enabled
) {
681 bool call_back_result
= GetIndustryProbabilityCallback(this->selected_type
, IACT_USERCREATION
, 1) > 0;
683 /* Only if result does match the previous state would it require a redraw. */
684 if (call_back_result
!= this->enabled
[this->selected_index
]) {
685 this->enabled
[this->selected_index
] = call_back_result
;
692 void OnTimeout() override
694 this->RaiseButtons();
697 void OnPlaceObjectAbort() override
699 this->RaiseButtons();
703 * Some data on this window has become invalid.
704 * @param data Information about the changed data.
705 * @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.
707 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
709 if (!gui_scope
) return;
712 const IndustrySpec
*indsp
= (this->selected_type
== INVALID_INDUSTRYTYPE
) ? nullptr : GetIndustrySpec(this->selected_type
);
713 if (indsp
== nullptr) this->enabled
[this->selected_index
] = _settings_game
.difficulty
.industry_density
!= ID_FUND_ONLY
;
718 void ShowBuildIndustryWindow()
720 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
721 if (BringWindowToFrontById(WC_BUILD_INDUSTRY
, 0)) return;
722 new BuildIndustryWindow();
725 static void UpdateIndustryProduction(Industry
*i
);
727 static inline bool IsProductionAlterable(const Industry
*i
)
729 const IndustrySpec
*is
= GetIndustrySpec(i
->type
);
730 bool has_prod
= false;
731 for (size_t j
= 0; j
< lengthof(is
->production_rate
); j
++) {
732 if (is
->production_rate
[j
] != 0) {
737 return ((_game_mode
== GM_EDITOR
|| _cheats
.setup_prod
.value
) &&
738 (has_prod
|| is
->IsRawIndustry()) &&
742 class IndustryViewWindow
: public Window
744 /** Modes for changing production */
746 EA_NONE
, ///< Not alterable
747 EA_MULTIPLIER
, ///< Allow changing the production multiplier
748 EA_RATE
, ///< Allow changing the production rates
751 /** Specific lines in the info panel */
753 IL_NONE
, ///< No line
754 IL_MULTIPLIER
, ///< Production multiplier
755 IL_RATE1
, ///< Production rate of cargo 1
756 IL_RATE2
, ///< Production rate of cargo 2
759 Editability editable
; ///< Mode for changing production
760 InfoLine editbox_line
; ///< The line clicked to open the edit box
761 InfoLine clicked_line
; ///< The line of the button that has been clicked
762 byte clicked_button
; ///< The button that has been clicked (to raise)
763 int production_offset_y
; ///< The offset of the production texts/buttons
764 int info_height
; ///< Height needed for the #WID_IV_INFO panel
767 IndustryViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
769 this->flags
|= WF_DISABLE_VP_SCROLL
;
770 this->editbox_line
= IL_NONE
;
771 this->clicked_line
= IL_NONE
;
772 this->clicked_button
= 0;
773 this->info_height
= WD_FRAMERECT_TOP
+ 2 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
+ 1; // Info panel has at least two lines text.
775 this->InitNested(window_number
);
776 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
777 nvp
->InitializeViewport(this, Industry::Get(window_number
)->location
.GetCenterTile(), ZOOM_LVL_INDUSTRY
);
779 this->InvalidateData();
782 void OnPaint() override
786 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
788 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_IV_INFO
);
789 uint expected
= this->DrawInfo(nwi
->pos_x
, nwi
->pos_x
+ nwi
->current_x
- 1, nwi
->pos_y
) - nwi
->pos_y
;
790 if (expected
> nwi
->current_y
- 1) {
791 this->info_height
= expected
+ 1;
798 * Draw the text in the #WID_IV_INFO panel.
799 * @param left Left edge of the panel.
800 * @param right Right edge of the panel.
801 * @param top Top edge of the panel.
802 * @return Expected position of the bottom edge of the panel.
804 int DrawInfo(uint left
, uint right
, uint top
)
806 Industry
*i
= Industry::Get(this->window_number
);
807 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
808 int y
= top
+ WD_FRAMERECT_TOP
;
810 bool has_accept
= false;
812 if (i
->prod_level
== PRODLEVEL_CLOSURE
) {
813 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_INDUSTRY_ANNOUNCED_CLOSURE
);
814 y
+= 2 * FONT_HEIGHT_NORMAL
;
817 CargoSuffix cargo_suffix
[lengthof(i
->accepts_cargo
)];
818 GetAllCargoSuffixes(CARGOSUFFIX_IN
, CST_VIEW
, i
, i
->type
, ind
, i
->accepts_cargo
, cargo_suffix
);
819 bool stockpiling
= HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_CARGO_ARRIVAL
) || HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_256_TICKS
);
821 uint left_side
= left
+ WD_FRAMERECT_LEFT
* 4; // Indent accepted cargoes.
822 for (byte j
= 0; j
< lengthof(i
->accepts_cargo
); j
++) {
823 if (i
->accepts_cargo
[j
] == CT_INVALID
) continue;
826 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_REQUIRES
);
827 y
+= FONT_HEIGHT_NORMAL
;
830 SetDParam(0, CargoSpec::Get(i
->accepts_cargo
[j
])->name
);
831 SetDParam(1, i
->accepts_cargo
[j
]);
832 SetDParam(2, i
->incoming_cargo_waiting
[j
]);
834 StringID str
= STR_NULL
;
835 switch (cargo_suffix
[j
].display
) {
836 case CSD_CARGO_AMOUNT_TEXT
:
837 SetDParamStr(3, cargo_suffix
[j
].text
);
839 case CSD_CARGO_AMOUNT
:
840 str
= stockpiling
? STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT
: STR_INDUSTRY_VIEW_ACCEPT_CARGO
;
844 SetDParamStr(3, cargo_suffix
[j
].text
);
847 str
= STR_INDUSTRY_VIEW_ACCEPT_CARGO
;
853 DrawString(left_side
, right
- WD_FRAMERECT_RIGHT
, y
, str
);
854 y
+= FONT_HEIGHT_NORMAL
;
857 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_VIEW
, i
, i
->type
, ind
, i
->produced_cargo
, cargo_suffix
);
859 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
860 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
862 if (has_accept
) y
+= WD_PAR_VSEP_WIDE
;
863 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE
);
864 y
+= FONT_HEIGHT_NORMAL
;
865 if (this->editable
== EA_RATE
) this->production_offset_y
= y
;
869 SetDParam(0, i
->produced_cargo
[j
]);
870 SetDParam(1, i
->last_month_production
[j
]);
871 SetDParamStr(2, cargo_suffix
[j
].text
);
872 SetDParam(3, ToPercent8(i
->last_month_pct_transported
[j
]));
873 uint x
= left
+ WD_FRAMETEXT_LEFT
+ (this->editable
== EA_RATE
? SETTING_BUTTON_WIDTH
+ 10 : 0);
874 DrawString(x
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_TRANSPORTED
);
875 /* Let's put out those buttons.. */
876 if (this->editable
== EA_RATE
) {
877 DrawArrowButtons(left
+ WD_FRAMETEXT_LEFT
, y
, COLOUR_YELLOW
, (this->clicked_line
== IL_RATE1
+ j
) ? this->clicked_button
: 0,
878 i
->production_rate
[j
] > 0, i
->production_rate
[j
] < 255);
880 y
+= FONT_HEIGHT_NORMAL
;
883 /* Display production multiplier if editable */
884 if (this->editable
== EA_MULTIPLIER
) {
885 y
+= WD_PAR_VSEP_WIDE
;
886 this->production_offset_y
= y
;
887 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
888 uint x
= left
+ WD_FRAMETEXT_LEFT
+ SETTING_BUTTON_WIDTH
+ 10;
889 DrawString(x
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL
);
890 DrawArrowButtons(left
+ WD_FRAMETEXT_LEFT
, y
, COLOUR_YELLOW
, (this->clicked_line
== IL_MULTIPLIER
) ? this->clicked_button
: 0,
891 i
->prod_level
> PRODLEVEL_MINIMUM
, i
->prod_level
< PRODLEVEL_MAXIMUM
);
892 y
+= FONT_HEIGHT_NORMAL
;
895 /* Get the extra message for the GUI */
896 if (HasBit(ind
->callback_mask
, CBM_IND_WINDOW_MORE_TEXT
)) {
897 uint16 callback_res
= GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT
, 0, 0, i
, i
->type
, i
->location
.tile
);
898 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
899 if (callback_res
> 0x400) {
900 ErrorUnknownCallbackResult(ind
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_WINDOW_MORE_TEXT
, callback_res
);
902 StringID message
= GetGRFStringID(ind
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
);
903 if (message
!= STR_NULL
&& message
!= STR_UNDEFINED
) {
904 y
+= WD_PAR_VSEP_WIDE
;
906 StartTextRefStackUsage(ind
->grf_prop
.grffile
, 6);
907 /* Use all the available space left from where we stand up to the
908 * end of the window. We ALSO enlarge the window if needed, so we
909 * can 'go' wild with the bottom of the window. */
910 y
= DrawStringMultiLine(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, UINT16_MAX
, message
, TC_BLACK
);
911 StopTextRefStackUsage();
916 return y
+ WD_FRAMERECT_BOTTOM
;
919 void SetStringParameters(int widget
) const override
921 if (widget
== WID_IV_CAPTION
) SetDParam(0, this->window_number
);
924 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
926 if (widget
== WID_IV_INFO
) size
->height
= this->info_height
;
929 void OnClick(Point pt
, int widget
, int click_count
) override
933 Industry
*i
= Industry::Get(this->window_number
);
934 InfoLine line
= IL_NONE
;
936 switch (this->editable
) {
940 if (IsInsideBS(pt
.y
, this->production_offset_y
, FONT_HEIGHT_NORMAL
)) line
= IL_MULTIPLIER
;
944 if (pt
.y
>= this->production_offset_y
) {
945 int row
= (pt
.y
- this->production_offset_y
) / FONT_HEIGHT_NORMAL
;
946 for (uint j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
947 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
950 line
= (InfoLine
)(IL_RATE1
+ j
);
957 if (line
== IL_NONE
) return;
959 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(widget
);
960 int left
= nwi
->pos_x
+ WD_FRAMETEXT_LEFT
;
961 int right
= nwi
->pos_x
+ nwi
->current_x
- 1 - WD_FRAMERECT_RIGHT
;
962 if (IsInsideMM(pt
.x
, left
, left
+ SETTING_BUTTON_WIDTH
)) {
963 /* Clicked buttons, decrease or increase production */
964 byte button
= (pt
.x
< left
+ SETTING_BUTTON_WIDTH
/ 2) ? 1 : 2;
965 switch (this->editable
) {
968 if (i
->prod_level
<= PRODLEVEL_MINIMUM
) return;
969 i
->prod_level
= max
<uint
>(i
->prod_level
/ 2, PRODLEVEL_MINIMUM
);
971 if (i
->prod_level
>= PRODLEVEL_MAXIMUM
) return;
972 i
->prod_level
= minu(i
->prod_level
* 2, PRODLEVEL_MAXIMUM
);
978 if (i
->production_rate
[line
- IL_RATE1
] <= 0) return;
979 i
->production_rate
[line
- IL_RATE1
] = max(i
->production_rate
[line
- IL_RATE1
] / 2, 0);
981 if (i
->production_rate
[line
- IL_RATE1
] >= 255) return;
982 /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
983 int new_prod
= i
->production_rate
[line
- IL_RATE1
] == 0 ? 1 : i
->production_rate
[line
- IL_RATE1
] * 2;
984 i
->production_rate
[line
- IL_RATE1
] = minu(new_prod
, 255);
988 default: NOT_REACHED();
991 UpdateIndustryProduction(i
);
994 this->clicked_line
= line
;
995 this->clicked_button
= button
;
996 } else if (IsInsideMM(pt
.x
, left
+ SETTING_BUTTON_WIDTH
+ 10, right
)) {
997 /* clicked the text */
998 this->editbox_line
= line
;
999 switch (this->editable
) {
1001 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
1002 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION_LEVEL
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
1006 SetDParam(0, i
->production_rate
[line
- IL_RATE1
] * 8);
1007 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
1010 default: NOT_REACHED();
1017 Industry
*i
= Industry::Get(this->window_number
);
1018 if (_ctrl_pressed
) {
1019 ShowExtraViewPortWindow(i
->location
.GetCenterTile());
1021 ScrollMainWindowToTile(i
->location
.GetCenterTile());
1026 case WID_IV_DISPLAY
: {
1027 Industry
*i
= Industry::Get(this->window_number
);
1028 ShowIndustryCargoesWindow(i
->type
);
1034 void OnTimeout() override
1036 this->clicked_line
= IL_NONE
;
1037 this->clicked_button
= 0;
1041 void OnResize() override
1043 if (this->viewport
!= nullptr) {
1044 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
1045 nvp
->UpdateViewportCoordinates(this);
1047 ScrollWindowToTile(Industry::Get(this->window_number
)->location
.GetCenterTile(), this, true); // Re-center viewport.
1051 void OnQueryTextFinished(char *str
) override
1053 if (StrEmpty(str
)) return;
1055 Industry
*i
= Industry::Get(this->window_number
);
1056 uint value
= atoi(str
);
1057 switch (this->editbox_line
) {
1058 case IL_NONE
: NOT_REACHED();
1061 i
->prod_level
= ClampU(RoundDivSU(value
* PRODLEVEL_DEFAULT
, 100), PRODLEVEL_MINIMUM
, PRODLEVEL_MAXIMUM
);
1065 i
->production_rate
[this->editbox_line
- IL_RATE1
] = ClampU(RoundDivSU(value
, 8), 0, 255);
1068 UpdateIndustryProduction(i
);
1073 * Some data on this window has become invalid.
1074 * @param data Information about the changed data.
1075 * @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.
1077 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1079 if (!gui_scope
) return;
1080 const Industry
*i
= Industry::Get(this->window_number
);
1081 if (IsProductionAlterable(i
)) {
1082 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
1083 this->editable
= ind
->UsesSmoothEconomy() ? EA_RATE
: EA_MULTIPLIER
;
1085 this->editable
= EA_NONE
;
1089 bool IsNewGRFInspectable() const override
1091 return ::IsNewGRFInspectable(GSF_INDUSTRIES
, this->window_number
);
1094 void ShowNewGRFInspectWindow() const override
1096 ::ShowNewGRFInspectWindow(GSF_INDUSTRIES
, this->window_number
);
1100 static void UpdateIndustryProduction(Industry
*i
)
1102 const IndustrySpec
*indspec
= GetIndustrySpec(i
->type
);
1103 if (!indspec
->UsesSmoothEconomy()) i
->RecomputeProductionMultipliers();
1105 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
1106 if (i
->produced_cargo
[j
] != CT_INVALID
) {
1107 i
->last_month_production
[j
] = 8 * i
->production_rate
[j
];
1112 /** Widget definition of the view industry gui */
1113 static const NWidgetPart _nested_industry_view_widgets
[] = {
1114 NWidget(NWID_HORIZONTAL
),
1115 NWidget(WWT_CLOSEBOX
, COLOUR_CREAM
),
1116 NWidget(WWT_CAPTION
, COLOUR_CREAM
, WID_IV_CAPTION
), SetDataTip(STR_INDUSTRY_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1117 NWidget(WWT_DEBUGBOX
, COLOUR_CREAM
),
1118 NWidget(WWT_SHADEBOX
, COLOUR_CREAM
),
1119 NWidget(WWT_DEFSIZEBOX
, COLOUR_CREAM
),
1120 NWidget(WWT_STICKYBOX
, COLOUR_CREAM
),
1122 NWidget(WWT_PANEL
, COLOUR_CREAM
),
1123 NWidget(WWT_INSET
, COLOUR_CREAM
), SetPadding(2, 2, 2, 2),
1124 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_IV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
1127 NWidget(WWT_PANEL
, COLOUR_CREAM
, WID_IV_INFO
), SetMinimalSize(260, 2), SetResize(1, 0),
1129 NWidget(NWID_HORIZONTAL
),
1130 NWidget(WWT_PUSHTXTBTN
, COLOUR_CREAM
, WID_IV_GOTO
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION
, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP
),
1131 NWidget(WWT_PUSHTXTBTN
, COLOUR_CREAM
, WID_IV_DISPLAY
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
1132 NWidget(WWT_RESIZEBOX
, COLOUR_CREAM
),
1136 /** Window definition of the view industry gui */
1137 static WindowDesc
_industry_view_desc(
1138 WDP_AUTO
, "view_industry", 260, 120,
1139 WC_INDUSTRY_VIEW
, WC_NONE
,
1141 _nested_industry_view_widgets
, lengthof(_nested_industry_view_widgets
)
1144 void ShowIndustryViewWindow(int industry
)
1146 AllocateWindowDescFront
<IndustryViewWindow
>(&_industry_view_desc
, industry
);
1149 /** Widget definition of the industry directory gui */
1150 static const NWidgetPart _nested_industry_directory_widgets
[] = {
1151 NWidget(NWID_HORIZONTAL
),
1152 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1153 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1154 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1155 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1156 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1158 NWidget(NWID_HORIZONTAL
),
1159 NWidget(NWID_VERTICAL
),
1160 NWidget(NWID_HORIZONTAL
),
1161 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_ID_DROPDOWN_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
1162 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_ID_DROPDOWN_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
1163 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
),
1164 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
),
1165 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetResize(1, 0), EndContainer(),
1167 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_ID_INDUSTRY_LIST
), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION
), SetResize(1, 1), SetScrollbar(WID_ID_SCROLLBAR
), EndContainer(),
1169 NWidget(NWID_VERTICAL
),
1170 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_ID_SCROLLBAR
),
1171 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1176 typedef GUIList
<const Industry
*, const std::pair
<CargoID
, CargoID
> &> GUIIndustryList
;
1178 /** Special cargo filter criteria */
1179 enum CargoFilterSpecialType
{
1180 CF_ANY
= CT_NO_REFIT
, ///< Show all industries (i.e. no filtering)
1181 CF_NONE
= CT_INVALID
, ///< Show only industries which do not produce/accept cargo
1184 /** Cargo filter functions */
1186 * Check whether an industry accepts and produces a certain cargo pair.
1187 * @param industry The industry whose cargoes will being checked.
1188 * @param cargoes The accepted and produced cargo pair to look for.
1189 * @return bool Whether the given cargoes accepted and produced by the industry.
1191 static bool CDECL
CargoFilter(const Industry
* const *industry
, const std::pair
<CargoID
, CargoID
> &cargoes
)
1193 auto accepted_cargo
= cargoes
.first
;
1194 auto produced_cargo
= cargoes
.second
;
1196 bool accepted_cargo_matches
;
1198 switch (accepted_cargo
) {
1200 accepted_cargo_matches
= true;
1204 accepted_cargo_matches
= std::all_of(std::begin((*industry
)->accepts_cargo
), std::end((*industry
)->accepts_cargo
), [](CargoID cargo
) {
1205 return cargo
== CT_INVALID
;
1210 const auto &ac
= (*industry
)->accepts_cargo
;
1211 accepted_cargo_matches
= std::find(std::begin(ac
), std::end(ac
), accepted_cargo
) != std::end(ac
);
1215 bool produced_cargo_matches
;
1217 switch (produced_cargo
) {
1219 produced_cargo_matches
= true;
1223 produced_cargo_matches
= std::all_of(std::begin((*industry
)->produced_cargo
), std::end((*industry
)->produced_cargo
), [](CargoID cargo
) {
1224 return cargo
== CT_INVALID
;
1229 const auto &pc
= (*industry
)->produced_cargo
;
1230 produced_cargo_matches
= std::find(std::begin(pc
), std::end(pc
), produced_cargo
) != std::end(pc
);
1234 return accepted_cargo_matches
&& produced_cargo_matches
;
1237 static GUIIndustryList::FilterFunction
* const _filter_funcs
[] = { &CargoFilter
};
1241 * The list of industries.
1243 class IndustryDirectoryWindow
: public Window
{
1245 /* Runtime saved values */
1246 static Listing last_sorting
;
1248 /* Constants for sorting stations */
1249 static const StringID sorter_names
[];
1250 static GUIIndustryList::SortFunction
* const sorter_funcs
[];
1252 GUIIndustryList industries
;
1255 CargoID cargo_filter
[NUM_CARGO
+ 2]; ///< Available cargo filters; CargoID or CF_ANY or CF_NONE
1256 StringID cargo_filter_texts
[NUM_CARGO
+ 3]; ///< Texts for filter_cargo, terminated by INVALID_STRING_ID
1257 CargoID produced_cargo_filter_criteria
; ///< Selected produced cargo filter
1258 CargoID accepted_cargo_filter_criteria
; ///< Selected accepted cargo filter
1261 * Set cargo filter list item index.
1262 * @param index The index of the cargo to be set
1264 void SetProducedCargoFilterIndex(int index
)
1266 if (this->produced_cargo_filter_criteria
!= index
) {
1267 this->produced_cargo_filter_criteria
= index
;
1268 /* deactivate filter if criteria is 'Show All', activate it otherwise */
1269 bool is_filtering_necessary
= this->cargo_filter
[this->produced_cargo_filter_criteria
] != CF_ANY
|| this->cargo_filter
[this->accepted_cargo_filter_criteria
] != CF_ANY
;
1271 this->industries
.SetFilterState(is_filtering_necessary
);
1272 this->industries
.SetFilterType(0);
1273 this->industries
.ForceRebuild();
1278 * Set cargo filter list item index.
1279 * @param index The index of the cargo to be set
1281 void SetAcceptedCargoFilterIndex(int index
)
1283 if (this->accepted_cargo_filter_criteria
!= index
) {
1284 this->accepted_cargo_filter_criteria
= index
;
1285 /* deactivate filter if criteria is 'Show All', activate it otherwise */
1286 bool is_filtering_necessary
= this->cargo_filter
[this->produced_cargo_filter_criteria
] != CF_ANY
|| this->cargo_filter
[this->accepted_cargo_filter_criteria
] != CF_ANY
;
1288 this->industries
.SetFilterState(is_filtering_necessary
);
1289 this->industries
.SetFilterType(0);
1290 this->industries
.ForceRebuild();
1295 * Populate the filter list and set the cargo filter criteria.
1297 void SetCargoFilterArray()
1299 uint filter_items
= 0;
1301 /* Add item for disabling filtering. */
1302 this->cargo_filter
[filter_items
] = CF_ANY
;
1303 this->cargo_filter_texts
[filter_items
] = STR_INDUSTRY_DIRECTORY_FILTER_ALL_TYPES
;
1304 this->produced_cargo_filter_criteria
= filter_items
;
1305 this->accepted_cargo_filter_criteria
= filter_items
;
1308 /* Add item for industries not producing anything, e.g. power plants */
1309 this->cargo_filter
[filter_items
] = CF_NONE
;
1310 this->cargo_filter_texts
[filter_items
] = STR_INDUSTRY_DIRECTORY_FILTER_NONE
;
1313 /* Collect available cargo types for filtering. */
1314 const CargoSpec
*cs
;
1315 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
1316 this->cargo_filter
[filter_items
] = cs
->Index();
1317 this->cargo_filter_texts
[filter_items
] = cs
->name
;
1321 /* Terminate the filter list. */
1322 this->cargo_filter_texts
[filter_items
] = INVALID_STRING_ID
;
1324 this->industries
.SetFilterFuncs(_filter_funcs
);
1326 bool is_filtering_necessary
= this->cargo_filter
[this->produced_cargo_filter_criteria
] != CF_ANY
|| this->cargo_filter
[this->accepted_cargo_filter_criteria
] != CF_ANY
;
1328 this->industries
.SetFilterState(is_filtering_necessary
);
1331 /** (Re)Build industries list */
1332 void BuildSortIndustriesList()
1334 if (this->industries
.NeedRebuild()) {
1335 this->industries
.clear();
1337 for (const Industry
*i
: Industry::Iterate()) {
1338 this->industries
.push_back(i
);
1341 this->industries
.shrink_to_fit();
1342 this->industries
.RebuildDone();
1345 auto filter
= std::make_pair(this->cargo_filter
[this->accepted_cargo_filter_criteria
],
1346 this->cargo_filter
[this->produced_cargo_filter_criteria
]);
1348 this->industries
.Filter(filter
);
1349 this->industries
.Sort();
1351 this->vscroll
->SetCount((uint
)this->industries
.size()); // Update scrollbar as well.
1357 * Returns percents of cargo transported if industry produces this cargo, else -1
1359 * @param i industry to check
1360 * @param id cargo slot
1361 * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot
1363 static inline int GetCargoTransportedPercentsIfValid(const Industry
*i
, uint id
)
1365 assert(id
< lengthof(i
->produced_cargo
));
1367 if (i
->produced_cargo
[id
] == CT_INVALID
) return 101;
1368 return ToPercent8(i
->last_month_pct_transported
[id
]);
1372 * Returns value representing industry's transported cargo
1373 * percentage for industry sorting
1375 * @param i industry to check
1376 * @return value used for sorting
1378 static int GetCargoTransportedSortValue(const Industry
*i
)
1380 int p1
= GetCargoTransportedPercentsIfValid(i
, 0);
1381 int p2
= GetCargoTransportedPercentsIfValid(i
, 1);
1383 if (p1
> p2
) Swap(p1
, p2
); // lower value has higher priority
1385 return (p1
<< 8) + p2
;
1388 /** Sort industries by name */
1389 static bool IndustryNameSorter(const Industry
* const &a
, const Industry
* const &b
)
1391 int r
= strnatcmp(a
->GetCachedName(), b
->GetCachedName()); // Sort by name (natural sorting).
1392 if (r
== 0) return a
->index
< b
->index
;
1396 /** Sort industries by type and name */
1397 static bool IndustryTypeSorter(const Industry
* const &a
, const Industry
* const &b
)
1400 while (it_a
!= NUM_INDUSTRYTYPES
&& a
->type
!= _sorted_industry_types
[it_a
]) it_a
++;
1402 while (it_b
!= NUM_INDUSTRYTYPES
&& b
->type
!= _sorted_industry_types
[it_b
]) it_b
++;
1403 int r
= it_a
- it_b
;
1404 return (r
== 0) ? IndustryNameSorter(a
, b
) : r
< 0;
1407 /** Sort industries by production and name */
1408 static bool IndustryProductionSorter(const Industry
* const &a
, const Industry
* const &b
)
1410 uint prod_a
= 0, prod_b
= 0;
1411 for (uint i
= 0; i
< lengthof(a
->produced_cargo
); i
++) {
1412 if (a
->produced_cargo
[i
] != CT_INVALID
) prod_a
+= a
->last_month_production
[i
];
1413 if (b
->produced_cargo
[i
] != CT_INVALID
) prod_b
+= b
->last_month_production
[i
];
1415 int r
= prod_a
- prod_b
;
1417 return (r
== 0) ? IndustryTypeSorter(a
, b
) : r
< 0;
1420 /** Sort industries by transported cargo and name */
1421 static bool IndustryTransportedCargoSorter(const Industry
* const &a
, const Industry
* const &b
)
1423 int r
= GetCargoTransportedSortValue(a
) - GetCargoTransportedSortValue(b
);
1424 return (r
== 0) ? IndustryNameSorter(a
, b
) : r
< 0;
1428 * Get the StringID to draw and set the appropriate DParams.
1429 * @param i the industry to get the StringID of.
1430 * @return the StringID.
1432 StringID
GetIndustryString(const Industry
*i
) const
1434 const IndustrySpec
*indsp
= GetIndustrySpec(i
->type
);
1438 SetDParam(p
++, i
->index
);
1440 static CargoSuffix cargo_suffix
[lengthof(i
->produced_cargo
)];
1441 GetAllCargoSuffixes(CARGOSUFFIX_OUT
, CST_DIR
, i
, i
->type
, indsp
, i
->produced_cargo
, cargo_suffix
);
1443 /* Get industry productions (CargoID, production, suffix, transported) */
1444 typedef std::tuple
<CargoID
, uint16
, const char*, uint
> CargoInfo
;
1445 std::vector
<CargoInfo
> cargos
;
1447 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
1448 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
1449 cargos
.emplace_back(i
->produced_cargo
[j
], i
->last_month_production
[j
], cargo_suffix
[j
].text
, ToPercent8(i
->last_month_pct_transported
[j
]));
1452 /* Sort by descending production, then descending transported */
1453 std::sort(cargos
.begin(), cargos
.end(), [](const CargoInfo a
, const CargoInfo b
) {
1454 if (std::get
<1>(a
) != std::get
<1>(b
)) return std::get
<1>(a
) > std::get
<1>(b
);
1455 return std::get
<3>(a
) > std::get
<3>(b
);
1458 /* If the produced cargo filter is active then move the filtered cargo to the beginning of the list,
1459 * because this is the one the player interested in, and that way it is not hidden in the 'n' more cargos */
1460 const CargoID cid
= this->cargo_filter
[this->produced_cargo_filter_criteria
];
1461 if (cid
!= CF_ANY
&& cid
!= CF_NONE
) {
1462 auto filtered_ci
= std::find_if(cargos
.begin(), cargos
.end(), [cid
](const CargoInfo
& ci
) -> bool {
1463 return std::get
<0>(ci
) == cid
;
1465 if (filtered_ci
!= cargos
.end()) {
1466 std::rotate(cargos
.begin(), filtered_ci
, filtered_ci
+ 1);
1470 /* Display first 3 cargos */
1471 for (size_t j
= 0; j
< min
<size_t>(3, cargos
.size()); j
++) {
1472 CargoInfo ci
= cargos
[j
];
1473 SetDParam(p
++, STR_INDUSTRY_DIRECTORY_ITEM_INFO
);
1474 SetDParam(p
++, std::get
<0>(ci
));
1475 SetDParam(p
++, std::get
<1>(ci
));
1476 SetDParamStr(p
++, std::get
<2>(ci
));
1477 SetDParam(p
++, std::get
<3>(ci
));
1480 /* Undisplayed cargos if any */
1481 SetDParam(p
++, cargos
.size() - 3);
1483 /* Drawing the right string */
1484 switch (cargos
.size()) {
1485 case 0: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD
;
1486 case 1: return STR_INDUSTRY_DIRECTORY_ITEM_PROD1
;
1487 case 2: return STR_INDUSTRY_DIRECTORY_ITEM_PROD2
;
1488 case 3: return STR_INDUSTRY_DIRECTORY_ITEM_PROD3
;
1489 default: return STR_INDUSTRY_DIRECTORY_ITEM_PRODMORE
;
1494 IndustryDirectoryWindow(WindowDesc
*desc
, WindowNumber number
) : Window(desc
)
1496 this->CreateNestedTree();
1497 this->vscroll
= this->GetScrollbar(WID_ID_SCROLLBAR
);
1499 this->industries
.SetListing(this->last_sorting
);
1500 this->industries
.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs
);
1501 this->industries
.ForceRebuild();
1502 this->BuildSortIndustriesList();
1504 this->FinishInitNested(0);
1507 ~IndustryDirectoryWindow()
1509 this->last_sorting
= this->industries
.GetListing();
1512 void OnInit() override
1514 this->SetCargoFilterArray();
1517 void SetStringParameters(int widget
) const override
1520 case WID_ID_DROPDOWN_CRITERIA
:
1521 SetDParam(0, IndustryDirectoryWindow::sorter_names
[this->industries
.SortType()]);
1524 case WID_ID_FILTER_BY_ACC_CARGO
:
1525 SetDParam(0, this->cargo_filter_texts
[this->accepted_cargo_filter_criteria
]);
1528 case WID_ID_FILTER_BY_PROD_CARGO
:
1529 SetDParam(0, this->cargo_filter_texts
[this->produced_cargo_filter_criteria
]);
1534 void DrawWidget(const Rect
&r
, int widget
) const override
1537 case WID_ID_DROPDOWN_ORDER
:
1538 this->DrawSortButtonState(widget
, this->industries
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
1541 case WID_ID_INDUSTRY_LIST
: {
1543 int y
= r
.top
+ WD_FRAMERECT_TOP
;
1544 if (this->industries
.size() == 0) {
1545 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_DIRECTORY_NONE
);
1549 const CargoID acf_cid
= this->cargo_filter
[this->accepted_cargo_filter_criteria
];
1550 for (uint i
= this->vscroll
->GetPosition(); i
< this->industries
.size(); i
++) {
1552 if (acf_cid
!= CF_ANY
&& acf_cid
!= CF_NONE
) {
1553 Industry
*ind
= const_cast<Industry
*>(this->industries
[i
]);
1554 if (IndustryTemporarilyRefusesCargo(ind
, acf_cid
)) {
1555 tc
= TC_GREY
| TC_FORCED
;
1558 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, this->GetIndustryString(this->industries
[i
]), tc
);
1560 y
+= this->resize
.step_height
;
1561 if (++n
== this->vscroll
->GetCapacity()) break; // max number of industries in 1 window
1568 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
1571 case WID_ID_DROPDOWN_ORDER
: {
1572 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
1573 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
1574 d
.height
+= padding
.height
;
1575 *size
= maxdim(*size
, d
);
1579 case WID_ID_DROPDOWN_CRITERIA
: {
1580 Dimension d
= {0, 0};
1581 for (uint i
= 0; IndustryDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
1582 d
= maxdim(d
, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names
[i
]));
1584 d
.width
+= padding
.width
;
1585 d
.height
+= padding
.height
;
1586 *size
= maxdim(*size
, d
);
1590 case WID_ID_INDUSTRY_LIST
: {
1591 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE
);
1592 for (uint i
= 0; i
< this->industries
.size(); i
++) {
1593 d
= maxdim(d
, GetStringBoundingBox(this->GetIndustryString(this->industries
[i
])));
1595 resize
->height
= d
.height
;
1597 d
.width
+= padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
1598 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
1599 *size
= maxdim(*size
, d
);
1606 void OnClick(Point pt
, int widget
, int click_count
) override
1609 case WID_ID_DROPDOWN_ORDER
:
1610 this->industries
.ToggleSortOrder();
1614 case WID_ID_DROPDOWN_CRITERIA
:
1615 ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names
, this->industries
.SortType(), WID_ID_DROPDOWN_CRITERIA
, 0, 0);
1618 case WID_ID_FILTER_BY_ACC_CARGO
: // Cargo filter dropdown
1619 ShowDropDownMenu(this, this->cargo_filter_texts
, this->accepted_cargo_filter_criteria
, WID_ID_FILTER_BY_ACC_CARGO
, 0, 0);
1622 case WID_ID_FILTER_BY_PROD_CARGO
: // Cargo filter dropdown
1623 ShowDropDownMenu(this, this->cargo_filter_texts
, this->produced_cargo_filter_criteria
, WID_ID_FILTER_BY_PROD_CARGO
, 0, 0);
1626 case WID_ID_INDUSTRY_LIST
: {
1627 uint p
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_ID_INDUSTRY_LIST
, WD_FRAMERECT_TOP
);
1628 if (p
< this->industries
.size()) {
1629 if (_ctrl_pressed
) {
1630 ShowExtraViewPortWindow(this->industries
[p
]->location
.tile
);
1632 ScrollMainWindowToTile(this->industries
[p
]->location
.tile
);
1640 void OnDropdownSelect(int widget
, int index
) override
1643 case WID_ID_DROPDOWN_CRITERIA
: {
1644 if (this->industries
.SortType() != index
) {
1645 this->industries
.SetSortType(index
);
1646 this->BuildSortIndustriesList();
1651 case WID_ID_FILTER_BY_ACC_CARGO
: {
1652 this->SetAcceptedCargoFilterIndex(index
);
1653 this->BuildSortIndustriesList();
1657 case WID_ID_FILTER_BY_PROD_CARGO
: {
1658 this->SetProducedCargoFilterIndex(index
);
1659 this->BuildSortIndustriesList();
1665 void OnResize() override
1667 this->vscroll
->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST
);
1670 void OnPaint() override
1672 if (this->industries
.NeedRebuild()) this->BuildSortIndustriesList();
1673 this->DrawWidgets();
1676 void OnHundredthTick() override
1678 this->industries
.ForceResort();
1679 this->BuildSortIndustriesList();
1683 * Some data on this window has become invalid.
1684 * @param data Information about the changed data.
1685 * @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.
1687 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1690 case IDIWD_FORCE_REBUILD
:
1691 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1692 this->industries
.ForceRebuild();
1695 case IDIWD_PRODUCTION_CHANGE
:
1696 if (this->industries
.SortType() == 2) this->industries
.ForceResort();
1700 this->industries
.ForceResort();
1706 Listing
IndustryDirectoryWindow::last_sorting
= {false, 0};
1708 /* Available station sorting functions. */
1709 GUIIndustryList::SortFunction
* const IndustryDirectoryWindow::sorter_funcs
[] = {
1710 &IndustryNameSorter
,
1711 &IndustryTypeSorter
,
1712 &IndustryProductionSorter
,
1713 &IndustryTransportedCargoSorter
1716 /* Names of the sorting functions */
1717 const StringID
IndustryDirectoryWindow::sorter_names
[] = {
1720 STR_SORT_BY_PRODUCTION
,
1721 STR_SORT_BY_TRANSPORTED
,
1726 /** Window definition of the industry directory gui */
1727 static WindowDesc
_industry_directory_desc(
1728 WDP_AUTO
, "list_industries", 428, 190,
1729 WC_INDUSTRY_DIRECTORY
, WC_NONE
,
1731 _nested_industry_directory_widgets
, lengthof(_nested_industry_directory_widgets
)
1734 void ShowIndustryDirectory()
1736 AllocateWindowDescFront
<IndustryDirectoryWindow
>(&_industry_directory_desc
, 0);
1739 /** Widgets of the industry cargoes window. */
1740 static const NWidgetPart _nested_industry_cargoes_widgets
[] = {
1741 NWidget(NWID_HORIZONTAL
),
1742 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1743 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_IC_CAPTION
), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1744 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1745 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1746 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1748 NWidget(NWID_HORIZONTAL
),
1749 NWidget(NWID_VERTICAL
),
1750 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_IC_PANEL
), SetResize(1, 10), SetMinimalSize(200, 90), SetScrollbar(WID_IC_SCROLLBAR
), EndContainer(),
1751 NWidget(NWID_HORIZONTAL
),
1752 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_IC_NOTIFY
),
1753 SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP
, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP
),
1754 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetFill(1, 0), SetResize(0, 0), EndContainer(),
1755 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_IND_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1756 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
, STR_INDUSTRY_CARGOES_SELECT_INDUSTRY_TOOLTIP
),
1757 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_CARGO_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1758 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_CARGO
, STR_INDUSTRY_CARGOES_SELECT_CARGO_TOOLTIP
),
1761 NWidget(NWID_VERTICAL
),
1762 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_IC_SCROLLBAR
),
1763 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1768 /** Window description for the industry cargoes window. */
1769 static WindowDesc
_industry_cargoes_desc(
1770 WDP_AUTO
, "industry_cargoes", 300, 210,
1771 WC_INDUSTRY_CARGOES
, WC_NONE
,
1773 _nested_industry_cargoes_widgets
, lengthof(_nested_industry_cargoes_widgets
)
1776 /** Available types of field. */
1777 enum CargoesFieldType
{
1778 CFT_EMPTY
, ///< Empty field.
1779 CFT_SMALL_EMPTY
, ///< Empty small field (for the header).
1780 CFT_INDUSTRY
, ///< Display industry.
1781 CFT_CARGO
, ///< Display cargo connections.
1782 CFT_CARGO_LABEL
, ///< Display cargo labels.
1783 CFT_HEADER
, ///< Header text.
1786 static const uint MAX_CARGOES
= 16; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField.
1788 /** Data about a single field in the #IndustryCargoesWindow panel. */
1789 struct CargoesField
{
1790 static const int VERT_INTER_INDUSTRY_SPACE
;
1791 static const int HOR_CARGO_BORDER_SPACE
;
1792 static const int CARGO_STUB_WIDTH
;
1793 static const int HOR_CARGO_WIDTH
, HOR_CARGO_SPACE
;
1794 static const int VERT_CARGO_SPACE
, VERT_CARGO_EDGE
;
1795 static const int BLOB_DISTANCE
, BLOB_WIDTH
, BLOB_HEIGHT
;
1797 static const int INDUSTRY_LINE_COLOUR
;
1798 static const int CARGO_LINE_COLOUR
;
1800 static int small_height
, normal_height
;
1801 static int cargo_field_width
;
1802 static int industry_width
;
1803 static uint max_cargoes
;
1805 CargoesFieldType type
; ///< Type of field.
1808 IndustryType ind_type
; ///< Industry type (#NUM_INDUSTRYTYPES means 'houses').
1809 CargoID other_produced
[MAX_CARGOES
]; ///< Cargoes produced but not used in this figure.
1810 CargoID other_accepted
[MAX_CARGOES
]; ///< Cargoes accepted but not used in this figure.
1811 } industry
; ///< Industry data (for #CFT_INDUSTRY).
1813 CargoID vertical_cargoes
[MAX_CARGOES
]; ///< Cargoes running from top to bottom (cargo ID or #INVALID_CARGO).
1814 byte num_cargoes
; ///< Number of cargoes.
1815 CargoID supp_cargoes
[MAX_CARGOES
]; ///< Cargoes entering from the left (index in #vertical_cargoes, or #INVALID_CARGO).
1816 byte top_end
; ///< Stop at the top of the vertical cargoes.
1817 CargoID cust_cargoes
[MAX_CARGOES
]; ///< Cargoes leaving to the right (index in #vertical_cargoes, or #INVALID_CARGO).
1818 byte bottom_end
; ///< Stop at the bottom of the vertical cargoes.
1819 } cargo
; ///< Cargo data (for #CFT_CARGO).
1821 CargoID cargoes
[MAX_CARGOES
]; ///< Cargoes to display (or #INVALID_CARGO).
1822 bool left_align
; ///< Align all cargo texts to the left (else align to the right).
1823 } cargo_label
; ///< Label data (for #CFT_CARGO_LABEL).
1824 StringID header
; ///< Header text (for #CFT_HEADER).
1825 } u
; // Data for each type.
1828 * Make one of the empty fields (#CFT_EMPTY or #CFT_SMALL_EMPTY).
1829 * @param type Type of empty field.
1831 void MakeEmpty(CargoesFieldType type
)
1837 * Make an industry type field.
1838 * @param ind_type Industry type (#NUM_INDUSTRYTYPES means 'houses').
1839 * @note #other_accepted and #other_produced should be filled later.
1841 void MakeIndustry(IndustryType ind_type
)
1843 this->type
= CFT_INDUSTRY
;
1844 this->u
.industry
.ind_type
= ind_type
;
1845 MemSetT(this->u
.industry
.other_accepted
, INVALID_CARGO
, MAX_CARGOES
);
1846 MemSetT(this->u
.industry
.other_produced
, INVALID_CARGO
, MAX_CARGOES
);
1850 * Connect a cargo from an industry to the #CFT_CARGO column.
1851 * @param cargo Cargo to connect.
1852 * @param producer Cargo is produced (if \c false, cargo is assumed to be accepted).
1853 * @return Horizontal connection index, or \c -1 if not accepted at all.
1855 int ConnectCargo(CargoID cargo
, bool producer
)
1857 assert(this->type
== CFT_CARGO
);
1858 if (cargo
== INVALID_CARGO
) return -1;
1860 /* Find the vertical cargo column carrying the cargo. */
1862 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
1863 if (cargo
== this->u
.cargo
.vertical_cargoes
[i
]) {
1868 if (column
< 0) return -1;
1871 assert(this->u
.cargo
.supp_cargoes
[column
] == INVALID_CARGO
);
1872 this->u
.cargo
.supp_cargoes
[column
] = column
;
1874 assert(this->u
.cargo
.cust_cargoes
[column
] == INVALID_CARGO
);
1875 this->u
.cargo
.cust_cargoes
[column
] = column
;
1881 * Does this #CFT_CARGO field have a horizontal connection?
1882 * @return \c true if a horizontal connection exists, \c false otherwise.
1884 bool HasConnection()
1886 assert(this->type
== CFT_CARGO
);
1888 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
1889 if (this->u
.cargo
.supp_cargoes
[i
] != INVALID_CARGO
) return true;
1890 if (this->u
.cargo
.cust_cargoes
[i
] != INVALID_CARGO
) return true;
1896 * Make a piece of cargo column.
1897 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
1898 * @param length Number of cargoes in \a cargoes.
1899 * @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).
1900 * @param top_end This is the first cargo field of this column.
1901 * @param bottom_end This is the last cargo field of this column.
1902 * @note #supp_cargoes and #cust_cargoes should be filled in later.
1904 void MakeCargo(const CargoID
*cargoes
, uint length
, int count
= -1, bool top_end
= false, bool bottom_end
= false)
1906 this->type
= CFT_CARGO
;
1909 for (i
= 0; i
< MAX_CARGOES
&& i
< length
; i
++) {
1910 if (cargoes
[i
] != INVALID_CARGO
) {
1911 this->u
.cargo
.vertical_cargoes
[num
] = cargoes
[i
];
1915 this->u
.cargo
.num_cargoes
= (count
< 0) ? num
: count
;
1916 for (; num
< MAX_CARGOES
; num
++) this->u
.cargo
.vertical_cargoes
[num
] = INVALID_CARGO
;
1917 this->u
.cargo
.top_end
= top_end
;
1918 this->u
.cargo
.bottom_end
= bottom_end
;
1919 MemSetT(this->u
.cargo
.supp_cargoes
, INVALID_CARGO
, MAX_CARGOES
);
1920 MemSetT(this->u
.cargo
.cust_cargoes
, INVALID_CARGO
, MAX_CARGOES
);
1924 * Make a field displaying cargo type names.
1925 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
1926 * @param length Number of cargoes in \a cargoes.
1927 * @param left_align ALign texts to the left (else to the right).
1929 void MakeCargoLabel(const CargoID
*cargoes
, uint length
, bool left_align
)
1931 this->type
= CFT_CARGO_LABEL
;
1933 for (i
= 0; i
< MAX_CARGOES
&& i
< length
; i
++) this->u
.cargo_label
.cargoes
[i
] = cargoes
[i
];
1934 for (; i
< MAX_CARGOES
; i
++) this->u
.cargo_label
.cargoes
[i
] = INVALID_CARGO
;
1935 this->u
.cargo_label
.left_align
= left_align
;
1939 * Make a header above an industry column.
1940 * @param textid Text to display.
1942 void MakeHeader(StringID textid
)
1944 this->type
= CFT_HEADER
;
1945 this->u
.header
= textid
;
1949 * For a #CFT_CARGO, compute the left position of the left-most vertical cargo connection.
1950 * @param xpos Left position of the field.
1951 * @return Left position of the left-most vertical cargo column.
1953 int GetCargoBase(int xpos
) const
1955 assert(this->type
== CFT_CARGO
);
1956 int n
= this->u
.cargo
.num_cargoes
;
1959 return xpos
+ cargo_field_width
/ 2 - (HOR_CARGO_WIDTH
+ HOR_CARGO_SPACE
/ 2) * (n
/ 2);
1961 return xpos
+ cargo_field_width
/ 2 - HOR_CARGO_WIDTH
/ 2 - (HOR_CARGO_WIDTH
+ HOR_CARGO_SPACE
) * (n
/ 2);
1967 * @param xpos Position of the left edge.
1968 * @param ypos Position of the top edge.
1970 void Draw(int xpos
, int ypos
) const
1972 switch (this->type
) {
1974 case CFT_SMALL_EMPTY
:
1978 ypos
+= (small_height
- FONT_HEIGHT_NORMAL
) / 2;
1979 DrawString(xpos
, xpos
+ industry_width
, ypos
, this->u
.header
, TC_WHITE
, SA_HOR_CENTER
);
1982 case CFT_INDUSTRY
: {
1983 int ypos1
= ypos
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
1984 int ypos2
= ypos
+ normal_height
- 1 - VERT_INTER_INDUSTRY_SPACE
/ 2;
1985 int xpos2
= xpos
+ industry_width
- 1;
1986 GfxDrawLine(xpos
, ypos1
, xpos2
, ypos1
, INDUSTRY_LINE_COLOUR
);
1987 GfxDrawLine(xpos
, ypos1
, xpos
, ypos2
, INDUSTRY_LINE_COLOUR
);
1988 GfxDrawLine(xpos
, ypos2
, xpos2
, ypos2
, INDUSTRY_LINE_COLOUR
);
1989 GfxDrawLine(xpos2
, ypos1
, xpos2
, ypos2
, INDUSTRY_LINE_COLOUR
);
1990 ypos
+= (normal_height
- FONT_HEIGHT_NORMAL
) / 2;
1991 if (this->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
1992 const IndustrySpec
*indsp
= GetIndustrySpec(this->u
.industry
.ind_type
);
1993 DrawString(xpos
, xpos2
, ypos
, indsp
->name
, TC_WHITE
, SA_HOR_CENTER
);
1995 /* Draw the industry legend. */
1996 int blob_left
, blob_right
;
1997 if (_current_text_dir
== TD_RTL
) {
1998 blob_right
= xpos2
- BLOB_DISTANCE
;
1999 blob_left
= blob_right
- BLOB_WIDTH
;
2001 blob_left
= xpos
+ BLOB_DISTANCE
;
2002 blob_right
= blob_left
+ BLOB_WIDTH
;
2004 GfxFillRect(blob_left
, ypos2
- BLOB_DISTANCE
- BLOB_HEIGHT
, blob_right
, ypos2
- BLOB_DISTANCE
, PC_BLACK
); // Border
2005 GfxFillRect(blob_left
+ 1, ypos2
- BLOB_DISTANCE
- BLOB_HEIGHT
+ 1, blob_right
- 1, ypos2
- BLOB_DISTANCE
- 1, indsp
->map_colour
);
2007 DrawString(xpos
, xpos2
, ypos
, STR_INDUSTRY_CARGOES_HOUSES
, TC_FROMSTRING
, SA_HOR_CENTER
);
2010 /* Draw the other_produced/other_accepted cargoes. */
2011 const CargoID
*other_right
, *other_left
;
2012 if (_current_text_dir
== TD_RTL
) {
2013 other_right
= this->u
.industry
.other_accepted
;
2014 other_left
= this->u
.industry
.other_produced
;
2016 other_right
= this->u
.industry
.other_produced
;
2017 other_left
= this->u
.industry
.other_accepted
;
2019 ypos1
+= VERT_CARGO_EDGE
;
2020 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2021 if (other_right
[i
] != INVALID_CARGO
) {
2022 const CargoSpec
*csp
= CargoSpec::Get(other_right
[i
]);
2023 int xp
= xpos
+ industry_width
+ CARGO_STUB_WIDTH
;
2024 DrawHorConnection(xpos
+ industry_width
, xp
- 1, ypos1
, csp
);
2025 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
2027 if (other_left
[i
] != INVALID_CARGO
) {
2028 const CargoSpec
*csp
= CargoSpec::Get(other_left
[i
]);
2029 int xp
= xpos
- CARGO_STUB_WIDTH
;
2030 DrawHorConnection(xp
+ 1, xpos
- 1, ypos1
, csp
);
2031 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
2033 ypos1
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
2039 int cargo_base
= this->GetCargoBase(xpos
);
2040 int top
= ypos
+ (this->u
.cargo
.top_end
? VERT_INTER_INDUSTRY_SPACE
/ 2 + 1 : 0);
2041 int bot
= ypos
- (this->u
.cargo
.bottom_end
? VERT_INTER_INDUSTRY_SPACE
/ 2 + 1 : 0) + normal_height
- 1;
2042 int colpos
= cargo_base
;
2043 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
2044 if (this->u
.cargo
.top_end
) GfxDrawLine(colpos
, top
- 1, colpos
+ HOR_CARGO_WIDTH
- 1, top
- 1, CARGO_LINE_COLOUR
);
2045 if (this->u
.cargo
.bottom_end
) GfxDrawLine(colpos
, bot
+ 1, colpos
+ HOR_CARGO_WIDTH
- 1, bot
+ 1, CARGO_LINE_COLOUR
);
2046 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
2048 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[i
]);
2049 GfxFillRect(colpos
, top
, colpos
+ HOR_CARGO_WIDTH
- 2, bot
, csp
->legend_colour
, FILLRECT_OPAQUE
);
2050 colpos
+= HOR_CARGO_WIDTH
- 2;
2051 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
2052 colpos
+= 1 + HOR_CARGO_SPACE
;
2055 const CargoID
*hor_left
, *hor_right
;
2056 if (_current_text_dir
== TD_RTL
) {
2057 hor_left
= this->u
.cargo
.cust_cargoes
;
2058 hor_right
= this->u
.cargo
.supp_cargoes
;
2060 hor_left
= this->u
.cargo
.supp_cargoes
;
2061 hor_right
= this->u
.cargo
.cust_cargoes
;
2063 ypos
+= VERT_CARGO_EDGE
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
2064 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
2065 if (hor_left
[i
] != INVALID_CARGO
) {
2066 int col
= hor_left
[i
];
2068 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
2069 for (; col
> 0; col
--) {
2070 int lf
= cargo_base
+ col
* HOR_CARGO_WIDTH
+ (col
- 1) * HOR_CARGO_SPACE
;
2071 DrawHorConnection(lf
, lf
+ HOR_CARGO_SPACE
- dx
, ypos
, csp
);
2074 DrawHorConnection(xpos
, cargo_base
- dx
, ypos
, csp
);
2076 if (hor_right
[i
] != INVALID_CARGO
) {
2077 int col
= hor_right
[i
];
2079 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
2080 for (; col
< this->u
.cargo
.num_cargoes
- 1; col
++) {
2081 int lf
= cargo_base
+ (col
+ 1) * HOR_CARGO_WIDTH
+ col
* HOR_CARGO_SPACE
;
2082 DrawHorConnection(lf
+ dx
- 1, lf
+ HOR_CARGO_SPACE
- 1, ypos
, csp
);
2085 DrawHorConnection(cargo_base
+ col
* HOR_CARGO_SPACE
+ (col
+ 1) * HOR_CARGO_WIDTH
- 1 + dx
, xpos
+ CargoesField::cargo_field_width
- 1, ypos
, csp
);
2087 ypos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
2092 case CFT_CARGO_LABEL
:
2093 ypos
+= VERT_CARGO_EDGE
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
2094 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
2095 if (this->u
.cargo_label
.cargoes
[i
] != INVALID_CARGO
) {
2096 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo_label
.cargoes
[i
]);
2097 DrawString(xpos
+ WD_FRAMERECT_LEFT
, xpos
+ industry_width
- 1 - WD_FRAMERECT_RIGHT
, ypos
, csp
->name
, TC_WHITE
,
2098 (this->u
.cargo_label
.left_align
) ? SA_LEFT
: SA_RIGHT
);
2100 ypos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
2110 * Decide which cargo was clicked at in a #CFT_CARGO field.
2111 * @param left Left industry neighbour if available (else \c nullptr should be supplied).
2112 * @param right Right industry neighbour if available (else \c nullptr should be supplied).
2113 * @param pt Click position in the cargo field.
2114 * @return Cargo clicked at, or #INVALID_CARGO if none.
2116 CargoID
CargoClickedAt(const CargoesField
*left
, const CargoesField
*right
, Point pt
) const
2118 assert(this->type
== CFT_CARGO
);
2120 /* Vertical matching. */
2121 int cpos
= this->GetCargoBase(0);
2123 for (col
= 0; col
< this->u
.cargo
.num_cargoes
; col
++) {
2124 if (pt
.x
< cpos
) break;
2125 if (pt
.x
< cpos
+ CargoesField::HOR_CARGO_WIDTH
) return this->u
.cargo
.vertical_cargoes
[col
];
2126 cpos
+= CargoesField::HOR_CARGO_WIDTH
+ CargoesField::HOR_CARGO_SPACE
;
2128 /* col = 0 -> left of first col, 1 -> left of 2nd col, ... this->u.cargo.num_cargoes right of last-col. */
2130 int vpos
= VERT_INTER_INDUSTRY_SPACE
/ 2 + VERT_CARGO_EDGE
;
2132 for (row
= 0; row
< MAX_CARGOES
; row
++) {
2133 if (pt
.y
< vpos
) return INVALID_CARGO
;
2134 if (pt
.y
< vpos
+ FONT_HEIGHT_NORMAL
) break;
2135 vpos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
2137 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
2139 /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
2141 if (this->u
.cargo
.supp_cargoes
[row
] != INVALID_CARGO
) return this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.supp_cargoes
[row
]];
2142 if (left
!= nullptr) {
2143 if (left
->type
== CFT_INDUSTRY
) return left
->u
.industry
.other_produced
[row
];
2144 if (left
->type
== CFT_CARGO_LABEL
&& !left
->u
.cargo_label
.left_align
) return left
->u
.cargo_label
.cargoes
[row
];
2146 return INVALID_CARGO
;
2148 if (col
== this->u
.cargo
.num_cargoes
) {
2149 if (this->u
.cargo
.cust_cargoes
[row
] != INVALID_CARGO
) return this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.cust_cargoes
[row
]];
2150 if (right
!= nullptr) {
2151 if (right
->type
== CFT_INDUSTRY
) return right
->u
.industry
.other_accepted
[row
];
2152 if (right
->type
== CFT_CARGO_LABEL
&& right
->u
.cargo_label
.left_align
) return right
->u
.cargo_label
.cargoes
[row
];
2154 return INVALID_CARGO
;
2157 /* Clicked somewhere in-between vertical cargo connection.
2158 * Since the horizontal connection is made in the same order as the vertical list, the above condition
2159 * ensures we are left-below the main diagonal, thus at the supplying side.
2161 return (this->u
.cargo
.supp_cargoes
[row
] != INVALID_CARGO
) ? this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.supp_cargoes
[row
]] : INVALID_CARGO
;
2163 /* Clicked at a customer connection. */
2164 return (this->u
.cargo
.cust_cargoes
[row
] != INVALID_CARGO
) ? this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.cust_cargoes
[row
]] : INVALID_CARGO
;
2169 * Decide what cargo the user clicked in the cargo label field.
2170 * @param pt Click position in the cargo label field.
2171 * @return Cargo clicked at, or #INVALID_CARGO if none.
2173 CargoID
CargoLabelClickedAt(Point pt
) const
2175 assert(this->type
== CFT_CARGO_LABEL
);
2177 int vpos
= VERT_INTER_INDUSTRY_SPACE
/ 2 + VERT_CARGO_EDGE
;
2179 for (row
= 0; row
< MAX_CARGOES
; row
++) {
2180 if (pt
.y
< vpos
) return INVALID_CARGO
;
2181 if (pt
.y
< vpos
+ FONT_HEIGHT_NORMAL
) break;
2182 vpos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
2184 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
2185 return this->u
.cargo_label
.cargoes
[row
];
2190 * Draw a horizontal cargo connection.
2191 * @param left Left-most coordinate to draw.
2192 * @param right Right-most coordinate to draw.
2193 * @param top Top coordinate of the cargo connection.
2194 * @param csp Cargo to draw.
2196 static void DrawHorConnection(int left
, int right
, int top
, const CargoSpec
*csp
)
2198 GfxDrawLine(left
, top
, right
, top
, CARGO_LINE_COLOUR
);
2199 GfxFillRect(left
, top
+ 1, right
, top
+ FONT_HEIGHT_NORMAL
- 2, csp
->legend_colour
, FILLRECT_OPAQUE
);
2200 GfxDrawLine(left
, top
+ FONT_HEIGHT_NORMAL
- 1, right
, top
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
2204 assert_compile(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, produced_cargo
));
2205 assert_compile(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, accepts_cargo
));
2207 int CargoesField::small_height
; ///< Height of the header row.
2208 int CargoesField::normal_height
; ///< Height of the non-header rows.
2209 int CargoesField::industry_width
; ///< Width of an industry field.
2210 int CargoesField::cargo_field_width
; ///< Width of a cargo field.
2211 uint
CargoesField::max_cargoes
; ///< Largest number of cargoes actually on any industry.
2212 const int CargoesField::VERT_INTER_INDUSTRY_SPACE
= 6; ///< Amount of space between two industries in a column.
2214 const int CargoesField::HOR_CARGO_BORDER_SPACE
= 15; ///< Amount of space between the left/right edge of a #CFT_CARGO field, and the left/right most vertical cargo.
2215 const int CargoesField::CARGO_STUB_WIDTH
= 10; ///< Width of a cargo not carried in the column (should be less than #HOR_CARGO_BORDER_SPACE).
2216 const int CargoesField::HOR_CARGO_WIDTH
= 15; ///< Width of a vertical cargo column (inclusive the border line).
2217 const int CargoesField::HOR_CARGO_SPACE
= 5; ///< Amount of horizontal space between two vertical cargoes.
2218 const int CargoesField::VERT_CARGO_EDGE
= 4; ///< Amount of vertical space between top/bottom and the top/bottom connected cargo at an industry.
2219 const int CargoesField::VERT_CARGO_SPACE
= 4; ///< Amount of vertical space between two connected cargoes at an industry.
2221 const int CargoesField::BLOB_DISTANCE
= 5; ///< Distance of the industry legend colour from the edge of the industry box.
2222 const int CargoesField::BLOB_WIDTH
= 12; ///< Width of the industry legend colour, including border.
2223 const int CargoesField::BLOB_HEIGHT
= 9; ///< Height of the industry legend colour, including border
2225 const int CargoesField::INDUSTRY_LINE_COLOUR
= PC_YELLOW
; ///< Line colour of the industry type box.
2226 const int CargoesField::CARGO_LINE_COLOUR
= PC_YELLOW
; ///< Line colour around the cargo.
2228 /** A single row of #CargoesField. */
2230 CargoesField columns
[5]; ///< One row of fields.
2233 * Connect industry production cargoes to the cargo column after it.
2234 * @param column Column of the industry.
2236 void ConnectIndustryProduced(int column
)
2238 CargoesField
*ind_fld
= this->columns
+ column
;
2239 CargoesField
*cargo_fld
= this->columns
+ column
+ 1;
2240 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
2242 MemSetT(ind_fld
->u
.industry
.other_produced
, INVALID_CARGO
, MAX_CARGOES
);
2244 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
2245 CargoID others
[MAX_CARGOES
]; // Produced cargoes not carried in the cargo column.
2246 int other_count
= 0;
2248 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
2249 assert(CargoesField::max_cargoes
<= lengthof(indsp
->produced_cargo
));
2250 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2251 int col
= cargo_fld
->ConnectCargo(indsp
->produced_cargo
[i
], true);
2252 if (col
< 0) others
[other_count
++] = indsp
->produced_cargo
[i
];
2255 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
2256 for (uint i
= 0; i
< CargoesField::max_cargoes
&& other_count
> 0; i
++) {
2257 if (cargo_fld
->u
.cargo
.supp_cargoes
[i
] == INVALID_CARGO
) ind_fld
->u
.industry
.other_produced
[i
] = others
[--other_count
];
2260 /* Houses only display what is demanded. */
2261 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2262 CargoID cid
= cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
2263 if (cid
== CT_PASSENGERS
|| cid
== CT_MAIL
) cargo_fld
->ConnectCargo(cid
, true);
2269 * Construct a #CFT_CARGO_LABEL field.
2270 * @param column Column to create the new field.
2271 * @param accepting Display accepted cargo (if \c false, display produced cargo).
2273 void MakeCargoLabel(int column
, bool accepting
)
2275 CargoID cargoes
[MAX_CARGOES
];
2276 MemSetT(cargoes
, INVALID_CARGO
, lengthof(cargoes
));
2278 CargoesField
*label_fld
= this->columns
+ column
;
2279 CargoesField
*cargo_fld
= this->columns
+ (accepting
? column
- 1 : column
+ 1);
2281 assert(cargo_fld
->type
== CFT_CARGO
&& label_fld
->type
== CFT_EMPTY
);
2282 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2283 int col
= cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], !accepting
);
2284 if (col
>= 0) cargoes
[col
] = cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
2286 label_fld
->MakeCargoLabel(cargoes
, lengthof(cargoes
), accepting
);
2291 * Connect industry accepted cargoes to the cargo column before it.
2292 * @param column Column of the industry.
2294 void ConnectIndustryAccepted(int column
)
2296 CargoesField
*ind_fld
= this->columns
+ column
;
2297 CargoesField
*cargo_fld
= this->columns
+ column
- 1;
2298 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
2300 MemSetT(ind_fld
->u
.industry
.other_accepted
, INVALID_CARGO
, MAX_CARGOES
);
2302 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
2303 CargoID others
[MAX_CARGOES
]; // Accepted cargoes not carried in the cargo column.
2304 int other_count
= 0;
2306 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
2307 assert(CargoesField::max_cargoes
<= lengthof(indsp
->accepts_cargo
));
2308 for (uint i
= 0; i
< CargoesField::max_cargoes
; i
++) {
2309 int col
= cargo_fld
->ConnectCargo(indsp
->accepts_cargo
[i
], false);
2310 if (col
< 0) others
[other_count
++] = indsp
->accepts_cargo
[i
];
2313 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
2314 for (uint i
= 0; i
< CargoesField::max_cargoes
&& other_count
> 0; i
++) {
2315 if (cargo_fld
->u
.cargo
.cust_cargoes
[i
] == INVALID_CARGO
) ind_fld
->u
.industry
.other_accepted
[i
] = others
[--other_count
];
2318 /* Houses only display what is demanded. */
2319 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
2320 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2321 HouseSpec
*hs
= HouseSpec::Get(h
);
2322 if (!hs
->enabled
) continue;
2324 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2325 if (hs
->cargo_acceptance
[j
] > 0 && cargo_fld
->u
.cargo
.vertical_cargoes
[i
] == hs
->accepts_cargo
[j
]) {
2326 cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], false);
2339 * Window displaying the cargo connections around an industry (or cargo).
2341 * The main display is constructed from 'fields', rectangles that contain an industry, piece of the cargo connection, cargo labels, or headers.
2342 * For a nice display, the following should be kept in mind:
2343 * - A #CFT_HEADER is always at the top of an column of #CFT_INDUSTRY fields.
2344 * - A #CFT_CARGO_LABEL field is also always put in a column of #CFT_INDUSTRY fields.
2345 * - The top row contains #CFT_HEADER and #CFT_SMALL_EMPTY fields.
2346 * - Cargo connections have a column of their own (#CFT_CARGO fields).
2347 * - 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.
2348 * The information however is part of the industry.
2350 * This results in the following invariants:
2351 * - Width of a #CFT_INDUSTRY column is large enough to hold all industry type labels, all cargo labels, and all header texts.
2352 * - Height of a #CFT_INDUSTRY is large enough to hold a header line, or a industry type line, \c N cargo labels
2353 * (where \c N is the maximum number of cargoes connected between industries), \c N connections of cargo types, and space
2354 * between two industry types (1/2 above it, and 1/2 underneath it).
2355 * - Width of a cargo field (#CFT_CARGO) is large enough to hold \c N vertical columns (one for each type of cargo).
2356 * Also, space is needed between an industry and the leftmost/rightmost column to draw the non-carried cargoes.
2357 * - Height of a #CFT_CARGO field is equally high as the height of the #CFT_INDUSTRY.
2358 * - A field at the top (#CFT_HEADER or #CFT_SMALL_EMPTY) match the width of the fields below them (#CFT_INDUSTRY respectively
2359 * #CFT_CARGO), the height should be sufficient to display the header text.
2361 * When displaying the cargoes around an industry type, five columns are needed (supplying industries, accepted cargoes, the industry,
2362 * produced cargoes, customer industries). Displaying the industries around a cargo needs three columns (supplying industries, the cargo,
2363 * 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.
2365 struct IndustryCargoesWindow
: public Window
{
2366 static const int HOR_TEXT_PADDING
, VERT_TEXT_PADDING
;
2368 typedef std::vector
<CargoesRow
> Fields
;
2370 Fields fields
; ///< Fields to display in the #WID_IC_PANEL.
2371 uint ind_cargo
; ///< If less than #NUM_INDUSTRYTYPES, an industry type, else a cargo id + NUM_INDUSTRYTYPES.
2372 Dimension cargo_textsize
; ///< Size to hold any cargo text, as well as STR_INDUSTRY_CARGOES_SELECT_CARGO.
2373 Dimension ind_textsize
; ///< Size to hold any industry type text, as well as STR_INDUSTRY_CARGOES_SELECT_INDUSTRY.
2376 IndustryCargoesWindow(int id
) : Window(&_industry_cargoes_desc
)
2379 this->CreateNestedTree();
2380 this->vscroll
= this->GetScrollbar(WID_IC_SCROLLBAR
);
2381 this->FinishInitNested(0);
2382 this->OnInvalidateData(id
);
2385 void OnInit() override
2387 /* Initialize static CargoesField size variables. */
2388 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS
);
2389 d
= maxdim(d
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS
));
2390 d
.width
+= WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
;
2391 d
.height
+= WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
;
2392 CargoesField::small_height
= d
.height
;
2394 /* Decide about the size of the box holding the text of an industry type. */
2395 this->ind_textsize
.width
= 0;
2396 this->ind_textsize
.height
= 0;
2397 CargoesField::max_cargoes
= 0;
2398 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2399 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2400 if (!indsp
->enabled
) continue;
2401 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(indsp
->name
));
2402 CargoesField::max_cargoes
= max
<uint
>(CargoesField::max_cargoes
, std::count_if(indsp
->accepts_cargo
, endof(indsp
->accepts_cargo
), IsCargoIDValid
));
2403 CargoesField::max_cargoes
= max
<uint
>(CargoesField::max_cargoes
, std::count_if(indsp
->produced_cargo
, endof(indsp
->produced_cargo
), IsCargoIDValid
));
2405 d
.width
= max(d
.width
, this->ind_textsize
.width
);
2406 d
.height
= this->ind_textsize
.height
;
2407 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
));
2409 /* Compute max size of the cargo texts. */
2410 this->cargo_textsize
.width
= 0;
2411 this->cargo_textsize
.height
= 0;
2412 for (uint i
= 0; i
< NUM_CARGO
; i
++) {
2413 const CargoSpec
*csp
= CargoSpec::Get(i
);
2414 if (!csp
->IsValid()) continue;
2415 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(csp
->name
));
2417 d
= maxdim(d
, this->cargo_textsize
); // Box must also be wide enough to hold any cargo label.
2418 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_CARGO
));
2420 d
.width
+= 2 * HOR_TEXT_PADDING
;
2421 /* Ensure the height is enough for the industry type text, for the horizontal connections, and for the cargo labels. */
2422 uint min_ind_height
= CargoesField::VERT_CARGO_EDGE
* 2 + CargoesField::max_cargoes
* FONT_HEIGHT_NORMAL
+ (CargoesField::max_cargoes
- 1) * CargoesField::VERT_CARGO_SPACE
;
2423 d
.height
= max(d
.height
+ 2 * VERT_TEXT_PADDING
, min_ind_height
);
2425 CargoesField::industry_width
= d
.width
;
2426 CargoesField::normal_height
= d
.height
+ CargoesField::VERT_INTER_INDUSTRY_SPACE
;
2428 /* Width of a #CFT_CARGO field. */
2429 CargoesField::cargo_field_width
= CargoesField::HOR_CARGO_BORDER_SPACE
* 2 + CargoesField::HOR_CARGO_WIDTH
* CargoesField::max_cargoes
+ CargoesField::HOR_CARGO_SPACE
* (CargoesField::max_cargoes
- 1);
2432 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
2436 size
->width
= WD_FRAMETEXT_LEFT
+ CargoesField::industry_width
* 3 + CargoesField::cargo_field_width
* 2 + WD_FRAMETEXT_RIGHT
;
2439 case WID_IC_IND_DROPDOWN
:
2440 size
->width
= max(size
->width
, this->ind_textsize
.width
+ padding
.width
);
2443 case WID_IC_CARGO_DROPDOWN
:
2444 size
->width
= max(size
->width
, this->cargo_textsize
.width
+ padding
.width
);
2450 CargoesFieldType type
; ///< Type of field.
2451 void SetStringParameters (int widget
) const override
2453 if (widget
!= WID_IC_CAPTION
) return;
2455 if (this->ind_cargo
< NUM_INDUSTRYTYPES
) {
2456 const IndustrySpec
*indsp
= GetIndustrySpec(this->ind_cargo
);
2457 SetDParam(0, indsp
->name
);
2459 const CargoSpec
*csp
= CargoSpec::Get(this->ind_cargo
- NUM_INDUSTRYTYPES
);
2460 SetDParam(0, csp
->name
);
2465 * Do the two sets of cargoes have a valid cargo in common?
2466 * @param cargoes1 Base address of the first cargo array.
2467 * @param length1 Number of cargoes in the first cargo array.
2468 * @param cargoes2 Base address of the second cargo array.
2469 * @param length2 Number of cargoes in the second cargo array.
2470 * @return Arrays have at least one valid cargo in common.
2472 static bool HasCommonValidCargo(const CargoID
*cargoes1
, uint length1
, const CargoID
*cargoes2
, uint length2
)
2474 while (length1
> 0) {
2475 if (*cargoes1
!= INVALID_CARGO
) {
2476 for (uint i
= 0; i
< length2
; i
++) if (*cargoes1
== cargoes2
[i
]) return true;
2485 * Can houses be used to supply one of the cargoes?
2486 * @param cargoes Base address of the cargo array.
2487 * @param length Number of cargoes in the array.
2488 * @return Houses can supply at least one of the cargoes.
2490 static bool HousesCanSupply(const CargoID
*cargoes
, uint length
)
2492 for (uint i
= 0; i
< length
; i
++) {
2493 if (cargoes
[i
] == INVALID_CARGO
) continue;
2494 if (cargoes
[i
] == CT_PASSENGERS
|| cargoes
[i
] == CT_MAIL
) return true;
2500 * Can houses be used as customers of the produced cargoes?
2501 * @param cargoes Base address of the cargo array.
2502 * @param length Number of cargoes in the array.
2503 * @return Houses can accept at least one of the cargoes.
2505 static bool HousesCanAccept(const CargoID
*cargoes
, uint length
)
2507 HouseZones climate_mask
;
2508 switch (_settings_game
.game_creation
.landscape
) {
2509 case LT_TEMPERATE
: climate_mask
= HZ_TEMP
; break;
2510 case LT_ARCTIC
: climate_mask
= HZ_SUBARTC_ABOVE
| HZ_SUBARTC_BELOW
; break;
2511 case LT_TROPIC
: climate_mask
= HZ_SUBTROPIC
; break;
2512 case LT_TOYLAND
: climate_mask
= HZ_TOYLND
; break;
2513 default: NOT_REACHED();
2515 for (uint i
= 0; i
< length
; i
++) {
2516 if (cargoes
[i
] == INVALID_CARGO
) continue;
2518 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2519 HouseSpec
*hs
= HouseSpec::Get(h
);
2520 if (!hs
->enabled
|| !(hs
->building_availability
& climate_mask
)) continue;
2522 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2523 if (hs
->cargo_acceptance
[j
] > 0 && cargoes
[i
] == hs
->accepts_cargo
[j
]) return true;
2531 * Count how many industries have accepted cargoes in common with one of the supplied set.
2532 * @param cargoes Cargoes to search.
2533 * @param length Number of cargoes in \a cargoes.
2534 * @return Number of industries that have an accepted cargo in common with the supplied set.
2536 static int CountMatchingAcceptingIndustries(const CargoID
*cargoes
, uint length
)
2539 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2540 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2541 if (!indsp
->enabled
) continue;
2543 if (HasCommonValidCargo(cargoes
, length
, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) count
++;
2549 * Count how many industries have produced cargoes in common with one of the supplied set.
2550 * @param cargoes Cargoes to search.
2551 * @param length Number of cargoes in \a cargoes.
2552 * @return Number of industries that have a produced cargo in common with the supplied set.
2554 static int CountMatchingProducingIndustries(const CargoID
*cargoes
, uint length
)
2557 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2558 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2559 if (!indsp
->enabled
) continue;
2561 if (HasCommonValidCargo(cargoes
, length
, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) count
++;
2567 * Shorten the cargo column to just the part between industries.
2568 * @param column Column number of the cargo column.
2569 * @param top Current top row.
2570 * @param bottom Current bottom row.
2572 void ShortenCargoColumn(int column
, int top
, int bottom
)
2574 while (top
< bottom
&& !this->fields
[top
].columns
[column
].HasConnection()) {
2575 this->fields
[top
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2578 this->fields
[top
].columns
[column
].u
.cargo
.top_end
= true;
2580 while (bottom
> top
&& !this->fields
[bottom
].columns
[column
].HasConnection()) {
2581 this->fields
[bottom
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2584 this->fields
[bottom
].columns
[column
].u
.cargo
.bottom_end
= true;
2588 * Place an industry in the fields.
2589 * @param row Row of the new industry.
2590 * @param col Column of the new industry.
2591 * @param it Industry to place.
2593 void PlaceIndustry(int row
, int col
, IndustryType it
)
2595 assert(this->fields
[row
].columns
[col
].type
== CFT_EMPTY
);
2596 this->fields
[row
].columns
[col
].MakeIndustry(it
);
2598 this->fields
[row
].ConnectIndustryProduced(col
);
2600 this->fields
[row
].ConnectIndustryAccepted(col
);
2605 * Notify smallmap that new displayed industries have been selected (in #_displayed_industries).
2607 void NotifySmallmap()
2609 if (!this->IsWidgetLowered(WID_IC_NOTIFY
)) return;
2611 /* Only notify the smallmap window if it exists. In particular, do not
2612 * bring it to the front to prevent messing up any nice layout of the user. */
2613 InvalidateWindowClassesData(WC_SMALLMAP
, 0);
2617 * Compute what and where to display for industry type \a it.
2618 * @param it Industry type to display.
2620 void ComputeIndustryDisplay(IndustryType it
)
2622 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
;
2623 this->ind_cargo
= it
;
2624 _displayed_industries
.reset();
2625 _displayed_industries
.set(it
);
2627 this->fields
.clear();
2628 /*C++17: CargoesRow &row = */ this->fields
.emplace_back();
2629 CargoesRow
&row
= this->fields
.back();
2630 row
.columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2631 row
.columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2632 row
.columns
[2].MakeEmpty(CFT_SMALL_EMPTY
);
2633 row
.columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2634 row
.columns
[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2636 const IndustrySpec
*central_sp
= GetIndustrySpec(it
);
2637 bool houses_supply
= HousesCanSupply(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2638 bool houses_accept
= HousesCanAccept(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2639 /* Make a field consisting of two cargo columns. */
2640 int num_supp
= CountMatchingProducingIndustries(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
)) + houses_supply
;
2641 int num_cust
= CountMatchingAcceptingIndustries(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
)) + houses_accept
;
2642 int num_indrows
= max(3, max(num_supp
, num_cust
)); // One is needed for the 'it' industry, and 2 for the cargo labels.
2643 for (int i
= 0; i
< num_indrows
; i
++) {
2644 /*C++17: CargoesRow &row = */ this->fields
.emplace_back();
2645 CargoesRow
&row
= this->fields
.back();
2646 row
.columns
[0].MakeEmpty(CFT_EMPTY
);
2647 row
.columns
[1].MakeCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2648 row
.columns
[2].MakeEmpty(CFT_EMPTY
);
2649 row
.columns
[3].MakeCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2650 row
.columns
[4].MakeEmpty(CFT_EMPTY
);
2652 /* Add central industry. */
2653 int central_row
= 1 + num_indrows
/ 2;
2654 this->fields
[central_row
].columns
[2].MakeIndustry(it
);
2655 this->fields
[central_row
].ConnectIndustryProduced(2);
2656 this->fields
[central_row
].ConnectIndustryAccepted(2);
2658 /* Add cargo labels. */
2659 this->fields
[central_row
- 1].MakeCargoLabel(2, true);
2660 this->fields
[central_row
+ 1].MakeCargoLabel(2, false);
2662 /* Add suppliers and customers of the 'it' industry. */
2665 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2666 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2667 if (!indsp
->enabled
) continue;
2669 if (HasCommonValidCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
), indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2670 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2671 _displayed_industries
.set(it
);
2674 if (HasCommonValidCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
), indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2675 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, it
);
2676 _displayed_industries
.set(it
);
2680 if (houses_supply
) {
2681 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2684 if (houses_accept
) {
2685 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, NUM_INDUSTRYTYPES
);
2689 this->ShortenCargoColumn(1, 1, num_indrows
);
2690 this->ShortenCargoColumn(3, 1, num_indrows
);
2691 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2692 this->vscroll
->SetCount(CeilDiv(WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
+ CargoesField::small_height
+ num_indrows
* CargoesField::normal_height
, nwp
->resize_y
));
2694 this->NotifySmallmap();
2698 * Compute what and where to display for cargo id \a cid.
2699 * @param cid Cargo id to display.
2701 void ComputeCargoDisplay(CargoID cid
)
2703 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_CARGO_CAPTION
;
2704 this->ind_cargo
= cid
+ NUM_INDUSTRYTYPES
;
2705 _displayed_industries
.reset();
2707 this->fields
.clear();
2708 /*C++17: CargoesRow &row = */ this->fields
.emplace_back();
2709 CargoesRow
&row
= this->fields
.back();
2710 row
.columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2711 row
.columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2712 row
.columns
[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2713 row
.columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2714 row
.columns
[4].MakeEmpty(CFT_SMALL_EMPTY
);
2716 bool houses_supply
= HousesCanSupply(&cid
, 1);
2717 bool houses_accept
= HousesCanAccept(&cid
, 1);
2718 int num_supp
= CountMatchingProducingIndustries(&cid
, 1) + houses_supply
+ 1; // Ensure room for the cargo label.
2719 int num_cust
= CountMatchingAcceptingIndustries(&cid
, 1) + houses_accept
;
2720 int num_indrows
= max(num_supp
, num_cust
);
2721 for (int i
= 0; i
< num_indrows
; i
++) {
2722 /*C++17: CargoesRow &row = */ this->fields
.emplace_back();
2723 CargoesRow
&row
= this->fields
.back();
2724 row
.columns
[0].MakeEmpty(CFT_EMPTY
);
2725 row
.columns
[1].MakeCargo(&cid
, 1);
2726 row
.columns
[2].MakeEmpty(CFT_EMPTY
);
2727 row
.columns
[3].MakeEmpty(CFT_EMPTY
);
2728 row
.columns
[4].MakeEmpty(CFT_EMPTY
);
2731 this->fields
[num_indrows
].MakeCargoLabel(0, false); // Add cargo labels at the left bottom.
2733 /* Add suppliers and customers of the cargo. */
2736 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2737 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2738 if (!indsp
->enabled
) continue;
2740 if (HasCommonValidCargo(&cid
, 1, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2741 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2742 _displayed_industries
.set(it
);
2745 if (HasCommonValidCargo(&cid
, 1, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2746 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, it
);
2747 _displayed_industries
.set(it
);
2751 if (houses_supply
) {
2752 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2755 if (houses_accept
) {
2756 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, NUM_INDUSTRYTYPES
);
2760 this->ShortenCargoColumn(1, 1, num_indrows
);
2761 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2762 this->vscroll
->SetCount(CeilDiv(WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
+ CargoesField::small_height
+ num_indrows
* CargoesField::normal_height
, nwp
->resize_y
));
2764 this->NotifySmallmap();
2768 * Some data on this window has become invalid.
2769 * @param data Information about the changed data.
2770 * - data = 0 .. NUM_INDUSTRYTYPES - 1: Display the chain around the given industry.
2771 * - data = NUM_INDUSTRYTYPES: Stop sending updates to the smallmap window.
2772 * @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.
2774 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
2776 if (!gui_scope
) return;
2777 if (data
== NUM_INDUSTRYTYPES
) {
2778 if (this->IsWidgetLowered(WID_IC_NOTIFY
)) {
2779 this->RaiseWidget(WID_IC_NOTIFY
);
2780 this->SetWidgetDirty(WID_IC_NOTIFY
);
2785 assert(data
>= 0 && data
< NUM_INDUSTRYTYPES
);
2786 this->ComputeIndustryDisplay(data
);
2789 void DrawWidget(const Rect
&r
, int widget
) const override
2791 if (widget
!= WID_IC_PANEL
) return;
2793 DrawPixelInfo tmp_dpi
, *old_dpi
;
2794 int width
= r
.right
- r
.left
+ 1;
2795 int height
= r
.bottom
- r
.top
+ 1 - WD_FRAMERECT_TOP
- WD_FRAMERECT_BOTTOM
;
2796 if (!FillDrawPixelInfo(&tmp_dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.top
+ WD_FRAMERECT_TOP
, width
, height
)) return;
2798 _cur_dpi
= &tmp_dpi
;
2800 int left_pos
= WD_FRAMERECT_LEFT
;
2801 if (this->ind_cargo
>= NUM_INDUSTRYTYPES
) left_pos
+= (CargoesField::industry_width
+ CargoesField::cargo_field_width
) / 2;
2802 int last_column
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
2804 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2805 int vpos
= -this->vscroll
->GetPosition() * nwp
->resize_y
;
2806 for (uint i
= 0; i
< this->fields
.size(); i
++) {
2807 int row_height
= (i
== 0) ? CargoesField::small_height
: CargoesField::normal_height
;
2808 if (vpos
+ row_height
>= 0) {
2809 int xpos
= left_pos
;
2811 if (_current_text_dir
== TD_RTL
) {
2818 while (col
>= 0 && col
<= last_column
) {
2819 this->fields
[i
].columns
[col
].Draw(xpos
, vpos
);
2820 xpos
+= (col
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
;
2825 if (vpos
>= height
) break;
2832 * Calculate in which field was clicked, and within the field, at what position.
2833 * @param pt Clicked position in the #WID_IC_PANEL widget.
2834 * @param fieldxy If \c true is returned, field x/y coordinate of \a pt.
2835 * @param xy If \c true is returned, x/y coordinate with in the field.
2836 * @return Clicked at a valid position.
2838 bool CalculatePositionInWidget(Point pt
, Point
*fieldxy
, Point
*xy
)
2840 const NWidgetBase
*nw
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2844 int vpos
= WD_FRAMERECT_TOP
+ CargoesField::small_height
- this->vscroll
->GetPosition() * nw
->resize_y
;
2845 if (pt
.y
< vpos
) return false;
2847 int row
= (pt
.y
- vpos
) / CargoesField::normal_height
; // row is relative to row 1.
2848 if (row
+ 1 >= (int)this->fields
.size()) return false;
2849 vpos
= pt
.y
- vpos
- row
* CargoesField::normal_height
; // Position in the row + 1 field
2850 row
++; // rebase row to match index of this->fields.
2852 int xpos
= 2 * WD_FRAMERECT_LEFT
+ ((this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 0 : (CargoesField::industry_width
+ CargoesField::cargo_field_width
) / 2);
2853 if (pt
.x
< xpos
) return false;
2855 for (column
= 0; column
<= 5; column
++) {
2856 int width
= (column
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
;
2857 if (pt
.x
< xpos
+ width
) break;
2860 int num_columns
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
2861 if (column
> num_columns
) return false;
2864 /* Return both positions, compensating for RTL languages (which works due to the equal symmetry in both displays). */
2867 if (_current_text_dir
== TD_RTL
) {
2868 fieldxy
->x
= num_columns
- column
;
2869 xy
->x
= ((column
& 1) ? CargoesField::cargo_field_width
: CargoesField::industry_width
) - xpos
;
2871 fieldxy
->x
= column
;
2877 void OnClick(Point pt
, int widget
, int click_count
) override
2880 case WID_IC_PANEL
: {
2882 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return;
2884 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
2885 switch (fld
->type
) {
2887 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) this->ComputeIndustryDisplay(fld
->u
.industry
.ind_type
);
2891 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : nullptr;
2892 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : nullptr;
2893 CargoID cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
2894 if (cid
!= INVALID_CARGO
) this->ComputeCargoDisplay(cid
);
2898 case CFT_CARGO_LABEL
: {
2899 CargoID cid
= fld
->CargoLabelClickedAt(xy
);
2900 if (cid
!= INVALID_CARGO
) this->ComputeCargoDisplay(cid
);
2911 this->ToggleWidgetLoweredState(WID_IC_NOTIFY
);
2912 this->SetWidgetDirty(WID_IC_NOTIFY
);
2913 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
2915 if (this->IsWidgetLowered(WID_IC_NOTIFY
)) {
2916 if (FindWindowByClass(WC_SMALLMAP
) == nullptr) ShowSmallMap();
2917 this->NotifySmallmap();
2921 case WID_IC_CARGO_DROPDOWN
: {
2923 const CargoSpec
*cs
;
2924 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
2925 lst
.emplace_back(new DropDownListStringItem(cs
->name
, cs
->Index(), false));
2928 int selected
= (this->ind_cargo
>= NUM_INDUSTRYTYPES
) ? (int)(this->ind_cargo
- NUM_INDUSTRYTYPES
) : -1;
2929 ShowDropDownList(this, std::move(lst
), selected
, WID_IC_CARGO_DROPDOWN
, 0, true);
2934 case WID_IC_IND_DROPDOWN
: {
2936 for (IndustryType ind
: _sorted_industry_types
) {
2937 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
2938 if (!indsp
->enabled
) continue;
2939 lst
.emplace_back(new DropDownListStringItem(indsp
->name
, ind
, false));
2942 int selected
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? (int)this->ind_cargo
: -1;
2943 ShowDropDownList(this, std::move(lst
), selected
, WID_IC_IND_DROPDOWN
, 0, true);
2950 void OnDropdownSelect(int widget
, int index
) override
2952 if (index
< 0) return;
2955 case WID_IC_CARGO_DROPDOWN
:
2956 this->ComputeCargoDisplay(index
);
2959 case WID_IC_IND_DROPDOWN
:
2960 this->ComputeIndustryDisplay(index
);
2965 bool OnTooltip(Point pt
, int widget
, TooltipCloseCondition close_cond
) override
2967 if (widget
!= WID_IC_PANEL
) return false;
2970 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return false;
2972 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
2973 CargoID cid
= INVALID_CARGO
;
2974 switch (fld
->type
) {
2976 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : nullptr;
2977 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : nullptr;
2978 cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
2982 case CFT_CARGO_LABEL
: {
2983 cid
= fld
->CargoLabelClickedAt(xy
);
2988 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
&& (this->ind_cargo
>= NUM_INDUSTRYTYPES
|| fieldxy
.x
!= 2)) {
2989 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP
, 0, nullptr, close_cond
);
2996 if (cid
!= INVALID_CARGO
&& (this->ind_cargo
< NUM_INDUSTRYTYPES
|| cid
!= this->ind_cargo
- NUM_INDUSTRYTYPES
)) {
2997 const CargoSpec
*csp
= CargoSpec::Get(cid
);
2999 params
[0] = csp
->name
;
3000 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP
, 1, params
, close_cond
);
3007 void OnResize() override
3009 this->vscroll
->SetCapacityFromWidget(this, WID_IC_PANEL
);
3013 const int IndustryCargoesWindow::HOR_TEXT_PADDING
= 5; ///< Horizontal padding around the industry type text.
3014 const int IndustryCargoesWindow::VERT_TEXT_PADDING
= 5; ///< Vertical padding around the industry type text.
3017 * Open the industry and cargoes window.
3018 * @param id Industry type to display, \c NUM_INDUSTRYTYPES selects a default industry type.
3020 static void ShowIndustryCargoesWindow(IndustryType id
)
3022 if (id
>= NUM_INDUSTRYTYPES
) {
3023 for (IndustryType ind
: _sorted_industry_types
) {
3024 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
3025 if (indsp
->enabled
) {
3030 if (id
>= NUM_INDUSTRYTYPES
) return;
3033 Window
*w
= BringWindowToFrontById(WC_INDUSTRY_CARGOES
, 0);
3035 w
->InvalidateData(id
);
3038 new IndustryCargoesWindow(id
);
3041 /** Open the industry and cargoes window with an industry. */
3042 void ShowIndustryCargoesWindow()
3044 ShowIndustryCargoesWindow(NUM_INDUSTRYTYPES
);