mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Changes to make it work in windows again;
New header util/platform.hpp for platform specific stuff that doesn't go elsewhere; Changed resource names from "CURSOR_SOMETHING" to "cursor/something", so it matches the filenames (in progress for other resource types). git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@190 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+1
-1
@@ -10,7 +10,7 @@
|
||||
#include <data/game.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(FieldP);
|
||||
typedef IndexMap<FieldP,ValueP> IndexMap_FieldP_ValueP;
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#include <data/statistics.hpp>
|
||||
#include <util/io/package_manager.hpp>
|
||||
#include <script/script.hpp>
|
||||
#include <script/value.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(FieldP);
|
||||
DECLARE_TYPEOF_COLLECTION(StatsDimensionP);
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@
|
||||
|
||||
#include <data/locale.hpp>
|
||||
#include <util/io/package_manager.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Locale class
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include <data/field/text.hpp> // for 0.2.7 fix
|
||||
#include <util/tagged_string.hpp> // for 0.2.7 fix
|
||||
#include <util/order_cache.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <script/script_manager.hpp>
|
||||
#include <wx/sstream.h>
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/field.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/platform.hpp>
|
||||
#include <util/io/reader.hpp>
|
||||
#include <util/io/writer.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <wx/filename.h>
|
||||
#include <wx/wfstream.h>
|
||||
#include <wx/stdpaths.h>
|
||||
@@ -136,7 +136,7 @@ StyleSheetSettings& Settings::stylesheetSettingsFor(const StyleSheet& stylesheet
|
||||
/// Retrieve the directory to use for settings and other data files
|
||||
String user_settings_dir() {
|
||||
String dir = wxStandardPaths::Get().GetUserDataDir();
|
||||
if (!wxDirExists(dir)) wxMkDir(wxConvLocal.cWX2MB(dir), 0777);
|
||||
if (!wxDirExists(dir)) wxMkDir(dir);
|
||||
return dir + _("/");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -7,8 +7,8 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <data/symbol.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <gfx/bezier.hpp>
|
||||
#include <script/value.hpp>
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(SymbolPartP);
|
||||
DECLARE_TYPEOF_COLLECTION(ControlPointP);
|
||||
|
||||
@@ -24,10 +24,10 @@ SymbolPointEditor::SymbolPointEditor(SymbolControl* control, const SymbolPartP&
|
||||
, selection(SELECTED_NONE)
|
||||
, hovering(SELECTED_NONE)
|
||||
// Load gui stock
|
||||
, pointSelect(load_resource_image(_("CUR_POINT")))
|
||||
, pointAdd (load_resource_image(_("CUR_POINT_ADD")))
|
||||
, pointCurve (load_resource_image(_("CUR_POINT_CURVE")))
|
||||
, pointMove (load_resource_image(_("CUR_POINT_MOVE")))
|
||||
, pointSelect(load_resource_cursor(_("point")))
|
||||
, pointAdd (load_resource_cursor(_("point_add")))
|
||||
, pointCurve (load_resource_cursor(_("curve")))
|
||||
, pointMove (load_resource_cursor(_("point_move")))
|
||||
{
|
||||
resetActions();
|
||||
// // fix pen joins
|
||||
|
||||
@@ -20,9 +20,9 @@ DECLARE_TYPEOF_COLLECTION(SymbolPartP);
|
||||
SymbolSelectEditor::SymbolSelectEditor(SymbolControl* control, bool rotate)
|
||||
: SymbolEditorBase(control)
|
||||
, rotate(rotate)
|
||||
, cursorRotate(load_resource_image(_("CUR_ROTATE")))
|
||||
, cursorShearX(load_resource_image(_("CUR_SHEAR_X")))
|
||||
, cursorShearY(load_resource_image(_("CUR_SHEAR_Y")))
|
||||
, cursorRotate(load_resource_cursor(_("rotate")))
|
||||
, cursorShearX(load_resource_cursor(_("shear_x")))
|
||||
, cursorShearY(load_resource_cursor(_("shear_y")))
|
||||
{
|
||||
// Load resource images
|
||||
Image rot = load_resource_image(_("HANDLE_ROTATE"));
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <gui/thumbnail_thread.hpp>
|
||||
#include <util/platform.hpp>
|
||||
#include <wx/thread.h>
|
||||
|
||||
typedef pair<ThumbnailRequestP,Image> pair_ThumbnailRequestP_Image;
|
||||
@@ -17,7 +18,7 @@ DECLARE_TYPEOF_COLLECTION(pair_ThumbnailRequestP_Image);
|
||||
String user_settings_dir();
|
||||
String image_cache_dir() {
|
||||
String dir = user_settings_dir() + _("/cache");
|
||||
if (!wxDirExists(dir)) wxMkDir(wxConvLocal.cWX2MB(dir), 0777);
|
||||
if (!wxDirExists(dir)) wxMkDir(dir);
|
||||
return dir + _("/");
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -69,7 +69,7 @@ void draw_checker(RotatedDC& dc, const RealRect& rect) {
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Image related
|
||||
// ----------------------------------------------------------------------------- : Resource related
|
||||
|
||||
Image load_resource_image(const String& name) {
|
||||
#if defined(__WXMSW__)
|
||||
@@ -102,6 +102,14 @@ Image load_resource_image(const String& name) {
|
||||
#endif
|
||||
}
|
||||
|
||||
wxCursor load_resource_cursor(const String& name) {
|
||||
#if defined(__WXMSW__)
|
||||
return wxCursor(_("cursor/") + name, wxBITMAP_TYPE_CUR_RESOURCE);
|
||||
#else
|
||||
return wxCursor(load_resource_image(_("cursor/") + name));
|
||||
#endif
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Platform look
|
||||
|
||||
// Draw a basic 3D border
|
||||
|
||||
@@ -39,6 +39,9 @@ void draw_checker(RotatedDC& dc, const RealRect&);
|
||||
/// Load an image from a resource
|
||||
Image load_resource_image(const String& name);
|
||||
|
||||
/// Load a cursor from a resource
|
||||
wxCursor load_resource_cursor(const String& name);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Platform look
|
||||
|
||||
/// Draws a border for a control *around* a rect
|
||||
|
||||
@@ -259,7 +259,7 @@ wxCursor rotated_ibeam;
|
||||
wxCursor TextValueEditor::cursor() const {
|
||||
if (viewer.getRotation().sideways() ^ style().getRotation().sideways()) { // 90 or 270 degrees
|
||||
if (!rotated_ibeam.Ok()) {
|
||||
rotated_ibeam = wxCursor(load_resource_image(_("CUR_ROT_IBEAM")));
|
||||
rotated_ibeam = wxCursor(load_resource_cursor(_("rot_text")));
|
||||
}
|
||||
return rotated_ibeam;
|
||||
} else {
|
||||
|
||||
@@ -1645,6 +1645,9 @@
|
||||
<File
|
||||
RelativePath=".\util\order_cache.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\util\platform.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\util\prec.hpp">
|
||||
</File>
|
||||
@@ -2132,6 +2135,9 @@
|
||||
<File
|
||||
RelativePath=".\script\scriptable.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\script\to_value.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\script\value.cpp">
|
||||
</File>
|
||||
@@ -2630,6 +2636,9 @@
|
||||
<File
|
||||
RelativePath=".\code_template.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\conversion-todo.txt">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\data\en.mse-locale\locale">
|
||||
</File>
|
||||
|
||||
@@ -17,7 +17,7 @@ ICON_SYMBOL ICON "icon/symbol.ico"
|
||||
|
||||
// -------------------------------------------------------- : Toolbar
|
||||
|
||||
CUR_ROT_IBEAM CURSOR "cursor/rot_text.cur"
|
||||
cursor/rot_text CURSOR "cursor/rot_text.cur"
|
||||
|
||||
TOOL_NEW BITMAP "tool/file_new.bmp"
|
||||
TOOL_OPEN BITMAP "tool/file_open.bmp"
|
||||
@@ -52,13 +52,13 @@ TOOL_HELP BITMAP "tool/help.bmp"
|
||||
|
||||
// -------------------------------------------------------- : Symbol editor
|
||||
|
||||
CUR_POINT CURSOR "cursor/point.cur"
|
||||
CUR_POINT_ADD CURSOR "cursor/point_add.cur"
|
||||
CUR_POINT_MOVE CURSOR "cursor/point_move.cur"
|
||||
CUR_POINT_CURVE CURSOR "cursor/curve.cur"
|
||||
CUR_ROTATE CURSOR "cursor/rotate.cur"
|
||||
CUR_SHEAR_X CURSOR "cursor/shear_x.cur"
|
||||
CUR_SHEAR_Y CURSOR "cursor/shear_y.cur"
|
||||
cursor/point CURSOR "cursor/point.cur"
|
||||
cursor/point_add CURSOR "cursor/point_add.cur"
|
||||
cursor/point_move CURSOR "cursor/point_move.cur"
|
||||
cursor/curve CURSOR "cursor/curve.cur"
|
||||
cursor/rotate CURSOR "cursor/rotate.cur"
|
||||
cursor/shear_x CURSOR "cursor/shear_x.cur"
|
||||
cursor/shear_y CURSOR "cursor/shear_y.cur"
|
||||
|
||||
TOOL_LINE BITMAP "tool/line.bmp"
|
||||
TOOL_CURVE BITMAP "tool/curve.bmp"
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/context.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/context.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <queue>
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/value.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/dependency.hpp>
|
||||
#include <util/tagged_string.hpp>
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <script/image.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/dynamic_arg.hpp>
|
||||
#include <util/io/package.hpp>
|
||||
// for functions:
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <script/script.hpp>
|
||||
#include <script/parser.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <util/io/package_manager.hpp> // for "include file" semi hack
|
||||
#include <stack>
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <script/script.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/error.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Variables
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/script_manager.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <data/set.hpp>
|
||||
#include <data/stylesheet.hpp>
|
||||
#include <data/game.hpp>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <script/script.hpp>
|
||||
#include <script/context.hpp>
|
||||
#include <script/parser.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
||||
|
||||
@@ -86,9 +87,6 @@ class OptionalScript {
|
||||
template <typename T> friend class Scriptable;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v()); }
|
||||
|
||||
// ----------------------------------------------------------------------------- : StringScript
|
||||
|
||||
/// An optional script which is parsed in string mode
|
||||
|
||||
@@ -0,0 +1,292 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
#ifndef HEADER_SCRIPT_TO_VALUE
|
||||
#define HEADER_SCRIPT_TO_VALUE
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/value.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <util/io/get_member.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
// Iterator over a collection
|
||||
struct ScriptIterator : public ScriptValue {
|
||||
virtual ScriptType type() const;// { return SCRIPT_ITERATOR; }
|
||||
virtual String typeName() const;// { return "iterator"; }
|
||||
|
||||
/// Return the next item for this iterator, or ScriptValueP() if there is no such item
|
||||
virtual ScriptValueP next() = 0;
|
||||
};
|
||||
|
||||
// make an iterator over a range
|
||||
ScriptValueP rangeIterator(int start, int end);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Collections
|
||||
|
||||
// Iterator over a collection
|
||||
template <typename Collection>
|
||||
class ScriptCollectionIterator : public ScriptIterator {
|
||||
public:
|
||||
ScriptCollectionIterator(const Collection* col) : pos(0), col(col) {}
|
||||
virtual ScriptValueP next() {
|
||||
if (pos < col->size()) {
|
||||
return toScript(col->at(pos++));
|
||||
} else {
|
||||
return ScriptValueP();
|
||||
}
|
||||
}
|
||||
private:
|
||||
size_t pos;
|
||||
const Collection* col;
|
||||
};
|
||||
|
||||
/// Script value containing a collection
|
||||
template <typename Collection>
|
||||
class ScriptCollection : public ScriptValue {
|
||||
public:
|
||||
inline ScriptCollection(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
long index;
|
||||
if (name.ToLong(&index) && index >= 0 && (size_t)index < value->size()) {
|
||||
return toScript(value->at(index));
|
||||
} else {
|
||||
throw ScriptError(_("Collection has no member ") + name);
|
||||
}
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
return new_intrusive1<ScriptCollectionIterator<Collection> >(value);
|
||||
}
|
||||
virtual int itemCount() const { return (int)value->size(); }
|
||||
private:
|
||||
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script
|
||||
const Collection* value;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Collections : maps
|
||||
|
||||
template <typename V>
|
||||
ScriptValueP get_member(const map<String,V>& m, const String& name) {
|
||||
typename map<String,V>::const_iterator it = m.find(name);
|
||||
if (it != m.end()) {
|
||||
return toScript(it->second);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
ScriptValueP get_member(const IndexMap<K,V>& m, const String& name) {
|
||||
typename IndexMap<K,V>::const_iterator it = m.find(name);
|
||||
if (it != m.end()) {
|
||||
return toScript(*it);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
}
|
||||
}
|
||||
|
||||
/// Script value containing a map-like collection
|
||||
template <typename Collection>
|
||||
class ScriptMap : public ScriptValue {
|
||||
public:
|
||||
inline ScriptMap(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
return get_member(*value, name);
|
||||
}
|
||||
virtual int itemCount() const { return (int)value->size(); }
|
||||
private:
|
||||
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script
|
||||
const Collection* value;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Objects
|
||||
|
||||
/// Number of items in some collection like object, can be overloaded
|
||||
template <typename T>
|
||||
int item_count(const T& v) {
|
||||
return -1;
|
||||
}
|
||||
/// Return an iterator for some collection, can be overloaded
|
||||
template <typename T>
|
||||
ScriptValueP make_iterator(const T& v) {
|
||||
return ScriptValueP();
|
||||
}
|
||||
|
||||
/// Mark a dependency on a member of value, can be overloaded
|
||||
template <typename T>
|
||||
void mark_dependency_member(const T& value, const String& name, const Dependency& dep) {}
|
||||
|
||||
/// Script value containing an object (pointer)
|
||||
template <typename T>
|
||||
class ScriptObject : public ScriptValue {
|
||||
public:
|
||||
inline ScriptObject(const T& v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_OBJECT; }
|
||||
virtual String typeName() const { return _("object"); }
|
||||
virtual operator String() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator String(); }
|
||||
virtual operator double() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator double(); }
|
||||
virtual operator int() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator int(); }
|
||||
virtual operator Color() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator Color(); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
GetMember gm(name);
|
||||
gm.handle(*value);
|
||||
if (gm.result()) return gm.result();
|
||||
else {
|
||||
// try nameless member
|
||||
ScriptValueP d = getDefault();
|
||||
if (d) {
|
||||
return d->getMember(name);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("object has no member", name));
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual ScriptValueP dependencyMember(const String& name, const Dependency& dep) const {
|
||||
mark_dependency_member(value, name, dep);
|
||||
return getMember(name);
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
ScriptValueP it = make_iterator(*value);
|
||||
return it ? it : ScriptValue::makeIterator();
|
||||
}
|
||||
virtual int itemCount() const {
|
||||
int i = item_count(*value);
|
||||
return i >= 0 ? i : ScriptValue::itemCount();
|
||||
}
|
||||
/// Get access to the value
|
||||
inline T getValue() const { return value; }
|
||||
private:
|
||||
T value; ///< The object
|
||||
ScriptValueP getDefault() const {
|
||||
GetDefaultMember gdm;
|
||||
gdm.handle(*value);
|
||||
return gdm.result();
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Creating
|
||||
|
||||
/// Convert a value to a script value
|
||||
ScriptValueP toScript(int v);
|
||||
inline ScriptValueP toScript(long v) { return toScript((int) v); }
|
||||
ScriptValueP toScript(double v);
|
||||
ScriptValueP toScript(const String& v);
|
||||
ScriptValueP toScript(const Color& v);
|
||||
inline ScriptValueP toScript(bool v) { return v ? script_true : script_false; }
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); }
|
||||
template <typename K, typename V>
|
||||
inline ScriptValueP toScript(const map<K,V>* v) { return new_intrusive1<ScriptMap<map<K,V> > >(v); }
|
||||
template <typename K, typename V>
|
||||
inline ScriptValueP toScript(const IndexMap<K,V>* v) { return new_intrusive1<ScriptMap<IndexMap<K,V> > >(v); }
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<shared_ptr<T> > >(v); }
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const Defaultable<T>& v) { return toScript(v()); }
|
||||
|
||||
// ----------------------------------------------------------------------------- : Buildin functions
|
||||
|
||||
/// Macro to declare a new script function
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* // function code goes here
|
||||
* }
|
||||
* @endcode
|
||||
* This declares a value 'script_my_function' which can be added as a variable to a context
|
||||
* using:
|
||||
* @code
|
||||
* extern ScriptValueP script_my_function;
|
||||
* context.setVariable("my_function", script_my_function);
|
||||
* @endcode
|
||||
*/
|
||||
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
|
||||
|
||||
/// Macro to declare a new script function with custom dependency handling
|
||||
#define SCRIPT_FUNCTION_DEP(name) SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
|
||||
|
||||
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
|
||||
#define SCRIPT_FUNCTION_AUX(name,dep) \
|
||||
class ScriptBuildin_##name : public ScriptValue { \
|
||||
dep \
|
||||
virtual ScriptType type() const \
|
||||
{ return SCRIPT_FUNCTION; } \
|
||||
virtual String typeName() const \
|
||||
{ return _("build in function '") _(#name) _("'"); } \
|
||||
virtual ScriptValueP eval(Context&) const; \
|
||||
}; \
|
||||
ScriptValueP script_##name(new ScriptBuildin_##name); \
|
||||
ScriptValueP ScriptBuildin_##name::eval(Context& ctx) const
|
||||
|
||||
/// Retrieve a parameter to a SCRIPT_FUNCTION with the given name and type
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* SCRIPT_PARAM(String, my_string_param);
|
||||
* ... my_string_param ...
|
||||
* }
|
||||
* @endcode
|
||||
* Throws an error if the parameter is not found.
|
||||
*/
|
||||
#define SCRIPT_PARAM(Type, name) \
|
||||
Type name = getParam<Type>(ctx.getVariable(_(#name)))
|
||||
|
||||
template <typename T>
|
||||
inline T getParam (const ScriptValueP& value) {
|
||||
ScriptObject<T>* o = dynamic_cast<ScriptObject<T>*>(value.get());
|
||||
if (!o) throw ScriptError(_("Can't convert from ")+value->typeName()+_(" to object"));
|
||||
return o->getValue();
|
||||
}
|
||||
template <> inline ScriptValueP getParam<ScriptValueP>(const ScriptValueP& value) { return value; }
|
||||
template <> inline String getParam<String> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline int getParam<int> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline double getParam<double> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline bool getParam<bool> (const ScriptValueP& value) { return (int)*value; }
|
||||
|
||||
/// Retrieve an optional parameter
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* SCRIPT_OPTIONAL_PARAM(String, my_string_param) {
|
||||
* ... my_string_param ...
|
||||
* }
|
||||
* ...
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
#define SCRIPT_OPTIONAL_PARAM(Type, name) SCRIPT_OPTIONAL_PARAM_N(Type, _(#name), name)
|
||||
|
||||
#define SCRIPT_OPTIONAL_PARAM_N(Type, str, name) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : Type(); \
|
||||
if (name##_)
|
||||
|
||||
/// Retrieve an optional parameter, can't be used as an if statement
|
||||
#define SCRIPT_OPTIONAL_PARAM_(Type, name) SCRIPT_OPTIONAL_PARAM_N_(Type, _(#name), name)
|
||||
|
||||
#define SCRIPT_OPTIONAL_PARAM_N_(Type, str, name) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : Type();
|
||||
|
||||
/// Retrieve an optional parameter with a default value
|
||||
#define SCRIPT_PARAM_DEFAULT(Type, name, def) SCRIPT_PARAM_DEFAULT_N(Type, _(#name), name, def)
|
||||
/// Retrieve an optional parameter with a default value
|
||||
#define SCRIPT_PARAM_DEFAULT_N(Type, str, name, def) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : def
|
||||
|
||||
/// Return a value from a SCRIPT_FUNCTION
|
||||
#define SCRIPT_RETURN(value) return toScript(value)
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
@@ -7,6 +7,7 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <script/value.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <boost/pool/singleton_pool.hpp>
|
||||
|
||||
|
||||
@@ -10,9 +10,6 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <util/error.hpp>
|
||||
#include <util/io/get_member.hpp>
|
||||
class Context;
|
||||
class Dependency;
|
||||
|
||||
@@ -84,278 +81,5 @@ extern ScriptValueP script_true; ///< The preallocated true value
|
||||
extern ScriptValueP script_false; ///< The preallocated false value
|
||||
extern ScriptValueP dependency_dummy; ///< Dummy value used during dependency analysis
|
||||
|
||||
// ----------------------------------------------------------------------------- : Iterators
|
||||
|
||||
// Iterator over a collection
|
||||
struct ScriptIterator : public ScriptValue {
|
||||
virtual ScriptType type() const;// { return SCRIPT_ITERATOR; }
|
||||
virtual String typeName() const;// { return "iterator"; }
|
||||
|
||||
/// Return the next item for this iterator, or ScriptValueP() if there is no such item
|
||||
virtual ScriptValueP next() = 0;
|
||||
};
|
||||
|
||||
// make an iterator over a range
|
||||
ScriptValueP rangeIterator(int start, int end);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Collections
|
||||
|
||||
// Iterator over a collection
|
||||
template <typename Collection>
|
||||
class ScriptCollectionIterator : public ScriptIterator {
|
||||
public:
|
||||
ScriptCollectionIterator(const Collection* col) : pos(0), col(col) {}
|
||||
virtual ScriptValueP next() {
|
||||
if (pos < col->size()) {
|
||||
return toScript(col->at(pos++));
|
||||
} else {
|
||||
return ScriptValueP();
|
||||
}
|
||||
}
|
||||
private:
|
||||
size_t pos;
|
||||
const Collection* col;
|
||||
};
|
||||
|
||||
/// Script value containing a collection
|
||||
template <typename Collection>
|
||||
class ScriptCollection : public ScriptValue {
|
||||
public:
|
||||
inline ScriptCollection(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
long index;
|
||||
if (name.ToLong(&index) && index >= 0 && (size_t)index < value->size()) {
|
||||
return toScript(value->at(index));
|
||||
} else {
|
||||
throw ScriptError(_("Collection has no member ") + name);
|
||||
}
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
return new_intrusive1<ScriptCollectionIterator<Collection> >(value);
|
||||
}
|
||||
virtual int itemCount() const { return (int)value->size(); }
|
||||
private:
|
||||
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script
|
||||
const Collection* value;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Collections : maps
|
||||
|
||||
template <typename V>
|
||||
ScriptValueP get_member(const map<String,V>& m, const String& name) {
|
||||
typename map<String,V>::const_iterator it = m.find(name);
|
||||
if (it != m.end()) {
|
||||
return toScript(it->second);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
ScriptValueP get_member(const IndexMap<K,V>& m, const String& name) {
|
||||
typename IndexMap<K,V>::const_iterator it = m.find(name);
|
||||
if (it != m.end()) {
|
||||
return toScript(*it);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("collection has no member", name));
|
||||
}
|
||||
}
|
||||
|
||||
/// Script value containing a map-like collection
|
||||
template <typename Collection>
|
||||
class ScriptMap : public ScriptValue {
|
||||
public:
|
||||
inline ScriptMap(const Collection* v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_COLLECTION; }
|
||||
virtual String typeName() const { return _("collection"); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
return get_member(*value, name);
|
||||
}
|
||||
virtual int itemCount() const { return (int)value->size(); }
|
||||
private:
|
||||
/// Store a pointer to a collection, collections are only ever used for structures owned outside the script
|
||||
const Collection* value;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Objects
|
||||
|
||||
/// Number of items in some collection like object, can be overloaded
|
||||
template <typename T>
|
||||
int item_count(const T& v) {
|
||||
return -1;
|
||||
}
|
||||
/// Return an iterator for some collection, can be overloaded
|
||||
template <typename T>
|
||||
ScriptValueP make_iterator(const T& v) {
|
||||
return ScriptValueP();
|
||||
}
|
||||
|
||||
/// Mark a dependency on a member of value, can be overloaded
|
||||
template <typename T>
|
||||
void mark_dependency_member(const T& value, const String& name, const Dependency& dep) {}
|
||||
|
||||
/// Script value containing an object (pointer)
|
||||
template <typename T>
|
||||
class ScriptObject : public ScriptValue {
|
||||
public:
|
||||
inline ScriptObject(const T& v) : value(v) {}
|
||||
virtual ScriptType type() const { return SCRIPT_OBJECT; }
|
||||
virtual String typeName() const { return _("object"); }
|
||||
virtual operator String() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator String(); }
|
||||
virtual operator double() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator double(); }
|
||||
virtual operator int() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator int(); }
|
||||
virtual operator Color() const { ScriptValueP d = getDefault(); return d ? *d : ScriptValue::operator Color(); }
|
||||
virtual ScriptValueP getMember(const String& name) const {
|
||||
GetMember gm(name);
|
||||
gm.handle(*value);
|
||||
if (gm.result()) return gm.result();
|
||||
else {
|
||||
// try nameless member
|
||||
ScriptValueP d = getDefault();
|
||||
if (d) {
|
||||
return d->getMember(name);
|
||||
} else {
|
||||
throw ScriptError(_ERROR_1_("object has no member", name));
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual ScriptValueP dependencyMember(const String& name, const Dependency& dep) const {
|
||||
mark_dependency_member(value, name, dep);
|
||||
return getMember(name);
|
||||
}
|
||||
virtual ScriptValueP makeIterator() const {
|
||||
ScriptValueP it = make_iterator(*value);
|
||||
return it ? it : ScriptValue::makeIterator();
|
||||
}
|
||||
virtual int itemCount() const {
|
||||
int i = item_count(*value);
|
||||
return i >= 0 ? i : ScriptValue::itemCount();
|
||||
}
|
||||
/// Get access to the value
|
||||
inline T getValue() const { return value; }
|
||||
private:
|
||||
T value; ///< The object
|
||||
ScriptValueP getDefault() const {
|
||||
GetDefaultMember gdm;
|
||||
gdm.handle(*value);
|
||||
return gdm.result();
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Creating
|
||||
|
||||
/// Convert a value to a script value
|
||||
ScriptValueP toScript(int v);
|
||||
inline ScriptValueP toScript(long v) { return toScript((int) v); }
|
||||
ScriptValueP toScript(double v);
|
||||
ScriptValueP toScript(const String& v);
|
||||
ScriptValueP toScript(const Color& v);
|
||||
inline ScriptValueP toScript(bool v) { return v ? script_true : script_false; }
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const vector<T>* v) { return new_intrusive1<ScriptCollection<vector<T> > >(v); }
|
||||
template <typename K, typename V>
|
||||
inline ScriptValueP toScript(const map<K,V>* v) { return new_intrusive1<ScriptMap<map<K,V> > >(v); }
|
||||
template <typename K, typename V>
|
||||
inline ScriptValueP toScript(const IndexMap<K,V>* v) { return new_intrusive1<ScriptMap<IndexMap<K,V> > >(v); }
|
||||
template <typename T>
|
||||
inline ScriptValueP toScript(const shared_ptr<T>& v) { return new_intrusive1<ScriptObject<shared_ptr<T> > >(v); }
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Buildin functions
|
||||
|
||||
/// Macro to declare a new script function
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* // function code goes here
|
||||
* }
|
||||
* @endcode
|
||||
* This declares a value 'script_my_function' which can be added as a variable to a context
|
||||
* using:
|
||||
* @code
|
||||
* extern ScriptValueP script_my_function;
|
||||
* context.setVariable("my_function", script_my_function);
|
||||
* @endcode
|
||||
*/
|
||||
#define SCRIPT_FUNCTION(name) SCRIPT_FUNCTION_AUX(name,;)
|
||||
|
||||
/// Macro to declare a new script function with custom dependency handling
|
||||
#define SCRIPT_FUNCTION_DEP(name) SCRIPT_FUNCTION_AUX(name, virtual ScriptValueP dependencies(Context&, const Dependency&) const;)
|
||||
|
||||
// helper for SCRIPT_FUNCTION and SCRIPT_FUNCTION_DEP
|
||||
#define SCRIPT_FUNCTION_AUX(name,dep) \
|
||||
class ScriptBuildin_##name : public ScriptValue { \
|
||||
dep \
|
||||
virtual ScriptType type() const \
|
||||
{ return SCRIPT_FUNCTION; } \
|
||||
virtual String typeName() const \
|
||||
{ return _("build in function '") _(#name) _("'"); } \
|
||||
virtual ScriptValueP eval(Context&) const; \
|
||||
}; \
|
||||
ScriptValueP script_##name(new ScriptBuildin_##name); \
|
||||
ScriptValueP ScriptBuildin_##name::eval(Context& ctx) const
|
||||
|
||||
/// Retrieve a parameter to a SCRIPT_FUNCTION with the given name and type
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* SCRIPT_PARAM(String, my_string_param);
|
||||
* ... my_string_param ...
|
||||
* }
|
||||
* @endcode
|
||||
* Throws an error if the parameter is not found.
|
||||
*/
|
||||
#define SCRIPT_PARAM(Type, name) \
|
||||
Type name = getParam<Type>(ctx.getVariable(_(#name)))
|
||||
|
||||
template <typename T>
|
||||
inline T getParam (const ScriptValueP& value) {
|
||||
ScriptObject<T>* o = dynamic_cast<ScriptObject<T>*>(value.get());
|
||||
if (!o) throw ScriptError(_("Can't convert from ")+value->typeName()+_(" to object"));
|
||||
return o->getValue();
|
||||
}
|
||||
template <> inline ScriptValueP getParam<ScriptValueP>(const ScriptValueP& value) { return value; }
|
||||
template <> inline String getParam<String> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline int getParam<int> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline double getParam<double> (const ScriptValueP& value) { return *value; }
|
||||
template <> inline bool getParam<bool> (const ScriptValueP& value) { return (int)*value; }
|
||||
|
||||
/// Retrieve an optional parameter
|
||||
/** Usage:
|
||||
* @code
|
||||
* SCRIPT_FUNCTION(my_function) {
|
||||
* SCRIPT_OPTIONAL_PARAM(String, my_string_param) {
|
||||
* ... my_string_param ...
|
||||
* }
|
||||
* ...
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
#define SCRIPT_OPTIONAL_PARAM(Type, name) SCRIPT_OPTIONAL_PARAM_N(Type, _(#name), name)
|
||||
|
||||
#define SCRIPT_OPTIONAL_PARAM_N(Type, str, name) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : Type(); \
|
||||
if (name##_)
|
||||
|
||||
/// Retrieve an optional parameter, can't be used as an if statement
|
||||
#define SCRIPT_OPTIONAL_PARAM_(Type, name) SCRIPT_OPTIONAL_PARAM_N_(Type, _(#name), name)
|
||||
|
||||
#define SCRIPT_OPTIONAL_PARAM_N_(Type, str, name) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : Type();
|
||||
|
||||
/// Retrieve an optional parameter with a default value
|
||||
#define SCRIPT_PARAM_DEFAULT(Type, name, def) SCRIPT_PARAM_DEFAULT_N(Type, _(#name), name, def)
|
||||
/// Retrieve an optional parameter with a default value
|
||||
#define SCRIPT_PARAM_DEFAULT_N(Type, str, name, def) \
|
||||
ScriptValueP name##_ = ctx.getVariableOpt(str); \
|
||||
Type name = name##_ ? getParam<Type>(name##_) : def
|
||||
|
||||
/// Return a value from a SCRIPT_FUNCTION
|
||||
#define SCRIPT_RETURN(value) return toScript(value)
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
#include <util/io/get_member.hpp>
|
||||
#include <util/vector2d.hpp>
|
||||
#include <script/value.hpp>
|
||||
#include <script/script.hpp>
|
||||
#include <script/to_value.hpp>
|
||||
|
||||
// ---------------------------------------------------------------------------- : GetDefaultMember
|
||||
|
||||
|
||||
@@ -10,11 +10,9 @@
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <script/value.hpp>
|
||||
|
||||
class Script;
|
||||
class ScriptValue;
|
||||
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(ScriptValue);
|
||||
DECLARE_INTRUSIVE_POINTER_TYPE(Script);
|
||||
|
||||
template <typename T> class Defaultable;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
//+----------------------------------------------------------------------------+
|
||||
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
|
||||
//| Copyright: (C) 2001 - 2006 Twan van Laarhoven |
|
||||
//| License: GNU General Public License 2 or later (see file COPYING) |
|
||||
//+----------------------------------------------------------------------------+
|
||||
|
||||
#ifndef HEADER_UTIL_PREC
|
||||
#define HEADER_UTIL_PREC
|
||||
|
||||
/** @file util/platform.hpp
|
||||
*
|
||||
* @brief Platform specific hacks
|
||||
*/
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include <util/prec.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Windows
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------- : Linux
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
inline void wxMkDir(const String& str) {
|
||||
wxMkDir(wxConvLocal.cWX2MB(dir), 0777);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------- : EOF
|
||||
#endif
|
||||
@@ -14,9 +14,9 @@
|
||||
|
||||
// ----------------------------------------------------------------------------- : Includes
|
||||
|
||||
#include "io/reader.hpp"
|
||||
#include "io/writer.hpp"
|
||||
#include "io/get_member.hpp"
|
||||
#include <util/io/reader.hpp>
|
||||
#include <util/io/writer.hpp>
|
||||
#include <util/io/get_member.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Declaring reflection
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
#include <util/version.hpp>
|
||||
#include <util/reflect.hpp>
|
||||
#include <script/value.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Version
|
||||
|
||||
|
||||
Reference in New Issue
Block a user