diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 5360c9193e..7a6f803c8d 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -159,6 +159,32 @@ struct AIConfigWindow : public Window { return slot < MAX_COMPANIES && !Company::IsValidID(slot); } + /** + * Get text to display for a given company slot. + * @param cid Company to display. + * @returns Text to display for company. + */ + std::string GetSlotText(CompanyID cid) const + { + if ((_game_mode != GM_NORMAL && cid == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(cid))) return GetString(STR_AI_CONFIG_HUMAN_PLAYER); + if (const AIInfo *info = AIConfig::GetConfig(cid)->GetInfo(); info != nullptr) return info->GetName(); + return GetString(STR_AI_CONFIG_RANDOM_AI); + } + + /** + * Get colour to display text in for a given company slot. + * @param cid Company to display. + * @param max_slot Maximum company ID that can be an AI. + * @returns Colour to display text for company. + */ + TextColour GetSlotColour(CompanyID cid, CompanyID max_slot) const + { + if (this->selected_slot == cid) return TC_WHITE; + if (IsEditable(cid)) return cid < max_slot ? TC_ORANGE : TC_SILVER; + if (Company::IsValidAiID(cid)) return TC_GREEN; + return TC_SILVER; + } + void DrawWidget(const Rect &r, WidgetID widget) const override { switch (widget) { @@ -176,26 +202,8 @@ struct AIConfigWindow : public Window { max_slot++; // Slot 0 is human } for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) { - StringID text; - - if ((_game_mode != GM_NORMAL && i == 0) || (_game_mode == GM_NORMAL && Company::IsValidHumanID(i))) { - text = STR_AI_CONFIG_HUMAN_PLAYER; - } else if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != nullptr) { - SetDParamStr(0, AIConfig::GetConfig((CompanyID)i)->GetInfo()->GetName()); - text = STR_JUST_RAW_STRING; - } else { - text = STR_AI_CONFIG_RANDOM_AI; - } - - TextColour tc = TC_SILVER; - if (this->selected_slot == i) { - tc = TC_WHITE; - } else if (IsEditable((CompanyID)i)) { - if (i < max_slot) tc = TC_ORANGE; - } else if (Company::IsValidAiID(i)) { - tc = TC_GREEN; - } - DrawString(tr, text, tc); + CompanyID cid = static_cast(i); + DrawString(tr, this->GetSlotText(cid), this->GetSlotColour(cid, static_cast(max_slot))); tr.top += this->line_height; } break; diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index 3af917abda..bc9a298dc3 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -159,19 +159,22 @@ struct GSConfigWindow : public Window { return _game_mode != GM_NORMAL || Game::GetInstance() != nullptr; } + /** + * Get text to display for game script name. + * @returns Text to display for game script name. + */ + std::string GetText() const + { + if (const GameInfo *info = GameConfig::GetConfig()->GetInfo(); info != nullptr) return info->GetName(); + return GetString(STR_AI_CONFIG_NONE); + } + void DrawWidget(const Rect &r, WidgetID widget) const override { switch (widget) { case WID_GSC_GSLIST: { - StringID text = STR_AI_CONFIG_NONE; - - if (GameConfig::GetConfig()->GetInfo() != nullptr) { - SetDParamStr(0, GameConfig::GetConfig()->GetInfo()->GetName()); - text = STR_JUST_RAW_STRING; - } - /* There is only one slot, unlike with the GS GUI, so it should never be white */ - DrawString(r.Shrink(WidgetDimensions::scaled.matrix), text, (IsEditable() ? TC_ORANGE : TC_SILVER)); + DrawString(r.Shrink(WidgetDimensions::scaled.matrix), this->GetText(), (IsEditable() ? TC_ORANGE : TC_SILVER)); break; } case WID_GSC_SETTINGS: { @@ -190,21 +193,8 @@ struct GSConfigWindow : public Window { int current_value = this->gs_config->GetSetting(config_item.name); bool editable = this->IsEditableItem(config_item); - StringID str; - TextColour colour; - uint idx = 0; - if (config_item.description.empty()) { - str = STR_JUST_STRING1; - colour = TC_ORANGE; - } else { - str = STR_AI_SETTINGS_SETTING; - colour = TC_LIGHT_BLUE; - SetDParamStr(idx++, config_item.description); - } - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); - SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); if (config_item.complete_labels) { @@ -212,18 +202,9 @@ struct GSConfigWindow : public Window { } else { DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value); } - - auto config_iterator = config_item.labels.find(current_value); - if (config_iterator != config_item.labels.end()) { - SetDParam(idx++, STR_JUST_RAW_STRING); - SetDParamStr(idx++, config_iterator->second); - } else { - SetDParam(idx++, STR_JUST_INT); - SetDParam(idx++, current_value); - } } - DrawString(tr.left, tr.right, y + text_y_offset, str, colour); + DrawString(tr.left, tr.right, y + text_y_offset, config_item.GetString(current_value), config_item.GetColour()); y += this->line_height; } break; diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 463b26703e..2a4b7815cf 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -191,3 +191,35 @@ ScriptInstance::ScriptData *ScriptConfig::GetToLoadData() return this->to_load_data.get(); } +static std::pair GetValueParams(const ScriptConfigItem &config_item, int value) +{ + if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}}; + + auto it = config_item.labels.find(value); + if (it != std::end(config_item.labels)) return {STR_JUST_RAW_STRING, it->second}; + + return {STR_JUST_INT, value}; +} + +/** + * Get string to display this setting in the configuration interface. + * @param value Current value. + * @returns String to display. + */ +std::string ScriptConfigItem::GetString(int value) const +{ + auto [param1, param2] = GetValueParams(*this, value); + return this->description.empty() + ? ::GetString(STR_JUST_STRING1, param1, param2) + : ::GetString(STR_AI_SETTINGS_SETTING, this->description, param1, param2); +} + +/** + * Get text colour to display this setting in the configuration interface. + * @returns Text colour to display this setting. + */ +TextColour ScriptConfigItem::GetColour() const +{ + return this->description.empty() ? TC_ORANGE : TC_LIGHT_BLUE; +} + diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 08d4f14c0a..8ac73a8d90 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -39,6 +39,9 @@ struct ScriptConfigItem { ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting. LabelMapping labels; ///< Text labels for the integer values. bool complete_labels = false; ///< True if all values have a label. + + std::string GetString(int value) const; + TextColour GetColour() const; }; typedef std::vector ScriptConfigItemList; ///< List of ScriptConfig items. diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 30c97695e5..08e7909720 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -22,6 +22,7 @@ #include "../hotkeys.h" #include "../company_cmd.h" #include "../misc_cmd.h" +#include "../strings_func.h" #include "../timer/timer.h" #include "../timer/timer_window.h" @@ -124,14 +125,11 @@ struct ScriptListWindow : public Window { DrawString(tr, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE); tr.top += this->line_height; } - StringID str = this->show_all ? STR_AI_CONFIG_NAME_VERSION : STR_JUST_RAW_STRING; int i = 0; for (const auto &item : *this->info_list) { i++; if (this->vscroll->IsVisible(i)) { - SetDParamStr(0, item.second->GetName()); - SetDParam(1, item.second->GetVersion()); - DrawString(tr, str, (this->selected == i - 1) ? TC_WHITE : TC_ORANGE); + DrawString(tr, this->show_all ? GetString(STR_AI_CONFIG_NAME_VERSION, item.second->GetName(), item.second->GetVersion()) : item.second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE); tr.top += this->line_height; } } @@ -147,19 +145,15 @@ struct ScriptListWindow : public Window { /* Some info about the currently selected Script. */ if (selected_info != nullptr) { Rect tr = r.Shrink(WidgetDimensions::scaled.frametext, WidgetDimensions::scaled.framerect); - SetDParamStr(0, selected_info->GetAuthor()); - DrawString(tr, STR_AI_LIST_AUTHOR); + DrawString(tr, GetString(STR_AI_LIST_AUTHOR, selected_info->GetAuthor())); tr.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; - SetDParam(0, selected_info->GetVersion()); - DrawString(tr, STR_AI_LIST_VERSION); + DrawString(tr, GetString(STR_AI_LIST_VERSION, selected_info->GetVersion())); tr.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; if (!selected_info->GetURL().empty()) { - SetDParamStr(0, selected_info->GetURL()); - DrawString(tr, STR_AI_LIST_URL); + DrawString(tr, GetString(STR_AI_LIST_URL, selected_info->GetURL())); tr.top += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; } - SetDParamStr(0, selected_info->GetDescription()); - DrawStringMultiLine(tr, STR_JUST_RAW_STRING, TC_WHITE); + DrawStringMultiLine(tr, selected_info->GetDescription(), TC_WHITE); } break; } @@ -366,21 +360,8 @@ struct ScriptSettingsWindow : public Window { int current_value = this->script_config->GetSetting(config_item.name); bool editable = this->IsEditableItem(config_item); - StringID str; - TextColour colour; - uint idx = 0; - if (config_item.description.empty()) { - str = STR_JUST_STRING1; - colour = TC_ORANGE; - } else { - str = STR_AI_SETTINGS_SETTING; - colour = TC_LIGHT_BLUE; - SetDParamStr(idx++, config_item.description); - } - if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) { DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable); - SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); } else { int i = static_cast(std::distance(std::begin(this->visible_settings), it)); if (config_item.complete_labels) { @@ -388,18 +369,9 @@ struct ScriptSettingsWindow : public Window { } else { DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value); } - - auto config_iterator = config_item.labels.find(current_value); - if (config_iterator != config_item.labels.end()) { - SetDParam(idx++, STR_JUST_RAW_STRING); - SetDParamStr(idx++, config_iterator->second); - } else { - SetDParam(idx++, STR_JUST_INT); - SetDParam(idx++, current_value); - } } - DrawString(tr.left, tr.right, y + text_y_offset, str, colour); + DrawString(tr.left, tr.right, y + text_y_offset, config_item.GetString(current_value), config_item.GetColour()); y += this->line_height; } }