Oolite 1.91.0.7604-240417-a536cbe
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
31
32static JSObject *sSoundSourcePrototype;
33
34
35static JSBool SoundSourceGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
36static JSBool SoundSourceSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
37static JSBool SoundSourceConstruct(JSContext *context, uintN argc, jsval *vp);
38
39// Methods
40static JSBool SoundSourcePlay(JSContext *context, uintN argc, jsval *vp);
41static JSBool SoundSourceStop(JSContext *context, uintN argc, jsval *vp);
42static JSBool SoundSourcePlayOrRepeat(JSContext *context, uintN argc, jsval *vp);
43
44
45static JSClass sSoundSourceClass =
46{
47 "SoundSource",
48 JSCLASS_HAS_PRIVATE,
49
50 JS_PropertyStub, // addProperty
51 JS_PropertyStub, // delProperty
52 SoundSourceGetProperty, // getProperty
53 SoundSourceSetProperty, // setProperty
54 JS_EnumerateStub, // enumerate
55 JS_ResolveStub, // resolve
56 JS_ConvertStub, // convert
57 OOJSObjectWrapperFinalize, // finalize
58 JSCLASS_NO_OPTIONAL_MEMBERS
59};
60
61
62enum
63{
64 // Property IDs
72};
73
74
75static JSPropertySpec sSoundSourceProperties[] =
76{
77 // JS name ID flags
85 { 0 }
86};
87
88
89static JSFunctionSpec sSoundSourceMethods[] =
90{
91 // JS name Function min args
92 { "toString", OOJSObjectWrapperToString, 0, },
93 { "play", SoundSourcePlay, 0, },
94 { "playOrRepeat", SoundSourcePlayOrRepeat, 0, },
95 // playSound is defined in oolite-global-prefix.js.
96 { "stop", SoundSourceStop, 0, },
97 { 0 }
98};
99
100
102
103
104// *** Public ***
105
106void InitOOJSSoundSource(JSContext *context, JSObject *global)
107{
108 sSoundSourcePrototype = JS_InitClass(context, global, NULL, &sSoundSourceClass, SoundSourceConstruct, 0, sSoundSourceProperties, sSoundSourceMethods, NULL, NULL);
110}
111
112
113static JSBool SoundSourceConstruct(JSContext *context, uintN argc, jsval *vp)
114{
115 OOJS_NATIVE_ENTER(context)
116
117 if (EXPECT_NOT(!JS_IsConstructing(context, vp)))
118 {
119 OOJSReportError(context, @"SoundSource() cannot be called as a function, it must be used as a constructor (as in new SoundSource()).");
120 return NO;
121 }
122
123 OOJS_RETURN_OBJECT([[[OOSoundSource alloc] init] autorelease]);
124
126}
127
128
129// *** Implementation stuff ***
130
131static JSBool SoundSourceGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
132{
133 if (!JSID_IS_INT(propID)) return YES;
134
135 OOJS_NATIVE_ENTER(context)
136
137 OOSoundSource *soundSource = nil;
138
139 if (!JSSoundSourceGetSoundSource(context, this, &soundSource)) return NO;
140
141 switch (JSID_TO_INT(propID))
142 {
144 *value = OOJSValueFromNativeObject(context, [soundSource sound]);
145 return YES;
146
148 *value = OOJSValueFromBOOL([soundSource isPlaying]);
149 return YES;
150
152 *value = OOJSValueFromBOOL([soundSource loop]);
153 return YES;
154
156 *value = INT_TO_JSVAL([soundSource repeatCount]);
157 return YES;
158
160 return VectorToJSValue(context, [soundSource position], value);
161
163 *value = OOJSValueFromBOOL([soundSource positional]);
164 return YES;
165
167 return JS_NewNumberValue(context, [soundSource gain], value);
168
169
170 default:
172 return NO;
173 }
174
176}
177
178
179static JSBool SoundSourceSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
180{
181 if (!JSID_IS_INT(propID)) return YES;
182
183 OOJS_NATIVE_ENTER(context)
184
185 OOSoundSource *soundSource = nil;
186 int32 iValue;
187 JSBool bValue;
188 Vector vValue;
189 double fValue;
190
191 if (!JSSoundSourceGetSoundSource(context, this, &soundSource)) return NO;
192
193 switch (JSID_TO_INT(propID))
194 {
196 [soundSource setSound:SoundFromJSValue(context, *value)];
197 return YES;
198 break;
199
201 if (JS_ValueToBoolean(context, *value, &bValue))
202 {
203 [soundSource setLoop:bValue];
204 return YES;
205 }
206 break;
207
209 if (JS_ValueToInt32(context, *value, &iValue))
210 {
211 if (iValue > 100) iValue = 100;
212 if (100 < 1) iValue = 1;
213 [soundSource setRepeatCount:iValue];
214 return YES;
215 }
216 break;
217
218
220 if (JSValueToVector(context, *value, &vValue))
221 {
222 [soundSource setPosition:vValue];
223 return YES;
224 }
225 break;
226
228 if (JS_ValueToBoolean(context, *value, &bValue))
229 {
230 [soundSource setPositional:bValue];
231 return YES;
232 }
233 break;
234
236 if (JS_ValueToNumber(context, *value, &fValue))
237 {
238 fValue = OOClamp_0_max_d(fValue, 1);
239 [soundSource setGain:fValue];
240 return YES;
241 }
242 break;
243
244 default:
246 return NO;
247 }
248
249 OOJSReportBadPropertyValue(context, this, propID, sSoundSourceProperties, *value);
250 return NO;
251
253}
254
255
256// *** Methods ***
257
258// play([count : Number])
259static JSBool SoundSourcePlay(JSContext *context, uintN argc, jsval *vp)
260{
261 OOJS_NATIVE_ENTER(context)
262
263 OOSoundSource *thisv = nil;
264 int32 count = 0;
265
266 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv))) return NO;
267 if (argc > 0 && !JSVAL_IS_VOID(OOJS_ARGV[0]) && !JS_ValueToInt32(context, OOJS_ARGV[0], &count))
268 {
269 OOJSReportBadArguments(context, @"SoundSource", @"play", 1, OOJS_ARGV, nil, @"integer count or no argument");
270 return NO;
271 }
272
273 if (count > 0)
274 {
275 if (count > 100) count = 100;
276 [thisv setRepeatCount:count];
277 }
278
280 [thisv play];
282
284
286}
287
288
289// stop()
290static JSBool SoundSourceStop(JSContext *context, uintN argc, jsval *vp)
291{
292 OOJS_NATIVE_ENTER(context)
293
294 OOSoundSource *thisv = nil;
295
296 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv))) return NO;
297
299 [thisv stop];
301
303
305}
306
307
308// playOrRepeat()
309static JSBool SoundSourcePlayOrRepeat(JSContext *context, uintN argc, jsval *vp)
310{
311 OOJS_NATIVE_ENTER(context)
312
313 OOSoundSource *thisv = nil;
314
315 if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv))) return NO;
316
318 [thisv playOrRepeat];
320
322
324}
325
326
327@implementation OOSoundSource (OOJavaScriptExtentions)
328
329- (jsval) oo_jsValueInContext:(JSContext *)context
330{
331 JSObject *jsSelf = NULL;
332 jsval result = JSVAL_NULL;
333
334 jsSelf = JS_NewObject(context, &sSoundSourceClass, sSoundSourcePrototype, NULL);
335 if (jsSelf != NULL)
336 {
337 if (!JS_SetPrivate(context, jsSelf, [self retain])) jsSelf = NULL;
338 }
339 if (jsSelf != NULL) result = OBJECT_TO_JSVAL(jsSelf);
340
341 return result;
342}
343
344
345- (NSString *) oo_jsClassName
346{
347 return @"SoundSource";
348}
349
350@end
#define EXPECT_NOT(x)
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static JSBool SoundSourceConstruct(JSContext *context, uintN argc, jsval *vp)
void InitOOJSSoundSource(JSContext *context, JSObject *global)
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)
@ kSoundSource_loop
@ kSoundSource_positional
@ kSoundSource_volume
@ kSoundSource_position
@ kSoundSource_sound
@ kSoundSource_repeatCount
@ kSoundSource_isPlaying
static JSFunctionSpec sSoundSourceMethods[]
static JSObject * sSoundSourcePrototype
static JSPropertySpec sSoundSourceProperties[]
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:259
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:185
#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)