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