NLA SoC: Merge from 2.5
[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                                                          : SCA_IActuator(gameobject)
53 {
54         m_soundscene = soundscene;
55         m_type = type;
56         m_track = track;
57         m_lastEvent = true;
58         m_isplaying = false;
59         m_startFrame = start;
60         m_endFrame = end;
61         m_gain = SND_CDObject::Instance()->GetGain();
62 }
63
64
65
66 KX_CDActuator::~KX_CDActuator()
67 {
68 }
69
70
71 /* hmmm, do we want this? */
72 CValue* KX_CDActuator::GetReplica()
73 {
74         KX_CDActuator* replica = new KX_CDActuator(*this);
75         replica->ProcessReplica();
76         return replica;
77 };
78
79
80
81 bool KX_CDActuator::Update()
82 {
83         bool result = false;
84         bool bNegativeEvent = IsNegativeEvent();
85         
86         RemoveAllEvents();
87         
88         if (!bNegativeEvent)
89         {
90                 switch (m_type)
91                 {
92                 case KX_CDACT_PLAY_ALL:
93                         {
94                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
95                                 SND_CDObject::Instance()->SetTrack(1);
96                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
97                                 //result = true;
98                                 break;
99                         }
100                 case KX_CDACT_PLAY_TRACK:
101                         {
102                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_TRACK);
103                                 SND_CDObject::Instance()->SetTrack(m_track);
104                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
105                                 //result = true;
106                                 break;
107                         }
108                 case KX_CDACT_LOOP_TRACK:
109                         {
110                                 SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
111                                 SND_CDObject::Instance()->SetTrack(m_track);
112                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
113                                 //result = true;
114                                 break;
115                         }
116                 case KX_CDACT_STOP:
117                         {
118                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
119                                 break;
120                         }
121                 case KX_CDACT_PAUSE:
122                         {
123                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
124                                 //result = true;
125                                 break;
126                         }
127                 case KX_CDACT_RESUME:
128                         {
129                                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_RESUME);
130                                 //result = true;
131                                 break;
132                         }
133                 case KX_CDACT_VOLUME:
134                         {
135                                 SND_CDObject::Instance()->SetGain(m_gain);
136                                 //result = true;
137                                 break;
138                         }
139                 default:
140                         // implement me !!
141                         break;
142                 }
143         }
144         return result;
145 }
146
147
148
149 /* ------------------------------------------------------------------------- */
150 /* Python functions                                                          */
151 /* ------------------------------------------------------------------------- */
152
153
154
155 /* Integration hooks ------------------------------------------------------- */
156 PyTypeObject KX_CDActuator::Type = {
157 #if (PY_VERSION_HEX >= 0x02060000)
158         PyVarObject_HEAD_INIT(NULL, 0)
159 #else
160         /* python 2.5 and below */
161         PyObject_HEAD_INIT( NULL )  /* required py macro */
162         0,                          /* ob_size */
163 #endif
164                 "KX_SoundActuator",
165                 sizeof(PyObjectPlus_Proxy),
166                 0,
167                 py_base_dealloc,
168                 0,
169                 0,
170                 0,
171                 0,
172                 py_base_repr,
173                 0,0,0,0,0,0,0,0,0,
174                 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
175                 0,0,0,0,0,0,0,
176                 Methods,
177                 0,
178                 0,
179                 &SCA_IActuator::Type,
180                 0,0,0,0,0,0,
181                 py_base_new
182 };
183
184 PyMethodDef KX_CDActuator::Methods[] = {
185         // Deprecated ----->
186         {"setGain",(PyCFunction) KX_CDActuator::sPySetGain,METH_VARARGS,NULL},
187         {"getGain",(PyCFunction) KX_CDActuator::sPyGetGain,METH_VARARGS,NULL},
188         // <-----
189         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, startCD),
190         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, pauseCD),
191         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, resumeCD),
192         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, stopCD),
193         KX_PYMETHODTABLE_NOARGS(KX_CDActuator, playAll),
194         KX_PYMETHODTABLE_O(KX_CDActuator, playTrack),
195         {NULL,NULL,NULL,NULL} //Sentinel
196 };
197
198 PyAttributeDef KX_CDActuator::Attributes[] = {
199         KX_PYATTRIBUTE_FLOAT_RW_CHECK("volume", 0.0, 1.0, KX_CDActuator, m_gain,pyattr_setGain),
200         KX_PYATTRIBUTE_INT_RW("track", 1, 99, false, KX_CDActuator, m_track),
201         { NULL }        //Sentinel
202 };
203
204 int KX_CDActuator::pyattr_setGain(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
205 {
206         KX_CDActuator* act = static_cast<KX_CDActuator*>(self);
207         SND_CDObject::Instance()->SetGain(act->m_gain);
208         return PY_SET_ATTR_SUCCESS;
209 }
210
211 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, startCD,
212 "startCD()\n"
213 "\tStarts the CD playing.\n")
214 {
215         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
216         Py_RETURN_NONE;
217 }         
218
219
220 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, pauseCD,
221 "pauseCD()\n"
222 "\tPauses the CD playing.\n")
223 {
224         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PAUSE);
225         Py_RETURN_NONE;
226
227
228
229 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, resumeCD,
230 "resumeCD()\n"
231 "\tResumes the CD playing.\n")
232 {
233         SND_CDObject::Instance()->SetPlaystate(SND_MUST_RESUME);
234         Py_RETURN_NONE;
235
236
237
238 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, stopCD,
239 "stopCD()\n"
240 "\tStops the CD playing.\n")
241 {
242         SND_CDObject::Instance()->SetPlaystate(SND_MUST_STOP);
243         Py_RETURN_NONE;
244 }
245
246
247 KX_PYMETHODDEF_DOC_O(KX_CDActuator, playTrack,
248 "playTrack(trackNumber)\n"
249 "\tPlays the track selected.\n")
250 {
251         if (PyLong_Check(value)) {
252                 int track = PyLong_AsSsize_t(value);
253                 SND_CDObject::Instance()->SetPlaymode(SND_CD_TRACK);
254                 SND_CDObject::Instance()->SetTrack(track);
255                 SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
256         }
257         Py_RETURN_NONE;
258 }
259
260
261
262 KX_PYMETHODDEF_DOC_NOARGS(KX_CDActuator, playAll,
263 "playAll()\n"
264 "\tPlays the CD from the beginning.\n")
265 {
266         SND_CDObject::Instance()->SetPlaymode(SND_CD_ALL);
267         SND_CDObject::Instance()->SetTrack(1);
268         SND_CDObject::Instance()->SetPlaystate(SND_MUST_PLAY);
269         Py_RETURN_NONE;
270 }     
271
272 // Deprecated ----->
273 PyObject* KX_CDActuator::PySetGain(PyObject* args)
274 {
275         float gain = 1.0;
276         ShowDeprecationWarning("setGain()", "the volume property");
277         if (!PyArg_ParseTuple(args, "f:setGain", &gain))
278                 return NULL;
279         
280         SND_CDObject::Instance()->SetGain(gain);
281         
282         Py_RETURN_NONE;
283 }         
284
285
286
287 PyObject* KX_CDActuator::PyGetGain(PyObject* args)
288 {
289         float gain = SND_CDObject::Instance()->GetGain();
290         ShowDeprecationWarning("getGain()", "the volume property");
291         PyObject* result = PyFloat_FromDouble(gain);
292         
293         return result;
294 }
295 // <-----