00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "error.h"
00014 #include "gui.h"
00015 #include "sound_func.h"
00016 #include "window_func.h"
00017 #include "textbuf_gui.h"
00018 #include "command_func.h"
00019 #include "viewport_func.h"
00020 #include "gfx_func.h"
00021 #include "industry.h"
00022 #include "town.h"
00023 #include "cheat_type.h"
00024 #include "newgrf_industries.h"
00025 #include "newgrf_text.h"
00026 #include "newgrf_debug.h"
00027 #include "strings_func.h"
00028 #include "company_func.h"
00029 #include "tilehighlight_func.h"
00030 #include "string_func.h"
00031 #include "sortlist_type.h"
00032 #include "widgets/dropdown_func.h"
00033 #include "company_base.h"
00034 #include "core/geometry_func.hpp"
00035 #include "core/random_func.hpp"
00036 #include "core/backup_type.hpp"
00037 #include "genworld.h"
00038 #include "smallmap_gui.h"
00039
00040 #include "widgets/industry_widget.h"
00041
00042 #include "table/strings.h"
00043 #include "table/sprites.h"
00044
00045 bool _ignore_restrictions;
00046 uint64 _displayed_industries;
00047
00048 assert_compile(NUM_INDUSTRYTYPES <= 64);
00049
00051 enum CargoSuffixType {
00052 CST_FUND,
00053 CST_VIEW,
00054 CST_DIR,
00055 };
00056
00057 static void ShowIndustryCargoesWindow(IndustryType id);
00058
00074 static void GetCargoSuffix(uint cargo, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, char *suffix, const char *suffix_last)
00075 {
00076 suffix[0] = '\0';
00077 if (HasBit(indspec->callback_mask, CBM_IND_CARGO_SUFFIX)) {
00078 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);
00079 if (callback == CALLBACK_FAILED || callback == 0x400) return;
00080 if (callback > 0x400) {
00081 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_CARGO_SUFFIX, callback);
00082 } else if (indspec->grf_prop.grffile->grf_version >= 8 || GB(callback, 0, 8) != 0xFF) {
00083 StartTextRefStackUsage(6);
00084 GetString(suffix, GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + callback), suffix_last);
00085 StopTextRefStackUsage();
00086 }
00087 }
00088 }
00089
00100 template <typename TC, typename TS>
00101 static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, const Industry *ind, IndustryType ind_type, const IndustrySpec *indspec, const TC &cargoes, TS &suffixes)
00102 {
00103 assert_compile(lengthof(cargoes) <= lengthof(suffixes));
00104 for (uint j = 0; j < lengthof(cargoes); j++) {
00105 if (cargoes[j] != CT_INVALID) {
00106 GetCargoSuffix(cb_offset + j, cst, ind, ind_type, indspec, suffixes[j], lastof(suffixes[j]));
00107 } else {
00108 suffixes[j][0] = '\0';
00109 }
00110 }
00111 }
00112
00113 IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES];
00114
00116 static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b)
00117 {
00118 static char industry_name[2][64];
00119
00120 const IndustrySpec *indsp1 = GetIndustrySpec(*a);
00121 SetDParam(0, indsp1->name);
00122 GetString(industry_name[0], STR_JUST_STRING, lastof(industry_name[0]));
00123
00124 const IndustrySpec *indsp2 = GetIndustrySpec(*b);
00125 SetDParam(0, indsp2->name);
00126 GetString(industry_name[1], STR_JUST_STRING, lastof(industry_name[1]));
00127
00128 int r = strnatcmp(industry_name[0], industry_name[1]);
00129
00130
00131 return (r != 0) ? r : (*a - *b);
00132 }
00133
00137 void SortIndustryTypes()
00138 {
00139
00140 for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
00141 _sorted_industry_types[i] = i;
00142 }
00143
00144
00145 QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter);
00146 }
00147
00155 void CcBuildIndustry(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00156 {
00157 if (result.Succeeded()) return;
00158
00159 uint8 indtype = GB(p1, 0, 8);
00160 if (indtype < NUM_INDUSTRYTYPES) {
00161 const IndustrySpec *indsp = GetIndustrySpec(indtype);
00162 if (indsp->enabled) {
00163 SetDParam(0, indsp->name);
00164 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, result.GetErrorMessage(), WL_INFO, TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE);
00165 }
00166 }
00167 }
00168
00169 static const NWidgetPart _nested_build_industry_widgets[] = {
00170 NWidget(NWID_HORIZONTAL),
00171 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00172 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FUND_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00173 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00174 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00175 EndContainer(),
00176 NWidget(NWID_HORIZONTAL),
00177 NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, WID_DPI_MATRIX_WIDGET), SetDataTip(0x801, STR_FUND_INDUSTRY_SELECTION_TOOLTIP), SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_DPI_SCROLLBAR),
00178 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_DPI_SCROLLBAR),
00179 EndContainer(),
00180 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_DPI_INFOPANEL), SetResize(1, 0),
00181 EndContainer(),
00182 NWidget(NWID_HORIZONTAL),
00183 NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_DPI_DISPLAY_WIDGET), SetFill(1, 0), SetResize(1, 0),
00184 SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
00185 NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_DPI_FUND_WIDGET), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_NULL),
00186 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00187 EndContainer(),
00188 };
00189
00191 static const WindowDesc _build_industry_desc(
00192 WDP_AUTO, 170, 212,
00193 WC_BUILD_INDUSTRY, WC_NONE,
00194 WDF_CONSTRUCTION,
00195 _nested_build_industry_widgets, lengthof(_nested_build_industry_widgets)
00196 );
00197
00199 class BuildIndustryWindow : public Window {
00200 int selected_index;
00201 IndustryType selected_type;
00202 uint16 callback_timer;
00203 bool timer_enabled;
00204 uint16 count;
00205 IndustryType index[NUM_INDUSTRYTYPES + 1];
00206 bool enabled[NUM_INDUSTRYTYPES + 1];
00207 Scrollbar *vscroll;
00208
00210 static const int MATRIX_TEXT_OFFSET = 17;
00211
00212 void SetupArrays()
00213 {
00214 this->count = 0;
00215
00216 for (uint i = 0; i < lengthof(this->index); i++) {
00217 this->index[i] = INVALID_INDUSTRYTYPE;
00218 this->enabled[i] = false;
00219 }
00220
00221 if (_game_mode == GM_EDITOR) {
00222 this->index[this->count] = INVALID_INDUSTRYTYPE;
00223 this->enabled[this->count] = true;
00224 this->count++;
00225 this->timer_enabled = false;
00226 }
00227
00228
00229
00230
00231 for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
00232 IndustryType ind = _sorted_industry_types[i];
00233 const IndustrySpec *indsp = GetIndustrySpec(ind);
00234 if (indsp->enabled) {
00235
00236
00237
00238 if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
00239
00240 if (this->selected_type == ind) this->selected_index = -1;
00241 continue;
00242 }
00243 this->index[this->count] = ind;
00244 this->enabled[this->count] = (_game_mode == GM_EDITOR) || GetIndustryProbabilityCallback(ind, IACT_USERCREATION, 1) > 0;
00245
00246 if (this->selected_type == ind) this->selected_index = this->count;
00247 this->count++;
00248 }
00249 }
00250
00251
00252
00253 if (this->selected_index == -1) {
00254 this->selected_index = 0;
00255 this->selected_type = this->index[0];
00256 }
00257
00258 this->vscroll->SetCount(this->count);
00259 }
00260
00262 void SetButtons()
00263 {
00264 this->SetWidgetDisabledState(WID_DPI_FUND_WIDGET, this->selected_type != INVALID_INDUSTRYTYPE && !this->enabled[this->selected_index]);
00265 this->SetWidgetDisabledState(WID_DPI_DISPLAY_WIDGET, this->selected_type == INVALID_INDUSTRYTYPE && this->enabled[this->selected_index]);
00266 }
00267
00268 public:
00269 BuildIndustryWindow() : Window()
00270 {
00271 this->timer_enabled = _loaded_newgrf_features.has_newindustries;
00272
00273 this->selected_index = -1;
00274 this->selected_type = INVALID_INDUSTRYTYPE;
00275
00276 this->callback_timer = DAY_TICKS;
00277
00278 this->CreateNestedTree(&_build_industry_desc);
00279 this->vscroll = this->GetScrollbar(WID_DPI_SCROLLBAR);
00280 this->FinishInitNested(&_build_industry_desc, 0);
00281
00282 this->SetButtons();
00283 }
00284
00285 virtual void OnInit()
00286 {
00287 this->SetupArrays();
00288 }
00289
00290 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00291 {
00292 switch (widget) {
00293 case WID_DPI_MATRIX_WIDGET: {
00294 Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES);
00295 for (byte i = 0; i < this->count; i++) {
00296 if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
00297 d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name));
00298 }
00299 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00300 d.width += MATRIX_TEXT_OFFSET + padding.width;
00301 d.height = 5 * resize->height;
00302 *size = maxdim(*size, d);
00303 break;
00304 }
00305
00306 case WID_DPI_INFOPANEL: {
00307
00308 int height = 2 + (_game_mode == GM_EDITOR ? 0 : 1) + (_loaded_newgrf_features.has_newindustries ? 4 : 0);
00309 Dimension d = {0, 0};
00310 for (byte i = 0; i < this->count; i++) {
00311 if (this->index[i] == INVALID_INDUSTRYTYPE) continue;
00312
00313 const IndustrySpec *indsp = GetIndustrySpec(this->index[i]);
00314
00315 char cargo_suffix[3][512];
00316 GetAllCargoSuffixes(0, CST_FUND, NULL, this->index[i], indsp, indsp->accepts_cargo, cargo_suffix);
00317 StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
00318 byte p = 0;
00319 SetDParam(0, STR_JUST_NOTHING);
00320 SetDParamStr(1, "");
00321 for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
00322 if (indsp->accepts_cargo[j] == CT_INVALID) continue;
00323 if (p > 0) str++;
00324 SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
00325 SetDParamStr(p++, cargo_suffix[j]);
00326 }
00327 d = maxdim(d, GetStringBoundingBox(str));
00328
00329
00330 GetAllCargoSuffixes(3, CST_FUND, NULL, this->index[i], indsp, indsp->produced_cargo, cargo_suffix);
00331 str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
00332 p = 0;
00333 SetDParam(0, STR_JUST_NOTHING);
00334 SetDParamStr(1, "");
00335 for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
00336 if (indsp->produced_cargo[j] == CT_INVALID) continue;
00337 if (p > 0) str++;
00338 SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
00339 SetDParamStr(p++, cargo_suffix[j]);
00340 }
00341 d = maxdim(d, GetStringBoundingBox(str));
00342 }
00343
00344
00345 size->height = height * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00346 size->width = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00347 break;
00348 }
00349
00350 case WID_DPI_FUND_WIDGET: {
00351 Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
00352 d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY));
00353 d = maxdim(d, GetStringBoundingBox(STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY));
00354 d.width += padding.width;
00355 d.height += padding.height;
00356 *size = maxdim(*size, d);
00357 break;
00358 }
00359 }
00360 }
00361
00362 virtual void SetStringParameters(int widget) const
00363 {
00364 switch (widget) {
00365 case WID_DPI_FUND_WIDGET:
00366
00367
00368 if (_game_mode == GM_EDITOR) {
00369
00370 SetDParam(0, STR_FUND_INDUSTRY_BUILD_NEW_INDUSTRY);
00371 } else {
00372 const IndustrySpec *indsp = GetIndustrySpec(this->index[this->selected_index]);
00373 SetDParam(0, (_settings_game.construction.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_FUND_INDUSTRY_PROSPECT_NEW_INDUSTRY : STR_FUND_INDUSTRY_FUND_NEW_INDUSTRY);
00374 }
00375 break;
00376 }
00377 }
00378
00379 virtual void DrawWidget(const Rect &r, int widget) const
00380 {
00381 switch (widget) {
00382 case WID_DPI_MATRIX_WIDGET:
00383 for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) {
00384 int x = r.left + WD_MATRIX_LEFT;
00385 int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
00386 bool selected = this->selected_index == i + this->vscroll->GetPosition();
00387
00388 if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) {
00389 DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
00390 continue;
00391 }
00392 const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll->GetPosition()]);
00393
00394
00395 DrawString(x + MATRIX_TEXT_OFFSET, r.right - WD_MATRIX_RIGHT, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
00396 GfxFillRect(x, y + 1, x + 10, y + 7, selected ? PC_WHITE : PC_BLACK);
00397 GfxFillRect(x + 1, y + 2, x + 9, y + 6, indsp->map_colour);
00398 }
00399 break;
00400
00401 case WID_DPI_INFOPANEL: {
00402 int y = r.top + WD_FRAMERECT_TOP;
00403 int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00404 int left = r.left + WD_FRAMERECT_LEFT;
00405 int right = r.right - WD_FRAMERECT_RIGHT;
00406
00407 if (this->selected_type == INVALID_INDUSTRYTYPE) {
00408 DrawStringMultiLine(left, right, y, bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
00409 break;
00410 }
00411
00412 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00413
00414 if (_game_mode != GM_EDITOR) {
00415 SetDParam(0, indsp->GetConstructionCost());
00416 DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
00417 y += FONT_HEIGHT_NORMAL;
00418 }
00419
00420
00421 char cargo_suffix[3][512];
00422 GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
00423 StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
00424 byte p = 0;
00425 SetDParam(0, STR_JUST_NOTHING);
00426 SetDParamStr(1, "");
00427 for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
00428 if (indsp->accepts_cargo[j] == CT_INVALID) continue;
00429 if (p > 0) str++;
00430 SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
00431 SetDParamStr(p++, cargo_suffix[j]);
00432 }
00433 DrawString(left, right, y, str);
00434 y += FONT_HEIGHT_NORMAL;
00435
00436
00437 GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
00438 str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
00439 p = 0;
00440 SetDParam(0, STR_JUST_NOTHING);
00441 SetDParamStr(1, "");
00442 for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
00443 if (indsp->produced_cargo[j] == CT_INVALID) continue;
00444 if (p > 0) str++;
00445 SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
00446 SetDParamStr(p++, cargo_suffix[j]);
00447 }
00448 DrawString(left, right, y, str);
00449 y += FONT_HEIGHT_NORMAL;
00450
00451
00452 str = STR_NULL;
00453 if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
00454 uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
00455 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00456 if (callback_res > 0x400) {
00457 ErrorUnknownCallbackResult(indsp->grf_prop.grffile->grfid, CBID_INDUSTRY_FUND_MORE_TEXT, callback_res);
00458 } else {
00459 str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);
00460 if (str != STR_UNDEFINED) {
00461 StartTextRefStackUsage(6);
00462 DrawStringMultiLine(left, right, y, bottom, str, TC_YELLOW);
00463 StopTextRefStackUsage();
00464 }
00465 }
00466 }
00467 }
00468 break;
00469 }
00470 }
00471 }
00472
00473 virtual void OnClick(Point pt, int widget, int click_count)
00474 {
00475 switch (widget) {
00476 case WID_DPI_MATRIX_WIDGET: {
00477 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_DPI_MATRIX_WIDGET);
00478 if (y < this->count) {
00479 this->selected_index = y;
00480 this->selected_type = this->index[y];
00481 const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
00482
00483 this->SetDirty();
00484
00485 if (_thd.GetCallbackWnd() == this &&
00486 ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
00487 this->selected_type == INVALID_INDUSTRYTYPE ||
00488 !this->enabled[this->selected_index])) {
00489
00490 this->RaiseButtons();
00491 ResetObjectToPlace();
00492 }
00493
00494 this->SetButtons();
00495 if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, WID_DPI_FUND_WIDGET, 1);
00496 }
00497 break;
00498 }
00499
00500 case WID_DPI_DISPLAY_WIDGET:
00501 if (this->selected_type != INVALID_INDUSTRYTYPE) ShowIndustryCargoesWindow(this->selected_type);
00502 break;
00503
00504 case WID_DPI_FUND_WIDGET: {
00505 if (this->selected_type == INVALID_INDUSTRYTYPE) {
00506 this->HandleButtonClick(WID_DPI_FUND_WIDGET);
00507
00508 if (Town::GetNumItems() == 0) {
00509 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO);
00510 } else {
00511 extern void GenerateIndustries();
00512 _generating_world = true;
00513 GenerateIndustries();
00514 _generating_world = false;
00515 }
00516 } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
00517 DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
00518 this->HandleButtonClick(WID_DPI_FUND_WIDGET);
00519 } else {
00520 HandlePlacePushButton(this, WID_DPI_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT);
00521 }
00522 break;
00523 }
00524 }
00525 }
00526
00527 virtual void OnResize()
00528 {
00529
00530 this->vscroll->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET);
00531 this->GetWidget<NWidgetCore>(WID_DPI_MATRIX_WIDGET)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00532 }
00533
00534 virtual void OnPlaceObject(Point pt, TileIndex tile)
00535 {
00536 bool success = true;
00537
00538 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00539 uint32 seed = InteractiveRandom();
00540
00541 if (_game_mode == GM_EDITOR) {
00542
00543 if (Town::GetNumItems() == 0) {
00544 SetDParam(0, indsp->name);
00545 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO, pt.x, pt.y);
00546 return;
00547 }
00548
00549 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
00550 _generating_world = true;
00551 _ignore_restrictions = true;
00552
00553 DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed,
00554 CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY), &CcBuildIndustry);
00555
00556 cur_company.Restore();
00557 _ignore_restrictions = false;
00558 _generating_world = false;
00559 } else {
00560 success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
00561 }
00562
00563
00564 if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00565 }
00566
00567 virtual void OnTick()
00568 {
00569 if (_pause_mode != PM_UNPAUSED) return;
00570 if (!this->timer_enabled) return;
00571 if (--this->callback_timer == 0) {
00572
00573
00574 this->callback_timer = DAY_TICKS;
00575
00576 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00577
00578 if (indsp->enabled) {
00579 bool call_back_result = GetIndustryProbabilityCallback(this->selected_type, IACT_USERCREATION, 1) > 0;
00580
00581
00582 if (call_back_result != this->enabled[this->selected_index]) {
00583 this->enabled[this->selected_index] = call_back_result;
00584 this->SetButtons();
00585 this->SetDirty();
00586 }
00587 }
00588 }
00589 }
00590
00591 virtual void OnTimeout()
00592 {
00593 this->RaiseButtons();
00594 }
00595
00596 virtual void OnPlaceObjectAbort()
00597 {
00598 this->RaiseButtons();
00599 }
00600
00606 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00607 {
00608 if (!gui_scope) return;
00609 this->SetupArrays();
00610
00611 const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
00612 if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.industry_density != ID_FUND_ONLY;
00613 this->SetButtons();
00614 }
00615 };
00616
00617 void ShowBuildIndustryWindow()
00618 {
00619 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
00620 if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
00621 new BuildIndustryWindow();
00622 }
00623
00624 static void UpdateIndustryProduction(Industry *i);
00625
00626 static inline bool IsProductionAlterable(const Industry *i)
00627 {
00628 const IndustrySpec *is = GetIndustrySpec(i->type);
00629 return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
00630 (is->production_rate[0] != 0 || is->production_rate[1] != 0 || is->IsRawIndustry()));
00631 }
00632
00633 class IndustryViewWindow : public Window
00634 {
00636 enum Editability {
00637 EA_NONE,
00638 EA_MULTIPLIER,
00639 EA_RATE,
00640 };
00641
00643 enum InfoLine {
00644 IL_NONE,
00645 IL_MULTIPLIER,
00646 IL_RATE1,
00647 IL_RATE2,
00648 };
00649
00650 Editability editable;
00651 InfoLine editbox_line;
00652 InfoLine clicked_line;
00653 byte clicked_button;
00654 int production_offset_y;
00655 int info_height;
00656
00657 public:
00658 IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00659 {
00660 this->flags |= WF_DISABLE_VP_SCROLL;
00661 this->editbox_line = IL_NONE;
00662 this->clicked_line = IL_NONE;
00663 this->clicked_button = 0;
00664 this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1;
00665
00666 this->InitNested(desc, window_number);
00667 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_IV_VIEWPORT);
00668 nvp->InitializeViewport(this, Industry::Get(window_number)->location.GetCenterTile(), ZOOM_LVL_INDUSTRY);
00669
00670 this->InvalidateData();
00671 }
00672
00673 virtual void OnPaint()
00674 {
00675 this->DrawWidgets();
00676
00677 if (this->IsShaded()) return;
00678
00679 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_IV_INFO);
00680 uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
00681 if (expected > nwi->current_y - 1) {
00682 this->info_height = expected + 1;
00683 this->ReInit();
00684 return;
00685 }
00686 }
00687
00695 int DrawInfo(uint left, uint right, uint top)
00696 {
00697 Industry *i = Industry::Get(this->window_number);
00698 const IndustrySpec *ind = GetIndustrySpec(i->type);
00699 int y = top + WD_FRAMERECT_TOP;
00700 bool first = true;
00701 bool has_accept = false;
00702 char cargo_suffix[3][512];
00703
00704 if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
00705 GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
00706 for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00707 if (i->accepts_cargo[j] == CT_INVALID) continue;
00708 has_accept = true;
00709 if (first) {
00710 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
00711 y += FONT_HEIGHT_NORMAL;
00712 first = false;
00713 }
00714 SetDParam(0, i->accepts_cargo[j]);
00715 SetDParam(1, i->incoming_cargo_waiting[j]);
00716 SetDParamStr(2, cargo_suffix[j]);
00717 DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
00718 y += FONT_HEIGHT_NORMAL;
00719 }
00720 } else {
00721 GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
00722 StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
00723 byte p = 0;
00724 for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00725 if (i->accepts_cargo[j] == CT_INVALID) continue;
00726 has_accept = true;
00727 if (p > 0) str++;
00728 SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
00729 SetDParamStr(p++, cargo_suffix[j]);
00730 }
00731 if (has_accept) {
00732 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
00733 y += FONT_HEIGHT_NORMAL;
00734 }
00735 }
00736
00737 GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
00738 first = true;
00739 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00740 if (i->produced_cargo[j] == CT_INVALID) continue;
00741 if (first) {
00742 if (has_accept) y += WD_PAR_VSEP_WIDE;
00743 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
00744 y += FONT_HEIGHT_NORMAL;
00745 if (this->editable == EA_RATE) this->production_offset_y = y;
00746 first = false;
00747 }
00748
00749 SetDParam(0, i->produced_cargo[j]);
00750 SetDParam(1, i->last_month_production[j]);
00751 SetDParamStr(2, cargo_suffix[j]);
00752 SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
00753 uint x = left + WD_FRAMETEXT_LEFT + (this->editable == EA_RATE ? 30 : 0);
00754 DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
00755
00756 if (this->editable == EA_RATE) {
00757 DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + j) ? this->clicked_button : 0,
00758 i->production_rate[j] > 0, i->production_rate[j] < 255);
00759 }
00760 y += FONT_HEIGHT_NORMAL;
00761 }
00762
00763
00764 if (this->editable == EA_MULTIPLIER) {
00765 y += WD_PAR_VSEP_WIDE;
00766 this->production_offset_y = y;
00767 SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
00768 uint x = left + WD_FRAMETEXT_LEFT + 30;
00769 DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL);
00770 DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0,
00771 i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM);
00772 y += FONT_HEIGHT_NORMAL;
00773 }
00774
00775
00776 if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
00777 uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
00778 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00779 if (callback_res > 0x400) {
00780 ErrorUnknownCallbackResult(ind->grf_prop.grffile->grfid, CBID_INDUSTRY_WINDOW_MORE_TEXT, callback_res);
00781 } else {
00782 StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
00783 if (message != STR_NULL && message != STR_UNDEFINED) {
00784 y += WD_PAR_VSEP_WIDE;
00785
00786 StartTextRefStackUsage(6);
00787
00788
00789
00790 y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message, TC_BLACK);
00791 StopTextRefStackUsage();
00792 }
00793 }
00794 }
00795 }
00796 return y + WD_FRAMERECT_BOTTOM;
00797 }
00798
00799 virtual void SetStringParameters(int widget) const
00800 {
00801 if (widget == WID_IV_CAPTION) SetDParam(0, this->window_number);
00802 }
00803
00804 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00805 {
00806 if (widget == WID_IV_INFO) size->height = this->info_height;
00807 }
00808
00809 virtual void OnClick(Point pt, int widget, int click_count)
00810 {
00811 switch (widget) {
00812 case WID_IV_INFO: {
00813 Industry *i = Industry::Get(this->window_number);
00814 InfoLine line = IL_NONE;
00815
00816 switch (this->editable) {
00817 case EA_NONE: break;
00818
00819 case EA_MULTIPLIER:
00820 if (IsInsideBS(pt.y, this->production_offset_y, FONT_HEIGHT_NORMAL)) line = IL_MULTIPLIER;
00821 break;
00822
00823 case EA_RATE:
00824 if (pt.y >= this->production_offset_y) {
00825 int row = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
00826 for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
00827 if (i->produced_cargo[j] == CT_INVALID) continue;
00828 row--;
00829 if (row < 0) {
00830 line = (InfoLine)(IL_RATE1 + j);
00831 break;
00832 }
00833 }
00834 }
00835 break;
00836 }
00837 if (line == IL_NONE) return;
00838
00839 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
00840 int left = nwi->pos_x + WD_FRAMETEXT_LEFT;
00841 int right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
00842 if (IsInsideMM(pt.x, left, left + 20)) {
00843
00844 byte button = (pt.x < left + 10) ? 1 : 2;
00845 switch (this->editable) {
00846 case EA_MULTIPLIER:
00847 if (button == 1) {
00848 if (i->prod_level <= PRODLEVEL_MINIMUM) return;
00849 i->prod_level = max<uint>(i->prod_level / 2, PRODLEVEL_MINIMUM);
00850 } else {
00851 if (i->prod_level >= PRODLEVEL_MAXIMUM) return;
00852 i->prod_level = minu(i->prod_level * 2, PRODLEVEL_MAXIMUM);
00853 }
00854 break;
00855
00856 case EA_RATE:
00857 if (button == 1) {
00858 if (i->production_rate[line - IL_RATE1] <= 0) return;
00859 i->production_rate[line - IL_RATE1] = max(i->production_rate[line - IL_RATE1] / 2, 0);
00860 } else {
00861 if (i->production_rate[line - IL_RATE1] >= 255) return;
00862
00863 int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
00864 i->production_rate[line - IL_RATE1] = minu(new_prod, 255);
00865 }
00866 break;
00867
00868 default: NOT_REACHED();
00869 }
00870
00871 UpdateIndustryProduction(i);
00872 this->SetDirty();
00873 this->SetTimeout();
00874 this->clicked_line = line;
00875 this->clicked_button = button;
00876 } else if (IsInsideMM(pt.x, left + 30, right)) {
00877
00878 this->editbox_line = line;
00879 switch (this->editable) {
00880 case EA_MULTIPLIER:
00881 SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
00882 ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION_LEVEL, 10, this, CS_ALPHANUMERAL, QSF_NONE);
00883 break;
00884
00885 case EA_RATE:
00886 SetDParam(0, i->production_rate[line - IL_RATE1] * 8);
00887 ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, this, CS_ALPHANUMERAL, QSF_NONE);
00888 break;
00889
00890 default: NOT_REACHED();
00891 }
00892 }
00893 break;
00894 }
00895
00896 case WID_IV_GOTO: {
00897 Industry *i = Industry::Get(this->window_number);
00898 if (_ctrl_pressed) {
00899 ShowExtraViewPortWindow(i->location.GetCenterTile());
00900 } else {
00901 ScrollMainWindowToTile(i->location.GetCenterTile());
00902 }
00903 break;
00904 }
00905
00906 case WID_IV_DISPLAY: {
00907 Industry *i = Industry::Get(this->window_number);
00908 ShowIndustryCargoesWindow(i->type);
00909 break;
00910 }
00911 }
00912 }
00913
00914 virtual void OnTimeout()
00915 {
00916 this->clicked_line = IL_NONE;
00917 this->clicked_button = 0;
00918 this->SetDirty();
00919 }
00920
00921 virtual void OnResize()
00922 {
00923 if (this->viewport != NULL) {
00924 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_IV_VIEWPORT);
00925 nvp->UpdateViewportCoordinates(this);
00926
00927 ScrollWindowToTile(Industry::Get(this->window_number)->location.GetCenterTile(), this, true);
00928 }
00929 }
00930
00931 virtual void OnQueryTextFinished(char *str)
00932 {
00933 if (StrEmpty(str)) return;
00934
00935 Industry *i = Industry::Get(this->window_number);
00936 uint value = atoi(str);
00937 switch (this->editbox_line) {
00938 case IL_NONE: NOT_REACHED();
00939
00940 case IL_MULTIPLIER:
00941 i->prod_level = ClampU(RoundDivSU(value * PRODLEVEL_DEFAULT, 100), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
00942 break;
00943
00944 default:
00945 i->production_rate[this->editbox_line - IL_RATE1] = ClampU(RoundDivSU(value, 8), 0, 255);
00946 break;
00947 }
00948 UpdateIndustryProduction(i);
00949 this->SetDirty();
00950 }
00951
00957 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00958 {
00959 if (!gui_scope) return;
00960 const Industry *i = Industry::Get(this->window_number);
00961 if (IsProductionAlterable(i)) {
00962 const IndustrySpec *ind = GetIndustrySpec(i->type);
00963 this->editable = ind->UsesSmoothEconomy() ? EA_RATE : EA_MULTIPLIER;
00964 } else {
00965 this->editable = EA_NONE;
00966 }
00967 }
00968
00969 virtual bool IsNewGRFInspectable() const
00970 {
00971 return ::IsNewGRFInspectable(GSF_INDUSTRIES, this->window_number);
00972 }
00973
00974 virtual void ShowNewGRFInspectWindow() const
00975 {
00976 ::ShowNewGRFInspectWindow(GSF_INDUSTRIES, this->window_number);
00977 }
00978 };
00979
00980 static void UpdateIndustryProduction(Industry *i)
00981 {
00982 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00983 if (!indspec->UsesSmoothEconomy()) i->RecomputeProductionMultipliers();
00984
00985 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00986 if (i->produced_cargo[j] != CT_INVALID) {
00987 i->last_month_production[j] = 8 * i->production_rate[j];
00988 }
00989 }
00990 }
00991
00993 static const NWidgetPart _nested_industry_view_widgets[] = {
00994 NWidget(NWID_HORIZONTAL),
00995 NWidget(WWT_CLOSEBOX, COLOUR_CREAM),
00996 NWidget(WWT_CAPTION, COLOUR_CREAM, WID_IV_CAPTION), SetDataTip(STR_INDUSTRY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00997 NWidget(WWT_DEBUGBOX, COLOUR_CREAM),
00998 NWidget(WWT_SHADEBOX, COLOUR_CREAM),
00999 NWidget(WWT_STICKYBOX, COLOUR_CREAM),
01000 EndContainer(),
01001 NWidget(WWT_PANEL, COLOUR_CREAM),
01002 NWidget(WWT_INSET, COLOUR_CREAM), SetPadding(2, 2, 2, 2),
01003 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_IV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
01004 EndContainer(),
01005 EndContainer(),
01006 NWidget(WWT_PANEL, COLOUR_CREAM, WID_IV_INFO), SetMinimalSize(260, 2), SetResize(1, 0),
01007 EndContainer(),
01008 NWidget(NWID_HORIZONTAL),
01009 NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_GOTO), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP),
01010 NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_DISPLAY), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
01011 NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
01012 EndContainer(),
01013 };
01014
01016 static const WindowDesc _industry_view_desc(
01017 WDP_AUTO, 260, 120,
01018 WC_INDUSTRY_VIEW, WC_NONE,
01019 WDF_UNCLICK_BUTTONS,
01020 _nested_industry_view_widgets, lengthof(_nested_industry_view_widgets)
01021 );
01022
01023 void ShowIndustryViewWindow(int industry)
01024 {
01025 AllocateWindowDescFront<IndustryViewWindow>(&_industry_view_desc, industry);
01026 }
01027
01029 static const NWidgetPart _nested_industry_directory_widgets[] = {
01030 NWidget(NWID_HORIZONTAL),
01031 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01032 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01033 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01034 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01035 EndContainer(),
01036 NWidget(NWID_HORIZONTAL),
01037 NWidget(NWID_VERTICAL),
01038 NWidget(NWID_HORIZONTAL),
01039 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_ID_DROPDOWN_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
01040 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_ID_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
01041 NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
01042 EndContainer(),
01043 NWidget(WWT_PANEL, COLOUR_BROWN, WID_ID_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(WID_ID_SCROLLBAR), EndContainer(),
01044 EndContainer(),
01045 NWidget(NWID_VERTICAL),
01046 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_ID_SCROLLBAR),
01047 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01048 EndContainer(),
01049 EndContainer(),
01050 };
01051
01052 typedef GUIList<const Industry*> GUIIndustryList;
01053
01054
01058 class IndustryDirectoryWindow : public Window {
01059 protected:
01060
01061 static Listing last_sorting;
01062 static const Industry *last_industry;
01063
01064
01065 static const StringID sorter_names[];
01066 static GUIIndustryList::SortFunction * const sorter_funcs[];
01067
01068 GUIIndustryList industries;
01069 Scrollbar *vscroll;
01070
01072 void BuildSortIndustriesList()
01073 {
01074 if (this->industries.NeedRebuild()) {
01075 this->industries.Clear();
01076
01077 const Industry *i;
01078 FOR_ALL_INDUSTRIES(i) {
01079 *this->industries.Append() = i;
01080 }
01081
01082 this->industries.Compact();
01083 this->industries.RebuildDone();
01084 this->vscroll->SetCount(this->industries.Length());
01085 }
01086
01087 if (!this->industries.Sort()) return;
01088 IndustryDirectoryWindow::last_industry = NULL;
01089 this->SetWidgetDirty(WID_ID_INDUSTRY_LIST);
01090 }
01091
01099 static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
01100 {
01101 assert(id < lengthof(i->produced_cargo));
01102
01103 if (i->produced_cargo[id] == CT_INVALID) return 101;
01104 return ToPercent8(i->last_month_pct_transported[id]);
01105 }
01106
01114 static int GetCargoTransportedSortValue(const Industry *i)
01115 {
01116 int p1 = GetCargoTransportedPercentsIfValid(i, 0);
01117 int p2 = GetCargoTransportedPercentsIfValid(i, 1);
01118
01119 if (p1 > p2) Swap(p1, p2);
01120
01121 return (p1 << 8) + p2;
01122 }
01123
01125 static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
01126 {
01127 static char buf_cache[96];
01128 static char buf[96];
01129
01130 SetDParam(0, (*a)->index);
01131 GetString(buf, STR_INDUSTRY_NAME, lastof(buf));
01132
01133 if (*b != last_industry) {
01134 last_industry = *b;
01135 SetDParam(0, (*b)->index);
01136 GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache));
01137 }
01138
01139 return strnatcmp(buf, buf_cache);
01140 }
01141
01143 static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
01144 {
01145 int it_a = 0;
01146 while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++;
01147 int it_b = 0;
01148 while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++;
01149 int r = it_a - it_b;
01150 return (r == 0) ? IndustryNameSorter(a, b) : r;
01151 }
01152
01154 static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
01155 {
01156 uint prod_a = 0, prod_b = 0;
01157 for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) {
01158 if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i];
01159 if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i];
01160 }
01161 int r = prod_a - prod_b;
01162
01163 return (r == 0) ? IndustryTypeSorter(a, b) : r;
01164 }
01165
01167 static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
01168 {
01169 int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
01170 return (r == 0) ? IndustryNameSorter(a, b) : r;
01171 }
01172
01178 StringID GetIndustryString(const Industry *i) const
01179 {
01180 const IndustrySpec *indsp = GetIndustrySpec(i->type);
01181 byte p = 0;
01182
01183
01184 SetDParam(p++, i->index);
01185
01186 static char cargo_suffix[lengthof(i->produced_cargo)][512];
01187 GetAllCargoSuffixes(3, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);
01188
01189
01190 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01191 if (i->produced_cargo[j] == CT_INVALID) continue;
01192 SetDParam(p++, i->produced_cargo[j]);
01193 SetDParam(p++, i->last_month_production[j]);
01194 SetDParamStr(p++, cargo_suffix[j]);
01195 }
01196
01197
01198 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01199 if (i->produced_cargo[j] == CT_INVALID) continue;
01200 SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
01201 }
01202
01203
01204 switch (p) {
01205 case 1: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
01206 case 5: return STR_INDUSTRY_DIRECTORY_ITEM;
01207 default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
01208 }
01209 }
01210
01211 public:
01212 IndustryDirectoryWindow(const WindowDesc *desc, WindowNumber number) : Window()
01213 {
01214 this->CreateNestedTree(desc);
01215 this->vscroll = this->GetScrollbar(WID_ID_SCROLLBAR);
01216
01217 this->industries.SetListing(this->last_sorting);
01218 this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
01219 this->industries.ForceRebuild();
01220 this->BuildSortIndustriesList();
01221
01222 this->FinishInitNested(desc, 0);
01223 }
01224
01225 ~IndustryDirectoryWindow()
01226 {
01227 this->last_sorting = this->industries.GetListing();
01228 }
01229
01230 virtual void SetStringParameters(int widget) const
01231 {
01232 if (widget == WID_ID_DROPDOWN_CRITERIA) SetDParam(0, IndustryDirectoryWindow::sorter_names[this->industries.SortType()]);
01233 }
01234
01235 virtual void DrawWidget(const Rect &r, int widget) const
01236 {
01237 switch (widget) {
01238 case WID_ID_DROPDOWN_ORDER:
01239 this->DrawSortButtonState(widget, this->industries.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
01240 break;
01241
01242 case WID_ID_INDUSTRY_LIST: {
01243 int n = 0;
01244 int y = r.top + WD_FRAMERECT_TOP;
01245 if (this->industries.Length() == 0) {
01246 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
01247 break;
01248 }
01249 for (uint i = this->vscroll->GetPosition(); i < this->industries.Length(); i++) {
01250 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
01251
01252 y += this->resize.step_height;
01253 if (++n == this->vscroll->GetCapacity()) break;
01254 }
01255 break;
01256 }
01257 }
01258 }
01259
01260 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01261 {
01262 switch (widget) {
01263 case WID_ID_DROPDOWN_ORDER: {
01264 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
01265 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2;
01266 d.height += padding.height;
01267 *size = maxdim(*size, d);
01268 break;
01269 }
01270
01271 case WID_ID_DROPDOWN_CRITERIA: {
01272 Dimension d = {0, 0};
01273 for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
01274 d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
01275 }
01276 d.width += padding.width;
01277 d.height += padding.height;
01278 *size = maxdim(*size, d);
01279 break;
01280 }
01281
01282 case WID_ID_INDUSTRY_LIST: {
01283 Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
01284 for (uint i = 0; i < this->industries.Length(); i++) {
01285 d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
01286 }
01287 resize->height = d.height;
01288 d.height *= 5;
01289 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01290 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
01291 *size = maxdim(*size, d);
01292 break;
01293 }
01294 }
01295 }
01296
01297
01298 virtual void OnClick(Point pt, int widget, int click_count)
01299 {
01300 switch (widget) {
01301 case WID_ID_DROPDOWN_ORDER:
01302 this->industries.ToggleSortOrder();
01303 this->SetDirty();
01304 break;
01305
01306 case WID_ID_DROPDOWN_CRITERIA:
01307 ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names, this->industries.SortType(), WID_ID_DROPDOWN_CRITERIA, 0, 0);
01308 break;
01309
01310 case WID_ID_INDUSTRY_LIST: {
01311 uint p = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_ID_INDUSTRY_LIST, WD_FRAMERECT_TOP);
01312 if (p < this->industries.Length()) {
01313 if (_ctrl_pressed) {
01314 ShowExtraViewPortWindow(this->industries[p]->location.tile);
01315 } else {
01316 ScrollMainWindowToTile(this->industries[p]->location.tile);
01317 }
01318 }
01319 break;
01320 }
01321 }
01322 }
01323
01324 virtual void OnDropdownSelect(int widget, int index)
01325 {
01326 if (this->industries.SortType() != index) {
01327 this->industries.SetSortType(index);
01328 this->BuildSortIndustriesList();
01329 }
01330 }
01331
01332 virtual void OnResize()
01333 {
01334 this->vscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST);
01335 }
01336
01337 virtual void OnPaint()
01338 {
01339 if (this->industries.NeedRebuild()) this->BuildSortIndustriesList();
01340 this->DrawWidgets();
01341 }
01342
01343 virtual void OnHundredthTick()
01344 {
01345 this->industries.ForceResort();
01346 this->BuildSortIndustriesList();
01347 }
01348
01354 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01355 {
01356 if (data == 0) {
01357
01358 this->industries.ForceRebuild();
01359 } else {
01360 this->industries.ForceResort();
01361 }
01362 }
01363 };
01364
01365 Listing IndustryDirectoryWindow::last_sorting = {false, 0};
01366 const Industry *IndustryDirectoryWindow::last_industry = NULL;
01367
01368
01369 GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = {
01370 &IndustryNameSorter,
01371 &IndustryTypeSorter,
01372 &IndustryProductionSorter,
01373 &IndustryTransportedCargoSorter
01374 };
01375
01376
01377 const StringID IndustryDirectoryWindow::sorter_names[] = {
01378 STR_SORT_BY_NAME,
01379 STR_SORT_BY_TYPE,
01380 STR_SORT_BY_PRODUCTION,
01381 STR_SORT_BY_TRANSPORTED,
01382 INVALID_STRING_ID
01383 };
01384
01385
01387 static const WindowDesc _industry_directory_desc(
01388 WDP_AUTO, 428, 190,
01389 WC_INDUSTRY_DIRECTORY, WC_NONE,
01390 WDF_UNCLICK_BUTTONS,
01391 _nested_industry_directory_widgets, lengthof(_nested_industry_directory_widgets)
01392 );
01393
01394 void ShowIndustryDirectory()
01395 {
01396 AllocateWindowDescFront<IndustryDirectoryWindow>(&_industry_directory_desc, 0);
01397 }
01398
01400 static const NWidgetPart _nested_industry_cargoes_widgets[] = {
01401 NWidget(NWID_HORIZONTAL),
01402 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01403 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_IC_CAPTION), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01404 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01405 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01406 EndContainer(),
01407 NWidget(NWID_HORIZONTAL),
01408 NWidget(NWID_VERTICAL),
01409 NWidget(WWT_PANEL, COLOUR_BROWN, WID_IC_PANEL), SetResize(1, 10), SetMinimalSize(200, 90), SetScrollbar(WID_IC_SCROLLBAR), EndContainer(),
01410 NWidget(NWID_HORIZONTAL),
01411 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_IC_NOTIFY),
01412 SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP),
01413 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0), EndContainer(),
01414 EndContainer(),
01415 EndContainer(),
01416 NWidget(NWID_VERTICAL),
01417 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_IC_SCROLLBAR),
01418 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01419 EndContainer(),
01420 EndContainer(),
01421 };
01422
01424 static const WindowDesc _industry_cargoes_desc(
01425 WDP_AUTO, 300, 210,
01426 WC_INDUSTRY_CARGOES, WC_NONE,
01427 0,
01428 _nested_industry_cargoes_widgets, lengthof(_nested_industry_cargoes_widgets)
01429 );
01430
01432 enum CargoesFieldType {
01433 CFT_EMPTY,
01434 CFT_SMALL_EMPTY,
01435 CFT_INDUSTRY,
01436 CFT_CARGO,
01437 CFT_CARGO_LABEL,
01438 CFT_HEADER,
01439 };
01440
01441 static const uint MAX_CARGOES = 3;
01442
01444 struct CargoesField {
01445 static const int VERT_INTER_INDUSTRY_SPACE;
01446 static const int HOR_CARGO_BORDER_SPACE;
01447 static const int CARGO_STUB_WIDTH;
01448 static const int HOR_CARGO_WIDTH, HOR_CARGO_SPACE;
01449 static const int CARGO_FIELD_WIDTH;
01450 static const int VERT_CARGO_SPACE, VERT_CARGO_EDGE;
01451 static const int BLOB_DISTANCE, BLOB_WIDTH, BLOB_HEIGHT;
01452
01453 static const int INDUSTRY_LINE_COLOUR;
01454 static const int CARGO_LINE_COLOUR;
01455
01456 static int small_height, normal_height;
01457 static int industry_width;
01458
01459 CargoesFieldType type;
01460 union {
01461 struct {
01462 IndustryType ind_type;
01463 CargoID other_produced[MAX_CARGOES];
01464 CargoID other_accepted[MAX_CARGOES];
01465 } industry;
01466 struct {
01467 CargoID vertical_cargoes[MAX_CARGOES];
01468 byte num_cargoes;
01469 CargoID supp_cargoes[MAX_CARGOES];
01470 byte top_end;
01471 CargoID cust_cargoes[MAX_CARGOES];
01472 byte bottom_end;
01473 } cargo;
01474 struct {
01475 CargoID cargoes[MAX_CARGOES];
01476 bool left_align;
01477 } cargo_label;
01478 StringID header;
01479 } u;
01480
01485 void MakeEmpty(CargoesFieldType type)
01486 {
01487 this->type = type;
01488 }
01489
01495 void MakeIndustry(IndustryType ind_type)
01496 {
01497 this->type = CFT_INDUSTRY;
01498 this->u.industry.ind_type = ind_type;
01499 MemSetT(this->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
01500 MemSetT(this->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
01501 }
01502
01509 int ConnectCargo(CargoID cargo, bool producer)
01510 {
01511 assert(this->type == CFT_CARGO);
01512 if (cargo == INVALID_CARGO) return -1;
01513
01514
01515 int column = -1;
01516 for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
01517 if (cargo == this->u.cargo.vertical_cargoes[i]) {
01518 column = i;
01519 break;
01520 }
01521 }
01522 if (column < 0) return -1;
01523
01524 if (producer) {
01525 assert(this->u.cargo.supp_cargoes[column] == INVALID_CARGO);
01526 this->u.cargo.supp_cargoes[column] = column;
01527 } else {
01528 assert(this->u.cargo.cust_cargoes[column] == INVALID_CARGO);
01529 this->u.cargo.cust_cargoes[column] = column;
01530 }
01531 return column;
01532 }
01533
01538 bool HasConnection()
01539 {
01540 assert(this->type == CFT_CARGO);
01541
01542 for (uint i = 0; i < MAX_CARGOES; i++) {
01543 if (this->u.cargo.supp_cargoes[i] != INVALID_CARGO) return true;
01544 if (this->u.cargo.cust_cargoes[i] != INVALID_CARGO) return true;
01545 }
01546 return false;
01547 }
01548
01558 void MakeCargo(const CargoID *cargoes, uint length, int count = -1, bool top_end = false, bool bottom_end = false)
01559 {
01560 this->type = CFT_CARGO;
01561 uint i;
01562 uint num = 0;
01563 for (i = 0; i < MAX_CARGOES && i < length; i++) {
01564 if (cargoes[i] != INVALID_CARGO) {
01565 this->u.cargo.vertical_cargoes[num] = cargoes[i];
01566 num++;
01567 }
01568 }
01569 this->u.cargo.num_cargoes = (count < 0) ? num : count;
01570 for (; num < MAX_CARGOES; num++) this->u.cargo.vertical_cargoes[num] = INVALID_CARGO;
01571 this->u.cargo.top_end = top_end;
01572 this->u.cargo.bottom_end = bottom_end;
01573 MemSetT(this->u.cargo.supp_cargoes, INVALID_CARGO, MAX_CARGOES);
01574 MemSetT(this->u.cargo.cust_cargoes, INVALID_CARGO, MAX_CARGOES);
01575 }
01576
01583 void MakeCargoLabel(const CargoID *cargoes, uint length, bool left_align)
01584 {
01585 this->type = CFT_CARGO_LABEL;
01586 uint i;
01587 for (i = 0; i < MAX_CARGOES && i < length; i++) this->u.cargo_label.cargoes[i] = cargoes[i];
01588 for (; i < MAX_CARGOES; i++) this->u.cargo_label.cargoes[i] = INVALID_CARGO;
01589 this->u.cargo_label.left_align = left_align;
01590 }
01591
01596 void MakeHeader(StringID textid)
01597 {
01598 this->type = CFT_HEADER;
01599 this->u.header = textid;
01600 }
01601
01607 int GetCargoBase(int xpos) const
01608 {
01609 assert(this->type == CFT_CARGO);
01610
01611 switch (this->u.cargo.num_cargoes) {
01612 case 0: return xpos + CARGO_FIELD_WIDTH / 2;
01613 case 1: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH / 2;
01614 case 2: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE / 2;
01615 case 3: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE - HOR_CARGO_WIDTH / 2;
01616 default: NOT_REACHED();
01617 }
01618 }
01619
01625 void Draw(int xpos, int ypos) const
01626 {
01627 switch (this->type) {
01628 case CFT_EMPTY:
01629 case CFT_SMALL_EMPTY:
01630 break;
01631
01632 case CFT_HEADER:
01633 ypos += (small_height - FONT_HEIGHT_NORMAL) / 2;
01634 DrawString(xpos, xpos + industry_width, ypos, this->u.header, TC_WHITE, SA_HOR_CENTER);
01635 break;
01636
01637 case CFT_INDUSTRY: {
01638 int ypos1 = ypos + VERT_INTER_INDUSTRY_SPACE / 2;
01639 int ypos2 = ypos + normal_height - 1 - VERT_INTER_INDUSTRY_SPACE / 2;
01640 int xpos2 = xpos + industry_width - 1;
01641 GfxDrawLine(xpos, ypos1, xpos2, ypos1, INDUSTRY_LINE_COLOUR);
01642 GfxDrawLine(xpos, ypos1, xpos, ypos2, INDUSTRY_LINE_COLOUR);
01643 GfxDrawLine(xpos, ypos2, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
01644 GfxDrawLine(xpos2, ypos1, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
01645 ypos += (normal_height - FONT_HEIGHT_NORMAL) / 2;
01646 if (this->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01647 const IndustrySpec *indsp = GetIndustrySpec(this->u.industry.ind_type);
01648 SetDParam(0, indsp->name);
01649 DrawString(xpos, xpos2, ypos, STR_JUST_STRING, TC_WHITE, SA_HOR_CENTER);
01650
01651
01652 int blob_left, blob_right;
01653 if (_current_text_dir == TD_RTL) {
01654 blob_right = xpos2 - BLOB_DISTANCE;
01655 blob_left = blob_right - BLOB_WIDTH;
01656 } else {
01657 blob_left = xpos + BLOB_DISTANCE;
01658 blob_right = blob_left + BLOB_WIDTH;
01659 }
01660 GfxFillRect(blob_left, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT, blob_right, ypos2 - BLOB_DISTANCE, PC_BLACK);
01661 GfxFillRect(blob_left + 1, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT + 1, blob_right - 1, ypos2 - BLOB_DISTANCE - 1, indsp->map_colour);
01662 } else {
01663 DrawString(xpos, xpos2, ypos, STR_INDUSTRY_CARGOES_HOUSES, TC_FROMSTRING, SA_HOR_CENTER);
01664 }
01665
01666
01667 const CargoID *other_right, *other_left;
01668 if (_current_text_dir == TD_RTL) {
01669 other_right = this->u.industry.other_accepted;
01670 other_left = this->u.industry.other_produced;
01671 } else {
01672 other_right = this->u.industry.other_produced;
01673 other_left = this->u.industry.other_accepted;
01674 }
01675 ypos1 += VERT_CARGO_EDGE;
01676 for (uint i = 0; i < MAX_CARGOES; i++) {
01677 if (other_right[i] != INVALID_CARGO) {
01678 const CargoSpec *csp = CargoSpec::Get(other_right[i]);
01679 int xp = xpos + industry_width + CARGO_STUB_WIDTH;
01680 DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp);
01681 GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01682 }
01683 if (other_left[i] != INVALID_CARGO) {
01684 const CargoSpec *csp = CargoSpec::Get(other_left[i]);
01685 int xp = xpos - CARGO_STUB_WIDTH;
01686 DrawHorConnection(xp + 1, xpos - 1, ypos1, csp);
01687 GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01688 }
01689 ypos1 += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01690 }
01691 break;
01692 }
01693
01694 case CFT_CARGO: {
01695 int cargo_base = this->GetCargoBase(xpos);
01696 int top = ypos + (this->u.cargo.top_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0);
01697 int bot = ypos - (this->u.cargo.bottom_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0) + normal_height - 1;
01698 int colpos = cargo_base;
01699 for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
01700 if (this->u.cargo.top_end) GfxDrawLine(colpos, top - 1, colpos + HOR_CARGO_WIDTH - 1, top - 1, CARGO_LINE_COLOUR);
01701 if (this->u.cargo.bottom_end) GfxDrawLine(colpos, bot + 1, colpos + HOR_CARGO_WIDTH - 1, bot + 1, CARGO_LINE_COLOUR);
01702 GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
01703 colpos++;
01704 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[i]);
01705 GfxFillRect(colpos, top, colpos + HOR_CARGO_WIDTH - 2, bot, csp->legend_colour, FILLRECT_OPAQUE);
01706 colpos += HOR_CARGO_WIDTH - 2;
01707 GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
01708 colpos += 1 + HOR_CARGO_SPACE;
01709 }
01710
01711 const CargoID *hor_left, *hor_right;
01712 if (_current_text_dir == TD_RTL) {
01713 hor_left = this->u.cargo.cust_cargoes;
01714 hor_right = this->u.cargo.supp_cargoes;
01715 } else {
01716 hor_left = this->u.cargo.supp_cargoes;
01717 hor_right = this->u.cargo.cust_cargoes;
01718 }
01719 ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
01720 for (uint i = 0; i < MAX_CARGOES; i++) {
01721 if (hor_left[i] != INVALID_CARGO) {
01722 int col = hor_left[i];
01723 int dx = 0;
01724 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
01725 for (; col > 0; col--) {
01726 int lf = cargo_base + col * HOR_CARGO_WIDTH + (col - 1) * HOR_CARGO_SPACE;
01727 DrawHorConnection(lf, lf + HOR_CARGO_SPACE - dx, ypos, csp);
01728 dx = 1;
01729 }
01730 DrawHorConnection(xpos, cargo_base - dx, ypos, csp);
01731 }
01732 if (hor_right[i] != INVALID_CARGO) {
01733 int col = hor_right[i];
01734 int dx = 0;
01735 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
01736 for (; col < this->u.cargo.num_cargoes - 1; col++) {
01737 int lf = cargo_base + (col + 1) * HOR_CARGO_WIDTH + col * HOR_CARGO_SPACE;
01738 DrawHorConnection(lf + dx - 1, lf + HOR_CARGO_SPACE - 1, ypos, csp);
01739 dx = 1;
01740 }
01741 DrawHorConnection(cargo_base + col * HOR_CARGO_SPACE + (col + 1) * HOR_CARGO_WIDTH - 1 + dx, xpos + CARGO_FIELD_WIDTH - 1, ypos, csp);
01742 }
01743 ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01744 }
01745 break;
01746 }
01747
01748 case CFT_CARGO_LABEL:
01749 ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
01750 for (uint i = 0; i < MAX_CARGOES; i++) {
01751 if (this->u.cargo_label.cargoes[i] != INVALID_CARGO) {
01752 const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]);
01753 DrawString(xpos + WD_FRAMERECT_LEFT, xpos + industry_width - 1 - WD_FRAMERECT_RIGHT, ypos, csp->name, TC_WHITE,
01754 (this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT);
01755 }
01756 ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01757 }
01758 break;
01759
01760 default:
01761 NOT_REACHED();
01762 }
01763 }
01764
01772 CargoID CargoClickedAt(const CargoesField *left, const CargoesField *right, Point pt) const
01773 {
01774 assert(this->type == CFT_CARGO);
01775
01776
01777 int cpos = this->GetCargoBase(0);
01778 uint col;
01779 for (col = 0; col < this->u.cargo.num_cargoes; col++) {
01780 if (pt.x < cpos) break;
01781 if (pt.x < cpos + CargoesField::HOR_CARGO_WIDTH) return this->u.cargo.vertical_cargoes[col];
01782 cpos += CargoesField::HOR_CARGO_WIDTH + CargoesField::HOR_CARGO_SPACE;
01783 }
01784
01785
01786 int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
01787 uint row;
01788 for (row = 0; row < MAX_CARGOES; row++) {
01789 if (pt.y < vpos) return INVALID_CARGO;
01790 if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
01791 vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01792 }
01793 if (row == MAX_CARGOES) return INVALID_CARGO;
01794
01795
01796 if (col == 0) {
01797 if (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]];
01798 if (left != NULL) {
01799 if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row];
01800 if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row];
01801 }
01802 return INVALID_CARGO;
01803 }
01804 if (col == this->u.cargo.num_cargoes) {
01805 if (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]];
01806 if (right != NULL) {
01807 if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row];
01808 if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row];
01809 }
01810 return INVALID_CARGO;
01811 }
01812 if (row >= col) {
01813
01814
01815
01816
01817 return (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO;
01818 } else {
01819
01820 return (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO;
01821 }
01822 }
01823
01829 CargoID CargoLabelClickedAt(Point pt) const
01830 {
01831 assert(this->type == CFT_CARGO_LABEL);
01832
01833 int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
01834 uint row;
01835 for (row = 0; row < MAX_CARGOES; row++) {
01836 if (pt.y < vpos) return INVALID_CARGO;
01837 if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
01838 vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01839 }
01840 if (row == MAX_CARGOES) return INVALID_CARGO;
01841 return this->u.cargo_label.cargoes[row];
01842 }
01843
01844 private:
01852 static void DrawHorConnection(int left, int right, int top, const CargoSpec *csp)
01853 {
01854 GfxDrawLine(left, top, right, top, CARGO_LINE_COLOUR);
01855 GfxFillRect(left, top + 1, right, top + FONT_HEIGHT_NORMAL - 2, csp->legend_colour, FILLRECT_OPAQUE);
01856 GfxDrawLine(left, top + FONT_HEIGHT_NORMAL - 1, right, top + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01857 }
01858 };
01859
01860 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, produced_cargo));
01861 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, accepts_cargo));
01862
01863 int CargoesField::small_height;
01864 int CargoesField::normal_height;
01865 int CargoesField::industry_width;
01866 const int CargoesField::VERT_INTER_INDUSTRY_SPACE = 6;
01867
01868 const int CargoesField::HOR_CARGO_BORDER_SPACE = 15;
01869 const int CargoesField::CARGO_STUB_WIDTH = 10;
01870 const int CargoesField::HOR_CARGO_WIDTH = 15;
01871 const int CargoesField::HOR_CARGO_SPACE = 5;
01872 const int CargoesField::VERT_CARGO_EDGE = 4;
01873 const int CargoesField::VERT_CARGO_SPACE = 4;
01874
01875 const int CargoesField::BLOB_DISTANCE = 5;
01876 const int CargoesField::BLOB_WIDTH = 12;
01877 const int CargoesField::BLOB_HEIGHT = 9;
01878
01880 const int CargoesField::CARGO_FIELD_WIDTH = HOR_CARGO_BORDER_SPACE * 2 + HOR_CARGO_WIDTH * MAX_CARGOES + HOR_CARGO_SPACE * (MAX_CARGOES - 1);
01881
01882 const int CargoesField::INDUSTRY_LINE_COLOUR = PC_YELLOW;
01883 const int CargoesField::CARGO_LINE_COLOUR = PC_YELLOW;
01884
01886 struct CargoesRow {
01887 CargoesField columns[5];
01888
01893 void ConnectIndustryProduced(int column)
01894 {
01895 CargoesField *ind_fld = this->columns + column;
01896 CargoesField *cargo_fld = this->columns + column + 1;
01897 assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
01898
01899 MemSetT(ind_fld->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
01900
01901 if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01902 CargoID others[MAX_CARGOES];
01903 int other_count = 0;
01904
01905 const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
01906 for (uint i = 0; i < lengthof(indsp->produced_cargo); i++) {
01907 int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
01908 if (col < 0) others[other_count++] = indsp->produced_cargo[i];
01909 }
01910
01911
01912 for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
01913 if (cargo_fld->u.cargo.supp_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_produced[i] = others[--other_count];
01914 }
01915 } else {
01916
01917 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01918 CargoID cid = cargo_fld->u.cargo.vertical_cargoes[i];
01919 if (cid == CT_PASSENGERS || cid == CT_MAIL) cargo_fld->ConnectCargo(cid, true);
01920 }
01921 }
01922 }
01923
01929 void MakeCargoLabel(int column, bool accepting)
01930 {
01931 CargoID cargoes[MAX_CARGOES];
01932 MemSetT(cargoes, INVALID_CARGO, lengthof(cargoes));
01933
01934 CargoesField *label_fld = this->columns + column;
01935 CargoesField *cargo_fld = this->columns + (accepting ? column - 1 : column + 1);
01936
01937 assert(cargo_fld->type == CFT_CARGO && label_fld->type == CFT_EMPTY);
01938 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01939 int col = cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], !accepting);
01940 cargoes[col] = cargo_fld->u.cargo.vertical_cargoes[i];
01941 }
01942 label_fld->MakeCargoLabel(cargoes, lengthof(cargoes), accepting);
01943 }
01944
01945
01950 void ConnectIndustryAccepted(int column)
01951 {
01952 CargoesField *ind_fld = this->columns + column;
01953 CargoesField *cargo_fld = this->columns + column - 1;
01954 assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
01955
01956 MemSetT(ind_fld->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
01957
01958 if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01959 CargoID others[MAX_CARGOES];
01960 int other_count = 0;
01961
01962 const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
01963 for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
01964 int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
01965 if (col < 0) others[other_count++] = indsp->accepts_cargo[i];
01966 }
01967
01968
01969 for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
01970 if (cargo_fld->u.cargo.cust_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_accepted[i] = others[--other_count];
01971 }
01972 } else {
01973
01974 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01975 for (uint h = 0; h < HOUSE_MAX; h++) {
01976 HouseSpec *hs = HouseSpec::Get(h);
01977 if (!hs->enabled) continue;
01978
01979 for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
01980 if (cargo_fld->u.cargo.vertical_cargoes[i] == hs->accepts_cargo[j]) {
01981 cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], false);
01982 goto next_cargo;
01983 }
01984 }
01985 }
01986 next_cargo: ;
01987 }
01988 }
01989 }
01990 };
01991
01992
02020 struct IndustryCargoesWindow : public Window {
02021 static const int HOR_TEXT_PADDING, VERT_TEXT_PADDING;
02022
02023 typedef SmallVector<CargoesRow, 4> Fields;
02024
02025 Fields fields;
02026 uint ind_cargo;
02027
02028 Scrollbar *vscroll;
02029
02030 IndustryCargoesWindow(int id) : Window()
02031 {
02032 this->OnInit();
02033 this->CreateNestedTree(&_industry_cargoes_desc);
02034 this->vscroll = this->GetScrollbar(WID_IC_SCROLLBAR);
02035 this->FinishInitNested(&_industry_cargoes_desc, 0);
02036 this->OnInvalidateData(id);
02037 }
02038
02039 virtual void OnInit()
02040 {
02041
02042 Dimension d = GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS);
02043 d = maxdim(d, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS));
02044 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
02045 d.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
02046 CargoesField::small_height = d.height;
02047
02048
02049 d.height = 0;
02050 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02051 const IndustrySpec *indsp = GetIndustrySpec(it);
02052 if (!indsp->enabled) continue;
02053 SetDParam(0, indsp->name);
02054 d = maxdim(d, GetStringBoundingBox(STR_JUST_STRING));
02055 }
02056
02057 for (uint i = 0; i < NUM_CARGO; i++) {
02058 const CargoSpec *csp = CargoSpec::Get(i);
02059 if (!csp->IsValid()) continue;
02060 d = maxdim(d, GetStringBoundingBox(csp->name));
02061 }
02062 d.width += 2 * HOR_TEXT_PADDING;
02063
02064 uint min_ind_height = CargoesField::VERT_CARGO_EDGE * 2 + MAX_CARGOES * FONT_HEIGHT_NORMAL + (MAX_CARGOES - 1) * CargoesField::VERT_CARGO_SPACE;
02065 d.height = max(d.height + 2 * VERT_TEXT_PADDING, min_ind_height);
02066
02067 CargoesField::industry_width = d.width;
02068 CargoesField::normal_height = d.height + CargoesField::VERT_INTER_INDUSTRY_SPACE;
02069 }
02070
02071 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02072 {
02073 if (widget != WID_IC_PANEL) return;
02074
02075 size->width = WD_FRAMETEXT_LEFT + CargoesField::industry_width * 3 + CargoesField::CARGO_FIELD_WIDTH * 2 + WD_FRAMETEXT_RIGHT;
02076 }
02077
02078
02079 CargoesFieldType type;
02080 virtual void SetStringParameters (int widget) const
02081 {
02082 if (widget != WID_IC_CAPTION) return;
02083
02084 if (this->ind_cargo < NUM_INDUSTRYTYPES) {
02085 const IndustrySpec *indsp = GetIndustrySpec(this->ind_cargo);
02086 SetDParam(0, indsp->name);
02087 } else {
02088 const CargoSpec *csp = CargoSpec::Get(this->ind_cargo - NUM_INDUSTRYTYPES);
02089 SetDParam(0, csp->name);
02090 }
02091 }
02092
02101 static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2)
02102 {
02103 while (length1 > 0) {
02104 if (*cargoes1 != INVALID_CARGO) {
02105 for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true;
02106 }
02107 cargoes1++;
02108 length1--;
02109 }
02110 return false;
02111 }
02112
02119 static bool HousesCanSupply(const CargoID *cargoes, uint length)
02120 {
02121 for (uint i = 0; i < length; i++) {
02122 if (cargoes[i] == INVALID_CARGO) continue;
02123 if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true;
02124 }
02125 return false;
02126 }
02127
02134 static bool HousesCanAccept(const CargoID *cargoes, uint length)
02135 {
02136 HouseZones climate_mask;
02137 switch (_settings_game.game_creation.landscape) {
02138 case LT_TEMPERATE: climate_mask = HZ_TEMP; break;
02139 case LT_ARCTIC: climate_mask = HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW; break;
02140 case LT_TROPIC: climate_mask = HZ_SUBTROPIC; break;
02141 case LT_TOYLAND: climate_mask = HZ_TOYLND; break;
02142 default: NOT_REACHED();
02143 }
02144 for (uint i = 0; i < length; i++) {
02145 if (cargoes[i] == INVALID_CARGO) continue;
02146
02147 for (uint h = 0; h < HOUSE_MAX; h++) {
02148 HouseSpec *hs = HouseSpec::Get(h);
02149 if (!hs->enabled || !(hs->building_availability & climate_mask)) continue;
02150
02151 for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
02152 if (cargoes[i] == hs->accepts_cargo[j]) return true;
02153 }
02154 }
02155 }
02156 return false;
02157 }
02158
02165 static int CountMatchingAcceptingIndustries(const CargoID *cargoes, uint length)
02166 {
02167 int count = 0;
02168 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02169 const IndustrySpec *indsp = GetIndustrySpec(it);
02170 if (!indsp->enabled) continue;
02171
02172 if (HasCommonValidCargo(cargoes, length, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) count++;
02173 }
02174 return count;
02175 }
02176
02183 static int CountMatchingProducingIndustries(const CargoID *cargoes, uint length)
02184 {
02185 int count = 0;
02186 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02187 const IndustrySpec *indsp = GetIndustrySpec(it);
02188 if (!indsp->enabled) continue;
02189
02190 if (HasCommonValidCargo(cargoes, length, indsp->produced_cargo, lengthof(indsp->produced_cargo))) count++;
02191 }
02192 return count;
02193 }
02194
02201 void ShortenCargoColumn(int column, int top, int bottom)
02202 {
02203 while (top < bottom && !this->fields[top].columns[column].HasConnection()) {
02204 this->fields[top].columns[column].MakeEmpty(CFT_EMPTY);
02205 top++;
02206 }
02207 this->fields[top].columns[column].u.cargo.top_end = true;
02208
02209 while (bottom > top && !this->fields[bottom].columns[column].HasConnection()) {
02210 this->fields[bottom].columns[column].MakeEmpty(CFT_EMPTY);
02211 bottom--;
02212 }
02213 this->fields[bottom].columns[column].u.cargo.bottom_end = true;
02214 }
02215
02222 void PlaceIndustry(int row, int col, IndustryType it)
02223 {
02224 assert(this->fields[row].columns[col].type == CFT_EMPTY);
02225 this->fields[row].columns[col].MakeIndustry(it);
02226 if (col == 0) {
02227 this->fields[row].ConnectIndustryProduced(col);
02228 } else {
02229 this->fields[row].ConnectIndustryAccepted(col);
02230 }
02231 }
02232
02236 void NotifySmallmap()
02237 {
02238 if (!this->IsWidgetLowered(WID_IC_NOTIFY)) return;
02239
02240
02241
02242 InvalidateWindowClassesData(WC_SMALLMAP, 0);
02243 }
02244
02249 void ComputeIndustryDisplay(IndustryType it)
02250 {
02251 this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION;
02252 this->ind_cargo = it;
02253 _displayed_industries = 1ULL << it;
02254
02255 this->fields.Clear();
02256 CargoesRow *row = this->fields.Append();
02257 row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
02258 row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
02259 row->columns[2].MakeEmpty(CFT_SMALL_EMPTY);
02260 row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
02261 row->columns[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
02262
02263 const IndustrySpec *central_sp = GetIndustrySpec(it);
02264 bool houses_supply = HousesCanSupply(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
02265 bool houses_accept = HousesCanAccept(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
02266
02267 int num_supp = CountMatchingProducingIndustries(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo)) + houses_supply;
02268 int num_cust = CountMatchingAcceptingIndustries(central_sp->produced_cargo, lengthof(central_sp->produced_cargo)) + houses_accept;
02269 int num_indrows = max(3, max(num_supp, num_cust));
02270 for (int i = 0; i < num_indrows; i++) {
02271 CargoesRow *row = this->fields.Append();
02272 row->columns[0].MakeEmpty(CFT_EMPTY);
02273 row->columns[1].MakeCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
02274 row->columns[2].MakeEmpty(CFT_EMPTY);
02275 row->columns[3].MakeCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
02276 row->columns[4].MakeEmpty(CFT_EMPTY);
02277 }
02278
02279 int central_row = 1 + num_indrows / 2;
02280 this->fields[central_row].columns[2].MakeIndustry(it);
02281 this->fields[central_row].ConnectIndustryProduced(2);
02282 this->fields[central_row].ConnectIndustryAccepted(2);
02283
02284
02285 this->fields[central_row - 1].MakeCargoLabel(2, true);
02286 this->fields[central_row + 1].MakeCargoLabel(2, false);
02287
02288
02289 int supp_count = 0;
02290 int cust_count = 0;
02291 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02292 const IndustrySpec *indsp = GetIndustrySpec(it);
02293 if (!indsp->enabled) continue;
02294
02295 if (HasCommonValidCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo), indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
02296 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
02297 SetBit(_displayed_industries, it);
02298 supp_count++;
02299 }
02300 if (HasCommonValidCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo), indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
02301 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, it);
02302 SetBit(_displayed_industries, it);
02303 cust_count++;
02304 }
02305 }
02306 if (houses_supply) {
02307 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
02308 supp_count++;
02309 }
02310 if (houses_accept) {
02311 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, NUM_INDUSTRYTYPES);
02312 cust_count++;
02313 }
02314
02315 this->ShortenCargoColumn(1, 1, num_indrows);
02316 this->ShortenCargoColumn(3, 1, num_indrows);
02317 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02318 this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
02319 this->SetDirty();
02320 this->NotifySmallmap();
02321 }
02322
02327 void ComputeCargoDisplay(CargoID cid)
02328 {
02329 this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_CARGO_CAPTION;
02330 this->ind_cargo = cid + NUM_INDUSTRYTYPES;
02331 _displayed_industries = 0;
02332
02333 this->fields.Clear();
02334 CargoesRow *row = this->fields.Append();
02335 row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
02336 row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
02337 row->columns[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
02338 row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
02339 row->columns[4].MakeEmpty(CFT_SMALL_EMPTY);
02340
02341 bool houses_supply = HousesCanSupply(&cid, 1);
02342 bool houses_accept = HousesCanAccept(&cid, 1);
02343 int num_supp = CountMatchingProducingIndustries(&cid, 1) + houses_supply + 1;
02344 int num_cust = CountMatchingAcceptingIndustries(&cid, 1) + houses_accept;
02345 int num_indrows = max(num_supp, num_cust);
02346 for (int i = 0; i < num_indrows; i++) {
02347 CargoesRow *row = this->fields.Append();
02348 row->columns[0].MakeEmpty(CFT_EMPTY);
02349 row->columns[1].MakeCargo(&cid, 1);
02350 row->columns[2].MakeEmpty(CFT_EMPTY);
02351 row->columns[3].MakeEmpty(CFT_EMPTY);
02352 row->columns[4].MakeEmpty(CFT_EMPTY);
02353 }
02354
02355 this->fields[num_indrows].MakeCargoLabel(0, false);
02356
02357
02358 int supp_count = 0;
02359 int cust_count = 0;
02360 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02361 const IndustrySpec *indsp = GetIndustrySpec(it);
02362 if (!indsp->enabled) continue;
02363
02364 if (HasCommonValidCargo(&cid, 1, indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
02365 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
02366 SetBit(_displayed_industries, it);
02367 supp_count++;
02368 }
02369 if (HasCommonValidCargo(&cid, 1, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
02370 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, it);
02371 SetBit(_displayed_industries, it);
02372 cust_count++;
02373 }
02374 }
02375 if (houses_supply) {
02376 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
02377 supp_count++;
02378 }
02379 if (houses_accept) {
02380 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, NUM_INDUSTRYTYPES);
02381 cust_count++;
02382 }
02383
02384 this->ShortenCargoColumn(1, 1, num_indrows);
02385 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02386 this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
02387 this->SetDirty();
02388 this->NotifySmallmap();
02389 }
02390
02398 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
02399 {
02400 if (!gui_scope) return;
02401 if (data == NUM_INDUSTRYTYPES) {
02402 if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
02403 this->RaiseWidget(WID_IC_NOTIFY);
02404 this->SetWidgetDirty(WID_IC_NOTIFY);
02405 }
02406 return;
02407 }
02408
02409 assert(data >= 0 && data < NUM_INDUSTRYTYPES);
02410 this->ComputeIndustryDisplay(data);
02411 }
02412
02413 virtual void DrawWidget(const Rect &r, int widget) const
02414 {
02415 if (widget != WID_IC_PANEL) return;
02416
02417 DrawPixelInfo tmp_dpi, *old_dpi;
02418 int width = r.right - r.left + 1;
02419 int height = r.bottom - r.top + 1 - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM;
02420 if (!FillDrawPixelInfo(&tmp_dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, width, height)) return;
02421 old_dpi = _cur_dpi;
02422 _cur_dpi = &tmp_dpi;
02423
02424 int left_pos = WD_FRAMERECT_LEFT;
02425 if (this->ind_cargo >= NUM_INDUSTRYTYPES) left_pos += (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2;
02426 int last_column = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
02427
02428 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02429 int vpos = -this->vscroll->GetPosition() * nwp->resize_y;
02430 for (uint i = 0; i < this->fields.Length(); i++) {
02431 int row_height = (i == 0) ? CargoesField::small_height : CargoesField::normal_height;
02432 if (vpos + row_height >= 0) {
02433 int xpos = left_pos;
02434 int col, dir;
02435 if (_current_text_dir == TD_RTL) {
02436 col = last_column;
02437 dir = -1;
02438 } else {
02439 col = 0;
02440 dir = 1;
02441 }
02442 while (col >= 0 && col <= last_column) {
02443 this->fields[i].columns[col].Draw(xpos, vpos);
02444 xpos += (col & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
02445 col += dir;
02446 }
02447 }
02448 vpos += row_height;
02449 if (vpos >= height) break;
02450 }
02451
02452 _cur_dpi = old_dpi;
02453 }
02454
02462 bool CalculatePositionInWidget(Point pt, Point *fieldxy, Point *xy)
02463 {
02464 const NWidgetBase *nw = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02465 pt.x -= nw->pos_x;
02466 pt.y -= nw->pos_y;
02467
02468 int vpos = WD_FRAMERECT_TOP + CargoesField::small_height - this->vscroll->GetPosition() * nw->resize_y;
02469 if (pt.y < vpos) return false;
02470
02471 int row = (pt.y - vpos) / CargoesField::normal_height;
02472 if (row + 1 >= (int)this->fields.Length()) return false;
02473 vpos = pt.y - vpos - row * CargoesField::normal_height;
02474 row++;
02475
02476 int xpos = 2 * WD_FRAMERECT_LEFT + ((this->ind_cargo < NUM_INDUSTRYTYPES) ? 0 : (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2);
02477 if (pt.x < xpos) return false;
02478 int column;
02479 for (column = 0; column <= 5; column++) {
02480 int width = (column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
02481 if (pt.x < xpos + width) break;
02482 xpos += width;
02483 }
02484 int num_columns = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
02485 if (column > num_columns) return false;
02486 xpos = pt.x - xpos;
02487
02488
02489 fieldxy->y = row;
02490 xy->y = vpos;
02491 if (_current_text_dir == TD_RTL) {
02492 fieldxy->x = num_columns - column;
02493 xy->x = ((column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width) - xpos;
02494 } else {
02495 fieldxy->x = column;
02496 xy->x = xpos;
02497 }
02498 return true;
02499 }
02500
02501 virtual void OnClick(Point pt, int widget, int click_count)
02502 {
02503 switch (widget) {
02504 case WID_IC_PANEL: {
02505 Point fieldxy, xy;
02506 if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
02507
02508 const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
02509 switch (fld->type) {
02510 case CFT_INDUSTRY:
02511 if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES) this->ComputeIndustryDisplay(fld->u.industry.ind_type);
02512 break;
02513
02514 case CFT_CARGO: {
02515 CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
02516 CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
02517 CargoID cid = fld->CargoClickedAt(lft, rgt, xy);
02518 if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
02519 break;
02520 }
02521
02522 case CFT_CARGO_LABEL: {
02523 CargoID cid = fld->CargoLabelClickedAt(xy);
02524 if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
02525 break;
02526 }
02527
02528 default:
02529 break;
02530 }
02531 break;
02532 }
02533
02534 case WID_IC_NOTIFY:
02535 this->ToggleWidgetLoweredState(WID_IC_NOTIFY);
02536 this->SetWidgetDirty(WID_IC_NOTIFY);
02537 SndPlayFx(SND_15_BEEP);
02538
02539 if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
02540 if (FindWindowByClass(WC_SMALLMAP) == NULL) ShowSmallMap();
02541 this->NotifySmallmap();
02542 }
02543 break;
02544 }
02545 }
02546
02547 virtual void OnHover(Point pt, int widget)
02548 {
02549 if (widget != WID_IC_PANEL) return;
02550
02551 Point fieldxy, xy;
02552 if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
02553
02554 const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
02555 CargoID cid = INVALID_CARGO;
02556 switch (fld->type) {
02557 case CFT_CARGO: {
02558 CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
02559 CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
02560 cid = fld->CargoClickedAt(lft, rgt, xy);
02561 break;
02562 }
02563
02564 case CFT_CARGO_LABEL: {
02565 cid = fld->CargoLabelClickedAt(xy);
02566 break;
02567 }
02568
02569 case CFT_INDUSTRY:
02570 if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) {
02571 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, 0, NULL, TCC_HOVER);
02572 }
02573 return;
02574
02575 default:
02576 break;
02577 }
02578 if (cid != INVALID_CARGO && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
02579 const CargoSpec *csp = CargoSpec::Get(cid);
02580 uint64 params[5];
02581 params[0] = csp->name;
02582 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, 1, params, TCC_HOVER);
02583 }
02584 }
02585
02586 virtual void OnResize()
02587 {
02588 this->vscroll->SetCapacityFromWidget(this, WID_IC_PANEL);
02589 }
02590 };
02591
02592 const int IndustryCargoesWindow::HOR_TEXT_PADDING = 5;
02593 const int IndustryCargoesWindow::VERT_TEXT_PADDING = 5;
02594
02599 static void ShowIndustryCargoesWindow(IndustryType id)
02600 {
02601 assert(id < NUM_INDUSTRYTYPES);
02602
02603 Window *w = BringWindowToFrontById(WC_INDUSTRY_CARGOES, 0);
02604 if (w != NULL) {
02605 w->InvalidateData(id);
02606 return;
02607 }
02608 new IndustryCargoesWindow(id);
02609 }