BGE Python API cleanup - no functionality changes
[blender.git] / source / gameengine / Ketsji / KX_CDActuator.cpp
1 /**
2  * KX_CDActuator.cpp
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  *
31  */
32
33 #include "KX_CDActuator.h"
34 #include "SND_CDObject.h"
35 #include "KX_GameObject.h"
36 #include "SND_Scene.h" // needed for replication
37 #include <iostream>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46 KX_CDActuator::KX_CDActuator(SCA_IObject* gameobject,
47                                                          SND_Scene* soundscene,
48                                                          KX_CDACT_TYPE type,
49                                                          int track,
50                                                          short start,
51                                                          short end,
52                                                          PyTypeObject* T)
53                                                          : SCA_IActuator(gameobject,T)
54 {
55         m_soundscene = soundscene;
56         m_type = type;
57         m_track = track;
58         m_lastEvent = true;
59         m_isplaying = false;
60         m_startFrame = start;
61         m_endFrame = end;
62         m_gain = SND_CDObject::Instance()->GetGain();
63 }
64
65
66
67 KX_CDActuator::~KX_CDActuator()
68 {
69 }
70
71
72 /* hmmm, do we want this? */
73 CValue* KX_CDActuator::GetReplica()
74 {
75         KX_CDActuator* replica = new KX_CDActuator(*this);
76         replica->ProcessReplica();
77         
78         // this will copy properties and so on...
79         CValue::AddDataToReplica(replica);
80         return replica;
81 };
82
83
84
85 bool KX_CDActuator::Update()
86 {
87         bool result = false;
88         bool bNegativeEvent = IsNegativeEvent();
89         
90         RemoveAllEvents();
91         
92         if (!bNegativeEvent)
93         {
94                 switch (m_type)
95                 {
96                 case KX_CDACT_PLAY_ALL:
97                         {
98                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
99                                 SND_CDObject::Instance()->SetTrack(1);
100                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
101                                 //result = true;
102                                 break;
103                         }
104                 case KX_CDACT_PLAY_TRACK:
105                         {
106                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_TRACK);
107                                 SND_CDObject::Instance()->SetTrack(m_track);
108                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
109                                 //result = true;
110                                 break;
111                         }
112                 case KX_CDACT_LOOP_TRACK:
113                         {
114                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
115                                 SND_CDObject::Instance()->SetTrack(m_track);
116                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
117                                 //result = true;
118                                 break;
119                         }
120                 case KX_CDACT_STOP:
121                         {
122                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
123                                 break;
124                         }
125                 case KX_CDACT_PAUSE:
126                         {
127                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
128                                 //result = true;
129                                 break;
130                         }
131                 case KX_CDACT_RESUME:
132                         {
133                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_RESUME);
134                                 //result = true;
135                                 break;
136                         }
137                 case KX_CDACT_VOLUME:
138                         {
139                                 SND_CDObject::Instance()->SetGain(m_gain);
140                                 //result = true;
141                                 break;
142                         }
143                 default:
144                         // implement me !!
145                         break;
146                 }
147         }
148         return result;
149 }
150
151
152
153 /* ------------------------------------------------------------------------- */
154 /* Python functions                                                          */
155 /* ------------------------------------------------------------------------- */
156
157
158
159 /* Integration hooks ------------------------------------------------------- */
160 PyTypeObject KX_CDActuator::Type = {
161         PyObject_HEAD_INIT(NULL)
162                 0,
163                 "KX_SoundActuator",
164                 sizeof(PyObjectPlus_Proxy),
165                 0,
166                 py_base_dealloc,
167                 0,
168                 0,
169                 0,
170                 0,
171                 py_base_repr,
172                 0,0,0,0,0,0,
173                 py_base_getattro,
174                 py_base_setattro,
175                 0,0,0,0,0,0,0,0,0,
176                 Methods
177 };
178
179
180
181 PyParentObject KX_CDActuator::Parents[] = {
182         &KX_CDActuator::Type,
183                 &SCA_IActuator::Type,
184                 &SCA_ILogicBrick::Type,
185                 &CValue::Type,
186                 NULL
187 };
188
189
190
191 PyMethodDef KX_CDActuator::Methods[] = {
192         // Deprecated ----->
193         {"setGain",(PyCFunction) KX_CDActuator::sPySetGain,METH_VARARGS,NULL},
194         {"getGain",(PyCFunction) KX_CDActuator::sPyGetGain,METH_VARARGS,NULL},
195         // <-----
196         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, startCD),
197         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, pauseCD),
198         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, resumeCD),
199         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, stopCD),
200         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, playAll),
201         KX_PYMETHODTABLE_O(KX_CDActuator, playTrack),
202         {NULL,NULL,NULL,NULL} //Sentinel
203 };
204
205 PyAttributeDef KX_CDActuator::Attributes[] = {
206         KX_PYATTRIBUTE_FLOAT_RW_CHECK("volume", 0.0, 1.0, KX_CDActuator, m_gain,pyattr_setGain),
207         KX_PYATTRIBUTE_INT_RW("track", 1, 99, false, KX_CDActuator, m_track),
208         { NULL }        //Sentinel
209 };
210
211 int KX_CDActuator::pyattr_setGain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
212 {
213         KX_CDActuator* act = static_cast<KX_CDActuator*>(self);
214         SND_CDObject::Instance()->SetGain(act->m_gain);
215         return 0;
216 }
217
218 PyObject* KX_CDActuator::py_getattro(PyObject *attr)
219 {
220         py_getattro_up(SCA_IActuator);
221 }
222
223 int KX_CDActuator::py_setattro(PyObject *attr, PyObject *value)
224 {
225         py_setattro_up(SCA_IActuator);
226 }
227
228
229
230 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, startCD,
231 "startCD()\n"
232 "\tStarts the CD playing.\n")
233 {
234         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
235         Py_RETURN_NONE;
236 }         
237
238
239 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, pauseCD,
240 "pauseCD()\n"
241 "\tPauses the CD playing.\n")
242 {
243         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
244         Py_RETURN_NONE;
245
246
247
248 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, resumeCD,
249 "resumeCD()\n"
250 "\tResumes the CD playing.\n")
251 {
252         SND_CDObject::Instance()->SetPlaystate(SND_MUST_RESUME);
253         Py_RETURN_NONE;
254
255
256
257 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, stopCD,
258 "stopCD()\n"
259 "\tStops the CD playing.\n")
260 {
261         SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
262         Py_RETURN_NONE;
263 }
264
265
266 KX_PYMETHODDEF_DOC_O(KX_CDActuator, playTrack,
267 "playTrack(trackNumber)\n"
268 "\tPlays the track selected.\n")
269 {
270         if (PyInt_Check(value)) {
271                 int track = PyInt_AsLong(value);
272                 SND_CDObject::Instance()->SetPlaymode(SND_CD_TRACK);
273                 SND_CDObject::Instance()->SetTrack(track);
274                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
275         }
276         Py_RETURN_NONE;
277 }
278
279
280
281 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, playAll,
282 "playAll()\n"
283 "\tPlays the CD from the beginning.\n")
284 {
285         SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
286         SND_CDObject::Instance()->SetTrack(1);
287         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
288         Py_RETURN_NONE;
289 }     
290
291 // Deprecated ----->
292 PyObject* KX_CDActuator::PySetGain(PyObject* self, PyObject* args, PyObject* kwds)
293 {
294         float gain = 1.0;
295         ShowDeprecationWarning("setGain()", "the volume property");
296         if (!PyArg_ParseTuple(args, "f:setGain", &gain))
297                 return NULL;
298         
299         SND_CDObject::Instance()->SetGain(gain);
300         
301         Py_RETURN_NONE;
302 }         
303
304
305
306 PyObject* KX_CDActuator::PyGetGain(PyObject* self, PyObject* args, PyObject* kwds)
307 {
308         float gain = SND_CDObject::Instance()->GetGain();
309         ShowDeprecationWarning("getGain()", "the volume property");
310         PyObject* result = PyFloat_FromDouble(gain);
311         
312         return result;
313 }
314 // <-----