script.C

Go to the documentation of this file.
00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00026 #include <archon/x3d/server/field_type.H>
00027 #include <archon/x3d/server/field.H>
00028 #include <archon/x3d/server/server.H>
00029 #include <archon/x3d/server/script.H>
00030 #include <archon/x3d/server/script_ecma.H>
00031 
00032 namespace Archon
00033 {
00034   namespace X3D
00035   {
00036     const NodeType *ScriptNode::type = 0;
00037     const NodeType *Script::type = 0;
00038 
00039     void initializeScriptComponent()
00040     {
00041       vector<const FieldBase *> fields;
00042 
00043 
00044       // ScriptNode
00045 
00046       ScriptNode::type =
00047         NodeType::newAbstract("ScriptNode", false, 0,
00048                               ChildNode::type);
00049 
00050 
00051       // Script
00052 
00053       fields.push_back(newPrivateField("directOutput", SFBool::type,
00054                                        &Script::directOutput));
00055       fields.push_back(newPrivateField("mustEvaluate", SFBool::type,
00056                                        &Script::mustEvaluate));
00057       Script::type =
00058         NodeType::newConcrete("Script", "children",
00059                               Script::instantiate, &fields,
00060                               ScriptNode::type, UrlObject::type);
00061       fields.clear();
00062     }
00063 
00064 
00065     Script::Script(BackRef<ExecutionContext> c):
00066       NodeBase(c), directOutput(false), mustEvaluate(false)
00067     {
00068     }
00069 
00070 
00075     void Script::update()
00076     {
00077       Ref<const Loader::Contents> c = getContents();
00078       if(!c) return;
00079       Mutex::Lock l(context->server->scenegraphMutex);
00080       if(engine) return;
00081       const Loader::ScriptContents *d =
00082         dynamic_cast<const Loader::ScriptContents *>(c.get());
00083       if(!d) ARCHON_THROW1(InternalException,
00084                            "Script::update: Got non-script "
00085                            "contents object");
00086       if(d->language == "ecmascript")
00087         engine = new ECMA::Engine(this, d->sourceCode, d->sourceName, d->sourceLine);
00088       else ARCHON_THROW1(InternalException,
00089                          "Script::update: Got unexpected language");
00090     }
00091 
00092     void Script::clear()
00093     {
00094       engine = 0;
00095     }
00096 
00101     void Script::onLoaded()
00102     {
00103       if(!isRealized()) return;
00104       update();
00105     }
00106 
00107     void Script::onRealized()
00108     {
00109       update();
00110     }
00111 
00112     void Script::handleEvent(int fieldIndex, Time timestamp)
00113     {
00114       if(engine) engine->handleEvent(fieldIndex, timestamp);
00115     }
00116 
00117     void Script::handleCDATA(string s, Time stamp)
00118     {
00119       // Default to ECMA Script
00120       if(!Text::isPrefixOf("ecmascript:", s, true) &&
00121          !Text::isPrefixOf("javascript:", s, true))
00122         s = "ecmascript:" + s;
00123       addUri(s, stamp);
00124     }
00125   }
00126 }

Generated on Sun Jul 30 22:55:50 2006 for Archon by  doxygen 1.4.4