mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Added script support for wxDateTime values:
- added ScriptDateTime type - added to_date function - added date formating support to to_string. - documented all of the above. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1348 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -818,6 +818,7 @@ type:
|
|||||||
boolean: boolean
|
boolean: boolean
|
||||||
color: color
|
color: color
|
||||||
image: image
|
image: image
|
||||||
|
date: date
|
||||||
nil: nothing
|
nil: nothing
|
||||||
|
|
||||||
# Object types
|
# Object types
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ These functions are built into the program, other [[type:function]]s can be defi
|
|||||||
| [[fun:to_number]] Convert any value to a number
|
| [[fun:to_number]] Convert any value to a number
|
||||||
| [[fun:to_boolean]] Convert any value to a [[type:boolean]]
|
| [[fun:to_boolean]] Convert any value to a [[type:boolean]]
|
||||||
| [[fun:to_color]] Convert any value to a [[type:color]]
|
| [[fun:to_color]] Convert any value to a [[type:color]]
|
||||||
|
| [[fun:to_date]] Convert any value to a [[type:date]]
|
||||||
|
|
||||||
! Numbers <<<
|
! Numbers <<<
|
||||||
| [[fun:abs]] Absolute value
|
| [[fun:abs]] Absolute value
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
Function: to_date
|
||||||
|
|
||||||
|
DOC_MSE_VERSION: since 0.3.8
|
||||||
|
|
||||||
|
--Usage--
|
||||||
|
> to_date(any value)
|
||||||
|
|
||||||
|
Convert a string value to a [[type:date]].
|
||||||
|
|
||||||
|
--Parameters--
|
||||||
|
! Parameter Type Description
|
||||||
|
| @input@ [[type:string]] Value to convert to a date.
|
||||||
|
|
||||||
|
--Examples--
|
||||||
|
> to_date("2008-12-31 23:59:59")
|
||||||
|
> to_date("today midnight")
|
||||||
|
|
||||||
|
--See also--
|
||||||
|
| [[fun:to_string]] Convert dates to strings
|
||||||
@@ -15,7 +15,8 @@ The former converts things like number to string, while the latter removes tags
|
|||||||
! Parameter Type Description
|
! Parameter Type Description
|
||||||
| @input@ ''any type'' Value to convert to a string
|
| @input@ ''any type'' Value to convert to a string
|
||||||
| @format@ ''optional'' Formatting to apply.<br/>
|
| @format@ ''optional'' Formatting to apply.<br/>
|
||||||
This takes the same form as [[http://www.cplusplus.com/reference/clibrary/cstdio/printf.html|printf format specifiers]].
|
This takes the same form as [[http://www.cplusplus.com/reference/clibrary/cstdio/printf.html|printf format specifiers]].<br/>
|
||||||
|
When formating a date, the format string takes the same form as [[http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html|strftime format specifiers]].
|
||||||
|
|
||||||
--Examples--
|
--Examples--
|
||||||
> to_string(to_color("blue")) == "rgb(0,0,255)"
|
> to_string(to_color("blue")) == "rgb(0,0,255)"
|
||||||
@@ -23,4 +24,4 @@ The former converts things like number to string, while the latter removes tags
|
|||||||
> to_string(10 + 20, format: ".3f") == "30.000"
|
> to_string(10 + 20, format: ".3f") == "30.000"
|
||||||
> to_string(10 + 20, format: "x") == "1e" # hexadecimal notation
|
> to_string(10 + 20, format: "x") == "1e" # hexadecimal notation
|
||||||
> to_string("xy", format: "3s") == " xy"
|
> to_string("xy", format: "3s") == " xy"
|
||||||
|
> to_string(to_date("2008-12-31 23:59:59"), format:"%d/%m/%y") == "31/12/08"
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
Primitive type: date and time
|
||||||
|
|
||||||
|
DOC_MSE_VERSION: since 0.3.8
|
||||||
|
|
||||||
|
A point in time, consisting of a date and a time.
|
||||||
|
The file syntax uses [[http://en.wikipedia.org/wiki/ISO_8601|ISO 8601]] notation.
|
||||||
|
|
||||||
|
--File syntax--
|
||||||
|
> something: 2008-12-31 23:59:59
|
||||||
|
|
||||||
|
--See also--
|
||||||
|
| [[fun:to_date]] Convert a value to a date
|
||||||
|
| [[fun:to_string]] Convert dates to strings
|
||||||
@@ -66,3 +66,4 @@ These contain several properties, similair to the file types. But they are part
|
|||||||
| [[type:double]] Real numbers, @1, 0.5, 21.3@
|
| [[type:double]] Real numbers, @1, 0.5, 21.3@
|
||||||
| [[type:version]] Version numbers
|
| [[type:version]] Version numbers
|
||||||
| [[type:color]] Colors
|
| [[type:color]] Colors
|
||||||
|
| [[type:date]] Dates/times
|
||||||
|
|||||||
+2
-2
@@ -78,8 +78,8 @@ IMPLEMENT_REFLECTION(Card) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
REFLECT(notes);
|
REFLECT(notes);
|
||||||
REFLECT_NO_SCRIPT(time_created);
|
REFLECT(time_created);
|
||||||
REFLECT_NO_SCRIPT(time_modified);
|
REFLECT(time_modified);
|
||||||
REFLECT(extra_data); // don't allow scripts to depend on style specific data
|
REFLECT(extra_data); // don't allow scripts to depend on style specific data
|
||||||
REFLECT_NAMELESS(data);
|
REFLECT_NAMELESS(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,6 @@ template <> void Reader::handle(Color& col) {
|
|||||||
if (!col.Ok()) col = *wxBLACK;
|
if (!col.Ok()) col = *wxBLACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <> void GetDefaultMember::handle(const AColor& col) {
|
|
||||||
handle((const Color&)col);
|
|
||||||
}
|
|
||||||
template <> void Reader::handle(AColor& col) {
|
template <> void Reader::handle(AColor& col) {
|
||||||
col = parse_acolor(getValue());
|
col = parse_acolor(getValue());
|
||||||
if (!col.Ok()) col = AColor(0,0,0,0);
|
if (!col.Ok()) col = AColor(0,0,0,0);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# This file contains the keys expected to be in MSE locales
|
# This file contains the keys expected to be in MSE locales
|
||||||
# It was automatically generated by tools/locale/locale.pl
|
# It was automatically generated by tools/locale/locale.pl
|
||||||
# Generated on Sat Jan 10 02:51:15 2009
|
# Generated on Sun Jan 11 17:40:25 2009
|
||||||
|
|
||||||
action:
|
action:
|
||||||
add control point: 0
|
add control point: 0
|
||||||
@@ -580,6 +580,7 @@ type:
|
|||||||
collection: 0
|
collection: 0
|
||||||
collection of: 1
|
collection of: 1
|
||||||
color: 0
|
color: 0
|
||||||
|
date: 0
|
||||||
double: 0
|
double: 0
|
||||||
ellipse: 0
|
ellipse: 0
|
||||||
export template: 0
|
export template: 0
|
||||||
|
|||||||
@@ -59,33 +59,36 @@ SCRIPT_FUNCTION(warning_if_neq) {
|
|||||||
// ----------------------------------------------------------------------------- : Conversion
|
// ----------------------------------------------------------------------------- : Conversion
|
||||||
|
|
||||||
/// Format the input variable based on a printf like style specification
|
/// Format the input variable based on a printf like style specification
|
||||||
String format_input(const String& format, Context& ctx) {
|
String format_input(const String& format, const ScriptValue& input) {
|
||||||
String fmt = _("%") + replace_all(format, _("%"), _(""));
|
// determine type of input
|
||||||
|
ScriptType type = input.type();
|
||||||
|
if (type == SCRIPT_DATETIME) {
|
||||||
|
return static_cast<wxDateTime>(input).Format(format.c_str());
|
||||||
|
} else {
|
||||||
// determine type expected by format string
|
// determine type expected by format string
|
||||||
|
String fmt = _("%") + replace_all(format, _("%"), _(""));
|
||||||
if (format.find_first_of(_("DdIiOoXx")) != String::npos) {
|
if (format.find_first_of(_("DdIiOoXx")) != String::npos) {
|
||||||
SCRIPT_PARAM_C(int, input);
|
return String::Format(fmt, (int)input);
|
||||||
return String::Format(fmt, input);
|
|
||||||
} else if (format.find_first_of(_("EeFfGg")) != String::npos) {
|
} else if (format.find_first_of(_("EeFfGg")) != String::npos) {
|
||||||
SCRIPT_PARAM_C(double, input);
|
return String::Format(fmt, (double)input);
|
||||||
return String::Format(fmt, input);
|
|
||||||
} else if (format.find_first_of(_("Ss")) != String::npos) {
|
} else if (format.find_first_of(_("Ss")) != String::npos) {
|
||||||
SCRIPT_PARAM_C(String, input);
|
return format_string(fmt, input.toString());
|
||||||
return format_string(fmt, input);
|
|
||||||
} else {
|
} else {
|
||||||
throw ScriptError(_ERROR_1_("unsupported format", format));
|
throw ScriptError(_ERROR_1_("unsupported format", format));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_string) {
|
SCRIPT_FUNCTION(to_string) {
|
||||||
|
SCRIPT_PARAM_C(ScriptValueP, input);
|
||||||
ScriptValueP format = ctx.getVariable(SCRIPT_VAR_format);
|
ScriptValueP format = ctx.getVariable(SCRIPT_VAR_format);
|
||||||
try {
|
try {
|
||||||
if (format && format->type() == SCRIPT_STRING) {
|
if (format && format->type() == SCRIPT_STRING) {
|
||||||
// format specifier. Be careful, the built in function 'format' has the same name
|
// format specifier. Be careful, the built in function 'format' has the same name
|
||||||
SCRIPT_RETURN(format_input(*format, ctx));
|
SCRIPT_RETURN(format_input(*format, *input));
|
||||||
} else {
|
} else {
|
||||||
// simple conversion
|
// simple conversion
|
||||||
SCRIPT_PARAM_C(String, input);
|
SCRIPT_RETURN(input->toString());
|
||||||
SCRIPT_RETURN(input);
|
|
||||||
}
|
}
|
||||||
} catch (const ScriptError& e) {
|
} catch (const ScriptError& e) {
|
||||||
return new_intrusive1<ScriptDelayedError>(e);
|
return new_intrusive1<ScriptDelayedError>(e);
|
||||||
@@ -203,6 +206,15 @@ SCRIPT_FUNCTION(to_color) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SCRIPT_FUNCTION(to_date) {
|
||||||
|
try {
|
||||||
|
SCRIPT_PARAM_C(wxDateTime, input);
|
||||||
|
SCRIPT_RETURN(input);
|
||||||
|
} catch (const ScriptError& e) {
|
||||||
|
return delay_error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_code) {
|
SCRIPT_FUNCTION(to_code) {
|
||||||
SCRIPT_PARAM_C(ScriptValueP, input);
|
SCRIPT_PARAM_C(ScriptValueP, input);
|
||||||
SCRIPT_RETURN(input->toCode());
|
SCRIPT_RETURN(input->toCode());
|
||||||
@@ -295,7 +307,8 @@ SCRIPT_FUNCTION(contains) {
|
|||||||
|
|
||||||
SCRIPT_FUNCTION(format) {
|
SCRIPT_FUNCTION(format) {
|
||||||
SCRIPT_PARAM_C(String, format);
|
SCRIPT_PARAM_C(String, format);
|
||||||
SCRIPT_RETURN(format_input(format,ctx));
|
SCRIPT_PARAM_C(ScriptValueP, input);
|
||||||
|
SCRIPT_RETURN(format_input(format,*input));
|
||||||
}
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(curly_quotes) {
|
SCRIPT_FUNCTION(curly_quotes) {
|
||||||
@@ -651,6 +664,7 @@ void init_script_basic_functions(Context& ctx) {
|
|||||||
ctx.setVariable(_("to number"), script_to_number);
|
ctx.setVariable(_("to number"), script_to_number);
|
||||||
ctx.setVariable(_("to boolean"), script_to_boolean);
|
ctx.setVariable(_("to boolean"), script_to_boolean);
|
||||||
ctx.setVariable(_("to color"), script_to_color);
|
ctx.setVariable(_("to color"), script_to_color);
|
||||||
|
ctx.setVariable(_("to date"), script_to_date);
|
||||||
ctx.setVariable(_("to code"), script_to_code);
|
ctx.setVariable(_("to code"), script_to_code);
|
||||||
// math
|
// math
|
||||||
ctx.setVariable(_("abs"), script_abs);
|
ctx.setVariable(_("abs"), script_abs);
|
||||||
|
|||||||
@@ -390,6 +390,7 @@ inline ScriptValueP to_script(long v) { return to_script((int) v); }
|
|||||||
ScriptValueP to_script(const String& v);
|
ScriptValueP to_script(const String& v);
|
||||||
ScriptValueP to_script(Color v);
|
ScriptValueP to_script(Color v);
|
||||||
ScriptValueP to_script(AColor v);
|
ScriptValueP to_script(AColor v);
|
||||||
|
ScriptValueP to_script(wxDateTime v);
|
||||||
inline ScriptValueP to_script(bool v) { return v ? script_true : script_false; }
|
inline ScriptValueP to_script(bool v) { return v ? script_true : script_false; }
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline ScriptValueP to_script(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); }
|
inline ScriptValueP to_script(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); }
|
||||||
@@ -419,6 +420,7 @@ template <> inline double from_script<double> (const ScriptValueP& va
|
|||||||
template <> inline bool from_script<bool> (const ScriptValueP& value) { return *value; }
|
template <> inline bool from_script<bool> (const ScriptValueP& value) { return *value; }
|
||||||
template <> inline Color from_script<Color> (const ScriptValueP& value) { return (AColor)*value; }
|
template <> inline Color from_script<Color> (const ScriptValueP& value) { return (AColor)*value; }
|
||||||
template <> inline AColor from_script<AColor> (const ScriptValueP& value) { return *value; }
|
template <> inline AColor from_script<AColor> (const ScriptValueP& value) { return *value; }
|
||||||
|
template <> inline wxDateTime from_script<wxDateTime> (const ScriptValueP& value) { return *value; }
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : EOF
|
// ----------------------------------------------------------------------------- : EOF
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ ScriptValue::operator int() const { throw Script
|
|||||||
ScriptValue::operator bool() const { throw ScriptErrorConversion(typeName(), _TYPE_("boolean" )); }
|
ScriptValue::operator bool() const { throw ScriptErrorConversion(typeName(), _TYPE_("boolean" )); }
|
||||||
ScriptValue::operator double() const { throw ScriptErrorConversion(typeName(), _TYPE_("double" )); }
|
ScriptValue::operator double() const { throw ScriptErrorConversion(typeName(), _TYPE_("double" )); }
|
||||||
ScriptValue::operator AColor() const { throw ScriptErrorConversion(typeName(), _TYPE_("color" )); }
|
ScriptValue::operator AColor() const { throw ScriptErrorConversion(typeName(), _TYPE_("color" )); }
|
||||||
|
ScriptValue::operator wxDateTime() const { throw ScriptErrorConversion(typeName(), _TYPE_("date" )); }
|
||||||
ScriptValueP ScriptValue::eval(Context&) const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("function"))); }
|
ScriptValueP ScriptValue::eval(Context&) const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("function"))); }
|
||||||
ScriptValueP ScriptValue::next(ScriptValueP* key_out) { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
ScriptValueP ScriptValue::next(ScriptValueP* key_out) { throw InternalError(_("Can't convert from ")+typeName()+_(" to iterator")); }
|
||||||
ScriptValueP ScriptValue::makeIterator(const ScriptValueP&) const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("collection"))); }
|
ScriptValueP ScriptValue::makeIterator(const ScriptValueP&) const { return delay_error(ScriptErrorConversion(typeName(), _TYPE_("collection"))); }
|
||||||
@@ -276,6 +277,13 @@ class ScriptString : public ScriptValue {
|
|||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
virtual operator wxDateTime() const {
|
||||||
|
wxDateTime date;
|
||||||
|
if (!date.ParseDateTime(value.c_str())) {
|
||||||
|
throw ScriptErrorConversion(value, typeName(), _TYPE_("date"));
|
||||||
|
}
|
||||||
|
return date;
|
||||||
|
}
|
||||||
virtual GeneratedImageP toImage(const ScriptValueP&) const {
|
virtual GeneratedImageP toImage(const ScriptValueP&) const {
|
||||||
if (value.empty()) {
|
if (value.empty()) {
|
||||||
return new_intrusive<BlankImage>();
|
return new_intrusive<BlankImage>();
|
||||||
@@ -327,6 +335,27 @@ ScriptValueP to_script(AColor v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------- : DateTime
|
||||||
|
|
||||||
|
// wxDateTime values
|
||||||
|
class ScriptDateTime : public ScriptValue {
|
||||||
|
public:
|
||||||
|
ScriptDateTime(const wxDateTime& v) : value(v) {}
|
||||||
|
virtual ScriptType type() const { return SCRIPT_DATETIME; }
|
||||||
|
virtual String typeName() const { return _TYPE_("date"); }
|
||||||
|
virtual operator wxDateTime() const { return value; }
|
||||||
|
virtual operator String() const {
|
||||||
|
return value.Format(_("%Y-%m-%d %H:%M:%S"));
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
wxDateTime value;
|
||||||
|
};
|
||||||
|
|
||||||
|
ScriptValueP to_script(wxDateTime v) {
|
||||||
|
return new_intrusive1<ScriptDateTime>(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Nil type
|
// ----------------------------------------------------------------------------- : Nil type
|
||||||
|
|
||||||
// the nil object
|
// the nil object
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ enum ScriptType
|
|||||||
, SCRIPT_OBJECT // Only ScriptObject
|
, SCRIPT_OBJECT // Only ScriptObject
|
||||||
, SCRIPT_COLLECTION
|
, SCRIPT_COLLECTION
|
||||||
, SCRIPT_REGEX
|
, SCRIPT_REGEX
|
||||||
|
, SCRIPT_DATETIME
|
||||||
, SCRIPT_ITERATOR
|
, SCRIPT_ITERATOR
|
||||||
, SCRIPT_DUMMY
|
, SCRIPT_DUMMY
|
||||||
, SCRIPT_ERROR
|
, SCRIPT_ERROR
|
||||||
@@ -67,6 +68,8 @@ class ScriptValue : public IntrusivePtrBaseWithDelete {
|
|||||||
virtual operator bool() const;
|
virtual operator bool() const;
|
||||||
/// Convert this value to a color
|
/// Convert this value to a color
|
||||||
virtual operator AColor() const;
|
virtual operator AColor() const;
|
||||||
|
/// Convert this value to a wxDateTime
|
||||||
|
virtual operator wxDateTime() const;
|
||||||
|
|
||||||
/// Script code to generate this value
|
/// Script code to generate this value
|
||||||
virtual String toCode() const;
|
virtual String toCode() const;
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ template <> void GetDefaultMember::handle(const bool& v) { value = to_sc
|
|||||||
template <> void GetDefaultMember::handle(const tribool& v) { value = to_script((bool)v); }
|
template <> void GetDefaultMember::handle(const tribool& v) { value = to_script((bool)v); }
|
||||||
template <> void GetDefaultMember::handle(const Vector2D& v) { value = to_script(String::Format(_("(%.10lf,%.10lf)"), v.x, v.y)); }
|
template <> void GetDefaultMember::handle(const Vector2D& v) { value = to_script(String::Format(_("(%.10lf,%.10lf)"), v.x, v.y)); }
|
||||||
template <> void GetDefaultMember::handle(const Color& v) { value = to_script(v); }
|
template <> void GetDefaultMember::handle(const Color& v) { value = to_script(v); }
|
||||||
|
template <> void GetDefaultMember::handle(const AColor& v) { value = to_script(v); }
|
||||||
|
template <> void GetDefaultMember::handle(const wxDateTime& v) { value = to_script(v); }
|
||||||
void GetDefaultMember::handle(const ScriptValueP& v) { value = v; }
|
void GetDefaultMember::handle(const ScriptValueP& v) { value = v; }
|
||||||
void GetDefaultMember::handle(const ScriptP& v) { value = v; }
|
void GetDefaultMember::handle(const ScriptP& v) { value = v; }
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ $built_in_functions = array(
|
|||||||
'to_number' =>'',
|
'to_number' =>'',
|
||||||
'to_boolean' =>'',
|
'to_boolean' =>'',
|
||||||
'to_color' =>'',
|
'to_color' =>'',
|
||||||
|
'to_date' =>'',
|
||||||
// numbers
|
// numbers
|
||||||
'abs' =>'',
|
'abs' =>'',
|
||||||
'random_int' =>'',
|
'random_int' =>'',
|
||||||
@@ -33,6 +34,7 @@ $built_in_functions = array(
|
|||||||
'match' =>'', 'match_rule' => 'match',
|
'match' =>'', 'match_rule' => 'match',
|
||||||
'trim' =>'',
|
'trim' =>'',
|
||||||
'regex_escape' =>'',
|
'regex_escape' =>'',
|
||||||
|
'check_spelling' =>'',
|
||||||
// tags
|
// tags
|
||||||
'tag_contents' =>'', 'tag_contents_rule'=>'tag_contents',
|
'tag_contents' =>'', 'tag_contents_rule'=>'tag_contents',
|
||||||
'remove_tag' =>'', 'tag_remove_rule' =>'remove_tag',
|
'remove_tag' =>'', 'tag_remove_rule' =>'remove_tag',
|
||||||
|
|||||||
Reference in New Issue
Block a user