OSDN Git Service

[dennco] changed the application icon (for windows)
[dennco/dennco.git] / Source / layer3 / QtScript / dnqsbasiccell.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on 2/22/2012.
18 //
19 #include "dnqsbasiccell.h"
20
21 #include "TKContainer.h"
22 #include "dnqscontainer.h"
23 #include "TKLog.h"
24 #include "TKReceptor.h"
25 #include "TKAxon.h"
26 #include "TKCellCodeInstance.h"
27 #include "DNUtils.h"
28
29 DNQSBasicCell::DNQSBasicCell(DNQSContainer *container, std::string location, std::string name, bool canInterface):
30     DNQSCellBase(container,location,name,canInterface)
31 {
32     mEngine = container->getScriptEngine();
33
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);
37
38     mQSReceptors = mEngine->newObject();
39     mQSAPIInstance.setProperty("receptors", mQSReceptors,QScriptValue::ReadOnly|QScriptValue::Undeletable);
40
41     mAxonValueHandle = mEngine->toStringHandle("axonValue");
42 }
43
44 DNQSBasicCell::~DNQSBasicCell()
45 {
46
47 }
48
49 //TODO: find better way
50 void DNQSBasicCell::prepareValueForScript()
51 {
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);
55     }
56     //update axonValue
57     mQSAPIInstance.setProperty(mAxonValueHandle,QScriptValue(mAxon->getValue()));
58 }
59
60 //TODO: find better way
61 void DNQSBasicCell::reflectValueFromScript()
62 {
63     QScriptValue value = mQSAPIInstance.property(mAxonValueHandle);
64     mAxon->setValue(value.toNumber());
65 }
66
67 void DNQSBasicCell::handleScriptException(const char *funcName)
68 {
69     std::string message = "Script Error! in ";
70     message += mLocation;
71     message += "#";
72     message += funcName;
73     if (mEngine->hasUncaughtException())
74     {
75         QScriptValue error = mEngine->uncaughtException();
76         QString errorString = error.toString();
77         message += "\n";
78         message += errorString.toStdString();
79         mEngine->clearExceptions();
80     }
81     dnNotifyWarning("Script error", message);
82 }
83
84 bool DNQSBasicCell::doTick(float time)
85 {
86     if (!mCellCodeInstance) return false;
87
88     prepareValueForScript();
89
90     if (mCellCodeInstance->doTick(time))
91     {
92         reflectValueFromScript();
93         return true;
94     }
95     else
96     {
97         handleScriptException("doTick()");
98         return false;
99     }
100 }
101
102 bool DNQSBasicCell::doInit()
103 {
104     if (!mCellCodeInstance) return false;
105
106     prepareValueForScript();
107
108     if (mCellCodeInstance->doInit())
109     {
110         reflectValueFromScript();
111         return true;
112     }
113     else
114     {
115         handleScriptException("doInit()");
116         return false;
117     }
118 }
119
120 bool DNQSBasicCell::doDestroy()
121 {
122     if (!mCellCodeInstance) return false;
123
124     prepareValueForScript();
125
126     if (mCellCodeInstance->doDestroy())
127     {
128         reflectValueFromScript();
129         return true;
130     }
131     else
132     {
133         handleScriptException("doDestroy()");
134         return false;
135     }
136 }