Picker tool: Add support for industries

This commit is contained in:
Jonathan G Rennison
2024-02-01 20:03:56 +00:00
parent 9f55550417
commit 3886ea75a6
3 changed files with 59 additions and 19 deletions

View File

@@ -12,6 +12,7 @@
#include "vehicle_type.h" #include "vehicle_type.h"
#include "economy_type.h" #include "economy_type.h"
#include "industry_type.h"
#include "tile_type.h" #include "tile_type.h"
#include "transport_type.h" #include "transport_type.h"
#include "story_type.h" #include "story_type.h"
@@ -56,6 +57,7 @@ void ShowFoundTownWindow();
void ShowIndustryDirectory(); void ShowIndustryDirectory();
void ShowIndustryCargoesWindow(); void ShowIndustryCargoesWindow();
void ShowBuildIndustryWindow(); void ShowBuildIndustryWindow();
void ShowBuildIndustryWindowForIndustryType(IndustryType industry_type);
/* subsidy_gui.cpp */ /* subsidy_gui.cpp */
void ShowSubsidiesList(); void ShowSubsidiesList();

View File

@@ -623,7 +623,7 @@ public:
MarkWholeScreenDirty(); MarkWholeScreenDirty();
} }
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override void OnClick(Point pt, WidgetID widget, int click_count) override
{ {
switch (widget) { switch (widget) {
case WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET: { case WID_DPI_CREATE_RANDOM_INDUSTRIES_WIDGET: {
@@ -643,21 +643,7 @@ public:
case WID_DPI_MATRIX_WIDGET: { case WID_DPI_MATRIX_WIDGET: {
auto it = this->vscroll->GetScrolledItemFromWidget(this->list, pt.y, this, WID_DPI_MATRIX_WIDGET); auto it = this->vscroll->GetScrolledItemFromWidget(this->list, pt.y, this, WID_DPI_MATRIX_WIDGET);
if (it != this->list.end()) { // Is it within the boundaries of available data? if (it != this->list.end()) { // Is it within the boundaries of available data?
this->selected_type = *it; this->SelectIndustryType(*it);
this->UpdateAvailability();
const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
this->SetDirty();
if (_thd.GetCallbackWnd() == this &&
((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != nullptr && indsp->IsRawIndustry()) || !this->enabled)) {
/* Reset the button state if going to prospecting or "build many industries" */
this->RaiseButtons();
ResetObjectToPlace();
}
this->SetButtons();
if (this->enabled && click_count > 1) this->OnClick(pt, WID_DPI_FUND_WIDGET, 1); if (this->enabled && click_count > 1) this->OnClick(pt, WID_DPI_FUND_WIDGET, 1);
} }
break; break;
@@ -681,6 +667,33 @@ public:
} }
} }
void ScrollToSelected()
{
auto iter = std::find(this->list.begin(), this->list.end(), this->selected_type);
if (iter != this->list.end()) {
this->vscroll->ScrollTowards(iter - this->list.begin());
}
}
void SelectIndustryType(IndustryType type)
{
this->selected_type = type;
this->UpdateAvailability();
const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
this->SetDirty();
if (_thd.GetCallbackWnd() == this &&
((_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indsp != nullptr && indsp->IsRawIndustry()) || !this->enabled)) {
/* Reset the button state if going to prospecting or "build many industries" */
this->RaiseButtons();
ResetObjectToPlace();
}
this->SetButtons();
}
void OnResize() override void OnResize() override
{ {
/* Adjust the number of items in the matrix depending of the resize */ /* Adjust the number of items in the matrix depending of the resize */
@@ -758,11 +771,30 @@ public:
} }
}; };
static BuildIndustryWindow *CreateBuildIndustryWindow()
{
if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return nullptr;
Window *existing = BringWindowToFrontById(WC_BUILD_INDUSTRY, 0);
if (existing != nullptr) return static_cast<BuildIndustryWindow *>(existing);
return new BuildIndustryWindow();
}
void ShowBuildIndustryWindow() void ShowBuildIndustryWindow()
{ {
if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return; CreateBuildIndustryWindow();
if (BringWindowToFrontById(WC_BUILD_INDUSTRY, 0)) return; }
new BuildIndustryWindow();
void ShowBuildIndustryWindowForIndustryType(IndustryType industry_type)
{
const IndustrySpec *indsp = GetIndustrySpec(industry_type);
if (!indsp->enabled) return;
if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) return;
BuildIndustryWindow *w = CreateBuildIndustryWindow();
if (w == nullptr) return;
w->SelectIndustryType(industry_type);
w->ScrollToSelected();
} }
static void UpdateIndustryProduction(Industry *i); static void UpdateIndustryProduction(Industry *i);

View File

@@ -61,6 +61,7 @@
#include "newgrf_station.h" #include "newgrf_station.h"
#include "zoom_func.h" #include "zoom_func.h"
#include "help_gui.h" #include "help_gui.h"
#include "industry_map.h"
#include "widgets/toolbar_widget.h" #include "widgets/toolbar_widget.h"
@@ -1136,6 +1137,11 @@ static void UsePickerTool(TileIndex tile)
break; break;
} }
case MP_INDUSTRY: {
ShowBuildIndustryWindowForIndustryType(GetIndustryType(tile));
break;
}
default: default:
break; break;
} }