Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOJSSound.m
Go to the documentation of this file.
1/*
2
3OOJSSound.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"
27#import "OOSound.h"
28#import "OOMusicController.h"
29#import "ResourceManager.h"
30#import "Universe.h"
31
32
33static JSObject *sSoundPrototype;
34
35
36static OOSound *GetNamedSound(NSString *name);
37
38
39static JSBool SoundGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
40
41// Static methods
42static JSBool SoundStaticLoad(JSContext *context, uintN argc, jsval *vp);
43static JSBool SoundStaticMusicSoundSource(JSContext *context, uintN argc, jsval *vp);
44static JSBool SoundStaticPlayMusic(JSContext *context, uintN argc, jsval *vp);
45static JSBool SoundStaticStopMusic(JSContext *context, uintN argc, jsval *vp);
46
47
48static JSClass sSoundClass =
49{
50 "Sound",
51 JSCLASS_HAS_PRIVATE,
52
53 JS_PropertyStub, // addProperty
54 JS_PropertyStub, // delProperty
55 SoundGetProperty, // getProperty
56 JS_StrictPropertyStub, // setProperty
57 JS_EnumerateStub, // enumerate
58 JS_ResolveStub, // resolve
59 JS_ConvertStub, // convert
60 OOJSObjectWrapperFinalize, // finalize
61 JSCLASS_NO_OPTIONAL_MEMBERS
62};
63
64
65enum
66{
67 // Property IDs
69};
70
71
72static JSPropertySpec sSoundProperties[] =
73{
74 // JS name ID flags
76 { 0 }
77};
78
79
80static JSFunctionSpec sSoundMethods[] =
81{
82 // JS name Function min args
83 { "toString", OOJSObjectWrapperToString, 0, },
84 { 0 }
85};
86
87
88static JSFunctionSpec sSoundStaticMethods[] =
89{
90 // JS name Function min args
91 { "load", SoundStaticLoad, 1, },
92 { "musicSoundSource", SoundStaticMusicSoundSource,0, },
93 { "playMusic", SoundStaticPlayMusic, 1, },
94 { "stopMusic", SoundStaticStopMusic, 0, },
95 { 0 }
96};
97
98
100
101
102// *** Public ***
103
104void InitOOJSSound(JSContext *context, JSObject *global)
105{
108}
109
110
111OOSound *SoundFromJSValue(JSContext *context, jsval value)
112{
114
116 if ([PLAYER status] != STATUS_START_GAME && JSVAL_IS_STRING(value))
117 {
118 return GetNamedSound(OOStringFromJSValue(context, value));
119 }
120 else
121 {
122 return OOJSNativeObjectOfClassFromJSValue(context, value, [OOSound class]);
123 }
125
127}
128
129
130// *** Implementation stuff ***
131
132static JSBool SoundGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
133{
134 if (!JSID_IS_INT(propID)) return YES;
135
136 OOJS_NATIVE_ENTER(context)
137
138 OOSound *sound = nil;
139
140 if (EXPECT_NOT(!JSSoundGetSound(context, this, &sound))) return NO;
141
142 switch (JSID_TO_INT(propID))
143 {
144 case kSound_name:
145 *value = OOJSValueFromNativeObject(context, [sound name]);
146 return YES;
147
148 default:
149 OOJSReportBadPropertySelector(context, this, propID, sSoundProperties);
150 return NO;
151 }
152
154}
155
156
157static OOSound *GetNamedSound(NSString *name)
158{
159 OOSound *sound = nil;
160
161 if ([name hasPrefix:@"["] && [name hasSuffix:@"]"])
162 {
163 sound = [OOSound soundWithCustomSoundKey:name];
164 }
165 else
166 {
167 sound = [ResourceManager ooSoundNamed:name inFolder:@"Sounds"];
168 }
169
170 return sound;
171}
172
173
174// *** Static methods ***
175
176// load(name : String) : Sound
177static JSBool SoundStaticLoad(JSContext *context, uintN argc, jsval *vp)
178{
179 OOJS_NATIVE_ENTER(context)
180
181 NSString *name = nil;
182 OOSound *sound = nil;
183
184 if (argc > 0) name = OOStringFromJSValue(context, OOJS_ARGV[0]);
185 if (name == nil)
186 {
187 OOJSReportBadArguments(context, @"Sound", @"load", MIN(argc, 1U), OOJS_ARGV, nil, @"string");
188 return NO;
189 }
190
192 sound = GetNamedSound(name);
194
195 OOJS_RETURN_OBJECT(sound);
196
198}
199
200
201static JSBool SoundStaticMusicSoundSource(JSContext *context, uintN argc, jsval *vp)
202{
203 OOJS_NATIVE_ENTER(context)
204 OOSoundSource *musicSource = nil;
208 OOJS_RETURN_OBJECT(musicSource);
210}
211
212
213// playMusic(name : String [, loop : Boolean] [, gain : float])
214static JSBool SoundStaticPlayMusic(JSContext *context, uintN argc, jsval *vp)
215{
216 OOJS_NATIVE_ENTER(context)
217
218 NSString *name = nil;
219 JSBool loop = NO;
220 double gain = OO_DEFAULT_SOUNDSOURCE_GAIN;
221
222 if (argc > 0) name = OOStringFromJSValue(context, OOJS_ARGV[0]);
223 if (name == nil)
224 {
225 OOJSReportBadArguments(context, @"Sound", @"playMusic", MIN(argc, 1U), OOJS_ARGV, nil, @"string");
226 return NO;
227 }
228 if (argc > 1)
229 {
230 if (!JS_ValueToBoolean(context, OOJS_ARGV[1], &loop))
231 {
232 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 1, nil, @"boolean");
233 return NO;
234 }
235 }
236
237 if (argc > 2)
238 {
239 if (!OOJSArgumentListGetNumber(context, @"Sound", @"playMusic", 2, OOJS_ARGV + 2, &gain, NULL))
240 {
241 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 2, nil, @"float");
242 return NO;
243 }
244 }
245
249
251
253}
254
255
256// Sound.stopMusic([name : String])
257static JSBool SoundStaticStopMusic(JSContext *context, uintN argc, jsval *vp)
258{
259 OOJS_NATIVE_ENTER(context)
260
261 NSString *name = nil;
262
263 if (argc > 0)
264 {
265 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
266 if (EXPECT_NOT(name == nil))
267 {
268 OOJSReportBadArguments(context, @"Sound", @"stopMusic", argc, OOJS_ARGV, nil, @"string or no argument");
269 return NO;
270 }
271 }
272
275 if (name == nil || [name isEqualToString:[controller playingMusic]])
276 {
278 }
280
282
284}
285
286
287@implementation OOSound (OOJavaScriptExtentions)
288
289- (jsval) oo_jsValueInContext:(JSContext *)context
290{
291 JSObject *jsSelf = NULL;
292 jsval result = JSVAL_NULL;
293
294 jsSelf = JS_NewObject(context, &sSoundClass, sSoundPrototype, NULL);
295 if (jsSelf != NULL)
296 {
297 if (!JS_SetPrivate(context, jsSelf, [self retain])) jsSelf = NULL;
298 }
299 if (jsSelf != NULL) result = OBJECT_TO_JSVAL(jsSelf);
300
301 return result;
302}
303
304
305- (NSString *) oo_jsDescription
306{
307 return [NSString stringWithFormat:@"[Sound \"%@\"]", [self name]];
308}
309
310
311- (NSString *) oo_jsClassName
312{
313 return @"Sound";
314}
315
316@end
#define EXPECT_NOT(x)
#define OOJS_PROFILE_EXIT
#define OOJS_END_FULL_NATIVE
#define OOJS_BEGIN_FULL_NATIVE(context)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
static JSBool SoundGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSSound.m:132
static JSObject * sSoundPrototype
Definition OOJSSound.m:33
static JSPropertySpec sSoundProperties[]
Definition OOJSSound.m:72
@ kSound_name
Definition OOJSSound.m:68
static JSBool SoundStaticStopMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:257
static JSBool SoundStaticPlayMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:214
static JSBool SoundStaticLoad(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:177
static JSFunctionSpec sSoundMethods[]
Definition OOJSSound.m:80
static JSFunctionSpec sSoundStaticMethods[]
Definition OOJSSound.m:88
OOSound * SoundFromJSValue(JSContext *context, jsval value)
Definition OOJSSound.m:111
static JSClass sSoundClass
Definition OOJSSound.m:48
static JSBool SoundStaticMusicSoundSource(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:201
void InitOOJSSound(JSContext *context, JSObject *global)
Definition OOJSSound.m:104
static OOSound * GetNamedSound(NSString *name)
Definition OOJSSound.m:157
void OOJSPauseTimeLimiter(void)
BOOL OOJSArgumentListGetNumber(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
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)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_ARGV
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
void OOJSResumeTimeLimiter(void)
#define OOJS_RETURN_VOID
#define MIN(A, B)
Definition OOMaths.h:111
return nil
#define PLAYER
OOMusicController * sharedController()
void playMusicNamed:loop:gain:(NSString *name,[loop] BOOL loop,[gain] float gain)
OOSoundSource * soundSource()
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:11029
OOSound * ooSoundNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)