Skip to content

Commit

Permalink
Codechange: Cache ScriptConfig for the slot in constructor
Browse files Browse the repository at this point in the history
Also simplify code in some parts and fix some typos
  • Loading branch information
SamuXarick committed Mar 2, 2023
1 parent 2a2e517 commit a02f9a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/ai/ai_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static const NWidgetPart _nested_ai_config_widgets[] = {

/** Window definition for the configure AI window. */
static WindowDesc _ai_config_desc(
WDP_CENTER, "settings_script_config", 0, 0,
WDP_CENTER, "settings_ai_config", 0, 0,
WC_GAME_OPTIONS, WC_NONE,
0,
_nested_ai_config_widgets, lengthof(_nested_ai_config_widgets)
Expand Down Expand Up @@ -270,4 +270,3 @@ void ShowAIConfigWindow()
CloseWindowByClass(WC_GAME_OPTIONS);
new AIConfigWindow();
}

22 changes: 9 additions & 13 deletions src/game/game_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ struct GSConfigWindow : public Window {
case WID_GSC_GSLIST: {
StringID text = STR_AI_CONFIG_NONE;

if (GameConfig::GetConfig()->GetInfo() != nullptr) {
SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName());
if (this->gs_config->GetInfo() != nullptr) {
SetDParamStr(0, this->gs_config->GetInfo()->GetName());
text = STR_JUST_RAW_STRING;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ struct GSConfigWindow : public Window {
void OnClick(Point pt, int widget, int click_count) override
{
if (widget >= WID_GSC_TEXTFILE && widget < WID_GSC_TEXTFILE + TFT_END) {
if (GameConfig::GetConfig() == nullptr) return;
if (this->gs_config == nullptr) return;

ShowScriptTextfileWindow((TextfileType)(widget - WID_GSC_TEXTFILE), (CompanyID)OWNER_DEITY);
return;
Expand Down Expand Up @@ -321,16 +321,12 @@ struct GSConfigWindow : public Window {
int new_val = old_val;
if (bool_item) {
new_val = !new_val;
} else if (x >= SETTING_BUTTON_WIDTH / 2) {
/* Increase button clicked */
new_val += config_item.step_size;
if (new_val > config_item.max_value) new_val = config_item.max_value;
this->clicked_increase = true;
} else {
/* Decrease button clicked */
new_val -= config_item.step_size;
if (new_val < config_item.min_value) new_val = config_item.min_value;
this->clicked_increase = false;
/* Increase/Decrease button clicked */
this->clicked_increase = (x >= SETTING_BUTTON_WIDTH / 2);
new_val = this->clicked_increase ?
std::min(config_item.max_value, new_val + config_item.step_size) :
std::max(config_item.min_value, new_val - config_item.step_size);
}

if (new_val != old_val) {
Expand Down Expand Up @@ -407,7 +403,7 @@ struct GSConfigWindow : public Window {
this->SetWidgetDisabledState(WID_GSC_CHANGE, (_game_mode == GM_NORMAL) || !IsEditable());

for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY) == nullptr);
this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, this->gs_config->GetTextfile(tft, (CompanyID)OWNER_DEITY) == nullptr);
}
this->RebuildVisibleSettings();
HideDropDownMenu(this);
Expand Down
51 changes: 27 additions & 24 deletions src/script/script_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@
#include "../safeguards.h"


static ScriptConfig *GetConfig(CompanyID slot)
{
if (slot == OWNER_DEITY) return GameConfig::GetConfig();
return AIConfig::GetConfig(slot);
}

/**
* Window that let you choose an available Script.
*/
struct ScriptListWindow : public Window {
const ScriptInfoList *info_list; ///< The list of Scripts.
ScriptConfig *script_config; ///< The configuration we're modifying.
int selected; ///< The currently selected Script.
CompanyID slot; ///< The company we're selecting a new Script for.
int line_height; ///< Height of a row in the matrix widget.
Expand All @@ -65,8 +60,10 @@ struct ScriptListWindow : public Window {
{
if (slot == OWNER_DEITY) {
this->info_list = Game::GetUniqueInfoList();
this->script_config = GameConfig::GetConfig();
} else {
this->info_list = AI::GetUniqueInfoList();
this->script_config = AIConfig::GetConfig(slot);
}

this->CreateNestedTree();
Expand All @@ -77,8 +74,8 @@ struct ScriptListWindow : public Window {

/* Try if we can find the currently selected AI */
this->selected = -1;
if (GetConfig(slot)->HasScript()) {
ScriptInfo *info = GetConfig(slot)->GetInfo();
if (this->script_config->HasScript()) {
ScriptInfo *info = this->script_config->GetInfo();
int i = 0;
for (const auto &item : *this->info_list) {
if (item.second == info) {
Expand Down Expand Up @@ -165,11 +162,11 @@ struct ScriptListWindow : public Window {
void ChangeScript()
{
if (this->selected == -1) {
GetConfig(slot)->Change(nullptr);
this->script_config->Change(nullptr);
} else {
ScriptInfoList::const_iterator it = this->info_list->begin();
for (int i = 0; i < this->selected; i++) it++;
GetConfig(slot)->Change((*it).second->GetName(), (*it).second->GetVersion());
this->script_config->Change((*it).second->GetName(), (*it).second->GetVersion());
}
InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI);
InvalidateWindowClassesData(WC_SCRIPT_SETTINGS);
Expand Down Expand Up @@ -301,7 +298,11 @@ struct ScriptSettingsWindow : public Window {
closing_dropdown(false),
timeout(0)
{
this->script_config = GetConfig(slot);
if (slot == OWNER_DEITY) {
this->script_config = GameConfig::GetConfig();
} else {
this->script_config = AIConfig::GetConfig(slot);
}

this->CreateNestedTree();
this->vscroll = this->GetScrollbar(WID_SCRS_SCROLLBAR);
Expand Down Expand Up @@ -478,16 +479,12 @@ struct ScriptSettingsWindow : public Window {
int new_val = old_val;
if (bool_item) {
new_val = !new_val;
} else if (x >= SETTING_BUTTON_WIDTH / 2) {
/* Increase button clicked */
new_val += config_item.step_size;
if (new_val > config_item.max_value) new_val = config_item.max_value;
this->clicked_increase = true;
} else {
/* Decrease button clicked */
new_val -= config_item.step_size;
if (new_val < config_item.min_value) new_val = config_item.min_value;
this->clicked_increase = false;
/* Increase/Decrease button clicked */
this->clicked_increase = (x >= SETTING_BUTTON_WIDTH / 2);
new_val = this->clicked_increase ?
std::min(config_item.max_value, new_val + config_item.step_size) :
std::max(config_item.min_value, new_val - config_item.step_size);
}

if (new_val != old_val) {
Expand Down Expand Up @@ -628,24 +625,30 @@ void ShowScriptSettingsWindow(CompanyID slot)

/** Window for displaying the textfile of a AI. */
struct ScriptTextfileWindow : public TextfileWindow {
CompanyID slot; ///< View the textfile of this CompanyID slot.
CompanyID slot; ///< View the textfile of this CompanyID slot.
ScriptConfig *script_config; ///< The configuration we selected.

ScriptTextfileWindow(TextfileType file_type, CompanyID slot) : TextfileWindow(file_type), slot(slot)
{
if (slot == OWNER_DEITY) {
this->script_config = GameConfig::GetConfig();
} else {
this->script_config = AIConfig::GetConfig(slot);
}
this->OnInvalidateData();
}

void SetStringParameters(int widget) const override
{
if (widget == WID_TF_CAPTION) {
SetDParam(0, (slot == OWNER_DEITY) ? STR_CONTENT_TYPE_GAME_SCRIPT : STR_CONTENT_TYPE_AI);
SetDParamStr(1, GetConfig(slot)->GetInfo()->GetName());
SetDParamStr(1, script_config->GetInfo()->GetName());
}
}

void OnInvalidateData(int data = 0, bool gui_scope = true) override
{
const char *textfile = GetConfig(slot)->GetTextfile(file_type, slot);
const char *textfile = script_config->GetTextfile(file_type, slot);
if (textfile == nullptr) {
this->Close();
} else {
Expand Down Expand Up @@ -946,7 +949,7 @@ struct ScriptDebugWindow : public Window {

this->highlight_row = -1; // The highlight of one Script make little sense for another Script.

/* Close AI settings window to prevent confusion */
/* Close script settings windows to prevent confusion */
CloseWindowByClass(WC_SCRIPT_SETTINGS);

this->InvalidateData(-1);
Expand Down
2 changes: 1 addition & 1 deletion src/table/settings/gui_settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ cat = SC_EXPERT
var = gui.ai_developer_tools
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
def = false
post_cb = [](auto) { InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); }
post_cb = [](auto) { InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); InvalidateWindowData(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GS); InvalidateWindowClassesData(WC_SCRIPT_DEBUG); }
cat = SC_EXPERT

[SDTC_BOOL]
Expand Down

0 comments on commit a02f9a4

Please sign in to comment.