Oolite
Loading...
Searching...
No Matches
OOJSPlanet.m
Go to the documentation of this file.
1/*
2
3OOJSPlanet.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSPlanet.h"
27#import "OOJSEntity.h"
28#import "OOJSQuaternion.h"
29#import "OOJSVector.h"
31
32static JSObject* sPlanetPrototype;
33
34static JSBool PlanetGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
35static JSBool PlanetSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
36
37static JSClass sPlanetClass = {
38 "Planet",
39 JSCLASS_HAS_PRIVATE,
40
41 JS_PropertyStub, // addProperty
42 JS_PropertyStub, // delProperty
43 PlanetGetProperty, // getProperty
44 PlanetSetProperty, // setProperty
45 JS_EnumerateStub, // enumerate
46 JS_ResolveStub, // resolve
47 JS_ConvertStub, // convert
48 OOJSObjectWrapperFinalize, // finalize
49 JSCLASS_NO_OPTIONAL_MEMBERS
50};
51
52enum {
53 // Property IDs
54 kPlanet_airColor, // air color, read/write
55 kPlanet_airColorMixRatio, // air color mix ratio, float, read/write
56 kPlanet_airDensity, // air density, float, read/write
58 kPlanet_illuminationColor, // illumination color, read/write
59 kPlanet_isMainPlanet, // Is [UNIVERSE planet], boolean, read-only
60 kPlanet_name, // Name of planet, string, read/write
61 kPlanet_radius, // Radius of planet in metres, read-only
62 kPlanet_texture, // Planet texture read / write
63 kPlanet_orientation, // orientation, quaternion, read/write
66};
67
68static JSPropertySpec sPlanetProperties[] = {
69 // JS name ID flags
71 { "airColorMixRatio", kPlanet_airColorMixRatio, OOJS_PROP_READWRITE_CB },
73 { "hasAtmosphere", kPlanet_hasAtmosphere, OOJS_PROP_READONLY_CB },
74 { "illuminationColor", kPlanet_illuminationColor, OOJS_PROP_READWRITE_CB },
78 { "rotationalVelocity", kPlanet_rotationalVelocity, OOJS_PROP_READWRITE_CB },
80 { "orientation", kPlanet_orientation, OOJS_PROP_READWRITE_CB }, // Not documented since it's inherited from Entity
81 { "terminatorThresholdVector", kPlanet_terminatorThresholdVector, OOJS_PROP_READWRITE_CB },
82 { 0 }
83};
84
85DEFINE_JS_OBJECT_GETTER(JSPlanetGetPlanetEntity, &sPlanetClass, sPlanetPrototype, OOPlanetEntity)
86
93
94@implementation OOPlanetEntity (OOJavaScriptExtensions)
95
97{
98 OOStellarBodyType type = [self planetType];
99 return type == STELLAR_TYPE_NORMAL_PLANET || type == STELLAR_TYPE_MOON;
100}
101
102- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
103{
104 *outClass = &sPlanetClass;
105 *outPrototype = sPlanetPrototype;
106}
107
108- (NSString*)oo_jsClassName
109{
110 switch ([self planetType]) {
112 return @"Planet";
114 return @"Moon";
115 default:
116 return @"Unknown";
117 }
118}
119
120@end
121
122static JSBool PlanetGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
123{
124 if (!JSID_IS_INT(propID))
125 return YES;
126
127 OOJS_NATIVE_ENTER(context)
128
129 OOPlanetEntity* planet = nil;
130 if (!JSPlanetGetPlanetEntity(context, this, &planet))
131 return NO;
132
133 switch (JSID_TO_INT(propID)) {
134 case kPlanet_airColor:
135 *value = OOJSValueFromNativeObject(context, [[planet airColor] normalizedArray]);
136 return YES;
137
139 return JS_NewNumberValue(context, [planet airColorMixRatio], value);
140
142 return JS_NewNumberValue(context, [planet airDensity], value);
143
145 *value = OOJSValueFromNativeObject(context, [[planet illuminationColor] normalizedArray]);
146 return YES;
147
149 *value = OOJSValueFromBOOL(planet == (id)[UNIVERSE planet]);
150 return YES;
151
152 case kPlanet_radius:
153 return JS_NewNumberValue(context, [planet radius], value);
154
156 *value = OOJSValueFromBOOL([planet hasAtmosphere]);
157 return YES;
158
159 case kPlanet_texture:
160 *value = OOJSValueFromNativeObject(context, [planet textureFileName]);
161 return YES;
162
163 case kPlanet_name:
164 *value = OOJSValueFromNativeObject(context, [planet name]);
165 return YES;
166
168 return QuaternionToJSValue(context, [planet normalOrientation], value);
169
171 return JS_NewNumberValue(context, [planet rotationalVelocity], value);
172
174 return VectorToJSValue(context, [planet terminatorThresholdVector], value);
175
176 default:
177 OOJSReportBadPropertySelector(context, this, propID, sPlanetProperties);
178 return NO;
179 }
180
182}
183
184static JSBool PlanetSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
185{
186 if (!JSID_IS_INT(propID))
187 return YES;
188
189 OOJS_NATIVE_ENTER(context)
190
191 OOPlanetEntity* planet = nil;
192 NSString* sValue = nil;
193 Quaternion qValue;
194 Vector vValue;
195 jsdouble dValue;
196 OOColor* colorForScript = nil;
197
198 if (!JSPlanetGetPlanetEntity(context, this, &planet))
199 return NO;
200
201 switch (JSID_TO_INT(propID)) {
202 case kPlanet_airColor:
203 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
204 if (colorForScript != nil || JSVAL_IS_NULL(*value)) {
205 [planet setAirColor:colorForScript];
206 return YES;
207 }
208 break;
209
211 if (JS_ValueToNumber(context, *value, &dValue)) {
212 [planet setAirColorMixRatio:dValue];
213 return YES;
214 }
215 break;
216
218 if (JS_ValueToNumber(context, *value, &dValue)) {
219 [planet setAirDensity:dValue];
220 return YES;
221 }
222 break;
223
225 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, *value)];
226 if (colorForScript != nil || JSVAL_IS_NULL(*value)) {
227 [planet setIlluminationColor:colorForScript];
228 return YES;
229 }
230 break;
231
232 case kPlanet_name:
233 sValue = OOStringFromJSValue(context, *value);
234 [planet setName:sValue];
235 return YES;
236
237 case kPlanet_texture: {
238 BOOL OK = NO;
239 sValue = OOStringFromJSValue(context, *value);
240
242
243 if ([planet isKindOfClass:[OOPlanetEntity class]]) {
244 if (sValue == nil) {
245 OOJSReportWarning(context, @"Expected texture string. Value not set.");
246 } else {
247 OK = YES;
248 }
249 }
250
251 if (OK) {
252 OK = [planet setUpPlanetFromTexture:sValue];
253 if (!OK)
254 OOJSReportWarning(context, @"Cannot find texture \"%@\". Value not set.", sValue);
255 }
256
258
259 return YES; // Even if !OK, no exception was raised.
260 }
261
263 if (JSValueToQuaternion(context, *value, &qValue)) {
264 quaternion_normalize(&qValue);
265 [planet setOrientation:qValue];
266 return YES;
267 }
268 break;
269
271 if (JS_ValueToNumber(context, *value, &dValue)) {
272 [planet setRotationalVelocity:dValue];
273 return YES;
274 }
275 break;
276
278 if (JSValueToVector(context, *value, &vValue)) {
279 [planet setTerminatorThresholdVector:vValue];
280 return YES;
281 }
282 break;
283
284 default:
285 OOJSReportBadPropertySelector(context, this, propID, sPlanetProperties);
286 return NO;
287 }
288
289 OOJSReportBadPropertyValue(context, this, propID, sPlanetProperties, *value);
290 return NO;
291
293}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
void InitOOJSPlanet(JSContext *context, JSObject *global)
Definition OOJSPlanet.m:87
static JSClass sPlanetClass
Definition OOJSPlanet.m:37
static JSObject * sPlanetPrototype
Definition OOJSPlanet.m:32
static JSPropertySpec sPlanetProperties[]
Definition OOJSPlanet.m:68
@ kPlanet_airColor
Definition OOJSPlanet.m:54
@ kPlanet_orientation
Definition OOJSPlanet.m:63
@ kPlanet_radius
Definition OOJSPlanet.m:61
@ kPlanet_airColorMixRatio
Definition OOJSPlanet.m:55
@ kPlanet_isMainPlanet
Definition OOJSPlanet.m:59
@ kPlanet_terminatorThresholdVector
Definition OOJSPlanet.m:65
@ kPlanet_airDensity
Definition OOJSPlanet.m:56
@ kPlanet_name
Definition OOJSPlanet.m:60
@ kPlanet_rotationalVelocity
Definition OOJSPlanet.m:64
@ kPlanet_hasAtmosphere
Definition OOJSPlanet.m:57
@ kPlanet_illuminationColor
Definition OOJSPlanet.m:58
@ kPlanet_texture
Definition OOJSPlanet.m:62
static JSBool PlanetGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSPlanet.m:122
static JSBool PlanetSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSPlanet.m:184
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
BOOL QuaternionToJSValue(JSContext *context, Quaternion quaternion, jsval *outValue) NONNULL_FUNC
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
void OOJSPauseTimeLimiter(void)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#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)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
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)
#define OOJS_PROP_READONLY_CB
void OOJSResumeTimeLimiter(void)
return nil
OOINLINE void quaternion_normalize(Quaternion *quat) NONNULL_FUNC ALWAYS_INLINE_FUNC
OOStellarBodyType
@ STELLAR_TYPE_MOON
@ STELLAR_TYPE_NORMAL_PLANET
#define UNIVERSE
Definition Universe.h:827
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135