Oolite
Loading...
Searching...
No Matches
OOJSSoundSource.m
Go to the documentation of this file.
1/*
2
3OOJSSoundSource.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOJSSound.h"
26#import "OOJSVector.h"
28#import "OOSound.h"
29#import "ResourceManager.h"
30
31static JSObject* sSoundSourcePrototype;
32
33static JSBool SoundSourceGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
34static JSBool SoundSourceSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
35static JSBool SoundSourceConstruct(JSContext* context, uintN argc, jsval* vp);
36
37// Methods
38static JSBool SoundSourcePlay(JSContext* context, uintN argc, jsval* vp);
39static JSBool SoundSourceStop(JSContext* context, uintN argc, jsval* vp);
40static JSBool SoundSourcePlayOrRepeat(JSContext* context, uintN argc, jsval* vp);
41
42static JSClass sSoundSourceClass = {
43 "SoundSource",
44 JSCLASS_HAS_PRIVATE,
45
46 JS_PropertyStub, // addProperty
47 JS_PropertyStub, // delProperty
48 SoundSourceGetProperty, // getProperty
49 SoundSourceSetProperty, // setProperty
50 JS_EnumerateStub, // enumerate
51 JS_ResolveStub, // resolve
52 JS_ConvertStub, // convert
53 OOJSObjectWrapperFinalize, // finalize
54 JSCLASS_NO_OPTIONAL_MEMBERS
55};
56
57enum {
58 // Property IDs
66};
67
68static JSPropertySpec sSoundSourceProperties[] = {
69 // JS name ID flags
77 { 0 }
78};
79
80static JSFunctionSpec sSoundSourceMethods[] = {
81 // JS name Function min args
82 {
83 "toString",
85 0,
86 },
87 {
88 "play",
90 0,
91 },
92 {
93 "playOrRepeat",
95 0,
96 },
97 // playSound is defined in oolite-global-prefix.js.
98 {
99 "stop",
101 0,
102 },
103 { 0 }
104};
105
107
108// *** Public ***
109
110void InitOOJSSoundSource(JSContext* context, JSObject* global)
111{
112 sSoundSourcePrototype = JS_InitClass(context, global, NULL, &sSoundSourceClass, SoundSourceConstruct, 0, sSoundSourceProperties, sSoundSourceMethods, NULL, NULL);
114}
115
116static JSBool SoundSourceConstruct(JSContext* context, uintN argc, jsval* vp)
117{
118 OOJS_NATIVE_ENTER(context)
119
120 if (EXPECT_NOT(!JS_IsConstructing(context, vp))) {
121 OOJSReportError(context, @"SoundSource() cannot be called as a function, it must be used as a constructor (as in new SoundSource()).");
122 return NO;
123 }
124
125 OOJS_RETURN_OBJECT([[[OOSoundSource alloc] init] autorelease]);
126
128}
129
130// *** Implementation stuff ***
131
132static JSBool SoundSourceGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
133{
134 if (!JSID_IS_INT(propID))
135 return YES;
136
137 OOJS_NATIVE_ENTER(context)
138
139 OOSoundSource* soundSource = nil;
140
141 if (!JSSoundSourceGetSoundSource(context, this, &soundSource))
142 return NO;
143
144 switch (JSID_TO_INT(propID)) {
146 *value = OOJSValueFromNativeObject(context, [soundSource sound]);
147 return YES;
148
150 *value = OOJSValueFromBOOL([soundSource isPlaying]);
151 return YES;
152
154 *value = OOJSValueFromBOOL([soundSource loop]);
155 return YES;
156
158 *value = INT_TO_JSVAL([soundSource repeatCount]);
159 return YES;
160
162 return VectorToJSValue(context, [soundSource position], value);
163
165 *value = OOJSValueFromBOOL([soundSource positional]);
166 return YES;
167
169 return JS_NewNumberValue(context, [soundSource gain], value);
170
171 default:
173 return NO;
174 }
175
177}
178
179static JSBool SoundSourceSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
180{
181 if (!JSID_IS_INT(propID))
182 return YES;
183
184 OOJS_NATIVE_ENTER(context)
185
186 OOSoundSource* soundSource = nil;
187 int32 iValue;
188 JSBool bValue;
189 Vector vValue;
190 double fValue;
191
192 if (!JSSoundSourceGetSoundSource(context, this, &soundSource))
193 return NO;
194
195 switch (JSID_TO_INT(propID)) {
197 [soundSource setSound:SoundFromJSValue(context, *value)];
198 return YES;
199 break;
200
202 if (JS_ValueToBoolean(context, *value, &bValue)) {
203 [soundSource setLoop:bValue];
204 return YES;
205 }
206 break;
207
209 if (JS_ValueToInt32(context, *value, &iValue)) {
210 if (iValue > 100)
211 iValue = 100;
212 if (100 < 1)
213 iValue = 1;
214 [soundSource setRepeatCount:iValue];
215 return YES;
216 }
217 break;
218
220 if (JSValueToVector(context, *value, &vValue)) {
221 [soundSource setPosition:vValue];
222 return YES;
223 }
224 break;
225
227 if (JS_ValueToBoolean(context, *value, &bValue)) {
228 [soundSource setPositional:bValue];
229 return YES;
230 }
231 break;
232
234 if (JS_ValueToNumber(context, *value, &fValue)) {
235 fValue = OOClamp_0_max_d(fValue, 1);
236 [soundSource setGain:fValue];
237 return YES;
238 }
239 break;
240
241 default:
243 return NO;
244 }
245
246 OOJSReportBadPropertyValue(context, this, propID, sSoundSourceProperties, *value);
247 return NO;
248
250}
251
252// *** Methods ***
253
254// play([count : Number])
255static JSBool SoundSourcePlay(JSContext* context, uintN argc, jsval* vp)
256{
257 OOJS_NATIVE_ENTER(context)
258
259 OOSoundSource* thisv = nil;
260 int32 count = 0;
261
262 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
263 return NO;
264 if (argc > 0 && !JSVAL_IS_VOID(OOJS_ARGV[0]) && !JS_ValueToInt32(context, OOJS_ARGV[0], &count)) {
265 OOJSReportBadArguments(context, @"SoundSource", @"play", 1, OOJS_ARGV, nil, @"integer count or no argument");
266 return NO;
267 }
268
269 if (count > 0) {
270 if (count > 100)
271 count = 100;
272 [thisv setRepeatCount:count];
273 }
274
276 [thisv play];
278
280
282}
283
284// stop()
285static JSBool SoundSourceStop(JSContext* context, uintN argc, jsval* vp)
286{
287 OOJS_NATIVE_ENTER(context)
288
289 OOSoundSource* thisv = nil;
290
291 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
292 return NO;
293
295 [thisv stop];
297
299
301}
302
303// playOrRepeat()
304static JSBool SoundSourcePlayOrRepeat(JSContext* context, uintN argc, jsval* vp)
305{
306 OOJS_NATIVE_ENTER(context)
307
308 OOSoundSource* thisv = nil;
309
310 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv)))
311 return NO;
312
314 [thisv playOrRepeat];
316
318
320}
321
322@implementation OOSoundSource (OOJavaScriptExtentions)
323
324- (jsval)oo_jsValueInContext:(JSContext*)context
325{
326 JSObject* jsSelf = NULL;
327 jsval result = JSVAL_NULL;
328
329 jsSelf = JS_NewObject(context, &sSoundSourceClass, sSoundSourcePrototype, NULL);
330 if (jsSelf != NULL) {
331 if (!JS_SetPrivate(context, jsSelf, [self retain]))
332 jsSelf = NULL;
333 }
334 if (jsSelf != NULL)
335 result = OBJECT_TO_JSVAL(jsSelf);
336
337 return result;
338}
339
340- (NSString*)oo_jsClassName
341{
342 return @"SoundSource";
343}
344
345@end
OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC
#define EXPECT_NOT(x)
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
void InitOOJSSoundSource(JSContext *context, JSObject *global)
static JSBool SoundSourceConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool SoundSourcePlay(JSContext *context, uintN argc, jsval *vp)
static JSClass sSoundSourceClass
static JSBool SoundSourcePlayOrRepeat(JSContext *context, uintN argc, jsval *vp)
static JSBool SoundSourceGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool SoundSourceSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool SoundSourceStop(JSContext *context, uintN argc, jsval *vp)
static JSFunctionSpec sSoundSourceMethods[]
static JSObject * sSoundSourcePrototype
@ kSoundSource_loop
@ kSoundSource_positional
@ kSoundSource_volume
@ kSoundSource_position
@ kSoundSource_sound
@ kSoundSource_repeatCount
@ kSoundSource_isPlaying
static JSPropertySpec sSoundSourceProperties[]
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:341
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:264
#define OOJS_THIS
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READWRITE_CB
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_PROP_READONLY_CB
#define OOJS_RETURN_VOID
unsigned count
return nil
void setPositional:(BOOL inPositional)
void setLoop:(BOOL inLoop)
void setRepeatCount:(uint8_t inCount)
void setGain:(float gain)
void setPosition:(Vector inPosition)
void setSound:(OOSound *inSound)