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 #include "widgets/dropdown_type.h"
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 uint text_left, text_right, icon_left, icon_right;
00383 if (_current_text_dir == TD_RTL) {
00384 icon_right = r.right - WD_MATRIX_RIGHT;
00385 icon_left = icon_right - 10;
00386 text_right = icon_right - BuildIndustryWindow::MATRIX_TEXT_OFFSET;
00387 text_left = r.left + WD_MATRIX_LEFT;
00388 } else {
00389 icon_left = r.left + WD_MATRIX_LEFT;
00390 icon_right = icon_left + 10;
00391 text_left = icon_left + BuildIndustryWindow::MATRIX_TEXT_OFFSET;
00392 text_right = r.right - WD_MATRIX_RIGHT;
00393 }
00394
00395 for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) {
00396 int y = r.top + WD_MATRIX_TOP + i * this->resize.step_height;
00397 bool selected = this->selected_index == i + this->vscroll->GetPosition();
00398
00399 if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) {
00400 DrawString(text_left, text_right, y, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES, selected ? TC_WHITE : TC_ORANGE);
00401 continue;
00402 }
00403 const IndustrySpec *indsp = GetIndustrySpec(this->index[i + this->vscroll->GetPosition()]);
00404
00405
00406 DrawString(text_left, text_right, y, indsp->name, selected ? TC_WHITE : TC_ORANGE);
00407 GfxFillRect(icon_left, y + 1, icon_right, y + 7, selected ? PC_WHITE : PC_BLACK);
00408 GfxFillRect(icon_left + 1, y + 2, icon_right - 1, y + 6, indsp->map_colour);
00409 }
00410 break;
00411 }
00412
00413 case WID_DPI_INFOPANEL: {
00414 int y = r.top + WD_FRAMERECT_TOP;
00415 int bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00416 int left = r.left + WD_FRAMERECT_LEFT;
00417 int right = r.right - WD_FRAMERECT_RIGHT;
00418
00419 if (this->selected_type == INVALID_INDUSTRYTYPE) {
00420 DrawStringMultiLine(left, right, y, bottom, STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES_TOOLTIP);
00421 break;
00422 }
00423
00424 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00425
00426 if (_game_mode != GM_EDITOR) {
00427 SetDParam(0, indsp->GetConstructionCost());
00428 DrawString(left, right, y, STR_FUND_INDUSTRY_INDUSTRY_BUILD_COST);
00429 y += FONT_HEIGHT_NORMAL;
00430 }
00431
00432
00433 char cargo_suffix[3][512];
00434 GetAllCargoSuffixes(0, CST_FUND, NULL, this->selected_type, indsp, indsp->accepts_cargo, cargo_suffix);
00435 StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
00436 byte p = 0;
00437 SetDParam(0, STR_JUST_NOTHING);
00438 SetDParamStr(1, "");
00439 for (byte j = 0; j < lengthof(indsp->accepts_cargo); j++) {
00440 if (indsp->accepts_cargo[j] == CT_INVALID) continue;
00441 if (p > 0) str++;
00442 SetDParam(p++, CargoSpec::Get(indsp->accepts_cargo[j])->name);
00443 SetDParamStr(p++, cargo_suffix[j]);
00444 }
00445 DrawString(left, right, y, str);
00446 y += FONT_HEIGHT_NORMAL;
00447
00448
00449 GetAllCargoSuffixes(3, CST_FUND, NULL, this->selected_type, indsp, indsp->produced_cargo, cargo_suffix);
00450 str = STR_INDUSTRY_VIEW_PRODUCES_CARGO;
00451 p = 0;
00452 SetDParam(0, STR_JUST_NOTHING);
00453 SetDParamStr(1, "");
00454 for (byte j = 0; j < lengthof(indsp->produced_cargo); j++) {
00455 if (indsp->produced_cargo[j] == CT_INVALID) continue;
00456 if (p > 0) str++;
00457 SetDParam(p++, CargoSpec::Get(indsp->produced_cargo[j])->name);
00458 SetDParamStr(p++, cargo_suffix[j]);
00459 }
00460 DrawString(left, right, y, str);
00461 y += FONT_HEIGHT_NORMAL;
00462
00463
00464 str = STR_NULL;
00465 if (HasBit(indsp->callback_mask, CBM_IND_FUND_MORE_TEXT)) {
00466 uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, this->selected_type, INVALID_TILE);
00467 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00468 if (callback_res > 0x400) {
00469 ErrorUnknownCallbackResult(indsp->grf_prop.grffile->grfid, CBID_INDUSTRY_FUND_MORE_TEXT, callback_res);
00470 } else {
00471 str = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);
00472 if (str != STR_UNDEFINED) {
00473 StartTextRefStackUsage(6);
00474 DrawStringMultiLine(left, right, y, bottom, str, TC_YELLOW);
00475 StopTextRefStackUsage();
00476 }
00477 }
00478 }
00479 }
00480 break;
00481 }
00482 }
00483 }
00484
00485 virtual void OnClick(Point pt, int widget, int click_count)
00486 {
00487 switch (widget) {
00488 case WID_DPI_MATRIX_WIDGET: {
00489 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_DPI_MATRIX_WIDGET);
00490 if (y < this->count) {
00491 this->selected_index = y;
00492 this->selected_type = this->index[y];
00493 const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
00494
00495 this->SetDirty();
00496
00497 if (_thd.GetCallbackWnd() == this &&
00498 ((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
00499 this->selected_type == INVALID_INDUSTRYTYPE ||
00500 !this->enabled[this->selected_index])) {
00501
00502 this->RaiseButtons();
00503 ResetObjectToPlace();
00504 }
00505
00506 this->SetButtons();
00507 if (this->enabled[this->selected_index] && click_count > 1) this->OnClick(pt, WID_DPI_FUND_WIDGET, 1);
00508 }
00509 break;
00510 }
00511
00512 case WID_DPI_DISPLAY_WIDGET:
00513 if (this->selected_type != INVALID_INDUSTRYTYPE) ShowIndustryCargoesWindow(this->selected_type);
00514 break;
00515
00516 case WID_DPI_FUND_WIDGET: {
00517 if (this->selected_type == INVALID_INDUSTRYTYPE) {
00518 this->HandleButtonClick(WID_DPI_FUND_WIDGET);
00519
00520 if (Town::GetNumItems() == 0) {
00521 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_INDUSTRIES, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO);
00522 } else {
00523 extern void GenerateIndustries();
00524 _generating_world = true;
00525 GenerateIndustries();
00526 _generating_world = false;
00527 }
00528 } else if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && GetIndustrySpec(this->selected_type)->IsRawIndustry()) {
00529 DoCommandP(0, this->selected_type, InteractiveRandom(), CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
00530 this->HandleButtonClick(WID_DPI_FUND_WIDGET);
00531 } else {
00532 HandlePlacePushButton(this, WID_DPI_FUND_WIDGET, SPR_CURSOR_INDUSTRY, HT_RECT);
00533 }
00534 break;
00535 }
00536 }
00537 }
00538
00539 virtual void OnResize()
00540 {
00541
00542 this->vscroll->SetCapacityFromWidget(this, WID_DPI_MATRIX_WIDGET);
00543 this->GetWidget<NWidgetCore>(WID_DPI_MATRIX_WIDGET)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00544 }
00545
00546 virtual void OnPlaceObject(Point pt, TileIndex tile)
00547 {
00548 bool success = true;
00549
00550 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00551 uint32 seed = InteractiveRandom();
00552
00553 if (_game_mode == GM_EDITOR) {
00554
00555 if (Town::GetNumItems() == 0) {
00556 SetDParam(0, indsp->name);
00557 ShowErrorMessage(STR_ERROR_CAN_T_BUILD_HERE, STR_ERROR_MUST_FOUND_TOWN_FIRST, WL_INFO, pt.x, pt.y);
00558 return;
00559 }
00560
00561 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
00562 _generating_world = true;
00563 _ignore_restrictions = true;
00564
00565 DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed,
00566 CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY), &CcBuildIndustry);
00567
00568 cur_company.Restore();
00569 _ignore_restrictions = false;
00570 _generating_world = false;
00571 } else {
00572 success = DoCommandP(tile, (InteractiveRandomRange(indsp->num_table) << 8) | this->selected_type, seed, CMD_BUILD_INDUSTRY | CMD_MSG(STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY));
00573 }
00574
00575
00576 if (success && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00577 }
00578
00579 virtual void OnTick()
00580 {
00581 if (_pause_mode != PM_UNPAUSED) return;
00582 if (!this->timer_enabled) return;
00583 if (--this->callback_timer == 0) {
00584
00585
00586 this->callback_timer = DAY_TICKS;
00587
00588 const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
00589
00590 if (indsp->enabled) {
00591 bool call_back_result = GetIndustryProbabilityCallback(this->selected_type, IACT_USERCREATION, 1) > 0;
00592
00593
00594 if (call_back_result != this->enabled[this->selected_index]) {
00595 this->enabled[this->selected_index] = call_back_result;
00596 this->SetButtons();
00597 this->SetDirty();
00598 }
00599 }
00600 }
00601 }
00602
00603 virtual void OnTimeout()
00604 {
00605 this->RaiseButtons();
00606 }
00607
00608 virtual void OnPlaceObjectAbort()
00609 {
00610 this->RaiseButtons();
00611 }
00612
00618 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00619 {
00620 if (!gui_scope) return;
00621 this->SetupArrays();
00622
00623 const IndustrySpec *indsp = (this->selected_type == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(this->selected_type);
00624 if (indsp == NULL) this->enabled[this->selected_index] = _settings_game.difficulty.industry_density != ID_FUND_ONLY;
00625 this->SetButtons();
00626 }
00627 };
00628
00629 void ShowBuildIndustryWindow()
00630 {
00631 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
00632 if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return;
00633 new BuildIndustryWindow();
00634 }
00635
00636 static void UpdateIndustryProduction(Industry *i);
00637
00638 static inline bool IsProductionAlterable(const Industry *i)
00639 {
00640 const IndustrySpec *is = GetIndustrySpec(i->type);
00641 return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
00642 (is->production_rate[0] != 0 || is->production_rate[1] != 0 || is->IsRawIndustry()));
00643 }
00644
00645 class IndustryViewWindow : public Window
00646 {
00648 enum Editability {
00649 EA_NONE,
00650 EA_MULTIPLIER,
00651 EA_RATE,
00652 };
00653
00655 enum InfoLine {
00656 IL_NONE,
00657 IL_MULTIPLIER,
00658 IL_RATE1,
00659 IL_RATE2,
00660 };
00661
00662 Editability editable;
00663 InfoLine editbox_line;
00664 InfoLine clicked_line;
00665 byte clicked_button;
00666 int production_offset_y;
00667 int info_height;
00668
00669 public:
00670 IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00671 {
00672 this->flags |= WF_DISABLE_VP_SCROLL;
00673 this->editbox_line = IL_NONE;
00674 this->clicked_line = IL_NONE;
00675 this->clicked_button = 0;
00676 this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1;
00677
00678 this->InitNested(desc, window_number);
00679 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_IV_VIEWPORT);
00680 nvp->InitializeViewport(this, Industry::Get(window_number)->location.GetCenterTile(), ZOOM_LVL_INDUSTRY);
00681
00682 this->InvalidateData();
00683 }
00684
00685 virtual void OnPaint()
00686 {
00687 this->DrawWidgets();
00688
00689 if (this->IsShaded()) return;
00690
00691 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_IV_INFO);
00692 uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
00693 if (expected > nwi->current_y - 1) {
00694 this->info_height = expected + 1;
00695 this->ReInit();
00696 return;
00697 }
00698 }
00699
00707 int DrawInfo(uint left, uint right, uint top)
00708 {
00709 Industry *i = Industry::Get(this->window_number);
00710 const IndustrySpec *ind = GetIndustrySpec(i->type);
00711 int y = top + WD_FRAMERECT_TOP;
00712 bool first = true;
00713 bool has_accept = false;
00714 char cargo_suffix[3][512];
00715
00716 if (HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) {
00717 GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
00718 for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00719 if (i->accepts_cargo[j] == CT_INVALID) continue;
00720 has_accept = true;
00721 if (first) {
00722 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_FOR_PROCESSING);
00723 y += FONT_HEIGHT_NORMAL;
00724 first = false;
00725 }
00726 SetDParam(0, i->accepts_cargo[j]);
00727 SetDParam(1, i->incoming_cargo_waiting[j]);
00728 SetDParamStr(2, cargo_suffix[j]);
00729 DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_WAITING_STOCKPILE_CARGO);
00730 y += FONT_HEIGHT_NORMAL;
00731 }
00732 } else {
00733 GetAllCargoSuffixes(0, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
00734 StringID str = STR_INDUSTRY_VIEW_REQUIRES_CARGO;
00735 byte p = 0;
00736 for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
00737 if (i->accepts_cargo[j] == CT_INVALID) continue;
00738 has_accept = true;
00739 if (p > 0) str++;
00740 SetDParam(p++, CargoSpec::Get(i->accepts_cargo[j])->name);
00741 SetDParamStr(p++, cargo_suffix[j]);
00742 }
00743 if (has_accept) {
00744 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, str);
00745 y += FONT_HEIGHT_NORMAL;
00746 }
00747 }
00748
00749 GetAllCargoSuffixes(3, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
00750 first = true;
00751 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00752 if (i->produced_cargo[j] == CT_INVALID) continue;
00753 if (first) {
00754 if (has_accept) y += WD_PAR_VSEP_WIDE;
00755 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
00756 y += FONT_HEIGHT_NORMAL;
00757 if (this->editable == EA_RATE) this->production_offset_y = y;
00758 first = false;
00759 }
00760
00761 SetDParam(0, i->produced_cargo[j]);
00762 SetDParam(1, i->last_month_production[j]);
00763 SetDParamStr(2, cargo_suffix[j]);
00764 SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
00765 uint x = left + WD_FRAMETEXT_LEFT + (this->editable == EA_RATE ? SETTING_BUTTON_WIDTH + 10 : 0);
00766 DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
00767
00768 if (this->editable == EA_RATE) {
00769 DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + j) ? this->clicked_button : 0,
00770 i->production_rate[j] > 0, i->production_rate[j] < 255);
00771 }
00772 y += FONT_HEIGHT_NORMAL;
00773 }
00774
00775
00776 if (this->editable == EA_MULTIPLIER) {
00777 y += WD_PAR_VSEP_WIDE;
00778 this->production_offset_y = y;
00779 SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
00780 uint x = left + WD_FRAMETEXT_LEFT + SETTING_BUTTON_WIDTH + 10;
00781 DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_PRODUCTION_LEVEL);
00782 DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0,
00783 i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM);
00784 y += FONT_HEIGHT_NORMAL;
00785 }
00786
00787
00788 if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
00789 uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
00790 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00791 if (callback_res > 0x400) {
00792 ErrorUnknownCallbackResult(ind->grf_prop.grffile->grfid, CBID_INDUSTRY_WINDOW_MORE_TEXT, callback_res);
00793 } else {
00794 StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
00795 if (message != STR_NULL && message != STR_UNDEFINED) {
00796 y += WD_PAR_VSEP_WIDE;
00797
00798 StartTextRefStackUsage(6);
00799
00800
00801
00802 y = DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, UINT16_MAX, message, TC_BLACK);
00803 StopTextRefStackUsage();
00804 }
00805 }
00806 }
00807 }
00808 return y + WD_FRAMERECT_BOTTOM;
00809 }
00810
00811 virtual void SetStringParameters(int widget) const
00812 {
00813 if (widget == WID_IV_CAPTION) SetDParam(0, this->window_number);
00814 }
00815
00816 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00817 {
00818 if (widget == WID_IV_INFO) size->height = this->info_height;
00819 }
00820
00821 virtual void OnClick(Point pt, int widget, int click_count)
00822 {
00823 switch (widget) {
00824 case WID_IV_INFO: {
00825 Industry *i = Industry::Get(this->window_number);
00826 InfoLine line = IL_NONE;
00827
00828 switch (this->editable) {
00829 case EA_NONE: break;
00830
00831 case EA_MULTIPLIER:
00832 if (IsInsideBS(pt.y, this->production_offset_y, FONT_HEIGHT_NORMAL)) line = IL_MULTIPLIER;
00833 break;
00834
00835 case EA_RATE:
00836 if (pt.y >= this->production_offset_y) {
00837 int row = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
00838 for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
00839 if (i->produced_cargo[j] == CT_INVALID) continue;
00840 row--;
00841 if (row < 0) {
00842 line = (InfoLine)(IL_RATE1 + j);
00843 break;
00844 }
00845 }
00846 }
00847 break;
00848 }
00849 if (line == IL_NONE) return;
00850
00851 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
00852 int left = nwi->pos_x + WD_FRAMETEXT_LEFT;
00853 int right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
00854 if (IsInsideMM(pt.x, left, left + SETTING_BUTTON_WIDTH)) {
00855
00856 byte button = (pt.x < left + SETTING_BUTTON_WIDTH / 2) ? 1 : 2;
00857 switch (this->editable) {
00858 case EA_MULTIPLIER:
00859 if (button == 1) {
00860 if (i->prod_level <= PRODLEVEL_MINIMUM) return;
00861 i->prod_level = max<uint>(i->prod_level / 2, PRODLEVEL_MINIMUM);
00862 } else {
00863 if (i->prod_level >= PRODLEVEL_MAXIMUM) return;
00864 i->prod_level = minu(i->prod_level * 2, PRODLEVEL_MAXIMUM);
00865 }
00866 break;
00867
00868 case EA_RATE:
00869 if (button == 1) {
00870 if (i->production_rate[line - IL_RATE1] <= 0) return;
00871 i->production_rate[line - IL_RATE1] = max(i->production_rate[line - IL_RATE1] / 2, 0);
00872 } else {
00873 if (i->production_rate[line - IL_RATE1] >= 255) return;
00874
00875 int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
00876 i->production_rate[line - IL_RATE1] = minu(new_prod, 255);
00877 }
00878 break;
00879
00880 default: NOT_REACHED();
00881 }
00882
00883 UpdateIndustryProduction(i);
00884 this->SetDirty();
00885 this->SetTimeout();
00886 this->clicked_line = line;
00887 this->clicked_button = button;
00888 } else if (IsInsideMM(pt.x, left + SETTING_BUTTON_WIDTH + 10, right)) {
00889
00890 this->editbox_line = line;
00891 switch (this->editable) {
00892 case EA_MULTIPLIER:
00893 SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT));
00894 ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION_LEVEL, 10, this, CS_ALPHANUMERAL, QSF_NONE);
00895 break;
00896
00897 case EA_RATE:
00898 SetDParam(0, i->production_rate[line - IL_RATE1] * 8);
00899 ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, this, CS_ALPHANUMERAL, QSF_NONE);
00900 break;
00901
00902 default: NOT_REACHED();
00903 }
00904 }
00905 break;
00906 }
00907
00908 case WID_IV_GOTO: {
00909 Industry *i = Industry::Get(this->window_number);
00910 if (_ctrl_pressed) {
00911 ShowExtraViewPortWindow(i->location.GetCenterTile());
00912 } else {
00913 ScrollMainWindowToTile(i->location.GetCenterTile());
00914 }
00915 break;
00916 }
00917
00918 case WID_IV_DISPLAY: {
00919 Industry *i = Industry::Get(this->window_number);
00920 ShowIndustryCargoesWindow(i->type);
00921 break;
00922 }
00923 }
00924 }
00925
00926 virtual void OnTimeout()
00927 {
00928 this->clicked_line = IL_NONE;
00929 this->clicked_button = 0;
00930 this->SetDirty();
00931 }
00932
00933 virtual void OnResize()
00934 {
00935 if (this->viewport != NULL) {
00936 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_IV_VIEWPORT);
00937 nvp->UpdateViewportCoordinates(this);
00938
00939 ScrollWindowToTile(Industry::Get(this->window_number)->location.GetCenterTile(), this, true);
00940 }
00941 }
00942
00943 virtual void OnQueryTextFinished(char *str)
00944 {
00945 if (StrEmpty(str)) return;
00946
00947 Industry *i = Industry::Get(this->window_number);
00948 uint value = atoi(str);
00949 switch (this->editbox_line) {
00950 case IL_NONE: NOT_REACHED();
00951
00952 case IL_MULTIPLIER:
00953 i->prod_level = ClampU(RoundDivSU(value * PRODLEVEL_DEFAULT, 100), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
00954 break;
00955
00956 default:
00957 i->production_rate[this->editbox_line - IL_RATE1] = ClampU(RoundDivSU(value, 8), 0, 255);
00958 break;
00959 }
00960 UpdateIndustryProduction(i);
00961 this->SetDirty();
00962 }
00963
00969 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00970 {
00971 if (!gui_scope) return;
00972 const Industry *i = Industry::Get(this->window_number);
00973 if (IsProductionAlterable(i)) {
00974 const IndustrySpec *ind = GetIndustrySpec(i->type);
00975 this->editable = ind->UsesSmoothEconomy() ? EA_RATE : EA_MULTIPLIER;
00976 } else {
00977 this->editable = EA_NONE;
00978 }
00979 }
00980
00981 virtual bool IsNewGRFInspectable() const
00982 {
00983 return ::IsNewGRFInspectable(GSF_INDUSTRIES, this->window_number);
00984 }
00985
00986 virtual void ShowNewGRFInspectWindow() const
00987 {
00988 ::ShowNewGRFInspectWindow(GSF_INDUSTRIES, this->window_number);
00989 }
00990 };
00991
00992 static void UpdateIndustryProduction(Industry *i)
00993 {
00994 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00995 if (!indspec->UsesSmoothEconomy()) i->RecomputeProductionMultipliers();
00996
00997 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
00998 if (i->produced_cargo[j] != CT_INVALID) {
00999 i->last_month_production[j] = 8 * i->production_rate[j];
01000 }
01001 }
01002 }
01003
01005 static const NWidgetPart _nested_industry_view_widgets[] = {
01006 NWidget(NWID_HORIZONTAL),
01007 NWidget(WWT_CLOSEBOX, COLOUR_CREAM),
01008 NWidget(WWT_CAPTION, COLOUR_CREAM, WID_IV_CAPTION), SetDataTip(STR_INDUSTRY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01009 NWidget(WWT_DEBUGBOX, COLOUR_CREAM),
01010 NWidget(WWT_SHADEBOX, COLOUR_CREAM),
01011 NWidget(WWT_STICKYBOX, COLOUR_CREAM),
01012 EndContainer(),
01013 NWidget(WWT_PANEL, COLOUR_CREAM),
01014 NWidget(WWT_INSET, COLOUR_CREAM), SetPadding(2, 2, 2, 2),
01015 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_IV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetPadding(1, 1, 1, 1), SetResize(1, 1),
01016 EndContainer(),
01017 EndContainer(),
01018 NWidget(WWT_PANEL, COLOUR_CREAM, WID_IV_INFO), SetMinimalSize(260, 2), SetResize(1, 0),
01019 EndContainer(),
01020 NWidget(NWID_HORIZONTAL),
01021 NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_GOTO), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_INDUSTRY_VIEW_LOCATION_TOOLTIP),
01022 NWidget(WWT_PUSHTXTBTN, COLOUR_CREAM, WID_IV_DISPLAY), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_INDUSTRY_DISPLAY_CHAIN, STR_INDUSTRY_DISPLAY_CHAIN_TOOLTIP),
01023 NWidget(WWT_RESIZEBOX, COLOUR_CREAM),
01024 EndContainer(),
01025 };
01026
01028 static const WindowDesc _industry_view_desc(
01029 WDP_AUTO, 260, 120,
01030 WC_INDUSTRY_VIEW, WC_NONE,
01031 0,
01032 _nested_industry_view_widgets, lengthof(_nested_industry_view_widgets)
01033 );
01034
01035 void ShowIndustryViewWindow(int industry)
01036 {
01037 AllocateWindowDescFront<IndustryViewWindow>(&_industry_view_desc, industry);
01038 }
01039
01041 static const NWidgetPart _nested_industry_directory_widgets[] = {
01042 NWidget(NWID_HORIZONTAL),
01043 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01044 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_INDUSTRY_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01045 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01046 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01047 EndContainer(),
01048 NWidget(NWID_HORIZONTAL),
01049 NWidget(NWID_VERTICAL),
01050 NWidget(NWID_HORIZONTAL),
01051 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_ID_DROPDOWN_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
01052 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_ID_DROPDOWN_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
01053 NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
01054 EndContainer(),
01055 NWidget(WWT_PANEL, COLOUR_BROWN, WID_ID_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(WID_ID_SCROLLBAR), EndContainer(),
01056 EndContainer(),
01057 NWidget(NWID_VERTICAL),
01058 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_ID_SCROLLBAR),
01059 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01060 EndContainer(),
01061 EndContainer(),
01062 };
01063
01064 typedef GUIList<const Industry*> GUIIndustryList;
01065
01066
01070 class IndustryDirectoryWindow : public Window {
01071 protected:
01072
01073 static Listing last_sorting;
01074 static const Industry *last_industry;
01075
01076
01077 static const StringID sorter_names[];
01078 static GUIIndustryList::SortFunction * const sorter_funcs[];
01079
01080 GUIIndustryList industries;
01081 Scrollbar *vscroll;
01082
01084 void BuildSortIndustriesList()
01085 {
01086 if (this->industries.NeedRebuild()) {
01087 this->industries.Clear();
01088
01089 const Industry *i;
01090 FOR_ALL_INDUSTRIES(i) {
01091 *this->industries.Append() = i;
01092 }
01093
01094 this->industries.Compact();
01095 this->industries.RebuildDone();
01096 this->vscroll->SetCount(this->industries.Length());
01097 }
01098
01099 if (!this->industries.Sort()) return;
01100 IndustryDirectoryWindow::last_industry = NULL;
01101 this->SetWidgetDirty(WID_ID_INDUSTRY_LIST);
01102 }
01103
01111 static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
01112 {
01113 assert(id < lengthof(i->produced_cargo));
01114
01115 if (i->produced_cargo[id] == CT_INVALID) return 101;
01116 return ToPercent8(i->last_month_pct_transported[id]);
01117 }
01118
01126 static int GetCargoTransportedSortValue(const Industry *i)
01127 {
01128 int p1 = GetCargoTransportedPercentsIfValid(i, 0);
01129 int p2 = GetCargoTransportedPercentsIfValid(i, 1);
01130
01131 if (p1 > p2) Swap(p1, p2);
01132
01133 return (p1 << 8) + p2;
01134 }
01135
01137 static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b)
01138 {
01139 static char buf_cache[96];
01140 static char buf[96];
01141
01142 SetDParam(0, (*a)->index);
01143 GetString(buf, STR_INDUSTRY_NAME, lastof(buf));
01144
01145 if (*b != last_industry) {
01146 last_industry = *b;
01147 SetDParam(0, (*b)->index);
01148 GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache));
01149 }
01150
01151 return strnatcmp(buf, buf_cache);
01152 }
01153
01155 static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b)
01156 {
01157 int it_a = 0;
01158 while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++;
01159 int it_b = 0;
01160 while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++;
01161 int r = it_a - it_b;
01162 return (r == 0) ? IndustryNameSorter(a, b) : r;
01163 }
01164
01166 static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b)
01167 {
01168 uint prod_a = 0, prod_b = 0;
01169 for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) {
01170 if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i];
01171 if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i];
01172 }
01173 int r = prod_a - prod_b;
01174
01175 return (r == 0) ? IndustryTypeSorter(a, b) : r;
01176 }
01177
01179 static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b)
01180 {
01181 int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b);
01182 return (r == 0) ? IndustryNameSorter(a, b) : r;
01183 }
01184
01190 StringID GetIndustryString(const Industry *i) const
01191 {
01192 const IndustrySpec *indsp = GetIndustrySpec(i->type);
01193 byte p = 0;
01194
01195
01196 SetDParam(p++, i->index);
01197
01198 static char cargo_suffix[lengthof(i->produced_cargo)][512];
01199 GetAllCargoSuffixes(3, CST_DIR, i, i->type, indsp, i->produced_cargo, cargo_suffix);
01200
01201
01202 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01203 if (i->produced_cargo[j] == CT_INVALID) continue;
01204 SetDParam(p++, i->produced_cargo[j]);
01205 SetDParam(p++, i->last_month_production[j]);
01206 SetDParamStr(p++, cargo_suffix[j]);
01207 }
01208
01209
01210 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01211 if (i->produced_cargo[j] == CT_INVALID) continue;
01212 SetDParam(p++, ToPercent8(i->last_month_pct_transported[j]));
01213 }
01214
01215
01216 switch (p) {
01217 case 1: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
01218 case 5: return STR_INDUSTRY_DIRECTORY_ITEM;
01219 default: return STR_INDUSTRY_DIRECTORY_ITEM_TWO;
01220 }
01221 }
01222
01223 public:
01224 IndustryDirectoryWindow(const WindowDesc *desc, WindowNumber number) : Window()
01225 {
01226 this->CreateNestedTree(desc);
01227 this->vscroll = this->GetScrollbar(WID_ID_SCROLLBAR);
01228
01229 this->industries.SetListing(this->last_sorting);
01230 this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
01231 this->industries.ForceRebuild();
01232 this->BuildSortIndustriesList();
01233
01234 this->FinishInitNested(desc, 0);
01235 }
01236
01237 ~IndustryDirectoryWindow()
01238 {
01239 this->last_sorting = this->industries.GetListing();
01240 }
01241
01242 virtual void SetStringParameters(int widget) const
01243 {
01244 if (widget == WID_ID_DROPDOWN_CRITERIA) SetDParam(0, IndustryDirectoryWindow::sorter_names[this->industries.SortType()]);
01245 }
01246
01247 virtual void DrawWidget(const Rect &r, int widget) const
01248 {
01249 switch (widget) {
01250 case WID_ID_DROPDOWN_ORDER:
01251 this->DrawSortButtonState(widget, this->industries.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
01252 break;
01253
01254 case WID_ID_INDUSTRY_LIST: {
01255 int n = 0;
01256 int y = r.top + WD_FRAMERECT_TOP;
01257 if (this->industries.Length() == 0) {
01258 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_DIRECTORY_NONE);
01259 break;
01260 }
01261 for (uint i = this->vscroll->GetPosition(); i < this->industries.Length(); i++) {
01262 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, this->GetIndustryString(this->industries[i]));
01263
01264 y += this->resize.step_height;
01265 if (++n == this->vscroll->GetCapacity()) break;
01266 }
01267 break;
01268 }
01269 }
01270 }
01271
01272 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01273 {
01274 switch (widget) {
01275 case WID_ID_DROPDOWN_ORDER: {
01276 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
01277 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2;
01278 d.height += padding.height;
01279 *size = maxdim(*size, d);
01280 break;
01281 }
01282
01283 case WID_ID_DROPDOWN_CRITERIA: {
01284 Dimension d = {0, 0};
01285 for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
01286 d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
01287 }
01288 d.width += padding.width;
01289 d.height += padding.height;
01290 *size = maxdim(*size, d);
01291 break;
01292 }
01293
01294 case WID_ID_INDUSTRY_LIST: {
01295 Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
01296 for (uint i = 0; i < this->industries.Length(); i++) {
01297 d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
01298 }
01299 resize->height = d.height;
01300 d.height *= 5;
01301 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01302 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
01303 *size = maxdim(*size, d);
01304 break;
01305 }
01306 }
01307 }
01308
01309
01310 virtual void OnClick(Point pt, int widget, int click_count)
01311 {
01312 switch (widget) {
01313 case WID_ID_DROPDOWN_ORDER:
01314 this->industries.ToggleSortOrder();
01315 this->SetDirty();
01316 break;
01317
01318 case WID_ID_DROPDOWN_CRITERIA:
01319 ShowDropDownMenu(this, IndustryDirectoryWindow::sorter_names, this->industries.SortType(), WID_ID_DROPDOWN_CRITERIA, 0, 0);
01320 break;
01321
01322 case WID_ID_INDUSTRY_LIST: {
01323 uint p = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_ID_INDUSTRY_LIST, WD_FRAMERECT_TOP);
01324 if (p < this->industries.Length()) {
01325 if (_ctrl_pressed) {
01326 ShowExtraViewPortWindow(this->industries[p]->location.tile);
01327 } else {
01328 ScrollMainWindowToTile(this->industries[p]->location.tile);
01329 }
01330 }
01331 break;
01332 }
01333 }
01334 }
01335
01336 virtual void OnDropdownSelect(int widget, int index)
01337 {
01338 if (this->industries.SortType() != index) {
01339 this->industries.SetSortType(index);
01340 this->BuildSortIndustriesList();
01341 }
01342 }
01343
01344 virtual void OnResize()
01345 {
01346 this->vscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST);
01347 }
01348
01349 virtual void OnPaint()
01350 {
01351 if (this->industries.NeedRebuild()) this->BuildSortIndustriesList();
01352 this->DrawWidgets();
01353 }
01354
01355 virtual void OnHundredthTick()
01356 {
01357 this->industries.ForceResort();
01358 this->BuildSortIndustriesList();
01359 }
01360
01366 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01367 {
01368 if (data == 0) {
01369
01370 this->industries.ForceRebuild();
01371 } else {
01372 this->industries.ForceResort();
01373 }
01374 }
01375 };
01376
01377 Listing IndustryDirectoryWindow::last_sorting = {false, 0};
01378 const Industry *IndustryDirectoryWindow::last_industry = NULL;
01379
01380
01381 GUIIndustryList::SortFunction * const IndustryDirectoryWindow::sorter_funcs[] = {
01382 &IndustryNameSorter,
01383 &IndustryTypeSorter,
01384 &IndustryProductionSorter,
01385 &IndustryTransportedCargoSorter
01386 };
01387
01388
01389 const StringID IndustryDirectoryWindow::sorter_names[] = {
01390 STR_SORT_BY_NAME,
01391 STR_SORT_BY_TYPE,
01392 STR_SORT_BY_PRODUCTION,
01393 STR_SORT_BY_TRANSPORTED,
01394 INVALID_STRING_ID
01395 };
01396
01397
01399 static const WindowDesc _industry_directory_desc(
01400 WDP_AUTO, 428, 190,
01401 WC_INDUSTRY_DIRECTORY, WC_NONE,
01402 0,
01403 _nested_industry_directory_widgets, lengthof(_nested_industry_directory_widgets)
01404 );
01405
01406 void ShowIndustryDirectory()
01407 {
01408 AllocateWindowDescFront<IndustryDirectoryWindow>(&_industry_directory_desc, 0);
01409 }
01410
01412 static const NWidgetPart _nested_industry_cargoes_widgets[] = {
01413 NWidget(NWID_HORIZONTAL),
01414 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01415 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_IC_CAPTION), SetDataTip(STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01416 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01417 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01418 EndContainer(),
01419 NWidget(NWID_HORIZONTAL),
01420 NWidget(NWID_VERTICAL),
01421 NWidget(WWT_PANEL, COLOUR_BROWN, WID_IC_PANEL), SetResize(1, 10), SetMinimalSize(200, 90), SetScrollbar(WID_IC_SCROLLBAR), EndContainer(),
01422 NWidget(NWID_HORIZONTAL),
01423 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_IC_NOTIFY),
01424 SetDataTip(STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP, STR_INDUSTRY_CARGOES_NOTIFY_SMALLMAP_TOOLTIP),
01425 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(0, 0), EndContainer(),
01426 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_IC_IND_DROPDOWN), SetFill(0, 0), SetResize(0, 0),
01427 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY, STR_INDUSTRY_CARGOES_SELECT_INDUSTRY_TOOLTIP),
01428 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_IC_CARGO_DROPDOWN), SetFill(0, 0), SetResize(0, 0),
01429 SetDataTip(STR_INDUSTRY_CARGOES_SELECT_CARGO, STR_INDUSTRY_CARGOES_SELECT_CARGO_TOOLTIP),
01430 EndContainer(),
01431 EndContainer(),
01432 NWidget(NWID_VERTICAL),
01433 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_IC_SCROLLBAR),
01434 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01435 EndContainer(),
01436 EndContainer(),
01437 };
01438
01440 static const WindowDesc _industry_cargoes_desc(
01441 WDP_AUTO, 300, 210,
01442 WC_INDUSTRY_CARGOES, WC_NONE,
01443 0,
01444 _nested_industry_cargoes_widgets, lengthof(_nested_industry_cargoes_widgets)
01445 );
01446
01448 enum CargoesFieldType {
01449 CFT_EMPTY,
01450 CFT_SMALL_EMPTY,
01451 CFT_INDUSTRY,
01452 CFT_CARGO,
01453 CFT_CARGO_LABEL,
01454 CFT_HEADER,
01455 };
01456
01457 static const uint MAX_CARGOES = 3;
01458
01460 struct CargoesField {
01461 static const int VERT_INTER_INDUSTRY_SPACE;
01462 static const int HOR_CARGO_BORDER_SPACE;
01463 static const int CARGO_STUB_WIDTH;
01464 static const int HOR_CARGO_WIDTH, HOR_CARGO_SPACE;
01465 static const int CARGO_FIELD_WIDTH;
01466 static const int VERT_CARGO_SPACE, VERT_CARGO_EDGE;
01467 static const int BLOB_DISTANCE, BLOB_WIDTH, BLOB_HEIGHT;
01468
01469 static const int INDUSTRY_LINE_COLOUR;
01470 static const int CARGO_LINE_COLOUR;
01471
01472 static int small_height, normal_height;
01473 static int industry_width;
01474
01475 CargoesFieldType type;
01476 union {
01477 struct {
01478 IndustryType ind_type;
01479 CargoID other_produced[MAX_CARGOES];
01480 CargoID other_accepted[MAX_CARGOES];
01481 } industry;
01482 struct {
01483 CargoID vertical_cargoes[MAX_CARGOES];
01484 byte num_cargoes;
01485 CargoID supp_cargoes[MAX_CARGOES];
01486 byte top_end;
01487 CargoID cust_cargoes[MAX_CARGOES];
01488 byte bottom_end;
01489 } cargo;
01490 struct {
01491 CargoID cargoes[MAX_CARGOES];
01492 bool left_align;
01493 } cargo_label;
01494 StringID header;
01495 } u;
01496
01501 void MakeEmpty(CargoesFieldType type)
01502 {
01503 this->type = type;
01504 }
01505
01511 void MakeIndustry(IndustryType ind_type)
01512 {
01513 this->type = CFT_INDUSTRY;
01514 this->u.industry.ind_type = ind_type;
01515 MemSetT(this->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
01516 MemSetT(this->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
01517 }
01518
01525 int ConnectCargo(CargoID cargo, bool producer)
01526 {
01527 assert(this->type == CFT_CARGO);
01528 if (cargo == INVALID_CARGO) return -1;
01529
01530
01531 int column = -1;
01532 for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
01533 if (cargo == this->u.cargo.vertical_cargoes[i]) {
01534 column = i;
01535 break;
01536 }
01537 }
01538 if (column < 0) return -1;
01539
01540 if (producer) {
01541 assert(this->u.cargo.supp_cargoes[column] == INVALID_CARGO);
01542 this->u.cargo.supp_cargoes[column] = column;
01543 } else {
01544 assert(this->u.cargo.cust_cargoes[column] == INVALID_CARGO);
01545 this->u.cargo.cust_cargoes[column] = column;
01546 }
01547 return column;
01548 }
01549
01554 bool HasConnection()
01555 {
01556 assert(this->type == CFT_CARGO);
01557
01558 for (uint i = 0; i < MAX_CARGOES; i++) {
01559 if (this->u.cargo.supp_cargoes[i] != INVALID_CARGO) return true;
01560 if (this->u.cargo.cust_cargoes[i] != INVALID_CARGO) return true;
01561 }
01562 return false;
01563 }
01564
01574 void MakeCargo(const CargoID *cargoes, uint length, int count = -1, bool top_end = false, bool bottom_end = false)
01575 {
01576 this->type = CFT_CARGO;
01577 uint i;
01578 uint num = 0;
01579 for (i = 0; i < MAX_CARGOES && i < length; i++) {
01580 if (cargoes[i] != INVALID_CARGO) {
01581 this->u.cargo.vertical_cargoes[num] = cargoes[i];
01582 num++;
01583 }
01584 }
01585 this->u.cargo.num_cargoes = (count < 0) ? num : count;
01586 for (; num < MAX_CARGOES; num++) this->u.cargo.vertical_cargoes[num] = INVALID_CARGO;
01587 this->u.cargo.top_end = top_end;
01588 this->u.cargo.bottom_end = bottom_end;
01589 MemSetT(this->u.cargo.supp_cargoes, INVALID_CARGO, MAX_CARGOES);
01590 MemSetT(this->u.cargo.cust_cargoes, INVALID_CARGO, MAX_CARGOES);
01591 }
01592
01599 void MakeCargoLabel(const CargoID *cargoes, uint length, bool left_align)
01600 {
01601 this->type = CFT_CARGO_LABEL;
01602 uint i;
01603 for (i = 0; i < MAX_CARGOES && i < length; i++) this->u.cargo_label.cargoes[i] = cargoes[i];
01604 for (; i < MAX_CARGOES; i++) this->u.cargo_label.cargoes[i] = INVALID_CARGO;
01605 this->u.cargo_label.left_align = left_align;
01606 }
01607
01612 void MakeHeader(StringID textid)
01613 {
01614 this->type = CFT_HEADER;
01615 this->u.header = textid;
01616 }
01617
01623 int GetCargoBase(int xpos) const
01624 {
01625 assert(this->type == CFT_CARGO);
01626
01627 switch (this->u.cargo.num_cargoes) {
01628 case 0: return xpos + CARGO_FIELD_WIDTH / 2;
01629 case 1: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH / 2;
01630 case 2: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE / 2;
01631 case 3: return xpos + CARGO_FIELD_WIDTH / 2 - HOR_CARGO_WIDTH - HOR_CARGO_SPACE - HOR_CARGO_WIDTH / 2;
01632 default: NOT_REACHED();
01633 }
01634 }
01635
01641 void Draw(int xpos, int ypos) const
01642 {
01643 switch (this->type) {
01644 case CFT_EMPTY:
01645 case CFT_SMALL_EMPTY:
01646 break;
01647
01648 case CFT_HEADER:
01649 ypos += (small_height - FONT_HEIGHT_NORMAL) / 2;
01650 DrawString(xpos, xpos + industry_width, ypos, this->u.header, TC_WHITE, SA_HOR_CENTER);
01651 break;
01652
01653 case CFT_INDUSTRY: {
01654 int ypos1 = ypos + VERT_INTER_INDUSTRY_SPACE / 2;
01655 int ypos2 = ypos + normal_height - 1 - VERT_INTER_INDUSTRY_SPACE / 2;
01656 int xpos2 = xpos + industry_width - 1;
01657 GfxDrawLine(xpos, ypos1, xpos2, ypos1, INDUSTRY_LINE_COLOUR);
01658 GfxDrawLine(xpos, ypos1, xpos, ypos2, INDUSTRY_LINE_COLOUR);
01659 GfxDrawLine(xpos, ypos2, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
01660 GfxDrawLine(xpos2, ypos1, xpos2, ypos2, INDUSTRY_LINE_COLOUR);
01661 ypos += (normal_height - FONT_HEIGHT_NORMAL) / 2;
01662 if (this->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01663 const IndustrySpec *indsp = GetIndustrySpec(this->u.industry.ind_type);
01664 SetDParam(0, indsp->name);
01665 DrawString(xpos, xpos2, ypos, STR_JUST_STRING, TC_WHITE, SA_HOR_CENTER);
01666
01667
01668 int blob_left, blob_right;
01669 if (_current_text_dir == TD_RTL) {
01670 blob_right = xpos2 - BLOB_DISTANCE;
01671 blob_left = blob_right - BLOB_WIDTH;
01672 } else {
01673 blob_left = xpos + BLOB_DISTANCE;
01674 blob_right = blob_left + BLOB_WIDTH;
01675 }
01676 GfxFillRect(blob_left, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT, blob_right, ypos2 - BLOB_DISTANCE, PC_BLACK);
01677 GfxFillRect(blob_left + 1, ypos2 - BLOB_DISTANCE - BLOB_HEIGHT + 1, blob_right - 1, ypos2 - BLOB_DISTANCE - 1, indsp->map_colour);
01678 } else {
01679 DrawString(xpos, xpos2, ypos, STR_INDUSTRY_CARGOES_HOUSES, TC_FROMSTRING, SA_HOR_CENTER);
01680 }
01681
01682
01683 const CargoID *other_right, *other_left;
01684 if (_current_text_dir == TD_RTL) {
01685 other_right = this->u.industry.other_accepted;
01686 other_left = this->u.industry.other_produced;
01687 } else {
01688 other_right = this->u.industry.other_produced;
01689 other_left = this->u.industry.other_accepted;
01690 }
01691 ypos1 += VERT_CARGO_EDGE;
01692 for (uint i = 0; i < MAX_CARGOES; i++) {
01693 if (other_right[i] != INVALID_CARGO) {
01694 const CargoSpec *csp = CargoSpec::Get(other_right[i]);
01695 int xp = xpos + industry_width + CARGO_STUB_WIDTH;
01696 DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp);
01697 GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01698 }
01699 if (other_left[i] != INVALID_CARGO) {
01700 const CargoSpec *csp = CargoSpec::Get(other_left[i]);
01701 int xp = xpos - CARGO_STUB_WIDTH;
01702 DrawHorConnection(xp + 1, xpos - 1, ypos1, csp);
01703 GfxDrawLine(xp, ypos1, xp, ypos1 + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01704 }
01705 ypos1 += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01706 }
01707 break;
01708 }
01709
01710 case CFT_CARGO: {
01711 int cargo_base = this->GetCargoBase(xpos);
01712 int top = ypos + (this->u.cargo.top_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0);
01713 int bot = ypos - (this->u.cargo.bottom_end ? VERT_INTER_INDUSTRY_SPACE / 2 + 1 : 0) + normal_height - 1;
01714 int colpos = cargo_base;
01715 for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
01716 if (this->u.cargo.top_end) GfxDrawLine(colpos, top - 1, colpos + HOR_CARGO_WIDTH - 1, top - 1, CARGO_LINE_COLOUR);
01717 if (this->u.cargo.bottom_end) GfxDrawLine(colpos, bot + 1, colpos + HOR_CARGO_WIDTH - 1, bot + 1, CARGO_LINE_COLOUR);
01718 GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
01719 colpos++;
01720 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[i]);
01721 GfxFillRect(colpos, top, colpos + HOR_CARGO_WIDTH - 2, bot, csp->legend_colour, FILLRECT_OPAQUE);
01722 colpos += HOR_CARGO_WIDTH - 2;
01723 GfxDrawLine(colpos, top, colpos, bot, CARGO_LINE_COLOUR);
01724 colpos += 1 + HOR_CARGO_SPACE;
01725 }
01726
01727 const CargoID *hor_left, *hor_right;
01728 if (_current_text_dir == TD_RTL) {
01729 hor_left = this->u.cargo.cust_cargoes;
01730 hor_right = this->u.cargo.supp_cargoes;
01731 } else {
01732 hor_left = this->u.cargo.supp_cargoes;
01733 hor_right = this->u.cargo.cust_cargoes;
01734 }
01735 ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
01736 for (uint i = 0; i < MAX_CARGOES; i++) {
01737 if (hor_left[i] != INVALID_CARGO) {
01738 int col = hor_left[i];
01739 int dx = 0;
01740 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
01741 for (; col > 0; col--) {
01742 int lf = cargo_base + col * HOR_CARGO_WIDTH + (col - 1) * HOR_CARGO_SPACE;
01743 DrawHorConnection(lf, lf + HOR_CARGO_SPACE - dx, ypos, csp);
01744 dx = 1;
01745 }
01746 DrawHorConnection(xpos, cargo_base - dx, ypos, csp);
01747 }
01748 if (hor_right[i] != INVALID_CARGO) {
01749 int col = hor_right[i];
01750 int dx = 0;
01751 const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
01752 for (; col < this->u.cargo.num_cargoes - 1; col++) {
01753 int lf = cargo_base + (col + 1) * HOR_CARGO_WIDTH + col * HOR_CARGO_SPACE;
01754 DrawHorConnection(lf + dx - 1, lf + HOR_CARGO_SPACE - 1, ypos, csp);
01755 dx = 1;
01756 }
01757 DrawHorConnection(cargo_base + col * HOR_CARGO_SPACE + (col + 1) * HOR_CARGO_WIDTH - 1 + dx, xpos + CARGO_FIELD_WIDTH - 1, ypos, csp);
01758 }
01759 ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01760 }
01761 break;
01762 }
01763
01764 case CFT_CARGO_LABEL:
01765 ypos += VERT_CARGO_EDGE + VERT_INTER_INDUSTRY_SPACE / 2;
01766 for (uint i = 0; i < MAX_CARGOES; i++) {
01767 if (this->u.cargo_label.cargoes[i] != INVALID_CARGO) {
01768 const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]);
01769 DrawString(xpos + WD_FRAMERECT_LEFT, xpos + industry_width - 1 - WD_FRAMERECT_RIGHT, ypos, csp->name, TC_WHITE,
01770 (this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT);
01771 }
01772 ypos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01773 }
01774 break;
01775
01776 default:
01777 NOT_REACHED();
01778 }
01779 }
01780
01788 CargoID CargoClickedAt(const CargoesField *left, const CargoesField *right, Point pt) const
01789 {
01790 assert(this->type == CFT_CARGO);
01791
01792
01793 int cpos = this->GetCargoBase(0);
01794 uint col;
01795 for (col = 0; col < this->u.cargo.num_cargoes; col++) {
01796 if (pt.x < cpos) break;
01797 if (pt.x < cpos + CargoesField::HOR_CARGO_WIDTH) return this->u.cargo.vertical_cargoes[col];
01798 cpos += CargoesField::HOR_CARGO_WIDTH + CargoesField::HOR_CARGO_SPACE;
01799 }
01800
01801
01802 int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
01803 uint row;
01804 for (row = 0; row < MAX_CARGOES; row++) {
01805 if (pt.y < vpos) return INVALID_CARGO;
01806 if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
01807 vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01808 }
01809 if (row == MAX_CARGOES) return INVALID_CARGO;
01810
01811
01812 if (col == 0) {
01813 if (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]];
01814 if (left != NULL) {
01815 if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row];
01816 if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row];
01817 }
01818 return INVALID_CARGO;
01819 }
01820 if (col == this->u.cargo.num_cargoes) {
01821 if (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]];
01822 if (right != NULL) {
01823 if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row];
01824 if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row];
01825 }
01826 return INVALID_CARGO;
01827 }
01828 if (row >= col) {
01829
01830
01831
01832
01833 return (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO;
01834 } else {
01835
01836 return (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO;
01837 }
01838 }
01839
01845 CargoID CargoLabelClickedAt(Point pt) const
01846 {
01847 assert(this->type == CFT_CARGO_LABEL);
01848
01849 int vpos = VERT_INTER_INDUSTRY_SPACE / 2 + VERT_CARGO_EDGE;
01850 uint row;
01851 for (row = 0; row < MAX_CARGOES; row++) {
01852 if (pt.y < vpos) return INVALID_CARGO;
01853 if (pt.y < vpos + FONT_HEIGHT_NORMAL) break;
01854 vpos += FONT_HEIGHT_NORMAL + VERT_CARGO_SPACE;
01855 }
01856 if (row == MAX_CARGOES) return INVALID_CARGO;
01857 return this->u.cargo_label.cargoes[row];
01858 }
01859
01860 private:
01868 static void DrawHorConnection(int left, int right, int top, const CargoSpec *csp)
01869 {
01870 GfxDrawLine(left, top, right, top, CARGO_LINE_COLOUR);
01871 GfxFillRect(left, top + 1, right, top + FONT_HEIGHT_NORMAL - 2, csp->legend_colour, FILLRECT_OPAQUE);
01872 GfxDrawLine(left, top + FONT_HEIGHT_NORMAL - 1, right, top + FONT_HEIGHT_NORMAL - 1, CARGO_LINE_COLOUR);
01873 }
01874 };
01875
01876 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, produced_cargo));
01877 assert_compile(MAX_CARGOES >= cpp_lengthof(IndustrySpec, accepts_cargo));
01878
01879 int CargoesField::small_height;
01880 int CargoesField::normal_height;
01881 int CargoesField::industry_width;
01882 const int CargoesField::VERT_INTER_INDUSTRY_SPACE = 6;
01883
01884 const int CargoesField::HOR_CARGO_BORDER_SPACE = 15;
01885 const int CargoesField::CARGO_STUB_WIDTH = 10;
01886 const int CargoesField::HOR_CARGO_WIDTH = 15;
01887 const int CargoesField::HOR_CARGO_SPACE = 5;
01888 const int CargoesField::VERT_CARGO_EDGE = 4;
01889 const int CargoesField::VERT_CARGO_SPACE = 4;
01890
01891 const int CargoesField::BLOB_DISTANCE = 5;
01892 const int CargoesField::BLOB_WIDTH = 12;
01893 const int CargoesField::BLOB_HEIGHT = 9;
01894
01896 const int CargoesField::CARGO_FIELD_WIDTH = HOR_CARGO_BORDER_SPACE * 2 + HOR_CARGO_WIDTH * MAX_CARGOES + HOR_CARGO_SPACE * (MAX_CARGOES - 1);
01897
01898 const int CargoesField::INDUSTRY_LINE_COLOUR = PC_YELLOW;
01899 const int CargoesField::CARGO_LINE_COLOUR = PC_YELLOW;
01900
01902 struct CargoesRow {
01903 CargoesField columns[5];
01904
01909 void ConnectIndustryProduced(int column)
01910 {
01911 CargoesField *ind_fld = this->columns + column;
01912 CargoesField *cargo_fld = this->columns + column + 1;
01913 assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
01914
01915 MemSetT(ind_fld->u.industry.other_produced, INVALID_CARGO, MAX_CARGOES);
01916
01917 if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01918 CargoID others[MAX_CARGOES];
01919 int other_count = 0;
01920
01921 const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
01922 for (uint i = 0; i < lengthof(indsp->produced_cargo); i++) {
01923 int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
01924 if (col < 0) others[other_count++] = indsp->produced_cargo[i];
01925 }
01926
01927
01928 for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
01929 if (cargo_fld->u.cargo.supp_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_produced[i] = others[--other_count];
01930 }
01931 } else {
01932
01933 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01934 CargoID cid = cargo_fld->u.cargo.vertical_cargoes[i];
01935 if (cid == CT_PASSENGERS || cid == CT_MAIL) cargo_fld->ConnectCargo(cid, true);
01936 }
01937 }
01938 }
01939
01945 void MakeCargoLabel(int column, bool accepting)
01946 {
01947 CargoID cargoes[MAX_CARGOES];
01948 MemSetT(cargoes, INVALID_CARGO, lengthof(cargoes));
01949
01950 CargoesField *label_fld = this->columns + column;
01951 CargoesField *cargo_fld = this->columns + (accepting ? column - 1 : column + 1);
01952
01953 assert(cargo_fld->type == CFT_CARGO && label_fld->type == CFT_EMPTY);
01954 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01955 int col = cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], !accepting);
01956 cargoes[col] = cargo_fld->u.cargo.vertical_cargoes[i];
01957 }
01958 label_fld->MakeCargoLabel(cargoes, lengthof(cargoes), accepting);
01959 }
01960
01961
01966 void ConnectIndustryAccepted(int column)
01967 {
01968 CargoesField *ind_fld = this->columns + column;
01969 CargoesField *cargo_fld = this->columns + column - 1;
01970 assert(ind_fld->type == CFT_INDUSTRY && cargo_fld->type == CFT_CARGO);
01971
01972 MemSetT(ind_fld->u.industry.other_accepted, INVALID_CARGO, MAX_CARGOES);
01973
01974 if (ind_fld->u.industry.ind_type < NUM_INDUSTRYTYPES) {
01975 CargoID others[MAX_CARGOES];
01976 int other_count = 0;
01977
01978 const IndustrySpec *indsp = GetIndustrySpec(ind_fld->u.industry.ind_type);
01979 for (uint i = 0; i < lengthof(indsp->accepts_cargo); i++) {
01980 int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
01981 if (col < 0) others[other_count++] = indsp->accepts_cargo[i];
01982 }
01983
01984
01985 for (uint i = 0; i < MAX_CARGOES && other_count > 0; i++) {
01986 if (cargo_fld->u.cargo.cust_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_accepted[i] = others[--other_count];
01987 }
01988 } else {
01989
01990 for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
01991 for (uint h = 0; h < HOUSE_MAX; h++) {
01992 HouseSpec *hs = HouseSpec::Get(h);
01993 if (!hs->enabled) continue;
01994
01995 for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
01996 if (cargo_fld->u.cargo.vertical_cargoes[i] == hs->accepts_cargo[j]) {
01997 cargo_fld->ConnectCargo(cargo_fld->u.cargo.vertical_cargoes[i], false);
01998 goto next_cargo;
01999 }
02000 }
02001 }
02002 next_cargo: ;
02003 }
02004 }
02005 }
02006 };
02007
02008
02036 struct IndustryCargoesWindow : public Window {
02037 static const int HOR_TEXT_PADDING, VERT_TEXT_PADDING;
02038
02039 typedef SmallVector<CargoesRow, 4> Fields;
02040
02041 Fields fields;
02042 uint ind_cargo;
02043 Dimension cargo_textsize;
02044 Dimension ind_textsize;
02045 Scrollbar *vscroll;
02046
02047 IndustryCargoesWindow(int id) : Window()
02048 {
02049 this->OnInit();
02050 this->CreateNestedTree(&_industry_cargoes_desc);
02051 this->vscroll = this->GetScrollbar(WID_IC_SCROLLBAR);
02052 this->FinishInitNested(&_industry_cargoes_desc, 0);
02053 this->OnInvalidateData(id);
02054 }
02055
02056 virtual void OnInit()
02057 {
02058
02059 Dimension d = GetStringBoundingBox(STR_INDUSTRY_CARGOES_PRODUCERS);
02060 d = maxdim(d, GetStringBoundingBox(STR_INDUSTRY_CARGOES_CUSTOMERS));
02061 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
02062 d.height += WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM;
02063 CargoesField::small_height = d.height;
02064
02065
02066 this->ind_textsize.width = 0;
02067 this->ind_textsize.height = 0;
02068 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02069 const IndustrySpec *indsp = GetIndustrySpec(it);
02070 if (!indsp->enabled) continue;
02071 this->ind_textsize = maxdim(this->ind_textsize, GetStringBoundingBox(indsp->name));
02072 }
02073 d.width = max(d.width, this->ind_textsize.width);
02074 d.height = this->ind_textsize.height;
02075 this->ind_textsize = maxdim(this->ind_textsize, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_INDUSTRY));
02076
02077
02078 this->cargo_textsize.width = 0;
02079 this->cargo_textsize.height = 0;
02080 for (uint i = 0; i < NUM_CARGO; i++) {
02081 const CargoSpec *csp = CargoSpec::Get(i);
02082 if (!csp->IsValid()) continue;
02083 this->cargo_textsize = maxdim(this->cargo_textsize, GetStringBoundingBox(csp->name));
02084 }
02085 d = maxdim(d, this->cargo_textsize);
02086 this->cargo_textsize = maxdim(this->cargo_textsize, GetStringBoundingBox(STR_INDUSTRY_CARGOES_SELECT_CARGO));
02087
02088 d.width += 2 * HOR_TEXT_PADDING;
02089
02090 uint min_ind_height = CargoesField::VERT_CARGO_EDGE * 2 + MAX_CARGOES * FONT_HEIGHT_NORMAL + (MAX_CARGOES - 1) * CargoesField::VERT_CARGO_SPACE;
02091 d.height = max(d.height + 2 * VERT_TEXT_PADDING, min_ind_height);
02092
02093 CargoesField::industry_width = d.width;
02094 CargoesField::normal_height = d.height + CargoesField::VERT_INTER_INDUSTRY_SPACE;
02095 }
02096
02097 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02098 {
02099 switch (widget) {
02100 case WID_IC_PANEL:
02101 size->width = WD_FRAMETEXT_LEFT + CargoesField::industry_width * 3 + CargoesField::CARGO_FIELD_WIDTH * 2 + WD_FRAMETEXT_RIGHT;
02102 break;
02103
02104 case WID_IC_IND_DROPDOWN:
02105 size->width = max(size->width, this->ind_textsize.width + padding.width);
02106 break;
02107
02108 case WID_IC_CARGO_DROPDOWN:
02109 size->width = max(size->width, this->cargo_textsize.width + padding.width);
02110 break;
02111 }
02112 }
02113
02114
02115 CargoesFieldType type;
02116 virtual void SetStringParameters (int widget) const
02117 {
02118 if (widget != WID_IC_CAPTION) return;
02119
02120 if (this->ind_cargo < NUM_INDUSTRYTYPES) {
02121 const IndustrySpec *indsp = GetIndustrySpec(this->ind_cargo);
02122 SetDParam(0, indsp->name);
02123 } else {
02124 const CargoSpec *csp = CargoSpec::Get(this->ind_cargo - NUM_INDUSTRYTYPES);
02125 SetDParam(0, csp->name);
02126 }
02127 }
02128
02137 static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2)
02138 {
02139 while (length1 > 0) {
02140 if (*cargoes1 != INVALID_CARGO) {
02141 for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true;
02142 }
02143 cargoes1++;
02144 length1--;
02145 }
02146 return false;
02147 }
02148
02155 static bool HousesCanSupply(const CargoID *cargoes, uint length)
02156 {
02157 for (uint i = 0; i < length; i++) {
02158 if (cargoes[i] == INVALID_CARGO) continue;
02159 if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true;
02160 }
02161 return false;
02162 }
02163
02170 static bool HousesCanAccept(const CargoID *cargoes, uint length)
02171 {
02172 HouseZones climate_mask;
02173 switch (_settings_game.game_creation.landscape) {
02174 case LT_TEMPERATE: climate_mask = HZ_TEMP; break;
02175 case LT_ARCTIC: climate_mask = HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW; break;
02176 case LT_TROPIC: climate_mask = HZ_SUBTROPIC; break;
02177 case LT_TOYLAND: climate_mask = HZ_TOYLND; break;
02178 default: NOT_REACHED();
02179 }
02180 for (uint i = 0; i < length; i++) {
02181 if (cargoes[i] == INVALID_CARGO) continue;
02182
02183 for (uint h = 0; h < HOUSE_MAX; h++) {
02184 HouseSpec *hs = HouseSpec::Get(h);
02185 if (!hs->enabled || !(hs->building_availability & climate_mask)) continue;
02186
02187 for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
02188 if (cargoes[i] == hs->accepts_cargo[j]) return true;
02189 }
02190 }
02191 }
02192 return false;
02193 }
02194
02201 static int CountMatchingAcceptingIndustries(const CargoID *cargoes, uint length)
02202 {
02203 int count = 0;
02204 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02205 const IndustrySpec *indsp = GetIndustrySpec(it);
02206 if (!indsp->enabled) continue;
02207
02208 if (HasCommonValidCargo(cargoes, length, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) count++;
02209 }
02210 return count;
02211 }
02212
02219 static int CountMatchingProducingIndustries(const CargoID *cargoes, uint length)
02220 {
02221 int count = 0;
02222 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02223 const IndustrySpec *indsp = GetIndustrySpec(it);
02224 if (!indsp->enabled) continue;
02225
02226 if (HasCommonValidCargo(cargoes, length, indsp->produced_cargo, lengthof(indsp->produced_cargo))) count++;
02227 }
02228 return count;
02229 }
02230
02237 void ShortenCargoColumn(int column, int top, int bottom)
02238 {
02239 while (top < bottom && !this->fields[top].columns[column].HasConnection()) {
02240 this->fields[top].columns[column].MakeEmpty(CFT_EMPTY);
02241 top++;
02242 }
02243 this->fields[top].columns[column].u.cargo.top_end = true;
02244
02245 while (bottom > top && !this->fields[bottom].columns[column].HasConnection()) {
02246 this->fields[bottom].columns[column].MakeEmpty(CFT_EMPTY);
02247 bottom--;
02248 }
02249 this->fields[bottom].columns[column].u.cargo.bottom_end = true;
02250 }
02251
02258 void PlaceIndustry(int row, int col, IndustryType it)
02259 {
02260 assert(this->fields[row].columns[col].type == CFT_EMPTY);
02261 this->fields[row].columns[col].MakeIndustry(it);
02262 if (col == 0) {
02263 this->fields[row].ConnectIndustryProduced(col);
02264 } else {
02265 this->fields[row].ConnectIndustryAccepted(col);
02266 }
02267 }
02268
02272 void NotifySmallmap()
02273 {
02274 if (!this->IsWidgetLowered(WID_IC_NOTIFY)) return;
02275
02276
02277
02278 InvalidateWindowClassesData(WC_SMALLMAP, 0);
02279 }
02280
02285 void ComputeIndustryDisplay(IndustryType it)
02286 {
02287 this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_INDUSTRY_CAPTION;
02288 this->ind_cargo = it;
02289 _displayed_industries = 1ULL << it;
02290
02291 this->fields.Clear();
02292 CargoesRow *row = this->fields.Append();
02293 row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
02294 row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
02295 row->columns[2].MakeEmpty(CFT_SMALL_EMPTY);
02296 row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
02297 row->columns[4].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
02298
02299 const IndustrySpec *central_sp = GetIndustrySpec(it);
02300 bool houses_supply = HousesCanSupply(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
02301 bool houses_accept = HousesCanAccept(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
02302
02303 int num_supp = CountMatchingProducingIndustries(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo)) + houses_supply;
02304 int num_cust = CountMatchingAcceptingIndustries(central_sp->produced_cargo, lengthof(central_sp->produced_cargo)) + houses_accept;
02305 int num_indrows = max(3, max(num_supp, num_cust));
02306 for (int i = 0; i < num_indrows; i++) {
02307 CargoesRow *row = this->fields.Append();
02308 row->columns[0].MakeEmpty(CFT_EMPTY);
02309 row->columns[1].MakeCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo));
02310 row->columns[2].MakeEmpty(CFT_EMPTY);
02311 row->columns[3].MakeCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo));
02312 row->columns[4].MakeEmpty(CFT_EMPTY);
02313 }
02314
02315 int central_row = 1 + num_indrows / 2;
02316 this->fields[central_row].columns[2].MakeIndustry(it);
02317 this->fields[central_row].ConnectIndustryProduced(2);
02318 this->fields[central_row].ConnectIndustryAccepted(2);
02319
02320
02321 this->fields[central_row - 1].MakeCargoLabel(2, true);
02322 this->fields[central_row + 1].MakeCargoLabel(2, false);
02323
02324
02325 int supp_count = 0;
02326 int cust_count = 0;
02327 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02328 const IndustrySpec *indsp = GetIndustrySpec(it);
02329 if (!indsp->enabled) continue;
02330
02331 if (HasCommonValidCargo(central_sp->accepts_cargo, lengthof(central_sp->accepts_cargo), indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
02332 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
02333 SetBit(_displayed_industries, it);
02334 supp_count++;
02335 }
02336 if (HasCommonValidCargo(central_sp->produced_cargo, lengthof(central_sp->produced_cargo), indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
02337 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, it);
02338 SetBit(_displayed_industries, it);
02339 cust_count++;
02340 }
02341 }
02342 if (houses_supply) {
02343 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
02344 supp_count++;
02345 }
02346 if (houses_accept) {
02347 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 4, NUM_INDUSTRYTYPES);
02348 cust_count++;
02349 }
02350
02351 this->ShortenCargoColumn(1, 1, num_indrows);
02352 this->ShortenCargoColumn(3, 1, num_indrows);
02353 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02354 this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
02355 this->SetDirty();
02356 this->NotifySmallmap();
02357 }
02358
02363 void ComputeCargoDisplay(CargoID cid)
02364 {
02365 this->GetWidget<NWidgetCore>(WID_IC_CAPTION)->widget_data = STR_INDUSTRY_CARGOES_CARGO_CAPTION;
02366 this->ind_cargo = cid + NUM_INDUSTRYTYPES;
02367 _displayed_industries = 0;
02368
02369 this->fields.Clear();
02370 CargoesRow *row = this->fields.Append();
02371 row->columns[0].MakeHeader(STR_INDUSTRY_CARGOES_PRODUCERS);
02372 row->columns[1].MakeEmpty(CFT_SMALL_EMPTY);
02373 row->columns[2].MakeHeader(STR_INDUSTRY_CARGOES_CUSTOMERS);
02374 row->columns[3].MakeEmpty(CFT_SMALL_EMPTY);
02375 row->columns[4].MakeEmpty(CFT_SMALL_EMPTY);
02376
02377 bool houses_supply = HousesCanSupply(&cid, 1);
02378 bool houses_accept = HousesCanAccept(&cid, 1);
02379 int num_supp = CountMatchingProducingIndustries(&cid, 1) + houses_supply + 1;
02380 int num_cust = CountMatchingAcceptingIndustries(&cid, 1) + houses_accept;
02381 int num_indrows = max(num_supp, num_cust);
02382 for (int i = 0; i < num_indrows; i++) {
02383 CargoesRow *row = this->fields.Append();
02384 row->columns[0].MakeEmpty(CFT_EMPTY);
02385 row->columns[1].MakeCargo(&cid, 1);
02386 row->columns[2].MakeEmpty(CFT_EMPTY);
02387 row->columns[3].MakeEmpty(CFT_EMPTY);
02388 row->columns[4].MakeEmpty(CFT_EMPTY);
02389 }
02390
02391 this->fields[num_indrows].MakeCargoLabel(0, false);
02392
02393
02394 int supp_count = 0;
02395 int cust_count = 0;
02396 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02397 const IndustrySpec *indsp = GetIndustrySpec(it);
02398 if (!indsp->enabled) continue;
02399
02400 if (HasCommonValidCargo(&cid, 1, indsp->produced_cargo, lengthof(indsp->produced_cargo))) {
02401 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, it);
02402 SetBit(_displayed_industries, it);
02403 supp_count++;
02404 }
02405 if (HasCommonValidCargo(&cid, 1, indsp->accepts_cargo, lengthof(indsp->accepts_cargo))) {
02406 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, it);
02407 SetBit(_displayed_industries, it);
02408 cust_count++;
02409 }
02410 }
02411 if (houses_supply) {
02412 this->PlaceIndustry(1 + supp_count * num_indrows / num_supp, 0, NUM_INDUSTRYTYPES);
02413 supp_count++;
02414 }
02415 if (houses_accept) {
02416 this->PlaceIndustry(1 + cust_count * num_indrows / num_cust, 2, NUM_INDUSTRYTYPES);
02417 cust_count++;
02418 }
02419
02420 this->ShortenCargoColumn(1, 1, num_indrows);
02421 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02422 this->vscroll->SetCount(CeilDiv(WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM + CargoesField::small_height + num_indrows * CargoesField::normal_height, nwp->resize_y));
02423 this->SetDirty();
02424 this->NotifySmallmap();
02425 }
02426
02434 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
02435 {
02436 if (!gui_scope) return;
02437 if (data == NUM_INDUSTRYTYPES) {
02438 if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
02439 this->RaiseWidget(WID_IC_NOTIFY);
02440 this->SetWidgetDirty(WID_IC_NOTIFY);
02441 }
02442 return;
02443 }
02444
02445 assert(data >= 0 && data < NUM_INDUSTRYTYPES);
02446 this->ComputeIndustryDisplay(data);
02447 }
02448
02449 virtual void DrawWidget(const Rect &r, int widget) const
02450 {
02451 if (widget != WID_IC_PANEL) return;
02452
02453 DrawPixelInfo tmp_dpi, *old_dpi;
02454 int width = r.right - r.left + 1;
02455 int height = r.bottom - r.top + 1 - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM;
02456 if (!FillDrawPixelInfo(&tmp_dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, width, height)) return;
02457 old_dpi = _cur_dpi;
02458 _cur_dpi = &tmp_dpi;
02459
02460 int left_pos = WD_FRAMERECT_LEFT;
02461 if (this->ind_cargo >= NUM_INDUSTRYTYPES) left_pos += (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2;
02462 int last_column = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
02463
02464 const NWidgetBase *nwp = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02465 int vpos = -this->vscroll->GetPosition() * nwp->resize_y;
02466 for (uint i = 0; i < this->fields.Length(); i++) {
02467 int row_height = (i == 0) ? CargoesField::small_height : CargoesField::normal_height;
02468 if (vpos + row_height >= 0) {
02469 int xpos = left_pos;
02470 int col, dir;
02471 if (_current_text_dir == TD_RTL) {
02472 col = last_column;
02473 dir = -1;
02474 } else {
02475 col = 0;
02476 dir = 1;
02477 }
02478 while (col >= 0 && col <= last_column) {
02479 this->fields[i].columns[col].Draw(xpos, vpos);
02480 xpos += (col & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
02481 col += dir;
02482 }
02483 }
02484 vpos += row_height;
02485 if (vpos >= height) break;
02486 }
02487
02488 _cur_dpi = old_dpi;
02489 }
02490
02498 bool CalculatePositionInWidget(Point pt, Point *fieldxy, Point *xy)
02499 {
02500 const NWidgetBase *nw = this->GetWidget<NWidgetBase>(WID_IC_PANEL);
02501 pt.x -= nw->pos_x;
02502 pt.y -= nw->pos_y;
02503
02504 int vpos = WD_FRAMERECT_TOP + CargoesField::small_height - this->vscroll->GetPosition() * nw->resize_y;
02505 if (pt.y < vpos) return false;
02506
02507 int row = (pt.y - vpos) / CargoesField::normal_height;
02508 if (row + 1 >= (int)this->fields.Length()) return false;
02509 vpos = pt.y - vpos - row * CargoesField::normal_height;
02510 row++;
02511
02512 int xpos = 2 * WD_FRAMERECT_LEFT + ((this->ind_cargo < NUM_INDUSTRYTYPES) ? 0 : (CargoesField::industry_width + CargoesField::CARGO_FIELD_WIDTH) / 2);
02513 if (pt.x < xpos) return false;
02514 int column;
02515 for (column = 0; column <= 5; column++) {
02516 int width = (column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width;
02517 if (pt.x < xpos + width) break;
02518 xpos += width;
02519 }
02520 int num_columns = (this->ind_cargo < NUM_INDUSTRYTYPES) ? 4 : 2;
02521 if (column > num_columns) return false;
02522 xpos = pt.x - xpos;
02523
02524
02525 fieldxy->y = row;
02526 xy->y = vpos;
02527 if (_current_text_dir == TD_RTL) {
02528 fieldxy->x = num_columns - column;
02529 xy->x = ((column & 1) ? CargoesField::CARGO_FIELD_WIDTH : CargoesField::industry_width) - xpos;
02530 } else {
02531 fieldxy->x = column;
02532 xy->x = xpos;
02533 }
02534 return true;
02535 }
02536
02537 virtual void OnClick(Point pt, int widget, int click_count)
02538 {
02539 switch (widget) {
02540 case WID_IC_PANEL: {
02541 Point fieldxy, xy;
02542 if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
02543
02544 const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
02545 switch (fld->type) {
02546 case CFT_INDUSTRY:
02547 if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES) this->ComputeIndustryDisplay(fld->u.industry.ind_type);
02548 break;
02549
02550 case CFT_CARGO: {
02551 CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
02552 CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
02553 CargoID cid = fld->CargoClickedAt(lft, rgt, xy);
02554 if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
02555 break;
02556 }
02557
02558 case CFT_CARGO_LABEL: {
02559 CargoID cid = fld->CargoLabelClickedAt(xy);
02560 if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid);
02561 break;
02562 }
02563
02564 default:
02565 break;
02566 }
02567 break;
02568 }
02569
02570 case WID_IC_NOTIFY:
02571 this->ToggleWidgetLoweredState(WID_IC_NOTIFY);
02572 this->SetWidgetDirty(WID_IC_NOTIFY);
02573 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
02574
02575 if (this->IsWidgetLowered(WID_IC_NOTIFY)) {
02576 if (FindWindowByClass(WC_SMALLMAP) == NULL) ShowSmallMap();
02577 this->NotifySmallmap();
02578 }
02579 break;
02580
02581 case WID_IC_CARGO_DROPDOWN: {
02582 DropDownList *lst = new DropDownList;
02583 const CargoSpec *cs;
02584 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
02585 lst->push_back(new DropDownListStringItem(cs->name, cs->Index(), false));
02586 }
02587 if (lst->size() == 0) {
02588 delete lst;
02589 break;
02590 }
02591 int selected = (this->ind_cargo >= NUM_INDUSTRYTYPES) ? (int)(this->ind_cargo - NUM_INDUSTRYTYPES) : -1;
02592 ShowDropDownList(this, lst, selected, WID_IC_CARGO_DROPDOWN, 0, true);
02593 break;
02594 }
02595
02596 case WID_IC_IND_DROPDOWN: {
02597 DropDownList *lst = new DropDownList;
02598 for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
02599 IndustryType ind = _sorted_industry_types[i];
02600 const IndustrySpec *indsp = GetIndustrySpec(ind);
02601 if (!indsp->enabled) continue;
02602 lst->push_back(new DropDownListStringItem(indsp->name, ind, false));
02603 }
02604 if (lst->size() == 0) {
02605 delete lst;
02606 break;
02607 }
02608 int selected = (this->ind_cargo < NUM_INDUSTRYTYPES) ? (int)this->ind_cargo : -1;
02609 ShowDropDownList(this, lst, selected, WID_IC_IND_DROPDOWN, 0, true);
02610 break;
02611 }
02612 }
02613 }
02614
02615 virtual void OnDropdownSelect(int widget, int index)
02616 {
02617 if (index < 0) return;
02618
02619 switch (widget) {
02620 case WID_IC_CARGO_DROPDOWN:
02621 this->ComputeCargoDisplay(index);
02622 break;
02623
02624 case WID_IC_IND_DROPDOWN:
02625 this->ComputeIndustryDisplay(index);
02626 break;
02627 }
02628 }
02629
02630 virtual void OnHover(Point pt, int widget)
02631 {
02632 if (widget != WID_IC_PANEL) return;
02633
02634 Point fieldxy, xy;
02635 if (!CalculatePositionInWidget(pt, &fieldxy, &xy)) return;
02636
02637 const CargoesField *fld = this->fields[fieldxy.y].columns + fieldxy.x;
02638 CargoID cid = INVALID_CARGO;
02639 switch (fld->type) {
02640 case CFT_CARGO: {
02641 CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : NULL;
02642 CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : NULL;
02643 cid = fld->CargoClickedAt(lft, rgt, xy);
02644 break;
02645 }
02646
02647 case CFT_CARGO_LABEL: {
02648 cid = fld->CargoLabelClickedAt(xy);
02649 break;
02650 }
02651
02652 case CFT_INDUSTRY:
02653 if (fld->u.industry.ind_type < NUM_INDUSTRYTYPES && (this->ind_cargo >= NUM_INDUSTRYTYPES || fieldxy.x != 2)) {
02654 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_INDUSTRY_TOOLTIP, 0, NULL, TCC_HOVER);
02655 }
02656 return;
02657
02658 default:
02659 break;
02660 }
02661 if (cid != INVALID_CARGO && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
02662 const CargoSpec *csp = CargoSpec::Get(cid);
02663 uint64 params[5];
02664 params[0] = csp->name;
02665 GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, 1, params, TCC_HOVER);
02666 }
02667 }
02668
02669 virtual void OnResize()
02670 {
02671 this->vscroll->SetCapacityFromWidget(this, WID_IC_PANEL);
02672 }
02673 };
02674
02675 const int IndustryCargoesWindow::HOR_TEXT_PADDING = 5;
02676 const int IndustryCargoesWindow::VERT_TEXT_PADDING = 5;
02677
02682 static void ShowIndustryCargoesWindow(IndustryType id)
02683 {
02684 if (id >= NUM_INDUSTRYTYPES) {
02685 for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
02686 const IndustrySpec *indsp = GetIndustrySpec(_sorted_industry_types[i]);
02687 if (indsp->enabled) {
02688 id = _sorted_industry_types[i];
02689 break;
02690 }
02691 }
02692 if (id >= NUM_INDUSTRYTYPES) return;
02693 }
02694
02695 Window *w = BringWindowToFrontById(WC_INDUSTRY_CARGOES, 0);
02696 if (w != NULL) {
02697 w->InvalidateData(id);
02698 return;
02699 }
02700 new IndustryCargoesWindow(id);
02701 }
02702
02704 void ShowIndustryCargoesWindow()
02705 {
02706 ShowIndustryCargoesWindow(NUM_INDUSTRYTYPES);
02707 }