mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
add is_default script function
This commit is contained in:
@@ -78,6 +78,7 @@ These functions are built into the program, other [[type:function]]s can be defi
|
||||
| [[fun:exclusive_choice]] Require that at most one of the given choices is selected.
|
||||
| [[fun:require_exclusive_choice]] Require that exactly one of the given choices is selected.
|
||||
| [[fun:remove_choice]] Remove the given choices from a multiple choice value.
|
||||
| [[fun:is_default]] Check if a field is in its default state.
|
||||
|
||||
! Images <<<
|
||||
| [[fun:linear_blend]] Blend two images together using a linear gradient.
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
Function: is_default
|
||||
|
||||
--Usage--
|
||||
> is_default(some_field)
|
||||
|
||||
Returns true if the field is in its default state, that is, if the user hasn't modified the value yet, false otherwise.
|
||||
|
||||
--Parameters--
|
||||
! Parameter Type Description
|
||||
| @input@ [[type:field]] Field to test.
|
||||
@@ -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
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -77,7 +77,9 @@ public:
|
||||
DECLARE_VALUE_TYPE(Color, Defaultable<Color>);
|
||||
|
||||
ValueType value; ///< The value
|
||||
|
||||
|
||||
bool isDefault() override;
|
||||
|
||||
bool update(Context&) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -60,7 +60,9 @@ public:
|
||||
DECLARE_VALUE_TYPE(Info, String);
|
||||
|
||||
ValueType value;
|
||||
|
||||
|
||||
bool isDefault() override;
|
||||
|
||||
bool update(Context&) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -116,14 +116,15 @@ Bitmap ImageValueViewer::imagePlaceholder(const Rotation& rot, UInt w, UInt h, c
|
||||
}
|
||||
// Draw text
|
||||
if (editing) {
|
||||
// only when in editor mode
|
||||
// only when in editor mode
|
||||
String label = _LABEL_("load image");
|
||||
for (UInt size = 12 ; size > 2 ; --size) {
|
||||
dc.SetFont(wxFont(size, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));
|
||||
RealSize rs = dc.GetTextExtent(_LABEL_("load image"));
|
||||
RealSize rs = dc.GetTextExtent(label);
|
||||
if (rs.width <= w - 10 && rs.height < h - 10) {
|
||||
// text fits
|
||||
RealPoint pos = align_in_rect(ALIGN_MIDDLE_CENTER, rs, rect);
|
||||
dc.DrawText(_LABEL_("load image"), pos, Color(255,255,255), 2, Color(0,0,0), 3); // stroked
|
||||
dc.DrawText(label, pos, Color(255,255,255), 2, Color(0,0,0), 3); // stroked
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +187,13 @@ SCRIPT_FUNCTION_DEPENDENCIES(combined_editor) {
|
||||
}
|
||||
}
|
||||
return dependency_dummy;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Values
|
||||
|
||||
SCRIPT_FUNCTION(is_default) {
|
||||
SCRIPT_PARAM_C(ValueP,input);
|
||||
SCRIPT_RETURN(input->isDefault());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Choice values
|
||||
@@ -389,6 +396,7 @@ SCRIPT_FUNCTION(count_chosen) {
|
||||
void init_script_editor_functions(Context& ctx) {
|
||||
ctx.setVariable(_("forward_editor"), script_combined_editor); // compatability
|
||||
ctx.setVariable(_("combined_editor"), script_combined_editor);
|
||||
ctx.setVariable(_("is_default"), script_is_default);
|
||||
ctx.setVariable(_("primary_choice"), script_primary_choice);
|
||||
ctx.setVariable(_("chosen"), script_chosen);
|
||||
ctx.setVariable(_("count_chosen"), script_count_chosen);
|
||||
|
||||
Reference in New Issue
Block a user