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