add is_default script function

This commit is contained in:
GenevensiS
2026-04-19 19:06:52 +02:00
parent 17dd3cf407
commit d782e4851c
22 changed files with 93 additions and 13 deletions
+4
View File
@@ -305,6 +305,10 @@ bool Value::equals(const Value* that) {
return this == that;
}
bool Value::isDefault() {
return false;
}
bool Value::update(Context& ctx) {
updateAge();
updateSortValue(ctx);
+3 -1
View File
@@ -255,7 +255,9 @@ public:
virtual ValueP clone() const = 0;
/// Convert this value to a string for use in tables
virtual String toString() const = 0;
virtual String toString() const = 0;
/// Check if this value is in the default state
virtual bool isDefault();
/// Apply scripts to this value, return true if the value has changed
virtual bool update(Context& ctx);
/// This value has been updated by an action
+6 -1
View File
@@ -317,7 +317,12 @@ ChoiceValue::ChoiceValue(const ChoiceFieldP& field, bool initial_first_choice)
String ChoiceValue::toString() const {
return value();
}
}
bool ChoiceValue::isDefault() {
return value.isDefault();
}
bool ChoiceValue::update(Context& ctx) {
bool change = field().default_script.invokeOnDefault(ctx, value)
| field(). script.invokeOn(ctx, value);
+3 -1
View File
@@ -199,7 +199,9 @@ public:
DECLARE_VALUE_TYPE(Choice, Defaultable<String>);
ValueType value; /// The name of the selected choice
bool isDefault() override;
bool update(Context&) override;
};
+5
View File
@@ -110,7 +110,12 @@ String ColorValue::toString() const {
if (value() == c->color) return c->name;
}
return _("<color>");
}
bool ColorValue::isDefault() {
return value.isDefault();
}
bool ColorValue::update(Context& ctx) {
bool change = field().default_script.invokeOnDefault(ctx, value)
| field(). script.invokeOn(ctx, value);
+3 -1
View File
@@ -77,7 +77,9 @@ public:
DECLARE_VALUE_TYPE(Color, Defaultable<Color>);
ValueType value; ///< The value
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -39,6 +39,10 @@ String ImageValue::toString() const {
return filename.empty() ? _("") : _("<image>");
}
bool ImageValue::isDefault() {
return filename.empty();
}
// custom reflection: convert to ScriptImageP for scripting
void ImageValue::reflect(Reader& handler) {
+3 -1
View File
@@ -47,7 +47,9 @@ public:
}
ValueType filename; ///< Filename of the image (in the current package), or ""
Age last_update; ///< When was the image last changed?
Age last_update; ///< When was the image last changed?
bool isDefault() override;
};
// ----------------------------------------------------------------------------- : ImageStyle
+5
View File
@@ -62,7 +62,12 @@ IMPLEMENT_REFLECTION(InfoStyle) {
String InfoValue::toString() const {
return value;
}
bool InfoValue::isDefault() {
return true;
}
bool InfoValue::update(Context& ctx) {
if (value.empty()) value = field().caption.get();
bool change = field().script.invokeOn(ctx, value);
+3 -1
View File
@@ -60,7 +60,9 @@ public:
DECLARE_VALUE_TYPE(Info, String);
ValueType value;
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -52,6 +52,10 @@ IMPLEMENT_REFLECTION_NAMELESS(MultipleChoiceValue) {
REFLECT_BASE(ChoiceValue);
}
bool MultipleChoiceValue::isDefault() {
return value.isDefault();
}
bool MultipleChoiceValue::update(Context& ctx) {
String old_value = value();
ctx.setVariable(_("last_change"), to_script(last_change));
+3 -1
View File
@@ -63,7 +63,9 @@ public:
/// Splits the value, stores the selected choices in the out parameter
void get(vector<String>& out) const;
bool isDefault() override;
bool update(Context&) override;
private:
+6
View File
@@ -62,6 +62,12 @@ PackagedP PackageChoiceValue::getPackage() const {
else return package_manager.openAny(package_name, true);
}
bool PackageChoiceValue::isDefault() {
PackageChoiceFieldP packageFieldP = boost::dynamic_pointer_cast<PackageChoiceField>(fieldP);
if (packageFieldP) return package_name == packageFieldP->initial;
return false;
}
bool PackageChoiceValue::update(Context& ctx) {
bool change = field().script.invokeOn(ctx, package_name);
Value::update(ctx);
+3 -1
View File
@@ -61,7 +61,9 @@ public:
/// Get the package (if it is set), otherwise return nullptr
PackagedP getPackage() const;
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -51,6 +51,10 @@ String SymbolValue::toString() const {
return filename.empty() ? _("") : _("<symbol>");
}
bool SymbolValue::isDefault() {
return filename.empty();
}
IMPLEMENT_REFLECTION_NO_GET_MEMBER(SymbolValue) {
if (fieldP->save_value || !handler.isWriting) REFLECT_NAMELESS(filename);
}
+3 -1
View File
@@ -69,6 +69,8 @@ public:
DECLARE_VALUE_TYPE(Symbol, LocalFileName);
ValueType filename; ///< Filename of the symbol (in the current package)
Age last_update; ///< When was the symbol last changed?
Age last_update; ///< When was the symbol last changed?
bool isDefault() override;
};
+5
View File
@@ -176,7 +176,12 @@ IMPLEMENT_REFLECTION(TextStyle) {
String TextValue::toString() const {
return untag_hide_sep(value());
}
bool TextValue::isDefault() {
return value.isDefault();
}
bool TextValue::update(Context& ctx) {
updateAge();
WITH_DYNAMIC_ARG(last_update_age, last_update.get());
+3 -1
View File
@@ -112,7 +112,9 @@ public:
ValueType value; ///< The text of this value
Age last_update; ///< When was the text last changed?
bool isDefault() override;
bool update(Context&) override;
};