Line data Source code
1 0 : /*
2 : OOJSVisualEffect.m
3 :
4 : Oolite
5 : Copyright (C) 2004-2013 Giles C Williams and contributors
6 :
7 : This program is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License
9 : as published by the Free Software Foundation; either version 2
10 : of the License, or (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 : MA 02110-1301, USA.
21 :
22 : */
23 :
24 : #import "OOVisualEffectEntity.h"
25 : #import "OOJSVisualEffect.h"
26 : #import "OOJSEntity.h"
27 : #import "OOJSVector.h"
28 : #import "OOJavaScriptEngine.h"
29 : #import "OOMesh.h"
30 : #import "OOCollectionExtractors.h"
31 : #import "ResourceManager.h"
32 : #import "EntityOOJavaScriptExtensions.h"
33 :
34 :
35 0 : static JSObject *sVisualEffectPrototype;
36 :
37 : static BOOL JSVisualEffectGetVisualEffectEntity(JSContext *context, JSObject *stationObj, OOVisualEffectEntity **outEntity);
38 :
39 :
40 : static JSBool VisualEffectGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
41 : static JSBool VisualEffectSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
42 :
43 : static JSBool VisualEffectRemove(JSContext *context, uintN argc, jsval *vp);
44 : static JSBool VisualEffectGetShaders(JSContext *context, uintN argc, jsval *vp);
45 : static JSBool VisualEffectSetShaders(JSContext *context, uintN argc, jsval *vp);
46 : static JSBool VisualEffectGetMaterials(JSContext *context, uintN argc, jsval *vp);
47 : static JSBool VisualEffectSetMaterials(JSContext *context, uintN argc, jsval *vp);
48 : static JSBool VisualEffectScale(JSContext *context, uintN argc, jsval *vp);
49 : static JSBool VisualEffectRestoreSubEntities(JSContext *context, uintN argc, jsval *vp);
50 :
51 : static JSBool VisualEffectSetMaterialsInternal(JSContext *context, uintN argc, jsval *vp, OOVisualEffectEntity *thisEnt, BOOL fromShaders);
52 :
53 :
54 0 : static JSClass sVisualEffectClass =
55 : {
56 : "VisualEffect",
57 : JSCLASS_HAS_PRIVATE,
58 :
59 : JS_PropertyStub, // addProperty
60 : JS_PropertyStub, // delProperty
61 : VisualEffectGetProperty, // getProperty
62 : VisualEffectSetProperty, // setProperty
63 : JS_EnumerateStub, // enumerate
64 : JS_ResolveStub, // resolve
65 : JS_ConvertStub, // convert
66 : OOJSObjectWrapperFinalize,// finalize
67 : JSCLASS_NO_OPTIONAL_MEMBERS
68 : };
69 :
70 :
71 0 : enum
72 : {
73 : // Property IDs
74 : kVisualEffect_beaconCode,
75 : kVisualEffect_beaconLabel,
76 : kVisualEffect_dataKey,
77 : kVisualEffect_hullHeatLevel,
78 : kVisualEffect_isBreakPattern,
79 : kVisualEffect_scaleX,
80 : kVisualEffect_scaleY,
81 : kVisualEffect_scaleZ,
82 : kVisualEffect_scannerDisplayColor1,
83 : kVisualEffect_scannerDisplayColor2,
84 : kVisualEffect_script,
85 : kVisualEffect_scriptInfo,
86 : kVisualEffect_shaderFloat1,
87 : kVisualEffect_shaderFloat2,
88 : kVisualEffect_shaderInt1,
89 : kVisualEffect_shaderInt2,
90 : kVisualEffect_shaderVector1,
91 : kVisualEffect_shaderVector2,
92 : kVisualEffect_subEntities,
93 : kVisualEffect_vectorForward,
94 : kVisualEffect_vectorRight,
95 : kVisualEffect_vectorUp
96 : };
97 :
98 :
99 0 : static JSPropertySpec sVisualEffectProperties[] =
100 : {
101 : // JS name ID flags
102 : { "beaconCode", kVisualEffect_beaconCode, OOJS_PROP_READWRITE_CB },
103 : { "beaconLabel", kVisualEffect_beaconLabel, OOJS_PROP_READWRITE_CB },
104 : { "dataKey", kVisualEffect_dataKey, OOJS_PROP_READONLY_CB },
105 : { "isBreakPattern", kVisualEffect_isBreakPattern, OOJS_PROP_READWRITE_CB },
106 : { "scaleX", kVisualEffect_scaleX, OOJS_PROP_READWRITE_CB },
107 : { "scaleY", kVisualEffect_scaleY, OOJS_PROP_READWRITE_CB },
108 : { "scaleZ", kVisualEffect_scaleZ, OOJS_PROP_READWRITE_CB },
109 : { "scannerDisplayColor1", kVisualEffect_scannerDisplayColor1, OOJS_PROP_READWRITE_CB },
110 : { "scannerDisplayColor2", kVisualEffect_scannerDisplayColor2, OOJS_PROP_READWRITE_CB },
111 : { "hullHeatLevel", kVisualEffect_hullHeatLevel, OOJS_PROP_READWRITE_CB },
112 : { "script", kVisualEffect_script, OOJS_PROP_READONLY_CB },
113 : { "scriptInfo", kVisualEffect_scriptInfo, OOJS_PROP_READONLY_CB },
114 : { "shaderFloat1", kVisualEffect_shaderFloat1, OOJS_PROP_READWRITE_CB },
115 : { "shaderFloat2", kVisualEffect_shaderFloat2, OOJS_PROP_READWRITE_CB },
116 : { "shaderInt1", kVisualEffect_shaderInt1, OOJS_PROP_READWRITE_CB },
117 : { "shaderInt2", kVisualEffect_shaderInt2, OOJS_PROP_READWRITE_CB },
118 : { "shaderVector1", kVisualEffect_shaderVector1, OOJS_PROP_READWRITE_CB },
119 : { "shaderVector2", kVisualEffect_shaderVector2, OOJS_PROP_READWRITE_CB },
120 : { "subEntities", kVisualEffect_subEntities, OOJS_PROP_READONLY_CB },
121 : { "vectorForward", kVisualEffect_vectorForward, OOJS_PROP_READONLY_CB },
122 : { "vectorRight", kVisualEffect_vectorRight, OOJS_PROP_READONLY_CB },
123 : { "vectorUp", kVisualEffect_vectorUp, OOJS_PROP_READONLY_CB },
124 : { 0 }
125 : };
126 :
127 :
128 0 : static JSFunctionSpec sVisualEffectMethods[] =
129 : {
130 : // JS name Function min args
131 : { "getMaterials", VisualEffectGetMaterials, 0 },
132 : { "getShaders", VisualEffectGetShaders, 0 },
133 : { "remove", VisualEffectRemove, 0 },
134 : { "restoreSubEntities", VisualEffectRestoreSubEntities, 0 },
135 : { "scale", VisualEffectScale, 1 },
136 : { "setMaterials", VisualEffectSetMaterials, 1 },
137 : { "setShaders", VisualEffectSetShaders, 2 },
138 :
139 : { 0 }
140 : };
141 :
142 :
143 0 : void InitOOJSVisualEffect(JSContext *context, JSObject *global)
144 : {
145 : sVisualEffectPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sVisualEffectClass, OOJSUnconstructableConstruct, 0, sVisualEffectProperties, sVisualEffectMethods, NULL, NULL);
146 : OOJSRegisterObjectConverter(&sVisualEffectClass, OOJSBasicPrivateObjectConverter);
147 : OOJSRegisterSubclass(&sVisualEffectClass, JSEntityClass());
148 : }
149 :
150 :
151 0 : static BOOL JSVisualEffectGetVisualEffectEntity(JSContext *context, JSObject *visualEffectObj, OOVisualEffectEntity **outEntity)
152 : {
153 : OOJS_PROFILE_ENTER
154 :
155 : BOOL result;
156 : Entity *entity = nil;
157 :
158 : if (outEntity == NULL) return NO;
159 : *outEntity = nil;
160 :
161 : result = OOJSEntityGetEntity(context, visualEffectObj, &entity);
162 : if (!result) return NO;
163 :
164 : if (![entity isKindOfClass:[OOVisualEffectEntity class]]) return NO;
165 :
166 : *outEntity = (OOVisualEffectEntity *)entity;
167 : return YES;
168 :
169 : OOJS_PROFILE_EXIT
170 : }
171 :
172 :
173 : @implementation OOVisualEffectEntity (OOJavaScriptExtensions)
174 :
175 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
176 : {
177 : *outClass = &sVisualEffectClass;
178 : *outPrototype = sVisualEffectPrototype;
179 : }
180 :
181 :
182 : - (NSString *) oo_jsClassName
183 : {
184 : return @"VisualEffect";
185 : }
186 :
187 : - (BOOL) isVisibleToScripts
188 : {
189 : return YES;
190 : }
191 :
192 : - (NSArray *) subEntitiesForScript
193 : {
194 : return [[self visualEffectSubEntityEnumerator] allObjects];
195 : }
196 :
197 : @end
198 :
199 :
200 0 : static JSBool VisualEffectGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
201 : {
202 : if (!JSID_IS_INT(propID)) return YES;
203 :
204 : OOJS_NATIVE_ENTER(context)
205 :
206 : OOVisualEffectEntity *entity = nil;
207 : id result = nil;
208 :
209 : if (!JSVisualEffectGetVisualEffectEntity(context, this, &entity)) return NO;
210 : if (entity == nil) { *value = JSVAL_VOID; return YES; }
211 :
212 : switch (JSID_TO_INT(propID))
213 : {
214 : case kVisualEffect_beaconCode:
215 : result = [entity beaconCode];
216 : break;
217 :
218 : case kVisualEffect_beaconLabel:
219 : result = [entity beaconLabel];
220 : break;
221 :
222 : case kVisualEffect_dataKey:
223 : result = [entity effectKey];
224 : break;
225 :
226 : case kVisualEffect_isBreakPattern:
227 : *value = OOJSValueFromBOOL([entity isBreakPattern]);
228 :
229 : return YES;
230 :
231 : case kVisualEffect_vectorRight:
232 : return VectorToJSValue(context, [entity rightVector], value);
233 :
234 : case kVisualEffect_vectorForward:
235 : return VectorToJSValue(context, [entity forwardVector], value);
236 :
237 : case kVisualEffect_vectorUp:
238 : return VectorToJSValue(context, [entity upVector], value);
239 :
240 : case kVisualEffect_scaleX:
241 : return JS_NewNumberValue(context, [entity scaleX], value);
242 :
243 : case kVisualEffect_scaleY:
244 : return JS_NewNumberValue(context, [entity scaleY], value);
245 :
246 : case kVisualEffect_scaleZ:
247 : return JS_NewNumberValue(context, [entity scaleZ], value);
248 :
249 : case kVisualEffect_scannerDisplayColor1:
250 : result = [[entity scannerDisplayColor1] normalizedArray];
251 : break;
252 :
253 : case kVisualEffect_scannerDisplayColor2:
254 : result = [[entity scannerDisplayColor2] normalizedArray];
255 : break;
256 :
257 : case kVisualEffect_hullHeatLevel:
258 : return JS_NewNumberValue(context, [entity hullHeatLevel], value);
259 :
260 : case kVisualEffect_shaderFloat1:
261 : return JS_NewNumberValue(context, [entity shaderFloat1], value);
262 :
263 : case kVisualEffect_shaderFloat2:
264 : return JS_NewNumberValue(context, [entity shaderFloat2], value);
265 :
266 : case kVisualEffect_shaderInt1:
267 : *value = INT_TO_JSVAL([entity shaderInt1]);
268 : return YES;
269 :
270 : case kVisualEffect_shaderInt2:
271 : *value = INT_TO_JSVAL([entity shaderInt2]);
272 : return YES;
273 :
274 : case kVisualEffect_shaderVector1:
275 : return VectorToJSValue(context, [entity shaderVector1], value);
276 :
277 : case kVisualEffect_shaderVector2:
278 : return VectorToJSValue(context, [entity shaderVector2], value);
279 :
280 : case kVisualEffect_subEntities:
281 : result = [entity subEntitiesForScript];
282 : break;
283 :
284 :
285 : case kVisualEffect_script:
286 : result = [entity script];
287 : break;
288 :
289 : case kVisualEffect_scriptInfo:
290 : result = [entity scriptInfo];
291 : if (result == nil) result = [NSDictionary dictionary]; // empty rather than null
292 : break;
293 :
294 : default:
295 : OOJSReportBadPropertySelector(context, this, propID, sVisualEffectProperties);
296 : return NO;
297 : }
298 :
299 : *value = OOJSValueFromNativeObject(context, result);
300 : return YES;
301 :
302 : OOJS_NATIVE_EXIT
303 : }
304 :
305 :
306 0 : static JSBool VisualEffectSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
307 : {
308 : if (!JSID_IS_INT(propID)) return YES;
309 :
310 : OOJS_NATIVE_ENTER(context)
311 :
312 : OOVisualEffectEntity *entity = nil;
313 : JSBool bValue;
314 : OOColor *colorForScript;
315 : int32 iValue;
316 : jsdouble fValue;
317 : Vector vValue;
318 : NSString *sValue = nil;
319 :
320 :
321 : if (!JSVisualEffectGetVisualEffectEntity(context, this, &entity)) return NO;
322 : if (entity == nil) return YES;
323 :
324 : switch (JSID_TO_INT(propID))
325 : {
326 : case kVisualEffect_beaconCode:
327 : sValue = OOStringFromJSValue(context,*value);
328 : if (sValue == nil || [sValue length] == 0)
329 : {
330 : if ([entity isBeacon])
331 : {
332 : [UNIVERSE clearBeacon:entity];
333 : if ([PLAYER nextBeacon] == entity)
334 : {
335 : [PLAYER setCompassMode:COMPASS_MODE_PLANET];
336 : }
337 : }
338 : }
339 : else
340 : {
341 : if ([entity isBeacon])
342 : {
343 : [entity setBeaconCode:sValue];
344 : }
345 : else // Universe needs to update beacon lists in this case only
346 : {
347 : [entity setBeaconCode:sValue];
348 : [UNIVERSE setNextBeacon:entity];
349 : }
350 : }
351 : return YES;
352 : break;
353 :
354 : case kVisualEffect_beaconLabel:
355 : sValue = OOStringFromJSValue(context,*value);
356 : if (sValue != nil)
357 : {
358 : [entity setBeaconLabel:sValue];
359 : return YES;
360 : }
361 : break;
362 :
363 : case kVisualEffect_isBreakPattern:
364 : if (JS_ValueToBoolean(context, *value, &bValue))
365 : {
366 : [entity setIsBreakPattern:bValue];
367 : return YES;
368 : }
369 : break;
370 :
371 : case kVisualEffect_scannerDisplayColor1:
372 : colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
373 : if (colorForScript != nil || JSVAL_IS_NULL(*value))
374 : {
375 : [entity setScannerDisplayColor1:colorForScript];
376 : return YES;
377 : }
378 : break;
379 :
380 : case kVisualEffect_scannerDisplayColor2:
381 : colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
382 : if (colorForScript != nil || JSVAL_IS_NULL(*value))
383 : {
384 : [entity setScannerDisplayColor2:colorForScript];
385 : return YES;
386 : }
387 : break;
388 :
389 : case kVisualEffect_scaleX:
390 : if (JS_ValueToNumber(context, *value, &fValue))
391 : {
392 : if (fValue > 0.0)
393 : {
394 : [entity setScaleX:fValue];
395 : return YES;
396 : }
397 : }
398 : break;
399 :
400 : case kVisualEffect_scaleY:
401 : if (JS_ValueToNumber(context, *value, &fValue))
402 : {
403 : if (fValue > 0.0)
404 : {
405 : [entity setScaleY:fValue];
406 : return YES;
407 : }
408 : }
409 : break;
410 :
411 : case kVisualEffect_scaleZ:
412 : if (JS_ValueToNumber(context, *value, &fValue))
413 : {
414 : if (fValue > 0.0)
415 : {
416 : [entity setScaleZ:fValue];
417 : return YES;
418 : }
419 : }
420 : break;
421 :
422 : case kVisualEffect_hullHeatLevel:
423 : if (JS_ValueToNumber(context, *value, &fValue))
424 : {
425 : [entity setHullHeatLevel:fValue];
426 : return YES;
427 : }
428 : break;
429 :
430 : case kVisualEffect_shaderFloat1:
431 : if (JS_ValueToNumber(context, *value, &fValue))
432 : {
433 : [entity setShaderFloat1:fValue];
434 : return YES;
435 : }
436 : break;
437 :
438 : case kVisualEffect_shaderFloat2:
439 : if (JS_ValueToNumber(context, *value, &fValue))
440 : {
441 : [entity setShaderFloat2:fValue];
442 : return YES;
443 : }
444 : break;
445 :
446 : case kVisualEffect_shaderInt1:
447 : if (JS_ValueToInt32(context, *value, &iValue))
448 : {
449 : [entity setShaderInt1:iValue];
450 : return YES;
451 : }
452 : break;
453 :
454 : case kVisualEffect_shaderInt2:
455 : if (JS_ValueToInt32(context, *value, &iValue))
456 : {
457 : [entity setShaderInt2:iValue];
458 : return YES;
459 : }
460 : break;
461 :
462 : case kVisualEffect_shaderVector1:
463 : if (JSValueToVector(context, *value, &vValue))
464 : {
465 : [entity setShaderVector1:vValue];
466 : return YES;
467 : }
468 : break;
469 :
470 : case kVisualEffect_shaderVector2:
471 : if (JSValueToVector(context, *value, &vValue))
472 : {
473 : [entity setShaderVector2:vValue];
474 : return YES;
475 : }
476 : break;
477 :
478 : default:
479 : OOJSReportBadPropertySelector(context, this, propID, sVisualEffectProperties);
480 : return NO;
481 : }
482 :
483 : OOJSReportBadPropertyValue(context, this, propID, sVisualEffectProperties, *value);
484 : return NO;
485 :
486 : OOJS_NATIVE_EXIT
487 : }
488 :
489 :
490 : // *** Methods ***
491 :
492 0 : #define GET_THIS_EFFECT(THISENT) do { \
493 : if (EXPECT_NOT(!JSVisualEffectGetVisualEffectEntity(context, OOJS_THIS, &THISENT))) return NO; /* Exception */ \
494 : if (OOIsStaleEntity(THISENT)) OOJS_RETURN_VOID; \
495 : } while (0)
496 :
497 :
498 0 : static JSBool VisualEffectRemove(JSContext *context, uintN argc, jsval *vp)
499 : {
500 : OOJS_NATIVE_ENTER(context)
501 :
502 : OOVisualEffectEntity *thisEnt = nil;
503 : GET_THIS_EFFECT(thisEnt);
504 :
505 : if ([thisEnt isSubEntity])
506 : {
507 : OOVisualEffectEntity *parent = [thisEnt owner];
508 : [parent removeSubEntity:thisEnt];
509 : }
510 : else
511 : {
512 : [thisEnt remove];
513 : }
514 :
515 : OOJS_RETURN_VOID;
516 :
517 : OOJS_NATIVE_EXIT
518 : }
519 :
520 :
521 : //getMaterials()
522 0 : static JSBool VisualEffectGetMaterials(JSContext *context, uintN argc, jsval *vp)
523 : {
524 : OOJS_PROFILE_ENTER
525 :
526 : NSObject *result = nil;
527 : OOVisualEffectEntity *thisEnt = nil;
528 :
529 : GET_THIS_EFFECT(thisEnt);
530 :
531 : result = [[thisEnt mesh] materials];
532 : if (result == nil) result = [NSDictionary dictionary];
533 : OOJS_RETURN_OBJECT(result);
534 :
535 : OOJS_PROFILE_EXIT
536 : }
537 :
538 : //getShaders()
539 0 : static JSBool VisualEffectGetShaders(JSContext *context, uintN argc, jsval *vp)
540 : {
541 : OOJS_PROFILE_ENTER
542 :
543 : NSObject *result = nil;
544 : OOVisualEffectEntity *thisEnt = nil;
545 :
546 : GET_THIS_EFFECT(thisEnt);
547 :
548 : result = [[thisEnt mesh] shaders];
549 : if (result == nil) result = [NSDictionary dictionary];
550 : OOJS_RETURN_OBJECT(result);
551 :
552 : OOJS_PROFILE_EXIT
553 : }
554 :
555 :
556 : // setMaterials(params: dict, [shaders: dict]) // sets materials dictionary. Optional parameter sets the shaders dictionary too.
557 0 : static JSBool VisualEffectSetMaterials(JSContext *context, uintN argc, jsval *vp)
558 : {
559 : OOJS_NATIVE_ENTER(context)
560 :
561 : OOVisualEffectEntity *thisEnt = nil;
562 :
563 : if (argc < 1)
564 : {
565 : OOJSReportBadArguments(context, @"VisualEffect", @"setMaterials", 0, OOJS_ARGV, nil, @"parameter object");
566 : return NO;
567 : }
568 :
569 : GET_THIS_EFFECT(thisEnt);
570 :
571 : return VisualEffectSetMaterialsInternal(context, argc, vp, thisEnt, NO);
572 :
573 : OOJS_NATIVE_EXIT
574 : }
575 :
576 :
577 : // setShaders(params: dict)
578 0 : static JSBool VisualEffectSetShaders(JSContext *context, uintN argc, jsval *vp)
579 : {
580 : OOJS_NATIVE_ENTER(context)
581 :
582 : OOVisualEffectEntity *thisEnt = nil;
583 :
584 : GET_THIS_EFFECT(thisEnt);
585 :
586 : if (argc < 1)
587 : {
588 : OOJSReportBadArguments(context, @"VisualEffect", @"setShaders", 0, OOJS_ARGV, nil, @"parameter object");
589 : return NO;
590 : }
591 :
592 : if (JSVAL_IS_NULL(OOJS_ARGV[0]) || (!JSVAL_IS_NULL(OOJS_ARGV[0]) && !JSVAL_IS_OBJECT(OOJS_ARGV[0])))
593 : {
594 : // EMMSTRAN: JS_ValueToObject() and normal error handling here.
595 : OOJSReportWarning(context, @"VisualEffect.%@: expected %@ instead of '%@'.", @"setShaders", @"object", OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[0]));
596 : OOJS_RETURN_BOOL(NO);
597 : }
598 :
599 : OOJS_ARGV[1] = OOJS_ARGV[0];
600 : return VisualEffectSetMaterialsInternal(context, argc, vp, thisEnt, YES);
601 :
602 : OOJS_NATIVE_EXIT
603 : }
604 :
605 :
606 :
607 :
608 :
609 : /* ** helper functions ** */
610 :
611 0 : static JSBool VisualEffectSetMaterialsInternal(JSContext *context, uintN argc, jsval *vp, OOVisualEffectEntity *thisEnt, BOOL fromShaders)
612 : {
613 : OOJS_PROFILE_ENTER
614 :
615 : JSObject *params = NULL;
616 : NSDictionary *materials;
617 : NSDictionary *shaders;
618 : BOOL withShaders = NO;
619 : BOOL success = NO;
620 :
621 : GET_THIS_EFFECT(thisEnt);
622 :
623 : if (JSVAL_IS_NULL(OOJS_ARGV[0]) || (!JSVAL_IS_NULL(OOJS_ARGV[0]) && !JSVAL_IS_OBJECT(OOJS_ARGV[0])))
624 : {
625 : OOJSReportWarning(context, @"VisualEffect.%@: expected %@ instead of '%@'.", @"setMaterials", @"object", OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[0]));
626 : OOJS_RETURN_BOOL(NO);
627 : }
628 :
629 : if (argc > 1)
630 : {
631 : withShaders = YES;
632 : if (JSVAL_IS_NULL(OOJS_ARGV[1]) || (!JSVAL_IS_NULL(OOJS_ARGV[1]) && !JSVAL_IS_OBJECT(OOJS_ARGV[1])))
633 : {
634 : OOJSReportWarning(context, @"VisualEffect.%@: expected %@ instead of '%@'.", @"setMaterials", @"object as second parameter", OOStringFromJSValueEvenIfNull(context, OOJS_ARGV[1]));
635 : withShaders = NO;
636 : }
637 : }
638 :
639 : if (fromShaders)
640 : {
641 : materials = [[thisEnt mesh] materials];
642 : params = JSVAL_TO_OBJECT(OOJS_ARGV[0]);
643 : shaders = OOJSNativeObjectFromJSObject(context, params);
644 : }
645 : else
646 : {
647 : params = JSVAL_TO_OBJECT(OOJS_ARGV[0]);
648 : materials = OOJSNativeObjectFromJSObject(context, params);
649 : if (withShaders)
650 : {
651 : params = JSVAL_TO_OBJECT(OOJS_ARGV[1]);
652 : shaders = OOJSNativeObjectFromJSObject(context, params);
653 : }
654 : else
655 : {
656 : shaders = [[thisEnt mesh] shaders];
657 : }
658 : }
659 :
660 : OOJS_BEGIN_FULL_NATIVE(context)
661 : NSDictionary *effectDict = [thisEnt effectInfoDictionary];
662 :
663 : // First we test to see if we can create the mesh.
664 : OOMesh *mesh = [OOMesh meshWithName:[effectDict oo_stringForKey:@"model"]
665 : cacheKey:nil
666 : materialDictionary:materials
667 : shadersDictionary:shaders
668 : smooth:[effectDict oo_boolForKey:@"smooth" defaultValue:NO]
669 : shaderMacros:[[ResourceManager materialDefaults] oo_dictionaryForKey:@"ship-prefix-macros"]
670 : shaderBindingTarget:thisEnt];
671 :
672 : if (mesh != nil)
673 : {
674 : [thisEnt setMesh:mesh];
675 : success = YES;
676 : }
677 : OOJS_END_FULL_NATIVE
678 :
679 : OOJS_RETURN_BOOL(success);
680 :
681 : OOJS_PROFILE_EXIT
682 : }
683 :
684 0 : static JSBool VisualEffectScale(JSContext *context, uintN argc, jsval *vp)
685 : {
686 : OOJS_NATIVE_ENTER(context)
687 :
688 : OOVisualEffectEntity *thisEnt = nil;
689 : GET_THIS_EFFECT(thisEnt);
690 : jsdouble scale;
691 : BOOL gotScale;
692 :
693 : if (argc < 1)
694 : {
695 : OOJSReportBadArguments(context, @"VisualEffect", @"scale", argc, OOJS_ARGV, nil, @"scale factor needed");
696 : return NO;
697 : }
698 :
699 : gotScale = JS_ValueToNumber(context, OOJS_ARGV[0], &scale);
700 : if (EXPECT_NOT(scale <= 0.0 || !gotScale))
701 : {
702 : OOJSReportBadArguments(context, @"VisualEffect", @"scale", argc, OOJS_ARGV, nil, @"scale factor must be positive");
703 : return NO;
704 : }
705 :
706 : // set all three scales
707 : [thisEnt setScaleX:scale];
708 : [thisEnt setScaleY:scale];
709 : [thisEnt setScaleZ:scale];
710 :
711 : return YES;
712 : OOJS_NATIVE_EXIT
713 :
714 : }
715 :
716 :
717 : // restoreSubEntities(): boolean
718 0 : static JSBool VisualEffectRestoreSubEntities(JSContext *context, uintN argc, jsval *vp)
719 : {
720 : OOJS_NATIVE_ENTER(context)
721 :
722 : OOVisualEffectEntity *thisEnt = nil;
723 : NSUInteger numSubEntitiesRestored = 0U;
724 :
725 : GET_THIS_EFFECT(thisEnt);
726 :
727 : NSUInteger subCount = [[thisEnt subEntitiesForScript] count];
728 :
729 : [thisEnt clearSubEntities];
730 : [thisEnt setUpSubEntities];
731 :
732 : if ([[thisEnt subEntitiesForScript] count] - subCount > 0) numSubEntitiesRestored = [[thisEnt subEntitiesForScript] count] - subCount;
733 :
734 : OOJS_RETURN_BOOL(numSubEntitiesRestored > 0);
735 :
736 : OOJS_NATIVE_EXIT
737 : }
738 :
|