Oolite
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 "OOMusicController.h"
28#import "ResourceManager.h"
29#import "Universe.h"
30
31static JSObject* sSoundPrototype;
32
33static OOSound* GetNamedSound(NSString* name);
34
35static JSBool SoundGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
36
37// Static methods
38static JSBool SoundStaticLoad(JSContext* context, uintN argc, jsval* vp);
39static JSBool SoundStaticMusicSoundSource(JSContext* context, uintN argc, jsval* vp);
40static JSBool SoundStaticPlayMusic(JSContext* context, uintN argc, jsval* vp);
41static JSBool SoundStaticStopMusic(JSContext* context, uintN argc, jsval* vp);
42
43static JSClass sSoundClass = {
44 "Sound",
45 JSCLASS_HAS_PRIVATE,
46
47 JS_PropertyStub, // addProperty
48 JS_PropertyStub, // delProperty
49 SoundGetProperty, // getProperty
50 JS_StrictPropertyStub, // setProperty
51 JS_EnumerateStub, // enumerate
52 JS_ResolveStub, // resolve
53 JS_ConvertStub, // convert
54 OOJSObjectWrapperFinalize, // finalize
55 JSCLASS_NO_OPTIONAL_MEMBERS
56};
57
58enum {
59 // Property IDs
61};
62
63static JSPropertySpec sSoundProperties[] = {
64 // JS name ID flags
66 { 0 }
67};
68
69static JSFunctionSpec sSoundMethods[] = {
70 // JS name Function min args
71 {
72 "toString",
74 0,
75 },
76 { 0 }
77};
78
79static JSFunctionSpec sSoundStaticMethods[] = {
80 // JS name Function min args
81 {
82 "load",
84 1,
85 },
86 {
87 "musicSoundSource",
89 0,
90 },
91 {
92 "playMusic",
94 1,
95 },
96 {
97 "stopMusic",
99 0,
100 },
101 { 0 }
102};
103
105
106// *** Public ***
107
108void InitOOJSSound(JSContext* context, JSObject* global)
109{
112}
113
114OOSound* SoundFromJSValue(JSContext* context, jsval value)
115{
117
119 if ([PLAYER status] != STATUS_START_GAME && JSVAL_IS_STRING(value)) {
120 return GetNamedSound(OOStringFromJSValue(context, value));
121 } else {
122 return OOJSNativeObjectOfClassFromJSValue(context, value, [OOSound class]);
123 }
125
127}
128
129// *** Implementation stuff ***
130
131static JSBool SoundGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
132{
133 if (!JSID_IS_INT(propID))
134 return YES;
135
136 OOJS_NATIVE_ENTER(context)
137
138 OOSound* sound = nil;
139
140 if (EXPECT_NOT(!JSSoundGetSound(context, this, &sound)))
141 return NO;
142
143 switch (JSID_TO_INT(propID)) {
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
156static OOSound* GetNamedSound(NSString* name)
157{
158 OOSound* sound = nil;
159
160 if ([name hasPrefix:@"["] && [name hasSuffix:@"]"]) {
161 sound = [OOSound soundWithCustomSoundKey:name];
162 } else {
163 sound = [ResourceManager ooSoundNamed:name inFolder:@"Sounds"];
164 }
165
166 return sound;
167}
168
169// *** Static methods ***
170
171// load(name : String) : Sound
172static JSBool SoundStaticLoad(JSContext* context, uintN argc, jsval* vp)
173{
174 OOJS_NATIVE_ENTER(context)
175
176 NSString* name = nil;
177 OOSound* sound = nil;
178
179 if (argc > 0)
180 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
181 if (name == nil) {
182 OOJSReportBadArguments(context, @"Sound", @"load", MIN(argc, 1U), OOJS_ARGV, nil, @"string");
183 return NO;
184 }
185
187 sound = GetNamedSound(name);
189
190 OOJS_RETURN_OBJECT(sound);
191
193}
194
195static JSBool SoundStaticMusicSoundSource(JSContext* context, uintN argc, jsval* vp)
196{
197 OOJS_NATIVE_ENTER(context)
198 OOSoundSource* musicSource = nil;
202 OOJS_RETURN_OBJECT(musicSource);
204}
205
206// playMusic(name : String [, loop : Boolean] [, gain : float])
207static JSBool SoundStaticPlayMusic(JSContext* context, uintN argc, jsval* vp)
208{
209 OOJS_NATIVE_ENTER(context)
210
211 NSString* name = nil;
212 JSBool loop = NO;
213 double gain = OO_DEFAULT_SOUNDSOURCE_GAIN;
214
215 if (argc > 0)
216 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
217 if (name == nil) {
218 OOJSReportBadArguments(context, @"Sound", @"playMusic", MIN(argc, 1U), OOJS_ARGV, nil, @"string");
219 return NO;
220 }
221 if (argc > 1) {
222 if (!JS_ValueToBoolean(context, OOJS_ARGV[1], &loop)) {
223 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 1, nil, @"boolean");
224 return NO;
225 }
226 }
227
228 if (argc > 2) {
229 if (!OOJSArgumentListGetNumber(context, @"Sound", @"playMusic", 2, OOJS_ARGV + 2, &gain, NULL)) {
230 OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV + 2, nil, @"float");
231 return NO;
232 }
233 }
234
238
240
242}
243
244// Sound.stopMusic([name : String])
245static JSBool SoundStaticStopMusic(JSContext* context, uintN argc, jsval* vp)
246{
247 OOJS_NATIVE_ENTER(context)
248
249 NSString* name = nil;
250
251 if (argc > 0) {
252 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
253 if (EXPECT_NOT(name == nil)) {
254 OOJSReportBadArguments(context, @"Sound", @"stopMusic", argc, OOJS_ARGV, nil, @"string or no argument");
255 return NO;
256 }
257 }
258
261 if (name == nil || [name isEqualToString:[controller playingMusic]]) {
263 }
265
267
269}
270
271@implementation OOSound (OOJavaScriptExtentions)
272
273- (jsval)oo_jsValueInContext:(JSContext*)context
274{
275 JSObject* jsSelf = NULL;
276 jsval result = JSVAL_NULL;
277
278 jsSelf = JS_NewObject(context, &sSoundClass, sSoundPrototype, NULL);
279 if (jsSelf != NULL) {
280 if (!JS_SetPrivate(context, jsSelf, [self retain]))
281 jsSelf = NULL;
282 }
283 if (jsSelf != NULL)
284 result = OBJECT_TO_JSVAL(jsSelf);
285
286 return result;
287}
288
290{
291 return [NSString stringWithFormat:@"[Sound \"%@\"]", [self name]];
292}
293
294- (NSString*)oo_jsClassName
295{
296 return @"Sound";
297}
298
299@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
void InitOOJSSound(JSContext *context, JSObject *global)
Definition OOJSSound.m:108
static JSBool SoundGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSSound.m:131
static JSObject * sSoundPrototype
Definition OOJSSound.m:31
@ kSound_name
Definition OOJSSound.m:60
static JSPropertySpec sSoundProperties[]
Definition OOJSSound.m:63
static JSBool SoundStaticStopMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:245
static JSBool SoundStaticPlayMusic(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:207
static JSBool SoundStaticLoad(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:172
static JSFunctionSpec sSoundMethods[]
Definition OOJSSound.m:69
static JSFunctionSpec sSoundStaticMethods[]
Definition OOJSSound.m:79
OOSound * SoundFromJSValue(JSContext *context, jsval value)
Definition OOJSSound.m:114
static JSClass sSoundClass
Definition OOJSSound.m:43
static JSBool SoundStaticMusicSoundSource(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSound.m:195
static OOSound * GetNamedSound(NSString *name)
Definition OOJSSound.m:156
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:91
return nil
#define PLAYER
OOMusicController * sharedController()
void playMusicNamed:loop:gain:(NSString *name,[loop] BOOL loop,[gain] float gain)
OOSoundSource * soundSource()
NSString * name()
Definition OOALSound.m:130
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:9950
OOSound * ooSoundNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)