From 1ffaa5bd58e9f3dc2d92e689f06ce6bfdf569151 Mon Sep 17 00:00:00 2001 From: twanvl Date: Tue, 31 Oct 2006 12:17:36 +0000 Subject: [PATCH] made a start with script functions git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@62 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/data/field.cpp | 4 +- src/gui/set/window.cpp | 9 +- src/gui/set/window.hpp | 2 + src/mse.vcproj | 5 +- src/render/card/viewer.cpp | 4 +- src/script/context.cpp | 25 +++- src/script/context.hpp | 4 +- src/script/functions.cpp | 258 ++++++++++++++++++++++++++++++++++ src/script/image.cpp | 10 +- src/script/script.cpp | 2 +- src/script/script_manager.cpp | 5 +- src/script/scriptable.hpp | 2 +- src/script/value.cpp | 2 + src/script/value.hpp | 34 ++++- src/util/error.cpp | 11 +- src/util/io/reader.cpp | 2 +- src/util/io/reader.hpp | 2 +- src/util/io/writer.cpp | 4 +- src/util/string.cpp | 11 +- src/util/string.hpp | 5 +- 20 files changed, 372 insertions(+), 29 deletions(-) create mode 100644 src/script/functions.cpp diff --git a/src/data/field.cpp b/src/data/field.cpp index 81826812..3f7c688b 100644 --- a/src/data/field.cpp +++ b/src/data/field.cpp @@ -76,8 +76,8 @@ shared_ptr read_new(Reader& reader) { Style::Style(const FieldP& field) : fieldP(field) , z_index(0) - , left(0), width (1) - , top (0), height(1) + , left(0), width (0) + , top (0), height(0) , visible(true) {} diff --git a/src/gui/set/window.cpp b/src/gui/set/window.cpp index f1477181..14a121aa 100644 --- a/src/gui/set/window.cpp +++ b/src/gui/set/window.cpp @@ -546,6 +546,12 @@ void SetWindow::onChildMenu(wxCommandEvent& ev) { current_panel->onCommand(ev.GetId()); } +void SetWindow::onIdle(wxIdleEvent& ev) { + // Stuff that must be done in the main thread + handle_pending_errors(); +// showUpdateDialog(this); +} + // ----------------------------------------------------------------------------- : Event table BEGIN_EVENT_TABLE(SetWindow, wxFrame) @@ -584,7 +590,6 @@ BEGIN_EVENT_TABLE(SetWindow, wxFrame) // EVT_FIND_REPLACE (wxID_ANY, SetWindow::onReplace) // EVT_FIND_REPLACE_ALL(wxID_ANY, SetWindow::onReplaceAll) EVT_CLOSE ( SetWindow::onClose) -// EVT_TIMER (wxID_ANY, SetWindow::onTick) -// EVT_IDLE ( SetWindow::onIdle) + EVT_IDLE ( SetWindow::onIdle) EVT_CARD_SELECT (wxID_ANY, SetWindow::onCardSelect) END_EVENT_TABLE () diff --git a/src/gui/set/window.hpp b/src/gui/set/window.hpp index ddb089ff..e7d31296 100644 --- a/src/gui/set/window.hpp +++ b/src/gui/set/window.hpp @@ -154,6 +154,8 @@ class SetWindow : public wxFrame, public SetView { // --------------------------------------------------- : Window events - other void onChildMenu (wxCommandEvent&); + + void onIdle (wxIdleEvent&); }; // ----------------------------------------------------------------------------- : EOF diff --git a/src/mse.vcproj b/src/mse.vcproj index e4cda294..96652ac7 100644 --- a/src/mse.vcproj +++ b/src/mse.vcproj @@ -178,7 +178,7 @@ Name="VCCustomBuildTool"/> + + & styles) { // create viewers viewers.clear(); FOR_EACH(s, styles) { - if (s->visible || s->visible.isScripted()) { + if ((s->visible || s->visible.isScripted()) && + (s->width || s->width .isScripted()) && + (s->height || s->height .isScripted())) { // no need to make a viewer for things that are always invisible viewers.push_back(makeViewer(s)); // REMOVEME //TODO //%%% diff --git a/src/script/context.cpp b/src/script/context.cpp index 6a7511c7..b932d802 100644 --- a/src/script/context.cpp +++ b/src/script/context.cpp @@ -183,7 +183,7 @@ ScriptValueP Context::getVariable(const String& name) { return value; } -ScriptValueP Context::getVariableOrNil(const String& name) { +ScriptValueP Context::getVariableOpt(const String& name) { return variables[stringToVariable(name)].value; } @@ -246,6 +246,25 @@ void instrUnary (UnaryInstructionType i, ScriptValueP& a) { } \ break +/// Composition of two functions +class ScriptCompose : public ScriptValue { + public: + ScriptCompose(ScriptValueP a, ScriptValueP b) : a(a), b(b) {} + + virtual ScriptType type() const { return SCRIPT_FUNCTION; } + virtual String typeName() const { return _("replace_rule"); } + virtual ScriptValueP eval(Context& ctx) const { + ctx.setVariable(_("input"), a->eval(ctx)); + return b->eval(ctx); + } + virtual ScriptValueP dependencies(Context& ctx, const Dependency& dep) const { + ctx.setVariable(_("input"), a->dependencies(ctx, dep)); + return b->dependencies(ctx, dep); + } + private: + ScriptValueP a,b; +}; + void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& b) { ScriptType at = a->type(), bt = b->type(); switch (i) { @@ -260,8 +279,8 @@ void instrBinary (BinaryInstructionType i, ScriptValueP& a, const ScriptValueP& a = b; } else if (bt == SCRIPT_NIL) { // a = a; - //} else if (a->likesFunction() && b->likesFunction()) { - // a = compose(a, b); + } else if (at == SCRIPT_FUNCTION && bt == SCRIPT_FUNCTION) { + a = new_intrusive2(a, b); } else if (at == SCRIPT_STRING || bt == SCRIPT_STRING) { a = toScript((String)*a + (String)*b); } else if (at == SCRIPT_DOUBLE || bt == SCRIPT_DOUBLE) { diff --git a/src/script/context.hpp b/src/script/context.hpp index af441bed..3520f14f 100644 --- a/src/script/context.hpp +++ b/src/script/context.hpp @@ -54,8 +54,8 @@ class Context { /// Get the value of a variable, throws if it not set ScriptValueP getVariable(const String& name); - /// Get the value of a variable, returns nil if it is not set - ScriptValueP getVariableOrNil(const String& name); + /// Get the value of a variable, returns ScriptValue() if it is not set + ScriptValueP getVariableOpt(const String& name); public:// public for FOR_EACH /// Record of a variable diff --git a/src/script/functions.cpp b/src/script/functions.cpp new file mode 100644 index 00000000..6e208f8e --- /dev/null +++ b/src/script/functions.cpp @@ -0,0 +1,258 @@ +//+----------------------------------------------------------------------------+ +//| 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) | +//+----------------------------------------------------------------------------+ + +// ----------------------------------------------------------------------------- : Includes + +#include