4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file industry_gui.cpp GUIs related to industries. */
15 #include "settings_gui.h"
16 #include "sound_func.h"
17 #include "window_func.h"
18 #include "textbuf_gui.h"
19 #include "command_func.h"
20 #include "viewport_func.h"
23 #include "cheat_type.h"
24 #include "newgrf_industries.h"
25 #include "newgrf_text.h"
26 #include "newgrf_debug.h"
27 #include "strings_func.h"
28 #include "company_func.h"
29 #include "tilehighlight_func.h"
30 #include "string_func.h"
31 #include "sortlist_type.h"
32 #include "widgets/dropdown_func.h"
33 #include "company_base.h"
34 #include "core/geometry_func.hpp"
35 #include "core/random_func.hpp"
36 #include "core/backup_type.hpp"
38 #include "smallmap_gui.h"
39 #include "widgets/dropdown_type.h"
40 #include "widgets/industry_widget.h"
42 #include "table/strings.h"
44 bool _ignore_restrictions
;
45 uint64 _displayed_industries
; ///< Communication from the industry chain window to the smallmap window about what industries to display.
47 assert_compile(NUM_INDUSTRYTYPES
<= 64); // Make sure all industry types fit in _displayed_industries.
49 /** Cargo suffix type (for which window is it requested) */
50 enum CargoSuffixType
{
51 CST_FUND
, ///< Fund-industry window
52 CST_VIEW
, ///< View-industry window
53 CST_DIR
, ///< Industry-directory window
56 static void ShowIndustryCargoesWindow(IndustryType id
);
59 * Gets the string to display after the cargo name (using callback 37)
60 * @param cargo the cargo for which the suffix is requested
61 * - 00 - first accepted cargo type
62 * - 01 - second accepted cargo type
63 * - 02 - third accepted cargo type
64 * - 03 - first produced cargo type
65 * - 04 - second produced cargo type
66 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
67 * @param ind the industry (NULL if in fund window)
68 * @param ind_type the industry type
69 * @param indspec the industry spec
70 * @param suffix is filled with the string to display
71 * @param suffix_last lastof(suffix)
73 static void GetCargoSuffix(uint cargo
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, char *suffix
, const char *suffix_last
)
76 if (HasBit(indspec
->callback_mask
, CBM_IND_CARGO_SUFFIX
)) {
77 uint16 callback
= GetIndustryCallback(CBID_INDUSTRY_CARGO_SUFFIX
, 0, (cst
<< 8) | cargo
, const_cast<Industry
*>(ind
), ind_type
, (cst
!= CST_FUND
) ? ind
->location
.tile
: INVALID_TILE
);
78 if (callback
== CALLBACK_FAILED
|| callback
== 0x400) return;
79 if (callback
> 0x400) {
80 ErrorUnknownCallbackResult(indspec
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_CARGO_SUFFIX
, callback
);
81 } else if (indspec
->grf_prop
.grffile
->grf_version
>= 8 || GB(callback
, 0, 8) != 0xFF) {
82 StartTextRefStackUsage(6);
83 GetString(suffix
, GetGRFStringID(indspec
->grf_prop
.grffile
->grfid
, 0xD000 + callback
), suffix_last
);
84 StopTextRefStackUsage();
90 * Gets all strings to display after the cargoes of industries (using callback 37)
91 * @param cb_offset The offset for the cargo used in cb37, 0 for accepted cargoes, 3 for produced cargoes
92 * @param cst the cargo suffix type (for which window is it requested). @see CargoSuffixType
93 * @param ind the industry (NULL if in fund window)
94 * @param ind_type the industry type
95 * @param indspec the industry spec
96 * @param cargoes array with cargotypes. for CT_INVALID no suffix will be determined
97 * @param suffixes is filled with the suffixes
99 template <typename TC
, typename TS
>
100 static inline void GetAllCargoSuffixes(uint cb_offset
, CargoSuffixType cst
, const Industry
*ind
, IndustryType ind_type
, const IndustrySpec
*indspec
, const TC
&cargoes
, TS
&suffixes
)
102 assert_compile(lengthof(cargoes
) <= lengthof(suffixes
));
103 for (uint j
= 0; j
< lengthof(cargoes
); j
++) {
104 if (cargoes
[j
] != CT_INVALID
) {
105 GetCargoSuffix(cb_offset
+ j
, cst
, ind
, ind_type
, indspec
, suffixes
[j
], lastof(suffixes
[j
]));
107 suffixes
[j
][0] = '\0';
112 IndustryType _sorted_industry_types
[NUM_INDUSTRYTYPES
]; ///< Industry types sorted by name.
114 /** Sort industry types by their name. */
115 static int CDECL
IndustryTypeNameSorter(const IndustryType
*a
, const IndustryType
*b
)
117 static char industry_name
[2][64];
119 const IndustrySpec
*indsp1
= GetIndustrySpec(*a
);
120 SetDParam(0, indsp1
->name
);
121 GetString(industry_name
[0], STR_JUST_STRING
, lastof(industry_name
[0]));
123 const IndustrySpec
*indsp2
= GetIndustrySpec(*b
);
124 SetDParam(0, indsp2
->name
);
125 GetString(industry_name
[1], STR_JUST_STRING
, lastof(industry_name
[1]));
127 int r
= strnatcmp(industry_name
[0], industry_name
[1]); // Sort by name (natural sorting).
129 /* If the names are equal, sort by industry type. */
130 return (r
!= 0) ? r
: (*a
- *b
);
134 * Initialize the list of sorted industry types.
136 void SortIndustryTypes()
138 /* Add each industry type to the list. */
139 for (IndustryType i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
140 _sorted_industry_types
[i
] = i
;
143 /* Sort industry types by name. */
144 QSortT(_sorted_industry_types
, NUM_INDUSTRYTYPES
, &IndustryTypeNameSorter
);
148 * Command callback. In case of failure to build an industry, show an error message.
149 * @param result Result of the command.
150 * @param tile Tile where the industry is placed.
151 * @param p1 Additional data of the #CMD_BUILD_INDUSTRY command.
152 * @param p2 Additional data of the #CMD_BUILD_INDUSTRY command.
154 void CcBuildIndustry(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
156 if (result
.Succeeded()) return;
158 uint8 indtype
= GB(p1
, 0, 8);
159 if (indtype
< NUM_INDUSTRYTYPES
) {
160 const IndustrySpec
*indsp
= GetIndustrySpec(indtype
);
161 if (indsp
->enabled
) {
162 SetDParam(0, indsp
->name
);
163 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, result
.GetErrorMessage(), WL_INFO
, TileX(tile
) * TILE_SIZE
, TileY(tile
) * TILE_SIZE
);
168 static const NWidgetPart _nested_build_industry_widgets
[] = {
169 NWidget(NWID_HORIZONTAL
),
170 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
171 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FUND_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
172 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
173 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
174 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
176 NWidget(NWID_HORIZONTAL
),
177 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
),
178 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_DPI_SCROLLBAR
),
180 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_DPI_INFOPANEL
), SetResize(1, 0),
182 NWidget(NWID_HORIZONTAL
),
183 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_DISPLAY_WIDGET
), SetFill(1, 0), SetResize(1, 0),
184 SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
185 NWidget(WWT_TEXTBTN
, COLOUR_DARK_GREEN
, WID_DPI_FUND_WIDGET
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING
, STR_NULL
),
186 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
190 /** Window definition of the dynamic place industries gui */
191 static WindowDesc
_build_industry_desc(
192 WDP_AUTO
, "build_industry", 170, 212,
193 WC_BUILD_INDUSTRY
, WC_NONE
,
195 _nested_build_industry_widgets
, lengthof(_nested_build_industry_widgets
)
198 /** Build (fund or prospect) a new industry, */
199 class BuildIndustryWindow
: public Window
{
200 int selected_index
; ///< index of the element in the matrix
201 IndustryType selected_type
; ///< industry corresponding to the above index
202 uint16 callback_timer
; ///< timer counter for callback eventual verification
203 bool timer_enabled
; ///< timer can be used
204 uint16 count
; ///< How many industries are loaded
205 IndustryType index
[NUM_INDUSTRYTYPES
+ 1]; ///< Type of industry, in the order it was loaded
206 bool enabled
[NUM_INDUSTRYTYPES
+ 1]; ///< availability state, coming from CBID_INDUSTRY_PROBABILITY (if ever)
209 /** The offset for the text in the matrix. */
210 static const int MATRIX_TEXT_OFFSET
= 17;
216 for (uint i
= 0; i
< lengthof(this->index
); i
++) {
217 this->index
[i
] = INVALID_INDUSTRYTYPE
;
218 this->enabled
[i
] = false;
221 if (_game_mode
== GM_EDITOR
) { // give room for the Many Random "button"
222 this->index
[this->count
] = INVALID_INDUSTRYTYPE
;
223 this->enabled
[this->count
] = true;
225 this->timer_enabled
= false;
227 /* Fill the arrays with industries.
228 * The tests performed after the enabled allow to load the industries
229 * In the same way they are inserted by grf (if any)
231 for (uint8 i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
232 IndustryType ind
= _sorted_industry_types
[i
];
233 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
234 if (indsp
->enabled
) {
235 /* Rule is that editor mode loads all industries.
236 * In game mode, all non raw industries are loaded too
237 * and raw ones are loaded only when setting allows it */
238 if (_game_mode
!= GM_EDITOR
&& indsp
->IsRawIndustry() && _settings_game
.construction
.raw_industry_construction
== 0) {
239 /* Unselect if the industry is no longer in the list */
240 if (this->selected_type
== ind
) this->selected_index
= -1;
243 this->index
[this->count
] = ind
;
244 this->enabled
[this->count
] = (_game_mode
== GM_EDITOR
) || GetIndustryProbabilityCallback(ind
, IACT_USERCREATION
, 1) > 0;
245 /* Keep the selection to the correct line */
246 if (this->selected_type
== ind
) this->selected_index
= this->count
;
251 /* first industry type is selected if the current selection is invalid.
252 * I'll be damned if there are none available ;) */
253 if (this->selected_index
== -1) {
254 this->selected_index
= 0;
255 this->selected_type
= this->index
[0];
258 this->vscroll
->SetCount(this->count
);
261 /** Update status of the fund and display-chain widgets. */
264 this->SetWidgetDisabledState(WID_DPI_FUND_WIDGET
, this->selected_type
!= INVALID_INDUSTRYTYPE
&& !this->enabled
[this->selected_index
]);
265 this->SetWidgetDisabledState(WID_DPI_DISPLAY_WIDGET
, this->selected_type
== INVALID_INDUSTRYTYPE
&& this->enabled
[this->selected_index
]);
269 BuildIndustryWindow() : Window(&_build_industry_desc
)
271 this->timer_enabled
= _loaded_newgrf_features
.has_newindustries
;
273 this->selected_index
= -1;
274 this->selected_type
= INVALID_INDUSTRYTYPE
;
276 this->callback_timer
= DAY_TICKS
;
278 this->CreateNestedTree();
279 this->vscroll
= this->GetScrollbar(WID_DPI_SCROLLBAR
);
280 this->FinishInitNested(0);
285 virtual void OnInit()
290 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
293 case WID_DPI_MATRIX_WIDGET
: {
294 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
);
295 for (byte i
= 0; i
< this->count
; i
++) {
296 if (this->index
[i
] == INVALID_INDUSTRYTYPE
) continue;
297 d
= maxdim(d
, GetStringBoundingBox(GetIndustrySpec(this->index
[i
])->name
));
299 resize
->height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
300 d
.width
+= MATRIX_TEXT_OFFSET
+ padding
.width
;
301 d
.height
= 5 * resize
->height
;
302 *size
= maxdim(*size
, d
);
306 case WID_DPI_INFOPANEL
: {
307 /* Extra line for cost outside of editor + extra lines for 'extra' information for NewGRFs. */
308 int height
= 2 + (_game_mode
== GM_EDITOR
? 0 : 1) + (_loaded_newgrf_features
.has_newindustries
? 4 : 0);
309 Dimension d
= {0, 0};
310 for (byte i
= 0; i
< this->count
; i
++) {
311 if (this->index
[i
] == INVALID_INDUSTRYTYPE
) continue;
313 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[i
]);
315 char cargo_suffix
[3][512];
316 GetAllCargoSuffixes(0, CST_FUND
, NULL
, this->index
[i
], indsp
, indsp
->accepts_cargo
, cargo_suffix
);
317 StringID str
= STR_INDUSTRY_VIEW_REQUIRES_CARGO
;
319 SetDParam(0, STR_JUST_NOTHING
);
321 for (byte j
= 0; j
< lengthof(indsp
->accepts_cargo
); j
++) {
322 if (indsp
->accepts_cargo
[j
] == CT_INVALID
) continue;
324 SetDParam(p
++, CargoSpec::Get(indsp
->accepts_cargo
[j
])->name
);
325 SetDParamStr(p
++, cargo_suffix
[j
]);
327 d
= maxdim(d
, GetStringBoundingBox(str
));
329 /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
330 GetAllCargoSuffixes(3, CST_FUND
, NULL
, this->index
[i
], indsp
, indsp
->produced_cargo
, cargo_suffix
);
331 str
= STR_INDUSTRY_VIEW_PRODUCES_CARGO
;
333 SetDParam(0, STR_JUST_NOTHING
);
335 for (byte j
= 0; j
< lengthof(indsp
->produced_cargo
); j
++) {
336 if (indsp
->produced_cargo
[j
] == CT_INVALID
) continue;
338 SetDParam(p
++, CargoSpec::Get(indsp
->produced_cargo
[j
])->name
);
339 SetDParamStr(p
++, cargo_suffix
[j
]);
341 d
= maxdim(d
, GetStringBoundingBox(str
));
344 /* Set it to something more sane :) */
345 size
->height
= height
* FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
346 size
->width
= d
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
350 case WID_DPI_FUND_WIDGET
: {
351 Dimension d
= GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
352 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
));
353 d
= maxdim(d
, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
));
354 d
.width
+= padding
.width
;
355 d
.height
+= padding
.height
;
356 *size
= maxdim(*size
, d
);
362 virtual void SetStringParameters(int widget
) const
365 case WID_DPI_FUND_WIDGET
:
366 /* Raw industries might be prospected. Show this fact by changing the string
367 * In Editor, you just build, while ingame, or you fund or you prospect */
368 if (_game_mode
== GM_EDITOR
) {
369 /* We've chosen many random industries but no industries have been specified */
370 SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY
);
372 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[this->selected_index
]);
373 SetDParam(0, (_settings_game
.construction
.raw_industry_construction
== 2 && indsp
->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY
: STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY
);
379 virtual void DrawWidget(const Rect
&r
, int widget
) const
382 case WID_DPI_MATRIX_WIDGET
: {
383 uint text_left
, text_right
, icon_left
, icon_right
;
384 if (_current_text_dir
== TD_RTL
) {
385 icon_right
= r
.right
- WD_MATRIX_RIGHT
;
386 icon_left
= icon_right
- 10;
387 text_right
= icon_right
- BuildIndustryWindow::MATRIX_TEXT_OFFSET
;
388 text_left
= r
.left
+ WD_MATRIX_LEFT
;
390 icon_left
= r
.left
+ WD_MATRIX_LEFT
;
391 icon_right
= icon_left
+ 10;
392 text_left
= icon_left
+ BuildIndustryWindow::MATRIX_TEXT_OFFSET
;
393 text_right
= r
.right
- WD_MATRIX_RIGHT
;
396 for (byte i
= 0; i
< this->vscroll
->GetCapacity() && i
+ this->vscroll
->GetPosition() < this->count
; i
++) {
397 int y
= r
.top
+ WD_MATRIX_TOP
+ i
* this->resize
.step_height
;
398 bool selected
= this->selected_index
== i
+ this->vscroll
->GetPosition();
400 if (this->index
[i
+ this->vscroll
->GetPosition()] == INVALID_INDUSTRYTYPE
) {
401 DrawString(text_left
, text_right
, y
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES
, selected
? TC_WHITE
: TC_ORANGE
);
404 const IndustrySpec
*indsp
= GetIndustrySpec(this->index
[i
+ this->vscroll
->GetPosition()]);
406 /* Draw the name of the industry in white is selected, otherwise, in orange */
407 DrawString(text_left
, text_right
, y
, indsp
->name
, selected
? TC_WHITE
: TC_ORANGE
);
408 GfxFillRect(icon_left
, y
+ 1, icon_right
, y
+ 7, selected
? PC_WHITE
: PC_BLACK
);
409 GfxFillRect(icon_left
+ 1, y
+ 2, icon_right
- 1, y
+ 6, indsp
->map_colour
);
414 case WID_DPI_INFOPANEL
: {
415 int y
= r
.top
+ WD_FRAMERECT_TOP
;
416 int bottom
= r
.bottom
- WD_FRAMERECT_BOTTOM
;
417 int left
= r
.left
+ WD_FRAMERECT_LEFT
;
418 int right
= r
.right
- WD_FRAMERECT_RIGHT
;
420 if (this->selected_type
== INVALID_INDUSTRYTYPE
) {
421 DrawStringMultiLine(left
, right
, y
, bottom
, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP
);
425 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
427 if (_game_mode
!= GM_EDITOR
) {
428 SetDParam(0, indsp
->GetConstructionCost());
429 DrawString(left
, right
, y
, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST
);
430 y
+= FONT_HEIGHT_NORMAL
;
433 /* Draw the accepted cargoes, if any. Otherwise, will print "Nothing". */
434 char cargo_suffix
[3][512];
435 GetAllCargoSuffixes(0, CST_FUND
, NULL
, this->selected_type
, indsp
, indsp
->accepts_cargo
, cargo_suffix
);
436 StringID str
= STR_INDUSTRY_VIEW_REQUIRES_CARGO
;
438 SetDParam(0, STR_JUST_NOTHING
);
440 for (byte j
= 0; j
< lengthof(indsp
->accepts_cargo
); j
++) {
441 if (indsp
->accepts_cargo
[j
] == CT_INVALID
) continue;
443 SetDParam(p
++, CargoSpec::Get(indsp
->accepts_cargo
[j
])->name
);
444 SetDParamStr(p
++, cargo_suffix
[j
]);
446 DrawString(left
, right
, y
, str
);
447 y
+= FONT_HEIGHT_NORMAL
;
449 /* Draw the produced cargoes, if any. Otherwise, will print "Nothing". */
450 GetAllCargoSuffixes(3, CST_FUND
, NULL
, this->selected_type
, indsp
, indsp
->produced_cargo
, cargo_suffix
);
451 str
= STR_INDUSTRY_VIEW_PRODUCES_CARGO
;
453 SetDParam(0, STR_JUST_NOTHING
);
455 for (byte j
= 0; j
< lengthof(indsp
->produced_cargo
); j
++) {
456 if (indsp
->produced_cargo
[j
] == CT_INVALID
) continue;
458 SetDParam(p
++, CargoSpec::Get(indsp
->produced_cargo
[j
])->name
);
459 SetDParamStr(p
++, cargo_suffix
[j
]);
461 DrawString(left
, right
, y
, str
);
462 y
+= FONT_HEIGHT_NORMAL
;
464 /* Get the additional purchase info text, if it has not already been queried. */
466 if (HasBit(indsp
->callback_mask
, CBM_IND_FUND_MORE_TEXT
)) {
467 uint16 callback_res
= GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT
, 0, 0, NULL
, this->selected_type
, INVALID_TILE
);
468 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
469 if (callback_res
> 0x400) {
470 ErrorUnknownCallbackResult(indsp
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_FUND_MORE_TEXT
, callback_res
);
472 str
= GetGRFStringID(indsp
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
); // No. here's the new string
473 if (str
!= STR_UNDEFINED
) {
474 StartTextRefStackUsage(6);
475 DrawStringMultiLine(left
, right
, y
, bottom
, str
, TC_YELLOW
);
476 StopTextRefStackUsage();
486 virtual void OnClick(Point pt
, int widget
, int click_count
)
489 case WID_DPI_MATRIX_WIDGET
: {
490 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_DPI_MATRIX_WIDGET
);
491 if (y
< this->count
) { // Is it within the boundaries of available data?
492 this->selected_index
= y
;
493 this->selected_type
= this->index
[y
];
494 const IndustrySpec
*indsp
= (this->selected_type
== INVALID_INDUSTRYTYPE
) ? NULL
: GetIndustrySpec(this->selected_type
);
498 if (_thd
.GetCallbackWnd() == this &&
499 ((_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && indsp
!= NULL
&& indsp
->IsRawIndustry()) ||
500 this->selected_type
== INVALID_INDUSTRYTYPE
||
501 !this->enabled
[this->selected_index
])) {
502 /* Reset the button state if going to prospecting or "build many industries" */
503 this->RaiseButtons();
504 ResetObjectToPlace();
508 if (this->enabled
[this->selected_index
] && click_count
> 1) this->OnClick(pt
, WID_DPI_FUND_WIDGET
, 1);
513 case WID_DPI_DISPLAY_WIDGET
:
514 if (this->selected_type
!= INVALID_INDUSTRYTYPE
) ShowIndustryCargoesWindow(this->selected_type
);
517 case WID_DPI_FUND_WIDGET
: {
518 if (this->selected_type
== INVALID_INDUSTRYTYPE
) {
519 this->HandleButtonClick(WID_DPI_FUND_WIDGET
);
521 if (Town::GetNumItems() == 0) {
522 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
);
524 extern void GenerateIndustries();
525 _generating_world
= true;
526 GenerateIndustries();
527 _generating_world
= false;
529 } else if (_game_mode
!= GM_EDITOR
&& _settings_game
.construction
.raw_industry_construction
== 2 && GetIndustrySpec(this->selected_type
)->IsRawIndustry()) {
530 DoCommandP(0, this->selected_type
, InteractiveRandom(), CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
));
531 this->HandleButtonClick(WID_DPI_FUND_WIDGET
);
533 HandlePlacePushButton(this, WID_DPI_FUND_WIDGET
, SPR_CURSOR_INDUSTRY
, HT_RECT
);
540 virtual void OnResize()
542 /* Adjust the number of items in the matrix depending of the resize */
543 this->vscroll
->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET
);
546 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
549 /* We do not need to protect ourselves against "Random Many Industries" in this mode */
550 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
551 uint32 seed
= InteractiveRandom();
553 if (_game_mode
== GM_EDITOR
) {
554 /* Show error if no town exists at all */
555 if (Town::GetNumItems() == 0) {
556 SetDParam(0, indsp
->name
);
557 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE
, STR_ERROR_MUST_FOUND_TOWN_FIRST
, WL_INFO
, pt
.x
, pt
.y
);
561 Backup
<CompanyByte
> cur_company(_current_company
, OWNER_NONE
, FILE_LINE
);
562 _generating_world
= true;
563 _ignore_restrictions
= true;
565 DoCommandP(tile
, (InteractiveRandomRange(indsp
->num_table
) << 8) | this->selected_type
, seed
,
566 CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
), &CcBuildIndustry
);
568 cur_company
.Restore();
569 _ignore_restrictions
= false;
570 _generating_world
= false;
572 success
= DoCommandP(tile
, (InteractiveRandomRange(indsp
->num_table
) << 8) | this->selected_type
, seed
, CMD_BUILD_INDUSTRY
| CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY
));
575 /* If an industry has been built, just reset the cursor and the system */
576 if (success
&& !_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
579 virtual void OnTick()
581 if (_pause_mode
!= PM_UNPAUSED
) return;
582 if (!this->timer_enabled
) return;
583 if (--this->callback_timer
== 0) {
584 /* We have just passed another day.
585 * See if we need to update availability of currently selected industry */
586 this->callback_timer
= DAY_TICKS
; // restart counter
588 const IndustrySpec
*indsp
= GetIndustrySpec(this->selected_type
);
590 if (indsp
->enabled
) {
591 bool call_back_result
= GetIndustryProbabilityCallback(this->selected_type
, IACT_USERCREATION
, 1) > 0;
593 /* Only if result does match the previous state would it require a redraw. */
594 if (call_back_result
!= this->enabled
[this->selected_index
]) {
595 this->enabled
[this->selected_index
] = call_back_result
;
603 virtual void OnTimeout()
605 this->RaiseButtons();
608 virtual void OnPlaceObjectAbort()
610 this->RaiseButtons();
614 * Some data on this window has become invalid.
615 * @param data Information about the changed data.
616 * @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.
618 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
620 if (!gui_scope
) return;
623 const IndustrySpec
*indsp
= (this->selected_type
== INVALID_INDUSTRYTYPE
) ? NULL
: GetIndustrySpec(this->selected_type
);
624 if (indsp
== NULL
) this->enabled
[this->selected_index
] = _settings_game
.difficulty
.industry_density
!= ID_FUND_ONLY
;
629 void ShowBuildIndustryWindow()
631 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
632 if (BringWindowToFrontById(WC_BUILD_INDUSTRY
, 0)) return;
633 new BuildIndustryWindow();
636 static void UpdateIndustryProduction(Industry
*i
);
638 static inline bool IsProductionAlterable(const Industry
*i
)
640 const IndustrySpec
*is
= GetIndustrySpec(i
->type
);
641 return ((_game_mode
== GM_EDITOR
|| _cheats
.setup_prod
.value
) &&
642 (is
->production_rate
[0] != 0 || is
->production_rate
[1] != 0 || is
->IsRawIndustry()));
645 class IndustryViewWindow
: public Window
647 /** Modes for changing production */
649 EA_NONE
, ///< Not alterable
650 EA_MULTIPLIER
, ///< Allow changing the production multiplier
651 EA_RATE
, ///< Allow changing the production rates
654 /** Specific lines in the info panel */
656 IL_NONE
, ///< No line
657 IL_MULTIPLIER
, ///< Production multiplier
658 IL_RATE1
, ///< Production rate of cargo 1
659 IL_RATE2
, ///< Production rate of cargo 2
662 Editability editable
; ///< Mode for changing production
663 InfoLine editbox_line
; ///< The line clicked to open the edit box
664 InfoLine clicked_line
; ///< The line of the button that has been clicked
665 byte clicked_button
; ///< The button that has been clicked (to raise)
666 int production_offset_y
; ///< The offset of the production texts/buttons
667 int info_height
; ///< Height needed for the #WID_IV_INFO panel
670 IndustryViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
672 this->flags
|= WF_DISABLE_VP_SCROLL
;
673 this->editbox_line
= IL_NONE
;
674 this->clicked_line
= IL_NONE
;
675 this->clicked_button
= 0;
676 this->info_height
= WD_FRAMERECT_TOP
+ 2 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
+ 1; // Info panel has at least two lines text.
678 this->InitNested(window_number
);
679 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
680 nvp
->InitializeViewport(this, Industry::Get(window_number
)->location
.GetCenterTile(), ZOOM_LVL_INDUSTRY
);
682 this->InvalidateData();
685 virtual void OnPaint()
689 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
691 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_IV_INFO
);
692 uint expected
= this->DrawInfo(nwi
->pos_x
, nwi
->pos_x
+ nwi
->current_x
- 1, nwi
->pos_y
) - nwi
->pos_y
;
693 if (expected
> nwi
->current_y
- 1) {
694 this->info_height
= expected
+ 1;
701 * Draw the text in the #WID_IV_INFO panel.
702 * @param left Left edge of the panel.
703 * @param right Right edge of the panel.
704 * @param top Top edge of the panel.
705 * @return Expected position of the bottom edge of the panel.
707 int DrawInfo(uint left
, uint right
, uint top
)
709 Industry
*i
= Industry::Get(this->window_number
);
710 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
711 int y
= top
+ WD_FRAMERECT_TOP
;
713 bool has_accept
= false;
714 char cargo_suffix
[3][512];
716 if (i
->prod_level
== PRODLEVEL_CLOSURE
) {
717 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_INDUSTRY_ANNOUNCED_CLOSURE
);
718 y
+= 2 * FONT_HEIGHT_NORMAL
;
721 if (HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_CARGO_ARRIVAL
) || HasBit(ind
->callback_mask
, CBM_IND_PRODUCTION_256_TICKS
)) {
722 GetAllCargoSuffixes(0, CST_VIEW
, i
, i
->type
, ind
, i
->accepts_cargo
, cargo_suffix
);
723 for (byte j
= 0; j
< lengthof(i
->accepts_cargo
); j
++) {
724 if (i
->accepts_cargo
[j
] == CT_INVALID
) continue;
727 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING
);
728 y
+= FONT_HEIGHT_NORMAL
;
731 SetDParam(0, i
->accepts_cargo
[j
]);
732 SetDParam(1, i
->incoming_cargo_waiting
[j
]);
733 SetDParamStr(2, cargo_suffix
[j
]);
734 DrawString(left
+ WD_FRAMETEXT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO
);
735 y
+= FONT_HEIGHT_NORMAL
;
738 GetAllCargoSuffixes(0, CST_VIEW
, i
, i
->type
, ind
, i
->accepts_cargo
, cargo_suffix
);
739 StringID str
= STR_INDUSTRY_VIEW_REQUIRES_CARGO
;
741 for (byte j
= 0; j
< lengthof(i
->accepts_cargo
); j
++) {
742 if (i
->accepts_cargo
[j
] == CT_INVALID
) continue;
745 SetDParam(p
++, CargoSpec::Get(i
->accepts_cargo
[j
])->name
);
746 SetDParamStr(p
++, cargo_suffix
[j
]);
749 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, str
);
750 y
+= FONT_HEIGHT_NORMAL
;
754 GetAllCargoSuffixes(3, CST_VIEW
, i
, i
->type
, ind
, i
->produced_cargo
, cargo_suffix
);
756 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
757 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
759 if (has_accept
) y
+= WD_PAR_VSEP_WIDE
;
760 DrawString(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE
);
761 y
+= FONT_HEIGHT_NORMAL
;
762 if (this->editable
== EA_RATE
) this->production_offset_y
= y
;
766 SetDParam(0, i
->produced_cargo
[j
]);
767 SetDParam(1, i
->last_month_production
[j
]);
768 SetDParamStr(2, cargo_suffix
[j
]);
769 SetDParam(3, ToPercent8(i
->last_month_pct_transported
[j
]));
770 uint x
= left
+ WD_FRAMETEXT_LEFT
+ (this->editable
== EA_RATE
? SETTING_BUTTON_WIDTH
+ 10 : 0);
771 DrawString(x
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_TRANSPORTED
);
772 /* Let's put out those buttons.. */
773 if (this->editable
== EA_RATE
) {
774 DrawArrowButtons(left
+ WD_FRAMETEXT_LEFT
, y
, COLOUR_YELLOW
, (this->clicked_line
== IL_RATE1
+ j
) ? this->clicked_button
: 0,
775 i
->production_rate
[j
] > 0, i
->production_rate
[j
] < 255);
777 y
+= FONT_HEIGHT_NORMAL
;
780 /* Display production multiplier if editable */
781 if (this->editable
== EA_MULTIPLIER
) {
782 y
+= WD_PAR_VSEP_WIDE
;
783 this->production_offset_y
= y
;
784 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
785 uint x
= left
+ WD_FRAMETEXT_LEFT
+ SETTING_BUTTON_WIDTH
+ 10;
786 DrawString(x
, right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL
);
787 DrawArrowButtons(left
+ WD_FRAMETEXT_LEFT
, y
, COLOUR_YELLOW
, (this->clicked_line
== IL_MULTIPLIER
) ? this->clicked_button
: 0,
788 i
->prod_level
> PRODLEVEL_MINIMUM
, i
->prod_level
< PRODLEVEL_MAXIMUM
);
789 y
+= FONT_HEIGHT_NORMAL
;
792 /* Get the extra message for the GUI */
793 if (HasBit(ind
->callback_mask
, CBM_IND_WINDOW_MORE_TEXT
)) {
794 uint16 callback_res
= GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT
, 0, 0, i
, i
->type
, i
->location
.tile
);
795 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
796 if (callback_res
> 0x400) {
797 ErrorUnknownCallbackResult(ind
->grf_prop
.grffile
->grfid
, CBID_INDUSTRY_WINDOW_MORE_TEXT
, callback_res
);
799 StringID message
= GetGRFStringID(ind
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
);
800 if (message
!= STR_NULL
&& message
!= STR_UNDEFINED
) {
801 y
+= WD_PAR_VSEP_WIDE
;
803 StartTextRefStackUsage(6);
804 /* Use all the available space left from where we stand up to the
805 * end of the window. We ALSO enlarge the window if needed, so we
806 * can 'go' wild with the bottom of the window. */
807 y
= DrawStringMultiLine(left
+ WD_FRAMERECT_LEFT
, right
- WD_FRAMERECT_RIGHT
, y
, UINT16_MAX
, message
, TC_BLACK
);
808 StopTextRefStackUsage();
813 return y
+ WD_FRAMERECT_BOTTOM
;
816 virtual void SetStringParameters(int widget
) const
818 if (widget
== WID_IV_CAPTION
) SetDParam(0, this->window_number
);
821 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
823 if (widget
== WID_IV_INFO
) size
->height
= this->info_height
;
826 virtual void OnClick(Point pt
, int widget
, int click_count
)
830 Industry
*i
= Industry::Get(this->window_number
);
831 InfoLine line
= IL_NONE
;
833 switch (this->editable
) {
837 if (IsInsideBS(pt
.y
, this->production_offset_y
, FONT_HEIGHT_NORMAL
)) line
= IL_MULTIPLIER
;
841 if (pt
.y
>= this->production_offset_y
) {
842 int row
= (pt
.y
- this->production_offset_y
) / FONT_HEIGHT_NORMAL
;
843 for (uint j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
844 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
847 line
= (InfoLine
)(IL_RATE1
+ j
);
854 if (line
== IL_NONE
) return;
856 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(widget
);
857 int left
= nwi
->pos_x
+ WD_FRAMETEXT_LEFT
;
858 int right
= nwi
->pos_x
+ nwi
->current_x
- 1 - WD_FRAMERECT_RIGHT
;
859 if (IsInsideMM(pt
.x
, left
, left
+ SETTING_BUTTON_WIDTH
)) {
860 /* Clicked buttons, decrease or increase production */
861 byte button
= (pt
.x
< left
+ SETTING_BUTTON_WIDTH
/ 2) ? 1 : 2;
862 switch (this->editable
) {
865 if (i
->prod_level
<= PRODLEVEL_MINIMUM
) return;
866 i
->prod_level
= max
<uint
>(i
->prod_level
/ 2, PRODLEVEL_MINIMUM
);
868 if (i
->prod_level
>= PRODLEVEL_MAXIMUM
) return;
869 i
->prod_level
= minu(i
->prod_level
* 2, PRODLEVEL_MAXIMUM
);
875 if (i
->production_rate
[line
- IL_RATE1
] <= 0) return;
876 i
->production_rate
[line
- IL_RATE1
] = max(i
->production_rate
[line
- IL_RATE1
] / 2, 0);
878 if (i
->production_rate
[line
- IL_RATE1
] >= 255) return;
879 /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
880 int new_prod
= i
->production_rate
[line
- IL_RATE1
] == 0 ? 1 : i
->production_rate
[line
- IL_RATE1
] * 2;
881 i
->production_rate
[line
- IL_RATE1
] = minu(new_prod
, 255);
885 default: NOT_REACHED();
888 UpdateIndustryProduction(i
);
891 this->clicked_line
= line
;
892 this->clicked_button
= button
;
893 } else if (IsInsideMM(pt
.x
, left
+ SETTING_BUTTON_WIDTH
+ 10, right
)) {
894 /* clicked the text */
895 this->editbox_line
= line
;
896 switch (this->editable
) {
898 SetDParam(0, RoundDivSU(i
->prod_level
* 100, PRODLEVEL_DEFAULT
));
899 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION_LEVEL
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
903 SetDParam(0, i
->production_rate
[line
- IL_RATE1
] * 8);
904 ShowQueryString(STR_JUST_INT
, STR_CONFIG_GAME_PRODUCTION
, 10, this, CS_ALPHANUMERAL
, QSF_NONE
);
907 default: NOT_REACHED();
914 Industry
*i
= Industry::Get(this->window_number
);
916 ShowExtraViewPortWindow(i
->location
.GetCenterTile());
918 ScrollMainWindowToTile(i
->location
.GetCenterTile());
923 case WID_IV_DISPLAY
: {
924 Industry
*i
= Industry::Get(this->window_number
);
925 ShowIndustryCargoesWindow(i
->type
);
931 virtual void OnTimeout()
933 this->clicked_line
= IL_NONE
;
934 this->clicked_button
= 0;
938 virtual void OnResize()
940 if (this->viewport
!= NULL
) {
941 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_IV_VIEWPORT
);
942 nvp
->UpdateViewportCoordinates(this);
944 ScrollWindowToTile(Industry::Get(this->window_number
)->location
.GetCenterTile(), this, true); // Re-center viewport.
948 virtual void OnQueryTextFinished(char *str
)
950 if (StrEmpty(str
)) return;
952 Industry
*i
= Industry::Get(this->window_number
);
953 uint value
= atoi(str
);
954 switch (this->editbox_line
) {
955 case IL_NONE
: NOT_REACHED();
958 i
->prod_level
= ClampU(RoundDivSU(value
* PRODLEVEL_DEFAULT
, 100), PRODLEVEL_MINIMUM
, PRODLEVEL_MAXIMUM
);
962 i
->production_rate
[this->editbox_line
- IL_RATE1
] = ClampU(RoundDivSU(value
, 8), 0, 255);
965 UpdateIndustryProduction(i
);
970 * Some data on this window has become invalid.
971 * @param data Information about the changed data.
972 * @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.
974 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
976 if (!gui_scope
) return;
977 const Industry
*i
= Industry::Get(this->window_number
);
978 if (IsProductionAlterable(i
)) {
979 const IndustrySpec
*ind
= GetIndustrySpec(i
->type
);
980 this->editable
= ind
->UsesSmoothEconomy() ? EA_RATE
: EA_MULTIPLIER
;
982 this->editable
= EA_NONE
;
986 virtual bool IsNewGRFInspectable() const
988 return ::IsNewGRFInspectable(GSF_INDUSTRIES
, this->window_number
);
991 virtual void ShowNewGRFInspectWindow() const
993 ::ShowNewGRFInspectWindow(GSF_INDUSTRIES
, this->window_number
);
997 static void UpdateIndustryProduction(Industry
*i
)
999 const IndustrySpec
*indspec
= GetIndustrySpec(i
->type
);
1000 if (!indspec
->UsesSmoothEconomy()) i
->RecomputeProductionMultipliers();
1002 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
1003 if (i
->produced_cargo
[j
] != CT_INVALID
) {
1004 i
->last_month_production
[j
] = 8 * i
->production_rate
[j
];
1009 /** Widget definition of the view industry gui */
1010 static const NWidgetPart _nested_industry_view_widgets
[] = {
1011 NWidget(NWID_HORIZONTAL
),
1012 NWidget(WWT_CLOSEBOX
, COLOUR_CREAM
),
1013 NWidget(WWT_CAPTION
, COLOUR_CREAM
, WID_IV_CAPTION
), SetDataTip(STR_INDUSTRY_VIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1014 NWidget(WWT_DEBUGBOX
, COLOUR_CREAM
),
1015 NWidget(WWT_SHADEBOX
, COLOUR_CREAM
),
1016 NWidget(WWT_DEFSIZEBOX
, COLOUR_CREAM
),
1017 NWidget(WWT_STICKYBOX
, COLOUR_CREAM
),
1019 NWidget(WWT_PANEL
, COLOUR_CREAM
),
1020 NWidget(WWT_INSET
, COLOUR_CREAM
), SetPadding(2, 2, 2, 2),
1021 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_IV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
1024 NWidget(WWT_PANEL
, COLOUR_CREAM
, WID_IV_INFO
), SetMinimalSize(260, 2), SetResize(1, 0),
1026 NWidget(NWID_HORIZONTAL
),
1027 NWidget(WWT_PUSHTXTBTN
, COLOUR_CREAM
, WID_IV_GOTO
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION
, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP
),
1028 NWidget(WWT_PUSHTXTBTN
, COLOUR_CREAM
, WID_IV_DISPLAY
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN
, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP
),
1029 NWidget(WWT_RESIZEBOX
, COLOUR_CREAM
),
1033 /** Window definition of the view industry gui */
1034 static WindowDesc
_industry_view_desc(
1035 WDP_AUTO
, "view_industry", 260, 120,
1036 WC_INDUSTRY_VIEW
, WC_NONE
,
1038 _nested_industry_view_widgets
, lengthof(_nested_industry_view_widgets
)
1041 void ShowIndustryViewWindow(int industry
)
1043 AllocateWindowDescFront
<IndustryViewWindow
>(&_industry_view_desc
, industry
);
1046 /** Widget definition of the industry directory gui */
1047 static const NWidgetPart _nested_industry_directory_widgets
[] = {
1048 NWidget(NWID_HORIZONTAL
),
1049 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1050 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1051 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1052 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1053 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1055 NWidget(NWID_HORIZONTAL
),
1056 NWidget(NWID_VERTICAL
),
1057 NWidget(NWID_HORIZONTAL
),
1058 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_ID_DROPDOWN_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
1059 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_ID_DROPDOWN_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
1060 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetResize(1, 0), EndContainer(),
1062 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_ID_INDUSTRY_LIST
), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION
), SetResize(1, 1), SetScrollbar(WID_ID_SCROLLBAR
), EndContainer(),
1064 NWidget(NWID_VERTICAL
),
1065 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_ID_SCROLLBAR
),
1066 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1071 typedef GUIList
<const Industry
*> GUIIndustryList
;
1075 * The list of industries.
1077 class IndustryDirectoryWindow
: public Window
{
1079 /* Runtime saved values */
1080 static Listing last_sorting
;
1081 static const Industry
*last_industry
;
1083 /* Constants for sorting stations */
1084 static const StringID sorter_names
[];
1085 static GUIIndustryList::SortFunction
* const sorter_funcs
[];
1087 GUIIndustryList industries
;
1090 /** (Re)Build industries list */
1091 void BuildSortIndustriesList()
1093 if (this->industries
.NeedRebuild()) {
1094 this->industries
.Clear();
1097 FOR_ALL_INDUSTRIES(i
) {
1098 *this->industries
.Append() = i
;
1101 this->industries
.Compact();
1102 this->industries
.RebuildDone();
1103 this->vscroll
->SetCount(this->industries
.Length()); // Update scrollbar as well.
1106 if (!this->industries
.Sort()) return;
1107 IndustryDirectoryWindow::last_industry
= NULL
; // Reset name sorter sort cache
1108 this->SetWidgetDirty(WID_ID_INDUSTRY_LIST
); // Set the modified widget dirty
1112 * Returns percents of cargo transported if industry produces this cargo, else -1
1114 * @param i industry to check
1115 * @param id cargo slot
1116 * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot
1118 static inline int GetCargoTransportedPercentsIfValid(const Industry
*i
, uint id
)
1120 assert(id
< lengthof(i
->produced_cargo
));
1122 if (i
->produced_cargo
[id
] == CT_INVALID
) return 101;
1123 return ToPercent8(i
->last_month_pct_transported
[id
]);
1127 * Returns value representing industry's transported cargo
1128 * percentage for industry sorting
1130 * @param i industry to check
1131 * @return value used for sorting
1133 static int GetCargoTransportedSortValue(const Industry
*i
)
1135 int p1
= GetCargoTransportedPercentsIfValid(i
, 0);
1136 int p2
= GetCargoTransportedPercentsIfValid(i
, 1);
1138 if (p1
> p2
) Swap(p1
, p2
); // lower value has higher priority
1140 return (p1
<< 8) + p2
;
1143 /** Sort industries by name */
1144 static int CDECL
IndustryNameSorter(const Industry
* const *a
, const Industry
* const *b
)
1146 static char buf_cache
[96];
1147 static char buf
[96];
1149 SetDParam(0, (*a
)->index
);
1150 GetString(buf
, STR_INDUSTRY_NAME
, lastof(buf
));
1152 if (*b
!= last_industry
) {
1154 SetDParam(0, (*b
)->index
);
1155 GetString(buf_cache
, STR_INDUSTRY_NAME
, lastof(buf_cache
));
1158 return strnatcmp(buf
, buf_cache
); // Sort by name (natural sorting).
1161 /** Sort industries by type and name */
1162 static int CDECL
IndustryTypeSorter(const Industry
* const *a
, const Industry
* const *b
)
1165 while (it_a
!= NUM_INDUSTRYTYPES
&& (*a
)->type
!= _sorted_industry_types
[it_a
]) it_a
++;
1167 while (it_b
!= NUM_INDUSTRYTYPES
&& (*b
)->type
!= _sorted_industry_types
[it_b
]) it_b
++;
1168 int r
= it_a
- it_b
;
1169 return (r
== 0) ? IndustryNameSorter(a
, b
) : r
;
1172 /** Sort industries by production and name */
1173 static int CDECL
IndustryProductionSorter(const Industry
* const *a
, const Industry
* const *b
)
1175 uint prod_a
= 0, prod_b
= 0;
1176 for (uint i
= 0; i
< lengthof((*a
)->produced_cargo
); i
++) {
1177 if ((*a
)->produced_cargo
[i
] != CT_INVALID
) prod_a
+= (*a
)->last_month_production
[i
];
1178 if ((*b
)->produced_cargo
[i
] != CT_INVALID
) prod_b
+= (*b
)->last_month_production
[i
];
1180 int r
= prod_a
- prod_b
;
1182 return (r
== 0) ? IndustryTypeSorter(a
, b
) : r
;
1185 /** Sort industries by transported cargo and name */
1186 static int CDECL
IndustryTransportedCargoSorter(const Industry
* const *a
, const Industry
* const *b
)
1188 int r
= GetCargoTransportedSortValue(*a
) - GetCargoTransportedSortValue(*b
);
1189 return (r
== 0) ? IndustryNameSorter(a
, b
) : r
;
1193 * Get the StringID to draw and set the appropriate DParams.
1194 * @param i the industry to get the StringID of.
1195 * @return the StringID.
1197 StringID
GetIndustryString(const Industry
*i
) const
1199 const IndustrySpec
*indsp
= GetIndustrySpec(i
->type
);
1203 SetDParam(p
++, i
->index
);
1205 static char cargo_suffix
[lengthof(i
->produced_cargo
)][512];
1206 GetAllCargoSuffixes(3, CST_DIR
, i
, i
->type
, indsp
, i
->produced_cargo
, cargo_suffix
);
1208 /* Industry productions */
1209 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
1210 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
1211 SetDParam(p
++, i
->produced_cargo
[j
]);
1212 SetDParam(p
++, i
->last_month_production
[j
]);
1213 SetDParamStr(p
++, cargo_suffix
[j
]);
1216 /* Transported productions */
1217 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
1218 if (i
->produced_cargo
[j
] == CT_INVALID
) continue;
1219 SetDParam(p
++, ToPercent8(i
->last_month_pct_transported
[j
]));
1222 /* Drawing the right string */
1224 case 1: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD
;
1225 case 5: return STR_INDUSTRY_DIRECTORY_ITEM
;
1226 default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO
;
1231 IndustryDirectoryWindow(WindowDesc
*desc
, WindowNumber number
) : Window(desc
)
1233 this->CreateNestedTree();
1234 this->vscroll
= this->GetScrollbar(WID_ID_SCROLLBAR
);
1236 this->industries
.SetListing(this->last_sorting
);
1237 this->industries
.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs
);
1238 this->industries
.ForceRebuild();
1239 this->BuildSortIndustriesList();
1241 this->FinishInitNested(0);
1244 ~IndustryDirectoryWindow()
1246 this->last_sorting
= this->industries
.GetListing();
1249 virtual void SetStringParameters(int widget
) const
1251 if (widget
== WID_ID_DROPDOWN_CRITERIA
) SetDParam(0, IndustryDirectoryWindow::sorter_names
[this->industries
.SortType()]);
1254 virtual void DrawWidget(const Rect
&r
, int widget
) const
1257 case WID_ID_DROPDOWN_ORDER
:
1258 this->DrawSortButtonState(widget
, this->industries
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
1261 case WID_ID_INDUSTRY_LIST
: {
1263 int y
= r
.top
+ WD_FRAMERECT_TOP
;
1264 if (this->industries
.Length() == 0) {
1265 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_INDUSTRY_DIRECTORY_NONE
);
1268 for (uint i
= this->vscroll
->GetPosition(); i
< this->industries
.Length(); i
++) {
1269 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, this->GetIndustryString(this->industries
[i
]));
1271 y
+= this->resize
.step_height
;
1272 if (++n
== this->vscroll
->GetCapacity()) break; // max number of industries in 1 window
1279 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1282 case WID_ID_DROPDOWN_ORDER
: {
1283 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
1284 d
.width
+= padding
.width
+ WD_SORTBUTTON_ARROW_WIDTH
* 2; // Doubled since the string is centred and it also looks better.
1285 d
.height
+= padding
.height
;
1286 *size
= maxdim(*size
, d
);
1290 case WID_ID_DROPDOWN_CRITERIA
: {
1291 Dimension d
= {0, 0};
1292 for (uint i
= 0; IndustryDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
1293 d
= maxdim(d
, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names
[i
]));
1295 d
.width
+= padding
.width
;
1296 d
.height
+= padding
.height
;
1297 *size
= maxdim(*size
, d
);
1301 case WID_ID_INDUSTRY_LIST
: {
1302 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE
);
1303 for (uint i
= 0; i
< this->industries
.Length(); i
++) {
1304 d
= maxdim(d
, GetStringBoundingBox(this->GetIndustryString(this->industries
[i
])));
1306 resize
->height
= d
.height
;
1308 d
.width
+= padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
1309 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
1310 *size
= maxdim(*size
, d
);
1317 virtual void OnClick(Point pt
, int widget
, int click_count
)
1320 case WID_ID_DROPDOWN_ORDER
:
1321 this->industries
.ToggleSortOrder();
1325 case WID_ID_DROPDOWN_CRITERIA
:
1326 ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names
, this->industries
.SortType(), WID_ID_DROPDOWN_CRITERIA
, 0, 0);
1329 case WID_ID_INDUSTRY_LIST
: {
1330 uint p
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_ID_INDUSTRY_LIST
, WD_FRAMERECT_TOP
);
1331 if (p
< this->industries
.Length()) {
1332 if (_ctrl_pressed
) {
1333 ShowExtraViewPortWindow(this->industries
[p
]->location
.tile
);
1335 ScrollMainWindowToTile(this->industries
[p
]->location
.tile
);
1343 virtual void OnDropdownSelect(int widget
, int index
)
1345 if (this->industries
.SortType() != index
) {
1346 this->industries
.SetSortType(index
);
1347 this->BuildSortIndustriesList();
1351 virtual void OnResize()
1353 this->vscroll
->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST
);
1356 virtual void OnPaint()
1358 if (this->industries
.NeedRebuild()) this->BuildSortIndustriesList();
1359 this->DrawWidgets();
1362 virtual void OnHundredthTick()
1364 this->industries
.ForceResort();
1365 this->BuildSortIndustriesList();
1369 * Some data on this window has become invalid.
1370 * @param data Information about the changed data.
1371 * @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.
1373 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
1376 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
1377 this->industries
.ForceRebuild();
1379 this->industries
.ForceResort();
1384 Listing
IndustryDirectoryWindow::last_sorting
= {false, 0};
1385 const Industry
*IndustryDirectoryWindow::last_industry
= NULL
;
1387 /* Available station sorting functions. */
1388 GUIIndustryList::SortFunction
* const IndustryDirectoryWindow::sorter_funcs
[] = {
1389 &IndustryNameSorter
,
1390 &IndustryTypeSorter
,
1391 &IndustryProductionSorter
,
1392 &IndustryTransportedCargoSorter
1395 /* Names of the sorting functions */
1396 const StringID
IndustryDirectoryWindow::sorter_names
[] = {
1399 STR_SORT_BY_PRODUCTION
,
1400 STR_SORT_BY_TRANSPORTED
,
1405 /** Window definition of the industry directory gui */
1406 static WindowDesc
_industry_directory_desc(
1407 WDP_AUTO
, "list_industries", 428, 190,
1408 WC_INDUSTRY_DIRECTORY
, WC_NONE
,
1410 _nested_industry_directory_widgets
, lengthof(_nested_industry_directory_widgets
)
1413 void ShowIndustryDirectory()
1415 AllocateWindowDescFront
<IndustryDirectoryWindow
>(&_industry_directory_desc
, 0);
1418 /** Widgets of the industry cargoes window. */
1419 static const NWidgetPart _nested_industry_cargoes_widgets
[] = {
1420 NWidget(NWID_HORIZONTAL
),
1421 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
1422 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_IC_CAPTION
), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1423 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
1424 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
1425 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
1427 NWidget(NWID_HORIZONTAL
),
1428 NWidget(NWID_VERTICAL
),
1429 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_IC_PANEL
), SetResize(1, 10), SetMinimalSize(200, 90), SetScrollbar(WID_IC_SCROLLBAR
), EndContainer(),
1430 NWidget(NWID_HORIZONTAL
),
1431 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_IC_NOTIFY
),
1432 SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP
, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP
),
1433 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetFill(1, 0), SetResize(0, 0), EndContainer(),
1434 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_IND_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1435 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
, STR_INDUSTRY_CARGOES_SELECT_INDUSTRY_TOOLTIP
),
1436 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_IC_CARGO_DROPDOWN
), SetFill(0, 0), SetResize(0, 0),
1437 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_CARGO
, STR_INDUSTRY_CARGOES_SELECT_CARGO_TOOLTIP
),
1440 NWidget(NWID_VERTICAL
),
1441 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_IC_SCROLLBAR
),
1442 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
1447 /** Window description for the industry cargoes window. */
1448 static WindowDesc
_industry_cargoes_desc(
1449 WDP_AUTO
, "industry_cargoes", 300, 210,
1450 WC_INDUSTRY_CARGOES
, WC_NONE
,
1452 _nested_industry_cargoes_widgets
, lengthof(_nested_industry_cargoes_widgets
)
1455 /** Available types of field. */
1456 enum CargoesFieldType
{
1457 CFT_EMPTY
, ///< Empty field.
1458 CFT_SMALL_EMPTY
, ///< Empty small field (for the header).
1459 CFT_INDUSTRY
, ///< Display industry.
1460 CFT_CARGO
, ///< Display cargo connections.
1461 CFT_CARGO_LABEL
, ///< Display cargo labels.
1462 CFT_HEADER
, ///< Header text.
1465 static const uint MAX_CARGOES
= 3; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField.
1467 /** Data about a single field in the #IndustryCargoesWindow panel. */
1468 struct CargoesField
{
1469 static const int VERT_INTER_INDUSTRY_SPACE
;
1470 static const int HOR_CARGO_BORDER_SPACE
;
1471 static const int CARGO_STUB_WIDTH
;
1472 static const int HOR_CARGO_WIDTH
, HOR_CARGO_SPACE
;
1473 static const int CARGO_FIELD_WIDTH
;
1474 static const int VERT_CARGO_SPACE
, VERT_CARGO_EDGE
;
1475 static const int BLOB_DISTANCE
, BLOB_WIDTH
, BLOB_HEIGHT
;
1477 static const int INDUSTRY_LINE_COLOUR
;
1478 static const int CARGO_LINE_COLOUR
;
1480 static int small_height
, normal_height
;
1481 static int industry_width
;
1483 CargoesFieldType type
; ///< Type of field.
1486 IndustryType ind_type
; ///< Industry type (#NUM_INDUSTRYTYPES means 'houses').
1487 CargoID other_produced
[MAX_CARGOES
]; ///< Cargoes produced but not used in this figure.
1488 CargoID other_accepted
[MAX_CARGOES
]; ///< Cargoes accepted but not used in this figure.
1489 } industry
; ///< Industry data (for #CFT_INDUSTRY).
1491 CargoID vertical_cargoes
[MAX_CARGOES
]; ///< Cargoes running from top to bottom (cargo ID or #INVALID_CARGO).
1492 byte num_cargoes
; ///< Number of cargoes.
1493 CargoID supp_cargoes
[MAX_CARGOES
]; ///< Cargoes entering from the left (index in #vertical_cargoes, or #INVALID_CARGO).
1494 byte top_end
; ///< Stop at the top of the vertical cargoes.
1495 CargoID cust_cargoes
[MAX_CARGOES
]; ///< Cargoes leaving to the right (index in #vertical_cargoes, or #INVALID_CARGO).
1496 byte bottom_end
; ///< Stop at the bottom of the vertical cargoes.
1497 } cargo
; ///< Cargo data (for #CFT_CARGO).
1499 CargoID cargoes
[MAX_CARGOES
]; ///< Cargoes to display (or #INVALID_CARGO).
1500 bool left_align
; ///< Align all cargo texts to the left (else align to the right).
1501 } cargo_label
; ///< Label data (for #CFT_CARGO_LABEL).
1502 StringID header
; ///< Header text (for #CFT_HEADER).
1503 } u
; // Data for each type.
1506 * Make one of the empty fields (#CFT_EMPTY or #CFT_SMALL_EMPTY).
1507 * @param type Type of empty field.
1509 void MakeEmpty(CargoesFieldType type
)
1515 * Make an industry type field.
1516 * @param ind_type Industry type (#NUM_INDUSTRYTYPES means 'houses').
1517 * @note #other_accepted and #other_produced should be filled later.
1519 void MakeIndustry(IndustryType ind_type
)
1521 this->type
= CFT_INDUSTRY
;
1522 this->u
.industry
.ind_type
= ind_type
;
1523 MemSetT(this->u
.industry
.other_accepted
, INVALID_CARGO
, MAX_CARGOES
);
1524 MemSetT(this->u
.industry
.other_produced
, INVALID_CARGO
, MAX_CARGOES
);
1528 * Connect a cargo from an industry to the #CFT_CARGO column.
1529 * @param cargo Cargo to connect.
1530 * @param produced Cargo is produced (if \c false, cargo is assumed to be accepted).
1531 * @return Horizontal connection index, or \c -1 if not accepted at all.
1533 int ConnectCargo(CargoID cargo
, bool producer
)
1535 assert(this->type
== CFT_CARGO
);
1536 if (cargo
== INVALID_CARGO
) return -1;
1538 /* Find the vertical cargo column carrying the cargo. */
1540 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
1541 if (cargo
== this->u
.cargo
.vertical_cargoes
[i
]) {
1546 if (column
< 0) return -1;
1549 assert(this->u
.cargo
.supp_cargoes
[column
] == INVALID_CARGO
);
1550 this->u
.cargo
.supp_cargoes
[column
] = column
;
1552 assert(this->u
.cargo
.cust_cargoes
[column
] == INVALID_CARGO
);
1553 this->u
.cargo
.cust_cargoes
[column
] = column
;
1559 * Does this #CFT_CARGO field have a horizontal connection?
1560 * @return \c true if a horizontal connection exists, \c false otherwise.
1562 bool HasConnection()
1564 assert(this->type
== CFT_CARGO
);
1566 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
1567 if (this->u
.cargo
.supp_cargoes
[i
] != INVALID_CARGO
) return true;
1568 if (this->u
.cargo
.cust_cargoes
[i
] != INVALID_CARGO
) return true;
1574 * Make a piece of cargo column.
1575 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
1576 * @param length Number of cargoes in \a cargoes.
1577 * @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).
1578 * @param top_end This is the first cargo field of this column.
1579 * @param bottom_end This is the last cargo field of this column.
1580 * @note #supp_cargoes and #cust_cargoes should be filled in later.
1582 void MakeCargo(const CargoID
*cargoes
, uint length
, int count
= -1, bool top_end
= false, bool bottom_end
= false)
1584 this->type
= CFT_CARGO
;
1587 for (i
= 0; i
< MAX_CARGOES
&& i
< length
; i
++) {
1588 if (cargoes
[i
] != INVALID_CARGO
) {
1589 this->u
.cargo
.vertical_cargoes
[num
] = cargoes
[i
];
1593 this->u
.cargo
.num_cargoes
= (count
< 0) ? num
: count
;
1594 for (; num
< MAX_CARGOES
; num
++) this->u
.cargo
.vertical_cargoes
[num
] = INVALID_CARGO
;
1595 this->u
.cargo
.top_end
= top_end
;
1596 this->u
.cargo
.bottom_end
= bottom_end
;
1597 MemSetT(this->u
.cargo
.supp_cargoes
, INVALID_CARGO
, MAX_CARGOES
);
1598 MemSetT(this->u
.cargo
.cust_cargoes
, INVALID_CARGO
, MAX_CARGOES
);
1602 * Make a field displaying cargo type names.
1603 * @param cargoes Array of #CargoID (may contain #INVALID_CARGO).
1604 * @param length Number of cargoes in \a cargoes.
1605 * @param left_align ALign texts to the left (else to the right).
1607 void MakeCargoLabel(const CargoID
*cargoes
, uint length
, bool left_align
)
1609 this->type
= CFT_CARGO_LABEL
;
1611 for (i
= 0; i
< MAX_CARGOES
&& i
< length
; i
++) this->u
.cargo_label
.cargoes
[i
] = cargoes
[i
];
1612 for (; i
< MAX_CARGOES
; i
++) this->u
.cargo_label
.cargoes
[i
] = INVALID_CARGO
;
1613 this->u
.cargo_label
.left_align
= left_align
;
1617 * Make a header above an industry column.
1618 * @param textid Text to display.
1620 void MakeHeader(StringID textid
)
1622 this->type
= CFT_HEADER
;
1623 this->u
.header
= textid
;
1627 * For a #CFT_CARGO, compute the left position of the left-most vertical cargo connection.
1628 * @param xpos Left position of the field.
1629 * @return Left position of the left-most vertical cargo column.
1631 int GetCargoBase(int xpos
) const
1633 assert(this->type
== CFT_CARGO
);
1635 switch (this->u
.cargo
.num_cargoes
) {
1636 case 0: return xpos
+ CARGO_FIELD_WIDTH
/ 2;
1637 case 1: return xpos
+ CARGO_FIELD_WIDTH
/ 2 - HOR_CARGO_WIDTH
/ 2;
1638 case 2: return xpos
+ CARGO_FIELD_WIDTH
/ 2 - HOR_CARGO_WIDTH
- HOR_CARGO_SPACE
/ 2;
1639 case 3: return xpos
+ CARGO_FIELD_WIDTH
/ 2 - HOR_CARGO_WIDTH
- HOR_CARGO_SPACE
- HOR_CARGO_WIDTH
/ 2;
1640 default: NOT_REACHED();
1646 * @param xpos Position of the left edge.
1647 * @param vpos Position of the top edge.
1649 void Draw(int xpos
, int ypos
) const
1651 switch (this->type
) {
1653 case CFT_SMALL_EMPTY
:
1657 ypos
+= (small_height
- FONT_HEIGHT_NORMAL
) / 2;
1658 DrawString(xpos
, xpos
+ industry_width
, ypos
, this->u
.header
, TC_WHITE
, SA_HOR_CENTER
);
1661 case CFT_INDUSTRY
: {
1662 int ypos1
= ypos
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
1663 int ypos2
= ypos
+ normal_height
- 1 - VERT_INTER_INDUSTRY_SPACE
/ 2;
1664 int xpos2
= xpos
+ industry_width
- 1;
1665 GfxDrawLine(xpos
, ypos1
, xpos2
, ypos1
, INDUSTRY_LINE_COLOUR
);
1666 GfxDrawLine(xpos
, ypos1
, xpos
, ypos2
, INDUSTRY_LINE_COLOUR
);
1667 GfxDrawLine(xpos
, ypos2
, xpos2
, ypos2
, INDUSTRY_LINE_COLOUR
);
1668 GfxDrawLine(xpos2
, ypos1
, xpos2
, ypos2
, INDUSTRY_LINE_COLOUR
);
1669 ypos
+= (normal_height
- FONT_HEIGHT_NORMAL
) / 2;
1670 if (this->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
1671 const IndustrySpec
*indsp
= GetIndustrySpec(this->u
.industry
.ind_type
);
1672 SetDParam(0, indsp
->name
);
1673 DrawString(xpos
, xpos2
, ypos
, STR_JUST_STRING
, TC_WHITE
, SA_HOR_CENTER
);
1675 /* Draw the industry legend. */
1676 int blob_left
, blob_right
;
1677 if (_current_text_dir
== TD_RTL
) {
1678 blob_right
= xpos2
- BLOB_DISTANCE
;
1679 blob_left
= blob_right
- BLOB_WIDTH
;
1681 blob_left
= xpos
+ BLOB_DISTANCE
;
1682 blob_right
= blob_left
+ BLOB_WIDTH
;
1684 GfxFillRect(blob_left
, ypos2
- BLOB_DISTANCE
- BLOB_HEIGHT
, blob_right
, ypos2
- BLOB_DISTANCE
, PC_BLACK
); // Border
1685 GfxFillRect(blob_left
+ 1, ypos2
- BLOB_DISTANCE
- BLOB_HEIGHT
+ 1, blob_right
- 1, ypos2
- BLOB_DISTANCE
- 1, indsp
->map_colour
);
1687 DrawString(xpos
, xpos2
, ypos
, STR_INDUSTRY_CARGOES_HOUSES
, TC_FROMSTRING
, SA_HOR_CENTER
);
1690 /* Draw the other_produced/other_accepted cargoes. */
1691 const CargoID
*other_right
, *other_left
;
1692 if (_current_text_dir
== TD_RTL
) {
1693 other_right
= this->u
.industry
.other_accepted
;
1694 other_left
= this->u
.industry
.other_produced
;
1696 other_right
= this->u
.industry
.other_produced
;
1697 other_left
= this->u
.industry
.other_accepted
;
1699 ypos1
+= VERT_CARGO_EDGE
;
1700 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
1701 if (other_right
[i
] != INVALID_CARGO
) {
1702 const CargoSpec
*csp
= CargoSpec::Get(other_right
[i
]);
1703 int xp
= xpos
+ industry_width
+ CARGO_STUB_WIDTH
;
1704 DrawHorConnection(xpos
+ industry_width
, xp
- 1, ypos1
, csp
);
1705 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
1707 if (other_left
[i
] != INVALID_CARGO
) {
1708 const CargoSpec
*csp
= CargoSpec::Get(other_left
[i
]);
1709 int xp
= xpos
- CARGO_STUB_WIDTH
;
1710 DrawHorConnection(xp
+ 1, xpos
- 1, ypos1
, csp
);
1711 GfxDrawLine(xp
, ypos1
, xp
, ypos1
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
1713 ypos1
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
1719 int cargo_base
= this->GetCargoBase(xpos
);
1720 int top
= ypos
+ (this->u
.cargo
.top_end
? VERT_INTER_INDUSTRY_SPACE
/ 2 + 1 : 0);
1721 int bot
= ypos
- (this->u
.cargo
.bottom_end
? VERT_INTER_INDUSTRY_SPACE
/ 2 + 1 : 0) + normal_height
- 1;
1722 int colpos
= cargo_base
;
1723 for (int i
= 0; i
< this->u
.cargo
.num_cargoes
; i
++) {
1724 if (this->u
.cargo
.top_end
) GfxDrawLine(colpos
, top
- 1, colpos
+ HOR_CARGO_WIDTH
- 1, top
- 1, CARGO_LINE_COLOUR
);
1725 if (this->u
.cargo
.bottom_end
) GfxDrawLine(colpos
, bot
+ 1, colpos
+ HOR_CARGO_WIDTH
- 1, bot
+ 1, CARGO_LINE_COLOUR
);
1726 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
1728 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[i
]);
1729 GfxFillRect(colpos
, top
, colpos
+ HOR_CARGO_WIDTH
- 2, bot
, csp
->legend_colour
, FILLRECT_OPAQUE
);
1730 colpos
+= HOR_CARGO_WIDTH
- 2;
1731 GfxDrawLine(colpos
, top
, colpos
, bot
, CARGO_LINE_COLOUR
);
1732 colpos
+= 1 + HOR_CARGO_SPACE
;
1735 const CargoID
*hor_left
, *hor_right
;
1736 if (_current_text_dir
== TD_RTL
) {
1737 hor_left
= this->u
.cargo
.cust_cargoes
;
1738 hor_right
= this->u
.cargo
.supp_cargoes
;
1740 hor_left
= this->u
.cargo
.supp_cargoes
;
1741 hor_right
= this->u
.cargo
.cust_cargoes
;
1743 ypos
+= VERT_CARGO_EDGE
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
1744 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
1745 if (hor_left
[i
] != INVALID_CARGO
) {
1746 int col
= hor_left
[i
];
1748 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
1749 for (; col
> 0; col
--) {
1750 int lf
= cargo_base
+ col
* HOR_CARGO_WIDTH
+ (col
- 1) * HOR_CARGO_SPACE
;
1751 DrawHorConnection(lf
, lf
+ HOR_CARGO_SPACE
- dx
, ypos
, csp
);
1754 DrawHorConnection(xpos
, cargo_base
- dx
, ypos
, csp
);
1756 if (hor_right
[i
] != INVALID_CARGO
) {
1757 int col
= hor_right
[i
];
1759 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo
.vertical_cargoes
[col
]);
1760 for (; col
< this->u
.cargo
.num_cargoes
- 1; col
++) {
1761 int lf
= cargo_base
+ (col
+ 1) * HOR_CARGO_WIDTH
+ col
* HOR_CARGO_SPACE
;
1762 DrawHorConnection(lf
+ dx
- 1, lf
+ HOR_CARGO_SPACE
- 1, ypos
, csp
);
1765 DrawHorConnection(cargo_base
+ col
* HOR_CARGO_SPACE
+ (col
+ 1) * HOR_CARGO_WIDTH
- 1 + dx
, xpos
+ CARGO_FIELD_WIDTH
- 1, ypos
, csp
);
1767 ypos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
1772 case CFT_CARGO_LABEL
:
1773 ypos
+= VERT_CARGO_EDGE
+ VERT_INTER_INDUSTRY_SPACE
/ 2;
1774 for (uint i
= 0; i
< MAX_CARGOES
; i
++) {
1775 if (this->u
.cargo_label
.cargoes
[i
] != INVALID_CARGO
) {
1776 const CargoSpec
*csp
= CargoSpec::Get(this->u
.cargo_label
.cargoes
[i
]);
1777 DrawString(xpos
+ WD_FRAMERECT_LEFT
, xpos
+ industry_width
- 1 - WD_FRAMERECT_RIGHT
, ypos
, csp
->name
, TC_WHITE
,
1778 (this->u
.cargo_label
.left_align
) ? SA_LEFT
: SA_RIGHT
);
1780 ypos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
1790 * Decide which cargo was clicked at in a #CFT_CARGO field.
1791 * @param left Left industry neighbour if available (else \c NULL should be supplied).
1792 * @param right Right industry neighbour if available (else \c NULL should be supplied).
1793 * @param pt Click position in the cargo field.
1794 * @return Cargo clicked at, or #INVALID_CARGO if none.
1796 CargoID
CargoClickedAt(const CargoesField
*left
, const CargoesField
*right
, Point pt
) const
1798 assert(this->type
== CFT_CARGO
);
1800 /* Vertical matching. */
1801 int cpos
= this->GetCargoBase(0);
1803 for (col
= 0; col
< this->u
.cargo
.num_cargoes
; col
++) {
1804 if (pt
.x
< cpos
) break;
1805 if (pt
.x
< cpos
+ CargoesField::HOR_CARGO_WIDTH
) return this->u
.cargo
.vertical_cargoes
[col
];
1806 cpos
+= CargoesField::HOR_CARGO_WIDTH
+ CargoesField::HOR_CARGO_SPACE
;
1808 /* col = 0 -> left of first col, 1 -> left of 2nd col, ... this->u.cargo.num_cargoes right of last-col. */
1810 int vpos
= VERT_INTER_INDUSTRY_SPACE
/ 2 + VERT_CARGO_EDGE
;
1812 for (row
= 0; row
< MAX_CARGOES
; row
++) {
1813 if (pt
.y
< vpos
) return INVALID_CARGO
;
1814 if (pt
.y
< vpos
+ FONT_HEIGHT_NORMAL
) break;
1815 vpos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
1817 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
1819 /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
1821 if (this->u
.cargo
.supp_cargoes
[row
] != INVALID_CARGO
) return this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.supp_cargoes
[row
]];
1823 if (left
->type
== CFT_INDUSTRY
) return left
->u
.industry
.other_produced
[row
];
1824 if (left
->type
== CFT_CARGO_LABEL
&& !left
->u
.cargo_label
.left_align
) return left
->u
.cargo_label
.cargoes
[row
];
1826 return INVALID_CARGO
;
1828 if (col
== this->u
.cargo
.num_cargoes
) {
1829 if (this->u
.cargo
.cust_cargoes
[row
] != INVALID_CARGO
) return this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.cust_cargoes
[row
]];
1830 if (right
!= NULL
) {
1831 if (right
->type
== CFT_INDUSTRY
) return right
->u
.industry
.other_accepted
[row
];
1832 if (right
->type
== CFT_CARGO_LABEL
&& right
->u
.cargo_label
.left_align
) return right
->u
.cargo_label
.cargoes
[row
];
1834 return INVALID_CARGO
;
1837 /* Clicked somewhere in-between vertical cargo connection.
1838 * Since the horizontal connection is made in the same order as the vertical list, the above condition
1839 * ensures we are left-below the main diagonal, thus at the supplying side.
1841 return (this->u
.cargo
.supp_cargoes
[row
] != INVALID_CARGO
) ? this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.supp_cargoes
[row
]] : INVALID_CARGO
;
1843 /* Clicked at a customer connection. */
1844 return (this->u
.cargo
.cust_cargoes
[row
] != INVALID_CARGO
) ? this->u
.cargo
.vertical_cargoes
[this->u
.cargo
.cust_cargoes
[row
]] : INVALID_CARGO
;
1849 * Decide what cargo the user clicked in the cargo label field.
1850 * @param pt Click position in the cargo label field.
1851 * @return Cargo clicked at, or #INVALID_CARGO if none.
1853 CargoID
CargoLabelClickedAt(Point pt
) const
1855 assert(this->type
== CFT_CARGO_LABEL
);
1857 int vpos
= VERT_INTER_INDUSTRY_SPACE
/ 2 + VERT_CARGO_EDGE
;
1859 for (row
= 0; row
< MAX_CARGOES
; row
++) {
1860 if (pt
.y
< vpos
) return INVALID_CARGO
;
1861 if (pt
.y
< vpos
+ FONT_HEIGHT_NORMAL
) break;
1862 vpos
+= FONT_HEIGHT_NORMAL
+ VERT_CARGO_SPACE
;
1864 if (row
== MAX_CARGOES
) return INVALID_CARGO
;
1865 return this->u
.cargo_label
.cargoes
[row
];
1870 * Draw a horizontal cargo connection.
1871 * @param left Left-most coordinate to draw.
1872 * @param right Right-most coordinate to draw.
1873 * @param top Top coordinate of the cargo connection.
1874 * @param csp Cargo to draw.
1876 static void DrawHorConnection(int left
, int right
, int top
, const CargoSpec
*csp
)
1878 GfxDrawLine(left
, top
, right
, top
, CARGO_LINE_COLOUR
);
1879 GfxFillRect(left
, top
+ 1, right
, top
+ FONT_HEIGHT_NORMAL
- 2, csp
->legend_colour
, FILLRECT_OPAQUE
);
1880 GfxDrawLine(left
, top
+ FONT_HEIGHT_NORMAL
- 1, right
, top
+ FONT_HEIGHT_NORMAL
- 1, CARGO_LINE_COLOUR
);
1884 assert_compile(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, produced_cargo
));
1885 assert_compile(MAX_CARGOES
>= cpp_lengthof(IndustrySpec
, accepts_cargo
));
1887 int CargoesField::small_height
; ///< Height of the header row.
1888 int CargoesField::normal_height
; ///< Height of the non-header rows.
1889 int CargoesField::industry_width
; ///< Width of an industry field.
1890 const int CargoesField::VERT_INTER_INDUSTRY_SPACE
= 6; ///< Amount of space between two industries in a column.
1892 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.
1893 const int CargoesField::CARGO_STUB_WIDTH
= 10; ///< Width of a cargo not carried in the column (should be less than #HOR_CARGO_BORDER_SPACE).
1894 const int CargoesField::HOR_CARGO_WIDTH
= 15; ///< Width of a vertical cargo column (inclusive the border line).
1895 const int CargoesField::HOR_CARGO_SPACE
= 5; ///< Amount of horizontal space between two vertical cargoes.
1896 const int CargoesField::VERT_CARGO_EDGE
= 4; ///< Amount of vertical space between top/bottom and the top/bottom connected cargo at an industry.
1897 const int CargoesField::VERT_CARGO_SPACE
= 4; ///< Amount of vertical space between two connected cargoes at an industry.
1899 const int CargoesField::BLOB_DISTANCE
= 5; ///< Distance of the industry legend colour from the edge of the industry box.
1900 const int CargoesField::BLOB_WIDTH
= 12; ///< Width of the industry legend colour, including border.
1901 const int CargoesField::BLOB_HEIGHT
= 9; ///< Height of the industry legend colour, including border
1903 /** Width of a #CFT_CARGO field. */
1904 const int CargoesField::CARGO_FIELD_WIDTH
= HOR_CARGO_BORDER_SPACE
* 2 + HOR_CARGO_WIDTH
* MAX_CARGOES
+ HOR_CARGO_SPACE
* (MAX_CARGOES
- 1);
1906 const int CargoesField::INDUSTRY_LINE_COLOUR
= PC_YELLOW
; ///< Line colour of the industry type box.
1907 const int CargoesField::CARGO_LINE_COLOUR
= PC_YELLOW
; ///< Line colour around the cargo.
1909 /** A single row of #CargoesField. */
1911 CargoesField columns
[5]; ///< One row of fields.
1914 * Connect industry production cargoes to the cargo column after it.
1915 * @param column Column of the industry.
1917 void ConnectIndustryProduced(int column
)
1919 CargoesField
*ind_fld
= this->columns
+ column
;
1920 CargoesField
*cargo_fld
= this->columns
+ column
+ 1;
1921 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
1923 MemSetT(ind_fld
->u
.industry
.other_produced
, INVALID_CARGO
, MAX_CARGOES
);
1925 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
1926 CargoID others
[MAX_CARGOES
]; // Produced cargoes not carried in the cargo column.
1927 int other_count
= 0;
1929 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
1930 for (uint i
= 0; i
< lengthof(indsp
->produced_cargo
); i
++) {
1931 int col
= cargo_fld
->ConnectCargo(indsp
->produced_cargo
[i
], true);
1932 if (col
< 0) others
[other_count
++] = indsp
->produced_cargo
[i
];
1935 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
1936 for (uint i
= 0; i
< MAX_CARGOES
&& other_count
> 0; i
++) {
1937 if (cargo_fld
->u
.cargo
.supp_cargoes
[i
] == INVALID_CARGO
) ind_fld
->u
.industry
.other_produced
[i
] = others
[--other_count
];
1940 /* Houses only display what is demanded. */
1941 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
1942 CargoID cid
= cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
1943 if (cid
== CT_PASSENGERS
|| cid
== CT_MAIL
) cargo_fld
->ConnectCargo(cid
, true);
1949 * Construct a #CFT_CARGO_LABEL field.
1950 * @param column Column to create the new field.
1951 * @param accepting Display accepted cargo (if \c false, display produced cargo).
1953 void MakeCargoLabel(int column
, bool accepting
)
1955 CargoID cargoes
[MAX_CARGOES
];
1956 MemSetT(cargoes
, INVALID_CARGO
, lengthof(cargoes
));
1958 CargoesField
*label_fld
= this->columns
+ column
;
1959 CargoesField
*cargo_fld
= this->columns
+ (accepting
? column
- 1 : column
+ 1);
1961 assert(cargo_fld
->type
== CFT_CARGO
&& label_fld
->type
== CFT_EMPTY
);
1962 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
1963 int col
= cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], !accepting
);
1964 if (col
>= 0) cargoes
[col
] = cargo_fld
->u
.cargo
.vertical_cargoes
[i
];
1966 label_fld
->MakeCargoLabel(cargoes
, lengthof(cargoes
), accepting
);
1971 * Connect industry accepted cargoes to the cargo column before it.
1972 * @param column Column of the industry.
1974 void ConnectIndustryAccepted(int column
)
1976 CargoesField
*ind_fld
= this->columns
+ column
;
1977 CargoesField
*cargo_fld
= this->columns
+ column
- 1;
1978 assert(ind_fld
->type
== CFT_INDUSTRY
&& cargo_fld
->type
== CFT_CARGO
);
1980 MemSetT(ind_fld
->u
.industry
.other_accepted
, INVALID_CARGO
, MAX_CARGOES
);
1982 if (ind_fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) {
1983 CargoID others
[MAX_CARGOES
]; // Accepted cargoes not carried in the cargo column.
1984 int other_count
= 0;
1986 const IndustrySpec
*indsp
= GetIndustrySpec(ind_fld
->u
.industry
.ind_type
);
1987 for (uint i
= 0; i
< lengthof(indsp
->accepts_cargo
); i
++) {
1988 int col
= cargo_fld
->ConnectCargo(indsp
->accepts_cargo
[i
], false);
1989 if (col
< 0) others
[other_count
++] = indsp
->accepts_cargo
[i
];
1992 /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
1993 for (uint i
= 0; i
< MAX_CARGOES
&& other_count
> 0; i
++) {
1994 if (cargo_fld
->u
.cargo
.cust_cargoes
[i
] == INVALID_CARGO
) ind_fld
->u
.industry
.other_accepted
[i
] = others
[--other_count
];
1997 /* Houses only display what is demanded. */
1998 for (uint i
= 0; i
< cargo_fld
->u
.cargo
.num_cargoes
; i
++) {
1999 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2000 HouseSpec
*hs
= HouseSpec::Get(h
);
2001 if (!hs
->enabled
) continue;
2003 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2004 if (cargo_fld
->u
.cargo
.vertical_cargoes
[i
] == hs
->accepts_cargo
[j
]) {
2005 cargo_fld
->ConnectCargo(cargo_fld
->u
.cargo
.vertical_cargoes
[i
], false);
2018 * Window displaying the cargo connections around an industry (or cargo).
2020 * The main display is constructed from 'fields', rectangles that contain an industry, piece of the cargo connection, cargo labels, or headers.
2021 * For a nice display, the following should be kept in mind:
2022 * - A #CFT_HEADER is always at the top of an column of #CFT_INDUSTRY fields.
2023 * - A #CFT_CARGO_LABEL field is also always put in a column of #CFT_INDUSTRY fields.
2024 * - The top row contains #CFT_HEADER and #CFT_SMALL_EMPTY fields.
2025 * - Cargo connections have a column of their own (#CFT_CARGO fields).
2026 * - 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.
2027 * The information however is part of the industry.
2029 * This results in the following invariants:
2030 * - Width of a #CFT_INDUSTRY column is large enough to hold all industry type labels, all cargo labels, and all header texts.
2031 * - Height of a #CFT_INDUSTRY is large enough to hold a header line, or a industry type line, \c N cargo labels
2032 * (where \c N is the maximum number of cargoes connected between industries), \c N connections of cargo types, and space
2033 * between two industry types (1/2 above it, and 1/2 underneath it).
2034 * - Width of a cargo field (#CFT_CARGO) is large enough to hold \c N vertical columns (one for each type of cargo).
2035 * Also, space is needed between an industry and the leftmost/rightmost column to draw the non-carried cargoes.
2036 * - Height of a #CFT_CARGO field is equally high as the height of the #CFT_INDUSTRY.
2037 * - A field at the top (#CFT_HEADER or #CFT_SMALL_EMPTY) match the width of the fields below them (#CFT_INDUSTRY respectively
2038 * #CFT_CARGO), the height should be sufficient to display the header text.
2040 * When displaying the cargoes around an industry type, five columns are needed (supplying industries, accepted cargoes, the industry,
2041 * produced cargoes, customer industries). Displaying the industries around a cargo needs three columns (supplying industries, the cargo,
2042 * 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.
2044 struct IndustryCargoesWindow
: public Window
{
2045 static const int HOR_TEXT_PADDING
, VERT_TEXT_PADDING
;
2047 typedef SmallVector
<CargoesRow
, 4> Fields
;
2049 Fields fields
; ///< Fields to display in the #WID_IC_PANEL.
2050 uint ind_cargo
; ///< If less than #NUM_INDUSTRYTYPES, an industry type, else a cargo id + NUM_INDUSTRYTYPES.
2051 Dimension cargo_textsize
; ///< Size to hold any cargo text, as well as STR_INDUSTRY_CARGOES_SELECT_CARGO.
2052 Dimension ind_textsize
; ///< Size to hold any industry type text, as well as STR_INDUSTRY_CARGOES_SELECT_INDUSTRY.
2055 IndustryCargoesWindow(int id
) : Window(&_industry_cargoes_desc
)
2058 this->CreateNestedTree();
2059 this->vscroll
= this->GetScrollbar(WID_IC_SCROLLBAR
);
2060 this->FinishInitNested(0);
2061 this->OnInvalidateData(id
);
2064 virtual void OnInit()
2066 /* Initialize static CargoesField size variables. */
2067 Dimension d
= GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS
);
2068 d
= maxdim(d
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS
));
2069 d
.width
+= WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
;
2070 d
.height
+= WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
;
2071 CargoesField::small_height
= d
.height
;
2073 /* Decide about the size of the box holding the text of an industry type. */
2074 this->ind_textsize
.width
= 0;
2075 this->ind_textsize
.height
= 0;
2076 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2077 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2078 if (!indsp
->enabled
) continue;
2079 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(indsp
->name
));
2081 d
.width
= max(d
.width
, this->ind_textsize
.width
);
2082 d
.height
= this->ind_textsize
.height
;
2083 this->ind_textsize
= maxdim(this->ind_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY
));
2085 /* Compute max size of the cargo texts. */
2086 this->cargo_textsize
.width
= 0;
2087 this->cargo_textsize
.height
= 0;
2088 for (uint i
= 0; i
< NUM_CARGO
; i
++) {
2089 const CargoSpec
*csp
= CargoSpec::Get(i
);
2090 if (!csp
->IsValid()) continue;
2091 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(csp
->name
));
2093 d
= maxdim(d
, this->cargo_textsize
); // Box must also be wide enough to hold any cargo label.
2094 this->cargo_textsize
= maxdim(this->cargo_textsize
, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_CARGO
));
2096 d
.width
+= 2 * HOR_TEXT_PADDING
;
2097 /* Ensure the height is enough for the industry type text, for the horizontal connections, and for the cargo labels. */
2098 uint min_ind_height
= CargoesField::VERT_CARGO_EDGE
* 2 + MAX_CARGOES
* FONT_HEIGHT_NORMAL
+ (MAX_CARGOES
- 1) * CargoesField::VERT_CARGO_SPACE
;
2099 d
.height
= max(d
.height
+ 2 * VERT_TEXT_PADDING
, min_ind_height
);
2101 CargoesField::industry_width
= d
.width
;
2102 CargoesField::normal_height
= d
.height
+ CargoesField::VERT_INTER_INDUSTRY_SPACE
;
2105 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
2109 size
->width
= WD_FRAMETEXT_LEFT
+ CargoesField::industry_width
* 3 + CargoesField::CARGO_FIELD_WIDTH
* 2 + WD_FRAMETEXT_RIGHT
;
2112 case WID_IC_IND_DROPDOWN
:
2113 size
->width
= max(size
->width
, this->ind_textsize
.width
+ padding
.width
);
2116 case WID_IC_CARGO_DROPDOWN
:
2117 size
->width
= max(size
->width
, this->cargo_textsize
.width
+ padding
.width
);
2123 CargoesFieldType type
; ///< Type of field.
2124 virtual void SetStringParameters (int widget
) const
2126 if (widget
!= WID_IC_CAPTION
) return;
2128 if (this->ind_cargo
< NUM_INDUSTRYTYPES
) {
2129 const IndustrySpec
*indsp
= GetIndustrySpec(this->ind_cargo
);
2130 SetDParam(0, indsp
->name
);
2132 const CargoSpec
*csp
= CargoSpec::Get(this->ind_cargo
- NUM_INDUSTRYTYPES
);
2133 SetDParam(0, csp
->name
);
2138 * Do the two sets of cargoes have a valid cargo in common?
2139 * @param cargoes1 Base address of the first cargo array.
2140 * @param length1 Number of cargoes in the first cargo array.
2141 * @param cargoes2 Base address of the second cargo array.
2142 * @param length2 Number of cargoes in the second cargo array.
2143 * @return Arrays have at least one valid cargo in common.
2145 static bool HasCommonValidCargo(const CargoID
*cargoes1
, uint length1
, const CargoID
*cargoes2
, uint length2
)
2147 while (length1
> 0) {
2148 if (*cargoes1
!= INVALID_CARGO
) {
2149 for (uint i
= 0; i
< length2
; i
++) if (*cargoes1
== cargoes2
[i
]) return true;
2158 * Can houses be used to supply one of the cargoes?
2159 * @param cargoes Base address of the cargo array.
2160 * @param length Number of cargoes in the array.
2161 * @return Houses can supply at least one of the cargoes.
2163 static bool HousesCanSupply(const CargoID
*cargoes
, uint length
)
2165 for (uint i
= 0; i
< length
; i
++) {
2166 if (cargoes
[i
] == INVALID_CARGO
) continue;
2167 if (cargoes
[i
] == CT_PASSENGERS
|| cargoes
[i
] == CT_MAIL
) return true;
2173 * Can houses be used as customers of the produced cargoes?
2174 * @param cargoes Base address of the cargo array.
2175 * @param length Number of cargoes in the array.
2176 * @return Houses can accept at least one of the cargoes.
2178 static bool HousesCanAccept(const CargoID
*cargoes
, uint length
)
2180 HouseZones climate_mask
;
2181 switch (_settings_game
.game_creation
.landscape
) {
2182 case LT_TEMPERATE
: climate_mask
= HZ_TEMP
; break;
2183 case LT_ARCTIC
: climate_mask
= HZ_SUBARTC_ABOVE
| HZ_SUBARTC_BELOW
; break;
2184 case LT_TROPIC
: climate_mask
= HZ_SUBTROPIC
; break;
2185 case LT_TOYLAND
: climate_mask
= HZ_TOYLND
; break;
2186 default: NOT_REACHED();
2188 for (uint i
= 0; i
< length
; i
++) {
2189 if (cargoes
[i
] == INVALID_CARGO
) continue;
2191 for (uint h
= 0; h
< NUM_HOUSES
; h
++) {
2192 HouseSpec
*hs
= HouseSpec::Get(h
);
2193 if (!hs
->enabled
|| !(hs
->building_availability
& climate_mask
)) continue;
2195 for (uint j
= 0; j
< lengthof(hs
->accepts_cargo
); j
++) {
2196 if (cargoes
[i
] == hs
->accepts_cargo
[j
]) return true;
2204 * Count how many industries have accepted cargoes in common with one of the supplied set.
2205 * @param cargoes Cargoes to search.
2206 * @param length Number of cargoes in \a cargoes.
2207 * @return Number of industries that have an accepted cargo in common with the supplied set.
2209 static int CountMatchingAcceptingIndustries(const CargoID
*cargoes
, uint length
)
2212 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2213 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2214 if (!indsp
->enabled
) continue;
2216 if (HasCommonValidCargo(cargoes
, length
, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) count
++;
2222 * Count how many industries have produced cargoes in common with one of the supplied set.
2223 * @param cargoes Cargoes to search.
2224 * @param length Number of cargoes in \a cargoes.
2225 * @return Number of industries that have a produced cargo in common with the supplied set.
2227 static int CountMatchingProducingIndustries(const CargoID
*cargoes
, uint length
)
2230 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2231 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2232 if (!indsp
->enabled
) continue;
2234 if (HasCommonValidCargo(cargoes
, length
, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) count
++;
2240 * Shorten the cargo column to just the part between industries.
2241 * @param column Column number of the cargo column.
2242 * @param top Current top row.
2243 * @param bottom Current bottom row.
2245 void ShortenCargoColumn(int column
, int top
, int bottom
)
2247 while (top
< bottom
&& !this->fields
[top
].columns
[column
].HasConnection()) {
2248 this->fields
[top
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2251 this->fields
[top
].columns
[column
].u
.cargo
.top_end
= true;
2253 while (bottom
> top
&& !this->fields
[bottom
].columns
[column
].HasConnection()) {
2254 this->fields
[bottom
].columns
[column
].MakeEmpty(CFT_EMPTY
);
2257 this->fields
[bottom
].columns
[column
].u
.cargo
.bottom_end
= true;
2261 * Place an industry in the fields.
2262 * @param row Row of the new industry.
2263 * @param col Column of the new industry.
2264 * @param it Industry to place.
2266 void PlaceIndustry(int row
, int col
, IndustryType it
)
2268 assert(this->fields
[row
].columns
[col
].type
== CFT_EMPTY
);
2269 this->fields
[row
].columns
[col
].MakeIndustry(it
);
2271 this->fields
[row
].ConnectIndustryProduced(col
);
2273 this->fields
[row
].ConnectIndustryAccepted(col
);
2278 * Notify smallmap that new displayed industries have been selected (in #_displayed_industries).
2280 void NotifySmallmap()
2282 if (!this->IsWidgetLowered(WID_IC_NOTIFY
)) return;
2284 /* Only notify the smallmap window if it exists. In particular, do not
2285 * bring it to the front to prevent messing up any nice layout of the user. */
2286 InvalidateWindowClassesData(WC_SMALLMAP
, 0);
2290 * Compute what and where to display for industry type \a it.
2291 * @param it Industry type to display.
2293 void ComputeIndustryDisplay(IndustryType it
)
2295 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION
;
2296 this->ind_cargo
= it
;
2297 _displayed_industries
= 1ULL << it
;
2299 this->fields
.Clear();
2300 CargoesRow
*row
= this->fields
.Append();
2301 row
->columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2302 row
->columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2303 row
->columns
[2].MakeEmpty(CFT_SMALL_EMPTY
);
2304 row
->columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2305 row
->columns
[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2307 const IndustrySpec
*central_sp
= GetIndustrySpec(it
);
2308 bool houses_supply
= HousesCanSupply(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2309 bool houses_accept
= HousesCanAccept(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2310 /* Make a field consisting of two cargo columns. */
2311 int num_supp
= CountMatchingProducingIndustries(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
)) + houses_supply
;
2312 int num_cust
= CountMatchingAcceptingIndustries(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
)) + houses_accept
;
2313 int num_indrows
= max(3, max(num_supp
, num_cust
)); // One is needed for the 'it' industry, and 2 for the cargo labels.
2314 for (int i
= 0; i
< num_indrows
; i
++) {
2315 CargoesRow
*row
= this->fields
.Append();
2316 row
->columns
[0].MakeEmpty(CFT_EMPTY
);
2317 row
->columns
[1].MakeCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
));
2318 row
->columns
[2].MakeEmpty(CFT_EMPTY
);
2319 row
->columns
[3].MakeCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
));
2320 row
->columns
[4].MakeEmpty(CFT_EMPTY
);
2322 /* Add central industry. */
2323 int central_row
= 1 + num_indrows
/ 2;
2324 this->fields
[central_row
].columns
[2].MakeIndustry(it
);
2325 this->fields
[central_row
].ConnectIndustryProduced(2);
2326 this->fields
[central_row
].ConnectIndustryAccepted(2);
2328 /* Add cargo labels. */
2329 this->fields
[central_row
- 1].MakeCargoLabel(2, true);
2330 this->fields
[central_row
+ 1].MakeCargoLabel(2, false);
2332 /* Add suppliers and customers of the 'it' industry. */
2335 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2336 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2337 if (!indsp
->enabled
) continue;
2339 if (HasCommonValidCargo(central_sp
->accepts_cargo
, lengthof(central_sp
->accepts_cargo
), indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2340 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2341 SetBit(_displayed_industries
, it
);
2344 if (HasCommonValidCargo(central_sp
->produced_cargo
, lengthof(central_sp
->produced_cargo
), indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2345 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, it
);
2346 SetBit(_displayed_industries
, it
);
2350 if (houses_supply
) {
2351 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2354 if (houses_accept
) {
2355 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 4, NUM_INDUSTRYTYPES
);
2359 this->ShortenCargoColumn(1, 1, num_indrows
);
2360 this->ShortenCargoColumn(3, 1, num_indrows
);
2361 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2362 this->vscroll
->SetCount(CeilDiv(WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
+ CargoesField::small_height
+ num_indrows
* CargoesField::normal_height
, nwp
->resize_y
));
2364 this->NotifySmallmap();
2368 * Compute what and where to display for cargo id \a cid.
2369 * @param cid Cargo id to display.
2371 void ComputeCargoDisplay(CargoID cid
)
2373 this->GetWidget
<NWidgetCore
>(WID_IC_CAPTION
)->widget_data
= STR_INDUSTRY_CARGOES_CARGO_CAPTION
;
2374 this->ind_cargo
= cid
+ NUM_INDUSTRYTYPES
;
2375 _displayed_industries
= 0;
2377 this->fields
.Clear();
2378 CargoesRow
*row
= this->fields
.Append();
2379 row
->columns
[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS
);
2380 row
->columns
[1].MakeEmpty(CFT_SMALL_EMPTY
);
2381 row
->columns
[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS
);
2382 row
->columns
[3].MakeEmpty(CFT_SMALL_EMPTY
);
2383 row
->columns
[4].MakeEmpty(CFT_SMALL_EMPTY
);
2385 bool houses_supply
= HousesCanSupply(&cid
, 1);
2386 bool houses_accept
= HousesCanAccept(&cid
, 1);
2387 int num_supp
= CountMatchingProducingIndustries(&cid
, 1) + houses_supply
+ 1; // Ensure room for the cargo label.
2388 int num_cust
= CountMatchingAcceptingIndustries(&cid
, 1) + houses_accept
;
2389 int num_indrows
= max(num_supp
, num_cust
);
2390 for (int i
= 0; i
< num_indrows
; i
++) {
2391 CargoesRow
*row
= this->fields
.Append();
2392 row
->columns
[0].MakeEmpty(CFT_EMPTY
);
2393 row
->columns
[1].MakeCargo(&cid
, 1);
2394 row
->columns
[2].MakeEmpty(CFT_EMPTY
);
2395 row
->columns
[3].MakeEmpty(CFT_EMPTY
);
2396 row
->columns
[4].MakeEmpty(CFT_EMPTY
);
2399 this->fields
[num_indrows
].MakeCargoLabel(0, false); // Add cargo labels at the left bottom.
2401 /* Add suppliers and customers of the cargo. */
2404 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
2405 const IndustrySpec
*indsp
= GetIndustrySpec(it
);
2406 if (!indsp
->enabled
) continue;
2408 if (HasCommonValidCargo(&cid
, 1, indsp
->produced_cargo
, lengthof(indsp
->produced_cargo
))) {
2409 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, it
);
2410 SetBit(_displayed_industries
, it
);
2413 if (HasCommonValidCargo(&cid
, 1, indsp
->accepts_cargo
, lengthof(indsp
->accepts_cargo
))) {
2414 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, it
);
2415 SetBit(_displayed_industries
, it
);
2419 if (houses_supply
) {
2420 this->PlaceIndustry(1 + supp_count
* num_indrows
/ num_supp
, 0, NUM_INDUSTRYTYPES
);
2423 if (houses_accept
) {
2424 this->PlaceIndustry(1 + cust_count
* num_indrows
/ num_cust
, 2, NUM_INDUSTRYTYPES
);
2428 this->ShortenCargoColumn(1, 1, num_indrows
);
2429 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2430 this->vscroll
->SetCount(CeilDiv(WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
+ CargoesField::small_height
+ num_indrows
* CargoesField::normal_height
, nwp
->resize_y
));
2432 this->NotifySmallmap();
2436 * Some data on this window has become invalid.
2437 * @param data Information about the changed data.
2438 * - data = 0 .. NUM_INDUSTRYTYPES - 1: Display the chain around the given industry.
2439 * - data = NUM_INDUSTRYTYPES: Stop sending updates to the smallmap window.
2440 * @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.
2442 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
2444 if (!gui_scope
) return;
2445 if (data
== NUM_INDUSTRYTYPES
) {
2446 if (this->IsWidgetLowered(WID_IC_NOTIFY
)) {
2447 this->RaiseWidget(WID_IC_NOTIFY
);
2448 this->SetWidgetDirty(WID_IC_NOTIFY
);
2453 assert(data
>= 0 && data
< NUM_INDUSTRYTYPES
);
2454 this->ComputeIndustryDisplay(data
);
2457 virtual void DrawWidget(const Rect
&r
, int widget
) const
2459 if (widget
!= WID_IC_PANEL
) return;
2461 DrawPixelInfo tmp_dpi
, *old_dpi
;
2462 int width
= r
.right
- r
.left
+ 1;
2463 int height
= r
.bottom
- r
.top
+ 1 - WD_FRAMERECT_TOP
- WD_FRAMERECT_BOTTOM
;
2464 if (!FillDrawPixelInfo(&tmp_dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.top
+ WD_FRAMERECT_TOP
, width
, height
)) return;
2466 _cur_dpi
= &tmp_dpi
;
2468 int left_pos
= WD_FRAMERECT_LEFT
;
2469 if (this->ind_cargo
>= NUM_INDUSTRYTYPES
) left_pos
+= (CargoesField::industry_width
+ CargoesField::CARGO_FIELD_WIDTH
) / 2;
2470 int last_column
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
2472 const NWidgetBase
*nwp
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2473 int vpos
= -this->vscroll
->GetPosition() * nwp
->resize_y
;
2474 for (uint i
= 0; i
< this->fields
.Length(); i
++) {
2475 int row_height
= (i
== 0) ? CargoesField::small_height
: CargoesField::normal_height
;
2476 if (vpos
+ row_height
>= 0) {
2477 int xpos
= left_pos
;
2479 if (_current_text_dir
== TD_RTL
) {
2486 while (col
>= 0 && col
<= last_column
) {
2487 this->fields
[i
].columns
[col
].Draw(xpos
, vpos
);
2488 xpos
+= (col
& 1) ? CargoesField::CARGO_FIELD_WIDTH
: CargoesField::industry_width
;
2493 if (vpos
>= height
) break;
2500 * Calculate in which field was clicked, and within the field, at what position.
2501 * @param pt Clicked position in the #WID_IC_PANEL widget.
2502 * @param fieldxy If \c true is returned, field x/y coordinate of \a pt.
2503 * @param xy If \c true is returned, x/y coordinate with in the field.
2504 * @return Clicked at a valid position.
2506 bool CalculatePositionInWidget(Point pt
, Point
*fieldxy
, Point
*xy
)
2508 const NWidgetBase
*nw
= this->GetWidget
<NWidgetBase
>(WID_IC_PANEL
);
2512 int vpos
= WD_FRAMERECT_TOP
+ CargoesField::small_height
- this->vscroll
->GetPosition() * nw
->resize_y
;
2513 if (pt
.y
< vpos
) return false;
2515 int row
= (pt
.y
- vpos
) / CargoesField::normal_height
; // row is relative to row 1.
2516 if (row
+ 1 >= (int)this->fields
.Length()) return false;
2517 vpos
= pt
.y
- vpos
- row
* CargoesField::normal_height
; // Position in the row + 1 field
2518 row
++; // rebase row to match index of this->fields.
2520 int xpos
= 2 * WD_FRAMERECT_LEFT
+ ((this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 0 : (CargoesField::industry_width
+ CargoesField::CARGO_FIELD_WIDTH
) / 2);
2521 if (pt
.x
< xpos
) return false;
2523 for (column
= 0; column
<= 5; column
++) {
2524 int width
= (column
& 1) ? CargoesField::CARGO_FIELD_WIDTH
: CargoesField::industry_width
;
2525 if (pt
.x
< xpos
+ width
) break;
2528 int num_columns
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? 4 : 2;
2529 if (column
> num_columns
) return false;
2532 /* Return both positions, compensating for RTL languages (which works due to the equal symmetry in both displays). */
2535 if (_current_text_dir
== TD_RTL
) {
2536 fieldxy
->x
= num_columns
- column
;
2537 xy
->x
= ((column
& 1) ? CargoesField::CARGO_FIELD_WIDTH
: CargoesField::industry_width
) - xpos
;
2539 fieldxy
->x
= column
;
2545 virtual void OnClick(Point pt
, int widget
, int click_count
)
2548 case WID_IC_PANEL
: {
2550 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return;
2552 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
2553 switch (fld
->type
) {
2555 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
) this->ComputeIndustryDisplay(fld
->u
.industry
.ind_type
);
2559 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : NULL
;
2560 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : NULL
;
2561 CargoID cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
2562 if (cid
!= INVALID_CARGO
) this->ComputeCargoDisplay(cid
);
2566 case CFT_CARGO_LABEL
: {
2567 CargoID cid
= fld
->CargoLabelClickedAt(xy
);
2568 if (cid
!= INVALID_CARGO
) this->ComputeCargoDisplay(cid
);
2579 this->ToggleWidgetLoweredState(WID_IC_NOTIFY
);
2580 this->SetWidgetDirty(WID_IC_NOTIFY
);
2581 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
2583 if (this->IsWidgetLowered(WID_IC_NOTIFY
)) {
2584 if (FindWindowByClass(WC_SMALLMAP
) == NULL
) ShowSmallMap();
2585 this->NotifySmallmap();
2589 case WID_IC_CARGO_DROPDOWN
: {
2590 DropDownList
*lst
= new DropDownList
;
2591 const CargoSpec
*cs
;
2592 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
2593 *lst
->Append() = new DropDownListStringItem(cs
->name
, cs
->Index(), false);
2595 if (lst
->Length() == 0) {
2599 int selected
= (this->ind_cargo
>= NUM_INDUSTRYTYPES
) ? (int)(this->ind_cargo
- NUM_INDUSTRYTYPES
) : -1;
2600 ShowDropDownList(this, lst
, selected
, WID_IC_CARGO_DROPDOWN
, 0, true);
2604 case WID_IC_IND_DROPDOWN
: {
2605 DropDownList
*lst
= new DropDownList
;
2606 for (uint8 i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
2607 IndustryType ind
= _sorted_industry_types
[i
];
2608 const IndustrySpec
*indsp
= GetIndustrySpec(ind
);
2609 if (!indsp
->enabled
) continue;
2610 *lst
->Append() = new DropDownListStringItem(indsp
->name
, ind
, false);
2612 if (lst
->Length() == 0) {
2616 int selected
= (this->ind_cargo
< NUM_INDUSTRYTYPES
) ? (int)this->ind_cargo
: -1;
2617 ShowDropDownList(this, lst
, selected
, WID_IC_IND_DROPDOWN
, 0, true);
2623 virtual void OnDropdownSelect(int widget
, int index
)
2625 if (index
< 0) return;
2628 case WID_IC_CARGO_DROPDOWN
:
2629 this->ComputeCargoDisplay(index
);
2632 case WID_IC_IND_DROPDOWN
:
2633 this->ComputeIndustryDisplay(index
);
2638 virtual void OnHover(Point pt
, int widget
)
2640 if (widget
!= WID_IC_PANEL
) return;
2643 if (!CalculatePositionInWidget(pt
, &fieldxy
, &xy
)) return;
2645 const CargoesField
*fld
= this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
;
2646 CargoID cid
= INVALID_CARGO
;
2647 switch (fld
->type
) {
2649 CargoesField
*lft
= (fieldxy
.x
> 0) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
- 1 : NULL
;
2650 CargoesField
*rgt
= (fieldxy
.x
< 4) ? this->fields
[fieldxy
.y
].columns
+ fieldxy
.x
+ 1 : NULL
;
2651 cid
= fld
->CargoClickedAt(lft
, rgt
, xy
);
2655 case CFT_CARGO_LABEL
: {
2656 cid
= fld
->CargoLabelClickedAt(xy
);
2661 if (fld
->u
.industry
.ind_type
< NUM_INDUSTRYTYPES
&& (this->ind_cargo
>= NUM_INDUSTRYTYPES
|| fieldxy
.x
!= 2)) {
2662 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP
, 0, NULL
, TCC_HOVER
);
2669 if (cid
!= INVALID_CARGO
&& (this->ind_cargo
< NUM_INDUSTRYTYPES
|| cid
!= this->ind_cargo
- NUM_INDUSTRYTYPES
)) {
2670 const CargoSpec
*csp
= CargoSpec::Get(cid
);
2672 params
[0] = csp
->name
;
2673 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP
, 1, params
, TCC_HOVER
);
2677 virtual void OnResize()
2679 this->vscroll
->SetCapacityFromWidget(this, WID_IC_PANEL
);
2683 const int IndustryCargoesWindow::HOR_TEXT_PADDING
= 5; ///< Horizontal padding around the industry type text.
2684 const int IndustryCargoesWindow::VERT_TEXT_PADDING
= 5; ///< Vertical padding around the industry type text.
2687 * Open the industry and cargoes window.
2688 * @param id Industry type to display, \c NUM_INDUSTRYTYPES selects a default industry type.
2690 static void ShowIndustryCargoesWindow(IndustryType id
)
2692 if (id
>= NUM_INDUSTRYTYPES
) {
2693 for (uint8 i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
2694 const IndustrySpec
*indsp
= GetIndustrySpec(_sorted_industry_types
[i
]);
2695 if (indsp
->enabled
) {
2696 id
= _sorted_industry_types
[i
];
2700 if (id
>= NUM_INDUSTRYTYPES
) return;
2703 Window
*w
= BringWindowToFrontById(WC_INDUSTRY_CARGOES
, 0);
2705 w
->InvalidateData(id
);
2708 new IndustryCargoesWindow(id
);
2711 /** Open the industry and cargoes window with an industry. */
2712 void ShowIndustryCargoesWindow()
2714 ShowIndustryCargoesWindow(NUM_INDUSTRYTYPES
);