Tiny User Prefs tweaks. Also put the Save As Default button in header so it stays...
[blender-staging.git] / source / gameengine / Ketsji / KXNetwork / KX_NetworkMessageSensor.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Ketsji Logic Extenstion: Network Message Sensor generic implementation
29  */
30
31 #include "KX_NetworkMessageSensor.h"
32 #include "KX_NetworkEventManager.h"
33 #include "NG_NetworkMessage.h"
34 #include "NG_NetworkScene.h"
35 #include "NG_NetworkObject.h"
36 #include "SCA_IObject.h"        
37 #include "InputParser.h"
38 #include "ListValue.h"
39 #include "StringValue.h"
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #ifdef NAN_NET_DEBUG
46   #include <iostream>
47 #endif
48
49 KX_NetworkMessageSensor::KX_NetworkMessageSensor(
50         class KX_NetworkEventManager* eventmgr, // our eventmanager
51         class NG_NetworkScene *NetworkScene,    // our scene
52         SCA_IObject* gameobj,                                   // the sensor controlling object
53         const STR_String &subject
54 ) :
55     SCA_ISensor(gameobj,eventmgr),
56     m_Networkeventmgr(eventmgr),
57     m_NetworkScene(NetworkScene),
58     m_subject(subject),
59     m_frame_message_count (0),
60     m_BodyList(NULL),
61     m_SubjectList(NULL)
62 {
63         Init();
64 }
65
66 void KX_NetworkMessageSensor::Init()
67 {
68     m_IsUp = false;
69 }
70
71 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
72 {
73 }
74
75 CValue* KX_NetworkMessageSensor::GetReplica() {
76         // This is the standard sensor implementation of GetReplica
77         // There may be more network message sensor specific stuff to do here.
78         CValue* replica = new KX_NetworkMessageSensor(*this);
79
80         if (replica == NULL) return NULL;
81         replica->ProcessReplica();
82
83         return replica;
84 }
85
86 // Return true only for flank (UP and DOWN)
87 bool KX_NetworkMessageSensor::Evaluate()
88 {
89         bool result = false;
90         bool WasUp = m_IsUp;
91
92         m_IsUp = false;
93
94         if (m_BodyList) {
95                 m_BodyList->Release();
96                 m_BodyList = NULL;
97         }
98
99         if (m_SubjectList) {
100                 m_SubjectList->Release();
101                 m_SubjectList = NULL;
102         }
103
104         STR_String& toname=GetParent()->GetName();
105         STR_String& subject = this->m_subject;
106
107         vector<NG_NetworkMessage*> messages =
108                 m_NetworkScene->FindMessages(toname,"",subject,true);
109
110         m_frame_message_count = messages.size();
111
112         if (!messages.empty()) {
113 #ifdef NAN_NET_DEBUG
114                 printf("KX_NetworkMessageSensor found one or more messages\n");
115 #endif
116                 m_IsUp = true;
117                 m_BodyList = new CListValue();
118                 m_SubjectList = new CListValue();
119         }
120
121         vector<NG_NetworkMessage*>::iterator mesit;
122         for (mesit=messages.begin();mesit!=messages.end();mesit++)
123         {
124                 // save the body
125                 const STR_String& body = (*mesit)->GetMessageText();
126                 // save the subject
127                 const STR_String& messub = (*mesit)->GetSubject();
128 #ifdef NAN_NET_DEBUG
129                 if (body) {
130                         cout << "body [" << body << "]\n";
131                 }
132 #endif
133                 m_BodyList->Add(new CStringValue(body,"body"));
134                 // Store Subject
135                 m_SubjectList->Add(new CStringValue(messub,"subject"));
136
137                 // free the message
138                 (*mesit)->Release();
139         }
140         messages.clear();
141
142         result = (WasUp != m_IsUp);
143
144         // Return always true if a message was received otherwise we can loose messages
145         if (m_IsUp)
146                 return true;
147         // Is it usefull to return also true when the first frame without a message?? 
148         // This will cause a fast on/off cycle that seems useless!
149         return result;
150 }
151
152 // return true for being up (no flank needed)
153 bool KX_NetworkMessageSensor::IsPositiveTrigger()
154 {
155 //      printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
156         //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
157         //a better solution is to properly introduce separate Edge and Level triggering concept
158
159         return m_IsUp;
160 }
161
162 /* --------------------------------------------------------------------- */
163 /* Python interface ---------------------------------------------------- */
164 /* --------------------------------------------------------------------- */
165
166 /* Integration hooks --------------------------------------------------- */
167 PyTypeObject KX_NetworkMessageSensor::Type = {
168         PyVarObject_HEAD_INIT(NULL, 0)
169         "KX_NetworkMessageSensor",
170         sizeof(PyObjectPlus_Proxy),
171         0,
172         py_base_dealloc,
173         0,
174         0,
175         0,
176         0,
177         py_base_repr,
178         0,0,0,0,0,0,0,0,0,
179         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
180         0,0,0,0,0,0,0,
181         Methods,
182         0,
183         0,
184         &SCA_ISensor::Type,
185         0,0,0,0,0,0,
186         py_base_new
187 };
188
189 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
190         // Deprecated ----->
191         {"setSubjectFilterText", (PyCFunction)
192                 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_O,
193                 (const char *)SetSubjectFilterText_doc},
194         {"getFrameMessageCount", (PyCFunction)
195                 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_NOARGS,
196                 (const char *)GetFrameMessageCount_doc},
197         {"getBodies", (PyCFunction)
198                 KX_NetworkMessageSensor::sPyGetBodies, METH_NOARGS,
199                 (const char *)GetBodies_doc},
200         {"getSubject", (PyCFunction)
201                 KX_NetworkMessageSensor::sPyGetSubject, METH_NOARGS,
202                 (const char *)GetSubject_doc},
203         {"getSubjects", (PyCFunction)
204                 KX_NetworkMessageSensor::sPyGetSubjects, METH_NOARGS,
205                 (const char *)GetSubjects_doc},
206         // <-----
207         {NULL,NULL} //Sentinel
208 };
209
210 PyAttributeDef KX_NetworkMessageSensor::Attributes[] = {
211         KX_PYATTRIBUTE_STRING_RW("subject", 0, 100, false, KX_NetworkMessageSensor, m_subject),
212         KX_PYATTRIBUTE_INT_RO("frameMessageCount", KX_NetworkMessageSensor, m_frame_message_count),
213         KX_PYATTRIBUTE_RO_FUNCTION("bodies", KX_NetworkMessageSensor, pyattr_get_bodies),
214         KX_PYATTRIBUTE_RO_FUNCTION("subjects", KX_NetworkMessageSensor, pyattr_get_subjects),
215         { NULL }        //Sentinel
216 };
217
218 PyObject* KX_NetworkMessageSensor::pyattr_get_bodies(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
219 {
220         KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
221         if (self->m_BodyList) {
222                 return self->m_BodyList->GetProxy();
223         } else {
224                 return (new CListValue())->NewProxy(true);
225         }
226 }
227
228 PyObject* KX_NetworkMessageSensor::pyattr_get_subjects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
229 {
230         KX_NetworkMessageSensor *self = static_cast<KX_NetworkMessageSensor*>(self_v);
231         if (self->m_SubjectList) {
232                 return self->m_SubjectList->GetProxy();
233         } else {
234                 return (new CListValue())->NewProxy(true);
235         }
236 }
237
238 // Deprecated ----->
239 // 1. Set the message subject that this sensor listens for
240 const char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] = 
241 "\tsetSubjectFilterText(value)\n"
242 "\tChange the message subject text that this sensor is listening to.\n";
243
244 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(PyObject* value)
245 {
246         ShowDeprecationWarning("setSubjectFilterText()", "subject");
247         char* Subject = _PyUnicode_AsString(value);
248         if (Subject==NULL) {
249                 PyErr_SetString(PyExc_TypeError, "sensor.tsetSubjectFilterText(string): KX_NetworkMessageSensor, expected a string message");
250                 return NULL;
251         }
252         
253         m_subject = Subject;
254         Py_RETURN_NONE;
255 }
256
257 // 2. Get the number of messages received since the last frame
258 const char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
259 "\tgetFrameMessageCount()\n"
260 "\tGet the number of messages received since the last frame.\n";
261
262 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount()
263 {
264         ShowDeprecationWarning("getFrameMessageCount()", "frameMessageCount");
265         return PyLong_FromSsize_t(long(m_frame_message_count));
266 }
267
268 // 3. Get the message bodies
269 const char KX_NetworkMessageSensor::GetBodies_doc[] =
270 "\tgetBodies()\n"
271 "\tGet the list of message bodies.\n";
272
273 PyObject* KX_NetworkMessageSensor::PyGetBodies()
274 {
275         ShowDeprecationWarning("getBodies()", "bodies");
276         if (m_BodyList) {
277                 return m_BodyList->GetProxy();
278         } else {
279                 return (new CListValue())->NewProxy(true);
280         }
281 }
282
283 // 4. Get the message subject: field of the message sensor
284 const char KX_NetworkMessageSensor::GetSubject_doc[] =
285 "\tgetSubject()\n"
286 "\tGet the subject: field of the message sensor.\n";
287
288 PyObject* KX_NetworkMessageSensor::PyGetSubject()
289 {
290         ShowDeprecationWarning("getSubject()", "subject");
291         return PyUnicode_FromString(m_subject ? m_subject : "");
292 }
293
294 // 5. Get the message subjects
295 const char KX_NetworkMessageSensor::GetSubjects_doc[] =
296 "\tgetSubjects()\n"
297 "\tGet list of message subjects.\n";
298
299 PyObject* KX_NetworkMessageSensor::PyGetSubjects()
300 {
301         ShowDeprecationWarning("getSubjects()", "subjects");
302         if (m_SubjectList) {
303                 return m_SubjectList->GetProxy();
304         } else {
305                 return (new CListValue())->NewProxy(true);
306         }
307 }
308 // <----- Deprecated