1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on 2/22/2012.
19 #include "dnqsbasiccell.h"
21 #include "TKContainer.h"
22 #include "dnqscontainer.h"
24 #include "TKReceptor.h"
26 #include "TKCellCodeInstance.h"
29 DNQSBasicCell::DNQSBasicCell(DNQSContainer *container, std::string location, std::string name, bool canInterface):
30 DNQSCellBase(container,location,name,canInterface)
32 mEngine = container->getScriptEngine();
34 mQSAPIInstance = mEngine->newObject();
35 mQSAPIInstance.setProperty("location", QScriptValue(QString::fromStdString(location)),QScriptValue::ReadOnly|QScriptValue::Undeletable);
36 mQSAPIInstance.setProperty("name", QScriptValue(QString::fromStdString(name)),QScriptValue::ReadOnly|QScriptValue::Undeletable);
38 mQSReceptors = mEngine->newObject();
39 mQSAPIInstance.setProperty("receptors", mQSReceptors,QScriptValue::ReadOnly|QScriptValue::Undeletable);
41 mAxonValueHandle = mEngine->toStringHandle("axonValue");
44 DNQSBasicCell::~DNQSBasicCell()
49 //TODO: find better way
50 void DNQSBasicCell::prepareValueForScript()
52 //update receptor values
53 for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
54 mQSReceptors.setProperty(QString::fromStdString(it->first), QScriptValue(it->second->getValue()),QScriptValue::ReadOnly);
57 mQSAPIInstance.setProperty(mAxonValueHandle,QScriptValue(mAxon->getValue()));
60 //TODO: find better way
61 void DNQSBasicCell::reflectValueFromScript()
63 QScriptValue value = mQSAPIInstance.property(mAxonValueHandle);
64 mAxon->setValue(value.toNumber());
67 void DNQSBasicCell::handleScriptException(const char *funcName)
69 std::string message = "Script Error! in ";
73 if (mEngine->hasUncaughtException())
75 QScriptValue error = mEngine->uncaughtException();
76 QString errorString = error.toString();
78 message += errorString.toStdString();
79 mEngine->clearExceptions();
81 dnNotifyWarning("Script error", message);
84 bool DNQSBasicCell::doTick(float time)
86 if (!mCellCodeInstance) return false;
88 prepareValueForScript();
90 if (mCellCodeInstance->doTick(time))
92 reflectValueFromScript();
97 handleScriptException("doTick()");
102 bool DNQSBasicCell::doInit()
104 if (!mCellCodeInstance) return false;
106 prepareValueForScript();
108 if (mCellCodeInstance->doInit())
110 reflectValueFromScript();
115 handleScriptException("doInit()");
120 bool DNQSBasicCell::doDestroy()
122 if (!mCellCodeInstance) return false;
124 prepareValueForScript();
126 if (mCellCodeInstance->doDestroy())
128 reflectValueFromScript();
133 handleScriptException("doDestroy()");