mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use EnumBitSet for ScriptConfigFlags. (#13669)
parent
e70f20a781
commit
94783fe2ed
|
@ -37,17 +37,17 @@ template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq
|
|||
SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
|
||||
SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
|
||||
SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER");
|
||||
|
||||
/* Pre 1.2 had an AI prefix */
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "AICONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "AICONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "AICONFIG_INGAME");
|
||||
|
||||
SQAIInfo.PostRegister(engine);
|
||||
engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
|
||||
|
|
|
@ -124,7 +124,7 @@ struct GSConfigWindow : public Window {
|
|||
visible_settings.clear();
|
||||
|
||||
for (const auto &item : *this->gs_config->GetConfigList()) {
|
||||
bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0;
|
||||
bool no_hide = !item.flags.Test(ScriptConfigFlag::Developer);
|
||||
if (no_hide || _settings_client.gui.ai_developer_tools) {
|
||||
visible_settings.push_back(&item);
|
||||
}
|
||||
|
@ -193,7 +193,7 @@ struct GSConfigWindow : public Window {
|
|||
int current_value = this->gs_config->GetSetting(config_item.name);
|
||||
bool editable = this->IsEditableItem(config_item);
|
||||
|
||||
if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
|
||||
if (config_item.flags.Test(ScriptConfigFlag::Boolean)) {
|
||||
DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable);
|
||||
} else {
|
||||
int i = static_cast<int>(std::distance(std::begin(this->visible_settings), it));
|
||||
|
@ -264,7 +264,7 @@ struct GSConfigWindow : public Window {
|
|||
this->clicked_dropdown = false;
|
||||
}
|
||||
|
||||
bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
|
||||
bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean);
|
||||
|
||||
Rect r = this->GetWidget<NWidgetBase>(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero);
|
||||
int x = pt.x - r.left;
|
||||
|
@ -404,15 +404,15 @@ private:
|
|||
bool IsEditableItem(const ScriptConfigItem &config_item) const
|
||||
{
|
||||
return _game_mode == GM_MENU
|
||||
|| _game_mode == GM_EDITOR
|
||||
|| (config_item.flags & SCRIPTCONFIG_INGAME) != 0
|
||||
|| _settings_client.gui.ai_developer_tools;
|
||||
|| _game_mode == GM_EDITOR
|
||||
|| config_item.flags.Test(ScriptConfigFlag::InGame)
|
||||
|| _settings_client.gui.ai_developer_tools;
|
||||
}
|
||||
|
||||
void SetValue(int value)
|
||||
{
|
||||
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
|
||||
if (_game_mode == GM_NORMAL && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
|
||||
if (_game_mode == GM_NORMAL && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
|
||||
this->gs_config->SetSetting(config_item.name, value);
|
||||
this->SetDirty();
|
||||
}
|
||||
|
|
|
@ -35,11 +35,11 @@ template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) {
|
|||
SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
|
||||
SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
|
||||
SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
|
||||
SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE");
|
||||
SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
|
||||
SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN");
|
||||
SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME");
|
||||
SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER");
|
||||
|
||||
SQGSInfo.PostRegister(engine);
|
||||
engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
|
||||
|
|
|
@ -73,7 +73,7 @@ void ScriptConfig::ClearConfigList()
|
|||
void ScriptConfig::AnchorUnchangeableSettings()
|
||||
{
|
||||
for (const auto &item : *this->GetConfigList()) {
|
||||
if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
|
||||
if (!item.flags.Test(ScriptConfigFlag::InGame)) {
|
||||
this->SetSetting(item.name, this->GetSetting(item.name));
|
||||
}
|
||||
}
|
||||
|
@ -112,8 +112,8 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start)
|
|||
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
|
||||
assert(config_item != nullptr);
|
||||
|
||||
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
|
||||
bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
|
||||
bool editable = yet_to_start || config_item->flags.Test(ScriptConfigFlag::InGame);
|
||||
bool visible = _settings_client.gui.ai_developer_tools || !config_item->flags.Test(ScriptConfigFlag::Developer);
|
||||
|
||||
if (editable && visible) {
|
||||
it = this->settings.erase(it);
|
||||
|
@ -193,7 +193,7 @@ ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
|
|||
|
||||
static std::pair<StringParameter, StringParameter> 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, {}};
|
||||
if (config_item.flags.Test(ScriptConfigFlag::Boolean)) 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};
|
||||
|
|
|
@ -17,15 +17,16 @@
|
|||
/** Maximum of 10 digits for MIN / MAX_INT32, 1 for the sign and 1 for '\0'. */
|
||||
static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION = 10 + 1 + 1;
|
||||
|
||||
/** Bitmask of flags for Script settings. */
|
||||
enum ScriptConfigFlags : uint8_t {
|
||||
SCRIPTCONFIG_NONE = 0x0, ///< No flags set.
|
||||
/** Flags for Script settings. */
|
||||
enum class ScriptConfigFlag : uint8_t {
|
||||
// Unused flag 0x1.
|
||||
SCRIPTCONFIG_BOOLEAN = 0x2, ///< This value is a boolean (either 0 (false) or 1 (true) ).
|
||||
SCRIPTCONFIG_INGAME = 0x4, ///< This setting can be changed while the Script is running.
|
||||
SCRIPTCONFIG_DEVELOPER = 0x8, ///< This setting will only be visible when the Script development tools are active.
|
||||
Boolean = 1, ///< This value is a boolean (either 0 (false) or 1 (true) ).
|
||||
InGame = 2, ///< This setting can be changed while the Script is running.
|
||||
Developer = 3, ///< This setting will only be visible when the Script development tools are active.
|
||||
};
|
||||
|
||||
using ScriptConfigFlags = EnumBitSet<ScriptConfigFlag, uint8_t>;
|
||||
|
||||
typedef std::map<int, std::string> LabelMapping; ///< Map-type used to map the setting numbers to labels.
|
||||
|
||||
/** Info about a single Script setting. */
|
||||
|
@ -36,7 +37,7 @@ struct ScriptConfigItem {
|
|||
int max_value = 1; ///< The maximal value this configuration setting can have.
|
||||
int default_value = 0; ///< The default value of this configuration setting.
|
||||
int step_size = 1; ///< The step size in the gui.
|
||||
ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting.
|
||||
ScriptConfigFlags flags{}; ///< Flags for the configuration setting.
|
||||
LabelMapping labels; ///< Text labels for the integer values.
|
||||
bool complete_labels = false; ///< True if all values have a label.
|
||||
|
||||
|
|
|
@ -314,7 +314,7 @@ struct ScriptSettingsWindow : public Window {
|
|||
visible_settings.clear();
|
||||
|
||||
for (const auto &item : *this->script_config->GetConfigList()) {
|
||||
bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0;
|
||||
bool no_hide = !item.flags.Test(ScriptConfigFlag::Developer);
|
||||
if (no_hide || _settings_client.gui.ai_developer_tools) {
|
||||
visible_settings.push_back(&item);
|
||||
}
|
||||
|
@ -360,7 +360,7 @@ struct ScriptSettingsWindow : public Window {
|
|||
int current_value = this->script_config->GetSetting(config_item.name);
|
||||
bool editable = this->IsEditableItem(config_item);
|
||||
|
||||
if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
|
||||
if (config_item.flags.Test(ScriptConfigFlag::Boolean)) {
|
||||
DrawBoolButton(br.left, y + button_y_offset, current_value != 0, editable);
|
||||
} else {
|
||||
int i = static_cast<int>(std::distance(std::begin(this->visible_settings), it));
|
||||
|
@ -403,7 +403,7 @@ struct ScriptSettingsWindow : public Window {
|
|||
this->clicked_dropdown = false;
|
||||
}
|
||||
|
||||
bool bool_item = (config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0;
|
||||
bool bool_item = config_item.flags.Test(ScriptConfigFlag::Boolean);
|
||||
|
||||
Rect r = this->GetWidget<NWidgetBase>(widget)->GetCurrentRect().Shrink(WidgetDimensions::scaled.matrix, RectPadding::zero);
|
||||
int x = pt.x - r.left;
|
||||
|
@ -537,14 +537,14 @@ private:
|
|||
return _game_mode == GM_MENU
|
||||
|| _game_mode == GM_EDITOR
|
||||
|| ((this->slot != OWNER_DEITY) && !Company::IsValidID(this->slot))
|
||||
|| (config_item.flags & SCRIPTCONFIG_INGAME) != 0
|
||||
|| config_item.flags.Test(ScriptConfigFlag::InGame)
|
||||
|| _settings_client.gui.ai_developer_tools;
|
||||
}
|
||||
|
||||
void SetValue(int value)
|
||||
{
|
||||
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
|
||||
if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && (config_item.flags & SCRIPTCONFIG_INGAME) == 0) return;
|
||||
if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
|
||||
this->script_config->SetSetting(config_item.name, value);
|
||||
this->SetDirty();
|
||||
}
|
||||
|
|
|
@ -177,7 +177,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
|||
}
|
||||
|
||||
/* Make sure all properties are defined */
|
||||
uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
|
||||
uint mask = config.flags.Test(ScriptConfigFlag::Boolean) ? 0x1F3 : 0x1FF;
|
||||
if (items != mask) {
|
||||
this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)");
|
||||
return SQ_ERROR;
|
||||
|
|
Loading…
Reference in New Issue